3c14d5124f395f54182ce125e1a9bed4ccf03d93
[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 com.google.common.base.Preconditions;
12 import io.netty.channel.Channel;
13 import io.netty.util.Timer;
14 import io.netty.util.concurrent.Promise;
15 import org.opendaylight.controller.netconf.api.NetconfMessage;
16 import org.opendaylight.controller.netconf.api.NetconfServerSessionPreferences;
17 import org.opendaylight.controller.netconf.impl.mapping.CapabilityProvider;
18 import org.opendaylight.controller.netconf.impl.osgi.NetconfOperationServiceFactoryListener;
19 import org.opendaylight.controller.netconf.util.NetconfUtil;
20 import org.opendaylight.controller.netconf.util.xml.XMLNetconfUtil;
21 import org.opendaylight.controller.netconf.util.xml.XmlNetconfConstants;
22 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
23 import org.opendaylight.protocol.framework.SessionListenerFactory;
24 import org.opendaylight.protocol.framework.SessionNegotiator;
25 import org.opendaylight.protocol.framework.SessionNegotiatorFactory;
26 import org.w3c.dom.Document;
27 import org.w3c.dom.Element;
28 import org.w3c.dom.Node;
29
30 import javax.xml.xpath.XPathConstants;
31 import javax.xml.xpath.XPathExpression;
32 import java.io.InputStream;
33
34 public class NetconfServerSessionNegotiatorFactory implements SessionNegotiatorFactory {
35
36     public static final String SERVER_HELLO_XML_LOCATION = "/server_hello.xml";
37
38     private final Timer timer;
39
40     private static final Document helloMessageTemplate = loadHelloMessageTemplate();
41     private final SessionIdProvider idProvider;
42     private final NetconfOperationServiceFactoryListener factoriesListener;
43
44     public NetconfServerSessionNegotiatorFactory(Timer timer, NetconfOperationServiceFactoryListener factoriesListener,
45             SessionIdProvider idProvider) {
46         this.timer = timer;
47         this.factoriesListener = factoriesListener;
48         this.idProvider = idProvider;
49     }
50
51     private static Document loadHelloMessageTemplate() {
52         InputStream resourceAsStream = NetconfServerSessionNegotiatorFactory.class
53                 .getResourceAsStream(SERVER_HELLO_XML_LOCATION);
54         Preconditions.checkNotNull(resourceAsStream, "Unable to load server hello message blueprint from %s",
55                 SERVER_HELLO_XML_LOCATION);
56         return NetconfUtil.createMessage(resourceAsStream).getDocument();
57     }
58
59     @Override
60     public SessionNegotiator getSessionNegotiator(SessionListenerFactory sessionListenerFactory, Channel channel,
61             Promise promise) {
62         long sessionId = idProvider.getNextSessionId();
63
64         NetconfServerSessionPreferences proposal = new NetconfServerSessionPreferences(createHelloMessage(sessionId),
65                 sessionId);
66         return new NetconfServerSessionNegotiator(proposal, promise, channel, timer,
67                 sessionListenerFactory.getSessionListener());
68     }
69
70     private static final XPathExpression sessionIdXPath = XMLNetconfUtil
71             .compileXPath("/netconf:hello/netconf:session-id");
72     private static final XPathExpression capabilitiesXPath = XMLNetconfUtil
73             .compileXPath("/netconf:hello/netconf:capabilities");
74
75     private NetconfMessage createHelloMessage(long sessionId) {
76         Document helloMessageTemplate = getHelloTemplateClone();
77
78         // change session ID
79         final Node sessionIdNode = (Node) XmlUtil.evaluateXPath(sessionIdXPath, helloMessageTemplate,
80                 XPathConstants.NODE);
81         sessionIdNode.setTextContent(String.valueOf(sessionId));
82
83         // add capabilities from yang store
84         final Element capabilitiesElement = (Element) XmlUtil.evaluateXPath(capabilitiesXPath, helloMessageTemplate,
85                 XPathConstants.NODE);
86
87         CapabilityProvider capabilityProvider = new CapabilityProviderImpl(factoriesListener.getSnapshot(sessionId));
88
89         for (String capability : capabilityProvider.getCapabilities()) {
90             final Element capabilityElement = helloMessageTemplate.createElement(XmlNetconfConstants.CAPABILITY);
91             capabilityElement.setTextContent(capability);
92             capabilitiesElement.appendChild(capabilityElement);
93         }
94         return new NetconfMessage(helloMessageTemplate);
95     }
96
97     private synchronized Document getHelloTemplateClone() {
98         return (Document) this.helloMessageTemplate.cloneNode(true);
99     }
100 }