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

Merge branch '37-read-restconf-client-from-config-toml' into 'develop'

Resolve "Read RESTCONF Client from config.toml"

See merge request cocsn/gosdn!51
parents aad7e9a3 15ca3156
No related branches found
No related tags found
2 merge requests!51Resolve "Read RESTCONF Client from config.toml",!18Develop
Pipeline #52514 failed
[[client]]
identifier = "ciena-mcp"
endpoint = "141.100.70.170:8080"
\ No newline at end of file
...@@ -18,6 +18,10 @@ type controllerConfig struct { ...@@ -18,6 +18,10 @@ type controllerConfig struct {
ConfigPath string ConfigPath string
} }
type clientConfigs struct {
Client []interfaces.ClientConfig `toml:"client"`
}
type Core struct { type Core struct {
//Assert type with clients[key].(*MCPClient) //Assert type with clients[key].(*MCPClient)
clients map[string]interfaces.Client clients map[string]interfaces.Client
...@@ -26,33 +30,22 @@ type Core struct { ...@@ -26,33 +30,22 @@ type Core struct {
IsRunning chan bool IsRunning chan bool
} }
func (c *Core) Init(socket, configfile string, IsRunningChannel chan bool) { func (c *Core) Init(socket, configFileController, configFileClient string, IsRunningChannel chan bool) {
if configfile == "" { if err := c.readControllerConfig(configFileController); err != nil {
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 {
log.Fatal(err) log.Fatal(err)
} }
if socket != "localhost:55055" { if socket != "localhost:55055" {
c.config.CliSocket = socket c.config.CliSocket = socket
} }
if c.config.ConfigPath == "" {
c.config.ConfigPath = configfile
}
c.AttachDatabase() c.AttachDatabase()
c.IsRunning = IsRunningChannel c.IsRunning = IsRunningChannel
//TODO: Create client config/CLI adapter if err := c.readClientConfig(configFileClient); err != nil {
c.clients["ciena-mcp"] = ciena.NewMCPClient("141.100.70.170:8080", "", "", &c.database) log.Fatal(err)
}
} }
func (c *Core) AttachDatabase() { func (c *Core) AttachDatabase() {
...@@ -61,13 +54,8 @@ func (c *Core) AttachDatabase() { ...@@ -61,13 +54,8 @@ func (c *Core) AttachDatabase() {
func (c *Core) Shutdown() { func (c *Core) Shutdown() {
stopIt := <- c.IsRunning <- c.IsRunning
if !stopIt { log.Info("Received shutdown signal. Shutting down")
log.Debug("Shutdown() received action to shutdown")
}else {
log.Debug("Shutdown() received something else.")
}
f, err := os.Create(c.config.ConfigPath) f, err := os.Create(c.config.ConfigPath)
if err != nil { if err != nil {
...@@ -77,6 +65,42 @@ func (c *Core) Shutdown() { ...@@ -77,6 +65,42 @@ func (c *Core) Shutdown() {
if err := enc.Encode(c.config); err != nil { if err := enc.Encode(c.config); err != nil {
log.Fatal(err) log.Fatal(err)
} }
log.Info("Shutdown complete")
os.Exit(0) 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
package interfaces package interfaces
type Client interface { type Client interface {
GetConfig() string GetConfig() ClientConfig
} }
package interfaces
type ClientConfig struct {
Identifier string `toml:"identifier"`
Endpoint string `toml:"endpoint"`
Username string `toml:"username"`
Password string `toml:"password"`
}
...@@ -17,7 +17,7 @@ func StartAndRun(socket, filename string, IsRunningChannel chan bool) { ...@@ -17,7 +17,7 @@ func StartAndRun(socket, filename string, IsRunningChannel chan bool) {
clients: make(map[string]interfaces.Client), clients: make(map[string]interfaces.Client),
database: database.Database{}, database: database.Database{},
} }
core.Init(socket, filename, IsRunningChannel) core.Init(socket, filename,"", IsRunningChannel)
// Start the GRCP CLI // Start the GRCP CLI
go getCLIGoing(&core) go getCLIGoing(&core)
go core.Shutdown() go core.Shutdown()
...@@ -25,10 +25,8 @@ func StartAndRun(socket, filename string, IsRunningChannel chan bool) { ...@@ -25,10 +25,8 @@ func StartAndRun(socket, filename string, IsRunningChannel chan bool) {
log.Info("and ready for take off") log.Info("and ready for take off")
//Just to produce some signs of vitality... //Just to produce some signs of vitality...
for (true) { for {
time.Sleep(10 * time.Second) time.Sleep(10 * time.Second)
log.Debug("Still alive...") log.Debug("Still alive...")
} }
log.Info("Good bye....!")
} }
...@@ -4,6 +4,7 @@ import ( ...@@ -4,6 +4,7 @@ import (
"bytes" "bytes"
"code.fbi.h-da.de/cocsn/gosdn/database" "code.fbi.h-da.de/cocsn/gosdn/database"
"code.fbi.h-da.de/cocsn/gosdn/log" "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" "code.fbi.h-da.de/cocsn/gosdn/restconf/util"
apiclient "code.fbi.h-da.de/cocsn/swagger/apis/mcp/client" apiclient "code.fbi.h-da.de/cocsn/swagger/apis/mcp/client"
"crypto/tls" "crypto/tls"
...@@ -19,15 +20,15 @@ type MCPClient struct { ...@@ -19,15 +20,15 @@ type MCPClient struct {
client *apiclient.ServiceTopologyTAPI client *apiclient.ServiceTopologyTAPI
database *database.Database database *database.Database
buffer *bytes.Buffer buffer *bytes.Buffer
config *interfaces.ClientConfig
} }
func (c MCPClient) GetConfig() string { func (c MCPClient) GetConfig() interfaces.ClientConfig {
//TODO: Fill with life return *c.config
return "..."
} }
//NewMCPClient creates a client //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 // create the transport
transport := httptransport.New(endpoint, "/", nil) transport := httptransport.New(endpoint, "/", nil)
transport.Transport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: true} transport.Transport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
...@@ -45,6 +46,7 @@ func NewMCPClient(endpoint, username, password string, database *database.Databa ...@@ -45,6 +46,7 @@ func NewMCPClient(endpoint, username, password string, database *database.Databa
client: client, client: client,
database: database, database: database,
buffer: buffer, buffer: buffer,
config: config,
} }
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment