Skip to content
Snippets Groups Projects
pnd.go 6.64 KiB
Newer Older
  • Learn to ignore specific revisions
  • Manuel Kieweg's avatar
    Manuel Kieweg committed
    package api
    
    import (
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    	ppb "code.fbi.h-da.de/danet/api/go/gosdn/pnd"
    	tpb "code.fbi.h-da.de/danet/api/go/gosdn/transport"
    	"code.fbi.h-da.de/danet/gosdn/interfaces/change"
    	"code.fbi.h-da.de/danet/gosdn/interfaces/device"
    	"code.fbi.h-da.de/danet/gosdn/interfaces/networkdomain"
    	"code.fbi.h-da.de/danet/gosdn/interfaces/southbound"
    	"code.fbi.h-da.de/danet/gosdn/interfaces/store"
    	"code.fbi.h-da.de/danet/gosdn/nucleus/errors"
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    	"github.com/google/uuid"
    	log "github.com/sirupsen/logrus"
    
    	"google.golang.org/protobuf/proto"
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    // PrincipalNetworkDomainAdapter is an API adapter to reflect the NetworkDomain
    // interface
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    type PrincipalNetworkDomainAdapter struct {
    	id       uuid.UUID
    	endpoint string
    }
    
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    // NewAdapter creates a PND Adapter. It requires a valid PND UUID and a reachable
    // goSDN endpoint.
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    func NewAdapter(id, endpoint string) (networkdomain.NetworkDomain, error) {
    	pid, err := uuid.Parse(id)
    	if err != nil {
    		return nil, err
    	}
    	return &PrincipalNetworkDomainAdapter{
    		id:       pid,
    		endpoint: endpoint,
    	}, nil
    }
    
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    // Destroy destroys the PND Adapter. Currently not implemented
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    func (p *PrincipalNetworkDomainAdapter) Destroy() error {
    	return &errors.ErrNotYetImplemented{}
    }
    
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    // AddSbi adds an SBI to the PND Adapter. Currently not implemented
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    func (p *PrincipalNetworkDomainAdapter) AddSbi(s southbound.SouthboundInterface) error {
    	return &errors.ErrNotYetImplemented{}
    }
    
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    // RemoveSbi removes an SBI from the PND Adapter. Currently not implemented
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    func (p *PrincipalNetworkDomainAdapter) RemoveSbi(uuid.UUID) error {
    	return &errors.ErrNotYetImplemented{}
    }
    
    // AddDevice adds a new device to the controller. The device name is optional.
    // If no name is provided a name will be generated upon device creation.
    func (p *PrincipalNetworkDomainAdapter) AddDevice(name string, opts *tpb.TransportOption, sid uuid.UUID) error {
    	resp, err := addDevice(p.endpoint, name, opts, sid, p.ID())
    	if err != nil {
    		return err
    	}
    	log.Info(resp)
    	return nil
    }
    
    // GetDevice requests one or multiple devices belonging to a given
    // PrincipalNetworkDomain from the controller. If no device identifier
    // is provided, all devices are requested.
    func (p *PrincipalNetworkDomainAdapter) GetDevice(identifier string) (device.Device, error) {
    	resp, err := getDevice(p.endpoint, p.id.String(), identifier)
    	if err != nil {
    		return nil, err
    	}
    	// TODO: Parse into device.Device
    	log.Info(resp)
    	return nil, nil
    }
    
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    // RemoveDevice removes a device from the PND Adapter
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    func (p *PrincipalNetworkDomainAdapter) RemoveDevice(did uuid.UUID) error {
    	resp, err := deleteDevice(p.endpoint, p.id.String(), did.String())
    	if err != nil {
    		return err
    	}
    	log.Info(resp)
    	return nil
    }
    
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    // Devices sends an API call to the controller requesting the UUIDs of all
    // registered devices. Returns nil.
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    func (p *PrincipalNetworkDomainAdapter) Devices() []uuid.UUID {
    	return nil
    }
    
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    // ChangeOND sends an API call to the controller requesting the creation of
    // a change from the provided Operation, path and value. The Change is marked
    // as Pending and times out after the specified timeout period
    
    func (p *PrincipalNetworkDomainAdapter) ChangeOND(duid uuid.UUID, operation ppb.ApiOperation, path string, value ...string) (uuid.UUID, error) {
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    	var v string
    	if len(value) != 0 {
    		v = value[0]
    	}
    
    	resp, err := changeRequest(p.endpoint, duid.String(), p.id.String(), path, v, operation)
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    	if err != nil {
    
    		return uuid.Nil, err
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    	}
    	log.Info(resp)
    
    	return uuid.Nil, err
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    // Request sends an API call to the controller requesting the specified path
    // for the specified device
    
    func (p *PrincipalNetworkDomainAdapter) Request(did uuid.UUID, path string) (proto.Message, error) {
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    	resp, err := getPath(p.endpoint, p.id.String(), did.String(), path)
    	if err != nil {
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    	}
    
    	return resp, nil
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    // RequestAll sends an API call to the controller requesting the specified path
    // for all registered devices. Not yet implemented.
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    func (p *PrincipalNetworkDomainAdapter) RequestAll(string) error {
    	return &errors.ErrNotYetImplemented{}
    }
    
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    // GetName returns the PND Adapter's name
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    func (p *PrincipalNetworkDomainAdapter) GetName() string {
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    	return "PND Adapter"
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    // GetDescription returns the PND Adapter's description
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    func (p *PrincipalNetworkDomainAdapter) GetDescription() string {
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    	return "PND Adapter"
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    // MarshalDevice sends an API call to the controller requesting the specified
    // device as JSON representation. Not yet implemented
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    func (p *PrincipalNetworkDomainAdapter) MarshalDevice(string) (string, error) {
    	return "", &errors.ErrNotYetImplemented{}
    }
    
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    // ContainsDevice sends an API call to the controller checking if a device
    // with the given UUID is present. Not implemented, always returns false
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    func (p *PrincipalNetworkDomainAdapter) ContainsDevice(uuid.UUID) bool {
    	return false
    }
    
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    // GetSBIs sends an API call to the controller requesting the
    // registered SBIs. Not implemented, always returns nil
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    func (p *PrincipalNetworkDomainAdapter) GetSBIs() store.Store {
    	return nil
    }
    
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    // ID returns the PND Adapter's UUID
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    func (p *PrincipalNetworkDomainAdapter) ID() uuid.UUID {
    	return p.id
    }
    
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    // PendingChanges sends an API call to the controller requesting
    // the UUIDs of all pending changes
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    func (p *PrincipalNetworkDomainAdapter) PendingChanges() []uuid.UUID {
    	resp, err := getChanges(p.endpoint, p.id.String())
    	if err != nil {
    		log.Error(err)
    		return nil
    	}
    	return filterChanges(ppb.Change_PENDING, resp)
    }
    
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    // CommittedChanges sends an API call to the controller requesting
    // the UUIDs of all committed changes
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    func (p *PrincipalNetworkDomainAdapter) CommittedChanges() []uuid.UUID {
    	resp, err := getChanges(p.endpoint, p.id.String())
    	if err != nil {
    		log.Error(err)
    		return nil
    	}
    	return filterChanges(ppb.Change_COMMITTED, resp)
    }
    
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    // GetChange sends an API call to the controller requesting the specified change
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    func (p *PrincipalNetworkDomainAdapter) GetChange(uuid.UUID) (change.Change, error) {
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    	return nil, &errors.ErrNotYetImplemented{}
    }
    
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    // Commit sends an API call to the controller committing the specified change
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    func (p *PrincipalNetworkDomainAdapter) Commit(cuid uuid.UUID) error {
    	resp, err := commit(p.endpoint, p.id.String(), cuid.String())
    	if err != nil {
    		return err
    	}
    	log.Info(resp)
    	return nil
    }
    
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    // Confirm sends an API call to the controller confirming the specified change
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    func (p *PrincipalNetworkDomainAdapter) Confirm(cuid uuid.UUID) error {
    	resp, err := confirm(p.endpoint, p.id.String(), cuid.String())
    	if err != nil {
    		return err
    	}
    	log.Info(resp)
    	return nil
    }
    
    func filterChanges(state ppb.Change_State, resp *ppb.GetResponse) []uuid.UUID {
    	changes := make([]uuid.UUID, 0)
    	for _, ch := range resp.Change {
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    		if ch.State == state {
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    			id, _ := uuid.Parse(ch.Id)
    			changes = append(changes, id)
    		}
    	}
    	return changes
    }