VOL-2199 Initialize logical_device_agent on all logical device ops
Change-Id: I7ef2cc1384e5afb017f7470521ee7a4ec80bce8b
diff --git a/ro_core/core/logical_device_manager.go b/ro_core/core/logical_device_manager.go
index 57eb7bc..d12c2a5 100644
--- a/ro_core/core/logical_device_manager.go
+++ b/ro_core/core/logical_device_manager.go
@@ -67,10 +67,18 @@
}
}
+// getLogicalDeviceAgent returns the logical device agent. If the device is not in memory then the device will
+// be loaded from dB and a logical device agent created to managed it.
func (ldMgr *LogicalDeviceManager) getLogicalDeviceAgent(logicalDeviceId string) *LogicalDeviceAgent {
if agent, ok := ldMgr.logicalDeviceAgents.Load(logicalDeviceId); ok {
- //return agent
return agent.(*LogicalDeviceAgent)
+ } else {
+ // Try to load into memory - loading will also create the logical device agent
+ if err := ldMgr.load(logicalDeviceId); err == nil {
+ if agent, ok = ldMgr.logicalDeviceAgents.Load(logicalDeviceId); ok {
+ return agent.(*LogicalDeviceAgent)
+ }
+ }
}
return nil
}
@@ -88,35 +96,12 @@
return nil, status.Errorf(codes.NotFound, "%s", id)
}
-func (ldMgr *LogicalDeviceManager) IsLogicalDeviceInCache(id string) bool {
- ldMgr.lockLogicalDeviceAgentsMap.Lock()
- defer ldMgr.lockLogicalDeviceAgentsMap.Unlock()
- _, exist := ldMgr.logicalDeviceAgents.Load(id)
- return exist
-}
-
+//listLogicalDevices returns the list of all logical devices
func (ldMgr *LogicalDeviceManager) listLogicalDevices() (*voltha.LogicalDevices, error) {
log.Debug("ListAllLogicalDevices")
result := &voltha.LogicalDevices{}
- if logicalDevices := ldMgr.clusterDataProxy.List(context.Background(), "/logical_devices", 0, false,
- ""); logicalDevices != nil {
+ if logicalDevices := ldMgr.clusterDataProxy.List(context.Background(), "/logical_devices", 0, false, ""); logicalDevices != nil {
for _, logicalDevice := range logicalDevices.([]interface{}) {
- // If device is not in memory then set it up
- if !ldMgr.IsLogicalDeviceInCache(logicalDevice.(*voltha.LogicalDevice).Id) {
- agent := newLogicalDeviceAgent(
- logicalDevice.(*voltha.LogicalDevice).Id,
- logicalDevice.(*voltha.LogicalDevice).RootDeviceId,
- ldMgr,
- ldMgr.deviceMgr,
- ldMgr.clusterDataProxy,
- )
- if err := agent.start(nil, true); err != nil {
- log.Warnw("failure-starting-agent", log.Fields{"logicalDeviceId": logicalDevice.(*voltha.LogicalDevice).Id})
- agent.stop(nil)
- } else {
- ldMgr.addLogicalDeviceAgentToMap(agent)
- }
- }
result.Items = append(result.Items, logicalDevice.(*voltha.LogicalDevice))
}
}