Merge "Bug 714 - Fixed creating DOM Document's element with namespace"
[controller.git] / opendaylight / netconf / netconf-impl / src / main / java / org / opendaylight / controller / netconf / impl / NetconfServerSessionListener.java
index 43e55d746a4ee59d1ec9bb35245f74e0edbca018..86cfac0b606537973ab1bdeb32bcbad3d23aba7a 100644 (file)
@@ -12,14 +12,14 @@ 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;
 import org.opendaylight.controller.netconf.util.xml.XmlNetconfConstants;
 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
-import org.opendaylight.protocol.framework.SessionListener;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.w3c.dom.Document;
@@ -27,19 +27,19 @@ import org.w3c.dom.Node;
 
 import static com.google.common.base.Preconditions.checkState;
 
-public class NetconfServerSessionListener implements
-        SessionListener<NetconfMessage, NetconfServerSession, NetconfTerminationReason> {
+public class NetconfServerSessionListener implements NetconfSessionListener<NetconfServerSession> {
+    public static final String MESSAGE_ID = "message-id";
 
     static final Logger logger = LoggerFactory.getLogger(NetconfServerSessionListener.class);
-    public static final String MESSAGE_ID = "message-id";
     private final SessionMonitoringService monitoringService;
+    private final NetconfOperationRouter operationRouter;
+    private final AutoCloseable onSessionDownCloseable;
 
-    private NetconfOperationRouterImpl operationRouter;
-
-    public NetconfServerSessionListener(NetconfOperationRouterImpl operationRouter,
-                                        SessionMonitoringService monitoringService) {
+    public NetconfServerSessionListener(NetconfOperationRouter operationRouter, SessionMonitoringService monitoringService,
+                                        AutoCloseable onSessionDownCloseable) {
         this.operationRouter = operationRouter;
         this.monitoringService = monitoringService;
+        this.onSessionDownCloseable = onSessionDownCloseable;
     }
 
     @Override
@@ -48,11 +48,24 @@ public class NetconfServerSessionListener implements
     }
 
     @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
@@ -60,9 +73,7 @@ public class NetconfServerSessionListener implements
             NetconfTerminationReason netconfTerminationReason) {
         logger.debug("Session {} terminated, reason: {}", netconfNetconfServerSession,
                 netconfTerminationReason.getErrorMessage());
-        monitoringService.onSessionDown(netconfNetconfServerSession);
-
-        operationRouter.close();
+        onDown(netconfNetconfServerSession);
     }
 
     @Override