Skip to content
Snippets Groups Projects
Commit 6aacafa6 authored by André Sterba's avatar André Sterba
Browse files

Unify marshallDevice interface

parent d3acdec8
No related branches found
No related tags found
1 merge request!150Let user set a name for a device or autogenerate it
Pipeline #70484 failed
...@@ -181,7 +181,7 @@ func httpApi(writer http.ResponseWriter, request *http.Request) { ...@@ -181,7 +181,7 @@ func httpApi(writer http.ResponseWriter, request *http.Request) {
idAsString := id.String() idAsString := id.String()
if idAsString == "00000000-0000-0000-0000-000000000000" { if idAsString == "00000000-0000-0000-0000-000000000000" {
device, err := httpPnd.MarshalDeviceByName(deviceIdentifier) device, err := httpPnd.MarshalDevice(deviceIdentifier)
if err != nil { if err != nil {
switch err.(type) { switch err.(type) {
case *errors.ErrNotFound: case *errors.ErrNotFound:
......
...@@ -7,18 +7,6 @@ import ( ...@@ -7,18 +7,6 @@ import (
"github.com/openconfig/ygot/ygot" "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 // Device represents an Orchestrated Network Device (OND) which is managed by
// nucleus // nucleus
type Device struct { type Device struct {
......
...@@ -31,7 +31,7 @@ type PrincipalNetworkDomain interface { ...@@ -31,7 +31,7 @@ type PrincipalNetworkDomain interface {
RequestAll(string) error RequestAll(string) error
GetName() string GetName() string
GetDescription() string GetDescription() string
MarshalDevice(uuid.UUID) (string, error) MarshalDevice(interface{}) (string, error)
MarshalDeviceByName(name string) (string, error) MarshalDeviceByName(name string) (string, error)
ContainsDevice(uuid.UUID) bool ContainsDevice(uuid.UUID) bool
GetSBIs() interface{} GetSBIs() interface{}
...@@ -225,20 +225,6 @@ func (pnd *pndImplementation) addDevice(device *Device) error { ...@@ -225,20 +225,6 @@ func (pnd *pndImplementation) addDevice(device *Device) error {
return nil 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) { func (pnd *pndImplementation) getDeviceByUUID(id uuid.UUID) (*Device, error) {
return pnd.devices.Get(id) return pnd.devices.Get(id)
} }
...@@ -255,20 +241,33 @@ func (pnd *pndImplementation) removeDevice(id uuid.UUID) error { ...@@ -255,20 +241,33 @@ func (pnd *pndImplementation) removeDevice(id uuid.UUID) error {
return pnd.devices.Delete(id) return pnd.devices.Delete(id)
} }
func (pnd *pndImplementation) MarshalDevice(uuid uuid.UUID) (string, error) { func (pnd *pndImplementation) MarshalDevice(identifier interface{}) (string, error) {
d, err := pnd.getDeviceByUUID(uuid) var foundDevice *Device
if err != nil { var err error
return "", err
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 { if err != nil {
return "", err return "", err
} }
log.WithFields(log.Fields{ log.WithFields(log.Fields{
"pnd": pnd.id, "pnd": pnd.id,
"ID": uuid, "Identifier": identifier,
"Name": d.Name, "Name": foundDevice.Name,
}).Info("marshalled device") }).Info("marshalled device")
return string(jsonTree), nil return string(jsonTree), nil
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment