From 6aacafa6d43cfbdce7728d291d8d95494cf14cff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Sterba?= <andre.sterba@stud.h-da.de> Date: Thu, 13 May 2021 10:17:39 +0200 Subject: [PATCH] Unify marshallDevice interface --- http.go | 2 +- nucleus/device.go | 12 --------- nucleus/principalNetworkDomain.go | 45 +++++++++++++++---------------- 3 files changed, 23 insertions(+), 36 deletions(-) diff --git a/http.go b/http.go index 45bd8e78c..73c442972 100644 --- a/http.go +++ b/http.go @@ -181,7 +181,7 @@ func httpApi(writer http.ResponseWriter, request *http.Request) { idAsString := id.String() if idAsString == "00000000-0000-0000-0000-000000000000" { - device, err := httpPnd.MarshalDeviceByName(deviceIdentifier) + device, err := httpPnd.MarshalDevice(deviceIdentifier) if err != nil { switch err.(type) { case *errors.ErrNotFound: diff --git a/nucleus/device.go b/nucleus/device.go index 175eb2f4a..ccfa738f6 100644 --- a/nucleus/device.go +++ b/nucleus/device.go @@ -7,18 +7,6 @@ import ( "github.com/openconfig/ygot/ygot" ) -type DeviceIdentifier interface { - IsDeviceIdentifier() -} - -type StringIdentifier string - -func (si StringIdentifier) IsDeviceIdentifier() {} - -type UUIDIdentifier uuid.UUID - -func (ui UUIDIdentifier) IsDeviceIdentifier() {} - // Device represents an Orchestrated Network Device (OND) which is managed by // nucleus type Device struct { diff --git a/nucleus/principalNetworkDomain.go b/nucleus/principalNetworkDomain.go index 7acc16311..f130a89d6 100644 --- a/nucleus/principalNetworkDomain.go +++ b/nucleus/principalNetworkDomain.go @@ -31,7 +31,7 @@ type PrincipalNetworkDomain interface { RequestAll(string) error GetName() string GetDescription() string - MarshalDevice(uuid.UUID) (string, error) + MarshalDevice(interface{}) (string, error) MarshalDeviceByName(name string) (string, error) ContainsDevice(uuid.UUID) bool GetSBIs() interface{} @@ -225,20 +225,6 @@ func (pnd *pndImplementation) addDevice(device *Device) error { return nil } -func (pnd *pndImplementation) getDevice(identifier DeviceIdentifier) (*Device, error) { - stringIdentifier, ok := identifier.(StringIdentifier) - if ok { - return pnd.getDeviceByName(string(stringIdentifier)) - } - - uuidIdentifier, ok := identifier.(UUIDIdentifier) - if ok { - return pnd.getDeviceByUUID(uuid.UUID(uuidIdentifier)) - } - - return nil, &errors.ErrNotFound{ID: identifier} -} - func (pnd *pndImplementation) getDeviceByUUID(id uuid.UUID) (*Device, error) { return pnd.devices.Get(id) } @@ -255,20 +241,33 @@ func (pnd *pndImplementation) removeDevice(id uuid.UUID) error { return pnd.devices.Delete(id) } -func (pnd *pndImplementation) MarshalDevice(uuid uuid.UUID) (string, error) { - d, err := pnd.getDeviceByUUID(uuid) - if err != nil { - return "", err +func (pnd *pndImplementation) MarshalDevice(identifier interface{}) (string, error) { + var foundDevice *Device + var err error + + switch id := identifier.(type) { + case uuid.UUID: + foundDevice, err = pnd.getDeviceByUUID(id) + if err != nil { + return "", err + } + case string: + foundDevice, err = pnd.getDeviceByName(id) + if err != nil { + return "", err + } + default: + return "", &errors.ErrNotFound{ID: identifier} } - jsonTree, err := json.MarshalIndent(d.GoStruct, "", "\t") + jsonTree, err := json.MarshalIndent(foundDevice.GoStruct, "", "\t") if err != nil { return "", err } log.WithFields(log.Fields{ - "pnd": pnd.id, - "ID": uuid, - "Name": d.Name, + "pnd": pnd.id, + "Identifier": identifier, + "Name": foundDevice.Name, }).Info("marshalled device") return string(jsonTree), nil -- GitLab