Bug 451 - Fix netconf exception handling
[controller.git] / opendaylight / netconf / netconf-impl / src / main / java / org / opendaylight / controller / netconf / impl / NetconfServerSessionNegotiatorFactory.java
index 6fce8d333ad49b28f7365c2c3dc8555432528c8c..9d958660615c27c27e86fee68e669f7e17c6a1b1 100644 (file)
@@ -8,45 +8,42 @@
 
 package org.opendaylight.controller.netconf.impl;
 
-import com.google.common.base.Preconditions;
+import com.google.common.collect.Sets;
 import io.netty.channel.Channel;
 import io.netty.util.Timer;
 import io.netty.util.concurrent.Promise;
+import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
 import org.opendaylight.controller.netconf.api.NetconfServerSessionPreferences;
 import org.opendaylight.controller.netconf.impl.mapping.CapabilityProvider;
 import org.opendaylight.controller.netconf.impl.osgi.SessionMonitoringService;
 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationProvider;
 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationServiceSnapshot;
-import org.opendaylight.controller.netconf.util.NetconfUtil;
 import org.opendaylight.controller.netconf.util.messages.NetconfHelloMessage;
-import org.opendaylight.controller.netconf.util.xml.XMLNetconfUtil;
 import org.opendaylight.controller.netconf.util.xml.XmlNetconfConstants;
-import org.opendaylight.controller.netconf.util.xml.XmlUtil;
 import org.opendaylight.protocol.framework.SessionListenerFactory;
 import org.opendaylight.protocol.framework.SessionNegotiator;
 import org.opendaylight.protocol.framework.SessionNegotiatorFactory;
-import org.w3c.dom.Document;
-import org.w3c.dom.Element;
-import org.w3c.dom.Node;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
-import javax.xml.xpath.XPathConstants;
-import javax.xml.xpath.XPathExpression;
-import java.io.InputStream;
+import java.util.Set;
 
 import static org.opendaylight.controller.netconf.mapping.api.NetconfOperationProvider.NetconfOperationProviderUtil.getNetconfSessionIdForReporting;
 
 public class NetconfServerSessionNegotiatorFactory implements SessionNegotiatorFactory<NetconfHelloMessage, NetconfServerSession, NetconfServerSessionListener> {
 
-    public static final String SERVER_HELLO_XML_LOCATION = "/server_hello.xml";
+    private static final Set<String> DEFAULT_CAPABILITIES = Sets.newHashSet(
+            XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0,
+            XmlNetconfConstants.URN_IETF_PARAMS_NETCONF_CAPABILITY_EXI_1_0);
 
     private final Timer timer;
 
-    private static final Document helloMessageTemplate = loadHelloMessageTemplate();
     private final SessionIdProvider idProvider;
     private final NetconfOperationProvider netconfOperationProvider;
     private final long connectionTimeoutMillis;
     private final DefaultCommitNotificationProducer commitNotificationProducer;
     private final SessionMonitoringService monitoringService;
+    private static final Logger logger = LoggerFactory.getLogger(NetconfServerSessionNegotiatorFactory.class);
 
     public NetconfServerSessionNegotiatorFactory(Timer timer, NetconfOperationProvider netconfOperationProvider,
                                                  SessionIdProvider idProvider, long connectionTimeoutMillis,
@@ -59,14 +56,6 @@ public class NetconfServerSessionNegotiatorFactory implements SessionNegotiatorF
         this.monitoringService = monitoringService;
     }
 
-    private static Document loadHelloMessageTemplate() {
-        InputStream resourceAsStream = NetconfServerSessionNegotiatorFactory.class
-                .getResourceAsStream(SERVER_HELLO_XML_LOCATION);
-        Preconditions.checkNotNull(resourceAsStream, "Unable to load server hello message blueprint from %s",
-                SERVER_HELLO_XML_LOCATION);
-        return NetconfUtil.createMessage(resourceAsStream).getDocument();
-    }
-
     /**
      *
      * @param defunctSessionListenerFactory will not be taken into account as session listener factory can
@@ -84,8 +73,14 @@ public class NetconfServerSessionNegotiatorFactory implements SessionNegotiatorF
                 getNetconfSessionIdForReporting(sessionId));
         CapabilityProvider capabilityProvider = new CapabilityProviderImpl(netconfOperationServiceSnapshot);
 
-        NetconfServerSessionPreferences proposal = new NetconfServerSessionPreferences(
-                createHelloMessage(sessionId, capabilityProvider), sessionId);
+        NetconfServerSessionPreferences proposal = null;
+        try {
+            proposal = new NetconfServerSessionPreferences(
+                    createHelloMessage(sessionId, capabilityProvider), sessionId);
+        } catch (NetconfDocumentedException e) {
+            logger.error("Unable to create hello mesage for session {} with capability provider {}", sessionId,capabilityProvider);
+            throw new IllegalStateException(e);
+        }
 
         NetconfServerSessionListenerFactory sessionListenerFactory = new NetconfServerSessionListenerFactory(
                 commitNotificationProducer, monitoringService,
@@ -95,32 +90,8 @@ public class NetconfServerSessionNegotiatorFactory implements SessionNegotiatorF
                 sessionListenerFactory.getSessionListener(), connectionTimeoutMillis);
     }
 
-    private static final XPathExpression sessionIdXPath = XMLNetconfUtil
-            .compileXPath("/netconf:hello/netconf:session-id");
-    private static final XPathExpression capabilitiesXPath = XMLNetconfUtil
-            .compileXPath("/netconf:hello/netconf:capabilities");
-
-    private NetconfHelloMessage createHelloMessage(long sessionId, CapabilityProvider capabilityProvider) {
-        Document helloMessageTemplate = getHelloTemplateClone();
-
-        // change session ID
-        final Node sessionIdNode = (Node) XmlUtil.evaluateXPath(sessionIdXPath, helloMessageTemplate,
-                XPathConstants.NODE);
-        sessionIdNode.setTextContent(String.valueOf(sessionId));
-
-        // add capabilities from yang store
-        final Element capabilitiesElement = (Element) XmlUtil.evaluateXPath(capabilitiesXPath, helloMessageTemplate,
-                XPathConstants.NODE);
-
-        for (String capability : capabilityProvider.getCapabilities()) {
-            final Element capabilityElement = helloMessageTemplate.createElement(XmlNetconfConstants.CAPABILITY);
-            capabilityElement.setTextContent(capability);
-            capabilitiesElement.appendChild(capabilityElement);
-        }
-        return new NetconfHelloMessage(helloMessageTemplate);
+    private NetconfHelloMessage createHelloMessage(long sessionId, CapabilityProvider capabilityProvider) throws NetconfDocumentedException {
+        return NetconfHelloMessage.createServerHello(Sets.union(capabilityProvider.getCapabilities(), DEFAULT_CAPABILITIES), sessionId);
     }
 
-    private synchronized Document getHelloTemplateClone() {
-        return (Document) helloMessageTemplate.cloneNode(true);
-    }
 }