Skip to content
Snippets Groups Projects
controller.go 2.86 KiB
Newer Older
  • Learn to ignore specific revisions
  • package gosdn
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    	"context"
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    	"net"
    
    	"net/http"
    	"os"
    	"os/signal"
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    	"time"
    
    	"code.fbi.h-da.de/cocsn/gosdn/interfaces/southbound"
    
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    	"github.com/google/uuid"
    	log "github.com/sirupsen/logrus"
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    	"github.com/spf13/viper"
    	"google.golang.org/grpc"
    
    	pb "code.fbi.h-da.de/cocsn/api/go/gosdn/core"
    	ppb "code.fbi.h-da.de/cocsn/api/go/gosdn/pnd"
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    	spb "code.fbi.h-da.de/cocsn/api/go/gosdn/southbound"
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    	nbi "code.fbi.h-da.de/cocsn/gosdn/northbound/server"
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    
    	"code.fbi.h-da.de/cocsn/gosdn/database"
    
    	"code.fbi.h-da.de/cocsn/gosdn/nucleus"
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    var coreLock sync.RWMutex
    var coreOnce sync.Once
    
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    // Core is the representation of the controller's core
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    type Core struct {
    
    	// deprecated
    	database database.Database
    
    
    	pndc       *nucleus.PndStore
    
    	httpServer *http.Server
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    	grpcServer *grpc.Server
    	nbi        *nbi.NorthboundInterface
    
    	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:     nucleus.NewPndStore(),
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    		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 := startGrpcServer(); err != nil {
    
    Martin Stiemerling's avatar
    Martin Stiemerling committed
    	}
    
    	coreLock.Lock()
    
    	startHttpServer()
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    	coreLock.Unlock()
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    	return createSouthboundInterfaces()
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    }
    
    func startGrpcServer() error {
    	sock := viper.GetString("socket")
    	lis, err := net.Listen("tcp", sock)
    	if err != nil {
    		return err
    	}
    	c.grpcServer = grpc.NewServer()
    	c.nbi = nbi.NewNBI(c.pndc)
    	pb.RegisterCoreServer(c.grpcServer, c.nbi.Core)
    	ppb.RegisterPndServer(c.grpcServer, c.nbi.Pnd)
    	go func() {
    		if err := c.grpcServer.Serve(lis); err != nil {
    			log.Fatal(err)
    		}
    	}()
    
    	return nil
    
    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 := nucleus.NewSBI(spb.Type_OPENCONFIG)
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    	return createPrincipalNetworkDomain(sbi)
    }
    
    // createPrincipalNetworkDomain initializes the controller with an initial PND
    
    func createPrincipalNetworkDomain(s southbound.SouthboundInterface) error {
    
    	pnd, err := nucleus.NewPND("base", "gosdn base pnd", uuid.New(), s)
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    	if err != nil {
    		return err
    	}
    
    	err = c.pndc.Add(pnd)
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    	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")
    
    	coreLock.Lock()
    	defer coreLock.Unlock()
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    	c.grpcServer.GracefulStop()
    
    	return stopHttpServer()
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    }