Remove netconf from commons/opendaylight pom
[controller.git] / opendaylight / netconf / netconf-impl / src / main / java / org / opendaylight / controller / netconf / impl / NetconfServerSessionListener.java
index 9510a61a6edd0a234ea9ee1fc5237c144fdd59ec..b0dcf7876b3b398f141cb205d6885da69cf5b098 100644 (file)
@@ -11,17 +11,15 @@ package org.opendaylight.controller.netconf.impl;
 
 import com.google.common.base.Preconditions;
 import com.google.common.collect.ImmutableMap;
-import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
+import org.opendaylight.controller.config.util.xml.DocumentedException;
+import org.opendaylight.controller.config.util.xml.XmlUtil;
 import org.opendaylight.controller.netconf.api.NetconfMessage;
 import org.opendaylight.controller.netconf.api.NetconfSessionListener;
 import org.opendaylight.controller.netconf.api.NetconfTerminationReason;
 import org.opendaylight.controller.netconf.api.monitoring.NetconfMonitoringService;
 import org.opendaylight.controller.netconf.api.xml.XmlNetconfConstants;
-import org.opendaylight.controller.netconf.impl.mapping.operations.DefaultCloseSession;
 import org.opendaylight.controller.netconf.impl.osgi.NetconfOperationRouter;
 import org.opendaylight.controller.netconf.util.messages.SendErrorExceptionUtil;
-import org.opendaylight.controller.netconf.util.xml.XmlElement;
-import org.opendaylight.controller.netconf.util.xml.XmlUtil;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.w3c.dom.Document;
@@ -35,7 +33,7 @@ public class NetconfServerSessionListener implements NetconfSessionListener<Netc
     private final NetconfOperationRouter operationRouter;
     private final AutoCloseable onSessionDownCloseable;
 
-    public NetconfServerSessionListener(final NetconfOperationRouter operationRouter, NetconfMonitoringService monitoringService,
+    public NetconfServerSessionListener(final NetconfOperationRouter operationRouter, final NetconfMonitoringService monitoringService,
                                         final AutoCloseable onSessionDownCloseable) {
         this.operationRouter = operationRouter;
         this.monitoringService = monitoringService;
@@ -45,6 +43,8 @@ public class NetconfServerSessionListener implements NetconfSessionListener<Netc
     @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
     }
 
     @Override
@@ -87,17 +87,12 @@ public class NetconfServerSessionListener implements NetconfSessionListener<Netc
                     session);
             LOG.debug("Responding with message {}", message);
             session.sendMessage(message);
-
-            if (isCloseSession(netconfMessage)) {
-                closeNetconfSession(session);
-            }
-
         } catch (final RuntimeException e) {
             // TODO: should send generic error or close session?
             LOG.error("Unexpected exception", e);
             session.onIncommingRpcFail();
             throw new IllegalStateException("Unable to process incoming message " + netconfMessage, e);
-        } catch (NetconfDocumentedException e) {
+        } catch (DocumentedException e) {
             LOG.trace("Error occurred while processing message",e);
             session.onOutgoingRpcError();
             session.onIncommingRpcFail();
@@ -105,16 +100,8 @@ public class NetconfServerSessionListener implements NetconfSessionListener<Netc
         }
     }
 
-    private void closeNetconfSession(final NetconfServerSession session) {
-        // destroy NetconfOperationService
-        session.close();
-        LOG.info("Session {} closed successfully", session.getSessionId());
-    }
-
-
-
     private NetconfMessage processDocument(final NetconfMessage netconfMessage, final NetconfServerSession session)
-            throws NetconfDocumentedException {
+            throws DocumentedException {
 
         final Document incomingDocument = netconfMessage.getDocument();
         final Node rootNode = incomingDocument.getDocumentElement();
@@ -139,14 +126,14 @@ public class NetconfServerSessionListener implements NetconfSessionListener<Netc
              * unexpected element Description: An unexpected element is present.
              */
             // TODO add message to error info
-            throw new NetconfDocumentedException("Unknown tag " + rootNode.getNodeName(),
-                    NetconfDocumentedException.ErrorType.protocol, NetconfDocumentedException.ErrorTag.unknown_element,
-                    NetconfDocumentedException.ErrorSeverity.error, ImmutableMap.of("bad-element",
+            throw new DocumentedException("Unknown tag " + rootNode.getNodeName(),
+                    DocumentedException.ErrorType.protocol, DocumentedException.ErrorTag.unknown_element,
+                    DocumentedException.ErrorSeverity.error, ImmutableMap.of("bad-element",
                             rootNode.getNodeName()));
         }
     }
 
-    private void checkMessageId(final Node rootNode) throws NetconfDocumentedException {
+    private static void checkMessageId(final Node rootNode) throws DocumentedException {
 
         NamedNodeMap attributes = rootNode.getAttributes();
 
@@ -158,20 +145,10 @@ public class NetconfServerSessionListener implements NetconfSessionListener<Netc
             return;
         }
 
-        throw new NetconfDocumentedException("Missing attribute" + rootNode.getNodeName(),
-                NetconfDocumentedException.ErrorType.protocol, NetconfDocumentedException.ErrorTag.missing_attribute,
-                NetconfDocumentedException.ErrorSeverity.error,
-                ImmutableMap.of(NetconfDocumentedException.ErrorTag.missing_attribute.toString(),
+        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));
     }
-
-    private static boolean isCloseSession(final NetconfMessage incomingDocument) {
-        final Document document = incomingDocument.getDocument();
-        XmlElement rpcElement = XmlElement.fromDomDocument(document);
-        if (rpcElement.getOnlyChildElementOptionally(DefaultCloseSession.CLOSE_SESSION).isPresent()) {
-            return true;
-        }
-
-        return false;
-    }
 }