Skip to content
Snippets Groups Projects
principalNetworkDomain.go 4.58 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
    
    	RemoveDevice(uuid.UUID) error
    
    	Request(uuid.UUID, string) error
    	RequestAll(string) error
    
    	MarshalDevice(uuid.UUID) (string, error)
    	ContainsDevice(uuid.UUID) bool
    
    type pndImplementation struct {
    
    	name        string
    	description string
    	sbi         map[string]SouthboundInterface
    	devices     map[uuid.UUID]*Device
    
    // 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,
    
    // GetName returns the name of the PND
    
    Malte Bauch's avatar
    Malte Bauch committed
    func (pnd *pndImplementation) GetName() string {
    
    	return pnd.name
    
    // ContainsDevice 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
    
    // Destroy destroys the PND
    
    func (pnd *pndImplementation) Destroy() error {
    	return destroy()
    }
    
    
    // AddSbi adds a SBI to the PND which will be supported
    
    Malte Bauch's avatar
    Malte Bauch committed
    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
    
    Malte Bauch's avatar
    Malte Bauch committed
    func (pnd *pndImplementation) RemoveSbi(sbiIdentifier string) error {
    	return pnd.removeSbi(sbiIdentifier)
    
    Malte Bauch's avatar
    Malte Bauch committed
    //AddDevice adds a new device to the PND
    
    func (pnd *pndImplementation) AddDevice(device *Device) error {
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    	return pnd.addDevice(device)
    
    // RemoveDevice removes a device from the PND
    
    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
    }
    
    
    func (pnd *pndImplementation) getDevice(uuid uuid.UUID) (*Device, error) {
    	d, ok := pnd.devices[uuid]
    	if !ok {
    
    		return nil, &ErrNotFound{id: uuid}
    
    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) MarshalDevice(uuid uuid.UUID) (string, error) {
    
    	d, err := pnd.getDevice(uuid)
    	if err != nil {
    		return "", err
    	}
    	jsonTree, err := json.MarshalIndent(d.GoStruct, "", "\t")
    
    	return string(jsonTree), nil
    
    Malte Bauch's avatar
    Malte Bauch committed
    //Request sends a request for a specific device
    
    func (pnd *pndImplementation) Request(uuid uuid.UUID, path string) error {
    
    	d, err := pnd.getDevice(uuid)
    	if err != nil {
    		return err
    	}
    
    	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
    }
    
    
    Malte Bauch's avatar
    Malte Bauch committed
    //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
    }
    
    
    type pndStorage map[uuid.UUID]PrincipalNetworkDomain
    
    func (p pndStorage) exists(id uuid.UUID) bool {
    	_, ok := p[id]
    	return ok
    }
    
    func (p pndStorage) add(pnd PrincipalNetworkDomain) error {
    	// TODO: Implement duplicate detection. Changes PrincipalNetworkDomain API
    	p[uuid.New()] = pnd
    	return nil
    }
    
    func (p pndStorage) Sbi(id uuid.UUID) (PrincipalNetworkDomain, error) {
    	if !p.exists(id) {
    		return nil, &ErrNotFound{id: id}
    	}
    	return p[id], nil
    }
    
    func (p pndStorage) delete(id uuid.UUID) error {
    	if !p.exists(id) {
    		return &ErrNotFound{id: id}
    	}
    	delete(p, id)
    	return nil
    }