2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.controller.netconf.impl;
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.impl.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;
30 import javax.xml.xpath.XPathConstants;
31 import javax.xml.xpath.XPathExpression;
32 import java.io.InputStream;
34 public class NetconfServerSessionNegotiatorFactory implements SessionNegotiatorFactory {
36 public static final String SERVER_HELLO_XML_LOCATION = "/server_hello.xml";
38 private final Timer timer;
40 private static final Document helloMessageTemplate = loadHelloMessageTemplate();
41 private final SessionIdProvider idProvider;
42 private final NetconfOperationServiceFactoryListener factoriesListener;
44 public NetconfServerSessionNegotiatorFactory(Timer timer, NetconfOperationServiceFactoryListener factoriesListener,
45 SessionIdProvider idProvider) {
47 this.factoriesListener = factoriesListener;
48 this.idProvider = idProvider;
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();
60 public SessionNegotiator getSessionNegotiator(SessionListenerFactory sessionListenerFactory, Channel channel,
62 long sessionId = idProvider.getNextSessionId();
64 NetconfServerSessionPreferences proposal = new NetconfServerSessionPreferences(createHelloMessage(sessionId),
66 return new NetconfServerSessionNegotiator(proposal, promise, channel, timer,
67 sessionListenerFactory.getSessionListener());
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");
75 private NetconfMessage createHelloMessage(long sessionId) {
76 Document helloMessageTemplate = getHelloTemplateClone();
79 final Node sessionIdNode = (Node) XmlUtil.evaluateXPath(sessionIdXPath, helloMessageTemplate,
81 sessionIdNode.setTextContent(String.valueOf(sessionId));
83 // add capabilities from yang store
84 final Element capabilitiesElement = (Element) XmlUtil.evaluateXPath(capabilitiesXPath, helloMessageTemplate,
87 CapabilityProvider capabilityProvider = new CapabilityProviderImpl(factoriesListener.getSnapshot(sessionId));
89 for (String capability : capabilityProvider.getCapabilities()) {
90 final Element capabilityElement = helloMessageTemplate.createElement(XmlNetconfConstants.CAPABILITY);
91 capabilityElement.setTextContent(capability);
92 capabilitiesElement.appendChild(capabilityElement);
94 return new NetconfMessage(helloMessageTemplate);
97 private synchronized Document getHelloTemplateClone() {
98 return (Document) this.helloMessageTemplate.cloneNode(true);