Skip to content
Snippets Groups Projects
Commit 505aacd5 authored by Manuel Kieweg's avatar Manuel Kieweg
Browse files

Merge branch 'controller-config' into 'develop'

Controller core

See merge request cocsn/gosdn!21
parents 9fdfa860 a5ce53c3
No related branches found
No related tags found
2 merge requests!21Controller core,!18Develop
Pipeline #52036 passed
...@@ -5,29 +5,18 @@ import ( ...@@ -5,29 +5,18 @@ import (
"flag" "flag"
) )
// Generate the code out of the yang modules
//go:generate go run $GOPATH/src/github.com/openconfig/ygot/generator/generator.go -path=yang -output_file=yang-processor/gosdnyang.go.go -package_name=gosdnyang -generate_fakeroot -fakeroot_name=device -compress_paths=true -shorten_enum_leaf_names -exclude_modules=ietf-interfaces yang/openconfig-interfaces.yang yang/openconfig-if-ip.yang
type goSDNConfiguration struct {
cliServerAddr4 *string
cliServerPort4 *int
}
func main() { func main() {
// register our supported flags // register our supported flags
cliServerAddr4 := flag.String("cliServerAddr", "127.0.0.1", "The IPv4 Address of the grpcCLI.") cliListenAddr := flag.String("cli-listen-addr", "localhost", "The IP address of the grpcCLI.")
cliServerPort4 := flag.Int("cliServerPort", 55055, "The port number of the grpcCLI") cliListenPort := flag.String("cli-server-port", "55055", "The port number of the grpcCLI")
configFileName := flag.String("config-file", "", "Path to the config file")
flag.Parse() flag.Parse()
cliSocket := *cliListenAddr + *cliListenPort
var myConfiguration = new(goSDNConfiguration)
myConfiguration.cliServerAddr4 = cliServerAddr4
myConfiguration.cliServerPort4 = cliServerPort4
// hand off to cmd for further processing // hand off to cmd for further processing
nucleus.StartUp() nucleus.StartUp(cliSocket, *configFileName)
nucleus.Run() nucleus.Run()
// nothing to see here, please move on! // nothing to see here, please move on!
......
package nucleus package nucleus
import ( import (
yangPro "code.fbi.h-da.de/cocsn/yang-modules/generated/tapi" "code.fbi.h-da.de/cocsn/gosdn/nucleus/interfaces"
"fmt" "encoding/json"
"github.com/openconfig/ygot/ygot" "io/ioutil"
"net" "log"
) )
// This is a test function in order to see how to generate JSON encoded openconfig stuff /*
+-- type controllerConfig struct
+-- type Core struct
+-- client *restconf.Client
+-- db *database.Database
+-- config controllerConfig
+-- func Init(){
read.config
db.create/attach
if config.client{
client.init
}
}
*/
func AssembleJSON() { type controllerConfig struct {
// Build my device CliSocket string `json:"cli_socket"`
d := &yangPro.Device{} DatabaseSocket string `json:"database_socket"`
DatabaseUser string `json:"database_user,omitempty"`
DatabasePassword string `json:"database_password,omitempty"`
DatabaseCrypto bool `json:"database_crypto,omitempty"`
ConfigPath string `json:"config_path"`
}
interfaces, _ := net.Interfaces() type Core struct {
for _, iface := range interfaces { clients []interfaces.Client
fmt.Println(iface.Name) database interfaces.Database
} config controllerConfig
}
// EmitJSON from the ygot library directly does .Validate() and outputs JSON in func (c Core) Init(socket, filename string) {
// the specified format. if filename == "" {
json, err := ygot.EmitJSON(d, &ygot.EmitJSONConfig{ filename = "gosdn.json"
Format: ygot.RFC7951, }
Indent: " ", config, err := ioutil.ReadFile(filename)
RFC7951Config: &ygot.RFC7951JSONConfig{
AppendModuleName: true,
},
})
if err != nil { if err != nil {
panic(fmt.Sprintf("JSON demo error: %v", err)) log.Fatal(err)
} }
fmt.Println(json)
// and now try to read the data from it... c.config = controllerConfig{}
// Device struct to unmarshal into. if err := json.Unmarshal(config, &c.config); err != nil {
loadd := &yangPro.Device{} log.Fatal(err)
if err := yangPro.Unmarshal([]byte(json), loadd); err != nil { }
panic(fmt.Sprintf("Cannot unmarshal JSON: %v", err)) if socket != "localhost:55055" {
c.config.CliSocket = socket
} }
c.database = interfaces.NewDatabaseClient(c.config.DatabaseSocket, c.config.DatabaseUser, c.config.DatabasePassword, c.config.DatabaseCrypto)
}
func (c Core) Shutdown() {
config, err := json.Marshal(c.config)
if err != nil {
log.Fatal(err)
}
if err := ioutil.WriteFile("gosdn.json", config, 0644); err != nil {
log.Fatal(err)
}
} }
package restconf package interfaces
type Client interface { type Client interface {
GetConfig() string GetConfig() string
......
package interfaces
type Database interface {
DropTables()
}
type MockDatabase struct {
}
func (db MockDatabase) DropTables() {
}
func NewDatabaseClient(uri, username, password string, encrypted bool) Database {
return MockDatabase{}
}
...@@ -10,14 +10,10 @@ import ( ...@@ -10,14 +10,10 @@ import (
"time" "time"
) )
// TODO XXX This has to be moved to some configuration file
const (
cli_control_port = "localhost:55055"
)
// server is used to implement helloworld.GreeterServer. // server is used to implement helloworld.GreeterServer.
type server struct { type server struct {
pb.UnimplementedGreeterServer pb.UnimplementedGreeterServer
core *Core
} }
// SayHello implements helloworld.GreeterServer // SayHello implements helloworld.GreeterServer
...@@ -32,18 +28,17 @@ func (s *server) Shutdown(ctx context.Context, in *pb.ShutdownRequest) (*pb.Shut ...@@ -32,18 +28,17 @@ func (s *server) Shutdown(ctx context.Context, in *pb.ShutdownRequest) (*pb.Shut
return &pb.ShutdownReply{Message: "Shutdown " + in.GetName()}, nil return &pb.ShutdownReply{Message: "Shutdown " + in.GetName()}, nil
} }
func getCLIGoing(core *Core) {
func getCLIGoing() {
log.Println("Starting: GetCLIGoing") log.Println("Starting: GetCLIGoing")
// Boot-up the control interface for the cli // Boot-up the control interface for the cli
cliControlListener, err := net.Listen("tcp", cli_control_port) cliControlListener, err := net.Listen("tcp", core.config.CliSocket)
if err != nil { if err != nil {
log.Fatalf("failed to listen: %v", err) log.Fatalf("failed to listen: %v", err)
} }
cliControlServer := grpc.NewServer() cliControlServer := grpc.NewServer()
pb.RegisterGreeterServer(cliControlServer, &server{}) pb.RegisterGreeterServer(cliControlServer, &server{core: core})
if err := cliControlServer.Serve(cliControlListener); err != nil { if err := cliControlServer.Serve(cliControlListener); err != nil {
log.Fatalf("failed to serve: %v", err) log.Fatalf("failed to serve: %v", err)
} }
...@@ -56,12 +51,14 @@ func getCLIGoing() { ...@@ -56,12 +51,14 @@ func getCLIGoing() {
// Next-up: backend database. // Next-up: backend database.
func StartUp() { func StartUp(socket, filename string) {
log.Println("This is the network superintendent...") log.Println("This is the network superintendent...")
log.Println("Starting my ducks") log.Println("Starting my ducks")
// Init the Core
core := Core{}
core.Init(socket, filename)
// Start the GRCP CLI // Start the GRCP CLI
go getCLIGoing() go getCLIGoing(&core)
log.Println("and ready for take off") log.Println("and ready for take off")
} }
...@@ -76,7 +73,7 @@ func Run() { ...@@ -76,7 +73,7 @@ func Run() {
for isRunning { for isRunning {
time.Sleep(10 * time.Second) time.Sleep(10 * time.Second)
log.Println(("Still alive...")) log.Println("Still alive...")
} }
log.Println("Good bye....!") log.Println("Good bye....!")
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment