Decouple config and netconf subsystems.
[controller.git] / opendaylight / netconf / netconf-util / src / main / java / org / opendaylight / controller / netconf / util / messages / NetconfMessageUtil.java
index c532b7f9a6f235a0eebc7dc4a53bc10f2208dbc4..354d74016d48f170321bfca9e9d5886a5a03b851 100644 (file)
@@ -9,21 +9,23 @@
 package org.opendaylight.controller.netconf.util.messages;
 
 import com.google.common.base.Function;
+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 org.opendaylight.controller.config.util.xml.DocumentedException;
+import org.opendaylight.controller.config.util.xml.XmlElement;
 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
 import org.opendaylight.controller.netconf.api.NetconfMessage;
 import org.opendaylight.controller.netconf.api.xml.XmlNetconfConstants;
-import org.opendaylight.controller.netconf.util.xml.XmlElement;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.w3c.dom.Document;
 
 public final class NetconfMessageUtil {
 
-    private static final Logger logger = LoggerFactory.getLogger(NetconfMessageUtil.class);
+    private static final Logger LOG = LoggerFactory.getLogger(NetconfMessageUtil.class);
 
     private NetconfMessageUtil() {}
 
@@ -39,7 +41,11 @@ public final class NetconfMessageUtil {
         if(xmlElement.getChildElements().size() != 1) {
             return false;
         }
-        return xmlElement.getOnlyChildElement().getName().equals(XmlNetconfConstants.OK);
+        try {
+            return xmlElement.getOnlyChildElement().getName().equals(XmlNetconfConstants.OK);
+        } catch (DocumentedException e) {
+            throw new NetconfDocumentedException(e);
+        }
     }
 
     public static boolean isErrorMessage(NetconfMessage message) throws NetconfDocumentedException {
@@ -54,14 +60,22 @@ public final class NetconfMessageUtil {
         if(xmlElement.getChildElements().size() != 1) {
             return false;
         }
-        return xmlElement.getOnlyChildElement().getName().equals(XmlNetconfConstants.RPC_ERROR);
+        try {
+            return xmlElement.getOnlyChildElement().getName().equals(DocumentedException.RPC_ERROR);
+        } catch (DocumentedException e) {
+            throw new NetconfDocumentedException(e);
+        }
     }
 
     public static Collection<String> extractCapabilitiesFromHello(Document doc) throws NetconfDocumentedException {
         XmlElement responseElement = XmlElement.fromDomDocument(doc);
-        XmlElement capabilitiesElement = responseElement
-                .getOnlyChildElementWithSameNamespace(XmlNetconfConstants.CAPABILITIES);
-        List<XmlElement> caps = capabilitiesElement.getChildElements(XmlNetconfConstants.CAPABILITY);
+        // 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));
+
+        List<XmlElement> caps = capabilitiesElement.get().getChildElements(XmlNetconfConstants.CAPABILITY);
         return Collections2.transform(caps, new Function<XmlElement, String>() {
 
             @Override
@@ -69,8 +83,8 @@ public final class NetconfMessageUtil {
                 // Trim possible leading/tailing whitespace
                 try {
                     return input.getTextContent().trim();
-                } catch (NetconfDocumentedException e) {
-                    logger.trace("Error fetching inpit text content becauese {}",e);
+                } catch (DocumentedException e) {
+                    LOG.trace("Error fetching input text content",e);
                     return null;
                 }
             }