[VOL-1614] Device Management update in the Core
This commit went over the device management of devices in the Core
and made the following changes:
1) Update the device state machine to not remove logical
device or ports when a device is disabled.
2) Fix some issues around device deletion
3) Add additional APIs between the Core and Adapters to handle
the scenarios of enable/disable/delete a device
4) Update the simulated Adapters to handle disable/reenable/delete
5) Add a new set of tests for teh device state machine.
Change-Id: Ib2be87ec011762d5315a6d54581a87c1891e92be
diff --git a/rw_core/core/device_ownership.go b/rw_core/core/device_ownership.go
index 97de41c..5deb1cb 100644
--- a/rw_core/core/device_ownership.go
+++ b/rw_core/core/device_ownership.go
@@ -200,20 +200,40 @@
func (da *DeviceOwnership) AbandonDevice(id string) error {
da.deviceMapLock.Lock()
defer da.deviceMapLock.Unlock()
- if o, exist := da.deviceMap[id]; exist {
+ if o, exist := da.deviceMap[id]; exist { // id is ownership key
+ // Need to clean up all deviceToKeyMap entries using this device as key
+ da.deviceToKeyMapLock.Lock()
+ defer da.deviceToKeyMapLock.Unlock()
+ for k, v := range da.deviceToKeyMap {
+ if id == v {
+ delete(da.deviceToKeyMap, k)
+ }
+ }
+ // Remove the device reference from the devicMap
+ delete(da.deviceMap, id)
+
// Stop the Go routine monitoring the device
close(o.chnl)
delete(da.deviceMap, id)
log.Debugw("abandoning-device", log.Fields{"Id": id})
return nil
+ } else { // id is not ownership key
+ if err := da.deleteDeviceKey(id); err != nil {
+ log.Errorw("failed-deleting-key", log.Fields{"id": id})
+ }
}
- return status.Error(codes.NotFound, fmt.Sprintf("id-inexistent-%s", id))
+ return nil
}
//abandonAllDevices must be invoked whenever a device is deleted from the Core
func (da *DeviceOwnership) abandonAllDevices() {
da.deviceMapLock.Lock()
defer da.deviceMapLock.Unlock()
+ da.deviceToKeyMapLock.Lock()
+ defer da.deviceToKeyMapLock.Unlock()
+ for k, _ := range da.deviceToKeyMap {
+ delete(da.deviceToKeyMap, k)
+ }
for _, val := range da.deviceMap {
close(val.chnl)
}
@@ -238,6 +258,17 @@
return nil
}
+func (da *DeviceOwnership) deleteDeviceKey(id string) error {
+ da.deviceToKeyMapLock.Lock()
+ defer da.deviceToKeyMapLock.Unlock()
+ if _, exist := da.deviceToKeyMap[id]; exist {
+ delete(da.deviceToKeyMap, id)
+ return nil
+ }
+ log.Warnw("device-not-owned", log.Fields{"deviceId": id})
+ return nil
+}
+
func (da *DeviceOwnership) getOwnershipKey(id interface{}) (string, error) {
if id == nil {
return "", status.Error(codes.InvalidArgument, "nil-id")