Deprecate JSONNormalizedNodeStreamWriter.create*Writer() 99/101999/7
authorRobert Varga <robert.varga@pantheon.tech>
Sun, 7 Aug 2022 19:31:05 +0000 (21:31 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Sun, 7 Aug 2022 21:27:04 +0000 (23:27 +0200)
Deprecate methods that use SchemaPath for identification. Introduce
replacatement methods where appropriate.

JIRA: YANGTOOLS-1450
Change-Id: I7418873de01094aed75670cbfea18d221224814d
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
codec/yang-data-codec-gson/src/main/java/org/opendaylight/yangtools/yang/data/codec/gson/JSONNormalizedNodeStreamWriter.java
codec/yang-data-codec-gson/src/test/java/org/opendaylight/yangtools/yang/data/codec/gson/AbstractYT1027Test.java
codec/yang-data-codec-gson/src/test/java/org/opendaylight/yangtools/yang/data/codec/gson/AnyXmlSupportTest.java
codec/yang-data-codec-gson/src/test/java/org/opendaylight/yangtools/yang/data/codec/gson/Bug5446Test.java
codec/yang-data-codec-gson/src/test/java/org/opendaylight/yangtools/yang/data/codec/gson/Bug7246Test.java
codec/yang-data-codec-gson/src/test/java/org/opendaylight/yangtools/yang/data/codec/gson/NormalizedNodeToJsonStreamTest.java
codec/yang-data-codec-gson/src/test/java/org/opendaylight/yangtools/yang/data/codec/gson/StreamToNormalizedNodeTest.java
codec/yang-data-codec-gson/src/test/java/org/opendaylight/yangtools/yang/data/codec/gson/TestUtils.java
codec/yang-data-codec-gson/src/test/java/org/opendaylight/yangtools/yang/data/codec/gson/YT1029Test.java

index a4c9fe10f7278aa11d4858d29240b452e12e02bb..686a95b71d32a41c59de1e66b6dd1c5d5beafb24 100644 (file)
@@ -18,6 +18,7 @@ import java.util.NoSuchElementException;
 import java.util.regex.Pattern;
 import javax.xml.transform.dom.DOMSource;
 import org.checkerframework.checker.regex.qual.Regex;
+import org.eclipse.jdt.annotation.Nullable;
 import org.opendaylight.yangtools.rfc8528.data.api.MountPointContext;
 import org.opendaylight.yangtools.rfc8528.data.api.MountPointIdentifier;
 import org.opendaylight.yangtools.rfc8528.data.api.StreamWriterMountPointExtension;
@@ -106,6 +107,56 @@ public abstract class JSONNormalizedNodeStreamWriter implements NormalizedNodeSt
         context = requireNonNull(rootContext);
     }
 
+    /**
+     * Create a new stream writer, which writes to the specified output stream.
+     *
+     * <p>
+     * The codec factory can be reused between multiple writers.
+     *
+     * <p>
+     * Returned writer is exclusive user of JsonWriter, which means it will start
+     * top-level JSON element and ends it.
+     *
+     * <p>
+     * This instance of writer can be used only to emit one top level element,
+     * otherwise it will produce incorrect JSON. Closing this instance will close
+     * the writer too.
+     *
+     * @param codecFactory JSON codec factory
+     * @param jsonWriter JsonWriter
+     * @return A stream writer instance
+     */
+    public static NormalizedNodeStreamWriter createExclusiveWriter(final JSONCodecFactory codecFactory,
+            final JsonWriter jsonWriter) {
+        return createExclusiveWriter(codecFactory, jsonWriter, null);
+    }
+
+    /**
+     * Create a new stream writer, which writes to the specified output stream.
+     *
+     * <p>
+     * The codec factory can be reused between multiple writers.
+     *
+     * <p>
+     * Returned writer is exclusive user of JsonWriter, which means it will start
+     * top-level JSON element and ends it.
+     *
+     * <p>
+     * This instance of writer can be used only to emit one top level element,
+     * otherwise it will produce incorrect JSON. Closing this instance will close
+     * the writer too.
+     *
+     * @param codecFactory JSON codec factory
+     * @param jsonWriter JsonWriter
+     * @param initialNs Initial namespace, can be null
+     * @return A stream writer instance
+     */
+    public static NormalizedNodeStreamWriter createExclusiveWriter(final JSONCodecFactory codecFactory,
+            final JsonWriter jsonWriter, final @Nullable XMLNamespace initialNs) {
+        return new Exclusive(codecFactory, NormalizedNodeStreamWriterStack.of(codecFactory.getEffectiveModelContext()),
+            jsonWriter, new JSONStreamWriterExclusiveRootContext(initialNs));
+    }
+
     /**
      * Create a new stream writer, which writes to the specified output stream.
      *
@@ -126,7 +177,9 @@ public abstract class JSONNormalizedNodeStreamWriter implements NormalizedNodeSt
      * @param initialNs Initial namespace
      * @param jsonWriter JsonWriter
      * @return A stream writer instance
+     * @deprecated Use one of the alternative overloads
      */
