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.client;
11 import java.util.Collection;
12 import java.util.List;
14 import javax.annotation.Nullable;
15 import javax.xml.xpath.XPathConstants;
16 import javax.xml.xpath.XPathExpression;
18 import org.opendaylight.controller.netconf.api.NetconfSessionPreferences;
19 import org.opendaylight.controller.netconf.util.AbstractNetconfSessionNegotiator;
20 import org.opendaylight.controller.netconf.util.messages.NetconfHelloMessage;
21 import org.opendaylight.controller.netconf.util.xml.XMLNetconfUtil;
22 import org.opendaylight.controller.netconf.util.xml.XmlElement;
23 import org.opendaylight.controller.netconf.util.xml.XmlNetconfConstants;
24 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
25 import org.w3c.dom.Document;
26 import org.w3c.dom.Node;
28 import com.google.common.base.Function;
29 import com.google.common.collect.Collections2;
31 import io.netty.channel.Channel;
32 import io.netty.util.Timer;
33 import io.netty.util.concurrent.Promise;
35 public class NetconfClientSessionNegotiator extends
36 AbstractNetconfSessionNegotiator<NetconfSessionPreferences, NetconfClientSession, NetconfClientSessionListener> {
38 protected NetconfClientSessionNegotiator(NetconfSessionPreferences sessionPreferences,
39 Promise<NetconfClientSession> promise, Channel channel, Timer timer, NetconfClientSessionListener sessionListener,
40 long connectionTimeoutMillis) {
41 super(sessionPreferences, promise, channel, timer, sessionListener, connectionTimeoutMillis);
44 private static Collection<String> getCapabilities(Document doc) {
45 XmlElement responseElement = XmlElement.fromDomDocument(doc);
46 XmlElement capabilitiesElement = responseElement
47 .getOnlyChildElementWithSameNamespace(XmlNetconfConstants.CAPABILITIES);
48 List<XmlElement> caps = capabilitiesElement.getChildElements(XmlNetconfConstants.CAPABILITY);
49 return Collections2.transform(caps, new Function<XmlElement, String>() {
53 public String apply(@Nullable XmlElement input) {
54 // Trim possible leading/tailing whitespace
55 return input.getTextContent().trim();
60 private static final XPathExpression sessionIdXPath = XMLNetconfUtil
61 .compileXPath("/netconf:hello/netconf:session-id");
63 private long extractSessionId(Document doc) {
64 final Node sessionIdNode = (Node) XmlUtil.evaluateXPath(sessionIdXPath, doc, XPathConstants.NODE);
65 String textContent = sessionIdNode.getTextContent();
66 if (textContent == null || textContent.equals("")) {
67 throw new IllegalStateException("Session id not received from server");
70 return Long.valueOf(textContent);
74 protected NetconfClientSession getSession(NetconfClientSessionListener sessionListener, Channel channel, NetconfHelloMessage message) {
75 return new NetconfClientSession(sessionListener, channel, extractSessionId(message.getDocument()),
76 getCapabilities(message.getDocument()));