Skip to content
Snippets Groups Projects
principalNetworkDomain.go 3 KiB
Newer Older
  • Learn to ignore specific revisions
  • package nucleus
    
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    import (
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    	"github.com/google/uuid"
    )
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    // PrincipalNetworkDomain provides an
    // interface for PND implementations
    type PrincipalNetworkDomain interface {
    	Destroy() error
    
    Malte Bauch's avatar
    Malte Bauch committed
    	AddSbi(SouthboundInterface) error
    	RemoveSbi(string) error
    
    	AddDevice(*Device) error
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    	RemoveDevice(uuid uuid.UUID) error
    
    	Request(uuid.UUID, string) error
    	RequestAll(string) error
    
    	GetDevice(uuid.UUID) (*Device, bool)
    
    type pndImplementation struct {
    
    	name        string
    	description string
    	sbi         map[string]SouthboundInterface
    	devices     map[uuid.UUID]*Device
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    //NewPND creates a Principle Network Domain
    
    Malte Bauch's avatar
    Malte Bauch committed
    func NewPND(name, description string, sbi SouthboundInterface) PrincipalNetworkDomain {
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    	sbic := make(map[string]SouthboundInterface)
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    	sbic["default"] = sbi
    
    	devices := make(map[uuid.UUID]*Device)
    
    	return &pndImplementation{
    
    		name:        name,
    		description: description,
    		sbi:         sbic,
    		devices:     devices,
    
    Malte Bauch's avatar
    Malte Bauch committed
    func (pnd *pndImplementation) GetName() string {
    
    	return pnd.name
    
    func (pnd *pndImplementation) GetDevice(uuid uuid.UUID) (*Device, bool) {
    	return pnd.getDevice(uuid)
    
    func (pnd *pndImplementation) GetDescription() string {
    
    	return pnd.description
    
    func (pnd *pndImplementation) GetSBIs() map[string]SouthboundInterface {
    
    	return pnd.sbi
    
    // Interface satisfaction
    func (pnd *pndImplementation) Destroy() error {
    	return destroy()
    }
    
    
    Malte Bauch's avatar
    Malte Bauch committed
    func (pnd *pndImplementation) AddSbi(sbi SouthboundInterface) error {
    	return pnd.addSbi(sbi)
    
    Malte Bauch's avatar
    Malte Bauch committed
    func (pnd *pndImplementation) RemoveSbi(sbiIdentifier string) error {
    	return pnd.removeSbi(sbiIdentifier)
    
    func (pnd *pndImplementation) AddDevice(device *Device) error {
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    	return pnd.addDevice(device)
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    }
    
    func (pnd *pndImplementation) RemoveDevice(uuid uuid.UUID) error {
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    	return pnd.removeDevice(uuid)
    
    // Actual implementation, bind to struct if
    // neccessary
    func destroy() error {
    	return nil
    }
    
    
    Malte Bauch's avatar
    Malte Bauch committed
    func (pnd *pndImplementation) addSbi(sbi SouthboundInterface) error {
    
    	pnd.sbi[sbi.SbiIdentifier()] = sbi
    
    Malte Bauch's avatar
    Malte Bauch committed
    func (pnd *pndImplementation) removeSbi(sbiIdentifier string) error {
    
    	delete(pnd.sbi, sbiIdentifier)
    
    func (pnd *pndImplementation) addDevice(device *Device) error {
    
    	pnd.devices[device.Config.Uuid] = device
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    	return nil
    }
    
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    func (pnd *pndImplementation) removeDevice(uuid uuid.UUID) error {
    
    	delete(pnd.devices, uuid)
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    	return nil
    }
    
    func (pnd *pndImplementation) getDevice(uuid uuid.UUID) (*Device, bool) {
    	device, exists := pnd.devices[uuid]
    	return device, exists
    }
    
    
    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
    }
    
    func (pnd *pndImplementation) RequestAll(path string) error {
    
    	for k := range pnd.devices {
    
    		if err := pnd.Request(k, path); err != nil {
    			return err
    		}
    	}
    	return nil
    }