Skip to content
Snippets Groups Projects

"Overhaul Architecture"

Merged Ghost User requested to merge 67-overhaul-architecture into develop
1 file
+ 0
5
Compare changes
  • Side-by-side
  • Inline
+ 155
0
package nucleus
import (
"context"
"encoding/json"
"github.com/google/uuid"
)
// PrincipalNetworkDomain provides an
// interface for PND implementations
type PrincipalNetworkDomain interface {
Destroy() error
AddSbi(SouthboundInterface) error
RemoveSbi(string) error
AddDevice(*Device) error
RemoveDevice(uuid.UUID) error
Request(uuid.UUID, string) error
RequestAll(string) error
GetName() string
GetDescription() string
MarshalDevice(uuid.UUID) (string, error)
ContainsDevice(uuid.UUID) bool
GetSBIs() map[string]SouthboundInterface
}
type pndImplementation struct {
name string
description string
sbi map[string]SouthboundInterface
devices map[uuid.UUID]*Device
}
//NewPND creates a Principle Network Domain
func NewPND(name, description string, sbi SouthboundInterface) PrincipalNetworkDomain {
sbic := make(map[string]SouthboundInterface)
sbic["default"] = sbi
devices := make(map[uuid.UUID]*Device)
return &pndImplementation{
name: name,
description: description,
sbi: sbic,
devices: devices,
}
}
//GetName returns the name of the PND
func (pnd *pndImplementation) GetName() string {
return pnd.name
}
//HasDevice checks if the given device uuid is registered for this PND
func (pnd *pndImplementation) ContainsDevice(uuid uuid.UUID) bool {
_, exists := pnd.devices[uuid]
return exists
}
//GetDescription returns the current description of the PND
func (pnd *pndImplementation) GetDescription() string {
return pnd.description
}
//GetSBIs returns the registered SBIs
func (pnd *pndImplementation) GetSBIs() map[string]SouthboundInterface {
return pnd.sbi
}
// Interface satisfaction
func (pnd *pndImplementation) Destroy() error {
return destroy()
}
//AddSbi adds a SBI to the PND which will be supported
func (pnd *pndImplementation) AddSbi(sbi SouthboundInterface) error {
return pnd.addSbi(sbi)
}
//AddSbi removes a SBI from the PND
//TODO: this should to recursivly through
//devices and remove the devices using
//this SBI
func (pnd *pndImplementation) RemoveSbi(sbiIdentifier string) error {
return pnd.removeSbi(sbiIdentifier)
}
//AddDevice adds a new device to the PND
func (pnd *pndImplementation) AddDevice(device *Device) error {
return pnd.addDevice(device)
}
//RemoveDevice removes a device from the PND
func (pnd *pndImplementation) RemoveDevice(uuid uuid.UUID) error {
return pnd.removeDevice(uuid)
}
// Actual implementation, bind to struct if
// neccessary
func destroy() error {
return nil
}
func (pnd *pndImplementation) addSbi(sbi SouthboundInterface) error {
pnd.sbi[sbi.SbiIdentifier()] = sbi
return nil
}
func (pnd *pndImplementation) removeSbi(sbiIdentifier string) error {
delete(pnd.sbi, sbiIdentifier)
return nil
}
func (pnd *pndImplementation) addDevice(device *Device) error {
pnd.devices[device.Config.Uuid] = device
return nil
}
func (pnd *pndImplementation) removeDevice(uuid uuid.UUID) error {
delete(pnd.devices, uuid)
return nil
}
func (pnd *pndImplementation) MarshalDevice(uuid uuid.UUID) (string, error) {
d := pnd.devices[uuid]
json, err := json.MarshalIndent(d.GoStruct, "", "\t")
if err != nil {
return "", err
}
return string(json), nil
}
//Request sends a request for a specific device
func (pnd *pndImplementation) Request(uuid uuid.UUID, path string) error {
d := pnd.devices[uuid]
ctx := context.Background()
res, err := d.Transport.Get(ctx, path)
if err != nil {
return err
}
err = d.Transport.ProcessResponse(res, d.GoStruct, d.SBI.Schema())
if err != nil {
return err
}
return nil
}
//RequestAll sends a request for all registered devices
func (pnd *pndImplementation) RequestAll(path string) error {
for k := range pnd.devices {
if err := pnd.Request(k, path); err != nil {
return err
}
}
return nil
}
Loading