Merge "Resolve Bug:707 - ConfigPusher should wait for netconf-impl to register JMX...
[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 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.mapping.api.NetconfOperationProvider;
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;
29
30 import javax.xml.xpath.XPathConstants;
31 import javax.xml.xpath.XPathExpression;
32 import java.io.InputStream;
33
34 import static org.opendaylight.controller.netconf.mapping.api.NetconfOperationProvider.NetconfOperationProviderUtil.getNetconfSessionIdForReporting;
35
36 public class NetconfServerSessionNegotiatorFactory implements SessionNegotiatorFactory<NetconfHelloMessage, NetconfServerSession, NetconfServerSessionListener> {
37
38     public static final String SERVER_HELLO_XML_LOCATION = "/server_hello.xml";
39
40     private final Timer timer;
41
42     private static final Document helloMessageTemplate = loadHelloMessageTemplate();
43     private final SessionIdProvider idProvider;
44     private final NetconfOperationProvider netconfOperationProvider;
45     private final long connectionTimeoutMillis;
46
47     public NetconfServerSessionNegotiatorFactory(Timer timer, NetconfOperationProvider netconfOperationProvider,
48             SessionIdProvider idProvider, long connectionTimeoutMillis) {
49         this.timer = timer;
50         this.netconfOperationProvider = netconfOperationProvider;
51         this.idProvider = idProvider;
52         this.connectionTimeoutMillis = connectionTimeoutMillis;
53     }
54
55     private static Document loadHelloMessageTemplate() {
56         InputStream resourceAsStream = NetconfServerSessionNegotiatorFactory.class
57                 .getResourceAsStream(SERVER_HELLO_XML_LOCATION);
58         Preconditions.checkNotNull(resourceAsStream, "Unable to load server hello message blueprint from %s",
59                 SERVER_HELLO_XML_LOCATION);
60         return NetconfUtil.createMessage(resourceAsStream).getDocument();
61     }
62
63     @Override
64     public SessionNegotiator<NetconfServerSession> getSessionNegotiator(SessionListenerFactory<NetconfServerSessionListener> sessionListenerFactory, Channel channel,
65             Promise<NetconfServerSession> promise) {
66         long sessionId = idProvider.getNextSessionId();
67
68         NetconfServerSessionPreferences proposal = new NetconfServerSessionPreferences(createHelloMessage(sessionId),
69                 sessionId);
70         return new NetconfServerSessionNegotiator(proposal, promise, channel, timer,
71                 sessionListenerFactory.getSessionListener(), connectionTimeoutMillis);
72     }
73
74     private static final XPathExpression sessionIdXPath = XMLNetconfUtil
75             .compileXPath("/netconf:hello/netconf:session-id");
76     private static final XPathExpression capabilitiesXPath = XMLNetconfUtil
77             .compileXPath("/netconf:hello/netconf:capabilities");
78
79     private NetconfHelloMessage createHelloMessage(long sessionId) {
80         Document helloMessageTemplate = getHelloTemplateClone();
81
82         // change session ID
83         final Node sessionIdNode = (Node) XmlUtil.evaluateXPath(sessionIdXPath, helloMessageTemplate,
84                 XPathConstants.NODE);
85         sessionIdNode.setTextContent(String.valueOf(sessionId));
86
87         // add capabilities from yang store
88         final Element capabilitiesElement = (Element) XmlUtil.evaluateXPath(capabilitiesXPath, helloMessageTemplate,
89                 XPathConstants.NODE);
90
91         CapabilityProvider capabilityProvider = new CapabilityProviderImpl(netconfOperationProvider.getSnapshot(
92                 getNetconfSessionIdForReporting(sessionId)));
93
94         for (String capability : capabilityProvider.getCapabilities()) {
95             final Element capabilityElement = helloMessageTemplate.createElement(XmlNetconfConstants.CAPABILITY);
96             capabilityElement.setTextContent(capability);
97             capabilitiesElement.appendChild(capabilityElement);
98         }
99         return new NetconfHelloMessage(helloMessageTemplate);
100     }
101
102     private synchronized Document getHelloTemplateClone() {
103         return (Document) helloMessageTemplate.cloneNode(true);
104     }
105 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.