Skip to content
Snippets Groups Projects
device.go 3.34 KiB
Newer Older
  • Learn to ignore specific revisions
  • package server
    
    import (
    	"context"
    	"time"
    
    	dpb "code.fbi.h-da.de/danet/gosdn/api/go/gosdn/device"
    	"code.fbi.h-da.de/danet/gosdn/controller/interfaces/networkdomain"
    	"github.com/google/uuid"
    	log "github.com/sirupsen/logrus"
    	"google.golang.org/grpc/codes"
    	"google.golang.org/grpc/status"
    )
    
    // DeviceServer represents a deviceServer.
    type DeviceServer struct {
    	dpb.UnimplementedDeviceServiceServer
    	networkDomain networkdomain.NetworkDomain
    }
    
    // NewDeviceServer returns a new DeviceServer.
    func NewDeviceServer(networkDomain networkdomain.NetworkDomain) *DeviceServer {
    	return &DeviceServer{
    		networkDomain: networkDomain,
    	}
    }
    
    // Add adds a new device.
    func (d *DeviceServer) Add(ctx context.Context, request *dpb.AddDeviceRequest) (*dpb.AddDeviceResponse, error) {
    	sbiID, err := uuid.Parse(request.Device.Sbi.Id)
    	if err != nil {
    		return nil, status.Errorf(codes.Aborted, "%v", err)
    	}
    
    	id, err := d.networkDomain.AddDevice(
    		request.Device.DeviceName,
    		request.Device.TransportOption,
    		sbiID,
    	)
    	if err != nil {
    		return nil, status.Errorf(codes.Aborted, "%v", err)
    	}
    
    	return &dpb.AddDeviceResponse{
    		Timestamp: time.Now().UnixNano(),
    		Status:    dpb.Status_STATUS_OK,
    		DeviceId:  id.String(),
    	}, nil
    }
    
    // GetAll returns all stored devices.
    func (d *DeviceServer) GetAll(ctx context.Context, request *dpb.GetAllDeviceRequest) (*dpb.GetAllDeviceResponse, error) {
    	devices := d.networkDomain.Devices()
    
    	onds := []*dpb.Device{}
    	for _, device := range devices {
    		ygotStructAsJSON, err := device.GetModelAsString()
    		if err != nil {
    			log.Error(err)
    			return nil, status.Errorf(codes.Aborted, "%v", err)
    		}
    
    		onds = append(onds, &dpb.Device{
    			Id:    device.ID().String(),
    			Name:  device.Name(),
    			Model: ygotStructAsJSON,
    		})
    	}
    
    	return &dpb.GetAllDeviceResponse{
    		Timestamp: time.Now().UnixNano(),
    		Status:    dpb.Status_STATUS_OK,
    		Device:    onds,
    	}, nil
    }
    
    // Get returns a device.
    func (d *DeviceServer) Get(ctx context.Context, request *dpb.GetDeviceRequest) (*dpb.GetDeviceResponse, error) {
    	device, err := d.networkDomain.GetDevice(request.DeviceID)
    	if err != nil {
    		return nil, status.Errorf(codes.Aborted, "%v", err)
    	}
    
    	ygotStructAsJSON, err := device.GetModelAsString()
    	if err != nil {
    		log.Error(err)
    		return nil, status.Errorf(codes.Aborted, "%v", err)
    	}
    
    	ond := &dpb.Device{
    		Id:    device.ID().String(),
    		Name:  device.Name(),
    		Model: ygotStructAsJSON,
    	}
    
    	return &dpb.GetDeviceResponse{
    		Timestamp: time.Now().UnixNano(),
    		Status:    dpb.Status_STATUS_OK,
    		Device:    ond,
    	}, nil
    }
    
    // Update updates a device.
    func (d *DeviceServer) Update(ctx context.Context, request *dpb.UpdateDeviceRequest) (*dpb.UpdateDeviceResponse, error) {
    	deviceID, err := uuid.Parse(request.Device.Id)
    	if err != nil {
    		return &dpb.UpdateDeviceResponse{
    			Timestamp: time.Now().UnixNano(),
    			Status:    dpb.Status_STATUS_OK,
    		}, err
    	}
    	device, err := d.networkDomain.GetDevice(deviceID.String())
    	if err != nil {
    		return &dpb.UpdateDeviceResponse{
    			Timestamp: time.Now().UnixNano(),
    			Status:    dpb.Status_STATUS_OK,
    		}, err
    	}
    
    	err = d.networkDomain.UpdateDevice(device, request.Device.Model)
    	if err != nil {
    		return &dpb.UpdateDeviceResponse{
    			Timestamp: time.Now().UnixNano(),
    			Status:    dpb.Status_STATUS_OK,
    		}, err
    	}
    
    	return &dpb.UpdateDeviceResponse{
    		Timestamp: time.Now().UnixNano(),
    		Status:    dpb.Status_STATUS_OK,
    	}, nil
    }