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.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.util.NetconfUtil;
19 import org.opendaylight.controller.netconf.util.messages.NetconfHelloMessage;
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<NetconfHelloMessage, NetconfServerSession, NetconfServerSessionListener> {
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;
43 private final long connectionTimeoutMillis;
45 public NetconfServerSessionNegotiatorFactory(Timer timer, NetconfOperationServiceFactoryListener factoriesListener,
46 SessionIdProvider idProvider, long connectionTimeoutMillis) {
48 this.factoriesListener = factoriesListener;
49 this.idProvider = idProvider;
50 this.connectionTimeoutMillis = connectionTimeoutMillis;
53 private static Document loadHelloMessageTemplate() {
54 InputStream resourceAsStream = NetconfServerSessionNegotiatorFactory.class
55 .getResourceAsStream(SERVER_HELLO_XML_LOCATION);
56 Preconditions.checkNotNull(resourceAsStream, "Unable to load server hello message blueprint from %s",
57 SERVER_HELLO_XML_LOCATION);
58 return NetconfUtil.createMessage(resourceAsStream).getDocument();
62 public SessionNegotiator<NetconfServerSession> getSessionNegotiator(SessionListenerFactory<NetconfServerSessionListener> sessionListenerFactory, Channel channel,
63 Promise<NetconfServerSession> promise) {
64 long sessionId = idProvider.getNextSessionId();
66 NetconfServerSessionPreferences proposal = new NetconfServerSessionPreferences(createHelloMessage(sessionId),
68 return new NetconfServerSessionNegotiator(proposal, promise, channel, timer,
69 sessionListenerFactory.getSessionListener(), connectionTimeoutMillis);
72 private static final XPathExpression sessionIdXPath = XMLNetconfUtil
73 .compileXPath("/netconf:hello/netconf:session-id");
74 private static final XPathExpression capabilitiesXPath = XMLNetconfUtil
75 .compileXPath("/netconf:hello/netconf:capabilities");
77 private NetconfHelloMessage createHelloMessage(long sessionId) {
78 Document helloMessageTemplate = getHelloTemplateClone();
81 final Node sessionIdNode = (Node) XmlUtil.evaluateXPath(sessionIdXPath, helloMessageTemplate,
83 sessionIdNode.setTextContent(String.valueOf(sessionId));
85 // add capabilities from yang store
86 final Element capabilitiesElement = (Element) XmlUtil.evaluateXPath(capabilitiesXPath, helloMessageTemplate,
89 CapabilityProvider capabilityProvider = new CapabilityProviderImpl(factoriesListener.getSnapshot(sessionId));
91 for (String capability : capabilityProvider.getCapabilities()) {
92 final Element capabilityElement = helloMessageTemplate.createElement(XmlNetconfConstants.CAPABILITY);
93 capabilityElement.setTextContent(capability);
94 capabilitiesElement.appendChild(capabilityElement);
96 return new NetconfHelloMessage(helloMessageTemplate);
99 private synchronized Document getHelloTemplateClone() {
100 return (Document) helloMessageTemplate.cloneNode(true);