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.SessionMonitoringService;
23 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationProvider;
24 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationServiceSnapshot;
25 import org.opendaylight.controller.netconf.util.NetconfUtil;
26 import org.opendaylight.controller.netconf.util.messages.NetconfHelloMessage;
27 import org.opendaylight.controller.netconf.util.xml.XMLNetconfUtil;
28 import org.opendaylight.controller.netconf.util.xml.XmlNetconfConstants;
29 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
30 import org.opendaylight.protocol.framework.SessionListenerFactory;
31 import org.opendaylight.protocol.framework.SessionNegotiator;
32 import org.opendaylight.protocol.framework.SessionNegotiatorFactory;
33 import org.w3c.dom.Document;
34 import org.w3c.dom.Element;
35 import org.w3c.dom.Node;
37 import com.google.common.base.Optional;
38 import com.google.common.base.Preconditions;
40 import static org.opendaylight.controller.netconf.mapping.api.NetconfOperationProvider.NetconfOperationProviderUtil.getNetconfSessionIdForReporting;
42 public class NetconfServerSessionNegotiatorFactory implements SessionNegotiatorFactory<NetconfHelloMessage, NetconfServerSession, NetconfServerSessionListener> {
44 public static final String SERVER_HELLO_XML_LOCATION = "/server_hello.xml";
46 private final Timer timer;
48 private static final Document helloMessageTemplate = loadHelloMessageTemplate();
49 private final SessionIdProvider idProvider;
50 private final NetconfOperationProvider netconfOperationProvider;
51 private final long connectionTimeoutMillis;
52 private final DefaultCommitNotificationProducer commitNotificationProducer;
53 private final SessionMonitoringService monitoringService;
55 public NetconfServerSessionNegotiatorFactory(Timer timer, NetconfOperationProvider netconfOperationProvider,
56 SessionIdProvider idProvider, long connectionTimeoutMillis,
57 DefaultCommitNotificationProducer commitNot, SessionMonitoringService monitoringService) {
59 this.netconfOperationProvider = netconfOperationProvider;
60 this.idProvider = idProvider;
61 this.connectionTimeoutMillis = connectionTimeoutMillis;
62 this.commitNotificationProducer = commitNot;
63 this.monitoringService = monitoringService;
66 private static Document loadHelloMessageTemplate() {
67 InputStream resourceAsStream = NetconfServerSessionNegotiatorFactory.class
68 .getResourceAsStream(SERVER_HELLO_XML_LOCATION);
69 Preconditions.checkNotNull(resourceAsStream, "Unable to load server hello message blueprint from %s",
70 SERVER_HELLO_XML_LOCATION);
71 return NetconfUtil.createMessage(resourceAsStream).getDocument();
76 * @param defunctSessionListenerFactory will not be taken into account as session listener factory can
77 * only be created after snapshot is opened, thus this method constructs
78 * proper session listener factory.
79 * @param channel Underlying channel
80 * @param promise Promise to be notified
81 * @return session negotiator
84 public SessionNegotiator<NetconfServerSession> getSessionNegotiator(SessionListenerFactory<NetconfServerSessionListener> defunctSessionListenerFactory,
85 Channel channel, Promise<NetconfServerSession> promise) {
86 long sessionId = idProvider.getNextSessionId();
87 NetconfOperationServiceSnapshot netconfOperationServiceSnapshot = netconfOperationProvider.openSnapshot(
88 getNetconfSessionIdForReporting(sessionId));
89 CapabilityProvider capabilityProvider = new CapabilityProviderImpl(netconfOperationServiceSnapshot);
91 NetconfServerSessionPreferences proposal = new NetconfServerSessionPreferences(
92 createHelloMessage(sessionId, capabilityProvider), sessionId);
94 NetconfServerSessionListenerFactory sessionListenerFactory = new NetconfServerSessionListenerFactory(
95 commitNotificationProducer, monitoringService,
96 netconfOperationServiceSnapshot, capabilityProvider);
98 return new NetconfServerSessionNegotiator(proposal, promise, channel, timer,
99 sessionListenerFactory.getSessionListener(), connectionTimeoutMillis);
102 private static final XPathExpression sessionIdXPath = XMLNetconfUtil
103 .compileXPath("/netconf:hello/netconf:session-id");
104 private static final XPathExpression capabilitiesXPath = XMLNetconfUtil
105 .compileXPath("/netconf:hello/netconf:capabilities");
107 private NetconfHelloMessage createHelloMessage(long sessionId, CapabilityProvider capabilityProvider) {
108 Document helloMessageTemplate = getHelloTemplateClone();
111 final Node sessionIdNode = (Node) XmlUtil.evaluateXPath(sessionIdXPath, helloMessageTemplate,
112 XPathConstants.NODE);
113 sessionIdNode.setTextContent(String.valueOf(sessionId));
115 // add capabilities from yang store
116 final Element capabilitiesElement = (Element) XmlUtil.evaluateXPath(capabilitiesXPath, helloMessageTemplate,
117 XPathConstants.NODE);
119 for (String capability : capabilityProvider.getCapabilities()) {
120 final Element capabilityElement = XmlUtil.createElement(helloMessageTemplate, XmlNetconfConstants.CAPABILITY, Optional.<String>absent());
121 capabilityElement.setTextContent(capability);
122 capabilitiesElement.appendChild(capabilityElement);
124 return new NetconfHelloMessage(helloMessageTemplate);
127 private synchronized Document getHelloTemplateClone() {
128 return (Document) helloMessageTemplate.cloneNode(true);