From 1a884aa99edd1174755d44cc90ae507634ffab28 Mon Sep 17 00:00:00 2001 From: Manuel Kieweg <mail@manuelkieweg.de> Date: Thu, 8 Oct 2020 17:10:15 +0100 Subject: [PATCH] Commenting exported stuff --- database/database.go | 5 +- gosdn-cli/gosdn-cli.go | 86 ++++++++++++------------------ log/logger.go | 4 +- log/loglevel.go | 4 ++ nucleus/cli-handling.go | 5 +- nucleus/controller.go | 25 +++++---- nucleus/interfaces/client.go | 2 + nucleus/interfaces/clientConfig.go | 2 + nucleus/nucleus-core.go | 2 +- restconf/client/ciena/client.go | 14 ++++- restconf/util/unmarshal.go | 3 ++ 11 files changed, 79 insertions(+), 73 deletions(-) diff --git a/database/database.go b/database/database.go index 5f33a14dc..3ad8b568e 100644 --- a/database/database.go +++ b/database/database.go @@ -345,7 +345,6 @@ func CreateLink() {} func RemoveLink() {} //Shutdown closes the connection to the database -func (d Database) Shutdown() { - if err := d.driver.Close(); err != nil { - } +func (d Database) Shutdown() error { + return d.driver.Close() } diff --git a/gosdn-cli/gosdn-cli.go b/gosdn-cli/gosdn-cli.go index 52e73a71d..bc6763c5b 100644 --- a/gosdn-cli/gosdn-cli.go +++ b/gosdn-cli/gosdn-cli.go @@ -2,30 +2,21 @@ package main import ( pb "code.fbi.h-da.de/cocsn/gosdn/cliInterface" + "code.fbi.h-da.de/cocsn/gosdn/log" "context" "flag" "fmt" - "github.com/rivo/tview" "google.golang.org/grpc" - "log" "os" "time" ) const ( - address = "localhost:55055" defaultName = "gosdn-cli" ) // Based on the helloworld example of grpc.io -- thx! -const refreshInterval = 500 * time.Millisecond - -var ( - view *tview.Modal - app *tview.Application -) - type cliClientConfig struct { goSDNCLIAddr4 *string goSDNCLIPort4 *int @@ -40,12 +31,12 @@ type commandOptions struct { } var commandList = map[string]commandOptions{ - "hello": {"hello", "test connection to goSDN controller", goSDNSayHello}, - "shutdown": {"shutdown", "request goSDN controller to shutdown", goSDNShutdown}, - "testdb" : {"testdb", "test all database connections", goSDNTestDB}, - "tapigetedge" : {"tapigetedge", "get list of edges", TAPIGetEdge}, - "tapigetedgenode" : {"tapigetedgenode", "get list of edgenodes", TAPIGetEdgeNode}, - "tapigetlink" : {"tapigetlink", "get list of links", TAPIGetLink}, + "hello": {"hello", "test connection to goSDN controller", goSDNSayHello}, + "shutdown": {"shutdown", "request goSDN controller to shutdown", goSDNShutdown}, + "testdb": {"testdb", "test all database connections", goSDNTestDB}, + "tapigetedge": {"tapigetedge", "get list of edges", TAPIGetEdge}, + "tapigetedgenode": {"tapigetedgenode", "get list of edgenodes", TAPIGetEdgeNode}, + "tapigetlink": {"tapigetlink", "get list of links", TAPIGetLink}, } /* @@ -67,61 +58,49 @@ func main() { flag.Parse() // Print complete command list and exit - if *printCommandList == true { + if *printCommandList { for _, element := range commandList { fmt.Println(element.name + "\t" + element.description) } os.Exit(0) } - log.Println("Starting " + defaultName + " to access the goSDN controller") + log.Info("Starting " + defaultName + " to access the goSDN controller") // Prepare string with socket for connection to the goSDN controller goSDNSocketAddress := fmt.Sprintf("%s:%d", *myConfiguration.goSDNCLIAddr4, *myConfiguration.goSDNCLIPort4) - log.Println("Connecting to the goSDN server at: " + goSDNSocketAddress) + log.Info("Connecting to the goSDN server at: " + goSDNSocketAddress) // Set up a connection to the server. address := "localhost:55055" conn, err := grpc.Dial(address, grpc.WithInsecure(), grpc.WithBlock()) if err != nil { - log.Fatalf("did not connect: %v", err) + log.Fatal(err) } defer conn.Close() - log.Println(("Connected to " + conn.Target())) + log.Info("Connected to " + conn.Target()) // Check for non-interactive or interactive mode - if *myConfiguration.interactive == false { - log.Println("starting in non-interactive mode") + if !*myConfiguration.interactive { + log.Info("starting in non-interactive mode") // Lookup command or die _, found := commandList[*myConfiguration.goSDNCommand] if found { // Excecute desired command commandList[*myConfiguration.goSDNCommand].command(conn) } else { - log.Fatalf("Your desired command %s is not available", commandList[*myConfiguration.goSDNCommand].name) + // TODO: change once gosdn/errors exist + log.Fatal("Your desired command is not available: ", commandList[*myConfiguration.goSDNCommand].name) os.Exit(1) } } else { - log.Println("starting in interactive mode -- do not use yet") + log.Info("starting in interactive mode -- do not use yet") os.Exit(1) - - app = tview.NewApplication().EnableMouse(true) - - flex := tview.NewFlex(). - AddItem(tview.NewBox().SetBorder(true).SetTitle("Command List"), 0, 1, false). - AddItem(tview.NewFlex().SetDirection(tview.FlexRow). - AddItem(tview.NewBox().SetBorder(true).SetTitle("Top"), 0, 1, false). - AddItem(tview.NewBox().SetBorder(true).SetTitle("Middle (3 x height of Top)"), 0, 3, false). - AddItem(tview.NewBox().SetBorder(true).SetTitle("Bottom (5 rows)"), 5, 1, false), 0, 2, false) - - if err := app.SetRoot(flex, true).Run(); err != nil { - panic(err) - } } } -func goSDNSayHello (conn *grpc.ClientConn) { +func goSDNSayHello(conn *grpc.ClientConn) { c := pb.NewGrpcCliClient(conn) // Contact the server and print out its response. @@ -133,9 +112,9 @@ func goSDNSayHello (conn *grpc.ClientConn) { defer cancel() r, err := c.SayHello(ctx, &pb.HelloRequest{Name: name}) if err != nil { - log.Fatalf("could not say hello: %v", err) + log.Fatal(err) } - log.Printf("Greeting: %s", r.String()) + log.Info("Greeting: ", r.String()) } @@ -152,16 +131,17 @@ func goSDNShutdown(conn *grpc.ClientConn) { defer cancel() r, err := c.Shutdown(ctx, &pb.ShutdownRequest{Name: name}) if err != nil { - log.Fatalf("could not request shutdown: %v", err) + log.Fatal(err) } - log.Printf("Greeting: %s", r.GetMessage()) + log.Info("Greeting: ", r.GetMessage()) } func goSDNTestDB(conn *grpc.ClientConn) { // 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) { c := pb.NewGrpcCliClient(conn) @@ -175,11 +155,13 @@ func TAPIGetEdge(conn *grpc.ClientConn) { defer cancel() r, err := c.TAPIGetEdge(ctx, &pb.TAPIRequest{Name: name}) if err != nil { - log.Fatalf("could not request shutdown: %v", err) + log.Fatal(err) } - log.Printf("TAPIGetEdge said: %s", r.GetMessage()) + log.Info("TAPIGetEdge said: ", r.GetMessage()) } +// TAPIGetEdgeNode triggers the GetEdgeNode function of the Ciena +// flavoured TAPI client func TAPIGetEdgeNode(conn *grpc.ClientConn) { c := pb.NewGrpcCliClient(conn) @@ -192,11 +174,13 @@ func TAPIGetEdgeNode(conn *grpc.ClientConn) { defer cancel() r, err := c.TAPIGetEdgeNode(ctx, &pb.TAPIRequest{Name: name}) if err != nil { - log.Fatalf("could not request shutdown: %v", err) + log.Fatal(err) } - log.Printf("TAPIGetEdgeNode said: %s", r.GetMessage()) + log.Info("TAPIGetEdgeNode said: ", r.GetMessage()) } +// TAPIGetLink triggers the GetLink function of the Ciena +// flavoured TAPI client func TAPIGetLink(conn *grpc.ClientConn) { c := pb.NewGrpcCliClient(conn) @@ -210,7 +194,7 @@ func TAPIGetLink(conn *grpc.ClientConn) { defer cancel() r, err := c.TAPIGetLink(ctx, &pb.TAPIRequest{Name: name}) if err != nil { - log.Fatalf("could not request shutdown: %v", err) + log.Fatal(err) } - log.Printf("TAPIGetLink said: %s", r.GetMessage()) -} \ No newline at end of file + log.Info("TAPIGetLink said: ", r.GetMessage()) +} diff --git a/log/logger.go b/log/logger.go index adcaa5dc5..479d68e3b 100644 --- a/log/logger.go +++ b/log/logger.go @@ -83,7 +83,7 @@ func LoglevelOutput(level Level, out io.Writer) { l.lock.Lock() defer l.lock.Unlock() l.LoglevelWriter[level] = out - if reflect.TypeOf(out) == reflect.TypeOf(&syslog.Writer{}) { + if reflect.TypeOf(out) == reflect.TypeOf(&syslog.Writer{}) { l.toSyslog[level] = true } } @@ -94,7 +94,7 @@ func Debug(args ...interface{}) { log(DEBUG, args...) } -//Debug passes the DEBUG flag and a +//Info passes the INFO flag and a //message to the logger func Info(args ...interface{}) { log(INFO, args...) diff --git a/log/loglevel.go b/log/loglevel.go index 64a259cb4..6288bcbc9 100644 --- a/log/loglevel.go +++ b/log/loglevel.go @@ -1,7 +1,11 @@ package log +// Level is an 8 bit integer representing a +// log level for the logger type Level uint8 +// Constants for more verbose integer +// values const ( PANIC Level = iota FATAL diff --git a/nucleus/cli-handling.go b/nucleus/cli-handling.go index e6d843eb3..9c5886980 100644 --- a/nucleus/cli-handling.go +++ b/nucleus/cli-handling.go @@ -2,8 +2,7 @@ This file contains the grpc cli server-side calls. Functions here should call other functions in charge of the particular task. - */ - +*/ package nucleus @@ -24,7 +23,7 @@ type server struct { func (s *server) SayHello(ctx context.Context, in *pb.HelloRequest) (*pb.HelloReply, error) { log.Info("Received: ", in.GetName()) - return &pb.HelloReply{Message: "Hello " + in.GetName(), GoSDNInfo: "goSDN in version: DEVELOP"}, nil + return &pb.HelloReply{Message: "Hello " + in.GetName(), GoSDNInfo: "goSDN in version: DEVELOP"}, nil } func (s *server) Shutdown(ctx context.Context, in *pb.ShutdownRequest) (*pb.ShutdownReply, error) { diff --git a/nucleus/controller.go b/nucleus/controller.go index a0cdccbdf..f5f0997ef 100644 --- a/nucleus/controller.go +++ b/nucleus/controller.go @@ -22,14 +22,16 @@ type clientConfigs struct { Client []interfaces.ClientConfig `toml:"client"` } +// Core is the representation of the controllers core type Core struct { //Assert type with clients[key].(*MCPClient) - clients map[string]interfaces.Client - database database.Database - config controllerConfig + clients map[string]interfaces.Client + database database.Database + config controllerConfig IsRunning chan bool } +//Init does start-up housekeeping like reading controller and client config files func (c *Core) Init(socket, configFileController, configFileClient string, IsRunningChannel chan bool) { if err := c.readControllerConfig(configFileController); err != nil { log.Fatal(err) @@ -48,13 +50,14 @@ func (c *Core) Init(socket, configFileController, configFileClient string, IsRun } } +// AttachDatabase connects to the database and passes the connectio to the controller core func (c *Core) AttachDatabase() { c.database = database.NewDatabaseClient(c.config.DatabaseSocket, c.config.DatabaseUser, c.config.DatabasePassword, c.config.DatabaseCrypto) } +// Shutdown waits for the shutdown signal and gracefully shuts down once it arrived func (c *Core) Shutdown() { - - <- c.IsRunning + <-c.IsRunning log.Info("Received shutdown signal. Shutting down") f, err := os.Create(c.config.ConfigPath) @@ -69,7 +72,7 @@ func (c *Core) Shutdown() { os.Exit(0) } -func (c *Core)readControllerConfig(configFileController string) error { +func (c *Core) readControllerConfig(configFileController string) error { if configFileController == "" { configFileController = "gosdn.toml" } @@ -88,19 +91,19 @@ func (c *Core)readControllerConfig(configFileController string) error { return nil } -func (c *Core)readClientConfig(configFileClient string) error { +func (c *Core) readClientConfig(configFileClient string) error { if configFileClient == "" { configFileClient = "clients.toml" } - if _,err := os.Stat(configFileClient); err != nil { + if _, err := os.Stat(configFileClient); err != nil { return err } clients := clientConfigs{} - if _,err := toml.DecodeFile(configFileClient, &clients); err != nil { + if _, err := toml.DecodeFile(configFileClient, &clients); err != nil { return err } - for _,client := range clients.Client { + for _, client := range clients.Client { c.clients[client.Identifier] = ciena.NewMCPClient(client.Endpoint, client.Username, client.Password, &c.database, &client) } return nil -} \ No newline at end of file +} diff --git a/nucleus/interfaces/client.go b/nucleus/interfaces/client.go index 9e541e687..4d0b9e1ce 100644 --- a/nucleus/interfaces/client.go +++ b/nucleus/interfaces/client.go @@ -1,5 +1,7 @@ package interfaces +// Client provides an interface for +// SBI protocol clients type Client interface { GetConfig() ClientConfig } diff --git a/nucleus/interfaces/clientConfig.go b/nucleus/interfaces/clientConfig.go index 917537b75..5a2782e10 100644 --- a/nucleus/interfaces/clientConfig.go +++ b/nucleus/interfaces/clientConfig.go @@ -1,5 +1,7 @@ package interfaces +// ClientConfig contains SBI client +// configuration parameters type ClientConfig struct { Identifier string `toml:"identifier"` Endpoint string `toml:"endpoint"` diff --git a/nucleus/nucleus-core.go b/nucleus/nucleus-core.go index 1b63bcf35..0276a4ad8 100644 --- a/nucleus/nucleus-core.go +++ b/nucleus/nucleus-core.go @@ -17,7 +17,7 @@ func StartAndRun(socket, filename string, IsRunningChannel chan bool) { clients: make(map[string]interfaces.Client), database: database.Database{}, } - core.Init(socket, filename,"", IsRunningChannel) + core.Init(socket, filename, "", IsRunningChannel) // Start the GRCP CLI go getCLIGoing(&core) go core.Shutdown() diff --git a/restconf/client/ciena/client.go b/restconf/client/ciena/client.go index d9af6d024..072413d14 100644 --- a/restconf/client/ciena/client.go +++ b/restconf/client/ciena/client.go @@ -23,11 +23,13 @@ type MCPClient struct { config *interfaces.ClientConfig } +// GetConfig returns a ClientConfig struct containing +// the current configuration stat of the Ciena SBI client func (c MCPClient) GetConfig() interfaces.ClientConfig { return *c.config } -//NewMCPClient creates a client +//NewMCPClient creates a Ciena flavores TAPI client func NewMCPClient(endpoint, username, password string, database *database.Database, config *interfaces.ClientConfig) *MCPClient { // create the transport transport := httptransport.New(endpoint, "/", nil) @@ -46,10 +48,12 @@ func NewMCPClient(endpoint, username, password string, database *database.Databa client: client, database: database, buffer: buffer, - config: config, + config: config, } } +// GetConnections implements the TAPI Connectivity GetConnections call with a grain of +// Ciena salt. The response is written to the client's buffer and passed to the database func (c *MCPClient) GetConnections() error { defer c.buffer.Reset() _, err := c.client.TapiConnectivityCore.GetTapiCoreContextConnection(nil) @@ -58,6 +62,8 @@ func (c *MCPClient) GetConnections() error { return err } +// GetLinks implements the TAPI Topology GetLinks call with a grain of +// Ciena salt. The response is written to the client's buffer and passed to the database func (c *MCPClient) GetLinks() error { defer c.buffer.Reset() _, err := c.client.TapiTopologyCore.GetTapiCoreContextTopologyMcpBaseTopologyLink(nil) @@ -66,6 +72,8 @@ func (c *MCPClient) GetLinks() error { return err } +// GetNodes implements the TAPI Topology GetNodes call with a grain of +// Ciena salt. The response is written to the client's buffer and passed to the database func (c *MCPClient) GetNodes() error { defer c.buffer.Reset() _, err := c.client.TapiTopologyCore.GetTapiCoreContextTopologyMcpBaseTopologyNode(nil) @@ -74,6 +82,8 @@ func (c *MCPClient) GetNodes() error { return err } +// GetNodeEdgePoints implements the TAPI Topology GetNodeEdgePoints call with a grain of +// Ciena salt. The response is written to the client's buffer and passed to the database func (c *MCPClient) GetNodeEdgePoints() error { defer c.buffer.Reset() _, err := c.client.TapiTopologyCore.GetTapiCoreContextTopologyMcpBaseTopologyNodeEdgePoint(nil) diff --git a/restconf/util/unmarshal.go b/restconf/util/unmarshal.go index fa195f935..9c85a8cb4 100644 --- a/restconf/util/unmarshal.go +++ b/restconf/util/unmarshal.go @@ -5,10 +5,13 @@ import ( "io" ) +// YANGConsumer is a auxillary type to redirect the response +// of an RESTCONF call to a ygot YANG unmarshaler type YANGConsumer struct { Data *bytes.Buffer } +// Consume reads the received data into a byte buffer func (yc YANGConsumer) Consume(reader io.Reader, _ interface{}) error { _, err := yc.Data.ReadFrom(reader) return err -- GitLab