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 io.netty.channel.Channel;
12 import io.netty.util.Timer;
13 import io.netty.util.concurrent.Promise;
15 import java.io.InputStream;
17 import javax.xml.xpath.XPathConstants;
18 import javax.xml.xpath.XPathExpression;
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;
35 import com.google.common.base.Optional;
36 import com.google.common.base.Preconditions;
38 public class NetconfServerSessionNegotiatorFactory implements SessionNegotiatorFactory<NetconfHelloMessage, NetconfServerSession, NetconfServerSessionListener> {
40 public static final String SERVER_HELLO_XML_LOCATION = "/server_hello.xml";
42 private final Timer timer;
44 private static final Document helloMessageTemplate = loadHelloMessageTemplate();
45 private final SessionIdProvider idProvider;
46 private final NetconfOperationServiceFactoryListener factoriesListener;
47 private final long connectionTimeoutMillis;
49 public NetconfServerSessionNegotiatorFactory(Timer timer, NetconfOperationServiceFactoryListener factoriesListener,
50 SessionIdProvider idProvider, long connectionTimeoutMillis) {
52 this.factoriesListener = factoriesListener;
53 this.idProvider = idProvider;
54 this.connectionTimeoutMillis = connectionTimeoutMillis;
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();
66 public SessionNegotiator<NetconfServerSession> getSessionNegotiator(SessionListenerFactory<NetconfServerSessionListener> sessionListenerFactory, Channel channel,
67 Promise<NetconfServerSession> promise) {
68 long sessionId = idProvider.getNextSessionId();
70 NetconfServerSessionPreferences proposal = new NetconfServerSessionPreferences(createHelloMessage(sessionId),
72 return new NetconfServerSessionNegotiator(proposal, promise, channel, timer,
73 sessionListenerFactory.getSessionListener(), connectionTimeoutMillis);
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");
81 private NetconfHelloMessage createHelloMessage(long sessionId) {
82 Document helloMessageTemplate = getHelloTemplateClone();
85 final Node sessionIdNode = (Node) XmlUtil.evaluateXPath(sessionIdXPath, helloMessageTemplate,
87 sessionIdNode.setTextContent(String.valueOf(sessionId));
89 // add capabilities from yang store
90 final Element capabilitiesElement = (Element) XmlUtil.evaluateXPath(capabilitiesXPath, helloMessageTemplate,
93 CapabilityProvider capabilityProvider = new CapabilityProviderImpl(factoriesListener.getSnapshot(sessionId));
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);
100 return new NetconfHelloMessage(helloMessageTemplate);
103 private synchronized Document getHelloTemplateClone() {
104 return (Document) helloMessageTemplate.cloneNode(true);