+    @Deprecated(since = "9.0.1", forRemoval = true)
     public static NormalizedNodeStreamWriter createExclusiveWriter(final JSONCodecFactory codecFactory,
             final SchemaPath path, final XMLNamespace initialNs, final JsonWriter jsonWriter) {
         return new Exclusive(codecFactory,
@@ -189,6 +242,53 @@ public abstract class JSONNormalizedNodeStreamWriter implements NormalizedNodeSt
             new JSONStreamWriterExclusiveRootContext(initialNs));
     }
 
+    /**
+     * Create a new stream writer, which writes to the specified output stream.
+     *
+     * <p>
+     * The codec factory can be reused between multiple writers.
+     *
+     * <p>
+     * Returned writer can be used emit multiple top level element,
+     * but does not start / close parent JSON object, which must be done
+     * by user providing {@code jsonWriter} instance in order for
+     * JSON to be valid. Closing this instance <strong>will not</strong>
+     * close the wrapped writer; the caller must take care of that.
+     *
+     * @param codecFactory JSON codec factory
+     * @param jsonWriter JsonWriter
+     * @return A stream writer instance
+     */
+    public static NormalizedNodeStreamWriter createNestedWriter(final JSONCodecFactory codecFactory,
+            final JsonWriter jsonWriter) {
+        return createNestedWriter(codecFactory, jsonWriter, null);
+    }
+
+    /**
+     * Create a new stream writer, which writes to the specified output stream.
+     *
+     * <p>
+     * The codec factory can be reused between multiple writers.
+     *
+     * <p>
+     * Returned writer can be used emit multiple top level element,
+     * but does not start / close parent JSON object, which must be done
+     * by user providing {@code jsonWriter} instance in order for
+     * JSON to be valid. Closing this instance <strong>will not</strong>
+     * close the wrapped writer; the caller must take care of that.
+     *
+     * @param codecFactory JSON codec factory
+     * @param initialNs Initial namespace
+     * @param jsonWriter JsonWriter
+     * @return A stream writer instance
+     */
+    public static NormalizedNodeStreamWriter createNestedWriter(final JSONCodecFactory codecFactory,
+            final JsonWriter jsonWriter, final @Nullable XMLNamespace initialNs) {
+        return new Nested(codecFactory,
+            NormalizedNodeStreamWriterStack.of(codecFactory.getEffectiveModelContext()), jsonWriter,
+            new JSONStreamWriterSharedRootContext(initialNs));
+    }
+
     /**
      * Create a new stream writer, which writes to the specified output stream.
      *
@@ -207,7 +307,9 @@ public abstract class JSONNormalizedNodeStreamWriter implements NormalizedNodeSt
      * @param initialNs Initial namespace
      * @param jsonWriter JsonWriter
      * @return A stream writer instance
+     * @deprecated Use one of the alternative overloads
      */
