Allow returning DataObjectReferences 65/112565/1
authorRobert Varga <robert.varga@pantheon.tech>
Mon, 15 Jul 2024 12:05:03 +0000 (14:05 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Mon, 15 Jul 2024 12:07:12 +0000 (14:07 +0200)
toBinding() promises a DataObjectReference, hence we can remove the
check for KeylessStep -- resulting in DataObjectReference.

JIRA: YANGTOOLS-1577
Change-Id: I342f5b8d815542b558b1d088162c95feb49f0ca1
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
binding/binding-data-codec-dynamic/src/main/java/org/opendaylight/yangtools/binding/data/codec/impl/BindingCodecContext.java
binding/binding-data-codec-dynamic/src/main/java/org/opendaylight/yangtools/binding/data/codec/impl/InstanceIdentifierCodec.java
binding/binding-data-codec-dynamic/src/test/java/org/opendaylight/yangtools/binding/data/codec/impl/InstanceIdentifierSerializeDeserializeTest.java

index 0ff7f778825b64287a438a7cc7eaa2dbac215f88..17f05bae295dbb846a0c2b0d379d3bee7203a895 100644 (file)
@@ -32,7 +32,6 @@ import java.lang.reflect.Type;
 import java.lang.reflect.WildcardType;
 import java.time.Instant;
 import java.util.ArrayList;
-import java.util.Collection;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
@@ -486,7 +485,7 @@ public final class BindingCodecContext extends AbstractBindingNormalizedNodeSeri
      * @throws IllegalArgumentException if {@code dom} is empty
      */
     @Nullable BindingDataObjectCodecTreeNode<?> getCodecContextNode(final @NonNull YangInstanceIdentifier dom,
-            final @Nullable Collection<DataObjectStep<?>> bindingArguments) {
+            final @Nullable List<DataObjectStep<?>> bindingArguments) {
         final var it = dom.getPathArguments().iterator();
         if (!it.hasNext()) {
             throw new IllegalArgumentException("Path may not be empty");
index 02410769e6be776642e07c4113a075d334018677..da3eb151146316332f80989393e3dfbd259c9285 100644 (file)
@@ -9,13 +9,11 @@ package org.opendaylight.yangtools.binding.data.codec.impl;
 
 import static java.util.Objects.requireNonNull;
 
-import com.google.common.collect.Iterables;
 import java.util.ArrayList;
 import org.opendaylight.yangtools.binding.BindingInstanceIdentifier;
 import org.opendaylight.yangtools.binding.DataObject;
 import org.opendaylight.yangtools.binding.DataObjectReference;
 import org.opendaylight.yangtools.binding.DataObjectStep;
-import org.opendaylight.yangtools.binding.KeylessStep;
 import org.opendaylight.yangtools.binding.LeafListPropertyStep;
 import org.opendaylight.yangtools.binding.LeafPropertyStep;
 import org.opendaylight.yangtools.binding.PropertyIdentifier;
@@ -38,16 +36,7 @@ final class InstanceIdentifierCodec implements BindingInstanceIdentifierCodec,
     public <T extends DataObject> DataObjectReference<T> toBinding(final YangInstanceIdentifier domPath) {
         final var builder = new ArrayList<DataObjectStep<?>>();
         final var codec = context.getCodecContextNode(domPath, builder);
-        if (codec == null) {
-            return null;
-        }
-        if (codec instanceof ListCodecContext && Iterables.getLast(builder) instanceof KeylessStep) {
-            // We ended up in list, but without key, which means it represent list as a whole,
-            // which is not binding representable.
-            return null;
-        }
-
-        return (DataObjectReference<T>) DataObjectReference.ofUnsafeSteps(builder);
+        return codec == null ? null : (DataObjectReference<T>) DataObjectReference.ofUnsafeSteps(builder);
     }
 
     @Override
index 0e51965d6a42e74a11a11c9ca44585fbf863e7ac..41e3f9fde8969ca474a1909bd35063ff9227b669 100644 (file)
@@ -11,7 +11,6 @@ import static org.hamcrest.CoreMatchers.startsWith;
 import static org.hamcrest.MatcherAssert.assertThat;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertThrows;
 import static org.junit.Assert.assertTrue;
 import static org.junit.jupiter.api.Assertions.assertInstanceOf;
@@ -45,29 +44,26 @@ public class InstanceIdentifierSerializeDeserializeTest extends AbstractBindingC
     public static final String TOP_LEVEL_LIST_KEY_VALUE = "foo";
 
     private static final TopLevelListKey TOP_FOO_KEY = new TopLevelListKey("foo");
-    private static final InstanceIdentifier<TopLevelList> BA_TOP_LEVEL_LIST = InstanceIdentifier
-            .builder(Top.class).child(TopLevelList.class, TOP_FOO_KEY).build();
+    private static final InstanceIdentifier<TopLevelList> BA_TOP_LEVEL_LIST =
+        InstanceIdentifier.builder(Top.class).child(TopLevelList.class, TOP_FOO_KEY).build();
 
-    public static final QName TOP_QNAME = Top.QNAME;
-    public static final QName TOP_LEVEL_LIST_QNAME = QName.create(TOP_QNAME, "top-level-list");
-    public static final QName TOP_LEVEL_LIST_KEY = QName.create(TOP_QNAME, "name");
+    public static final QName TOP_LEVEL_LIST_KEY = QName.create(TopLevelList.QNAME, "name");
 
-    public static final YangInstanceIdentifier BI_TOP_PATH = YangInstanceIdentifier.of(TOP_QNAME);
-    public static final YangInstanceIdentifier BI_TOP_LEVEL_LIST_PATH = BI_TOP_PATH.node(TOP_LEVEL_LIST_QNAME);
+    public static final YangInstanceIdentifier BI_TOP_PATH = YangInstanceIdentifier.of(Top.QNAME);
+    public static final YangInstanceIdentifier BI_TOP_LEVEL_LIST_PATH = BI_TOP_PATH.node(TopLevelList.QNAME);
     public static final YangInstanceIdentifier BI_TOP_LEVEL_LIST_1_PATH = BI_TOP_LEVEL_LIST_PATH
-            .node(NodeIdentifierWithPredicates.of(TOP_LEVEL_LIST_QNAME, TOP_LEVEL_LIST_KEY,
-                TOP_LEVEL_LIST_KEY_VALUE));
+            .node(NodeIdentifierWithPredicates.of(TopLevelList.QNAME, TOP_LEVEL_LIST_KEY, TOP_LEVEL_LIST_KEY_VALUE));
 
     @Test
     public void testYangIIToBindingAwareII() {
-        final var instanceIdentifier = codecContext.fromYangInstanceIdentifier(BI_TOP_PATH);
-        assertEquals(Top.class, instanceIdentifier.lastStep().type());
+        assertEquals(InstanceIdentifier.create(Top.class).toIdentifier(),
+            codecContext.fromYangInstanceIdentifier(BI_TOP_PATH));
     }
 
     @Test
     public void testYangIIToBindingAwareIIListWildcarded() {
-        final var instanceIdentifier = codecContext.fromYangInstanceIdentifier(BI_TOP_LEVEL_LIST_PATH);
-        assertNull(instanceIdentifier);
+        assertEquals(InstanceIdentifier.builder(Top.class).child(TopLevelList.class).build().toReference(),
+            codecContext.fromYangInstanceIdentifier(BI_TOP_LEVEL_LIST_PATH));
     }
 
     @Test