BUG-1714: rework XmlDocumentUtils flow 37/10837/1
authorRobert Varga <rovarga@cisco.com>
Fri, 5 Sep 2014 06:16:32 +0000 (08:16 +0200)
committerRobert Varga <rovarga@cisco.com>
Fri, 5 Sep 2014 06:16:32 +0000 (08:16 +0200)
This reworks the flow such that we never override the result of a
codec's transformation. As a drive-by, we reuse the QName exposed in
SchemaContext to reuse the NETCONF QNameModule.

Change-Id: Id4e1c55fc403167436bbf7836b31e0de7f0d6355
Signed-off-by: Robert Varga <rovarga@cisco.com>
yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/codec/xml/XmlDocumentUtils.java
yang/yang-model-api/src/main/java/org/opendaylight/yangtools/yang/model/api/SchemaContext.java

index 68fc432e96952789b42d8164e7e25d39b347b138..d944842bf983ca534804929c2cd3b28143b2f577 100644 (file)
@@ -57,6 +57,7 @@ import org.opendaylight.yangtools.yang.model.api.SchemaContext;
 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
 import org.opendaylight.yangtools.yang.model.api.type.IdentityrefTypeDefinition;
+import org.opendaylight.yangtools.yang.model.util.InstanceIdentifierType;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.w3c.dom.Attr;
@@ -65,10 +66,6 @@ import org.w3c.dom.Element;
 import org.w3c.dom.NodeList;
 
 public class XmlDocumentUtils {
-    private final static String RPC_REPLY_LOCAL_NAME = "rpc-reply";
-    private final static String RPC_REPLY_NAMESPACE = "urn:ietf:params:xml:ns:netconf:base:1.0";
-    private final static QName RPC_REPLY_QNAME = QName.create(URI.create(RPC_REPLY_NAMESPACE), null, RPC_REPLY_LOCAL_NAME);
-
     private static class ElementWithSchemaContext {
         Element element;
         SchemaContext schemaContext;
@@ -87,8 +84,9 @@ public class XmlDocumentUtils {
         }
     }
 
-    public static final QName OPERATION_ATTRIBUTE_QNAME = QName.create(URI.create("urn:ietf:params:xml:ns:netconf:base:1.0"), null, "operation");
-    private static final Logger logger = LoggerFactory.getLogger(XmlDocumentUtils.class);
+    public static final QName OPERATION_ATTRIBUTE_QNAME = QName.create(SchemaContext.NAME, "operation");
+    private static final QName RPC_REPLY_QNAME = QName.create(SchemaContext.NAME, "rpc-reply");
+    private static final Logger LOG = LoggerFactory.getLogger(XmlDocumentUtils.class);
     private static final XMLOutputFactory FACTORY = XMLOutputFactory.newFactory();
 
     /**
@@ -119,7 +117,7 @@ public class XmlDocumentUtils {
             writer.close();
             return (Document)result.getNode();
         } catch (XMLStreamException e) {
-            logger.error("Failed to serialize data {}", data, e);
+            LOG.error("Failed to serialize data {}", data, e);
             return null;
         }
     }
@@ -154,7 +152,7 @@ public class XmlDocumentUtils {
             writer.close();
             return (Document)result.getNode();
         } catch (XMLStreamException e) {
-            logger.error("Failed to serialize data {}", data, e);
+            LOG.error("Failed to serialize data {}", data, e);
             return null;
         }
     }
@@ -230,22 +228,22 @@ public class XmlDocumentUtils {
 
     protected static Node<?> toSimpleNodeWithType(final Element xmlElement, final LeafSchemaNode schema,
             final XmlCodecProvider codecProvider,final SchemaContext schemaCtx) {
-        TypeDefinitionAwareCodec<? extends Object, ? extends TypeDefinition<?>> codec = codecProvider.codecFor(schema.getType());
-        String text = xmlElement.getTextContent();
-        Object value = null;
-        if (codec != null) {
-            value = codec.deserialize(text);
-        }
         final TypeDefinition<?> baseType = XmlUtils.resolveBaseTypeFrom(schema.getType());
+        final String text = xmlElement.getTextContent();
+        final Object value;
 
-        if (baseType instanceof org.opendaylight.yangtools.yang.model.util.InstanceIdentifierType) {
-            value = InstanceIdentifierForXmlCodec.deserialize(xmlElement,schemaCtx);
-        } else if(baseType instanceof IdentityrefTypeDefinition){
-            value = InstanceIdentifierForXmlCodec.toIdentity(xmlElement.getTextContent(), xmlElement, schemaCtx);
-        }
-
-        if (value == null) {
-            value = xmlElement.getTextContent();
+        if (baseType instanceof InstanceIdentifierType) {
+            value = InstanceIdentifierForXmlCodec.deserialize(xmlElement, schemaCtx);
+        } else if (baseType instanceof IdentityrefTypeDefinition) {
+            value = InstanceIdentifierForXmlCodec.toIdentity(text, xmlElement, schemaCtx);
+        } else {
+            final TypeDefinitionAwareCodec<?, ?> codec = codecProvider.codecFor(schema.getType());
+            if (codec == null) {
+                LOG.info("No codec for schema {}, falling back to text", schema);
+                value = text;
+            } else {
+                value = codec.deserialize(text);
+            }
         }
 
         Optional<ModifyAction> modifyAction = getModifyOperationFromAttributes(xmlElement);
@@ -254,17 +252,20 @@ public class XmlDocumentUtils {
 
     private static Node<?> toSimpleNodeWithType(final Element xmlElement, final LeafListSchemaNode schema,
             final XmlCodecProvider codecProvider,final SchemaContext schemaCtx) {
-        TypeDefinitionAwareCodec<? extends Object, ? extends TypeDefinition<?>> codec = codecProvider.codecFor(schema.getType());
-        String text = xmlElement.getTextContent();
-        Object value = null;
-        if (codec != null) {
-            value = codec.deserialize(text);
-        }
-        if (schema.getType() instanceof org.opendaylight.yangtools.yang.model.util.InstanceIdentifierType) {
-            value = InstanceIdentifierForXmlCodec.deserialize(xmlElement,schemaCtx);
-        }
-        if (value == null) {
-            value = xmlElement.getTextContent();
+        final Object value;
+
+        if (schema.getType() instanceof InstanceIdentifierType) {
+            value = InstanceIdentifierForXmlCodec.deserialize(xmlElement, schemaCtx);
+        } else {
+            final TypeDefinitionAwareCodec<?, ?> codec = codecProvider.codecFor(schema.getType());
+            final String text = xmlElement.getTextContent();
+
+            if (codec == null) {
+                LOG.info("No codec for schema {}, falling back to text", schema);
+                value = text;
+            } else {
+                value = codec.deserialize(text);
+            }
         }
 
         Optional<ModifyAction> modifyAction = getModifyOperationFromAttributes(xmlElement);
index 2bb1d993d9f12dcb2f486aa5064b0129d7177386..dec334dc7f420404deb81fed0a1bdb273c17a8b0 100644 (file)
@@ -8,12 +8,11 @@
 package org.opendaylight.yangtools.yang.model.api;
 
 import com.google.common.base.Optional;
-import javax.annotation.concurrent.Immutable;
-import org.opendaylight.yangtools.yang.common.QName;
-
 import java.net.URI;
 import java.util.Date;
 import java.util.Set;
+import javax.annotation.concurrent.Immutable;
+import org.opendaylight.yangtools.yang.common.QName;
 
 /**
  * The interface represents static view of compiled yang files,
@@ -25,7 +24,9 @@ import java.util.Set;
  */
 @Immutable
 public interface SchemaContext extends ContainerSchemaNode {
-
+    /**
+     * QName of NETCONF top-level data node.
+     */
     public static final QName NAME = QName.create(URI.create("urn:ietf:params:xml:ns:netconf:base:1.0"), null, "data");
 
     /**