Special-case schema-mount nodes
[yangtools.git] / yang / yang-data-codec-xml / src / main / java / org / opendaylight / yangtools / yang / data / codec / xml / XmlParserStream.java
index c952a968b4f8883c0ebd7043f28fa4023ff0fb93..a4ea945f6a422a7354d04a4c022c28155a638c26 100644 (file)
@@ -44,11 +44,17 @@ import javax.xml.transform.dom.DOMSource;
 import javax.xml.transform.stax.StAXSource;
 import org.opendaylight.yangtools.odlext.model.api.YangModeledAnyXmlSchemaNode;
 import org.opendaylight.yangtools.rfc7952.model.api.AnnotationSchemaNode;
+import org.opendaylight.yangtools.rfc8528.data.api.YangLibraryConstants;
+import org.opendaylight.yangtools.rfc8528.data.api.YangLibraryConstants.ContainerName;
+import org.opendaylight.yangtools.rfc8528.model.api.MountPointSchemaNode;
+import org.opendaylight.yangtools.rfc8528.model.api.SchemaMountConstants;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.common.QNameModule;
 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
+import org.opendaylight.yangtools.yang.data.util.AbstractMountPointDataWithSchema;
 import org.opendaylight.yangtools.yang.data.util.AbstractNodeDataWithSchema;
 import org.opendaylight.yangtools.yang.data.util.AnyXmlNodeDataWithSchema;
+import org.opendaylight.yangtools.yang.data.util.AnydataNodeDataWithSchema;
 import org.opendaylight.yangtools.yang.data.util.CompositeNodeDataWithSchema;
 import org.opendaylight.yangtools.yang.data.util.ContainerNodeDataWithSchema;
 import org.opendaylight.yangtools.yang.data.util.LeafListEntryNodeDataWithSchema;
@@ -56,10 +62,12 @@ import org.opendaylight.yangtools.yang.data.util.LeafListNodeDataWithSchema;
 import org.opendaylight.yangtools.yang.data.util.LeafNodeDataWithSchema;
 import org.opendaylight.yangtools.yang.data.util.ListEntryNodeDataWithSchema;
 import org.opendaylight.yangtools.yang.data.util.ListNodeDataWithSchema;
+import org.opendaylight.yangtools.yang.data.util.MountPointData;
 import org.opendaylight.yangtools.yang.data.util.OperationAsContainer;
 import org.opendaylight.yangtools.yang.data.util.ParserStreamUtils;
 import org.opendaylight.yangtools.yang.data.util.SimpleNodeDataWithSchema;
 import org.opendaylight.yangtools.yang.data.util.YangModeledAnyXmlNodeDataWithSchema;
+import org.opendaylight.yangtools.yang.model.api.AnyDataSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.AnyXmlSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
@@ -230,6 +238,8 @@ public final class XmlParserStream implements Closeable, Flushable {
                 nodeDataWithSchema = new LeafNodeDataWithSchema((LeafSchemaNode) parentNode);
             } else if (parentNode instanceof LeafListSchemaNode) {
                 nodeDataWithSchema = new LeafListNodeDataWithSchema((LeafListSchemaNode) parentNode);
+            } else if (parentNode instanceof AnyDataSchemaNode) {
+                nodeDataWithSchema = new AnydataNodeDataWithSchema((AnyDataSchemaNode) parentNode);
             } else {
                 throw new IllegalStateException("Unsupported schema node type " + parentNode.getClass() + ".");
             }
