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