17a55c52bcbf7b1a6dd13a9afcbaa61a382d9388
[controller.git] / opendaylight / netconf / netconf-client / src / main / java / org / opendaylight / controller / netconf / client / NetconfClientSessionNegotiator.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.client;
10
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;
25
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;
31
32 public class NetconfClientSessionNegotiator extends
33         AbstractNetconfSessionNegotiator<NetconfSessionPreferences, NetconfClientSession> {
34
35     protected NetconfClientSessionNegotiator(NetconfSessionPreferences sessionPreferences,
36             Promise<NetconfClientSession> promise, Channel channel, Timer timer, SessionListener sessionListener) {
37         super(sessionPreferences, promise, channel, timer, sessionListener);
38     }
39
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>() {
46
47             @Nullable
48             @Override
49             public String apply(@Nullable XmlElement input) {
50                 return input.getTextContent();
51             }
52         });
53     }
54
55     private static final XPathExpression sessionIdXPath = XMLNetconfUtil
56             .compileXPath("/netconf:hello/netconf:session-id");
57
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");
63         }
64
65         return Long.valueOf(textContent);
66     }
67
68     @Override
69     protected NetconfClientSession getSession(SessionListener sessionListener, Channel channel, Document doc) {
70         return new NetconfClientSession(sessionListener, channel, extractSessionId(doc), getCapabilities(doc));
71     }
72 }