Optimizatoin to get the nodeIid from the DeviceOperCache in HwvtepPhysicalPortUpdateC... 61/82761/4
authorChandra Shekar S <chandra.shekar.s@ericsson.com>
Thu, 27 Jun 2019 13:04:35 +0000 (18:34 +0530)
committerChandra Shekar S <chandra.shekar.s@ericsson.com>
Tue, 2 Jul 2019 05:52:18 +0000 (11:22 +0530)
JIRA: OVSDB-482

Description :
Currently in HwvtepPhysicalPortUpdateCommand while updating the terminationpoint the
nodeIid/switchIid is retrieved from the incoming event data, if not present in the
incoming event data, it is retrieved from the DataStore by looking/looping at all
switches for that node which is ineffecient. The logic has chances of throwing NPE when
there are no PhisicalSwitches present for that node in the DataStore.

The change is to retrieve the nodeIid from the deviceOperCache , if not present get it from the
DataStore. The change also contains the handling of the NPE while retrieving the data from DataStore

Change-Id: Ifbfad7eb9cdac1c2eedb29ea9eb2a303ac8a9a3c
Signed-off-by: Chandra Shekar S <chandra.shekar.s@ericsson.com>
hwvtepsouthbound/hwvtepsouthbound-impl/src/main/java/org/opendaylight/ovsdb/hwvtepsouthbound/HwvtepDeviceInfo.java
hwvtepsouthbound/hwvtepsouthbound-impl/src/main/java/org/opendaylight/ovsdb/hwvtepsouthbound/transactions/md/HwvtepPhysicalPortUpdateCommand.java

