Remove unused exceptions
[netconf.git] / netconf / netconf-util / src / main / java / org / opendaylight / netconf / util / messages / NetconfMessageUtil.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.netconf.util.messages;
10
11 import com.google.common.base.Optional;
12 import com.google.common.collect.Collections2;
13 import java.util.Collection;
14 import java.util.List;
15 import javax.annotation.Nonnull;
16 import org.opendaylight.netconf.api.DocumentedException;
17 import org.opendaylight.netconf.api.NetconfDocumentedException;
18 import org.opendaylight.netconf.api.NetconfMessage;
19 import org.opendaylight.netconf.api.xml.XmlElement;
20 import org.opendaylight.netconf.api.xml.XmlNetconfConstants;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23 import org.w3c.dom.Document;
24
25 public final class NetconfMessageUtil {
26
27     private static final Logger LOG = LoggerFactory.getLogger(NetconfMessageUtil.class);
28
29     private NetconfMessageUtil() {}
30
31     public static boolean isOKMessage(final NetconfMessage message) throws NetconfDocumentedException {
32         return isOKMessage(message.getDocument());
33     }
34
35     public static boolean isOKMessage(final Document document) throws NetconfDocumentedException {
36         return isOKMessage(XmlElement.fromDomDocument(document));
37     }
38
39     public static boolean isOKMessage(final XmlElement xmlElement) throws NetconfDocumentedException {
40         if (xmlElement.getChildElements().size() != 1) {
41             return false;
42         }
43         try {
44             return xmlElement.getOnlyChildElement().getName().equals(XmlNetconfConstants.OK);
45         } catch (DocumentedException e) {
46             throw new NetconfDocumentedException(e);
47         }
48     }
49
50     public static boolean isErrorMessage(final NetconfMessage message) throws NetconfDocumentedException {
51         return isErrorMessage(message.getDocument());
52     }
53
54     public static boolean isErrorMessage(final Document document) throws NetconfDocumentedException {
55         return isErrorMessage(XmlElement.fromDomDocument(document));
56     }
57
58     public static boolean isErrorMessage(final XmlElement xmlElement) throws NetconfDocumentedException {
59         if (xmlElement.getChildElements().size() != 1) {
60             return false;
61         }
62         try {
63             return xmlElement.getOnlyChildElement().getName().equals(DocumentedException.RPC_ERROR);
64         } catch (DocumentedException e) {
65             throw new NetconfDocumentedException(e);
66         }
67     }
68
69     public static Collection<String> extractCapabilitiesFromHello(final Document doc) {
70         XmlElement responseElement = XmlElement.fromDomDocument(doc);
71         // Extract child element <capabilities> from <hello> with or without(fallback) the same namespace
72         Optional<XmlElement> capabilitiesElement = responseElement
73                 .getOnlyChildElementWithSameNamespaceOptionally(XmlNetconfConstants.CAPABILITIES)
74                 .or(responseElement
75                         .getOnlyChildElementOptionally(XmlNetconfConstants.CAPABILITIES));
76
77         List<XmlElement> caps = capabilitiesElement.get().getChildElements(XmlNetconfConstants.CAPABILITY);
78         return Collections2.transform(caps, (@Nonnull final XmlElement input) -> {
79             // Trim possible leading/tailing whitespace
80             try {
81                 return input.getTextContent().trim();
82             } catch (DocumentedException e) {
83                 LOG.trace("Error fetching input text content",e);
84                 return null;
85             }
86         });
87
88     }
89 }