Skip to content
Snippets Groups Projects
pnd.go 4.61 KiB
Newer Older
  • Learn to ignore specific revisions
  • Manuel Kieweg's avatar
    Manuel Kieweg committed
    package api
    
    import (
    	ppb "code.fbi.h-da.de/cocsn/api/go/gosdn/pnd"
    	tpb "code.fbi.h-da.de/cocsn/api/go/gosdn/transport"
    	"code.fbi.h-da.de/cocsn/gosdn/interfaces/change"
    	"code.fbi.h-da.de/cocsn/gosdn/interfaces/device"
    	"code.fbi.h-da.de/cocsn/gosdn/interfaces/networkdomain"
    	"code.fbi.h-da.de/cocsn/gosdn/interfaces/southbound"
    	"code.fbi.h-da.de/cocsn/gosdn/interfaces/store"
    	"code.fbi.h-da.de/cocsn/gosdn/nucleus/errors"
    	"github.com/google/uuid"
    	log "github.com/sirupsen/logrus"
    )
    
    type PrincipalNetworkDomainAdapter struct {
    	id       uuid.UUID
    	endpoint string
    }
    
    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
    }
    
    func (p *PrincipalNetworkDomainAdapter) Destroy() error {
    	return &errors.ErrNotYetImplemented{}
    }
    
    func (p *PrincipalNetworkDomainAdapter) AddSbi(s southbound.SouthboundInterface) error {
    	return &errors.ErrNotYetImplemented{}
    }
    
    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
    }
    
    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
    }
    
    func (p *PrincipalNetworkDomainAdapter) Devices() []uuid.UUID {
    	return nil
    }
    
    func (p *PrincipalNetworkDomainAdapter) ChangeOND(uuid uuid.UUID, operation ppb.ApiOperation, path string, value ...string) error {
    	var v string
    	if len(value) != 0 {
    		v = value[0]
    	}
    	resp, err := changeRequest(p.endpoint, uuid.String(), p.id.String(), path, v, operation)
    	if err != nil {
    		return err
    	}
    	log.Info(resp)
    	return err
    }
    
    func (p *PrincipalNetworkDomainAdapter) Request(did uuid.UUID, path string) error {
    	resp, err := getPath(p.endpoint, p.id.String(), did.String(), path)
    	if err != nil {
    		return err
    	}
    	log.Info(resp)
    	return nil
    }
    
    func (p *PrincipalNetworkDomainAdapter) RequestAll(string) error {
    	return &errors.ErrNotYetImplemented{}
    }
    
    func (p *PrincipalNetworkDomainAdapter) GetName() string {
    	return ""
    }
    
    func (p *PrincipalNetworkDomainAdapter) GetDescription() string {
    	return ""
    }
    
    func (p *PrincipalNetworkDomainAdapter) MarshalDevice(string) (string, error) {
    	return "", &errors.ErrNotYetImplemented{}
    }
    
    func (p *PrincipalNetworkDomainAdapter) ContainsDevice(uuid.UUID) bool {
    	return false
    }
    
    func (p *PrincipalNetworkDomainAdapter) GetSBIs() store.Store {
    	return nil
    }
    
    func (p *PrincipalNetworkDomainAdapter) ID() uuid.UUID {
    	return p.id
    }
    
    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)
    }
    
    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)
    }
    
    func (p *PrincipalNetworkDomainAdapter) GetChange(uuid.UUID, ...int) (change.Change, error) {
    	return nil, &errors.ErrNotYetImplemented{}
    }
    
    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
    }
    
    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 {
    		if ch.State == ppb.Change_PENDING {
    			id, _ := uuid.Parse(ch.Id)
    			changes = append(changes, id)
    		}
    	}
    	return changes
    }