Remove NetconfMessageConstants
[netconf.git] / netconf / netconf-util / src / main / java / org / opendaylight / netconf / util / messages / NetconfMessageUtil.java
index a3f37d6c92e7cde366e881cf8476ea73fb26d4f7..d61b3c6d274fb625ebdf924b336f37f64ae84c64 100644 (file)
@@ -5,14 +5,12 @@
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
  * and is available at http://www.eclipse.org/legal/epl-v10.html
  */
-
 package org.opendaylight.netconf.util.messages;
 
-import com.google.common.base.Optional;
 import com.google.common.collect.Collections2;
 import java.util.Collection;
 import java.util.List;
-import javax.annotation.Nonnull;
+import java.util.Optional;
 import org.opendaylight.netconf.api.DocumentedException;
 import org.opendaylight.netconf.api.NetconfDocumentedException;
 import org.opendaylight.netconf.api.NetconfMessage;
@@ -23,10 +21,11 @@ import org.slf4j.LoggerFactory;
 import org.w3c.dom.Document;
 
 public final class NetconfMessageUtil {
-
     private static final Logger LOG = LoggerFactory.getLogger(NetconfMessageUtil.class);
 
-    private NetconfMessageUtil() {}
+    private NetconfMessageUtil() {
+
+    }
 
     public static boolean isOKMessage(final NetconfMessage message) throws NetconfDocumentedException {
         return isOKMessage(message.getDocument());
@@ -56,7 +55,15 @@ public final class NetconfMessageUtil {
     }
 
     public static boolean isErrorMessage(final XmlElement xmlElement) throws NetconfDocumentedException {
+
+        // In the case of multiple rpc-error messages, size will not be 1 but we still want to report as Error
         if (xmlElement.getChildElements().size() != 1) {
+            List<XmlElement> allResults = xmlElement.getChildElements();
+            for (XmlElement result : allResults) {
+                if (result.getName().equals(DocumentedException.RPC_ERROR)) {
+                    return true;
+                }
+            }
             return false;
         }
         try {
@@ -66,17 +73,17 @@ public final class NetconfMessageUtil {
         }
     }
 
-    public static Collection<String> extractCapabilitiesFromHello(final Document doc)
-            throws NetconfDocumentedException {
+    public static Collection<String> extractCapabilitiesFromHello(final Document doc) {
         XmlElement responseElement = XmlElement.fromDomDocument(doc);
         // Extract child element <capabilities> from <hello> with or without(fallback) the same namespace
         Optional<XmlElement> capabilitiesElement = responseElement
-                .getOnlyChildElementWithSameNamespaceOptionally(XmlNetconfConstants.CAPABILITIES)
-                .or(responseElement
-                        .getOnlyChildElementOptionally(XmlNetconfConstants.CAPABILITIES));
+                .getOnlyChildElementWithSameNamespaceOptionally(XmlNetconfConstants.CAPABILITIES);
+        if (capabilitiesElement.isEmpty()) {
+            capabilitiesElement = responseElement.getOnlyChildElementOptionally(XmlNetconfConstants.CAPABILITIES);
+        }
 
         List<XmlElement> caps = capabilitiesElement.get().getChildElements(XmlNetconfConstants.CAPABILITY);
-        return Collections2.transform(caps, (@Nonnull final XmlElement input) -> {
+        return Collections2.transform(caps, input -> {
             // Trim possible leading/tailing whitespace
             try {
                 return input.getTextContent().trim();