-
Malte Bauch authoredMalte Bauch authored
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
controller.go 2.28 KiB
package nucleus
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"code.fbi.h-da.de/cocsn/gosdn/database"
log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
)
// Core is the representation of the controllers core
type Core struct {
southboundInterfaces map[string]SouthboundInterface
principalNetworkDomains PrincipalNetworkDomainMap
database database.Database
IsRunning chan bool
}
//Initialize does start-up housekeeping like reading controller config files
func (c *Core) Initialize(IsRunningChannel chan bool) {
// Set config defaults
viper.SetDefault("socket", "localhost:55055")
// Set config path and read config
viper.SetConfigName("gosdn")
viper.SetConfigType("toml")
viper.AddConfigPath("/usr/local/etc/gosdn/")
viper.AddConfigPath("./configs/")
err := viper.ReadInConfig()
if err != nil {
log.Fatal(fmt.Errorf("Fatal error config file: %s \n", err))
}
c.AttachDatabase()
c.CreateSouthboundInterfaces()
err = c.UnMarshallPNDs()
if err != nil {
log.Info(err)
}
c.IsRunning = IsRunningChannel
}
// AttachDatabase connects to the database and passes the connection to the controller core
func (c *Core) AttachDatabase() {
c.database = database.NewDatabaseClient()
}
func (c *Core) MarshallPNDs() error {
b, err := json.MarshalIndent(c.principalNetworkDomains, "", "\t")
if err != nil {
return err
}
err = ioutil.WriteFile("./backup.json", b, 0644)
if err != nil {
return err
}
return nil
}
func (c *Core) UnMarshallPNDs() error {
backup, err := ioutil.ReadFile("./backup.json")
if err != nil {
return err
}
err = json.Unmarshal(backup, &c.principalNetworkDomains)
if err != nil {
return err
}
for _, v := range c.principalNetworkDomains {
log.Info(v.GetSBIs())
}
return nil
}
// CreateSouthboundInterfaces initializes the controller with his SBIs
func (c *Core) CreateSouthboundInterfaces() {
if len(c.southboundInterfaces) == 0 {
sbi := &AristaOC{}
sbi.SetDefaults()
c.southboundInterfaces[sbi.SbiIdentifier()] = sbi
}
}
// Shutdown waits for the shutdown signal and gracefully shuts down once it arrived
func (c *Core) Shutdown() {
<-c.IsRunning
log.Info("Received shutdown signal. Shutting down")
err := viper.WriteConfig()
if err != nil {
log.Fatal(err)
}
log.Info("Shutdown complete")
os.Exit(0)
}