diff --git a/configs/gosdn.toml b/configs/gosdn.toml index 229a018b6f844719e7fbb98a3ff18f4f672b6d0d..4cee085e92e1d2dd9565d205e8d2aac9bc8975aa 100644 --- a/configs/gosdn.toml +++ b/configs/gosdn.toml @@ -3,3 +3,4 @@ db.socket = "bolt://172.17.0.4:7687" db.user = "" db.password = "" db.crypto = false +pnd.path = "./pnds.gob" diff --git a/nucleus/cli-handling.go b/nucleus/cli-handling.go index 6ebe88cec126b0cb6255fb00e3363f8648ae7a7a..56efaff9f18cd46cc0ae4d2186b4bb13f25df876 100644 --- a/nucleus/cli-handling.go +++ b/nucleus/cli-handling.go @@ -152,3 +152,12 @@ func (s *server) TAPIGetLink(ctx context.Context, in *pb.TAPIRequest) (*pb.TAPIR // TODO: Implement return &pb.TAPIReply{Message: "Done"}, nil } + +func (s *server) CreatePND(ctx context.Context, in *pb.PNDRequest) (*pb.PNDReply, error) { + log.Info("Received: ", in.GetName()) + path := viper.GetString("pnd.path") + //TODO: change sbi + s.core.principalNetworkDomains[uuid.New()] = NewPND(in.GetName(), in.GetDescription(), s.core.southboundInterfaces["test"]) + s.core.savePNDs(path) + return &pb.PNDReply{Message: "Created new PND: " + in.GetName()}, nil +} diff --git a/nucleus/controller.go b/nucleus/controller.go index 9de78e91939e778db1c00f1c58b40d7d9ba9c72d..24dac33249ba5e28fe6cfe907b27466213a51c09 100644 --- a/nucleus/controller.go +++ b/nucleus/controller.go @@ -1,20 +1,22 @@ package nucleus import ( - "code.fbi.h-da.de/cocsn/gosdn/database" + "encoding/gob" "fmt" + "os" + + "code.fbi.h-da.de/cocsn/gosdn/database" "github.com/google/uuid" log "github.com/sirupsen/logrus" "github.com/spf13/viper" - "os" ) // Core is the representation of the controllers core type Core struct { - southboundInterfaces map[string]SouthboundInterface - prinipalNetworkDomains map[uuid.UUID]PrincipalNetworkDomain - database database.Database - IsRunning chan bool + southboundInterfaces map[string]SouthboundInterface + principalNetworkDomains map[uuid.UUID]PrincipalNetworkDomain + database database.Database + IsRunning chan bool } //Initialize does start-up housekeeping like reading controller config files @@ -31,16 +33,45 @@ func (c *Core) Initialize(IsRunningChannel chan bool) { if err != nil { log.Fatal(fmt.Errorf("Fatal error config file: %s \n", err)) } + path := viper.GetString("pnd.path") + gob.Register(&pndImplementation{}) c.AttachDatabase() + if err := c.loadPNDs(path); err != nil { + log.Info(err) + } + c.IsRunning = IsRunningChannel } -// AttachDatabase connects to the database and passes the connectio to the controller core +// AttachDatabase connects to the database and passes the connection to the controller core func (c *Core) AttachDatabase() { c.database = database.NewDatabaseClient() } +// TODO: the load and save functions for pnds are just temporary and should be +// moved to the database + +// SavePNDs imports the PNDs from last session +func (c *Core) savePNDs(path string) error { + f, err := os.Create(path) + if err != nil { + return err + } + defer f.Close() + return gob.NewEncoder(f).Encode(c.principalNetworkDomains) +} + +// loadPNDs imports the PNDs from last session +func (c *Core) loadPNDs(path string) error { + f, err := os.Open(path) + if err != nil { + return err + } + defer f.Close() + return gob.NewDecoder(f).Decode(&c.principalNetworkDomains) +} + // Shutdown waits for the shutdown signal and gracefully shuts down once it arrived func (c *Core) Shutdown() { <-c.IsRunning @@ -52,4 +83,4 @@ func (c *Core) Shutdown() { } log.Info("Shutdown complete") os.Exit(0) -} \ No newline at end of file +} diff --git a/nucleus/nucleus-core.go b/nucleus/nucleus-core.go index 47b265b96d5a743fbe55eb4f1d74b20ea8f7a65b..ae6d78056beb1e9c7d19e9385026f0117d033a91 100644 --- a/nucleus/nucleus-core.go +++ b/nucleus/nucleus-core.go @@ -1,9 +1,11 @@ package nucleus import ( + "time" + "code.fbi.h-da.de/cocsn/gosdn/database" + "github.com/google/uuid" log "github.com/sirupsen/logrus" - "time" ) //StartAndRun is used to start the core of the controller and any auxiliary services. @@ -13,7 +15,9 @@ func StartAndRun(IsRunningChannel chan bool) { // Initialize the Core core := Core{ - database: database.Database{}, + principalNetworkDomains: make(map[uuid.UUID]PrincipalNetworkDomain), + southboundInterfaces: make(map[string]SouthboundInterface), + database: database.Database{}, } core.Initialize(IsRunningChannel) // Start the GRCP CLI