Skip to content
Snippets Groups Projects
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
commands.go 3.15 KiB
package commands

import (
	pb "code.fbi.h-da.de/cocsn/gosdn/api/proto"
	"code.fbi.h-da.de/cocsn/gosdn/log"
	"context"
	grpc "google.golang.org/grpc"
	"time"
)

type commandType int

const (
	GoSDN commandType = iota
	Database
)

const (
	defaultName = "gosdn-cli"
)

type command struct {
	Name        string
	Description string
	//CommandType commandType
	Function func(conn *grpc.ClientConn) string
}

var CommandList = []command{
	{"hello", "test connection to goSDN controller", goSDNSayHello},
	{"shutdown", "request goSDN controller to shutdown", goSDNShutdown},
	{"testdb", "test all database connections", goSDNTestDB},
	{"tapigetedge", "get list of edges", TAPIGetEdge},
	{"tapigetedgenode", "get list of edgenodes", TAPIGetEdgeNode},
	{"tapigetlink", "get list of links", TAPIGetLink},
}

func Connect() (*grpc.ClientConn, error) {
	address := "localhost:55055"
	return grpc.Dial(address, grpc.WithInsecure(), grpc.WithTimeout(5*time.Second), grpc.WithBlock())
}

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)
	}
	return r.GetMessage()
}

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)
	}
	return r.GetMessage()
}

func goSDNTestDB(conn *grpc.ClientConn) string {
	// TODO: fill with code and also see if grpc interface has this stub implemented.
	return "not implemented yet"
}

// 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)
	}
	return r.GetMessage()
}

// 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)
	}
	return r.GetMessage()
}

// 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)
	}
	return r.GetMessage()
}