Eliminate IdentifierCodec 97/109097/1
authorRobert Varga <robert.varga@pantheon.tech>
Tue, 28 Nov 2023 14:50:19 +0000 (15:50 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Tue, 28 Nov 2023 14:51:19 +0000 (15:51 +0100)
This is now a mostly-useless indirection to
YangInstanceIdentifierSerializer. Eliminate it by inlining serialization
into callers and tests in YangInstanceIdentifierSerializerTest.

JIRA: NETCONF-1157
Change-Id: I6a560a3ce520570e63ea6b4add335bafe36c4438
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/databind/PatchBody.java
restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/rests/transactions/RestconfStrategy.java
restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/utils/parser/IdentifierCodec.java [deleted file]
restconf/restconf-nb/src/main/java/org/opendaylight/restconf/server/mdsal/streams/devnotif/SubscribeDeviceNotificationRpc.java
restconf/restconf-nb/src/main/java/org/opendaylight/restconf/server/mdsal/streams/dtcl/CreateDataChangeEventSubscriptionRpc.java
restconf/restconf-nb/src/test/java/org/opendaylight/restconf/nb/rfc8040/utils/parser/IdentifierCodecTest.java [deleted file]
restconf/restconf-nb/src/test/java/org/opendaylight/restconf/nb/rfc8040/utils/parser/YangInstanceIdentifierSerializerTest.java
restconf/restconf-nb/src/test/resources/restconf/parser/serializer/list-test.yang [moved from restconf/restconf-nb/src/test/resources/restconf/parser/list-test.yang with 100% similarity]

index 4a2956bcc88cad8f3b8d62ea9b8da757f36bbee6..b0b9a73b36689dbc7677795a50757b2b53396d92 100644 (file)
@@ -17,7 +17,7 @@ import org.opendaylight.restconf.api.ApiPath;
 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
 import org.opendaylight.restconf.common.patch.PatchContext;
 import org.opendaylight.restconf.nb.rfc8040.legacy.InstanceIdentifierContext;
-import org.opendaylight.restconf.nb.rfc8040.utils.parser.IdentifierCodec;
+import org.opendaylight.restconf.nb.rfc8040.utils.parser.YangInstanceIdentifierSerializer;
 import org.opendaylight.restconf.server.api.DataPatchPath;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.patch.rev170222.yang.patch.yang.patch.Edit.Operation;
 import org.opendaylight.yangtools.yang.common.ErrorTag;
@@ -54,7 +54,7 @@ public abstract sealed class PatchBody extends AbstractBody permits JsonPatchBod
         if (urlPath.isEmpty()) {
             targetUrl = target.startsWith("/") ? target.substring(1) : target;
         } else {
-            targetUrl = IdentifierCodec.serialize(urlPath, databind) + target;
+            targetUrl = new YangInstanceIdentifierSerializer(databind).serializePath(urlPath) + target;
         }
 
         try {
index 50725a28237bf202d9f9abc0e2962919e5fc5210..4042f9ea4a3f87963ce22a5eb084c5f2c61df5da 100644 (file)
@@ -48,7 +48,7 @@ import org.opendaylight.restconf.common.patch.PatchContext;
 import org.opendaylight.restconf.common.patch.PatchStatusContext;
 import org.opendaylight.restconf.common.patch.PatchStatusEntity;
 import org.opendaylight.restconf.nb.rfc8040.Insert;
-import org.opendaylight.restconf.nb.rfc8040.utils.parser.IdentifierCodec;
+import org.opendaylight.restconf.nb.rfc8040.utils.parser.YangInstanceIdentifierSerializer;
 import org.opendaylight.restconf.server.api.DataPostResult.CreateResource;
 import org.opendaylight.restconf.server.api.DataPutResult;
 import org.opendaylight.restconf.server.api.DatabindContext;
@@ -399,10 +399,9 @@ public abstract class RestconfStrategy {
         Futures.addCallback(future, new FutureCallback<CommitInfo>() {
             @Override
             public void onSuccess(final CommitInfo result) {
-                ret.set(new CreateResource(IdentifierCodec.serialize(
+                ret.set(new CreateResource(new YangInstanceIdentifierSerializer(databind).serializePath(
                     data instanceof MapNode mapData && !mapData.isEmpty()
-                        ? path.node(mapData.body().iterator().next().name()) : path,
-                    databind)));
+                        ? path.node(mapData.body().iterator().next().name()) : path)));
             }
 
             @Override
diff --git a/restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/utils/parser/IdentifierCodec.java b/restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/utils/parser/IdentifierCodec.java
deleted file mode 100644 (file)
index 101bc9d..0000000
+++ /dev/null
@@ -1,25 +0,0 @@
-/*
- * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
- *
- * This program and the accompanying materials are made available under the
- * terms of the Eclipse Public License v1.0 which accompanies this distribution,
- * and is available at http://www.eclipse.org/legal/epl-v10.html
- */
-package org.opendaylight.restconf.nb.rfc8040.utils.parser;
-
-import org.opendaylight.restconf.server.api.DatabindContext;
-import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
-
-/**
- * Codec for identifier to serialize {@link YangInstanceIdentifier} to {@link String} and deserialize {@link String} to
- * {@link YangInstanceIdentifier}.
- */
-public final class IdentifierCodec {
-    private IdentifierCodec() {
-        // Hidden on purpose
-    }
-
-    public static String serialize(final YangInstanceIdentifier data, final DatabindContext databind) {
-        return new YangInstanceIdentifierSerializer(databind).serializePath(data);
-    }
-}
index 0fe37e185b600f730932fdfed043b70b52b99467..1d3cd86b3f149fbc856330907f841d4a659e4df5 100644 (file)
@@ -15,7 +15,7 @@ import javax.inject.Singleton;
 import org.opendaylight.mdsal.dom.api.DOMMountPointService;
 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
 import org.opendaylight.restconf.common.errors.RestconfFuture;
-import org.opendaylight.restconf.nb.rfc8040.utils.parser.IdentifierCodec;
+import org.opendaylight.restconf.nb.rfc8040.utils.parser.YangInstanceIdentifierSerializer;
 import org.opendaylight.restconf.server.api.OperationsPostResult;
 import org.opendaylight.restconf.server.spi.OperationInput;
 import org.opendaylight.restconf.server.spi.RestconfStream;
@@ -82,7 +82,8 @@ public final class SubscribeDeviceNotificationRpc extends RpcImplementation {
         }
 
         return streamRegistry.createStream(restconfURI, new DeviceNotificationSource(mountPointService, path),
-            "All YANG notifications occuring on mount point /" + IdentifierCodec.serialize(path, input.databind()))
+            "All YANG notifications occuring on mount point /"
+                + new YangInstanceIdentifierSerializer(input.databind()).serializePath(path))
             .transform(stream -> input.newOperationOutput(Builders.containerBuilder()
                 .withNodeIdentifier(new NodeIdentifier(SubscribeDeviceNotificationOutput.QNAME))
                 .withChild(ImmutableNodes.leafNode(DEVICE_NOTIFICATION_STREAM_PATH_NODEID, stream.name()))
index f651b87ba7204ecf19bb04c6491ddf62e2404223..5493fc1685674a7d8d64d3006096bcf2f98e2697 100644 (file)
@@ -18,7 +18,7 @@ import org.opendaylight.mdsal.dom.api.DOMDataBroker;
 import org.opendaylight.mdsal.dom.api.DOMDataTreeChangeService;
 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
 import org.opendaylight.restconf.common.errors.RestconfFuture;
-import org.opendaylight.restconf.nb.rfc8040.utils.parser.IdentifierCodec;
+import org.opendaylight.restconf.nb.rfc8040.utils.parser.YangInstanceIdentifierSerializer;
 import org.opendaylight.restconf.server.api.OperationsPostResult;
 import org.opendaylight.restconf.server.spi.DatabindProvider;
 import org.opendaylight.restconf.server.spi.OperationInput;
@@ -112,7 +112,7 @@ public final class CreateDataChangeEventSubscriptionRpc extends RpcImplementatio
         return streamRegistry.createStream(restconfURI,
             new DataTreeChangeSource(databindProvider, changeService, datastore, path),
             "Events occuring in " + datastore + " datastore under /"
-                + IdentifierCodec.serialize(path, input.databind()))
+                + new YangInstanceIdentifierSerializer(input.databind()).serializePath(path))
             .transform(stream -> input.newOperationOutput(Builders.containerBuilder()
                 .withNodeIdentifier(OUTPUT_NODEID)
                 .withChild(ImmutableNodes.leafNode(STREAM_NAME_NODEID, stream.name()))
diff --git a/restconf/restconf-nb/src/test/java/org/opendaylight/restconf/nb/rfc8040/utils/parser/IdentifierCodecTest.java b/restconf/restconf-nb/src/test/java/org/opendaylight/restconf/nb/rfc8040/utils/parser/IdentifierCodecTest.java
deleted file mode 100644 (file)
index df9a9c3..0000000
+++ /dev/null
@@ -1,91 +0,0 @@
-/*
- * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
- *
- * This program and the accompanying materials are made available under the
- * terms of the Eclipse Public License v1.0 which accompanies this distribution,
- * and is available at http://www.eclipse.org/legal/epl-v10.html
- */
-package org.opendaylight.restconf.nb.rfc8040.utils.parser;
-
-import static org.junit.jupiter.api.Assertions.assertEquals;
-
-import java.text.ParseException;
-import java.util.Map;
-import org.junit.jupiter.api.Test;
-import org.opendaylight.restconf.api.ApiPath;
-import org.opendaylight.restconf.server.api.DatabindContext;
-import org.opendaylight.restconf.server.spi.ApiPathNormalizer;
-import org.opendaylight.yangtools.yang.common.QName;
-import org.opendaylight.yangtools.yang.common.Uint32;
-import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
-import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue;
-import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
-
-/**
- * Unit tests for {@link IdentifierCodec} mostly according to examples from draft-ietf-netconf-restconf-13.
- */
-class IdentifierCodecTest {
-    private static final DatabindContext DATABIND = DatabindContext.ofModel(
-        YangParserTestUtils.parseYangResourceDirectory("/restconf/parser"));
-
-    /**
-     * Positive test of deserialization URI <code>String</code> to <code>YangInstanceIdentifier</code> and
-     * serialization of <code>YangInstanceIdentifier</code> to <code>String</code> when original <code>String</code>
-     * URI contains list identifier and leaf identifier.
-     */
-    @Test
-    void codecListAndLeafTest() {
-        final var dataYangII = assertNormalized("list-test:top/list1=%2C%27\"%3A\"%20%2F,,foo/list2=a,b/result");
-        final var list1 = QName.create("list:test", "2016-04-29", "list1");
-        final var list2 = QName.create("list:test", "2016-04-29", "list2");
-        assertEquals(YangInstanceIdentifier.builder()
-            .node(QName.create("list:test", "2016-04-29", "top"))
-            .node(list1)
-            .nodeWithKey(list1, Map.<QName, Object>of(
-                QName.create(list1, "key1"), ",'\":\" /",
-                QName.create(list1, "key2"), "",
-                QName.create(list1, "key3"), "foo"))
-            .node(list2)
-            .nodeWithKey(list2, Map.<QName, Object>of(
-                QName.create(list2, "key4"), "a",
-                QName.create(list2, "key5"), "b"))
-            .node(QName.create("list:test", "2016-04-29", "result"))
-            .build(), dataYangII);
-        assertEquals("list-test:top/list1=%2C%27\"%3A\" %2F,,foo/list2=a,b/result",
-            IdentifierCodec.serialize(dataYangII, DATABIND));
-    }
-
-    /**
-     * Positive test of deserialization URI <code>String</code> to <code>YangInstanceIdentifier</code> and
-     * serialization of <code>YangInstanceIdentifier</code> to <code>String</code> when original <code>String</code>
-     * URI contains leaf list identifier.
-     */
-    @Test
-    void codecLeafListTest() {
-        final var str = "list-test:top/Y=4";
-        final var dataYangII = assertNormalized(str);
-        final var y = QName.create("list:test", "2016-04-29", "Y");
-        assertEquals(YangInstanceIdentifier.builder()
-            .node(QName.create("list:test", "2016-04-29", "top"))
-            .node(y)
-            .node(new NodeWithValue<>(y, Uint32.valueOf(4)))
-            .build(), dataYangII);
-        assertEquals(str, IdentifierCodec.serialize(dataYangII, DATABIND));
-    }
-
-    /**
-     * Positive test of serialization of an empty {@link YangInstanceIdentifier}.
-     */
-    @Test
-    void codecDeserializeAndSerializeEmptyTest() {
-        assertEquals("", IdentifierCodec.serialize(YangInstanceIdentifier.of(), DATABIND));
-    }
-
-    private static YangInstanceIdentifier assertNormalized(final String str) {
-        try {
-            return new ApiPathNormalizer(DATABIND).normalizePath(ApiPath.parse(str)).path;
-        } catch (ParseException e) {
-            throw new AssertionError(e);
-        }
-    }
-}
index 656c8a4143e3b57485aee899f5c72e0baedb0ee4..00e3a3c8efd337b6710cca8efcf3a3adfba2c122 100644 (file)
@@ -11,14 +11,19 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertThrows;
 
 import com.google.common.collect.ImmutableMap;
+import java.text.ParseException;
 import java.util.Map;
+import org.eclipse.jdt.annotation.NonNull;
 import org.junit.jupiter.api.Test;
+import org.opendaylight.restconf.api.ApiPath;
 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
 import org.opendaylight.restconf.common.errors.RestconfError;
 import org.opendaylight.restconf.server.api.DatabindContext;
+import org.opendaylight.restconf.server.spi.ApiPathNormalizer;
 import org.opendaylight.yangtools.yang.common.ErrorTag;
 import org.opendaylight.yangtools.yang.common.ErrorType;
 import org.opendaylight.yangtools.yang.common.QName;
+import org.opendaylight.yangtools.yang.common.Uint32;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue;
@@ -28,8 +33,9 @@ import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
  * Unit tests for {@link YangInstanceIdentifierSerializer}.
  */
 class YangInstanceIdentifierSerializerTest {
-    private static final YangInstanceIdentifierSerializer SERIALIZER = new YangInstanceIdentifierSerializer(
-        DatabindContext.ofModel(YangParserTestUtils.parseYangResourceDirectory("/restconf/parser/serializer")));
+    private static final @NonNull DatabindContext DATABIND = DatabindContext.ofModel(
+        YangParserTestUtils.parseYangResourceDirectory("/restconf/parser/serializer"));
+    private static final YangInstanceIdentifierSerializer SERIALIZER = new YangInstanceIdentifierSerializer(DATABIND);
 
     /**
      * Positive test of serialization of <code>YangInstanceIdentifier</code> containing container node to
@@ -273,11 +279,71 @@ class YangInstanceIdentifierSerializerTest {
         assertEquals(ErrorTag.UNKNOWN_ELEMENT, error.getErrorTag());
     }
 
+    /**
+     * Positive test of deserialization URI <code>String</code> to <code>YangInstanceIdentifier</code> and
+     * serialization of <code>YangInstanceIdentifier</code> to <code>String</code> when original <code>String</code>
+     * URI contains list identifier and leaf identifier.
+     */
+    @Test
+    void codecListAndLeafTest() {
+        final var dataYangII = assertNormalized("list-test:top/list1=%2C%27\"%3A\"%20%2F,,foo/list2=a,b/result");
+        final var list1 = QName.create("list:test", "2016-04-29", "list1");
+        final var list2 = QName.create("list:test", "2016-04-29", "list2");
+        assertEquals(YangInstanceIdentifier.builder()
+            .node(QName.create("list:test", "2016-04-29", "top"))
+            .node(list1)
+            .nodeWithKey(list1, Map.<QName, Object>of(
+                QName.create(list1, "key1"), ",'\":\" /",
+                QName.create(list1, "key2"), "",
+                QName.create(list1, "key3"), "foo"))
+            .node(list2)
+            .nodeWithKey(list2, Map.<QName, Object>of(
+                QName.create(list2, "key4"), "a",
+                QName.create(list2, "key5"), "b"))
+            .node(QName.create("list:test", "2016-04-29", "result"))
+            .build(), dataYangII);
+        assertEquals("list-test:top/list1=%2C%27\"%3A\" %2F,,foo/list2=a,b/result",
+            SERIALIZER.serializePath(dataYangII));
+    }
+
+    /**
+     * Positive test of deserialization URI <code>String</code> to <code>YangInstanceIdentifier</code> and
+     * serialization of <code>YangInstanceIdentifier</code> to <code>String</code> when original <code>String</code>
+     * URI contains leaf list identifier.
+     */
+    @Test
+    void codecLeafListTest() {
+        final var str = "list-test:top/Y=4";
+        final var dataYangII = assertNormalized(str);
+        final var y = QName.create("list:test", "2016-04-29", "Y");
+        assertEquals(YangInstanceIdentifier.builder()
+            .node(QName.create("list:test", "2016-04-29", "top"))
+            .node(y)
+            .node(new NodeWithValue<>(y, Uint32.valueOf(4)))
+            .build(), dataYangII);
+        assertEquals(str, SERIALIZER.serializePath(dataYangII));
+    }
+
+    /**
+     * Positive test of serialization of an empty {@link YangInstanceIdentifier}.
+     */
+    @Test
+    void codecDeserializeAndSerializeEmptyTest() {
+        assertEquals("", SERIALIZER.serializePath(YangInstanceIdentifier.of()));
+    }
+
+    private static YangInstanceIdentifier assertNormalized(final String str) {
+        try {
+            return new ApiPathNormalizer(DATABIND).normalizePath(ApiPath.parse(str)).path;
+        } catch (ParseException e) {
+            throw new AssertionError(e);
+        }
+    }
+
     private static RestconfError assertError(final YangInstanceIdentifier path) {
         final var ex = assertThrows(RestconfDocumentedException.class, () -> SERIALIZER.serializePath(path));
         final var errors = ex.getErrors();
         assertEquals(1, errors.size());
         return errors.get(0);
     }
-
 }