Add convenience methods for XML codec
[yangtools.git] / data / yang-data-util / src / main / java / org / opendaylight / yangtools / yang / data / util / NormalizedNodeStreamWriterStack.java
index 19b4a5564b25c6600abfc490a779b8d7ccca813b..9e5a27bff0ae117ee05f834b3ab9e36c87b3d98a 100644 (file)
@@ -22,6 +22,7 @@ import java.util.Set;
 import java.util.stream.Collectors;
 import org.eclipse.jdt.annotation.NonNull;
 import org.opendaylight.yangtools.yang.common.QName;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue;
@@ -136,6 +137,23 @@ public final class NormalizedNodeStreamWriterStack implements LeafrefResolver {
         return new NormalizedNodeStreamWriterStack(SchemaInferenceStack.of(context, path));
     }
 
+    /**
+     * Create a new writer with the specified context and rooted in the specified {@link YangInstanceIdentifier}..
+     *
+     * @param context Associated {@link EffectiveModelContext}
+     * @param path Normalized path
+     * @return A new {@link NormalizedNodeStreamWriterStack}
+     * @throws NullPointerException if any argument is null
+     * @throws IllegalArgumentException if {@code path} does not point to a valid root
+     */
+    public static @NonNull NormalizedNodeStreamWriterStack of(final EffectiveModelContext context,
+            final YangInstanceIdentifier path) {
+        return new NormalizedNodeStreamWriterStack(DataSchemaContextTree.from(context)
+            .enterPath(path)
+            .orElseThrow()
+            .stack());
+    }
+
     /**
      * Create a new writer with the specified context and rooted in the specified schema path.
      *
@@ -148,7 +166,7 @@ public final class NormalizedNodeStreamWriterStack implements LeafrefResolver {
     @Deprecated
     public static @NonNull NormalizedNodeStreamWriterStack of(final EffectiveModelContext context,
             final SchemaPath path) {
-        return new NormalizedNodeStreamWriterStack(SchemaInferenceStack.ofInstantiatedPath(context, path));
+        return new NormalizedNodeStreamWriterStack(SchemaInferenceStack.ofSchemaPath(context, path));
     }
 
     /**
@@ -247,9 +265,19 @@ public final class NormalizedNodeStreamWriterStack implements LeafrefResolver {
 
     public SchemaNode startContainerNode(final NodeIdentifier name) {
         LOG.debug("Enter container {}", name);
-        final SchemaNode schema = enterDataTree(name);
-        checkArgument(schema instanceof ContainerLike || schema instanceof NotificationDefinition,
-            "Node %s is not a container nor a notification", schema);
+
+        final SchemaNode schema;
+        if (schemaStack.isEmpty() && root instanceof NotificationDefinition) {
+            // Special case for stacks initialized at notification. We pretend the first container is contained within
+            // itself.
+            // FIXME: 8.0.0: factor this special case out to something more reasonable, like being initialized at the
+            //               Notification's parent and knowing to enterSchemaTree() instead of enterDataTree().
+            schema = (NotificationDefinition) root;
+        } else {
+            schema = enterDataTree(name);
+            checkArgument(schema instanceof ContainerLike, "Node %s is not a container", schema);
+        }
+
         schemaStack.push(schema);
         return schema;
     }