Bug 714 - Fixed creating DOM Document's element with namespace
[controller.git] / opendaylight / netconf / netconf-impl / src / main / java / org / opendaylight / controller / netconf / impl / NetconfServerSessionNegotiatorFactory.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.controller.netconf.impl;
10
11 import io.netty.channel.Channel;
12 import io.netty.util.Timer;
13 import io.netty.util.concurrent.Promise;
14
15 import java.io.InputStream;
16
17 import javax.xml.xpath.XPathConstants;
18 import javax.xml.xpath.XPathExpression;
19
20 import org.opendaylight.controller.netconf.api.NetconfServerSessionPreferences;
21 import org.opendaylight.controller.netconf.impl.mapping.CapabilityProvider;
22 import org.opendaylight.controller.netconf.impl.osgi.NetconfOperationServiceFactoryListener;
23 import org.opendaylight.controller.netconf.util.NetconfUtil;
24 import org.opendaylight.controller.netconf.util.messages.NetconfHelloMessage;
25 import org.opendaylight.controller.netconf.util.xml.XMLNetconfUtil;
26 import org.opendaylight.controller.netconf.util.xml.XmlNetconfConstants;
27 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
28 import org.opendaylight.protocol.framework.SessionListenerFactory;
29 import org.opendaylight.protocol.framework.SessionNegotiator;
30 import org.opendaylight.protocol.framework.SessionNegotiatorFactory;
31 import org.w3c.dom.Document;
32 import org.w3c.dom.Element;
33 import org.w3c.dom.Node;
34
35 import com.google.common.base.Optional;
36 import com.google.common.base.Preconditions;
37
38 public class NetconfServerSessionNegotiatorFactory implements SessionNegotiatorFactory<NetconfHelloMessage, NetconfServerSession, NetconfServerSessionListener> {
39
40     public static final String SERVER_HELLO_XML_LOCATION = "/server_hello.xml";
41
42     private final Timer timer;
43
44     private static final Document helloMessageTemplate = loadHelloMessageTemplate();
45     private final SessionIdProvider idProvider;
46     private final NetconfOperationServiceFactoryListener factoriesListener;
47     private final long connectionTimeoutMillis;
48
49     public NetconfServerSessionNegotiatorFactory(Timer timer, NetconfOperationServiceFactoryListener factoriesListener,
50             SessionIdProvider idProvider, long connectionTimeoutMillis) {
51         this.timer = timer;
52         this.factoriesListener = factoriesListener;
53         this.idProvider = idProvider;
54         this.connectionTimeoutMillis = connectionTimeoutMillis;
55     }
56
57     private static Document loadHelloMessageTemplate() {
58         InputStream resourceAsStream = NetconfServerSessionNegotiatorFactory.class
59                 .getResourceAsStream(SERVER_HELLO_XML_LOCATION);
60         Preconditions.checkNotNull(resourceAsStream, "Unable to load server hello message blueprint from %s",
61                 SERVER_HELLO_XML_LOCATION);
62         return NetconfUtil.createMessage(resourceAsStream).getDocument();
63     }
64
65     @Override
66     public SessionNegotiator<NetconfServerSession> getSessionNegotiator(SessionListenerFactory<NetconfServerSessionListener> sessionListenerFactory, Channel channel,
67             Promise<NetconfServerSession> promise) {
68         long sessionId = idProvider.getNextSessionId();
69
70         NetconfServerSessionPreferences proposal = new NetconfServerSessionPreferences(createHelloMessage(sessionId),
71                 sessionId);
72         return new NetconfServerSessionNegotiator(proposal, promise, channel, timer,
73                 sessionListenerFactory.getSessionListener(), connectionTimeoutMillis);
74     }
75
76     private static final XPathExpression sessionIdXPath = XMLNetconfUtil
77             .compileXPath("/netconf:hello/netconf:session-id");
78     private static final XPathExpression capabilitiesXPath = XMLNetconfUtil
79             .compileXPath("/netconf:hello/netconf:capabilities");
80
81     private NetconfHelloMessage createHelloMessage(long sessionId) {
82         Document helloMessageTemplate = getHelloTemplateClone();
83
84         // change session ID
85         final Node sessionIdNode = (Node) XmlUtil.evaluateXPath(sessionIdXPath, helloMessageTemplate,
86                 XPathConstants.NODE);
87         sessionIdNode.setTextContent(String.valueOf(sessionId));
88
89         // add capabilities from yang store
90         final Element capabilitiesElement = (Element) XmlUtil.evaluateXPath(capabilitiesXPath, helloMessageTemplate,
91                 XPathConstants.NODE);
92
93         CapabilityProvider capabilityProvider = new CapabilityProviderImpl(factoriesListener.getSnapshot(sessionId));
94
95         for (String capability : capabilityProvider.getCapabilities()) {
96             final Element capabilityElement = XmlUtil.createElement(helloMessageTemplate, XmlNetconfConstants.CAPABILITY, Optional.<String>absent());
97             capabilityElement.setTextContent(capability);
98             capabilitiesElement.appendChild(capabilityElement);
99         }
100         return new NetconfHelloMessage(helloMessageTemplate);
101     }
102
103     private synchronized Document getHelloTemplateClone() {
104         return (Document) helloMessageTemplate.cloneNode(true);
105     }
106 }