Resolve Bug:713 - Open snapshot only once per session.
[controller.git] / opendaylight / netconf / netconf-impl / src / main / java / org / opendaylight / controller / netconf / impl / NetconfServerSessionListener.java
index 460288fe33256147a6a55c666cfbbd9dd8181862..86cfac0b606537973ab1bdeb32bcbad3d23aba7a 100644 (file)
@@ -8,13 +8,13 @@
 
 package org.opendaylight.controller.netconf.impl;
 
-import static com.google.common.base.Preconditions.checkState;
-
+import com.google.common.base.Preconditions;
+import com.google.common.collect.ImmutableMap;
 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
 import org.opendaylight.controller.netconf.api.NetconfMessage;
+import org.opendaylight.controller.netconf.api.NetconfOperationRouter;
 import org.opendaylight.controller.netconf.api.NetconfSessionListener;
 import org.opendaylight.controller.netconf.api.NetconfTerminationReason;
-import org.opendaylight.controller.netconf.impl.osgi.NetconfOperationRouterImpl;
 import org.opendaylight.controller.netconf.impl.osgi.SessionMonitoringService;
 import org.opendaylight.controller.netconf.util.messages.SendErrorExceptionUtil;
 import org.opendaylight.controller.netconf.util.xml.XmlElement;
@@ -25,19 +25,21 @@ import org.slf4j.LoggerFactory;
 import org.w3c.dom.Document;
 import org.w3c.dom.Node;
 
-import com.google.common.base.Preconditions;
-import com.google.common.collect.ImmutableMap;
+import static com.google.common.base.Preconditions.checkState;
 
 public class NetconfServerSessionListener implements NetconfSessionListener<NetconfServerSession> {
     public static final String MESSAGE_ID = "message-id";
 
     static final Logger logger = LoggerFactory.getLogger(NetconfServerSessionListener.class);
     private final SessionMonitoringService monitoringService;
-    private final NetconfOperationRouterImpl operationRouter;
+    private final NetconfOperationRouter operationRouter;
+    private final AutoCloseable onSessionDownCloseable;
 
-    public NetconfServerSessionListener(NetconfOperationRouterImpl operationRouter, SessionMonitoringService monitoringService) {
+    public NetconfServerSessionListener(NetconfOperationRouter operationRouter, SessionMonitoringService monitoringService,
+                                        AutoCloseable onSessionDownCloseable) {
         this.operationRouter = operationRouter;
         this.monitoringService = monitoringService;
+        this.onSessionDownCloseable = onSessionDownCloseable;
     }
 
     @Override
@@ -46,11 +48,24 @@ public class NetconfServerSessionListener implements NetconfSessionListener<Netc
     }
 
     @Override
-    public void onSessionDown(NetconfServerSession netconfNetconfServerSession, Exception e) {
-        logger.debug("Session {} down, reason: {}", netconfNetconfServerSession, e.getMessage());
+    public void onSessionDown(NetconfServerSession netconfNetconfServerSession, Exception cause) {
+        logger.debug("Session {} down, reason: {}", netconfNetconfServerSession, cause.getMessage());
+        onDown(netconfNetconfServerSession);
+    }
+
+    public void onDown(NetconfServerSession netconfNetconfServerSession) {
         monitoringService.onSessionDown(netconfNetconfServerSession);
 
-        operationRouter.close();
+        try {
+            operationRouter.close();
+        } catch (Exception closingEx) {
+            logger.debug("Ignoring exception while closing operationRouter", closingEx);
+        }
+        try {
+            onSessionDownCloseable.close();
+        } catch(Exception ex){
+            logger.debug("Ignoring exception while closing onSessionDownCloseable", ex);
+        }
     }
 
     @Override
@@ -58,9 +73,7 @@ public class NetconfServerSessionListener implements NetconfSessionListener<Netc
             NetconfTerminationReason netconfTerminationReason) {
         logger.debug("Session {} terminated, reason: {}", netconfNetconfServerSession,
                 netconfTerminationReason.getErrorMessage());
-        monitoringService.onSessionDown(netconfNetconfServerSession);
-
-        operationRouter.close();
+        onDown(netconfNetconfServerSession);
     }
 
     @Override