From cfe4a3b26f5ea0254d6204468ee2813f465969b8 Mon Sep 17 00:00:00 2001
From: Malte Bauch <malte.bauch@stud.h-da.de>
Date: Wed, 27 Jan 2021 18:56:11 +0100
Subject: [PATCH] removed gob for persisting and changed:

- added initial sbis in controller
- added getter/setter transport in southbound
- added multiple geeter in principalNetworkDomain
---
 nucleus/controller.go             | 36 +++++++------------------------
 nucleus/gnmi_transport.go         | 16 +++++++-------
 nucleus/principalNetworkDomain.go | 11 +++++++---
 nucleus/southbound.go             | 20 ++++++++++++++++-
 4 files changed, 43 insertions(+), 40 deletions(-)

diff --git a/nucleus/controller.go b/nucleus/controller.go
index 7563da754..ec9a7df14 100644
--- a/nucleus/controller.go
+++ b/nucleus/controller.go
@@ -1,7 +1,6 @@
 package nucleus
 
 import (
-	"encoding/gob"
 	"fmt"
 	"os"
 
@@ -33,14 +32,8 @@ 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{})
-	gob.Register(&OpenConfig{})
 	c.AttachDatabase()
-
-	if err := c.loadPNDs(path); err != nil {
-		log.Info(err)
-	}
+	c.CreateSouthboundInterfaces()
 
 	c.IsRunning = IsRunningChannel
 }
@@ -50,27 +43,14 @@ 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
+// CreateSouthboundInterfaces initializes the controller with his SBIs
+func (c *Core) CreateSouthboundInterfaces() {
+	if len(c.southboundInterfaces) == 0 {
+		sbi := &AristaOC{}
+		t := &Gnmi{SetNode: sbi.SetNode()}
+		sbi.SetTransport(t)
+		c.southboundInterfaces[sbi.SbiIdentifier()] = sbi
 	}
-	defer f.Close()
-	return gob.NewDecoder(f).Decode(&c.principalNetworkDomains)
 }
 
 // Shutdown waits for the shutdown signal and gracefully shuts down once it arrived
diff --git a/nucleus/gnmi_transport.go b/nucleus/gnmi_transport.go
index c5f4054b9..313f61c13 100644
--- a/nucleus/gnmi_transport.go
+++ b/nucleus/gnmi_transport.go
@@ -11,7 +11,7 @@ import (
 )
 
 type Gnmi struct {
-	SetNode func(schema *yang.Entry, root interface{}, path *gpb.Path, val interface{}, opts ...ytypes.SetNodeOpt) error
+	SetNode  func(schema *yang.Entry, root interface{}, path *gpb.Path, val interface{}, opts ...ytypes.SetNodeOpt) error
 	RespChan chan *gpb.SubscribeResponse
 }
 
@@ -25,17 +25,17 @@ func (g *Gnmi) GetConfig() interface{} {
 
 // interface satisfaction for now
 // TODO: Convert to meaningfiul calls
-func (g *Gnmi)Get(ctx context.Context, params ...string) (interface{}, error){return  nil, nil}
-func (g *Gnmi)Set(ctx context.Context, params ...string) (interface{}, error){return  nil, nil}
-func (g *Gnmi)Subscribe(ctx context.Context, params ...string) error{
+func (g *Gnmi) Get(ctx context.Context, params ...string) (interface{}, error) { return nil, nil }
+func (g *Gnmi) Set(ctx context.Context, params ...string) (interface{}, error) { return nil, nil }
+func (g *Gnmi) Subscribe(ctx context.Context, params ...string) error {
 	return g.subscribe(ctx)
 }
 
-func (g *Gnmi)Type() string {
+func (g *Gnmi) Type() string {
 	return "gnmi"
 }
 
-func (g *Gnmi)ProcessResponse(resp interface{},root interface{}, s *ytypes.Schema) error {
+func (g *Gnmi) ProcessResponse(resp interface{}, root interface{}, s *ytypes.Schema) error {
 	models := s.SchemaTree
 	opts := []ytypes.SetNodeOpt{&ytypes.InitMissingElements{}, &ytypes.TolerateJSONInconsistencies{}}
 	r := resp.(*gpb.GetResponse)
@@ -105,8 +105,8 @@ func (g *Gnmi) subscribe(ctx context.Context) error {
 	}
 	opts := ctx.Value("opts").(*gnmi.SubscribeOptions)
 	go func() {
-		for  {
-			resp := <- g.RespChan
+		for {
+			resp := <-g.RespChan
 			if err := gnmi.LogSubscribeResponse(resp); err != nil {
 				log.Fatal(err)
 			}
diff --git a/nucleus/principalNetworkDomain.go b/nucleus/principalNetworkDomain.go
index 122346513..7d2d412d7 100644
--- a/nucleus/principalNetworkDomain.go
+++ b/nucleus/principalNetworkDomain.go
@@ -9,7 +9,8 @@ import (
 type PrincipalNetworkDomain interface {
 	GetName() string
 	GetDescription() string
-	GetDefaultSBIName() string
+	GetDevices() map[uuid.UUID]Device
+	GetSBIs() map[string]SouthboundInterface
 	Destroy() error
 	AddSbi(SouthboundInterface) error
 	RemoveSbi(string) error
@@ -41,12 +42,16 @@ func (pnd *pndImplementation) GetName() string {
 	return pnd.Name
 }
 
+func (pnd *pndImplementation) GetDevices() map[uuid.UUID]Device {
+	return pnd.Devices
+}
+
 func (pnd *pndImplementation) GetDescription() string {
 	return pnd.Description
 }
 
-func (pnd *pndImplementation) GetDefaultSBIName() string {
-	return pnd.Sbi["default"].SbiIdentifier()
+func (pnd *pndImplementation) GetSBIs() map[string]SouthboundInterface {
+	return pnd.Sbi
 }
 
 // Interface satisfaction
diff --git a/nucleus/southbound.go b/nucleus/southbound.go
index 4924845ac..e30dd2473 100644
--- a/nucleus/southbound.go
+++ b/nucleus/southbound.go
@@ -17,6 +17,8 @@ type SouthboundInterface interface {
 
 	SetNode() func(schema *yang.Entry, root interface{}, path *gpb.Path, val interface{}, opts ...ytypes.SetNodeOpt) error
 	Schema() *ytypes.Schema
+	SetTransport(t Transport)
+	GetTransport() Transport
 }
 
 type Tapi struct {
@@ -48,8 +50,16 @@ func (oc *OpenConfig) SetNode() func(schema *yang.Entry, root interface{}, path
 	}
 }
 
+func (oc *OpenConfig) GetTransport() Transport {
+	return oc.Transport
+}
+
+func (oc *OpenConfig) SetTransport(t Transport) {
+	oc.Transport = t
+}
+
 type AristaOC struct {
-	transport Transport
+	Transport Transport
 	schema    *ytypes.Schema
 }
 
@@ -73,3 +83,11 @@ func (oc *AristaOC) SetNode() func(schema *yang.Entry, root interface{}, path *g
 		return nil
 	}
 }
+
+func (oc *AristaOC) GetTransport() Transport {
+	return oc.Transport
+}
+
+func (oc *AristaOC) SetTransport(t Transport) {
+	oc.Transport = t
+}
-- 
GitLab