From 20c8e382c4eb6269581be42fd792266bfeebcf07 Mon Sep 17 00:00:00 2001
From: Malte Bauch <malte.bauch@stud.h-da.de>
Date: Mon, 15 Feb 2021 19:30:31 +0100
Subject: [PATCH] removed DeviceConfig in Device

since the values from DeviceConfig are only needed in transport they got
moved into the device options. DeviceConfig is therefore redundant.
---
 nucleus/cli-handling.go           |  8 +++----
 nucleus/device.go                 | 36 ++++++++++++-------------------
 nucleus/gnmi_transport.go         | 16 +++++++++++++-
 nucleus/principalNetworkDomain.go |  2 +-
 nucleus/transport.go              |  1 +
 5 files changed, 35 insertions(+), 28 deletions(-)

diff --git a/nucleus/cli-handling.go b/nucleus/cli-handling.go
index e46979d9d..81a1a697d 100644
--- a/nucleus/cli-handling.go
+++ b/nucleus/cli-handling.go
@@ -177,9 +177,9 @@ func (s *server) GetAllPNDs(ctx context.Context, in *emptypb.Empty) (*pb.AllPNDs
 		for uuidDevice, device := range pnd.(*pndImplementation).devices {
 			tmpDevice := pb.Device{
 				Uuid:     uuidDevice.String(),
-				Address:  device.Config.Address,
-				Username: device.Config.Username,
-				Password: device.Config.Password}
+				Address:  device.Transport.GetOptions().GetAddress(),
+				Username: device.Transport.GetOptions().GetUsername(),
+				Password: device.Transport.GetOptions().GetPassword()}
 			devices = append(devices, &tmpDevice)
 		}
 		tmpPND := pb.PND{
@@ -231,7 +231,7 @@ func (s *server) AddDevice(ctx context.Context, in *pb.AddDeviceRequest) (*pb.Ad
 		return &pb.AddDeviceReply{Message: err.Error()}, err
 	}
 
-	return &pb.AddDeviceReply{Message: "Added new Device: " + newDevice.Config.Uuid.String()}, err
+	return &pb.AddDeviceReply{Message: "Added new Device: " + newDevice.Uuid.String()}, err
 }
 
 //HandleDeviceGetRequest handles a GET request via pnd.Request()
diff --git a/nucleus/device.go b/nucleus/device.go
index 694a2d000..95a104c46 100644
--- a/nucleus/device.go
+++ b/nucleus/device.go
@@ -1,20 +1,22 @@
 package nucleus
 
 import (
+	"errors"
+
 	"github.com/google/uuid"
 	"github.com/openconfig/ygot/ygot"
 )
 
 type Device struct {
+	// Uuid represents the Devices UUID
+	Uuid uuid.UUID
+
 	// Device inherits properties of ygot.GoStruct
 	ygot.GoStruct
 
 	// SBI is the device's southbound interface implementation
 	SBI SouthboundInterface
 
-	// Config is the device's config. Under revision
-	Config DeviceConfig
-
 	// Transport is the device's Transport implementation
 	Transport Transport
 }
@@ -22,29 +24,19 @@ type Device struct {
 //NewDevice creates a Device
 func NewDevice(ttype string, sbi SouthboundInterface, opts TransportOptions) (*Device, error) {
 	var transport Transport
-	//TODO: change to switch if supported transports increase
-	if ttype == "gnmi" {
+	switch ttype {
+	case "gnmi":
 		transport = NewGnmiTransport(opts.(*gnmiTransportOptions))
-	} else if ttype == "restconf" {
+	case "restconf":
 		//TODO: implement restconf -> NewRestConfTransport(opts)
+	default:
+		return nil, errors.New("Unknown transport type.")
+
 	}
 	return &Device{
-		GoStruct: sbi.Schema().Root,
-		SBI:      sbi,
-		//TODO: needed? maybe change to TransportOptions
-		Config: DeviceConfig{
-			Uuid:     uuid.New(),
-			Address:  opts.GetAddress(),
-			Username: opts.GetUsername(),
-			Password: opts.GetPassword(),
-		},
+		Uuid:      uuid.New(),
+		GoStruct:  sbi.Schema().Root,
+		SBI:       sbi,
 		Transport: transport,
 	}, nil
 }
-
-type DeviceConfig struct {
-	Uuid     uuid.UUID
-	Address  string
-	Username string
-	Password string
-}
diff --git a/nucleus/gnmi_transport.go b/nucleus/gnmi_transport.go
index c55c8d097..7285b3f53 100644
--- a/nucleus/gnmi_transport.go
+++ b/nucleus/gnmi_transport.go
@@ -15,11 +15,15 @@ import (
 type Gnmi struct {
 	SetNode  func(schema *yang.Entry, root interface{}, path *gpb.Path, val interface{}, opts ...ytypes.SetNodeOpt) error
 	RespChan chan *gpb.SubscribeResponse
+	options  *gnmiTransportOptions
 	config   *gnmi.Config
 }
 
 func NewGnmiTransport(opts *gnmiTransportOptions) *Gnmi {
-	ngt := &Gnmi{SetNode: opts.setNode}
+	ngt := &Gnmi{
+		SetNode: opts.setNode,
+		options: opts,
+	}
 	config := ngt.CreateConfig(opts)
 	ngt.SetConfig(config)
 	return ngt
@@ -35,6 +39,16 @@ func (g *Gnmi) GetConfig() *gnmi.Config {
 	return g.config
 }
 
+//SetConfig sets the config of gnmi
+func (g *Gnmi) SetOptions(to TransportOptions) {
+	g.options = to.(*gnmiTransportOptions)
+}
+
+//GetConfig returns the gnmi config
+func (g *Gnmi) GetOptions() TransportOptions {
+	return g.options
+}
+
 //CreateConfig creates a new gnmi config based on the given parameters
 func (g *Gnmi) CreateConfig(opts *gnmiTransportOptions) *gnmi.Config {
 	return &gnmi.Config{
diff --git a/nucleus/principalNetworkDomain.go b/nucleus/principalNetworkDomain.go
index bc3307cb5..5cb15f72d 100644
--- a/nucleus/principalNetworkDomain.go
+++ b/nucleus/principalNetworkDomain.go
@@ -110,7 +110,7 @@ func (pnd *pndImplementation) removeSbi(sbiIdentifier string) error {
 }
 
 func (pnd *pndImplementation) addDevice(device *Device) error {
-	pnd.devices[device.Config.Uuid] = device
+	pnd.devices[device.Uuid] = device
 	return nil
 }
 
diff --git a/nucleus/transport.go b/nucleus/transport.go
index 814e750f1..ae5d676d4 100644
--- a/nucleus/transport.go
+++ b/nucleus/transport.go
@@ -15,6 +15,7 @@ type Transport interface {
 	Set(ctx context.Context, params ...string) (interface{}, error)
 	Subscribe(ctx context.Context, params ...string) error
 	Type() string
+	GetOptions() TransportOptions
 	ProcessResponse(resp interface{}, root interface{}, models *ytypes.Schema) error
 }
 
-- 
GitLab