Fix NPEs in HwvtepOperGlobalListener
[ovsdb.git] / hwvtepsouthbound / hwvtepsouthbound-impl / src / main / java / org / opendaylight / ovsdb / hwvtepsouthbound / HwvtepOperGlobalListener.java
index 593ceb4813b406a299a8aaafc2e6f63ed9a6de2e..27f236570cf8d9dd5f269a5c582fe3038cc867e0 100644 (file)
@@ -5,7 +5,6 @@
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
  * and is available at http://www.eclipse.org/legal/epl-v10.html
  */
-
 package org.opendaylight.ovsdb.hwvtepsouthbound;
 
 import java.util.Collection;
@@ -32,7 +31,6 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 public class HwvtepOperGlobalListener implements ClusteredDataTreeChangeListener<Node>, AutoCloseable {
-
     private static final Logger LOG = LoggerFactory.getLogger(HwvtepOperGlobalListener.class);
 
     private final Timer timer = new Timer();
@@ -41,7 +39,7 @@ public class HwvtepOperGlobalListener implements ClusteredDataTreeChangeListener
     private final DataBroker db;
     private static final Map<InstanceIdentifier<Node>, Node> CONNECTED_NODES = new ConcurrentHashMap<>();
 
-    HwvtepOperGlobalListener(DataBroker db, HwvtepConnectionManager hcm) {
+    HwvtepOperGlobalListener(final DataBroker db, final HwvtepConnectionManager hcm) {
         LOG.info("Registering HwvtepOperGlobalListener");
         this.db = db;
         this.hcm = hcm;
@@ -63,7 +61,7 @@ public class HwvtepOperGlobalListener implements ClusteredDataTreeChangeListener
     }
 
     @Override
-    public void onDataTreeChanged(Collection<DataTreeModification<Node>> changes) {
+    public void onDataTreeChanged(final Collection<DataTreeModification<Node>> changes) {
         changes.forEach(change -> {
             InstanceIdentifier<Node> key = change.getRootPath().getRootIdentifier();
             DataObjectModification<Node> mod = change.getRootNode();
@@ -76,8 +74,8 @@ public class HwvtepOperGlobalListener implements ClusteredDataTreeChangeListener
             if (node != null) {
                 CONNECTED_NODES.remove(key);
                 HwvtepConnectionInstance connectionInstance = hcm.getConnectionInstanceFromNodeIid(nodeIid);
-                if (Objects.equals(connectionInstance.getConnectionInfo().getRemotePort(),
-                        HwvtepSouthboundUtil.getRemotePort(node))) {
+                if (connectionInstance != null && Objects.equals(connectionInstance.getConnectionInfo().getRemotePort(),
+                            HwvtepSouthboundUtil.getRemotePort(node))) {
                     //Oops some one deleted the node held by me This should never happen
                     try {
                         connectionInstance.refreshOperNode();
@@ -90,14 +88,14 @@ public class HwvtepOperGlobalListener implements ClusteredDataTreeChangeListener
         });
     }
 
-    private Node getCreated(DataObjectModification<Node> mod) {
+    private static Node getCreated(final DataObjectModification<Node> mod) {
         if (mod.getModificationType() == ModificationType.WRITE && mod.getDataBefore() == null) {
             return mod.getDataAfter();
         }
         return null;
     }
 
-    private Node getRemoved(DataObjectModification<Node> mod) {
+    private static Node getRemoved(final DataObjectModification<Node> mod) {
         if (mod.getModificationType() == ModificationType.DELETE) {
             return mod.getDataBefore();
         }
@@ -108,15 +106,13 @@ public class HwvtepOperGlobalListener implements ClusteredDataTreeChangeListener
         return Collections.unmodifiableMap(CONNECTED_NODES);
     }
 
-    private InstanceIdentifier<Node> getWildcardPath() {
-        InstanceIdentifier<Node> path = InstanceIdentifier
-                        .create(NetworkTopology.class)
-                        .child(Topology.class, new TopologyKey(HwvtepSouthboundConstants.HWVTEP_TOPOLOGY_ID))
-                        .child(Node.class);
-        return path;
+    private static InstanceIdentifier<Node> getWildcardPath() {
+        return InstanceIdentifier.create(NetworkTopology.class)
+                .child(Topology.class, new TopologyKey(HwvtepSouthboundConstants.HWVTEP_TOPOLOGY_ID))
+                .child(Node.class);
     }
 
-    public static Node getNode(InstanceIdentifier<Node> key) {
+    public static Node getNode(final InstanceIdentifier<Node> key) {
         return CONNECTED_NODES.get(key);
     }
 }