Merge "Bug-915: Adding static document generation. Currently the API Explorer can...
[controller.git] / opendaylight / netconf / netconf-impl / src / main / java / org / opendaylight / controller / netconf / impl / mapping / operations / DefaultGetSchema.java
index 7134f5c580b601aee088fe2e2a510f83f4afcd02..53107048760dbee1ae27f23be7977b15481ba39a 100644 (file)
@@ -10,26 +10,25 @@ package org.opendaylight.controller.netconf.impl.mapping.operations;
 
 import com.google.common.base.Optional;
 import com.google.common.collect.Maps;
+import java.util.Map;
 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
+import org.opendaylight.controller.netconf.api.xml.XmlNetconfConstants;
 import org.opendaylight.controller.netconf.impl.mapping.CapabilityProvider;
+import org.opendaylight.controller.netconf.util.exception.MissingNameSpaceException;
 import org.opendaylight.controller.netconf.util.mapping.AbstractLastNetconfOperation;
 import org.opendaylight.controller.netconf.util.xml.XmlElement;
-import org.opendaylight.controller.netconf.util.xml.XmlNetconfConstants;
 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.w3c.dom.Document;
 import org.w3c.dom.Element;
 
-import java.util.HashMap;
-import java.util.Map;
-
 public final class DefaultGetSchema extends AbstractLastNetconfOperation {
     public static final String GET_SCHEMA = "get-schema";
     public static final String IDENTIFIER = "identifier";
     public static final String VERSION = "version";
 
-    private static final Logger logger = LoggerFactory.getLogger(DefaultGetSchema.class);
+    private static final Logger LOG = LoggerFactory.getLogger(DefaultGetSchema.class);
     private final CapabilityProvider cap;
 
     public DefaultGetSchema(CapabilityProvider cap, String netconfSessionIdForReporting) {
@@ -51,23 +50,7 @@ public final class DefaultGetSchema extends AbstractLastNetconfOperation {
     protected Element handleWithNoSubsequentOperations(Document document, XmlElement xml) throws NetconfDocumentedException {
         GetSchemaEntry entry;
 
-        try {
-            entry = new GetSchemaEntry(xml);
-        } catch (final IllegalArgumentException e) {
-            logger.warn("Error parsing xml", e);
-            final Map<String, String> errorInfo = new HashMap<>();
-            errorInfo.put(NetconfDocumentedException.ErrorTag.bad_attribute.name(), e.getMessage());
-            throw new NetconfDocumentedException(e.getMessage(), e, NetconfDocumentedException.ErrorType.rpc,
-                    NetconfDocumentedException.ErrorTag.bad_attribute, NetconfDocumentedException.ErrorSeverity.error,
-                    errorInfo);
-        } catch (final IllegalStateException e) {
-            logger.warn("Error parsing xml", e);
-            final Map<String, String> errorInfo = new HashMap<>();
-            errorInfo.put(NetconfDocumentedException.ErrorTag.bad_attribute.name(), e.getMessage());
-            throw new NetconfDocumentedException(e.getMessage(), e, NetconfDocumentedException.ErrorType.rpc,
-                    NetconfDocumentedException.ErrorTag.bad_attribute, NetconfDocumentedException.ErrorSeverity.error,
-                    errorInfo);
-        }
+        entry = new GetSchemaEntry(xml);
 
         String schema;
         try {
@@ -75,18 +58,16 @@ public final class DefaultGetSchema extends AbstractLastNetconfOperation {
         } catch (IllegalStateException e) {
             Map<String, String> errorInfo = Maps.newHashMap();
             errorInfo.put(entry.identifier, e.getMessage());
-            logger.warn("Rpc error: {}", NetconfDocumentedException.ErrorTag.operation_failed, e);
+            LOG.warn("Rpc error: {}", NetconfDocumentedException.ErrorTag.operation_failed, e);
             throw new NetconfDocumentedException(e.getMessage(), NetconfDocumentedException.ErrorType.application,
                     NetconfDocumentedException.ErrorTag.operation_failed,
                     NetconfDocumentedException.ErrorSeverity.error, errorInfo);
         }
 
         Element getSchemaResult;
-        getSchemaResult = XmlUtil.createTextElement(document, XmlNetconfConstants.DATA_KEY, schema);
-        XmlUtil.addNamespaceAttr(getSchemaResult,
-                XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_YANG_IETF_NETCONF_MONITORING);
-
-        logger.trace("{} operation successful", GET_SCHEMA);
+        getSchemaResult = XmlUtil.createTextElement(document, XmlNetconfConstants.DATA_KEY, schema,
+                Optional.of(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_YANG_IETF_NETCONF_MONITORING));
+        LOG.trace("{} operation successful", GET_SCHEMA);
 
         return getSchemaResult;
     }
@@ -95,11 +76,17 @@ public final class DefaultGetSchema extends AbstractLastNetconfOperation {
         private final String identifier;
         private final Optional<String> version;
 
-        GetSchemaEntry(XmlElement getSchemaElement) {
+        GetSchemaEntry(XmlElement getSchemaElement) throws NetconfDocumentedException {
             getSchemaElement.checkName(GET_SCHEMA);
             getSchemaElement.checkNamespace(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_YANG_IETF_NETCONF_MONITORING);
 
-            XmlElement identifierElement = getSchemaElement.getOnlyChildElementWithSameNamespace(IDENTIFIER);
+            XmlElement identifierElement = null;
+            try {
+                identifierElement = getSchemaElement.getOnlyChildElementWithSameNamespace(IDENTIFIER);
+            } catch (MissingNameSpaceException e) {
+                LOG.trace("Can't get identifier element as only child element with same namespace due to ",e);
+                throw NetconfDocumentedException.wrap(e);
+            }
             identifier = identifierElement.getTextContent();
             Optional<XmlElement> versionElement = getSchemaElement
                     .getOnlyChildElementWithSameNamespaceOptionally(VERSION);