Newer
Older
// PrincipalNetworkDomain provides an
// interface for PND implementations
type PrincipalNetworkDomain interface {
Destroy() error
AddSbi(SouthboundInterface) error
RemoveSbi(string) error
RemoveDevice(uuid.UUID) error
Request(uuid.UUID, string) error
RequestAll(string) error
Malte Bauch
committed
GetName() string
GetDescription() string
MarshalDevice(uuid.UUID) (string, error)
ContainsDevice(uuid.UUID) bool
Malte Bauch
committed
GetSBIs() map[string]SouthboundInterface
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 {
devices := make(map[uuid.UUID]*Device)
name: name,
description: description,
sbi: sbic,
devices: devices,
// GetName returns the name of the PND
// 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 {
// GetSBIs returns the registered SBIs
func (pnd *pndImplementation) GetSBIs() map[string]SouthboundInterface {
// Destroy destroys the PND
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)
func (pnd *pndImplementation) AddDevice(device *Device) error {
// RemoveDevice removes a device from the PND
func (pnd *pndImplementation) RemoveDevice(uuid uuid.UUID) error {
// Actual implementation, bind to struct if
// neccessary
func destroy() error {
return nil
}
func (pnd *pndImplementation) addSbi(sbi SouthboundInterface) error {
pnd.sbi[sbi.SbiIdentifier()] = sbi
func (pnd *pndImplementation) removeSbi(sbiIdentifier string) error {
func (pnd *pndImplementation) addDevice(device *Device) error {
pnd.devices[device.Config.Uuid] = device
func (pnd *pndImplementation) getDevice(uuid uuid.UUID) (*Device, error) {
d, ok := pnd.devices[uuid]
if !ok {
return nil, &ErrNotFound{id: uuid}
}
return d, nil
}
func (pnd *pndImplementation) removeDevice(uuid uuid.UUID) error {
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")
if err != nil {
return "", err
}
return string(jsonTree), nil
func (pnd *pndImplementation) Request(uuid uuid.UUID, path string) error {
d, err := pnd.getDevice(uuid)
if err != nil {
return err
}
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 {
if err := pnd.Request(k, path); err != nil {
return err
}
}
return nil
}
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
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
}