Cluster schema resolution pipeline
[netconf.git] / opendaylight / netconf / netconf-topology / src / main / java / org / opendaylight / netconf / topology / impl / NetconfNodeManagerCallback.java
index 4c5e04b211a33aaeb37a33d2a31ed44db12bfc5d..9813bbc878be3227466e114ad959e2d0713ea39d 100644 (file)
@@ -30,6 +30,10 @@ import javax.annotation.Nonnull;
 import javax.annotation.Nullable;
 import org.opendaylight.controller.md.sal.dom.api.DOMNotification;
 import org.opendaylight.controller.md.sal.dom.api.DOMRpcService;
+import org.opendaylight.netconf.api.NetconfMessage;
+import org.opendaylight.netconf.api.NetconfTerminationReason;
+import org.opendaylight.netconf.client.NetconfClientSession;
+import org.opendaylight.netconf.client.NetconfClientSessionListener;
 import org.opendaylight.netconf.sal.connect.netconf.listener.NetconfDeviceCapabilities;
 import org.opendaylight.netconf.sal.connect.netconf.listener.NetconfSessionPreferences;
 import org.opendaylight.netconf.topology.NetconfTopology;
@@ -37,6 +41,7 @@ import org.opendaylight.netconf.topology.NodeManager;
 import org.opendaylight.netconf.topology.NodeManagerCallback;
 import org.opendaylight.netconf.topology.RoleChangeStrategy;
 import org.opendaylight.netconf.topology.TopologyManager;
+import org.opendaylight.netconf.topology.pipeline.ClusteredNetconfDeviceCommunicator.NetconfClientSessionListenerRegistration;
 import org.opendaylight.netconf.topology.pipeline.TopologyMountPointFacade.ConnectionStatusListenerRegistration;
 import org.opendaylight.netconf.topology.util.BaseNodeManager;
 import org.opendaylight.netconf.topology.util.BaseTopologyManager;
@@ -64,7 +69,7 @@ import org.slf4j.LoggerFactory;
 import scala.concurrent.Future;
 import scala.concurrent.duration.FiniteDuration;
 
