BUG-731: do not catch Throwable
[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 java.util.Collection;
12 import java.util.List;
13
14 import javax.annotation.Nullable;
15 import javax.xml.xpath.XPathConstants;
16 import javax.xml.xpath.XPathExpression;
17
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;
27
28 import com.google.common.base.Function;
29 import com.google.common.collect.Collections2;
30
31 import io.netty.channel.Channel;
32 import io.netty.util.Timer;
33 import io.netty.util.concurrent.Promise;
34
35 public class NetconfClientSessionNegotiator extends
36         AbstractNetconfSessionNegotiator<NetconfSessionPreferences, NetconfClientSession, NetconfClientSessionListener> {
37
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);
42     }
43
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>() {
50
51             @Nullable
52             @Override
53             public String apply(@Nullable XmlElement input) {
54                 // Trim possible leading/tailing whitespace
55                 return input.getTextContent().trim();
56             }
57         });
58     }
59
60     private static final XPathExpression sessionIdXPath = XMLNetconfUtil
61             .compileXPath("/netconf:hello/netconf:session-id");
62
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");
68         }
69
70         return Long.valueOf(textContent);
71     }
72
73     @Override
74     protected NetconfClientSession getSession(NetconfClientSessionListener sessionListener, Channel channel, NetconfHelloMessage message) {
75         return new NetconfClientSession(sessionListener, channel, extractSessionId(message.getDocument()),
76                 getCapabilities(message.getDocument()));
77     }
78 }