Correct InstanceIdentifier.trustedCreate() 04/104604/1
authorRobert Varga <robert.varga@pantheon.tech>
Tue, 21 Mar 2023 15:24:31 +0000 (16:24 +0100)
committerRobert Varga <nite@hq.sk>
Wed, 22 Mar 2023 09:02:58 +0000 (09:02 +0000)
The check for wildcard is not correct: we should just consider the last
item when deciding to create a KeyedInstanceIdentifier. Add an explicit
test and fix the checks.

JIRA: MDSAL-818
Change-Id: Ic6fedca7e182eaf0d983e3e39b49dd75d01f8ae8
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
(cherry picked from commit 2cb72f53bee4699dc32c004810d05b2187aed1e9)

binding/mdsal-binding-test-model/src/test/java/org/opendaylight/mdsal/binding/test/model/Mdsal818Test.java [new file with mode: 0644]
binding/yang-binding/src/main/java/org/opendaylight/yangtools/yang/binding/InstanceIdentifier.java

diff --git a/binding/mdsal-binding-test-model/src/test/java/org/opendaylight/mdsal/binding/test/model/Mdsal818Test.java b/binding/mdsal-binding-test-model/src/test/java/org/opendaylight/mdsal/binding/test/model/Mdsal818Test.java
new file mode 100644 (file)
index 0000000..825be17
--- /dev/null
@@ -0,0 +1,47 @@
+/*
+ * Copyright (c) 2023 PANTHEON.tech, s.r.o. 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.mdsal.binding.test.model;
+
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertInstanceOf;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+import org.junit.jupiter.api.Test;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.Top;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.two.level.list.TopLevelList;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.two.level.list.TopLevelListKey;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.two.level.list.top.level.list.NestedList;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.two.level.list.top.level.list.NestedListKey;
+import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
+import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
+
+class Mdsal818Test {
+    @Test
+    void simpleWildcardInstanceIdentitifer() {
+        final var id = InstanceIdentifier.builder(Top.class).child(TopLevelList.class).build();
+        assertTrue(id.isWildcarded());
+        assertFalse(id instanceof KeyedInstanceIdentifier);
+    }
+
+    @Test
+    void simpleKeyedInstanceIdentitifer() {
+        final var first = assertInstanceOf(KeyedInstanceIdentifier.class, InstanceIdentifier.builder(Top.class)
+            .child(TopLevelList.class, new TopLevelListKey("foo"))
+            .build());
+        assertFalse(first.isWildcarded());
+    }
+
+    @Test
+    void wildcardKeyedInstanceIdentitifer() {
+        final var first = assertInstanceOf(KeyedInstanceIdentifier.class, InstanceIdentifier.builder(Top.class)
+            .child(TopLevelList.class)
+            .child(NestedList.class, new NestedListKey("foo"))
+            .build());
+        assertTrue(first.isWildcarded());
+    }
+}
index 48b199d678bd0280bb8a6613d0b6445d248ae6ae..2e26148960f180fd7d4bf93ff8f750701ddf17ec 100644 (file)
@@ -623,19 +623,14 @@ public class InstanceIdentifier<T extends DataObject>
 
     @SuppressWarnings({ "unchecked", "rawtypes" })
     static <N extends DataObject> @NonNull InstanceIdentifier<N> trustedCreate(final PathArgument arg,
-            final Iterable<PathArgument> pathArguments, final int hash, boolean wildcarded) {
-        if (Identifiable.class.isAssignableFrom(arg.getType()) && !wildcarded) {
-            Identifier<?> key = null;
-            if (arg instanceof IdentifiableItem) {
-                key = ((IdentifiableItem<?, ?>)arg).getKey();
-            } else {
-                wildcarded = true;
-            }
-
-            return new KeyedInstanceIdentifier(arg.getType(), pathArguments, wildcarded, hash, key);
+            final Iterable<PathArgument> pathArguments, final int hash, final boolean wildcarded) {
+        if (arg instanceof IdentifiableItem<?, ?> identifiable) {
+            return new KeyedInstanceIdentifier(arg.getType(), pathArguments, wildcarded, hash, identifiable.getKey());
         }
 
-        return new InstanceIdentifier(arg.getType(), pathArguments, wildcarded, hash);
+        final var type = arg.getType();
+        return new InstanceIdentifier(type, pathArguments, wildcarded || Identifiable.class.isAssignableFrom(type),
+            hash);
     }
 
     /**