-public class NetconfNodeManagerCallback implements NodeManagerCallback{
+public class NetconfNodeManagerCallback implements NodeManagerCallback, NetconfClientSessionListener{
 
     private static final Logger LOG = LoggerFactory.getLogger(NetconfNodeManagerCallback.class);
 
@@ -103,7 +108,8 @@ public class NetconfNodeManagerCallback implements NodeManagerCallback{
     private Node currentConfig;
     private Node currentOperationalNode;
 
-    private ConnectionStatusListenerRegistration registration = null;
+    private ConnectionStatusListenerRegistration connectionStatusregistration = null;
+    private NetconfClientSessionListenerRegistration sessionListener = null;
 
     private ActorRef masterDataBrokerRef = null;
     private boolean connected = false;
@@ -223,7 +229,8 @@ public class NetconfNodeManagerCallback implements NodeManagerCallback{
         Futures.addCallback(connectionFuture, new FutureCallback<NetconfDeviceCapabilities>() {
             @Override
             public void onSuccess(@Nullable NetconfDeviceCapabilities result) {
-                registration = topologyDispatcher.registerConnectionStatusListener(nodeId, nodeManager);
+                connectionStatusregistration = topologyDispatcher.registerConnectionStatusListener(nodeId, nodeManager);
+                sessionListener = topologyDispatcher.registerNetconfClientSessionListener(nodeId, NetconfNodeManagerCallback.this);
             }
 
             @Override
@@ -269,8 +276,9 @@ public class NetconfNodeManagerCallback implements NodeManagerCallback{
                                                 @Nonnull final Node configNode) {
         // first disconnect this node
         topologyDispatcher.unregisterMountPoint(nodeId);
-        if (registration != null) {
-            registration.close();
+
+        if (connectionStatusregistration != null) {
+            connectionStatusregistration.close();
         }
         topologyDispatcher.disconnectNode(nodeId);
 
@@ -280,7 +288,7 @@ public class NetconfNodeManagerCallback implements NodeManagerCallback{
         Futures.addCallback(connectionFuture, new FutureCallback<NetconfDeviceCapabilities>() {
             @Override
             public void onSuccess(@Nullable NetconfDeviceCapabilities result) {
-                registration = topologyDispatcher.registerConnectionStatusListener(nodeId, nodeManager);
+                connectionStatusregistration = topologyDispatcher.registerConnectionStatusListener(nodeId, NetconfNodeManagerCallback.this);
             }
 
             @Override
@@ -323,8 +331,9 @@ public class NetconfNodeManagerCallback implements NodeManagerCallback{
     @Nonnull @Override public ListenableFuture<Void> onNodeDeleted(@Nonnull final NodeId nodeId) {
         // cleanup and disconnect
         topologyDispatcher.unregisterMountPoint(nodeId);
-        if (registration != null) {
-            registration.close();
+
+        if(connectionStatusregistration != null) {
+            connectionStatusregistration.close();
         }
         roleChangeStrategy.unregisterRoleCandidate();
         return topologyDispatcher.disconnectNode(nodeId);
@@ -359,9 +368,7 @@ public class NetconfNodeManagerCallback implements NodeManagerCallback{
     @Override
     public void onDeviceConnected(final SchemaContext remoteSchemaContext, final NetconfSessionPreferences netconfSessionPreferences, final DOMRpcService deviceRpc) {
         // we need to notify the higher level that something happened, get a current status from all other nodes, and aggregate a new result
-        LOG.debug("onDeviceConnected received, registering role candidate");
         connected = true;
-        roleChangeStrategy.registerRoleCandidate(nodeManager);
         if (!isMaster && masterDataBrokerRef != null) {
             // if we're not master but one is present already, we need to register mountpoint
             LOG.warn("Device connected, master already present in topology, registering mount point");
@@ -403,7 +410,7 @@ public class NetconfNodeManagerCallback implements NodeManagerCallback{
     @Override
     public void onDeviceDisconnected() {
         // we need to notify the higher level that something happened, get a current status from all other nodes, and aggregate a new result
-        LOG.debug("onDeviceDisconnected received, unregistering role candidate");
+        LOG.debug("onDeviceDisconnected received, unregistered role candidate");
         connected = false;
         if (isMaster) {
             // announce that master mount point is going down
@@ -415,7 +422,6 @@ public class NetconfNodeManagerCallback implements NodeManagerCallback{
             // onRoleChanged() callback can sometimes lag behind, so unregister the mount right when it disconnects
             topologyDispatcher.unregisterMountPoint(new NodeId(nodeId));
         }
-        roleChangeStrategy.unregisterRoleCandidate();
 
         final NetconfNode netconfNode = currentConfig.getAugmentation(NetconfNode.class);
         currentOperationalNode = new NodeBuilder().setNodeId(new NodeId(nodeId))
@@ -445,7 +451,6 @@ public class NetconfNodeManagerCallback implements NodeManagerCallback{
         connected = false;
         String reason = (throwable != null && throwable.getMessage() != null) ? throwable.getMessage() : UNKNOWN_REASON;
 
-        roleChangeStrategy.unregisterRoleCandidate();
         currentOperationalNode = new NodeBuilder().setNodeId(new NodeId(nodeId))
                 .addAugmentation(NetconfNode.class,
                         new NetconfNodeBuilder()
@@ -491,4 +496,28 @@ public class NetconfNodeManagerCallback implements NodeManagerCallback{
             topologyDispatcher.unregisterMountPoint(new NodeId(nodeId));
         }
     }
+
+    @Override
+    public void onSessionUp(NetconfClientSession netconfClientSession) {
+        //NetconfClientSession is up, we can register role candidate
+        LOG.debug("Netconf client session is up, registering role candidate");
+        roleChangeStrategy.registerRoleCandidate(nodeManager);
+    }
+
+    @Override
+    public void onSessionDown(NetconfClientSession netconfClientSession, Exception e) {
+        LOG.debug("Netconf client session is down, unregistering role candidate");
+        roleChangeStrategy.unregisterRoleCandidate();
+    }
+
+    @Override
+    public void onSessionTerminated(NetconfClientSession netconfClientSession, NetconfTerminationReason netconfTerminationReason) {
+        LOG.debug("Netconf client session is down, unregistering role candidate");
+        roleChangeStrategy.unregisterRoleCandidate();
+    }
+
+    @Override
+    public void onMessage(NetconfClientSession netconfClientSession, NetconfMessage netconfMessage) {
+        //NOOP
+    }
 }
\ No newline at end of file