index 88fa773da0555d26b982fefbd6dcc0fb9c2058db..6229bd3542893ba1eb7e731087d71d0f2e240c56 100644 (file)
@@ -117,7 +117,7 @@ public class HwvtepDeviceInfo {
             new ConcurrentHashMap<>();
     private final Map<Class<? extends Identifiable>, Map<InstanceIdentifier, DeviceData>> opKeyVsData =
             new ConcurrentHashMap<>();
-    private final Map<Class<? extends Identifiable>, Map<UUID, Object>> uuidVsData = new ConcurrentHashMap<>();
+    private final Map<Class<? extends Identifiable>, Map<UUID, DeviceData>> uuidVsData = new ConcurrentHashMap<>();
     private final DependencyQueue dependencyQueue;
     private TransactionHistory controllerTxHistory;
     private TransactionHistory deviceUpdateHistory;
@@ -133,11 +133,11 @@ public class HwvtepDeviceInfo {
     }
 
     public Map<UUID, LogicalSwitch> getLogicalSwitches() {
-        Map<UUID, Object> switches = uuidVsData.get(LogicalSwitches.class);
+        Map<UUID, DeviceData> switches = uuidVsData.get(LogicalSwitches.class);
         Map<UUID, LogicalSwitch> result = new HashMap<>();
         if (switches != null) {
-            for (Map.Entry<UUID, Object> entry : switches.entrySet()) {
-                result.put(entry.getKey(), (LogicalSwitch) entry.getValue());
+            for (Map.Entry<UUID, DeviceData> entry : switches.entrySet()) {
+                result.put(entry.getKey(), (LogicalSwitch) entry.getValue().getData());
             }
         }
         return result;
@@ -164,11 +164,11 @@ public class HwvtepDeviceInfo {
     }
 
     public Map<UUID, PhysicalLocator> getPhysicalLocators() {
-        Map<UUID, Object> locators = uuidVsData.get(TerminationPoint.class);
+        Map<UUID, DeviceData> locators = uuidVsData.get(TerminationPoint.class);
         Map<UUID, PhysicalLocator> result = new HashMap<>();
         if (locators != null) {
-            for (Map.Entry<UUID, Object> entry : locators.entrySet()) {
-                result.put(entry.getKey(), (PhysicalLocator) entry.getValue());
+            for (Map.Entry<UUID, DeviceData> entry : locators.entrySet()) {
+                result.put(entry.getKey(), (PhysicalLocator) entry.getValue().getData());
             }
         }
         return result;
@@ -232,9 +232,9 @@ public class HwvtepDeviceInfo {
     public void updateDeviceOperData(Class<? extends Identifiable> cls, InstanceIdentifier key,
             UUID uuid, Object data) {
         LOG.debug("Updating device data {}", key);
-        HwvtepSouthboundUtil.updateData(opKeyVsData, cls, key,
-                new DeviceData(key, uuid, data, DeviceDataStatus.AVAILABLE));
-        HwvtepSouthboundUtil.updateData(uuidVsData, cls, uuid, data);
+        DeviceData deviceData = new DeviceData(key, uuid, data, DeviceDataStatus.AVAILABLE);
+        HwvtepSouthboundUtil.updateData(opKeyVsData, cls, key, deviceData);
+        HwvtepSouthboundUtil.updateData(uuidVsData, cls, uuid, deviceData);
     }
 
     public void clearDeviceOperData(Class<? extends Identifiable> cls, InstanceIdentifier key) {
@@ -260,7 +260,7 @@ public class HwvtepDeviceInfo {
     }
 
     public Object getDeviceOperData(Class<? extends Identifiable> cls, UUID uuid) {
-        return HwvtepSouthboundUtil.getData(uuidVsData, cls, uuid);
+        return HwvtepSouthboundUtil.getData(uuidVsData, cls, uuid).getData();
     }
 
     public DeviceData getDeviceOperData(Class<? extends Identifiable> cls, InstanceIdentifier key) {
@@ -271,6 +271,14 @@ public class HwvtepDeviceInfo {
         return opKeyVsData.get(cls);
     }
 
+    public InstanceIdentifier getDeviceOperKey(final Class<? extends Identifiable> cls, final UUID uuid) {
+        DeviceData deviceData = HwvtepSouthboundUtil.getData(uuidVsData, cls, uuid);
+        if (deviceData != null) {
+            return deviceData.getKey();
+        }
+        return null;
+    }
+
     public UUID getUUID(Class<? extends Identifiable> cls, InstanceIdentifier key) {
         DeviceData data = HwvtepSouthboundUtil.getData(opKeyVsData, cls, key);
         if (data != null) {
index 11a062b8736ed0f64c877948e05c4343e7073eed..3919932e5910cddb012530075725b3912bf613cb 100644 (file)
@@ -113,7 +113,12 @@ public class HwvtepPhysicalPortUpdateCommand extends AbstractTransactionCommand
             String portName = portUpdate.getNameColumn().getData();
             Optional<InstanceIdentifier<Node>> switchIid = getTerminationPointSwitch(portUpdateEntry.getKey());
             if (!switchIid.isPresent()) {
-                switchIid = getTerminationPointSwitch(transaction, node, portName);
+                switchIid = getFromDeviceOperCache(portUpdate.getUuid());
+                if (!switchIid.isPresent()) {
+                    LOG.debug("Failed to find node from the DeviceOperCache for port {}. Get it from the DS.",
+                            portUpdate);
+                    switchIid = getTerminationPointSwitch(transaction, node, portName);
+                }
             }
             if (switchIid.isPresent()) {
                 TerminationPointKey tpKey = new TerminationPointKey(new TpId(portName));
@@ -158,6 +163,8 @@ public class HwvtepPhysicalPortUpdateCommand extends AbstractTransactionCommand
                 }
                 // Update with Deleted portfaultstatus
                 deleteEntries(transaction,getPortFaultStatusToRemove(tpPath, portUpdate));
+            } else {
+                LOG.warn("switchIid was not found for port {}", portUpdate);
             }
         }
     }
@@ -295,18 +302,23 @@ public class HwvtepPhysicalPortUpdateCommand extends AbstractTransactionCommand
             Node node, String tpName) {
         HwvtepGlobalAugmentation hwvtepNode = node.augmentation(HwvtepGlobalAugmentation.class);
         List<Switches> switchNodes = hwvtepNode.getSwitches();
-        for (Switches managedNodeEntry : switchNodes) {
-            @SuppressWarnings("unchecked")
-            Node switchNode = HwvtepSouthboundUtil
-                    .readNode(transaction, (InstanceIdentifier<Node>) managedNodeEntry.getSwitchRef().getValue()).get();
-            TerminationPointKey tpKey = new TerminationPointKey(new TpId(tpName));
-            if (switchNode.getTerminationPoint() != null) {
-                for (TerminationPoint terminationPoint : switchNode.getTerminationPoint()) {
-                    if (terminationPoint.key().equals(tpKey)) {
-                        return Optional.of((InstanceIdentifier<Node>) managedNodeEntry.getSwitchRef().getValue());
+        if (switchNodes != null && !switchNodes.isEmpty()) {
+            for (Switches managedNodeEntry : switchNodes) {
+                @SuppressWarnings("unchecked")
+                Node switchNode = HwvtepSouthboundUtil
+                        .readNode(transaction,
+                                (InstanceIdentifier<Node>) managedNodeEntry.getSwitchRef().getValue()).get();
+                TerminationPointKey tpKey = new TerminationPointKey(new TpId(tpName));
+                if (switchNode.getTerminationPoint() != null) {
+                    for (TerminationPoint terminationPoint : switchNode.getTerminationPoint()) {
+                        if (terminationPoint.key().equals(tpKey)) {
+                            return Optional.of((InstanceIdentifier<Node>) managedNodeEntry.getSwitchRef().getValue());
+                        }
                     }
                 }
             }
+        } else {
+            LOG.trace("PhyscialSwitch not present for the Port {}", tpName);
         }
         return Optional.absent();
     }
@@ -342,4 +354,15 @@ public class HwvtepPhysicalPortUpdateCommand extends AbstractTransactionCommand
         }
         return result;
     }
+
+    private  Optional<InstanceIdentifier<Node>> getFromDeviceOperCache(final UUID uuid) {
+
+        InstanceIdentifier<TerminationPoint> terminationPointIid =
+                (InstanceIdentifier<TerminationPoint>)getOvsdbConnectionInstance()
+                        .getDeviceInfo().getDeviceOperKey(TerminationPoint.class, uuid);
+        if (terminationPointIid != null) {
+            return Optional.of(terminationPointIid.firstIdentifierOf(Node.class));
+        }
+        return Optional.absent();
+    }
 }