Handle nullable lists in elanmanager
[netvirt.git] / elanmanager / impl / src / main / java / org / opendaylight / netvirt / elan / l2gw / listeners / HwvtepTerminationPointListener.java
index 44a56c6ad4b4f21e792d134124e0d4ec0ca9a8e3..452f2d581becfa7297a17cbc00f7b944d1fae80c 100644 (file)
@@ -7,10 +7,15 @@
  */
 package org.opendaylight.netvirt.elan.l2gw.listeners;
 
+import static java.util.Collections.emptyList;
+import static org.opendaylight.genius.infra.Datastore.CONFIGURATION;
+import static org.opendaylight.netvirt.elan.utils.ElanUtils.requireNonNullElse;
+
 import com.google.common.util.concurrent.ListenableFuture;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.List;
+import java.util.Objects;
 import javax.inject.Inject;
 import javax.inject.Singleton;
 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
@@ -18,6 +23,7 @@ import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
 import org.opendaylight.genius.datastoreutils.hwvtep.HwvtepClusteredDataTreeChangeListener;
 import org.opendaylight.genius.infra.ManagedNewTransactionRunner;
 import org.opendaylight.genius.infra.ManagedNewTransactionRunnerImpl;
+import org.opendaylight.genius.utils.hwvtep.HwvtepNodeHACache;
 import org.opendaylight.genius.utils.hwvtep.HwvtepSouthboundConstants;
 import org.opendaylight.genius.utils.hwvtep.HwvtepSouthboundUtils;
 import org.opendaylight.genius.utils.hwvtep.HwvtepUtils;
@@ -61,8 +67,9 @@ public class HwvtepTerminationPointListener
 
     @Inject
     public HwvtepTerminationPointListener(DataBroker broker, ElanL2GatewayUtils elanL2GatewayUtils,
-            ElanClusterUtils elanClusterUtils, L2GatewayCache l2GatewayCache) {
-        super(TerminationPoint.class, HwvtepTerminationPointListener.class);
+            ElanClusterUtils elanClusterUtils, L2GatewayCache l2GatewayCache,
+            HwvtepNodeHACache hwvtepNodeHACache) {
+        super(TerminationPoint.class, HwvtepTerminationPointListener.class, hwvtepNodeHACache);
 
         this.broker = broker;
         this.txRunner = new ManagedNewTransactionRunnerImpl(broker);
@@ -78,7 +85,7 @@ public class HwvtepTerminationPointListener
     protected void removed(InstanceIdentifier<TerminationPoint> identifier, TerminationPoint del) {
         LOG.trace("physical locator removed {}", identifier);
         final HwvtepPhysicalPortAugmentation portAugmentation =
-                del.getAugmentation(HwvtepPhysicalPortAugmentation.class);
+                del.augmentation(HwvtepPhysicalPortAugmentation.class);
         if (portAugmentation != null) {
             elanClusterUtils.runOnlyInOwnerNode(HwvtepSouthboundConstants.ELAN_ENTITY_NAME,
                 "Handling Physical port delete",
@@ -95,7 +102,7 @@ public class HwvtepTerminationPointListener
     @Override
     protected void added(InstanceIdentifier<TerminationPoint> identifier, final TerminationPoint add) {
         final HwvtepPhysicalPortAugmentation portAugmentation =
-                add.getAugmentation(HwvtepPhysicalPortAugmentation.class);
+                add.augmentation(HwvtepPhysicalPortAugmentation.class);
         if (portAugmentation != null) {
             final NodeId nodeId = identifier.firstIdentifierOf(Node.class).firstKeyOf(Node.class).getNodeId();
             elanClusterUtils.runOnlyInOwnerNode(HwvtepSouthboundConstants.ELAN_ENTITY_NAME,
@@ -121,7 +128,7 @@ public class HwvtepTerminationPointListener
     private List<ListenableFuture<Void>> handlePortAdded(TerminationPoint portAdded, NodeId psNodeId) {
         Node psNode = HwvtepUtils.getHwVtepNode(broker, LogicalDatastoreType.OPERATIONAL, psNodeId);
         if (psNode != null) {
-            String psName = psNode.getAugmentation(PhysicalSwitchAugmentation.class).getHwvtepNodeName().getValue();
+            String psName = psNode.augmentation(PhysicalSwitchAugmentation.class).getHwvtepNodeName().getValue();
             L2GatewayDevice l2GwDevice = l2GatewayCache.get(psName);
             if (l2GwDevice != null) {
                 if (isL2GatewayConfigured(l2GwDevice)) {
@@ -140,14 +147,14 @@ public class HwvtepTerminationPointListener
         } else {
             LOG.error("{} entry not in config datastore", psNodeId);
         }
-        return Collections.emptyList();
+        return emptyList();
     }
 
     private List<ListenableFuture<Void>> handlePortDeleted(InstanceIdentifier<TerminationPoint> identifier) {
         InstanceIdentifier<Node> psNodeIid = identifier.firstIdentifierOf(Node.class);
-        return Collections.singletonList(txRunner.callWithNewReadWriteTransactionAndSubmit(
-            tx -> tx.read(LogicalDatastoreType.CONFIGURATION, psNodeIid).checkedGet().toJavaUtil().ifPresent(
-                node -> tx.delete(LogicalDatastoreType.CONFIGURATION, identifier))));
+        return Collections.singletonList(txRunner.callWithNewReadWriteTransactionAndSubmit(CONFIGURATION,
+            tx -> tx.read(psNodeIid).get().toJavaUtil().ifPresent(
+                node -> tx.delete(identifier))));
     }
 
     private List<VlanBindings> getVlanBindings(List<L2gatewayConnection> l2GwConns, NodeId hwvtepNodeId, String psName,
@@ -160,12 +167,13 @@ public class HwvtepTerminationPointListener
             } else {
                 String logicalSwitchName = ElanL2GatewayUtils.getLogicalSwitchFromElan(
                         l2GwConn.getNetworkId().getValue());
-                List<Devices> l2Devices = l2Gateway.getDevices();
+                List<Devices> l2Devices = requireNonNullElse(l2Gateway.getDevices(), emptyList());
                 for (Devices l2Device : l2Devices) {
                     String l2DeviceName = l2Device.getDeviceName();
                     if (l2DeviceName != null && l2DeviceName.equals(psName)) {
-                        for (Interfaces deviceInterface : l2Device.getInterfaces()) {
-                            if (deviceInterface.getInterfaceName().equals(newPortId)) {
+                        for (Interfaces deviceInterface : requireNonNullElse(l2Device.getInterfaces(),
+                                Collections.<Interfaces>emptyList())) {
+                            if (Objects.equals(deviceInterface.getInterfaceName(), newPortId)) {
                                 if (deviceInterface.getSegmentationIds() != null
                                         && !deviceInterface.getSegmentationIds().isEmpty()) {
                                     for (Integer vlanId : deviceInterface.getSegmentationIds()) {