Skip to content
Snippets Groups Projects
controller.go 2.27 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
    	"context"
    	"github.com/google/uuid"
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    	log "github.com/sirupsen/logrus"
    
    	"net/http"
    	"os"
    	"os/signal"
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    	"time"
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    var coreLock sync.RWMutex
    var coreOnce sync.Once
    
    
    // Core is the representation of the controllers core
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    type Core struct {
    
    	// deprecated
    	database database.Database
    
    
    	pndc       pndStore
    	sbic       sbiStore
    	httpServer *http.Server
    	stopChan   chan os.Signal
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    }
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    var c *Core
    
    
    func init() {
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    	c = &Core{
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    		database: database.Database{},
    		pndc:     pndStore{store{}},
    		sbic:     sbiStore{store{}},
    		stopChan: make(chan os.Signal, 1),
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    	}
    
    
    	// Setting up signal capturing
    	signal.Notify(c.stopChan, os.Interrupt)
    }
    
    // initialize does start-up housekeeping like reading controller config files
    func initialize() error {
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    	if err := createSouthboundInterfaces(); err != nil {
    
    Martin Stiemerling's avatar
    Martin Stiemerling committed
    	}
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    	// TODO: Start grpc listener here
    
    	if err := httpAPI(); err != nil {
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    	attachDatabase()
    
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    // attachDatabase connects to the database and passes the connection to the controller core
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    func attachDatabase() {
    
    	c.database = database.NewDatabaseClient()
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    }
    
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    // createSouthboundInterfaces initializes the controller with its supported SBIs
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    func createSouthboundInterfaces() error {
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    	sbi := &OpenConfig{id: uuid.New()}
    	if err := c.sbic.add(sbi); err != nil {
    		return err
    	}
    	return createPrincipalNetworkDomain(sbi)
    }
    
    // createPrincipalNetworkDomain initializes the controller with an initial PND
    
    func createPrincipalNetworkDomain(sbi SouthboundInterface) error {
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    	pnd, err := NewPND("base", "gosdn base pnd", uuid.New(), sbi)
    	if err != nil {
    		return err
    	}
    	err = c.pndc.add(pnd)
    	if err != nil {
    
    // Run calls initialize to start the controller
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    func Run(ctx context.Context) error {
    
    	var initError error
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    	coreOnce.Do(func() {
    
    		initError = initialize()
    	})
    	if initError != nil {
    		log.WithFields(log.Fields{}).Error(initError)
    		return initError
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    	}
    	log.WithFields(log.Fields{}).Info("initialisation finished")
    	for {
    		select {
    
    		case <-c.stopChan:
    			return shutdown()
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    		case <-ctx.Done():
    
    			return shutdown()
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    		case <-time.Tick(time.Minute):
    			log.Debug("up and running")
    		}
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    	}
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    func shutdown() error {
    
    	log.Info("shutting down controller")
    	return stopHttpServer()
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    }