Skip to content
Snippets Groups Projects
principalNetworkDomain.go 1.97 KiB
Newer Older
  • Learn to ignore specific revisions
  • 	"github.com/google/uuid"
    )
    
    // PrincipalNetworkDomain provides an
    // interface for PND implementations
    type PrincipalNetworkDomain interface {
    	Destroy() error
    	AddSbi() error
    	RemoveSbi() error
    
    	AddDevice(*Device) error
    	RemoveDevice(uuid.UUID) error
    	Request(uuid.UUID, string) error
    	RequestAll(string) error
    
    }
    
    type pndImplementation struct {
    	name    string
    	sbi     map[string]SouthboundInterface
    
    	devices map[uuid.UUID]*Device
    
    }
    
    //NewPND creates a Principle Network Domain
    func NewPND(name string, sbi SouthboundInterface) PrincipalNetworkDomain {
    	sbic := make(map[string]SouthboundInterface)
    	sbic["default"] = sbi
    
    	devices := make(map[uuid.UUID]*Device)
    
    	return &pndImplementation{
    		name:    name,
    		sbi:     sbic,
    		devices: devices,
    	}
    }
    
    // Interface satisfaction
    func (pnd *pndImplementation) Destroy() error {
    	return destroy()
    }
    
    func (pnd *pndImplementation) AddSbi() error {
    	return addSbi()
    }
    
    func (pnd *pndImplementation) RemoveSbi() error {
    	return removeSbi()
    }
    
    
    func (pnd *pndImplementation) AddDevice(device *Device) error {
    
    	return pnd.addDevice(device)
    }
    
    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 addSbi() error {
    	return nil
    }
    
    func removeSbi() error {
    	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) Request(uuid uuid.UUID, path string) error {
    	d := pnd.devices[uuid]
    	ctx := context.Background()
    	_, err := d.Transport.Get(ctx, path)
    	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
    }