Skip to content
Snippets Groups Projects
controller.go 2.8 KiB
Newer Older
  • Learn to ignore specific revisions
  • package nucleus
    
    import (
    
    	"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/sbi/restconf/client/ciena"
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    	"github.com/BurntSushi/toml"
    	"os"
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    type controllerConfig struct {
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    	CliSocket        string
    	DatabaseSocket   string
    	DatabaseUser     string
    	DatabasePassword string
    	DatabaseCrypto   bool
    	ConfigPath       string
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    }
    
    type clientConfigs struct {
    	Client []interfaces.ClientConfig `toml:"client"`
    
    // Core is the representation of the controllers core
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    type Core struct {
    
    	//Assert type with clients[key].(*MCPClient)
    
    	clients   map[string]interfaces.Client
    	database  database.Database
    	config    controllerConfig
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    }
    
    //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 {
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    		log.Fatal(err)
    	}
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    	if socket != "localhost:55055" {
    		c.config.CliSocket = socket
    
    Martin Stiemerling's avatar
    Martin Stiemerling committed
    	}
    
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    	c.AttachDatabase()
    
    
    	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's avatar
    Manuel Kieweg committed
    func (c *Core) AttachDatabase() {
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    	c.database = database.NewDatabaseClient(c.config.DatabaseSocket, c.config.DatabaseUser, c.config.DatabasePassword, c.config.DatabaseCrypto)
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    }
    
    
    // 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")
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    	f, err := os.Create(c.config.ConfigPath)
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    	if err != nil {
    		log.Fatal(err)
    	}
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    	enc := toml.NewEncoder(f)
    	if err := enc.Encode(c.config); err != nil {
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    		log.Fatal(err)
    	}
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    }
    
    func (c *Core) readControllerConfig(configFileController string) error {
    
    Malte Bauch's avatar
    Malte Bauch committed
    		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 {
    
    Malte Bauch's avatar
    Malte Bauch committed
    		configFileClient = "configs/clients.toml"
    
    	if _, err := os.Stat(configFileClient); err != nil {
    
    	clients := clientConfigs{}
    
    	if _, err := toml.DecodeFile(configFileClient, &clients); err != nil {
    
    	for _, client := range clients.Client {
    
    		c.clients[client.Identifier] = ciena.NewMCPClient(client.Endpoint, client.Username, client.Password, &c.database, &client)