diff --git a/clients.toml b/clients.toml new file mode 100644 index 0000000000000000000000000000000000000000..7a37fdcaac8aaa9d9563d2c591cf2a480943072b --- /dev/null +++ b/clients.toml @@ -0,0 +1,3 @@ +[[client]] +identifier = "ciena-mcp" +endpoint = "141.100.70.170:8080" \ No newline at end of file diff --git a/nucleus/controller.go b/nucleus/controller.go index 9cb0b781f6acb533db6c7d4a86e561f1e91869da..a0cdccbdf972faaa42b18d2cff72e23c6793c16a 100644 --- a/nucleus/controller.go +++ b/nucleus/controller.go @@ -18,6 +18,10 @@ type controllerConfig struct { ConfigPath string } +type clientConfigs struct { + Client []interfaces.ClientConfig `toml:"client"` +} + type Core struct { //Assert type with clients[key].(*MCPClient) clients map[string]interfaces.Client @@ -26,33 +30,22 @@ type Core struct { IsRunning chan bool } -func (c *Core) Init(socket, configfile string, IsRunningChannel chan bool) { - if configfile == "" { - configfile = "gosdn.toml" - } - _, err := os.Stat(configfile) - if err != nil { - log.Fatal("Config file is missing: ", configfile) - } - - c.config = controllerConfig{} - if _, err := toml.DecodeFile(configfile, &c.config); err != nil { +func (c *Core) Init(socket, configFileController, configFileClient string, IsRunningChannel chan bool) { + if err := c.readControllerConfig(configFileController); err != nil { log.Fatal(err) } + if socket != "localhost:55055" { c.config.CliSocket = socket } - if c.config.ConfigPath == "" { - c.config.ConfigPath = configfile - } c.AttachDatabase() c.IsRunning = IsRunningChannel - //TODO: Create client config/CLI adapter - c.clients["ciena-mcp"] = ciena.NewMCPClient("141.100.70.170:8080", "", "", &c.database) - + if err := c.readClientConfig(configFileClient); err != nil { + log.Fatal(err) + } } func (c *Core) AttachDatabase() { @@ -61,13 +54,8 @@ func (c *Core) AttachDatabase() { func (c *Core) Shutdown() { - stopIt := <- c.IsRunning - if !stopIt { - log.Debug("Shutdown() received action to shutdown") - }else { - log.Debug("Shutdown() received something else.") - } - + <- c.IsRunning + log.Info("Received shutdown signal. Shutting down") f, err := os.Create(c.config.ConfigPath) if err != nil { @@ -77,6 +65,42 @@ func (c *Core) Shutdown() { if err := enc.Encode(c.config); err != nil { log.Fatal(err) } - + log.Info("Shutdown complete") os.Exit(0) } + +func (c *Core)readControllerConfig(configFileController string) error { + if configFileController == "" { + configFileController = "gosdn.toml" + } + if _, err := os.Stat(configFileController); err != nil { + return err + } + + c.config = controllerConfig{} + if _, err := toml.DecodeFile(configFileController, &c.config); err != nil { + return err + } + + if c.config.ConfigPath == "" { + c.config.ConfigPath = configFileController + } + return nil +} + +func (c *Core)readClientConfig(configFileClient string) error { + if configFileClient == "" { + configFileClient = "clients.toml" + } + if _,err := os.Stat(configFileClient); err != nil { + return err + } + clients := clientConfigs{} + if _,err := toml.DecodeFile(configFileClient, &clients); err != nil { + return err + } + 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 1ada3063fe1ae8a8557d379b2d16bdc9f30efa81..9e541e687706e35d740cdc04ac1b3471a86b9be7 100644 --- a/nucleus/interfaces/client.go +++ b/nucleus/interfaces/client.go @@ -1,5 +1,5 @@ package interfaces type Client interface { - GetConfig() string + GetConfig() ClientConfig } diff --git a/nucleus/interfaces/clientConfig.go b/nucleus/interfaces/clientConfig.go new file mode 100644 index 0000000000000000000000000000000000000000..917537b7541ba7dedf5203228f756b4511297856 --- /dev/null +++ b/nucleus/interfaces/clientConfig.go @@ -0,0 +1,8 @@ +package interfaces + +type ClientConfig struct { + Identifier string `toml:"identifier"` + Endpoint string `toml:"endpoint"` + Username string `toml:"username"` + Password string `toml:"password"` +} diff --git a/nucleus/nucleus-core.go b/nucleus/nucleus-core.go index 1a2dfef3853df2a7acb12ecb85e39515e24a0ff2..1b63bcf3574ff23e730896b24977e7d082de0408 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() @@ -25,10 +25,8 @@ func StartAndRun(socket, filename string, IsRunningChannel chan bool) { log.Info("and ready for take off") //Just to produce some signs of vitality... - for (true) { + for { time.Sleep(10 * time.Second) log.Debug("Still alive...") } - - log.Info("Good bye....!") } diff --git a/restconf/client/ciena/client.go b/restconf/client/ciena/client.go index a384c683dd42af0550930f20f48e70e0b33105d3..d9af6d0247d776dc0d5a7f9771f2dc60fbb0dcc1 100644 --- a/restconf/client/ciena/client.go +++ b/restconf/client/ciena/client.go @@ -4,6 +4,7 @@ import ( "bytes" "code.fbi.h-da.de/cocsn/gosdn/database" "code.fbi.h-da.de/cocsn/gosdn/log" + "code.fbi.h-da.de/cocsn/gosdn/nucleus/interfaces" "code.fbi.h-da.de/cocsn/gosdn/restconf/util" apiclient "code.fbi.h-da.de/cocsn/swagger/apis/mcp/client" "crypto/tls" @@ -19,15 +20,15 @@ type MCPClient struct { client *apiclient.ServiceTopologyTAPI database *database.Database buffer *bytes.Buffer + config *interfaces.ClientConfig } -func (c MCPClient) GetConfig() string { - //TODO: Fill with life - return "..." +func (c MCPClient) GetConfig() interfaces.ClientConfig { + return *c.config } //NewMCPClient creates a client -func NewMCPClient(endpoint, username, password string, database *database.Database) *MCPClient { +func NewMCPClient(endpoint, username, password string, database *database.Database, config *interfaces.ClientConfig) *MCPClient { // create the transport transport := httptransport.New(endpoint, "/", nil) transport.Transport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: true} @@ -45,6 +46,7 @@ func NewMCPClient(endpoint, username, password string, database *database.Databa client: client, database: database, buffer: buffer, + config: config, } }