Remove NetconfServerSessionPreferences
[netconf.git] / netconf / netconf-impl / src / main / java / org / opendaylight / netconf / impl / NetconfServerSessionListener.java
index ca61311cdcbd48f046d2be71644be9432e2296ad..141edb98d36a88f23261c92e56b6d2ca1ee663af 100644 (file)
@@ -5,22 +5,25 @@
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
  * and is available at http://www.eclipse.org/legal/epl-v10.html
  */
-
 package org.opendaylight.netconf.impl;
 
-
 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.DocumentedException;
 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.api.xml.XmlUtil;
 import org.opendaylight.netconf.impl.osgi.NetconfOperationRouter;
+import org.opendaylight.netconf.notifications.NetconfNotification;
 import org.opendaylight.netconf.util.messages.SubtreeFilter;
+import org.opendaylight.yangtools.yang.common.ErrorSeverity;
+import org.opendaylight.yangtools.yang.common.ErrorTag;
+import org.opendaylight.yangtools.yang.common.ErrorType;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.w3c.dom.Document;
@@ -30,22 +33,21 @@ 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,
+    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,53 +56,62 @@ public class NetconfServerSessionListener implements NetconfSessionListener<Netc
         onDown(netconfNetconfServerSession);
     }
 
+    @SuppressWarnings("checkstyle:IllegalCatch")
     public void onDown(final NetconfServerSession netconfNetconfServerSession) {
-        monitoringService.onSessionDown(netconfNetconfServerSession);
+        monitoringSessionListener.onSessionDown(netconfNetconfServerSession);
 
         try {
             operationRouter.close();
-        } catch (Exception closingEx) {
+        } catch (final Exception closingEx) {
             LOG.debug("Ignoring exception while closing operationRouter", closingEx);
         }
         try {
             onSessionDownCloseable.close();
-        } catch(Exception ex){
+        } catch (final Exception ex) {
             LOG.debug("Ignoring exception while closing onSessionDownCloseable", ex);
         }
     }
 
     @Override
     public void onSessionTerminated(final NetconfServerSession netconfNetconfServerSession,
-            final NetconfTerminationReason netconfTerminationReason) {
+                                    final NetconfTerminationReason netconfTerminationReason) {
         LOG.debug("Session {} terminated, reason: {}", netconfNetconfServerSession,
                 netconfTerminationReason.getErrorMessage());
         onDown(netconfNetconfServerSession);
     }
 
+    @SuppressWarnings("checkstyle:IllegalCatch")
     @Override
     public void onMessage(final NetconfServerSession session, final NetconfMessage netconfMessage) {
         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);
+        } catch (final 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 {
 
@@ -127,28 +138,27 @@ public class NetconfServerSessionListener implements NetconfSessionListener<Netc
              * unexpected element Description: An unexpected element is present.
              */
             throw new DocumentedException("Unknown tag " + rootNode.getNodeName() + " in message:\n" + netconfMessage,
-                    DocumentedException.ErrorType.protocol, DocumentedException.ErrorTag.unknown_element,
-                    DocumentedException.ErrorSeverity.error, ImmutableMap.of("bad-element",
-                            rootNode.getNodeName()));
+                    ErrorType.PROTOCOL, ErrorTag.UNKNOWN_ELEMENT, ErrorSeverity.ERROR,
+                    ImmutableMap.of("bad-element", rootNode.getNodeName()));
         }
     }
 
     private static void checkMessageId(final Node rootNode) throws DocumentedException {
 
-        NamedNodeMap attributes = rootNode.getAttributes();
+        final NamedNodeMap attributes = rootNode.getAttributes();
 
-        if(attributes.getNamedItemNS(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0, XmlNetconfConstants.MESSAGE_ID)!=null) {
+        if (attributes.getNamedItemNS(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0,
+                XmlNetconfConstants.MESSAGE_ID) != null) {
             return;
         }
 
-        if(attributes.getNamedItem(XmlNetconfConstants.MESSAGE_ID)!=null) {
+        if (attributes.getNamedItem(XmlNetconfConstants.MESSAGE_ID) != null) {
             return;
         }
 
-        throw new DocumentedException("Missing attribute" + rootNode.getNodeName(),
-                DocumentedException.ErrorType.protocol, DocumentedException.ErrorTag.missing_attribute,
-                DocumentedException.ErrorSeverity.error,
-                ImmutableMap.of(DocumentedException.ErrorTag.missing_attribute.toString(),
-                        XmlNetconfConstants.MESSAGE_ID));
+        throw new DocumentedException("Missing attribute " + rootNode.getNodeName(),
+                ErrorType.RPC, ErrorTag.MISSING_ATTRIBUTE, ErrorSeverity.ERROR, ImmutableMap.of(
+                    "bad-attribute", XmlNetconfConstants.MESSAGE_ID,
+                    "bad-element", XmlNetconfConstants.RPC_KEY));
     }
 }