If the user specifies an empty InstanceIdentifier, we fail with an
undocumented NoSuchElementException. Make sure we properly handle
this case by returning a container which matches SchemaContext.NAME.
JIRA: YANGTOOLS-914
Change-Id: I0b503d3bed99d73d88e037d35979c503215f3b52
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
import org.opendaylight.yangtools.yang.model.api.SchemaContext;
public final class ImmutableNodes {
import org.opendaylight.yangtools.yang.model.api.SchemaContext;
public final class ImmutableNodes {
+ private static final NodeIdentifier SCHEMACONTEXT_NAME = NodeIdentifier.create(SchemaContext.NAME);
private ImmutableNodes() {
throw new UnsupportedOperationException("Utilities class should not be instantiated");
private ImmutableNodes() {
throw new UnsupportedOperationException("Utilities class should not be instantiated");
*/
public static NormalizedNode<?, ?> fromInstanceId(final SchemaContext ctx, final YangInstanceIdentifier id,
final Optional<NormalizedNode<?, ?>> deepestElement, final Optional<Entry<QName, ModifyAction>> operation) {
*/
public static NormalizedNode<?, ?> fromInstanceId(final SchemaContext ctx, final YangInstanceIdentifier id,
final Optional<NormalizedNode<?, ?>> deepestElement, final Optional<Entry<QName, ModifyAction>> operation) {
+ final PathArgument topLevelElement;
+ final InstanceIdToNodes<?> instanceIdToNodes;
final Iterator<PathArgument> it = id.getPathArguments().iterator();
final Iterator<PathArgument> it = id.getPathArguments().iterator();
- final PathArgument topLevelElement = it.next();
- final DataSchemaNode dataChildByName = ctx.getDataChildByName(topLevelElement.getNodeType());
- checkNotNull(dataChildByName,
- "Cannot find %s node in schema context. Instance identifier has to start from root", topLevelElement);
- final InstanceIdToNodes<?> instanceIdToNodes = InstanceIdToNodes.fromSchemaAndQNameChecked(ctx,
- topLevelElement.getNodeType());
+ if (it.hasNext()) {
+ topLevelElement = it.next();
+ final DataSchemaNode dataChildByName = ctx.getDataChildByName(topLevelElement.getNodeType());
+ checkNotNull(dataChildByName,
+ "Cannot find %s node in schema context. Instance identifier has to start from root", topLevelElement);
+ instanceIdToNodes = InstanceIdToNodes.fromSchemaAndQNameChecked(ctx, topLevelElement.getNodeType());
+ } else {
+ topLevelElement = SCHEMACONTEXT_NAME;
+ instanceIdToNodes = InstanceIdToNodes.fromDataSchemaNode(ctx);
+ }
+
return instanceIdToNodes.create(topLevelElement, it, deepestElement, operation);
}
}
return instanceIdToNodes.create(topLevelElement, it, deepestElement, operation);
}
}
YangInstanceIdentifier.create(rootContainer, leafList, leafListWithValue));
assertEquals(expectedFilter, filter);
}
YangInstanceIdentifier.create(rootContainer, leafList, leafListWithValue));
assertEquals(expectedFilter, filter);
}
+
+ @Test
+ public void testEmptyInstanceIdentifier() {
+ assertEquals(ImmutableNodes.containerNode(SchemaContext.NAME),
+ ImmutableNodes.fromInstanceId(ctx, YangInstanceIdentifier.EMPTY));
+ }