From a4d38b7bba375c1c4595c5f3ab7484a31e3d04bf Mon Sep 17 00:00:00 2001 From: Robert Varga Date: Mon, 2 Dec 2019 14:26:52 +0100 Subject: [PATCH] Fix NPEs in HwvtepOperGlobalListener A connection may not be available form HwvtepConnectionManager, in which case we would end up throwing a NPE, which floods logs in HwvtepDataChangeListenerTest. Fix that by checking for missing connection. Change-Id: I61a729266948fa1f7cf5eea1ff9c9a674cbad51e Signed-off-by: Robert Varga --- .../HwvtepOperGlobalListener.java | 26 ++++++++----------- 1 file changed, 11 insertions(+), 15 deletions(-) diff --git a/hwvtepsouthbound/hwvtepsouthbound-impl/src/main/java/org/opendaylight/ovsdb/hwvtepsouthbound/HwvtepOperGlobalListener.java b/hwvtepsouthbound/hwvtepsouthbound-impl/src/main/java/org/opendaylight/ovsdb/hwvtepsouthbound/HwvtepOperGlobalListener.java index 593ceb481..27f236570 100644 --- a/hwvtepsouthbound/hwvtepsouthbound-impl/src/main/java/org/opendaylight/ovsdb/hwvtepsouthbound/HwvtepOperGlobalListener.java +++ b/hwvtepsouthbound/hwvtepsouthbound-impl/src/main/java/org/opendaylight/ovsdb/hwvtepsouthbound/HwvtepOperGlobalListener.java @@ -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, 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, 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> changes) { + public void onDataTreeChanged(final Collection> changes) { changes.forEach(change -> { InstanceIdentifier key = change.getRootPath().getRootIdentifier(); DataObjectModification 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 mod) { + private static Node getCreated(final DataObjectModification mod) { if (mod.getModificationType() == ModificationType.WRITE && mod.getDataBefore() == null) { return mod.getDataAfter(); } return null; } - private Node getRemoved(DataObjectModification mod) { + private static Node getRemoved(final DataObjectModification 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 getWildcardPath() { - InstanceIdentifier path = InstanceIdentifier - .create(NetworkTopology.class) - .child(Topology.class, new TopologyKey(HwvtepSouthboundConstants.HWVTEP_TOPOLOGY_ID)) - .child(Node.class); - return path; + private static InstanceIdentifier getWildcardPath() { + return InstanceIdentifier.create(NetworkTopology.class) + .child(Topology.class, new TopologyKey(HwvtepSouthboundConstants.HWVTEP_TOPOLOGY_ID)) + .child(Node.class); } - public static Node getNode(InstanceIdentifier key) { + public static Node getNode(final InstanceIdentifier key) { return CONNECTED_NODES.get(key); } } -- 2.36.6