Merge "Bug 509: Fixed incorrect merging of Data Store Writes / Events"
[controller.git] / opendaylight / netconf / netconf-impl / src / main / java / org / opendaylight / controller / netconf / impl / NetconfServerSessionNegotiatorFactory.java
index 6fce8d333ad49b28f7365c2c3dc8555432528c8c..9106b6ad85ef68ec00e4cbb037c82e1b93047b03 100644 (file)
@@ -8,40 +8,35 @@
 
 package org.opendaylight.controller.netconf.impl;
 
-import com.google.common.base.Preconditions;
-import io.netty.channel.Channel;
-import io.netty.util.Timer;
-import io.netty.util.concurrent.Promise;
+import static org.opendaylight.controller.netconf.mapping.api.NetconfOperationProvider.NetconfOperationProviderUtil.getNetconfSessionIdForReporting;
+
+import java.util.Set;
+
 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 javax.xml.xpath.XPathConstants;
-import javax.xml.xpath.XPathExpression;
-import java.io.InputStream;
+import com.google.common.collect.Sets;
 
-import static org.opendaylight.controller.netconf.mapping.api.NetconfOperationProvider.NetconfOperationProviderUtil.getNetconfSessionIdForReporting;
+import io.netty.channel.Channel;
+import io.netty.util.Timer;
+import io.netty.util.concurrent.Promise;
 
 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;
@@ -59,14 +54,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
@@ -95,32 +82,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);
+        return NetconfHelloMessage.createServerHello(Sets.union(capabilityProvider.getCapabilities(), DEFAULT_CAPABILITIES), sessionId);
     }
 
-    private synchronized Document getHelloTemplateClone() {
-        return (Document) helloMessageTemplate.cloneNode(true);
-    }
 }