From 7539c7af2edf1127e35885f1aa0586eac0d5b505 Mon Sep 17 00:00:00 2001
From: Malte Bauch <malte.bauch@stud.h-da.de>
Date: Thu, 11 Feb 2021 17:16:03 +0100
Subject: [PATCH] removed GetDevices instead ContainsDevice is added

ContainsDevice() is enough to check if the given device uuid is present
in the current pnds device map.
For the cli device printing a JSONMarshal function was added.
---
 nucleus/cli-handling.go           | 10 ++++------
 nucleus/device.go                 |  6 +++---
 nucleus/principalNetworkDomain.go | 24 +++++++++++++++++++-----
 3 files changed, 26 insertions(+), 14 deletions(-)

diff --git a/nucleus/cli-handling.go b/nucleus/cli-handling.go
index 84fe3f70f..8a0218815 100644
--- a/nucleus/cli-handling.go
+++ b/nucleus/cli-handling.go
@@ -8,7 +8,6 @@ package nucleus
 
 import (
 	"context"
-	"encoding/json"
 	"errors"
 	"io"
 	"net"
@@ -261,9 +260,8 @@ func (s *server) HandleDeviceGetRequest(ctx context.Context, in *pb.DeviceGetReq
 		log.Info(err)
 		return &pb.DeviceGetReply{Message: err.Error()}, err
 	}
-	//the specific device a request will be sent to
-	device, exists := s.core.principalNetworkDomains[uuidPND].GetDevice(uuidDevice)
-	if exists != true {
+	//check if the device exists
+	if !pnd.ContainsDevice(uuidDevice) {
 		err := errors.New("Couldnt find device: UUID is wrong")
 		log.Info(err)
 		return &pb.DeviceGetReply{Message: err.Error()}, err
@@ -276,11 +274,11 @@ func (s *server) HandleDeviceGetRequest(ctx context.Context, in *pb.DeviceGetReq
 		return &pb.DeviceGetReply{Message: err.Error()}, err
 	}
 
-	d, err := json.MarshalIndent(device, "", "\t")
+	d, err := pnd.MarshalDevice(uuidDevice)
 	if err != nil {
 		log.Info(err)
 		return &pb.DeviceGetReply{Message: err.Error()}, err
 	}
 
-	return &pb.DeviceGetReply{Message: string(string(d))}, nil
+	return &pb.DeviceGetReply{Message: d}, nil
 }
diff --git a/nucleus/device.go b/nucleus/device.go
index 427fd049b..dcad6b388 100644
--- a/nucleus/device.go
+++ b/nucleus/device.go
@@ -10,13 +10,13 @@ type Device struct {
 	ygot.GoStruct
 
 	// SBI is the device's southbound interface implementation
-	SBI SouthboundInterface `json:"-"`
+	SBI SouthboundInterface
 
 	// Config is the device's config. Under revision
-	Config DeviceConfig `json:"-"`
+	Config DeviceConfig
 
 	// Transport is the device's Transport implementation
-	Transport Transport `json:"-"`
+	Transport Transport
 }
 
 //NewDevice creates a Device
diff --git a/nucleus/principalNetworkDomain.go b/nucleus/principalNetworkDomain.go
index ea2e51983..7ea7f83b8 100644
--- a/nucleus/principalNetworkDomain.go
+++ b/nucleus/principalNetworkDomain.go
@@ -2,6 +2,8 @@ package nucleus
 
 import (
 	"context"
+
+	"encoding/json"
 	"github.com/google/uuid"
 )
 
@@ -12,12 +14,13 @@ type PrincipalNetworkDomain interface {
 	AddSbi(SouthboundInterface) error
 	RemoveSbi(string) error
 	AddDevice(*Device) error
-	RemoveDevice(uuid uuid.UUID) error
+	RemoveDevice(uuid.UUID) error
 	Request(uuid.UUID, string) error
 	RequestAll(string) error
 	GetName() string
 	GetDescription() string
-	GetDevice(uuid.UUID) (*Device, bool)
+	MarshalDevice(uuid.UUID) (string, error)
+	ContainsDevice(uuid.UUID) bool
 	GetSBIs() map[string]SouthboundInterface
 }
 
@@ -46,9 +49,10 @@ func (pnd *pndImplementation) GetName() string {
 	return pnd.name
 }
 
-//GetDevice returns a specific device via the given uuid
-func (pnd *pndImplementation) GetDevice(uuid uuid.UUID) (*Device, bool) {
-	return pnd.getDevice(uuid)
+//HasDevice checks if the given device uuid is registered for this PND
+func (pnd *pndImplementation) ContainsDevice(uuid uuid.UUID) bool {
+	_, exists := pnd.devices[uuid]
+	return exists
 }
 
 //GetDescription returns the current description of the PND
@@ -120,6 +124,16 @@ func (pnd *pndImplementation) getDevice(uuid uuid.UUID) (*Device, bool) {
 	return device, exists
 }
 
+func (pnd *pndImplementation) MarshalDevice(uuid uuid.UUID) (string, error) {
+	d := pnd.devices[uuid]
+	json, err := json.MarshalIndent(d.GoStruct, "", "\t")
+	if err != nil {
+		return "", err
+	}
+
+	return string(json), nil
+}
+
 //Request sends a request for a specific device
 func (pnd *pndImplementation) Request(uuid uuid.UUID, path string) error {
 	d := pnd.devices[uuid]
-- 
GitLab