Bug 591: Minimize the use of Xtend in sal-binding-broker-impl
[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.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;
29
30 import javax.xml.xpath.XPathConstants;
31 import javax.xml.xpath.XPathExpression;
32 import java.io.InputStream;
33
34 public class NetconfServerSessionNegotiatorFactory implements SessionNegotiatorFactory<NetconfHelloMessage, NetconfServerSession, NetconfServerSessionListener> {
35
36     public static final String SERVER_HELLO_XML_LOCATION = "/server_hello.xml";
37
38     private final Timer timer;
39
40     private static final Document helloMessageTemplate = loadHelloMessageTemplate();
41     private final SessionIdProvider idProvider;
42     private final NetconfOperationServiceFactoryListener factoriesListener;
43     private final long connectionTimeoutMillis;
44
45     public NetconfServerSessionNegotiatorFactory(Timer timer, NetconfOperationServiceFactoryListener factoriesListener,
46             SessionIdProvider idProvider, long connectionTimeoutMillis) {
47         this.timer = timer;
48         this.factoriesListener = factoriesListener;
49         this.idProvider = idProvider;
50         this.connectionTimeoutMillis = connectionTimeoutMillis;
51     }
52
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();
59     }
60
61     @Override
62     public SessionNegotiator<NetconfServerSession> getSessionNegotiator(SessionListenerFactory<NetconfServerSessionListener> sessionListenerFactory, Channel channel,
63             Promise<NetconfServerSession> promise) {
64         long sessionId = idProvider.getNextSessionId();
65
66         NetconfServerSessionPreferences proposal = new NetconfServerSessionPreferences(createHelloMessage(sessionId),
67                 sessionId);
68         return new NetconfServerSessionNegotiator(proposal, promise, channel, timer,
69                 sessionListenerFactory.getSessionListener(), connectionTimeoutMillis);
70     }
71
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");
76
77     private NetconfHelloMessage createHelloMessage(long sessionId) {
78         Document helloMessageTemplate = getHelloTemplateClone();
79
80         // change session ID
81         final Node sessionIdNode = (Node) XmlUtil.evaluateXPath(sessionIdXPath, helloMessageTemplate,
82                 XPathConstants.NODE);
83         sessionIdNode.setTextContent(String.valueOf(sessionId));
84
85         // add capabilities from yang store
86         final Element capabilitiesElement = (Element) XmlUtil.evaluateXPath(capabilitiesXPath, helloMessageTemplate,
87                 XPathConstants.NODE);
88
89         CapabilityProvider capabilityProvider = new CapabilityProviderImpl(factoriesListener.getSnapshot(sessionId));
90
91         for (String capability : capabilityProvider.getCapabilities()) {
92             final Element capabilityElement = helloMessageTemplate.createElement(XmlNetconfConstants.CAPABILITY);
93             capabilityElement.setTextContent(capability);
94             capabilitiesElement.appendChild(capabilityElement);
95         }
96         return new NetconfHelloMessage(helloMessageTemplate);
97     }
98
99     private synchronized Document getHelloTemplateClone() {
100         return (Document) helloMessageTemplate.cloneNode(true);
101     }
102 }

©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.