Bug 3864: Notify netconf monitoring about changes in session
[netconf.git] / netconf / netconf-impl / src / main / java / org / opendaylight / netconf / impl / NetconfServerSessionListener.java
index a1a16f426c17c7316a2f6c082644b071bbef8d99..4117c3189f29e8cf13a1913238e54ec7f9995518 100644 (file)
@@ -13,13 +13,17 @@ import com.google.common.base.Preconditions;
 import com.google.common.collect.ImmutableMap;
 import org.opendaylight.controller.config.util.xml.DocumentedException;
 import org.opendaylight.controller.config.util.xml.XmlUtil;
-import org.opendaylight.netconf.util.messages.SendErrorExceptionUtil;
 import org.opendaylight.netconf.api.NetconfMessage;
 import org.opendaylight.netconf.api.NetconfSessionListener;
 import org.opendaylight.netconf.api.NetconfTerminationReason;
 import org.opendaylight.netconf.api.monitoring.NetconfMonitoringService;
+import org.opendaylight.netconf.api.monitoring.SessionEvent;
+import org.opendaylight.netconf.api.monitoring.SessionListener;
 import org.opendaylight.netconf.api.xml.XmlNetconfConstants;
 import org.opendaylight.netconf.impl.osgi.NetconfOperationRouter;
+import org.opendaylight.netconf.util.messages.SubtreeFilter;
+import org.opendaylight.netconf.notifications.NetconfNotification;
+import org.opendaylight.netconf.util.messages.SendErrorExceptionUtil;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.w3c.dom.Document;
@@ -29,22 +33,20 @@ import org.w3c.dom.Node;
 public class NetconfServerSessionListener implements NetconfSessionListener<NetconfServerSession> {
 
     private static final Logger LOG = LoggerFactory.getLogger(NetconfServerSessionListener.class);
-    private final NetconfMonitoringService monitoringService;
+    private final SessionListener monitoringSessionListener;
     private final NetconfOperationRouter operationRouter;
     private final AutoCloseable onSessionDownCloseable;
 
     public NetconfServerSessionListener(final NetconfOperationRouter operationRouter, final NetconfMonitoringService monitoringService,
                                         final AutoCloseable onSessionDownCloseable) {
         this.operationRouter = operationRouter;
-        this.monitoringService = monitoringService;
+        this.monitoringSessionListener = monitoringService.getSessionListener();
         this.onSessionDownCloseable = onSessionDownCloseable;
     }
 
     @Override
     public void onSessionUp(final NetconfServerSession netconfNetconfServerSession) {
-        monitoringService.onSessionUp(netconfNetconfServerSession);
-        // FIXME monitoring service should be also notified about all the other changes to netconf session (from ietf-netconf-monitoring point of view)
-        // This means also notifying after every message is processed
+        monitoringSessionListener.onSessionUp(netconfNetconfServerSession);
     }
 
     @Override
@@ -54,7 +56,7 @@ public class NetconfServerSessionListener implements NetconfSessionListener<Netc
     }
 
     public void onDown(final NetconfServerSession netconfNetconfServerSession) {
-        monitoringService.onSessionDown(netconfNetconfServerSession);
+        monitoringSessionListener.onSessionDown(netconfNetconfServerSession);
 
         try {
             operationRouter.close();
@@ -81,25 +83,32 @@ public class NetconfServerSessionListener implements NetconfSessionListener<Netc
         try {
 
             Preconditions.checkState(operationRouter != null, "Cannot handle message, session up was not yet received");
-            // FIXME: there is no validation since the document may contain yang
-            // schemas
+            // there is no validation since the document may contain yang schemas
             final NetconfMessage message = processDocument(netconfMessage,
                     session);
             LOG.debug("Responding with message {}", message);
             session.sendMessage(message);
+            monitoringSessionListener.onSessionEvent(SessionEvent.inRpcSuccess(session));
         } catch (final RuntimeException e) {
             // TODO: should send generic error or close session?
             LOG.error("Unexpected exception", e);
             session.onIncommingRpcFail();
+            monitoringSessionListener.onSessionEvent(SessionEvent.inRpcFail(session));
             throw new IllegalStateException("Unable to process incoming message " + netconfMessage, e);
         } catch (DocumentedException e) {
             LOG.trace("Error occurred while processing message",e);
             session.onOutgoingRpcError();
             session.onIncommingRpcFail();
+            monitoringSessionListener.onSessionEvent(SessionEvent.inRpcFail(session));
+            monitoringSessionListener.onSessionEvent(SessionEvent.outRpcError(session));
             SendErrorExceptionUtil.sendErrorMessage(session, e, netconfMessage);
         }
     }
 
+    public void onNotification(final NetconfServerSession session, final NetconfNotification notification) {
+        monitoringSessionListener.onSessionEvent(SessionEvent.notification(session));
+    }
+
     private NetconfMessage processDocument(final NetconfMessage netconfMessage, final NetconfServerSession session)
             throws DocumentedException {
 
@@ -112,7 +121,7 @@ public class NetconfServerSessionListener implements NetconfSessionListener<Netc
 
             Document rpcReply = operationRouter.onNetconfMessage(incomingDocument, session);
 
-            rpcReply = SubtreeFilter.applySubtreeFilter(incomingDocument, rpcReply);
+            rpcReply = SubtreeFilter.applyRpcSubtreeFilter(incomingDocument, rpcReply);
 
             session.onIncommingRpcSuccess();