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()]
if sbi == nil {
log.Info("lul")
}
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 := s.core.principalNetworkDomains[uuidPND]
sbi := s.core.principalNetworkDomains[uuidPND].GetSBIs()["default"]
newDevice := Device{
Uuid: uuid.New(),
Device: sbi.Schema().Root,
SBI: sbi,
Config: DeviceConfig{
Address: in.Device.Address,
Username: in.Device.Username,
Password: in.Device.Password,
},
Transport: sbi.GetTransport(),
devicesMap := pnd.GetDevices()
devicesMap[newDevice.Uuid] = newDevice
return &pb.AddDeviceReply{Message: "Added new Device: " + newDevice.Uuid.String()}, err
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
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
}
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
}
cfg := &gnmi.Config{
Addr: device.Config.Address,
Password: device.Config.Password,
Username: device.Config.Username,
}
ctxGnmi := gnmi.NewContext(context.Background(), cfg)
ctxGnmi = context.WithValue(ctxGnmi, "config", cfg)
paths := []string{in.GetPath()}
req, err := gnmi.NewGetRequest(gnmi.SplitPaths(paths), "")
resp, err := GetWithRequest(ctxGnmi, req)
if err != nil {
return &pb.DeviceGetReply{Message: err.Error()}, err
}
if err := device.Add(resp); err != nil {
return &pb.DeviceGetReply{Message: err.Error()}, err
}
d, err := json.Marshal(device.Device)
if err != nil {
return &pb.DeviceGetReply{Message: err.Error()}, err
}
return &pb.DeviceGetReply{Message: string(d)}, nil
}