+    @Deprecated(since = "9.0.1", forRemoval = true)
     public static NormalizedNodeStreamWriter createNestedWriter(final JSONCodecFactory codecFactory,
             final SchemaPath path, final XMLNamespace initialNs, final JsonWriter jsonWriter) {
         return new Nested(codecFactory,
index b50fdf2a748afef9a7944a6961a8e0b1f3ee262c..72567634d47f0be1e35bd6a69878b4096f0738a8 100644 (file)
@@ -33,7 +33,6 @@ import org.opendaylight.yangtools.yang.data.impl.schema.NormalizedNodeResult;
 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
-import org.opendaylight.yangtools.yang.model.api.SchemaPath;
 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
 import org.opendaylight.yangtools.yang.model.api.type.DecimalTypeDefinition;
 import org.opendaylight.yangtools.yang.model.api.type.Int64TypeDefinition;
@@ -152,7 +151,7 @@ public abstract class AbstractYT1027Test {
     private String toJSON(final NormalizedNode input) throws IOException {
         final Writer writer = new StringWriter();
         final NormalizedNodeStreamWriter jsonStream = JSONNormalizedNodeStreamWriter.createExclusiveWriter(
-            codecFactory(), SchemaPath.ROOT, null, JsonWriterFactory.createJsonWriter(writer, 2));
+            codecFactory(), JsonWriterFactory.createJsonWriter(writer, 2));
         try (NormalizedNodeWriter nodeWriter = NormalizedNodeWriter.forStreamWriter(jsonStream)) {
             nodeWriter.write(input);
         }
index 284fe600be0d5627bc09d6d773ddb799e15d239a..5f4656087711b95771eed0c205c8056685911050 100644 (file)
@@ -35,7 +35,6 @@ import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNormalizedNodeStreamWriter;
 import org.opendaylight.yangtools.yang.data.impl.schema.NormalizedNodeResult;
-import org.opendaylight.yangtools.yang.model.api.SchemaPath;
 import org.w3c.dom.Document;
 import org.w3c.dom.Element;
 import org.w3c.dom.Text;
@@ -73,8 +72,7 @@ public class AnyXmlSupportTest extends AbstractComplexJsonTest {
         final DOMSource Lf14AnyExpectedValue = createAnyXmlSimpleValue("ns:complex:json", "lf14-any", "null");
         verifyTransformedAnyXmlNodeValue(Lf14AnyExpectedValue, Lf14AnyActualValue);
 
-        final String serializationResult = normalizedNodesToJsonString(transformedInput, schemaContext,
-                SchemaPath.ROOT);
+        final String serializationResult = normalizedNodesToJsonString(transformedInput, schemaContext);
 
         assertEquals(JsonParser.parseString(inputJson), JsonParser.parseString(serializationResult));
     }
@@ -105,8 +103,7 @@ public class AnyXmlSupportTest extends AbstractComplexJsonTest {
         verifyTransformedAnyXmlNodeValue(Lf14AnyExpectedValue, Lf14AnyActualValue);
 
         // serialization
-        final String serializationResult = normalizedNodesToJsonString(transformedInput, schemaContext,
-                SchemaPath.ROOT);
+        final String serializationResult = normalizedNodesToJsonString(transformedInput, schemaContext);
 
         assertEquals(JsonParser.parseString(loadTextFile("/bug8927/json/composite.json")),
             JsonParser.parseString(serializationResult));
@@ -145,7 +142,7 @@ public class AnyXmlSupportTest extends AbstractComplexJsonTest {
         final DataContainerChild data = ((ContainerNode) result.getResult())
                 .childByArg(new NodeIdentifier(QName.create("bug8927.test", "2017-01-01", "foo")));
         assertNotNull(data);
-        final String jsonOutput = normalizedNodesToJsonString(data, schemaContext, SchemaPath.ROOT);
+        final String jsonOutput = normalizedNodesToJsonString(data, schemaContext);
         assertEquals(JsonParser.parseReader(new FileReader(
             new File(getClass().getResource(expectedJsonFile).toURI()), StandardCharsets.UTF_8)),
             JsonParser.parseString(jsonOutput));
index 30ce7247791db290f81dbdf86bae4a52f001e8e1..2df8e5cbf52f737da7671b5fb47dbc7ac563faa4 100644 (file)
@@ -34,7 +34,6 @@ import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeWrit
 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
-import org.opendaylight.yangtools.yang.model.api.SchemaPath;
 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
 
 public class Bug5446Test {
@@ -72,7 +71,7 @@ public class Bug5446Test {
             final NormalizedNode inputStructure) throws IOException {
 
         final NormalizedNodeStreamWriter jsonStream = JSONNormalizedNodeStreamWriter.createExclusiveWriter(
-            JSONCodecFactorySupplier.DRAFT_LHOTKA_NETMOD_YANG_JSON_02.getShared(schemaContext), SchemaPath.ROOT, null,
+            JSONCodecFactorySupplier.DRAFT_LHOTKA_NETMOD_YANG_JSON_02.getShared(schemaContext),
             JsonWriterFactory.createJsonWriter(writer, 2));
         try (NormalizedNodeWriter nodeWriter = NormalizedNodeWriter.forStreamWriter(jsonStream)) {
             nodeWriter.write(inputStructure);
index 46af2239580eab4cecde24aa48ef41fb1dbbf731..a735595268e712f46b5d31798f1591c28912ee10 100644 (file)
@@ -27,7 +27,7 @@ import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeWrit
 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
-import org.opendaylight.yangtools.yang.model.api.SchemaPath;
+import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
 
 public class Bug7246Test {
@@ -40,10 +40,9 @@ public class Bug7246Test {
         final ContainerNode inputStructure = ImmutableContainerNodeBuilder.create()
                 .withNodeIdentifier(new NodeIdentifier(qN("my-name")))
                 .withChild(ImmutableNodes.leafNode(new NodeIdentifier(qN("my-name")), "my-value")).build();
-        final SchemaPath rootPath = SchemaPath.create(true, qN("my-name"), qN("input"));
         final Writer writer = new StringWriter();
-        final String jsonOutput = normalizedNodeToJsonStreamTransformation(schemaContext, rootPath, writer,
-                inputStructure);
+        final String jsonOutput = normalizedNodeToJsonStreamTransformation(schemaContext,  writer, inputStructure,
+            qN("my-name"), qN("input"));
 
         assertEquals(JsonParser.parseReader(new FileReader(
             new File(getClass().getResource("/bug7246/json/expected-output.json").toURI()), StandardCharsets.UTF_8)),
@@ -55,11 +54,9 @@ public class Bug7246Test {
     }
 
     private static String normalizedNodeToJsonStreamTransformation(final EffectiveModelContext schemaContext,
-            final SchemaPath path, final Writer writer, final NormalizedNode inputStructure)
-            throws IOException {
-
+            final Writer writer, final NormalizedNode inputStructure, final QName... path) throws IOException {
         final NormalizedNodeStreamWriter jsonStream = JSONNormalizedNodeStreamWriter.createExclusiveWriter(
-                JSONCodecFactorySupplier.DRAFT_LHOTKA_NETMOD_YANG_JSON_02.getShared(schemaContext), path,
+                JSONCodecFactorySupplier.DRAFT_LHOTKA_NETMOD_YANG_JSON_02.getShared(schemaContext), Absolute.of(path),
                 XMLNamespace.of(NS), JsonWriterFactory.createJsonWriter(writer, 2));
         try (NormalizedNodeWriter nodeWriter = NormalizedNodeWriter.forStreamWriter(jsonStream)) {
             nodeWriter.write(inputStructure);
index e6483dc3c8df56a70ca3fcc49d2ede5ae0932e3d..9d7b003a7cda00022089055f0d5926bd37de7d3a 100644 (file)
@@ -32,7 +32,6 @@ import org.junit.Test;
 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeWriter;
-import org.opendaylight.yangtools.yang.model.api.SchemaPath;
 
 /**
  * Each test tests whether json output obtained after transformation contains is corect. The transformation takes
@@ -293,7 +292,7 @@ public class NormalizedNodeToJsonStreamTest extends AbstractComplexJsonTest {
             throws IOException {
         final Writer writer = new StringWriter();
         final NormalizedNodeStreamWriter jsonStream = JSONNormalizedNodeStreamWriter.createExclusiveWriter(
-            lhotkaCodecFactory, SchemaPath.ROOT, null, JsonWriterFactory.createJsonWriter(writer, 2));
+            lhotkaCodecFactory, JsonWriterFactory.createJsonWriter(writer, 2));
         try (NormalizedNodeWriter nodeWriter = NormalizedNodeWriter.forStreamWriter(jsonStream)) {
             nodeWriter.write(inputStructure);
         }
index d98ea28de00303fcf830e3d6de2e3bde10197a68..382a3f2e6b8da55ed78c5afe30da8a8437562f3d 100644 (file)
@@ -25,7 +25,6 @@ import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStre
 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeWriter;
 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNormalizedNodeStreamWriter;
 import org.opendaylight.yangtools.yang.data.impl.schema.NormalizedNodeResult;
-import org.opendaylight.yangtools.yang.model.api.SchemaPath;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -99,7 +98,7 @@ public class StreamToNormalizedNodeTest extends AbstractComplexJsonTest {
         // StreamWriter which outputs JSON strings
         // StreamWriter which outputs JSON strings
         final NormalizedNodeStreamWriter jsonStream = JSONNormalizedNodeStreamWriter.createExclusiveWriter(
-            lhotkaCodecFactory, SchemaPath.ROOT, null, JsonWriterFactory.createJsonWriter(writer, 2));
+            lhotkaCodecFactory, JsonWriterFactory.createJsonWriter(writer, 2));
 
         // NormalizedNode -> StreamWriter
         final NormalizedNodeWriter nodeWriter = NormalizedNodeWriter.forStreamWriter(jsonStream);
index 9271d0ee777a92b92303e2c33408da2d4e1452f7..c2ec37dba40a3be81785a458ae09c124a4237a96 100644 (file)
@@ -32,7 +32,6 @@ import org.opendaylight.yangtools.yang.data.codec.xml.XmlParserStream;
 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNormalizedNodeStreamWriter;
 import org.opendaylight.yangtools.yang.data.impl.schema.NormalizedNodeResult;
 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
-import org.opendaylight.yangtools.yang.model.api.SchemaPath;
 
 public final class TestUtils {
     private TestUtils() {
@@ -63,10 +62,10 @@ public final class TestUtils {
     }
 
     static String normalizedNodesToJsonString(final NormalizedNode data,
-            final EffectiveModelContext schemaContext, final SchemaPath rootPath) throws IOException {
+            final EffectiveModelContext schemaContext) throws IOException {
         final Writer writer = new StringWriter();
         final NormalizedNodeStreamWriter jsonStream = JSONNormalizedNodeStreamWriter.createExclusiveWriter(
-                JSONCodecFactorySupplier.DRAFT_LHOTKA_NETMOD_YANG_JSON_02.getShared(schemaContext), rootPath, null,
+                JSONCodecFactorySupplier.DRAFT_LHOTKA_NETMOD_YANG_JSON_02.getShared(schemaContext),
                 JsonWriterFactory.createJsonWriter(writer, 2));
         try (NormalizedNodeWriter nodeWriter = NormalizedNodeWriter.forStreamWriter(jsonStream)) {
             nodeWriter.write(data);
index 13cfd8f90eb3a9a7624f4739e72892611dbbd49d..5e4cb8390ca68ad8205e7d958144fdc89e3c64e3 100644 (file)
@@ -15,14 +15,13 @@ import java.io.Writer;
 import org.junit.Test;
 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeWriter;
-import org.opendaylight.yangtools.yang.model.api.SchemaPath;
 
 public class YT1029Test extends AbstractComplexJsonTest {
     @Test
     public void testMultipleRootChildren() throws IOException {
         final Writer writer = new StringWriter();
         final NormalizedNodeStreamWriter jsonStream = JSONNormalizedNodeStreamWriter.createExclusiveWriter(
-            lhotkaCodecFactory, SchemaPath.ROOT, null, JsonWriterFactory.createJsonWriter(writer, 2));
+            lhotkaCodecFactory, JsonWriterFactory.createJsonWriter(writer, 2));
         try (NormalizedNodeWriter nodeWriter = NormalizedNodeWriter.forStreamWriter(jsonStream)) {
             nodeWriter.write(CONT1_WITH_EMPTYLEAF);
             nodeWriter.write(CONT1_WITH_EMPTYLEAF);