2 * Copyright (c) 2016 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.impl.schema.tree;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.fail;
13 import java.util.Arrays;
14 import java.util.Collection;
15 import org.junit.Before;
16 import org.junit.Test;
17 import org.opendaylight.yangtools.yang.common.QName;
18 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
19 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
20 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
21 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
22 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
23 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
24 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafNodeBuilder;
25 import org.opendaylight.yangtools.yang.data.tree.api.DataTree;
26 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeCandidate;
27 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeCandidateNode;
28 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeConfiguration;
29 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeModification;
30 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeModificationCursor;
31 import org.opendaylight.yangtools.yang.data.tree.api.DataValidationFailedException;
32 import org.opendaylight.yangtools.yang.data.tree.api.ModificationType;
33 import org.opendaylight.yangtools.yang.data.tree.api.TreeType;
34 import org.opendaylight.yangtools.yang.data.tree.spi.DataTreeCandidates;
35 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
39 public class DataTreeCandidatesTest extends AbstractTestModelTest {
41 private static final Logger LOG = LoggerFactory.getLogger(DataTreeCandidatesTest.class);
43 private DataTree dataTree;
46 public void setUp() throws Exception {
47 dataTree = new InMemoryDataTreeFactory().create(DataTreeConfiguration.DEFAULT_OPERATIONAL, SCHEMA_CONTEXT);
49 final ContainerNode testContainer = ImmutableContainerNodeBuilder.create()
50 .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME))
51 .withChild(ImmutableContainerNodeBuilder.create()
52 .withNodeIdentifier(new NodeIdentifier(SchemaContext.NAME))
56 final InMemoryDataTreeModification modification = (InMemoryDataTreeModification) dataTree.takeSnapshot()
58 final DataTreeModificationCursor cursor = modification.openCursor();
59 cursor.write(TestModel.TEST_PATH.getLastPathArgument(), testContainer);
62 dataTree.validate(modification);
63 final DataTreeCandidate candidate = dataTree.prepare(modification);
64 dataTree.commit(candidate);
68 public void testRootedCandidate() throws DataValidationFailedException {
69 final DataTree innerDataTree = new InMemoryDataTreeFactory().create(
70 new DataTreeConfiguration.Builder(TreeType.OPERATIONAL)
71 .setMandatoryNodesValidation(true)
72 .setRootPath(TestModel.INNER_CONTAINER_PATH)
73 .setUniqueIndexes(true).build(), SCHEMA_CONTEXT);
75 final LeafNode<String> leaf = ImmutableLeafNodeBuilder.<String>create()
76 .withNodeIdentifier(new NodeIdentifier(TestModel.VALUE_QNAME))
77 .withValue("testing-value")
80 final DataTreeModification modification = innerDataTree.takeSnapshot().newModification();
81 modification.write(TestModel.VALUE_PATH, leaf);
84 dataTree.validate(modification);
85 final DataTreeCandidate candidate = dataTree.prepare(modification);
86 dataTree.commit(candidate);
88 final DataTreeModification newModification = dataTree.takeSnapshot().newModification();
89 final DataTreeCandidate newCandidate = DataTreeCandidates.newDataTreeCandidate(TestModel.INNER_CONTAINER_PATH,
90 candidate.getRootNode());
93 // lets see if getting the identifier of the root node throws an exception
94 newCandidate.getRootNode().getIdentifier();
96 } catch (IllegalStateException e) {
97 LOG.debug("Cannot get identifier of root node candidate which is correct", e);
100 // lets see if we can apply this rooted candidate to a new dataTree
101 DataTreeCandidates.applyToModification(newModification,
104 final LeafNode<?> readLeaf = (LeafNode<?>) newModification.readNode(TestModel.INNER_VALUE_PATH).get();
105 assertEquals(readLeaf, leaf);
109 public void testEmptyMergeOnContainer() throws DataValidationFailedException {
110 DataTreeModification modification = dataTree.takeSnapshot().newModification();
111 modification.merge(TestModel.NON_PRESENCE_PATH, ImmutableNodes.containerNode(TestModel.NON_PRESENCE_QNAME));
112 modification.ready();
113 dataTree.validate(modification);
115 // The entire transaction needs to fizzle to a no-op
116 DataTreeCandidate candidate = dataTree.prepare(modification);
117 DataTreeCandidateNode node = candidate.getRootNode();
118 assertEquals(ModificationType.UNMODIFIED, node.getModificationType());
121 assertUnmodified(1, node.getChildNodes());
125 public void testEmptyWriteOnContainer() throws DataValidationFailedException {
126 DataTreeModification modification = dataTree.takeSnapshot().newModification();
127 modification.write(TestModel.NON_PRESENCE_PATH, ImmutableNodes.containerNode(TestModel.NON_PRESENCE_QNAME));
128 modification.ready();
129 dataTree.validate(modification);
131 // The entire transaction needs to fizzle to a no-op
132 DataTreeCandidate candidate = dataTree.prepare(modification);
133 DataTreeCandidateNode node = candidate.getRootNode();
134 assertEquals(ModificationType.UNMODIFIED, node.getModificationType());
137 assertUnmodified(1, node.getChildNodes());
141 public void testEmptyMergesOnDeleted() throws DataValidationFailedException {
142 DataTreeModification modification = dataTree.takeSnapshot().newModification();
143 modification.delete(TestModel.NON_PRESENCE_PATH);
144 modification.merge(TestModel.DEEP_CHOICE_PATH, ImmutableNodes.choiceNode(TestModel.DEEP_CHOICE_QNAME));
145 modification.ready();
146 dataTree.validate(modification);
148 final DataTreeCandidate candidate = dataTree.prepare(modification);
149 assertEquals(YangInstanceIdentifier.empty(), candidate.getRootPath());
150 final DataTreeCandidateNode node = candidate.getRootNode();
151 assertEquals(ModificationType.UNMODIFIED, node.getModificationType());
154 assertUnmodified(1, node.getChildNodes());
158 public void testEmptyMergesOnExisting() throws DataValidationFailedException {
159 // Make sure 'non-presence' is present
160 DataTreeModification modification = dataTree.takeSnapshot().newModification();
161 modification.write(TestModel.NAME_PATH, ImmutableNodes.leafNode(TestModel.NAME_QNAME, "foo"));
162 modification.ready();
163 dataTree.validate(modification);
164 dataTree.commit(dataTree.prepare(modification));
166 // Issue an empty merge on it and a child choice
167 modification = dataTree.takeSnapshot().newModification();
168 modification.merge(TestModel.NON_PRESENCE_PATH, ImmutableNodes.containerNode(TestModel.NON_PRESENCE_QNAME));
169 modification.merge(TestModel.DEEP_CHOICE_PATH, ImmutableNodes.choiceNode(TestModel.DEEP_CHOICE_QNAME));
170 modification.ready();
171 dataTree.validate(modification);
173 // The entire transaction needs to fizzle to a no-op
174 final DataTreeCandidate candidate = dataTree.prepare(modification);
175 assertEquals(YangInstanceIdentifier.empty(), candidate.getRootPath());
176 final DataTreeCandidateNode node = candidate.getRootNode();
177 assertEquals(ModificationType.UNMODIFIED, node.getModificationType());
179 // 'non-presence' and 'test'
180 assertUnmodified(2, node.getChildNodes());
184 public void testAggregateWithoutChanges() throws DataValidationFailedException {
185 DataTreeModification modification1 = dataTree.takeSnapshot().newModification();
187 TestModel.INNER_CONTAINER_PATH.node(QName.create(TestModel.INNER_CONTAINER_QNAME,"value")),
188 ImmutableNodes.leafNode(QName.create(TestModel.INNER_CONTAINER_QNAME,"value"),"value1"));
189 modification1.ready();
190 dataTree.validate(modification1);
191 DataTreeCandidate candidate1 = dataTree.prepare(modification1);
192 dataTree.commit(candidate1);
194 DataTreeModification modification2 = dataTree.takeSnapshot().newModification();
195 modification2.delete(TestModel.INNER_CONTAINER_PATH);
196 modification2.ready();
197 dataTree.validate(modification2);
198 DataTreeCandidate candidate2 = dataTree.prepare(modification2);
199 dataTree.commit(candidate2);
201 DataTreeCandidate aggregateCandidate = DataTreeCandidates.aggregate(Arrays.asList(candidate1,candidate2));
203 assertEquals(ModificationType.UNMODIFIED,aggregateCandidate.getRootNode().getModificationType());
207 public void testAggregate() throws DataValidationFailedException {
208 DataTreeModification modification = dataTree.takeSnapshot().newModification();
210 TestModel.INNER_CONTAINER_PATH.node(QName.create(TestModel.INNER_CONTAINER_QNAME,"value")),
211 ImmutableNodes.leafNode(QName.create(TestModel.INNER_CONTAINER_QNAME,"value"),"value1"));
212 modification.ready();
213 dataTree.validate(modification);
214 DataTreeCandidate candidate = dataTree.prepare(modification);
215 dataTree.commit(candidate);
217 DataTreeModification modification1 = dataTree.takeSnapshot().newModification();
218 modification1.delete(TestModel.INNER_CONTAINER_PATH);
219 modification1.ready();
220 dataTree.validate(modification1);
221 DataTreeCandidate candidate1 = dataTree.prepare(modification1);
222 dataTree.commit(candidate1);
224 DataTreeModification modification2 = dataTree.takeSnapshot().newModification();
226 TestModel.INNER_CONTAINER_PATH.node(QName.create(TestModel.INNER_CONTAINER_QNAME,"value")),
227 ImmutableNodes.leafNode(QName.create(TestModel.INNER_CONTAINER_QNAME,"value"),"value2"));
228 modification2.ready();
229 dataTree.validate(modification2);
230 DataTreeCandidate candidate2 = dataTree.prepare(modification2);
231 dataTree.commit(candidate2);
233 DataTreeCandidate aggregateCandidate = DataTreeCandidates.aggregate(Arrays.asList(candidate1,candidate2));
235 assertEquals(ModificationType.SUBTREE_MODIFIED,aggregateCandidate.getRootNode().getModificationType());
238 private static void assertUnmodified(final int expSize, final Collection<DataTreeCandidateNode> nodes) {
239 assertEquals(expSize, nodes.size());
240 nodes.forEach(node -> assertEquals(ModificationType.UNMODIFIED, node.getModificationType()));