From ef0864c5bfdc81e4b28d08ac7018a032ac8eeede Mon Sep 17 00:00:00 2001 From: Robert Varga Date: Tue, 21 Mar 2023 16:24:31 +0100 Subject: [PATCH] Correct InstanceIdentifier.trustedCreate() 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 (cherry picked from commit 2cb72f53bee4699dc32c004810d05b2187aed1e9) --- .../binding/test/model/Mdsal818Test.java | 47 +++++++++++++++++++ .../yang/binding/InstanceIdentifier.java | 17 +++---- 2 files changed, 53 insertions(+), 11 deletions(-) create mode 100644 binding/mdsal-binding-test-model/src/test/java/org/opendaylight/mdsal/binding/test/model/Mdsal818Test.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 index 0000000000..825be1743b --- /dev/null +++ b/binding/mdsal-binding-test-model/src/test/java/org/opendaylight/mdsal/binding/test/model/Mdsal818Test.java @@ -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()); + } +} diff --git a/binding/yang-binding/src/main/java/org/opendaylight/yangtools/yang/binding/InstanceIdentifier.java b/binding/yang-binding/src/main/java/org/opendaylight/yangtools/yang/binding/InstanceIdentifier.java index 48b199d678..2e26148960 100644 --- a/binding/yang-binding/src/main/java/org/opendaylight/yangtools/yang/binding/InstanceIdentifier.java +++ b/binding/yang-binding/src/main/java/org/opendaylight/yangtools/yang/binding/InstanceIdentifier.java @@ -623,19 +623,14 @@ public class InstanceIdentifier @SuppressWarnings({ "unchecked", "rawtypes" }) static @NonNull InstanceIdentifier trustedCreate(final PathArgument arg, - final Iterable 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 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); } /** -- 2.36.6