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..76beb0c3cc69aa02e4790ade1b8bc2d36edefda1 100644 --- a/nucleus/controller.go +++ b/nucleus/controller.go @@ -18,6 +18,13 @@ type controllerConfig struct { ConfigPath string } +type clientConfig struct{ + identifier string + endpoint string + username string + password string +} + type Core struct { //Assert type with clients[key].(*MCPClient) clients map[string]interfaces.Client @@ -26,33 +33,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 +57,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 +68,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 := make([]clientConfig, 0) + if _,err := toml.DecodeFile(configFileClient, &clients); err != nil { + return err + } + for _,client := range clients { + c.clients[client.identifier] = ciena.NewMCPClient(client.endpoint, client.username, client.password, &c.database) + } + return nil +} \ No newline at end of file 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....!") }