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 super(sessionPreferences, promise, channel, timer, sessionListener);
41 private static Collection<String> getCapabilities(Document doc) {
42 XmlElement responseElement = XmlElement.fromDomDocument(doc);
43 XmlElement capabilitiesElement = responseElement
44 .getOnlyChildElementWithSameNamespace(XmlNetconfConstants.CAPABILITIES);
45 List<XmlElement> caps = capabilitiesElement.getChildElements(XmlNetconfConstants.CAPABILITY);
46 return Collections2.transform(caps, new Function<XmlElement, String>() {
50 public String apply(@Nullable XmlElement input) {
51 return input.getTextContent();
56 private static final XPathExpression sessionIdXPath = XMLNetconfUtil
57 .compileXPath("/netconf:hello/netconf:session-id");
59 private long extractSessionId(Document doc) {
60 final Node sessionIdNode = (Node) XmlUtil.evaluateXPath(sessionIdXPath, doc, XPathConstants.NODE);
61 String textContent = sessionIdNode.getTextContent();
62 if (textContent == null || textContent.equals("")) {
63 throw new IllegalStateException("Session id not received from server");
66 return Long.valueOf(textContent);
70 protected NetconfClientSession getSession(SessionListener sessionListener, Channel channel, NetconfMessage message) {
71 return new NetconfClientSession(sessionListener, channel, extractSessionId(message.getDocument()),
72 getCapabilities(message.getDocument()));