a0c2db36c4f634d1a630a6819cb4ceafac8a3cd6
[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 javax.annotation.Nonnull;
15 import org.opendaylight.netconf.api.DocumentedException;
16 import org.opendaylight.netconf.api.NetconfDocumentedException;
17 import org.opendaylight.netconf.api.NetconfMessage;
18 import org.opendaylight.netconf.api.xml.XmlElement;
19 import org.opendaylight.netconf.api.xml.XmlNetconfConstants;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22 import org.w3c.dom.Document;
23
24 public final class NetconfMessageUtil {
25
26     private static final Logger LOG = LoggerFactory.getLogger(NetconfMessageUtil.class);
27
28     private NetconfMessageUtil() {}
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         if (xmlElement.getChildElements().size() != 1) {
59             return false;
60         }
61         try {
62             return xmlElement.getOnlyChildElement().getName().equals(DocumentedException.RPC_ERROR);
63         } catch (DocumentedException e) {
64             throw new NetconfDocumentedException(e);
65         }
66     }
67
68     public static Collection<String> extractCapabilitiesFromHello(final Document doc) {
69         XmlElement responseElement = XmlElement.fromDomDocument(doc);
70         // Extract child element <capabilities> from <hello> with or without(fallback) the same namespace
71         Optional<XmlElement> capabilitiesElement = responseElement
72                 .getOnlyChildElementWithSameNamespaceOptionally(XmlNetconfConstants.CAPABILITIES);
73         if (!capabilitiesElement.isPresent()) {
74             capabilitiesElement = responseElement.getOnlyChildElementOptionally(XmlNetconfConstants.CAPABILITIES);
75         }
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 }