Skip to content
Snippets Groups Projects
controller.go 2.11 KiB
Newer Older
  • Learn to ignore specific revisions
  • package nucleus
    
    import (
    
    	"code.fbi.h-da.de/cocsn/gosdn/database"
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    	"github.com/BurntSushi/toml"
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    	"github.com/google/uuid"
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    	log "github.com/sirupsen/logrus"
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    	"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
    }
    
    // Core is the representation of the controllers core
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    type Core struct {
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    	southboundInterfaces   map[string]SouthboundInterface
    	prinipalNetworkDomains map[uuid.UUID]PrincipalNetworkDomain
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    	database               database.Database
    	config                 controllerConfig
    	IsRunning              chan bool
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    }
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    //Init does start-up housekeeping like reading controller 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()
    
    
    // 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
    }