From: Tomas Cere Date: Fri, 11 Dec 2015 12:23:25 +0000 (+0100) Subject: Prevent NPE's on failures X-Git-Tag: release/beryllium~45^2~6 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=commitdiff_plain;h=97cd42d749399c4ba7940026bfa2ff4fdd4cca9e;p=netconf.git Prevent NPE's on failures Change-Id: I7f69aa4f9a695c7d03cd7db6cd5c539bfd255cdd Signed-off-by: Tomas Cere --- diff --git a/opendaylight/netconf/abstract-topology/src/main/java/org/opendaylight/netconf/topology/InitialStateProvider.java b/opendaylight/netconf/abstract-topology/src/main/java/org/opendaylight/netconf/topology/InitialStateProvider.java index ed3623da49..ae53a9dc79 100644 --- a/opendaylight/netconf/abstract-topology/src/main/java/org/opendaylight/netconf/topology/InitialStateProvider.java +++ b/opendaylight/netconf/abstract-topology/src/main/java/org/opendaylight/netconf/topology/InitialStateProvider.java @@ -10,6 +10,7 @@ package org.opendaylight.netconf.topology; import com.google.common.annotations.Beta; import javax.annotation.Nonnull; +import javax.annotation.Nullable; import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId; import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node; @@ -22,5 +23,5 @@ public interface InitialStateProvider { Node getInitialState(@Nonnull final NodeId nodeId, @Nonnull final Node configNode); @Nonnull - Node getFailedState(@Nonnull final NodeId nodeId, @Nonnull final Node configNode); + Node getFailedState(@Nonnull final NodeId nodeId, @Nullable final Node configNode); } diff --git a/opendaylight/netconf/abstract-topology/src/main/java/org/opendaylight/netconf/topology/TopologyManagerCallback.java b/opendaylight/netconf/abstract-topology/src/main/java/org/opendaylight/netconf/topology/TopologyManagerCallback.java index ea7d05d98f..8eee06a54e 100644 --- a/opendaylight/netconf/abstract-topology/src/main/java/org/opendaylight/netconf/topology/TopologyManagerCallback.java +++ b/opendaylight/netconf/abstract-topology/src/main/java/org/opendaylight/netconf/topology/TopologyManagerCallback.java @@ -16,7 +16,7 @@ import com.google.common.annotations.Beta; * Customizable extension layer between the top level TopologyManager and NodeManager */ @Beta -public interface TopologyManagerCallback extends NodeListener, Receiver, RoleChangeListener { +public interface TopologyManagerCallback extends InitialStateProvider, NodeListener, Receiver, RoleChangeListener { interface TopologyManagerCallbackFactory { TopologyManagerCallback create(ActorSystem actorSystem, String topologyId); diff --git a/opendaylight/netconf/abstract-topology/src/main/java/org/opendaylight/netconf/topology/example/ExampleTopologyManagerCallback.java b/opendaylight/netconf/abstract-topology/src/main/java/org/opendaylight/netconf/topology/example/ExampleTopologyManagerCallback.java index 76e55acf4a..a2eed6a019 100644 --- a/opendaylight/netconf/abstract-topology/src/main/java/org/opendaylight/netconf/topology/example/ExampleTopologyManagerCallback.java +++ b/opendaylight/netconf/abstract-topology/src/main/java/org/opendaylight/netconf/topology/example/ExampleTopologyManagerCallback.java @@ -17,6 +17,7 @@ import com.google.common.util.concurrent.ListenableFuture; import java.util.HashMap; import java.util.Map; import javax.annotation.Nonnull; +import javax.annotation.Nullable; import org.opendaylight.controller.md.sal.binding.api.DataBroker; import org.opendaylight.netconf.topology.NodeManager; import org.opendaylight.netconf.topology.NodeManagerCallback.NodeManagerCallbackFactory; @@ -147,4 +148,16 @@ public class ExampleTopologyManagerCallback implements TopologyManagerCallback { public void onReceive(Object o, ActorRef actorRef) { } + + @Nonnull + @Override + public Node getInitialState(@Nonnull NodeId nodeId, @Nonnull Node configNode) { + return nodes.get(nodeId).getInitialState(nodeId, configNode); + } + + @Nonnull + @Override + public Node getFailedState(@Nonnull NodeId nodeId, @Nullable Node configNode) { + return nodes.get(nodeId).getFailedState(nodeId, configNode); + } } diff --git a/opendaylight/netconf/abstract-topology/src/main/java/org/opendaylight/netconf/topology/util/BaseTopologyManager.java b/opendaylight/netconf/abstract-topology/src/main/java/org/opendaylight/netconf/topology/util/BaseTopologyManager.java index ca55bb0552..f3bceab49a 100644 --- a/opendaylight/netconf/abstract-topology/src/main/java/org/opendaylight/netconf/topology/util/BaseTopologyManager.java +++ b/opendaylight/netconf/abstract-topology/src/main/java/org/opendaylight/netconf/topology/util/BaseTopologyManager.java @@ -35,7 +35,6 @@ import java.util.Map.Entry; import java.util.Random; import javax.annotation.Nonnull; import org.opendaylight.controller.md.sal.binding.api.DataBroker; -import org.opendaylight.netconf.topology.NodeManager; import org.opendaylight.netconf.topology.RoleChangeStrategy; import org.opendaylight.netconf.topology.StateAggregator; import org.opendaylight.netconf.topology.TopologyManager; @@ -81,7 +80,6 @@ public final class BaseTopologyManager private final String topologyId; private final TopologyManagerCallback delegateTopologyHandler; - private final Map nodes = new HashMap<>(); private final Map peers = new HashMap<>(); private TopologyManager masterPeer = null; private final int id = new Random().nextInt(); @@ -191,7 +189,7 @@ public final class BaseTopologyManager public void onFailure(final Throwable t) { // If the combined connection attempt failed, set the node to connection failed LOG.debug("Futures aggregation failed"); - naSalNodeWriter.update(nodeId, nodes.get(nodeId).getFailedState(nodeId, node)); + naSalNodeWriter.update(nodeId, delegateTopologyHandler.getFailedState(nodeId, node)); // FIXME disconnect those which succeeded // just issue a delete on delegateTopologyHandler that gets handled on lower level } @@ -250,7 +248,7 @@ public final class BaseTopologyManager @Override public void onFailure(final Throwable t) { // If the combined connection attempt failed, set the node to connection failed - naSalNodeWriter.update(nodeId, nodes.get(nodeId).getFailedState(nodeId, node)); + naSalNodeWriter.update(nodeId, delegateTopologyHandler.getFailedState(nodeId, node)); // FIXME disconnect those which succeeded // just issue a delete on delegateTopologyHandler that gets handled on lower level } @@ -376,7 +374,7 @@ public final class BaseTopologyManager public void onFailure(final Throwable t) { // If the combined connection attempt failed, set the node to connection failed LOG.debug("Futures aggregation failed"); - naSalNodeWriter.update(nodeId, nodes.get(nodeId).getFailedState(nodeId, null)); + naSalNodeWriter.update(nodeId, delegateTopologyHandler.getFailedState(nodeId, null)); // FIXME disconnect those which succeeded // just issue a delete on delegateTopologyHandler that gets handled on lower level } diff --git a/opendaylight/netconf/netconf-topology/src/main/java/org/opendaylight/netconf/topology/impl/ClusteredNetconfTopology.java b/opendaylight/netconf/netconf-topology/src/main/java/org/opendaylight/netconf/topology/impl/ClusteredNetconfTopology.java index bf03fe1eaa..bc472e98d8 100644 --- a/opendaylight/netconf/netconf-topology/src/main/java/org/opendaylight/netconf/topology/impl/ClusteredNetconfTopology.java +++ b/opendaylight/netconf/netconf-topology/src/main/java/org/opendaylight/netconf/topology/impl/ClusteredNetconfTopology.java @@ -118,7 +118,7 @@ public class ClusteredNetconfTopology extends AbstractNetconfTopology implements public void close() throws Exception { // close all existing connectors, delete whole topology in datastore? for (NetconfConnectorDTO connectorDTO : activeConnectors.values()) { - connectorDTO.getCommunicator().disconnect(); + connectorDTO.getCommunicator().close(); } activeConnectors.clear(); } diff --git a/opendaylight/netconf/netconf-topology/src/main/java/org/opendaylight/netconf/topology/impl/NetconfNodeManagerCallback.java b/opendaylight/netconf/netconf-topology/src/main/java/org/opendaylight/netconf/topology/impl/NetconfNodeManagerCallback.java index 5f0eb2e179..cb74134708 100644 --- a/opendaylight/netconf/netconf-topology/src/main/java/org/opendaylight/netconf/topology/impl/NetconfNodeManagerCallback.java +++ b/opendaylight/netconf/netconf-topology/src/main/java/org/opendaylight/netconf/topology/impl/NetconfNodeManagerCallback.java @@ -181,8 +181,8 @@ public class NetconfNodeManagerCallback implements NodeManagerCallback{ } @Nonnull @Override public Node getFailedState(@Nonnull final NodeId nodeId, - @Nonnull final Node configNode) { - final NetconfNode netconfNode = configNode.getAugmentation(NetconfNode.class); + @Nullable final Node configNode) { + final NetconfNode netconfNode = configNode == null ? currentOperationalNode.getAugmentation(NetconfNode.class) : configNode.getAugmentation(NetconfNode.class); return new NodeBuilder() .setNodeId(nodeId) diff --git a/opendaylight/netconf/netconf-topology/src/main/java/org/opendaylight/netconf/topology/impl/NetconfTopologyManagerCallback.java b/opendaylight/netconf/netconf-topology/src/main/java/org/opendaylight/netconf/topology/impl/NetconfTopologyManagerCallback.java index eaa5d6cab7..739aa66eb2 100644 --- a/opendaylight/netconf/netconf-topology/src/main/java/org/opendaylight/netconf/topology/impl/NetconfTopologyManagerCallback.java +++ b/opendaylight/netconf/netconf-topology/src/main/java/org/opendaylight/netconf/topology/impl/NetconfTopologyManagerCallback.java @@ -149,4 +149,16 @@ public class NetconfTopologyManagerCallback implements TopologyManagerCallback { public void onReceive(Object o, ActorRef actorRef) { } + + @Nonnull + @Override + public Node getInitialState(@Nonnull NodeId nodeId, @Nonnull Node configNode) { + return nodes.get(nodeId).getInitialState(nodeId, configNode); + } + + @Nonnull + @Override + public Node getFailedState(@Nonnull NodeId nodeId, @Nonnull Node configNode) { + return nodes.get(nodeId).getFailedState(nodeId, configNode); + } } diff --git a/opendaylight/netconf/netconf-topology/src/test/java/org/opendaylight/netconf/topology/ActorTest.java b/opendaylight/netconf/netconf-topology/src/test/java/org/opendaylight/netconf/topology/ActorTest.java index 544cb87d21..599acc9247 100644 --- a/opendaylight/netconf/netconf-topology/src/test/java/org/opendaylight/netconf/topology/ActorTest.java +++ b/opendaylight/netconf/netconf-topology/src/test/java/org/opendaylight/netconf/topology/ActorTest.java @@ -572,6 +572,34 @@ public class ActorTest { public void onReceive(Object o, ActorRef actorRef) { } + + @Nonnull + @Override + public Node getInitialState(@Nonnull NodeId nodeId, @Nonnull Node configNode) { + return new NodeBuilder() + .setNodeId(nodeId) + .addAugmentation(NetconfNode.class, + new NetconfNodeBuilder() + .setConnectionStatus(ConnectionStatus.Connecting) + .setHost(new Host(new IpAddress(new Ipv4Address("127.0.0.1")))) + .setPort(new PortNumber(65535)) + .build()) + .build(); + } + + @Nonnull + @Override + public Node getFailedState(@Nonnull NodeId nodeId, @Nonnull Node configNode) { + return new NodeBuilder() + .setNodeId(nodeId) + .addAugmentation(NetconfNode.class, + new NetconfNodeBuilder() + .setConnectionStatus(ConnectionStatus.UnableToConnect) + .setHost(new Host(new IpAddress(new Ipv4Address("127.0.0.1")))) + .setPort(new PortNumber(65535)) + .build()) + .build(); + } } public class TestingSuccesfulStateAggregator implements StateAggregator {