Skip to content
Snippets Groups Projects
controller.go 1.48 KiB
Newer Older
  • Learn to ignore specific revisions
  • package nucleus
    
    import (
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    	"code.fbi.h-da.de/cocsn/gosdn/nucleus/interfaces"
    	"encoding/json"
    	"io/ioutil"
    	"log"
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    /*
    +-- type controllerConfig struct
    +-- type Core struct
    	+-- client *restconf.Client
    	+-- db     *database.Database
    	+-- config controllerConfig
    +-- func Init(){
    		read.config
    		db.create/attach
    		if config.client{
    			client.init
    			}
    		}
    */
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    type controllerConfig struct {
    	CliSocket        string `json:"cli_socket"`
    	DatabaseSocket   string `json:"database_socket"`
    	DatabaseUser     string `json:"database_user,omitempty"`
    	DatabasePassword string `json:"database_password,omitempty"`
    	DatabaseCrypto   bool   `json:"database_crypto,omitempty"`
    	ConfigPath       string `json:"config_path"`
    }
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    type Core struct {
    	clients  []interfaces.Client
    	database interfaces.Database
    	config   controllerConfig
    }
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    func (c Core) Init(socket, filename string) {
    	if filename == "" {
    		filename = "gosdn.json"
    	}
    	config, err := ioutil.ReadFile(filename)
    
    	if err != nil {
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    		log.Fatal(err)
    
    Martin Stiemerling's avatar
    Martin Stiemerling committed
    
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    	c.config = controllerConfig{}
    	if err := json.Unmarshal(config, &c.config); err != nil {
    		log.Fatal(err)
    	}
    	if socket != "localhost:55055" {
    		c.config.CliSocket = socket
    
    Martin Stiemerling's avatar
    Martin Stiemerling committed
    	}
    
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    	c.database = interfaces.NewDatabaseClient(c.config.DatabaseSocket, c.config.DatabaseUser, c.config.DatabasePassword, c.config.DatabaseCrypto)
    }
    
    func (c Core) Shutdown() {
    	config, err := json.Marshal(c.config)
    	if err != nil {
    		log.Fatal(err)
    	}
    	if err := ioutil.WriteFile("gosdn.json", config, 0644); err != nil {
    		log.Fatal(err)
    	}
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    }