9ffb8da1dd975fc6592e2b789e49e7f5492e0069
[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 io.netty.channel.Channel;
12 import io.netty.util.Timer;
13 import io.netty.util.concurrent.Promise;
14
15 import java.io.InputStream;
16
17 import javax.xml.xpath.XPathConstants;
18 import javax.xml.xpath.XPathExpression;
19
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;
36
37 import com.google.common.base.Optional;
38 import com.google.common.base.Preconditions;
39
40 import static org.opendaylight.controller.netconf.mapping.api.NetconfOperationProvider.NetconfOperationProviderUtil.getNetconfSessionIdForReporting;
41
42 public class NetconfServerSessionNegotiatorFactory implements SessionNegotiatorFactory<NetconfHelloMessage, NetconfServerSession, NetconfServerSessionListener> {
43
44     public static final String SERVER_HELLO_XML_LOCATION = "/server_hello.xml";
45
46     private final Timer timer;
47
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;
54
55     public NetconfServerSessionNegotiatorFactory(Timer timer, NetconfOperationProvider netconfOperationProvider,
56                                                  SessionIdProvider idProvider, long connectionTimeoutMillis,
57                                                  DefaultCommitNotificationProducer commitNot, SessionMonitoringService monitoringService) {
58         this.timer = timer;
59         this.netconfOperationProvider = netconfOperationProvider;
60         this.idProvider = idProvider;
61         this.connectionTimeoutMillis = connectionTimeoutMillis;
62         this.commitNotificationProducer = commitNot;
63         this.monitoringService = monitoringService;
64     }
65
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();
72     }
73
74     /**
75      *
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
82      */
83     @Override
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);
90
91         NetconfServerSessionPreferences proposal = new NetconfServerSessionPreferences(
92                 createHelloMessage(sessionId, capabilityProvider), sessionId);
93
94         NetconfServerSessionListenerFactory sessionListenerFactory = new NetconfServerSessionListenerFactory(
95                 commitNotificationProducer, monitoringService,
96                 netconfOperationServiceSnapshot, capabilityProvider);
97
98         return new NetconfServerSessionNegotiator(proposal, promise, channel, timer,
99                 sessionListenerFactory.getSessionListener(), connectionTimeoutMillis);
100     }
101
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");
106
107     private NetconfHelloMessage createHelloMessage(long sessionId, CapabilityProvider capabilityProvider) {
108         Document helloMessageTemplate = getHelloTemplateClone();
109
110         // change session ID
111         final Node sessionIdNode = (Node) XmlUtil.evaluateXPath(sessionIdXPath, helloMessageTemplate,
112                 XPathConstants.NODE);
113         sessionIdNode.setTextContent(String.valueOf(sessionId));
114
115         // add capabilities from yang store
116         final Element capabilitiesElement = (Element) XmlUtil.evaluateXPath(capabilitiesXPath, helloMessageTemplate,
117                 XPathConstants.NODE);
118
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);
123         }
124         return new NetconfHelloMessage(helloMessageTemplate);
125     }
126
127     private synchronized Document getHelloTemplateClone() {
128         return (Document) helloMessageTemplate.cloneNode(true);
129     }
130 }