Newer
Older
/*
This file contains the grpc cli server-side calls.
Functions here should call other functions in charge of the
particular task.
"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 := &OpenConfig{}
id := uuid.New()
s.core.principalNetworkDomains[id] = NewPND(in.GetName(), in.GetDescription(), sbi)
if err := s.core.savePNDs(path); err != nil {
log.Info(err)
}
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
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.GetDefaultSBIName(),
Devices: devices}
pnds = append(pnds, &tmpPND)
}
return &pb.AllPNDsReply{Pnds: pnds}, nil
}
func (s *server) AddDevice(ctx context.Context, in *pb.AddDeviceRequest) (*pb.AddDeviceReply, error) {
uuidPND, err := uuid.Parse(in.UuidPND)
pnd := s.core.principalNetworkDomains[uuidPND]
newDevice := Device{
Uuid: uuid.New(),
Device: nil,
SBI: nil,
Config: DeviceConfig{
Address: in.Device.Address,
Username: in.Device.Username,
Password: in.Device.Password,
},
}
devicesMap := pnd.(*pndImplementation).Devices
devicesMap[newDevice.Uuid] = newDevice
return &pb.AddDeviceReply{Message: "Added new Device: " + newDevice.Uuid.String()}, err