diff --git a/http.go b/http.go index ad8016e188ec43508e40bc2af8eb66ddabc0fff8..cc44a1059dd61e717900ef725af22718e95b9b9d 100644 --- a/http.go +++ b/http.go @@ -172,10 +172,6 @@ func httpApi(writer http.ResponseWriter, request *http.Request) { case "getDevice": deviceIdentifier := query.Get("identifier") - // id, _ := uuid.Parse(deviceIdentifier) - // idAsString := id.String() - - // if idAsString == "00000000-0000-0000-0000-000000000000" { device, err := httpPnd.MarshalDevice(deviceIdentifier) if err != nil { switch err.(type) { diff --git a/nucleus/initialise_test.go b/nucleus/initialise_test.go index a29b8d191d6c6a5782a1493cd148ad9caa647bbd..cb03dfa8260aeb7409b31c3f91f3ce3616f06bb1 100644 --- a/nucleus/initialise_test.go +++ b/nucleus/initialise_test.go @@ -129,15 +129,14 @@ func mockDevice() Device { func newPnd() pndImplementation { return pndImplementation{ - name: "default", - description: "default test pnd", - sbic: SbiStore{store{}}, - devices: DeviceStore{store{}}, - pendingChanges: ChangeStore{store{}}, - committedChanges: ChangeStore{store{}}, - confirmedChanges: ChangeStore{store{}}, - id: defaultPndID, - errChans: make(map[uuid.UUID]chan error), - deviceNameToUUIDLookup: make(map[string]uuid.UUID), + name: "default", + description: "default test pnd", + sbic: SbiStore{store{}}, + devices: NewDeviceStore(), + pendingChanges: ChangeStore{store{}}, + committedChanges: ChangeStore{store{}}, + confirmedChanges: ChangeStore{store{}}, + id: defaultPndID, + errChans: make(map[uuid.UUID]chan error), } } diff --git a/nucleus/principalNetworkDomain.go b/nucleus/principalNetworkDomain.go index 305edb9f10be5ec76861bf8fb3b81854699c8af2..a6d2f182ac586de9d0d6c332a327c6f30112648c 100644 --- a/nucleus/principalNetworkDomain.go +++ b/nucleus/principalNetworkDomain.go @@ -23,7 +23,7 @@ type PrincipalNetworkDomain interface { AddSbi(interface{}) error RemoveSbi(uuid.UUID) error AddDevice(interface{}) error - GetDevice(uuid uuid.UUID) (ygot.GoStruct, error) + GetDevice(identifier string) (*Device, error) RemoveDevice(uuid.UUID) error Devices() []uuid.UUID ChangeOND(uuid uuid.UUID, operation interface{}, path string, value ...string) error @@ -32,7 +32,6 @@ type PrincipalNetworkDomain interface { GetName() string GetDescription() string MarshalDevice(string) (string, error) - MarshalDeviceByName(name string) (string, error) ContainsDevice(uuid.UUID) bool GetSBIs() interface{} ID() uuid.UUID @@ -45,16 +44,15 @@ type PrincipalNetworkDomain interface { // NewPND creates a Principle Network Domain func NewPND(name, description string, id uuid.UUID, sbi SouthboundInterface) (PrincipalNetworkDomain, error) { pnd := &pndImplementation{ - name: name, - description: description, - sbic: SbiStore{store{}}, - devices: NewDeviceStore(), - pendingChanges: ChangeStore{store{}}, - committedChanges: ChangeStore{store{}}, - confirmedChanges: ChangeStore{store{}}, - id: id, - errChans: make(map[uuid.UUID]chan error), - deviceNameToUUIDLookup: make(map[string]uuid.UUID), + name: name, + description: description, + sbic: SbiStore{store{}}, + devices: NewDeviceStore(), + pendingChanges: ChangeStore{store{}}, + committedChanges: ChangeStore{store{}}, + confirmedChanges: ChangeStore{store{}}, + id: id, + errChans: make(map[uuid.UUID]chan error), } if err := pnd.sbic.Add(sbi); err != nil { return nil, err @@ -63,16 +61,15 @@ func NewPND(name, description string, id uuid.UUID, sbi SouthboundInterface) (Pr } type pndImplementation struct { - name string - description string - sbic SbiStore - devices *DeviceStore - pendingChanges ChangeStore - committedChanges ChangeStore - confirmedChanges ChangeStore - id uuid.UUID - errChans map[uuid.UUID]chan error - deviceNameToUUIDLookup map[string]uuid.UUID + name string + description string + sbic SbiStore + devices *DeviceStore + pendingChanges ChangeStore + committedChanges ChangeStore + confirmedChanges ChangeStore + id uuid.UUID + errChans map[uuid.UUID]chan error } func (pnd *pndImplementation) Pending() []uuid.UUID { @@ -187,12 +184,12 @@ func (pnd *pndImplementation) AddDevice(device interface{}) error { return pnd.addDevice(d) } -func (pnd *pndImplementation) GetDevice(uuid uuid.UUID) (ygot.GoStruct, error) { - d, err := pnd.devices.Get(FromString(uuid.String())) +func (pnd *pndImplementation) GetDevice(identifier string) (*Device, error) { + d, err := pnd.devices.Get(FromString(identifier)) if err != nil { return nil, err } - return ygot.DeepCopy(d.GoStruct) + return d, nil } // RemoveDevice removes a device from the PND @@ -220,51 +217,19 @@ func (pnd *pndImplementation) addDevice(device *Device) error { return err } - pnd.deviceNameToUUIDLookup[device.Name] = device.UUID - return nil } -func (pnd *pndImplementation) getDeviceByUUID(id uuid.UUID) (*Device, error) { - return pnd.devices.Get(FromString("wasd")) -} - -func (pnd *pndImplementation) getDeviceByName(name string) (*Device, error) { - // deviceUUID, found := pnd.deviceNameToUUIDLookup[name] - // if !found { - // return nil, &errors.ErrNotFound{ID: name} - // } - return pnd.devices.Get(FromString("wasd")) -} - func (pnd *pndImplementation) removeDevice(id uuid.UUID) error { return pnd.devices.Delete(id) } func (pnd *pndImplementation) MarshalDevice(identifier string) (string, error) { - // var foundDevice *Device - // var err error - foundDevice, err := pnd.devices.Get(FromString(identifier)) if err != nil { 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(foundDevice.GoStruct, "", "\t") if err != nil { return "", err @@ -278,28 +243,9 @@ func (pnd *pndImplementation) MarshalDevice(identifier string) (string, error) { return string(jsonTree), nil } -func (pnd *pndImplementation) MarshalDeviceByName(name string) (string, error) { - d, err := pnd.getDeviceByName(name) - if err != nil { - return "", err - } - - jsonTree, err := json.MarshalIndent(d.GoStruct, "", "\t") - if err != nil { - return "", err - } - log.WithFields(log.Fields{ - "pnd": pnd.id, - "ID": d.UUID, - "Name": d.Name, - }).Info("marshalled device") - - return string(jsonTree), nil -} - // Request sends a get request to a specific device func (pnd *pndImplementation) Request(uuid uuid.UUID, path string) error { - d, err := pnd.getDeviceByUUID(uuid) + d, err := pnd.devices.Get(FromString(uuid.String())) if err != nil { return err } @@ -331,7 +277,7 @@ func (pnd *pndImplementation) RequestAll(path string) error { // ChangeOND creates a change from the provided Operation, path and value. The Change is pending and func (pnd *pndImplementation) ChangeOND(uuid uuid.UUID, operation interface{}, path string, value ...string) error { - d, err := pnd.getDeviceByUUID(uuid) + d, err := pnd.devices.Get(FromString(uuid.String())) if err != nil { return err } diff --git a/nucleus/principalNetworkDomain_test.go b/nucleus/principalNetworkDomain_test.go index 09ef3d1b5498d3992c07fc644a0cb31d448a82ca..cdff336c074896cafb06aabb40ff8cb3493937d1 100644 --- a/nucleus/principalNetworkDomain_test.go +++ b/nucleus/principalNetworkDomain_test.go @@ -224,7 +224,7 @@ func Test_pndImplementation_ContainsDevice(t *testing.T) { t.Run(tt.name, func(t *testing.T) { pnd := newPnd() if tt.name != "fails empty" { - if err := pnd.devices.Add(tt.args.device); err != nil { + if err := pnd.devices.Add(tt.args.device, "test"); err != nil { t.Error(err) } } @@ -243,7 +243,7 @@ func Test_pndImplementation_Destroy(t *testing.T) { name string description string sbi SbiStore - devices DeviceStore + devices *DeviceStore } tests := []struct { name string @@ -347,7 +347,7 @@ func Test_pndImplementation_MarshalDevice(t *testing.T) { if err := pnd.addDevice(d); err != nil { t.Error(err) } - got, err := pnd.MarshalDevice(tt.args.uuid) + got, err := pnd.MarshalDevice(tt.args.uuid.String()) if (err != nil) != tt.wantErr { t.Errorf("MarshalDevice() error = %v, wantErr %v", err, tt.wantErr) return @@ -413,7 +413,7 @@ func Test_pndImplementation_RemoveSbi(t *testing.T) { name: "test-remove-sbi", description: "test-remove-sbi", sbic: SbiStore{store{}}, - devices: DeviceStore{store{}}, + devices: NewDeviceStore(), id: defaultPndID, } if tt.name != "fails empty" { @@ -694,7 +694,7 @@ func Test_pndImplementation_GetDevice(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - got, err := pnd.GetDevice(tt.args.uuid) + got, err := pnd.GetDevice(tt.args.uuid.String()) if (err != nil) != tt.wantErr { t.Errorf("GetDevice() error = %v, wantErr %v", err, tt.wantErr) return @@ -742,7 +742,7 @@ func Test_pndImplementation_GetDeviceByName(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - foundDevice, err := p.getDeviceByName(tt.args.name) + foundDevice, err := p.GetDevice(tt.args.name) if (err != nil) != tt.wantErr { t.Errorf("GetDeviceByName() error = %v, wantErr %v", err, tt.wantErr) return