2 * Copyright (c) 2017 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.yangtools.yang.data.tree.impl;
10 import static org.junit.jupiter.api.Assertions.assertNotNull;
12 import org.junit.jupiter.api.Test;
13 import org.opendaylight.yangtools.yang.common.QName;
14 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
15 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
16 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
17 import org.opendaylight.yangtools.yang.data.spi.node.ImmutableNodes;
18 import org.opendaylight.yangtools.yang.data.tree.api.DataTree;
19 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeConfiguration;
20 import org.opendaylight.yangtools.yang.data.tree.api.DataValidationFailedException;
21 import org.opendaylight.yangtools.yang.data.tree.api.TreeType;
22 import org.opendaylight.yangtools.yang.data.tree.impl.di.InMemoryDataTreeFactory;
23 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
24 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
27 private static final String NS = "bug8291";
28 private static final QName ROOT = QName.create(NS, "root");
29 private static final QName OUTER_LIST = QName.create(NS, "outer-list");
30 private static final QName OUTER_LIST_ID = QName.create(NS, "id");
31 private static final QName INNER_LIST = QName.create(NS, "inner-list");
33 private EffectiveModelContext schemaContext;
35 private static DataTree initDataTree(final EffectiveModelContext schemaContext) {
36 final var config = new DataTreeConfiguration.Builder(TreeType.CONFIGURATION).setRootPath(
37 YangInstanceIdentifier.of(ROOT).node(OUTER_LIST)).build();
38 return new InMemoryDataTreeFactory().create(config, schemaContext);
42 void test() throws DataValidationFailedException {
43 schemaContext = YangParserTestUtils.parseYang("""
68 assertNotNull(schemaContext, "Schema context must not be null.");
70 final var inMemoryDataTree = initDataTree(schemaContext);
71 writeOuterListMapEntry(inMemoryDataTree);
72 writeInnerList(inMemoryDataTree);
75 private static void writeInnerList(final DataTree inMemoryDataTree) throws DataValidationFailedException {
76 final var modificationTree = inMemoryDataTree.takeSnapshot().newModification();
77 modificationTree.write(
78 YangInstanceIdentifier.of(NodeIdentifierWithPredicates.of(OUTER_LIST, OUTER_LIST_ID, 1)).node(INNER_LIST),
79 ImmutableNodes.newSystemMapBuilder().withNodeIdentifier(new NodeIdentifier(INNER_LIST)).build());
81 modificationTree.ready();
82 inMemoryDataTree.validate(modificationTree);
83 final var prepare = inMemoryDataTree.prepare(modificationTree);
84 inMemoryDataTree.commit(prepare);
87 private static void writeOuterListMapEntry(final DataTree inMemoryDataTree)
88 throws DataValidationFailedException {
89 final var modificationTree = inMemoryDataTree.takeSnapshot().newModification();
91 final var outerListMapEntry = ImmutableNodes.newMapEntryBuilder()
92 .withNodeIdentifier(NodeIdentifierWithPredicates.of(OUTER_LIST, OUTER_LIST_ID, 1))
93 .withChild(ImmutableNodes.leafNode(OUTER_LIST_ID, 1))
96 modificationTree.write(YangInstanceIdentifier.of(NodeIdentifierWithPredicates.of(OUTER_LIST, OUTER_LIST_ID, 1)),
98 modificationTree.ready();
100 inMemoryDataTree.validate(modificationTree);
101 final var prepare = inMemoryDataTree.prepare(modificationTree);
102 inMemoryDataTree.commit(prepare);