Newer
Older
/*
This file contains the grpc cli server-side calls.
Functions here should call other functions in charge of the
particular task.
"encoding/json"
"errors"
"github.com/google/uuid"
"github.com/spf13/viper"
"code.fbi.h-da.de/cocsn/gosdn/forks/goarista/gnmi"
"google.golang.org/grpc/health"
healthpb "google.golang.org/grpc/health/grpc_health_v1"
"google.golang.org/protobuf/types/known/emptypb"
stream pb.GrpcCli_CreateLogStreamServer
id string
active bool
error chan error
}
// server is used to implement the grcp cli server
pb.UnimplementedGrpcCliServer
var srv *server
type buf []byte
func (b *buf) Write(p []byte) (n int, err error) {
reply := pb.LogReply{Log: string(p)}
srv.BroadcastLog(&reply)
return len(p), nil
}
func (s *server) SayHello(ctx context.Context, in *pb.HelloRequest) (*pb.HelloReply, error) {
return &pb.HelloReply{Message: "Hello " + in.GetName(), GoSDNInfo: "goSDN in version: DEVELOP"}, nil
//GetLog creates a continuous stream between ciena and server to send goSDN logs
func (s *server) CreateLogStream(req *emptypb.Empty, stream pb.GrpcCli_CreateLogStreamServer) error {
stream: stream,
active: true,
error: make(chan error),
}
s.logConnections = append(s.logConnections, conn)
return <-conn.error
}
func (s *server) BroadcastLog(log *pb.LogReply) {
wait := sync.WaitGroup{}
done := make(chan int)
for _, conn := range s.logConnections {
wait.Add(1)
defer wait.Done()
if conn.active {
err := conn.stream.Send(log)
if err != nil {
conn.active = false
conn.error <- err
}
}
}(conn)
}
go func() {
//blocks until all send routines are finished
wait.Wait()
close(done)
}()
<-done
}
func (s *server) Shutdown(ctx context.Context, in *pb.ShutdownRequest) (*pb.ShutdownReply, error) {
log.Info("Shutdown Received: ", in.GetName())
s.core.IsRunning <- false
return &pb.ShutdownReply{Message: "Shutdown " + in.GetName()}, nil
}
func getCLIGoing(core *Core) {
var (
logConnections []*logConnection
logBuffer buf
system = ""
)
log.Info("Starting: GetCLIGoing")
// Boot-up the control interface for the cli
cliControlListener, err := net.Listen("tcp", viper.GetString("socket"))
}
cliControlServer := grpc.NewServer()
srv = &server{core: core, logConnections: logConnections}
//TODO: move?
wrt := io.MultiWriter(os.Stdout, &logBuffer)
healthpb.RegisterHealthServer(cliControlServer, healthCheck)
healthCheck.SetServingStatus(system, healthpb.HealthCheckResponse_SERVING)
if err := cliControlServer.Serve(cliControlListener); err != nil {
}
// SBI specific calls, by now TAPI only
func (s *server) TAPIGetEdge(ctx context.Context, in *pb.TAPIRequest) (*pb.TAPIReply, error) {
log.Info("Received: ", in.GetName())
return &pb.TAPIReply{Message: "Done"}, nil
}
func (s *server) TAPIGetEdgeNode(ctx context.Context, in *pb.TAPIRequest) (*pb.TAPIReply, error) {
log.Info("Received: ", in.GetName())
return &pb.TAPIReply{Message: "Done"}, nil
}
func (s *server) TAPIGetLink(ctx context.Context, in *pb.TAPIRequest) (*pb.TAPIReply, error) {
log.Info("Received: ", in.GetName())
return &pb.TAPIReply{Message: "Done"}, nil
}
func (s *server) CreatePND(ctx context.Context, in *pb.CreatePNDRequest) (*pb.CreatePNDReply, error) {
log.Info("Received: Create a PND with the name", in.GetName())
sbi := s.core.southboundInterfaces[in.GetSbi()]
id := uuid.New()
s.core.principalNetworkDomains[id] = NewPND(in.GetName(), in.GetDescription(), sbi)
return &pb.CreatePNDReply{Message: "Created new PND: " + id.String()}, nil
}
func (s *server) GetAllPNDs(ctx context.Context, in *emptypb.Empty) (*pb.AllPNDsReply, error) {
var pnds []*pb.PND
for uuidPND, pnd := range s.core.principalNetworkDomains {
var devices []*pb.Device
for uuidDevice, device := range pnd.(*pndImplementation).devices {
tmpDevice := pb.Device{
Uuid: uuidDevice.String(),
Address: device.Config.Address,
Username: device.Config.Username,
Password: device.Config.Password}
devices = append(devices, &tmpDevice)
}
tmpPND := pb.PND{
Uuid: uuidPND.String(),
Name: pnd.GetName(),
Description: pnd.GetDescription(),
Sbi: pnd.GetSBIs()["default"].SbiIdentifier(),
Devices: devices,
}
pnds = append(pnds, &tmpPND)
}
return &pb.AllPNDsReply{Pnds: pnds}, nil
}
func (s *server) GetAllSBINames(ctx context.Context, in *emptypb.Empty) (*pb.AllSBINamesReply, error) {
sbiNames := make([]string, len(s.core.southboundInterfaces))
for _, s := range s.core.southboundInterfaces {
sbiNames = append(sbiNames, s.SbiIdentifier())
}
return &pb.AllSBINamesReply{SbiNames: sbiNames}, nil
}
func (s *server) AddDevice(ctx context.Context, in *pb.AddDeviceRequest) (*pb.AddDeviceReply, error) {
log.Info("Received: AddDevice")
uuidPND, err := uuid.Parse(in.UuidPND)
if err != nil {
return &pb.AddDeviceReply{Message: err.Error()}, err
}
pnd, exists := s.core.principalNetworkDomains[uuidPND]
if exists != true {
return &pb.AddDeviceReply{Message: err.Error()}, err
}
sbi := s.core.principalNetworkDomains[uuidPND].GetSBIs()["default"]
transport := &Gnmi{SetNode: sbi.SetNode()}
cfg := &gnmi.Config{
Addr: in.Device.Address,
Username: in.Device.Username,
Password: in.Device.Password,
}
transport.SetConfig(cfg)
Address: in.Device.Address,
Username: in.Device.Username,
Password: in.Device.Password,
},
newDevice.GoStruct = sbi.Schema().Root
devicesMap := pnd.GetDevices()
devicesMap[newDevice.Config.Uuid] = &newDevice
return &pb.AddDeviceReply{Message: "Added new Device: " + newDevice.Config.Uuid.String()}, err
func (s *server) HandleDeviceGetRequest(ctx context.Context, in *pb.DeviceGetRequest) (*pb.DeviceGetReply, error) {
log.Info("Received: HandleDeviceGetRequest")
uuidPND, err := uuid.Parse(in.GetUuidPND())
if err != nil {
return &pb.DeviceGetReply{Message: err.Error()}, err
}
uuidDevice, err := uuid.Parse(in.GetUuidDevice())
if err != nil {
return &pb.DeviceGetReply{Message: err.Error()}, err
}
pnd, exists := s.core.principalNetworkDomains[uuidPND]
if exists != true {
err := errors.New("Couldnt find PND: UUID is wrong")
return &pb.DeviceGetReply{Message: err.Error()}, err
}
device, exists := s.core.principalNetworkDomains[uuidPND].GetDevices()[uuidDevice]
if exists != true {
err := errors.New("Couldnt find device: UUID is wrong")
return &pb.DeviceGetReply{Message: err.Error()}, err
}
err = pnd.Request(uuidDevice, in.GetPath())
if err != nil {
return &pb.DeviceGetReply{Message: err.Error()}, err
}
d, err := json.MarshalIndent(device, "", "\t")
if err != nil {
return &pb.DeviceGetReply{Message: err.Error()}, err
}
return &pb.DeviceGetReply{Message: string(string(d))}, nil