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 com.google.common.base.Function;
12 import com.google.common.collect.Collections2;
13 import io.netty.channel.Channel;
14 import io.netty.util.Timer;
15 import io.netty.util.concurrent.Promise;
16 import org.opendaylight.controller.netconf.api.NetconfMessage;
17 import org.opendaylight.controller.netconf.api.NetconfSessionPreferences;
18 import org.opendaylight.controller.netconf.util.AbstractNetconfSessionNegotiator;
19 import org.opendaylight.controller.netconf.util.xml.XMLNetconfUtil;
20 import org.opendaylight.controller.netconf.util.xml.XmlElement;
21 import org.opendaylight.controller.netconf.util.xml.XmlNetconfConstants;
22 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
23 import org.opendaylight.protocol.framework.SessionListener;
24 import org.w3c.dom.Document;
25 import org.w3c.dom.Node;
27 import javax.annotation.Nullable;
28 import javax.xml.xpath.XPathConstants;
29 import javax.xml.xpath.XPathExpression;
30 import java.util.Collection;
31 import java.util.List;
33 public class NetconfClientSessionNegotiator extends
34 AbstractNetconfSessionNegotiator<NetconfSessionPreferences, NetconfClientSession> {
36 protected NetconfClientSessionNegotiator(NetconfSessionPreferences sessionPreferences,
37 Promise<NetconfClientSession> promise, Channel channel, Timer timer, SessionListener sessionListener,
38 long connectionTimeoutMillis) {
39 super(sessionPreferences, promise, channel, timer, sessionListener, connectionTimeoutMillis);
42 private static Collection<String> getCapabilities(Document doc) {
43 XmlElement responseElement = XmlElement.fromDomDocument(doc);
44 XmlElement capabilitiesElement = responseElement
45 .getOnlyChildElementWithSameNamespace(XmlNetconfConstants.CAPABILITIES);
46 List<XmlElement> caps = capabilitiesElement.getChildElements(XmlNetconfConstants.CAPABILITY);
47 return Collections2.transform(caps, new Function<XmlElement, String>() {
51 public String apply(@Nullable XmlElement input) {
52 // Trim possible leading/tailing whitespace
53 return input.getTextContent().trim();
58 private static final XPathExpression sessionIdXPath = XMLNetconfUtil
59 .compileXPath("/netconf:hello/netconf:session-id");
61 private long extractSessionId(Document doc) {
62 final Node sessionIdNode = (Node) XmlUtil.evaluateXPath(sessionIdXPath, doc, XPathConstants.NODE);
63 String textContent = sessionIdNode.getTextContent();
64 if (textContent == null || textContent.equals("")) {
65 throw new IllegalStateException("Session id not received from server");
68 return Long.valueOf(textContent);
72 protected NetconfClientSession getSession(SessionListener sessionListener, Channel channel, NetconfMessage message) {
73 return new NetconfClientSession(sessionListener, channel, extractSessionId(message.getDocument()),
74 getCapabilities(message.getDocument()));