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.NetconfSessionPreferences;
17 import org.opendaylight.controller.netconf.util.AbstractNetconfSessionNegotiator;
18 import org.opendaylight.controller.netconf.util.xml.XMLNetconfUtil;
19 import org.opendaylight.controller.netconf.util.xml.XmlElement;
20 import org.opendaylight.controller.netconf.util.xml.XmlNetconfConstants;
21 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
22 import org.opendaylight.protocol.framework.SessionListener;
23 import org.w3c.dom.Document;
24 import org.w3c.dom.Node;
26 import javax.annotation.Nullable;
27 import javax.xml.xpath.XPathConstants;
28 import javax.xml.xpath.XPathExpression;
29 import java.util.Collection;
30 import java.util.List;
32 public class NetconfClientSessionNegotiator extends
33 AbstractNetconfSessionNegotiator<NetconfSessionPreferences, NetconfClientSession> {
35 protected NetconfClientSessionNegotiator(NetconfSessionPreferences sessionPreferences,
36 Promise<NetconfClientSession> promise, Channel channel, Timer timer, SessionListener sessionListener) {
37 super(sessionPreferences, promise, channel, timer, sessionListener);
40 private static Collection<String> getCapabilities(Document doc) {
41 XmlElement responseElement = XmlElement.fromDomDocument(doc);
42 XmlElement capabilitiesElement = responseElement
43 .getOnlyChildElementWithSameNamespace(XmlNetconfConstants.CAPABILITIES);
44 List<XmlElement> caps = capabilitiesElement.getChildElements(XmlNetconfConstants.CAPABILITY);
45 return Collections2.transform(caps, new Function<XmlElement, String>() {
49 public String apply(@Nullable XmlElement input) {
50 return input.getTextContent();
55 private static final XPathExpression sessionIdXPath = XMLNetconfUtil
56 .compileXPath("/netconf:hello/netconf:session-id");
58 private long extractSessionId(Document doc) {
59 final Node sessionIdNode = (Node) XmlUtil.evaluateXPath(sessionIdXPath, doc, XPathConstants.NODE);
60 String textContent = sessionIdNode.getTextContent();
61 if (textContent == null || textContent.equals("")) {
62 throw new IllegalStateException("Session id not received from server");
65 return Long.valueOf(textContent);
69 protected NetconfClientSession getSession(SessionListener sessionListener, Channel channel, Document doc) {
70 return new NetconfClientSession(sessionListener, channel, extractSessionId(doc), getCapabilities(doc));