Newer
Older
"code.fbi.h-da.de/cocsn/gosdn/database"
Manuel Kieweg
committed
"code.fbi.h-da.de/cocsn/gosdn/nucleus/interfaces"
"code.fbi.h-da.de/cocsn/gosdn/sbi/restconf/client/ciena"
"github.com/BurntSushi/toml"
Manuel Kieweg
committed
type controllerConfig struct {
CliSocket string
DatabaseSocket string
DatabaseUser string
DatabasePassword string
DatabaseCrypto bool
ConfigPath string
}
type clientConfigs struct {
Client []interfaces.ClientConfig `toml:"client"`
}
// Core is the representation of the controllers core
Manuel Kieweg
committed
//Assert type with clients[key].(*MCPClient)
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)
Manuel Kieweg
committed
if socket != "localhost:55055" {
c.config.CliSocket = socket
}
c.IsRunning = IsRunningChannel
Manuel Kieweg
committed
if err := c.readClientConfig(configFileClient); err != nil {
log.Fatal(err)
}
// AttachDatabase connects to the database and passes the connectio to the controller core
Manuel Kieweg
committed
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
log.Info("Received shutdown signal. Shutting down")
Manuel Kieweg
committed
f, err := os.Create(c.config.ConfigPath)
Manuel Kieweg
committed
enc := toml.NewEncoder(f)
if err := enc.Encode(c.config); err != nil {
log.Fatal(err)
}
log.Info("Shutdown complete")
os.Exit(0)
Manuel Kieweg
committed
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
}
func (c *Core) readControllerConfig(configFileController string) error {
if configFileController == "" {
configFileController = "configs/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 = "configs/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
}