Newer
Older
package commands
import (
pb "code.fbi.h-da.de/cocsn/gosdn/api/proto"
"code.fbi.h-da.de/cocsn/gosdn/log"
"context"
"github.com/rivo/tview"
"google.golang.org/protobuf/types/known/emptypb"
"time"
)
const (
defaultName = "gosdn-cli"
)
type command struct {
Name string
Description string
Function func(conn *grpc.ClientConn) string
//CommandList contains the specific goSDN gRPC calls
{"hello", "test connection to controller", goSDNSayHello},
{"testdb", "test all database connections", goSDNTestDB},
{"tapiGetDevices", "creates devices", TAPIGetEdge},
{"tapiGetInterfaces", "creates interfaces", TAPIGetEdgeNode},
{"tapiGetLinks", "creates links between devices", TAPIGetLink},
{"shutdown", "request controller to shutdown", goSDNShutdown},
//Connect creates a new connection to the gRPC server
func Connect(address string) (*grpc.ClientConn, error) {
return grpc.Dial(address, grpc.WithInsecure(), grpc.WithTimeout(5*time.Second), grpc.WithBlock())
func GoSDNLogStream(conn *grpc.ClientConn, clv *tview.TextView) error {
var streamError error
c := pb.NewGrpcCliClient(conn)
stream, err := c.CreateLogStream(context.Background(), &emptypb.Empty{})
if err != nil {
log.Error(err)
}
go func(stream pb.GrpcCli_CreateLogStreamClient) {
for {
msg, err := stream.Recv()
if err != nil {
streamError = err
break
}
clv.Write(response)
}
}(stream)
return streamError
}
func goSDNSayHello(conn *grpc.ClientConn) string {
c := pb.NewGrpcCliClient(conn)
// Contact the server and print out its response.
name := defaultName
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
r, err := c.SayHello(ctx, &pb.HelloRequest{Name: name})
if err != nil {
log.Fatal(err)
}
func goSDNShutdown(conn *grpc.ClientConn) string {
c := pb.NewGrpcCliClient(conn)
// Contact the server and print out its response.
name := defaultName
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
r, err := c.Shutdown(ctx, &pb.ShutdownRequest{Name: name})
if err != nil {
log.Fatal(err)
}
func goSDNTestDB(conn *grpc.ClientConn) string {
// TODO: fill with code and also see if grpc interface has this stub implemented.
}
// TAPIGetEdge triggers the GetEdge function of the Ciena
// flavoured TAPI client
func TAPIGetEdge(conn *grpc.ClientConn) string {
c := pb.NewGrpcCliClient(conn)
// Contact the server and print out its response.
name := defaultName
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
r, err := c.TAPIGetEdge(ctx, &pb.TAPIRequest{Name: name})
if err != nil {
log.Fatal(err)
}
}
// TAPIGetEdgeNode triggers the GetEdgeNode function of the Ciena
// flavoured TAPI client
func TAPIGetEdgeNode(conn *grpc.ClientConn) string {
c := pb.NewGrpcCliClient(conn)
// Contact the server and print out its response.
name := defaultName
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
r, err := c.TAPIGetEdgeNode(ctx, &pb.TAPIRequest{Name: name})
if err != nil {
log.Fatal(err)
}
}
// TAPIGetLink triggers the GetLink function of the Ciena
// flavoured TAPI client
func TAPIGetLink(conn *grpc.ClientConn) string {
c := pb.NewGrpcCliClient(conn)
// Contact the server and print out its response.
name := defaultName
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
r, err := c.TAPIGetLink(ctx, &pb.TAPIRequest{Name: name})
if err != nil {
log.Fatal(err)
}