diff --git a/http.go b/http.go index 45bd8e78c25da9fbe89d8660ec62b0bb08b2cf04..73c4429722e76afa570073172aac0230cda5a119 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 175eb2f4a6de6648b403a653c5687f0c913fcf33..ccfa738f62d99f6cb132ee25222726131e573e99 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 7acc16311588352ee04cb65c179bb499cb101c10..f130a89d602c2675a16e31457627ec69b7fb0ceb 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