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