@@ -339,7 +349,7 @@ public final class XmlParserStream implements Closeable, Flushable {
 
         if (parent instanceof LeafNodeDataWithSchema || parent instanceof LeafListEntryNodeDataWithSchema) {
             parent.setAttributes(getElementAttributes(in));
-            setValue(parent, in.getElementText().trim(), in.getNamespaceContext());
+            setValue((SimpleNodeDataWithSchema<?>) parent, in.getElementText().trim(), in.getNamespaceContext());
             if (isNextEndDocument(in)) {
                 return;
             }
@@ -369,7 +379,23 @@ public final class XmlParserStream implements Closeable, Flushable {
         }
 
         if (parent instanceof AnyXmlNodeDataWithSchema) {
-            setValue(parent, readAnyXmlValue(in), in.getNamespaceContext());
+            setValue((AnyXmlNodeDataWithSchema) parent, readAnyXmlValue(in), in.getNamespaceContext());
+            if (isNextEndDocument(in)) {
+                return;
+            }
+
+            if (!isAtElement(in)) {
+                in.nextTag();
+            }
+
+            return;
+        }
+
+        if (parent instanceof AnydataNodeDataWithSchema) {
+            final AnydataNodeDataWithSchema anydata = (AnydataNodeDataWithSchema) parent;
+            anydata.setObjectModel(DOMSourceAnydata.class);
+            anydata.setAttributes(getElementAttributes(in));
+            setValue(anydata, readAnyXmlValue(in), in.getNamespaceContext());
             if (isNextEndDocument(in)) {
                 return;
             }
@@ -432,21 +458,46 @@ public final class XmlParserStream implements Closeable, Flushable {
 
                     final Deque<DataSchemaNode> childDataSchemaNodes =
                             ParserStreamUtils.findSchemaNodeByNameAndNamespace(parentSchema, xmlElementName, nsUri);
+                    if (!childDataSchemaNodes.isEmpty()) {
+                        // We have a match, proceed with it
+                        read(in, ((CompositeNodeDataWithSchema<?>) parent).addChild(childDataSchemaNodes), rootElement);
+                        continue;
+                    }
+
+                    if (parent instanceof AbstractMountPointDataWithSchema) {
+                        // Parent can potentially hold a mount point, let's see if there is a label present
+                        final Optional<MountPointSchemaNode> optMount;
+                        if (parentSchema instanceof ContainerSchemaNode) {
+                            optMount = MountPointSchemaNode.streamAll((ContainerSchemaNode) parentSchema).findFirst();
+                        } else if (parentSchema instanceof ListSchemaNode) {
+                            optMount = MountPointSchemaNode.streamAll((ListSchemaNode) parentSchema).findFirst();
+                        } else {
+                            throw new XMLStreamException("Unhandled mount-aware schema " + parentSchema);
+                        }
 
-                    if (childDataSchemaNodes.isEmpty()) {
-                        if (!strictParsing) {
-                            LOG.debug("Skipping unknown node ns=\"{}\" localName=\"{}\" at path {}", elementNS,
-                                xmlElementName, parentSchema.getPath());
-                            skipUnknownNode(in);
+                        if (optMount.isPresent()) {
+                            final QName label = optMount.get().getQName();
+                            LOG.debug("Assuming node {} and namespace {} belongs to mount point {}", xmlElementName,
+                                nsUri, label);
+
+                            final MountPointData mountData =
+                                    ((AbstractMountPointDataWithSchema<?>) parent).getMountPointData(label);
+                            addMountPointChild(mountData, nsUri, xmlElementName,
+                                new DOMSource(readAnyXmlValue(in).getDocumentElement()));
                             continue;
                         }
+                    }
 
+                    // We have not handled the node -- let's decide what to do about that
+                    if (strictParsing) {
                         throw new XMLStreamException(String.format(
                             "Schema for node with name %s and namespace %s does not exist at %s", xmlElementName,
                             elementNS, parentSchema.getPath(), in.getLocation()));
                     }
 
-                    read(in, ((CompositeNodeDataWithSchema<?>) parent).addChild(childDataSchemaNodes), rootElement);
+                    LOG.debug("Skipping unknown node ns=\"{}\" localName=\"{}\" at path {}", elementNS, xmlElementName,
+                        parentSchema.getPath());
+                    skipUnknownNode(in);
                 }
                 break;
             case XMLStreamConstants.END_ELEMENT:
@@ -463,6 +514,25 @@ public final class XmlParserStream implements Closeable, Flushable {
         }
     }
 
+    private static void addMountPointChild(final MountPointData mount, final URI namespace, final String localName,
+            final DOMSource source) {
+        final DOMSourceMountPointChild child = new DOMSourceMountPointChild(source);
+        if (YangLibraryConstants.MODULE_NAMESPACE.equals(namespace)) {
+            final Optional<ContainerName> optName = ContainerName.forLocalName(localName);
+            if (optName.isPresent()) {
+                mount.setContainer(optName.get(), child);
+                return;
+            }
+
+            LOG.warn("Encountered unknown element {} from YANG Library namespace", localName);
+        } else if (SchemaMountConstants.RFC8528_MODULE.getNamespace().equals(namespace)) {
+            mount.setSchemaMounts(child);
+            return;
+        }
+
+        mount.addChild(child);
+    }
+
     private static boolean isNextEndDocument(final XMLStreamReader in) throws XMLStreamException {
         return !in.hasNext() || in.next() == XMLStreamConstants.END_DOCUMENT;
     }
@@ -500,29 +570,28 @@ public final class XmlParserStream implements Closeable, Flushable {
         in.nextTag();
     }
 
-    private void setValue(final AbstractNodeDataWithSchema<?> parent, final Object value,
+    private void setValue(final SimpleNodeDataWithSchema<?> parent, final Object value,
             final NamespaceContext nsContext) {
-        checkArgument(parent instanceof SimpleNodeDataWithSchema, "Node %s is not a simple type",
-                parent.getSchema().getQName());
-        final SimpleNodeDataWithSchema<?> parentSimpleNode = (SimpleNodeDataWithSchema<?>) parent;
-        checkArgument(parentSimpleNode.getValue() == null, "Node '%s' has already set its value to '%s'",
-                parentSimpleNode.getSchema().getQName(), parentSimpleNode.getValue());
-
-        parentSimpleNode.setValue(translateValueByType(value, parentSimpleNode.getSchema(), nsContext));
+        final DataSchemaNode schema = parent.getSchema();
+        final Object prev = parent.getValue();
+        checkArgument(prev == null, "Node '%s' has already set its value to '%s'", schema.getQName(), prev);
+        parent.setValue(translateValueByType(value, schema, nsContext));
     }
 
     private Object translateValueByType(final Object value, final DataSchemaNode node,
             final NamespaceContext namespaceCtx) {
         if (node instanceof AnyXmlSchemaNode) {
-
             checkArgument(value instanceof Document);
             /*
-             *  FIXME: Figure out some YANG extension dispatch, which will
-             *  reuse JSON parsing or XML parsing - anyxml is not well-defined in
-             * JSON.
+             * FIXME: Figure out some YANG extension dispatch, which will reuse JSON parsing or XML parsing -
+             *        anyxml is not well-defined in JSON.
              */
             return new DOMSource(((Document) value).getDocumentElement());
         }
+        if (node instanceof AnyDataSchemaNode) {
+            checkArgument(value instanceof Document);
+            return new DOMSourceAnydata(new DOMSource(((Document) value).getDocumentElement()));
+        }
 
         checkArgument(node instanceof TypedDataSchemaNode);
         checkArgument(value instanceof String);