2 * Copyright (c) 2014 Brocade Communications 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.controller.md.sal.common.impl.util.compat;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertNull;
13 import static org.junit.Assert.assertTrue;
14 import static org.junit.Assert.fail;
16 import com.google.common.collect.ImmutableList;
17 import com.google.common.collect.Iterables;
18 import com.google.common.collect.Lists;
19 import com.google.common.collect.Maps;
20 import com.google.common.collect.Sets;
21 import java.util.AbstractMap;
22 import java.util.ArrayList;
23 import java.util.Arrays;
24 import java.util.Collections;
25 import java.util.Comparator;
26 import java.util.List;
28 import java.util.Map.Entry;
30 import org.junit.Ignore;
31 import org.junit.Test;
32 import org.opendaylight.yangtools.yang.common.QName;
33 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
34 import org.opendaylight.yangtools.yang.data.api.Node;
35 import org.opendaylight.yangtools.yang.data.api.SimpleNode;
36 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
37 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
38 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
39 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
40 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue;
41 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
42 import org.opendaylight.yangtools.yang.data.api.schema.AnyXmlNode;
43 import org.opendaylight.yangtools.yang.data.api.schema.AugmentationNode;
44 import org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode;
45 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
46 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
47 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
48 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetNode;
49 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
50 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
51 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
52 import org.opendaylight.yangtools.yang.data.api.schema.OrderedLeafSetNode;
53 import org.opendaylight.yangtools.yang.data.api.schema.OrderedMapNode;
54 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListEntryNode;
55 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListNode;
56 import org.opendaylight.yangtools.yang.data.impl.ImmutableCompositeNode;
57 import org.opendaylight.yangtools.yang.data.impl.NodeFactory;
58 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
59 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
60 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.ListNodeBuilder;
61 import org.opendaylight.yangtools.yang.data.impl.util.CompositeNodeBuilder;
62 import org.opendaylight.yangtools.yang.model.api.Module;
63 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
64 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
67 public class DataNormalizerTest {
69 static class NormalizedNodeData {
72 Object nodeData; // List for a container, value Object for a leaf
74 NormalizedNodeData(final PathArgument nodeID, final Class<?> nodeClass, final Object nodeData) {
76 this.nodeClass = nodeClass;
77 this.nodeData = nodeData;
81 static class LegacyNodeData {
83 Object nodeData; // List for a CompositeNode, value Object for a
86 LegacyNodeData(final QName nodeKey, final Object nodeData) {
87 this.nodeKey = nodeKey;
88 this.nodeData = nodeData;
92 public String toString() {
93 return nodeKey.toString();
97 static final QName TEST_QNAME = QName.create(
98 "urn:opendaylight:params:xml:ns:yang:controller:md:sal:normalization:test", "2014-03-13", "test");
99 static final QName OUTER_LIST_QNAME = QName.create(TEST_QNAME, "outer-list");
100 static final QName INNER_LIST_QNAME = QName.create(TEST_QNAME, "inner-list");
101 static final QName OUTER_CHOICE_QNAME = QName.create(TEST_QNAME, "outer-choice");
102 static final QName ID_QNAME = QName.create(TEST_QNAME, "id");
103 static final QName NAME_QNAME = QName.create(TEST_QNAME, "name");
104 static final QName VALUE_QNAME = QName.create(TEST_QNAME, "value");
106 static final YangInstanceIdentifier TEST_PATH = YangInstanceIdentifier.of(TEST_QNAME);
107 static final YangInstanceIdentifier OUTER_LIST_PATH = YangInstanceIdentifier.builder(TEST_PATH).node(OUTER_LIST_QNAME)
109 static final QName ONE_QNAME = QName.create(TEST_QNAME, "one");
110 static final QName TWO_QNAME = QName.create(TEST_QNAME, "two");
111 static final QName THREE_QNAME = QName.create(TEST_QNAME, "three");
113 static final QName ANY_XML_DATA_QNAME = QName.create(TEST_QNAME, "any-xml-data");
114 static final QName OUTER_CONTAINER_QNAME = QName.create(TEST_QNAME, "outer-container");
115 static final QName AUGMENTED_LEAF_QNAME = QName.create(TEST_QNAME, "augmented-leaf");
116 static final QName UNKEYED_LIST_QNAME = QName.create(TEST_QNAME, "unkeyed-list");
117 static final QName UNORDERED_LEAF_LIST_QNAME = QName.create(TEST_QNAME, "unordered-leaf-list");
118 static final QName ORDERED_LEAF_LIST_QNAME = QName.create(TEST_QNAME, "ordered-leaf-list");
120 static final Short OUTER_LIST_ID = (short) 10;
122 static final YangInstanceIdentifier OUTER_LIST_PATH_LEGACY = YangInstanceIdentifier.builder(TEST_PATH)
123 .nodeWithKey(OUTER_LIST_QNAME, ID_QNAME, OUTER_LIST_ID).build();
125 static final YangInstanceIdentifier LEAF_TWO_PATH_LEGACY = YangInstanceIdentifier.builder(OUTER_LIST_PATH_LEGACY)
126 .node(TWO_QNAME).build();
128 static final QName ANY_XML_LEAF_QNAME = QName.create(TEST_QNAME, "leaf");;
129 static final QName ANY_XML_INNER_QNAME = QName.create(TEST_QNAME, "inner");
130 static final QName ANY_XML_INNER_LEAF_QNAME = QName.create(TEST_QNAME, "inner-leaf");
132 SchemaContext createTestContext() {
133 YangParserImpl parser = new YangParserImpl();
134 Set<Module> modules = parser.parseYangModelsFromStreams(Collections.singletonList(DataNormalizerTest.class
135 .getResourceAsStream("/normalization-test.yang")));
136 return parser.resolveSchemaContext(modules);
140 public void testToNormalizedInstanceIdentifier() {
141 SchemaContext testCtx = createTestContext();
142 DataNormalizer normalizer = new DataNormalizer(testCtx);
144 YangInstanceIdentifier normalizedPath = normalizer.toNormalized(LEAF_TWO_PATH_LEGACY);
146 verifyNormalizedInstanceIdentifier(normalizedPath, TEST_QNAME, OUTER_LIST_QNAME, new Object[] {
147 OUTER_LIST_QNAME, ID_QNAME, OUTER_LIST_ID }, OUTER_CHOICE_QNAME, TWO_QNAME);
150 private void verifyNormalizedInstanceIdentifier(final YangInstanceIdentifier actual, final Object... expPath) {
152 assertNotNull("Actual InstanceIdentifier is null", actual);
153 assertEquals("InstanceIdentifier path length", expPath.length, Iterables.size(actual.getPathArguments()));
155 for (int i = 0; i < expPath.length; i++) {
156 PathArgument actualArg = Iterables.get(actual.getPathArguments(), i);
157 if (expPath[i] instanceof Object[]) { // NodeIdentifierWithPredicates
158 Object[] exp = (Object[]) expPath[i];
159 assertEquals("Actual path arg " + (i + 1) + " class", NodeIdentifierWithPredicates.class,
160 actualArg.getClass());
161 NodeIdentifierWithPredicates actualNode = (NodeIdentifierWithPredicates) actualArg;
162 assertEquals("Actual path arg " + (i + 1) + " node type", exp[0], actualNode.getNodeType());
163 assertEquals("Actual path arg " + (i + 1) + " key values map size", 1, actualNode.getKeyValues().size());
164 Entry<QName, Object> keyValuesEntry = actualNode.getKeyValues().entrySet().iterator().next();
165 assertEquals("Actual path arg " + (i + 1) + " key values map key", exp[1], keyValuesEntry.getKey());
166 assertEquals("Actual path arg " + (i + 1) + " key values map value", exp[2], keyValuesEntry.getValue());
167 } else if (expPath[i] instanceof Set) { // AugmentationIdentifier
168 assertEquals("Actual path arg " + (i + 1) + " class", AugmentationIdentifier.class,
169 actualArg.getClass());
170 AugmentationIdentifier actualNode = (AugmentationIdentifier) actualArg;
171 assertEquals("Actual path arg " + (i + 1) + " PossibleChildNames", expPath[i],
172 actualNode.getPossibleChildNames());
174 assertEquals("Actual path arg " + (i + 1) + " node type", expPath[i], actualArg.getNodeType());
180 public void testToLegacyInstanceIdentifier() throws DataNormalizationException {
182 DataNormalizer normalizer = new DataNormalizer(createTestContext());
184 YangInstanceIdentifier normalized = YangInstanceIdentifier.builder().node(TEST_QNAME).node(OUTER_LIST_QNAME)
185 .nodeWithKey(OUTER_LIST_QNAME, ID_QNAME, OUTER_LIST_ID).node(OUTER_CHOICE_QNAME).node(TWO_QNAME)
188 YangInstanceIdentifier legacy = normalizer.toLegacy(normalized);
190 assertEquals("Legacy InstanceIdentifier", LEAF_TWO_PATH_LEGACY, legacy);
194 public void testToLegacyNormalizedNode() {
196 ChoiceNode choiceNode1 = Builders.choiceBuilder().withNodeIdentifier(new NodeIdentifier(OUTER_CHOICE_QNAME))
197 .withChild(ImmutableNodes.leafNode(TWO_QNAME, "two"))
198 .withChild(ImmutableNodes.leafNode(THREE_QNAME, "three")).build();
200 MapEntryNode innerListEntryNode1 = Builders.mapEntryBuilder()
201 .withNodeIdentifier(new NodeIdentifierWithPredicates(INNER_LIST_QNAME, NAME_QNAME, "inner-name1"))
202 .withChild(ImmutableNodes.leafNode(NAME_QNAME, "inner-name1"))
203 .withChild(ImmutableNodes.leafNode(VALUE_QNAME, "inner-value1")).build();
205 MapEntryNode innerListEntryNode2 = Builders.mapEntryBuilder()
206 .withNodeIdentifier(new NodeIdentifierWithPredicates(INNER_LIST_QNAME, NAME_QNAME, "inner-name2"))
207 .withChild(ImmutableNodes.leafNode(NAME_QNAME, "inner-name2"))
208 .withChild(ImmutableNodes.leafNode(VALUE_QNAME, "inner-value2")).build();
210 OrderedMapNode innerListNode = Builders.orderedMapBuilder()
211 .withNodeIdentifier(new NodeIdentifier(INNER_LIST_QNAME)).withChild(innerListEntryNode1)
212 .withChild(innerListEntryNode2).build();
214 Short outerListID1 = Short.valueOf((short) 10);
215 MapEntryNode outerListEntryNode1 = Builders.mapEntryBuilder()
216 .withNodeIdentifier(new NodeIdentifierWithPredicates(OUTER_LIST_QNAME, ID_QNAME, outerListID1))
217 .withChild(ImmutableNodes.leafNode(ID_QNAME, outerListID1)).withChild(choiceNode1)
218 .withChild(innerListNode).build();
220 ChoiceNode choiceNode2 = Builders.choiceBuilder().withNodeIdentifier(new NodeIdentifier(OUTER_CHOICE_QNAME))
221 .withChild(ImmutableNodes.leafNode(ONE_QNAME, "one")).build();
223 Short outerListID2 = Short.valueOf((short) 20);
224 MapEntryNode outerListEntryNode2 = Builders.mapEntryBuilder()
225 .withNodeIdentifier(new NodeIdentifierWithPredicates(OUTER_LIST_QNAME, ID_QNAME, outerListID2))
226 .withChild(ImmutableNodes.leafNode(ID_QNAME, outerListID2)).withChild(choiceNode2).build();
228 MapNode outerListNode = Builders.mapBuilder().withNodeIdentifier(new NodeIdentifier(OUTER_LIST_QNAME))
229 .withChild(outerListEntryNode1).withChild(outerListEntryNode2).build();
231 UnkeyedListEntryNode unkeyedListEntryNode1 = Builders.unkeyedListEntryBuilder()
232 .withNodeIdentifier(new NodeIdentifier(UNKEYED_LIST_QNAME))
233 .withChild(ImmutableNodes.leafNode(NAME_QNAME, "unkeyed1")).build();
235 UnkeyedListEntryNode unkeyedListEntryNode2 = Builders.unkeyedListEntryBuilder()
236 .withNodeIdentifier(new NodeIdentifier(UNKEYED_LIST_QNAME))
237 .withChild(ImmutableNodes.leafNode(NAME_QNAME, "unkeyed2")).build();
239 UnkeyedListNode unkeyedListNode = Builders.unkeyedListBuilder()
240 .withNodeIdentifier(new NodeIdentifier(UNKEYED_LIST_QNAME)).withChild(unkeyedListEntryNode1)
241 .withChild(unkeyedListEntryNode2).build();
243 ContainerNode testContainerNode = Builders.containerBuilder()
244 .withNodeIdentifier(new NodeIdentifier(TEST_QNAME)).withChild(outerListNode).withChild(unkeyedListNode)
247 Node<?> legacyNode = DataNormalizer.toLegacy(testContainerNode);
255 expectSimpleNode(ID_QNAME, outerListID1),
256 expectSimpleNode(TWO_QNAME, "two"),
257 expectSimpleNode(THREE_QNAME, "three"),
259 expectCompositeNode(INNER_LIST_QNAME, expectSimpleNode(NAME_QNAME, "inner-name1"),
260 expectSimpleNode(VALUE_QNAME, "inner-value1")),
262 expectCompositeNode(INNER_LIST_QNAME, expectSimpleNode(NAME_QNAME, "inner-name2"),
263 expectSimpleNode(VALUE_QNAME, "inner-value2"))),
264 expectCompositeNode(OUTER_LIST_QNAME, expectSimpleNode(ID_QNAME, outerListID2),
265 expectSimpleNode(ONE_QNAME, "one")),
266 expectCompositeNode(UNKEYED_LIST_QNAME, expectSimpleNode(NAME_QNAME, "unkeyed1")),
267 expectCompositeNode(UNKEYED_LIST_QNAME, expectSimpleNode(NAME_QNAME, "unkeyed2"))));
269 // Conversion of Mixin type nodes is not supported.
271 assertNull("Expected null returned for Mixin type node", DataNormalizer.toLegacy(outerListNode));
275 * Following data are constructed: <any-xml-data> <inner>
276 * <inner-leaf>inner-leaf-value</inner-leaf> </inner>
277 * <leaf>leaf-value</leaf> <any-xml-data>
281 public void testToLegacyNormalizedNodeWithAnyXml() {
283 Node<?> innerLeafChild = NodeFactory.createImmutableSimpleNode(ANY_XML_INNER_LEAF_QNAME, null,
285 CompositeNode innerContainer = NodeFactory.createImmutableCompositeNode(ANY_XML_INNER_QNAME, null,
286 Collections.<Node<?>> singletonList(innerLeafChild));
288 Node<?> leafChild = NodeFactory.createImmutableSimpleNode(ANY_XML_LEAF_QNAME, null, "leaf-value");
289 CompositeNode anyXmlNodeValue = NodeFactory.createImmutableCompositeNode(ANY_XML_DATA_QNAME, null,
290 Arrays.asList(leafChild, innerContainer));
292 AnyXmlNode testAnyXmlNode = Builders.anyXmlBuilder().withNodeIdentifier(new NodeIdentifier(TEST_QNAME)).build();
294 ContainerNode testContainerNode = Builders.containerBuilder()
295 .withNodeIdentifier(new NodeIdentifier(TEST_QNAME)).withChild(testAnyXmlNode).build();
297 DataNormalizer normalizer = new DataNormalizer(createTestContext());
298 Node<?> legacyNode = normalizer.toLegacy(YangInstanceIdentifier.builder().node(TEST_QNAME).build(), testContainerNode);
306 expectSimpleNode(ANY_XML_LEAF_QNAME, "leaf-value"),
307 expectCompositeNode(ANY_XML_INNER_QNAME,
308 expectSimpleNode(ANY_XML_INNER_LEAF_QNAME, "inner-leaf-value")))));
312 public void testToLegacyNormalizedNodeWithLeafLists() {
314 CompositeNodeBuilder<ImmutableCompositeNode> testBuilder = ImmutableCompositeNode.builder();
315 testBuilder.setQName(TEST_QNAME);
317 ListNodeBuilder<Object, LeafSetEntryNode<Object>> leafSetBuilder = Builders.leafSetBuilder()
318 .withNodeIdentifier(new NodeIdentifier(UNORDERED_LEAF_LIST_QNAME));
319 for (int i = 1; i <= 3; i++) {
320 leafSetBuilder.withChildValue("unordered-value" + i);
323 ListNodeBuilder<Object, LeafSetEntryNode<Object>> orderedLeafSetBuilder = Builders.orderedLeafSetBuilder()
324 .withNodeIdentifier(new NodeIdentifier(ORDERED_LEAF_LIST_QNAME));
325 for (int i = 3; i > 0; i--) {
326 orderedLeafSetBuilder.withChildValue("ordered-value" + i);
329 ContainerNode testContainerNode = Builders.containerBuilder()
330 .withNodeIdentifier(new NodeIdentifier(TEST_QNAME)).withChild(leafSetBuilder.build())
331 .withChild(orderedLeafSetBuilder.build()).build();
333 DataNormalizer normalizer = new DataNormalizer(createTestContext());
335 Node<?> legacyNode = normalizer.toLegacy(YangInstanceIdentifier.builder().node(TEST_QNAME).build(), testContainerNode);
339 expectCompositeNode(TEST_QNAME, expectSimpleNode(UNORDERED_LEAF_LIST_QNAME, "unordered-value1"),
340 expectSimpleNode(UNORDERED_LEAF_LIST_QNAME, "unordered-value2"),
341 expectSimpleNode(UNORDERED_LEAF_LIST_QNAME, "unordered-value3"),
342 expectSimpleNode(ORDERED_LEAF_LIST_QNAME, "ordered-value3"),
343 expectSimpleNode(ORDERED_LEAF_LIST_QNAME, "ordered-value2"),
344 expectSimpleNode(ORDERED_LEAF_LIST_QNAME, "ordered-value1")));
348 public void testToLegacyNormalizedNodeWithAugmentation() {
350 AugmentationNode augmentationNode = Builders.augmentationBuilder()
351 .withNodeIdentifier(new AugmentationIdentifier(Sets.newHashSet(AUGMENTED_LEAF_QNAME)))
352 .withChild(ImmutableNodes.leafNode(AUGMENTED_LEAF_QNAME, "augmented-value")).build();
354 ContainerNode outerContainerNode = Builders.containerBuilder()
355 .withNodeIdentifier(new NodeIdentifier(OUTER_CONTAINER_QNAME)).withChild(augmentationNode).build();
357 ContainerNode testContainerNode = Builders.containerBuilder()
358 .withNodeIdentifier(new NodeIdentifier(TEST_QNAME)).withChild(outerContainerNode).build();
360 DataNormalizer normalizer = new DataNormalizer(createTestContext());
362 Node<?> legacyNode = normalizer.toLegacy(YangInstanceIdentifier.builder().node(TEST_QNAME).build(), testContainerNode);
368 expectCompositeNode(OUTER_CONTAINER_QNAME,
369 expectSimpleNode(AUGMENTED_LEAF_QNAME, "augmented-value"))));
372 private boolean isOrdered(final QName nodeName) {
373 return ORDERED_LEAF_LIST_QNAME.equals(nodeName) || INNER_LIST_QNAME.equals(nodeName);
376 @SuppressWarnings("unchecked")
377 private void verifyLegacyNode(final Node<?> actual, final LegacyNodeData expNodeData) {
379 assertNotNull("Actual Node is null", actual);
380 assertTrue("Expected CompositeNode instance", actual instanceof CompositeNode);
381 CompositeNode actualCN = (CompositeNode) actual;
382 assertEquals("Node key", expNodeData.nodeKey, actualCN.getKey());
384 List<LegacyNodeData> expChildData = Lists.newArrayList();
385 List<LegacyNodeData> unorderdChildData = Lists.newArrayList();
386 for (LegacyNodeData data : (List<LegacyNodeData>) expNodeData.nodeData) {
387 if (isOrdered(data.nodeKey)) {
388 expChildData.add(data);
390 unorderdChildData.add(data);
394 Collections.sort(unorderdChildData, new Comparator<LegacyNodeData>() {
396 public int compare(final LegacyNodeData arg1, final LegacyNodeData arg2) {
397 if (!(arg1.nodeData instanceof List) && !(arg2.nodeData instanceof List)) {
398 // if neither is a list, just compare them
399 String str1 = arg1.nodeKey.getLocalName() + arg1.nodeData;
400 String str2 = arg2.nodeKey.getLocalName() + arg2.nodeData;
401 return str1.compareTo(str2);
402 } else if (arg1.nodeData instanceof List && arg2.nodeData instanceof List) {
403 // if both are lists, first check their local name
404 String str1 = arg1.nodeKey.getLocalName();
405 String str2 = arg2.nodeKey.getLocalName();
406 if (!str1.equals(str2)) {
407 return str1.compareTo(str2);
409 // if local names are the same, then look at the list contents
410 List<LegacyNodeData> l1 = (List<LegacyNodeData>) arg1.nodeData;
411 List<LegacyNodeData> l2 = (List<LegacyNodeData>) arg2.nodeData;
413 if (l1.size() != l2.size()) {
414 // if the sizes are different, use that
415 return l2.size() - l1.size();
417 // lastly sort and recursively check the list contents
418 Collections.sort(l1, this);
419 Collections.sort(l2, this);
421 for (int i = 0 ; i < l1.size() ; i++) {
422 int diff = this.compare(l1.get(i), l2.get(i));
430 } else if( arg1.nodeData instanceof List ) {
438 expChildData.addAll(unorderdChildData);
440 List<Node<?>> actualChildNodes = Lists.newArrayList();
441 List<Node<?>> unorderedChildNodes = Lists.newArrayList();
442 for (Node<?> node : actualCN.getValue()) {
443 if (isOrdered(node.getKey())) {
444 actualChildNodes.add(node);
446 unorderedChildNodes.add(node);
450 Collections.sort(unorderedChildNodes, new Comparator<Node<?>>() {
452 public int compare(final Node<?> n1, final Node<?> n2) {
453 if (n1 instanceof SimpleNode && n2 instanceof SimpleNode) {
454 // if they're SimpleNodes just compare their strings
455 String str1 = n1.getKey().getLocalName() + ((SimpleNode<?>)n1).getValue();
456 String str2 = n2.getKey().getLocalName() + ((SimpleNode<?>)n2).getValue();
457 return str1.compareTo(str2);
458 } else if (n1 instanceof CompositeNode && n2 instanceof CompositeNode) {
459 // if they're CompositeNodes, things are more interesting
460 String str1 = n1.getKey().getLocalName();
461 String str2 = n2.getKey().getLocalName();
462 if (!str1.equals(str2)) {
463 // if their local names differ, return that difference
464 return str1.compareTo(str2);
466 // otherwise, we need to look at their contents
467 ArrayList<Node<?>> l1 = new ArrayList<Node<?>>( ((CompositeNode)n1).getValue() );
468 ArrayList<Node<?>> l2 = new ArrayList<Node<?>>( ((CompositeNode)n2).getValue() );
470 if (l1.size() != l2.size()) {
471 // if they have different numbers of things in them return that
472 return l2.size() - l1.size();
474 // otherwise, compare the individual elements, first sort them
475 Collections.sort(l1, this);
476 Collections.sort(l2, this);
478 // then compare them individually
479 for(int i = 0 ; i < l2.size() ; i++) {
480 int diff = this.compare(l1.get(i), l2.get(i));
488 } else if (n1 instanceof CompositeNode && n2 instanceof SimpleNode) {
490 } else if (n2 instanceof CompositeNode && n1 instanceof SimpleNode) {
493 assertTrue("Expected either SimpleNodes CompositeNodes", false);
499 actualChildNodes.addAll(unorderedChildNodes);
501 for (Node<?> actualChild : actualChildNodes) {
502 LegacyNodeData expData = expChildData.isEmpty() ? null : expChildData.remove(0);
503 assertNotNull("Unexpected child node with key " + actualChild.getKey(), expData);
504 assertEquals("Child node QName", expData.nodeKey, actualChild.getKey());
506 if (expData.nodeData instanceof List) { // List represents a
508 verifyLegacyNode(actualChild, expData);
509 } else { // else a simple node
510 assertTrue("Expected SimpleNode instance", actualChild instanceof SimpleNode);
511 assertEquals("Child node value with key " + actualChild.getKey(), expData.nodeData,
512 ((SimpleNode<?>) actualChild).getValue());
516 if (!expChildData.isEmpty()) {
517 fail("Missing child nodes: " + expChildData);
521 private LegacyNodeData expectCompositeNode(final QName key, final LegacyNodeData... childData) {
522 return new LegacyNodeData(key, Lists.newArrayList(childData));
525 private LegacyNodeData expectSimpleNode(final QName key, final Object value) {
526 return new LegacyNodeData(key, value);
530 public void testToNormalizedCompositeNode() {
531 SchemaContext testCtx = createTestContext();
532 DataNormalizer normalizer = new DataNormalizer(testCtx);
534 CompositeNodeBuilder<ImmutableCompositeNode> testBuilder = ImmutableCompositeNode.builder();
535 testBuilder.setQName(TEST_QNAME);
537 CompositeNodeBuilder<ImmutableCompositeNode> outerListBuilder = ImmutableCompositeNode.builder();
538 outerListBuilder.setQName(OUTER_LIST_QNAME);
539 outerListBuilder.addLeaf(ID_QNAME, 10);
540 outerListBuilder.addLeaf(ONE_QNAME, "one");
542 for (int i = 3; i > 0; i--) {
543 CompositeNodeBuilder<ImmutableCompositeNode> innerListBuilder = ImmutableCompositeNode.builder();
544 innerListBuilder.setQName(INNER_LIST_QNAME);
545 innerListBuilder.addLeaf(NAME_QNAME, "inner-name" + i);
546 innerListBuilder.addLeaf(VALUE_QNAME, "inner-value" + i);
547 outerListBuilder.add(innerListBuilder.toInstance());
550 testBuilder.add(outerListBuilder.toInstance());
552 outerListBuilder = ImmutableCompositeNode.builder();
553 outerListBuilder.setQName(OUTER_LIST_QNAME);
554 outerListBuilder.addLeaf(ID_QNAME, 20);
555 outerListBuilder.addLeaf(TWO_QNAME, "two");
556 outerListBuilder.addLeaf(THREE_QNAME, "three");
557 testBuilder.add(outerListBuilder.toInstance());
559 for (int i = 1; i <= 2; i++) {
560 CompositeNodeBuilder<ImmutableCompositeNode> unkeyedListBuilder = ImmutableCompositeNode.builder();
561 unkeyedListBuilder.setQName(UNKEYED_LIST_QNAME);
562 unkeyedListBuilder.addLeaf(NAME_QNAME, "unkeyed-name" + i);
563 testBuilder.add(unkeyedListBuilder.toInstance());
566 Entry<YangInstanceIdentifier, NormalizedNode<?, ?>> normalizedNodeEntry = normalizer
567 .toNormalized(new AbstractMap.SimpleEntry<YangInstanceIdentifier, CompositeNode>(YangInstanceIdentifier.create(
568 ImmutableList.<PathArgument> of(new NodeIdentifier(TEST_QNAME))), testBuilder.toInstance()));
570 verifyNormalizedInstanceIdentifier(normalizedNodeEntry.getKey(), TEST_QNAME);
572 verifyNormalizedNode(
573 normalizedNodeEntry.getValue(),
582 expectLeafNode(ID_QNAME, 10),
583 expectChoiceNode(OUTER_CHOICE_QNAME, expectLeafNode(ONE_QNAME, "one")),
584 expectOrderedMapNode(
586 expectMapEntryNode(INNER_LIST_QNAME, NAME_QNAME, "inner-name3",
587 expectLeafNode(NAME_QNAME, "inner-name3"),
588 expectLeafNode(VALUE_QNAME, "inner-value3")),
589 expectMapEntryNode(INNER_LIST_QNAME, NAME_QNAME, "inner-name2",
590 expectLeafNode(NAME_QNAME, "inner-name2"),
591 expectLeafNode(VALUE_QNAME, "inner-value2")),
592 expectMapEntryNode(INNER_LIST_QNAME, NAME_QNAME, "inner-name1",
593 expectLeafNode(NAME_QNAME, "inner-name1"),
594 expectLeafNode(VALUE_QNAME, "inner-value1")))),
599 expectLeafNode(ID_QNAME, 20),
600 expectChoiceNode(OUTER_CHOICE_QNAME, expectLeafNode(TWO_QNAME, "two"),
601 expectLeafNode(THREE_QNAME, "three")))),
602 expectUnkeyedListNode(
604 expectUnkeyedListEntryNode(UNKEYED_LIST_QNAME,
605 expectLeafNode(NAME_QNAME, "unkeyed-name1")),
606 expectUnkeyedListEntryNode(UNKEYED_LIST_QNAME,
607 expectLeafNode(NAME_QNAME, "unkeyed-name2")))));
612 public void testToNormalizedCompositeNodeWithAnyXml() {
613 SchemaContext testCtx = createTestContext();
614 DataNormalizer normalizer = new DataNormalizer(testCtx);
616 CompositeNodeBuilder<ImmutableCompositeNode> testBuilder = ImmutableCompositeNode.builder();
617 testBuilder.setQName(TEST_QNAME);
619 CompositeNodeBuilder<ImmutableCompositeNode> anyXmlBuilder = ImmutableCompositeNode.builder();
620 anyXmlBuilder.setQName(ANY_XML_DATA_QNAME);
621 anyXmlBuilder.addLeaf(ANY_XML_LEAF_QNAME, "leaf-value");
623 CompositeNodeBuilder<ImmutableCompositeNode> innerBuilder = ImmutableCompositeNode.builder();
624 innerBuilder.setQName(ANY_XML_INNER_QNAME);
625 innerBuilder.addLeaf(ANY_XML_INNER_LEAF_QNAME, "inner-leaf-value");
627 anyXmlBuilder.add(innerBuilder.toInstance());
628 CompositeNode anyXmlLegacy = anyXmlBuilder.toInstance();
629 testBuilder.add(anyXmlLegacy);
631 Entry<YangInstanceIdentifier, NormalizedNode<?, ?>> normalizedNodeEntry = normalizer
632 .toNormalized(new AbstractMap.SimpleEntry<YangInstanceIdentifier, CompositeNode>(YangInstanceIdentifier.create(
633 ImmutableList.<PathArgument> of(new NodeIdentifier(TEST_QNAME))), testBuilder.toInstance()));
635 verifyNormalizedInstanceIdentifier(normalizedNodeEntry.getKey(), TEST_QNAME);
637 verifyNormalizedNode(normalizedNodeEntry.getValue(),
638 expectContainerNode(TEST_QNAME, expectAnyXmlNode(ANY_XML_DATA_QNAME, anyXmlLegacy)));
642 public void testToNormalizedCompositeNodeWithAugmentation() {
643 SchemaContext testCtx = createTestContext();
644 DataNormalizer normalizer = new DataNormalizer(testCtx);
646 CompositeNodeBuilder<ImmutableCompositeNode> testBuilder = ImmutableCompositeNode.builder();
647 testBuilder.setQName(TEST_QNAME);
649 CompositeNodeBuilder<ImmutableCompositeNode> outerContBuilder = ImmutableCompositeNode.builder();
650 outerContBuilder.setQName(OUTER_CONTAINER_QNAME);
651 outerContBuilder.addLeaf(AUGMENTED_LEAF_QNAME, "augmented-value");
653 testBuilder.add(outerContBuilder.toInstance());
655 Entry<YangInstanceIdentifier, NormalizedNode<?, ?>> normalizedNodeEntry = normalizer
656 .toNormalized(new AbstractMap.SimpleEntry<YangInstanceIdentifier, CompositeNode>(YangInstanceIdentifier.create(
657 ImmutableList.<PathArgument> of(new NodeIdentifier(TEST_QNAME))), testBuilder.toInstance()));
659 verifyNormalizedInstanceIdentifier(normalizedNodeEntry.getKey(), TEST_QNAME);
661 NormalizedNodeData expAugmentation = expectAugmentation(AUGMENTED_LEAF_QNAME,
662 expectLeafNode(AUGMENTED_LEAF_QNAME, "augmented-value"));
664 verifyNormalizedNode(normalizedNodeEntry.getValue(),
665 expectContainerNode(TEST_QNAME, expectContainerNode(OUTER_CONTAINER_QNAME, expAugmentation)));
667 normalizedNodeEntry = normalizer.toNormalized(new AbstractMap.SimpleEntry<YangInstanceIdentifier, CompositeNode>(
668 YangInstanceIdentifier.create(Lists.newArrayList(new NodeIdentifier(TEST_QNAME), new NodeIdentifier(
669 OUTER_CONTAINER_QNAME))), outerContBuilder.toInstance()));
674 public void testToNormalizedCompositeNodeWithLeafLists() {
675 SchemaContext testCtx = createTestContext();
676 DataNormalizer normalizer = new DataNormalizer(testCtx);
678 CompositeNodeBuilder<ImmutableCompositeNode> testBuilder = ImmutableCompositeNode.builder();
679 testBuilder.setQName(TEST_QNAME);
681 for (int i = 1; i <= 3; i++) {
682 testBuilder.addLeaf(UNORDERED_LEAF_LIST_QNAME, "unordered-value" + i);
685 for (int i = 3; i > 0; i--) {
686 testBuilder.addLeaf(ORDERED_LEAF_LIST_QNAME, "ordered-value" + i);
689 Entry<YangInstanceIdentifier, NormalizedNode<?, ?>> normalizedNodeEntry = normalizer
690 .toNormalized(new AbstractMap.SimpleEntry<YangInstanceIdentifier, CompositeNode>(YangInstanceIdentifier.create(
691 ImmutableList.<PathArgument> of(new NodeIdentifier(TEST_QNAME))), testBuilder.toInstance()));
693 verifyNormalizedInstanceIdentifier(normalizedNodeEntry.getKey(), TEST_QNAME);
695 verifyNormalizedNode(
696 normalizedNodeEntry.getValue(),
699 expectLeafSetNode(UNORDERED_LEAF_LIST_QNAME,
700 expectLeafSetEntryNode(UNORDERED_LEAF_LIST_QNAME, "unordered-value1"),
701 expectLeafSetEntryNode(UNORDERED_LEAF_LIST_QNAME, "unordered-value2"),
702 expectLeafSetEntryNode(UNORDERED_LEAF_LIST_QNAME, "unordered-value3")),
703 expectOrderedLeafSetNode(ORDERED_LEAF_LIST_QNAME,
704 expectLeafSetEntryNode(ORDERED_LEAF_LIST_QNAME, "ordered-value3"),
705 expectLeafSetEntryNode(ORDERED_LEAF_LIST_QNAME, "ordered-value2"),
706 expectLeafSetEntryNode(ORDERED_LEAF_LIST_QNAME, "ordered-value1"))));
709 @SuppressWarnings("unchecked")
710 private void verifyNormalizedNode(final NormalizedNode<?, ?> actual, final NormalizedNodeData expNodeData) {
712 Class<?> expNodeClass = expNodeData.nodeClass;
713 PathArgument expNodeID = expNodeData.nodeID;
715 assertNotNull("Actual NormalizedNode is null", actual);
716 assertTrue("NormalizedNode instance " + actual.getClass() + " is not derived from " + expNodeClass,
717 expNodeClass.isAssignableFrom(actual.getClass()));
718 assertEquals("NormalizedNode identifier", expNodeID, actual.getIdentifier());
720 if (expNodeData.nodeData instanceof List) {
721 Map<PathArgument, Integer> orderingMap = null;
722 if (expNodeClass.equals(OrderedMapNode.class) || expNodeClass.equals(OrderedLeafSetNode.class)) {
723 orderingMap = Maps.newHashMap();
727 Map<PathArgument, NormalizedNodeData> expChildDataMap = Maps.newHashMap();
728 List<NormalizedNodeData> expChildDataList = (List<NormalizedNodeData>) expNodeData.nodeData;
729 for (NormalizedNodeData data : expChildDataList) {
730 expChildDataMap.put(data.nodeID, data);
732 if (orderingMap != null) {
733 orderingMap.put(data.nodeID, i++);
737 assertNotNull("Actual value is null for node " + actual.getIdentifier(), actual.getValue());
738 assertTrue("Expected value instance Iterable for node " + actual.getIdentifier(),
739 Iterable.class.isAssignableFrom(actual.getValue().getClass()));
742 for (NormalizedNode<?, ?> actualChild : (Iterable<NormalizedNode<?, ?>>) actual.getValue()) {
743 NormalizedNodeData expChildData = expNodeClass.equals(UnkeyedListNode.class) ? expChildDataList
744 .remove(0) : expChildDataMap.remove(actualChild.getIdentifier());
747 "Unexpected child node " + actualChild.getClass() + " with identifier "
748 + actualChild.getIdentifier() + " for parent node " + actual.getClass()
749 + " with identifier " + actual.getIdentifier(), expChildData);
751 if (orderingMap != null) {
752 assertEquals("Order index for child node " + actualChild.getIdentifier(),
753 orderingMap.get(actualChild.getIdentifier()), Integer.valueOf(i));
756 verifyNormalizedNode(actualChild, expChildData);
760 if (expNodeClass.equals(UnkeyedListNode.class)) {
761 if (expChildDataList.size() > 0) {
762 fail("Missing " + expChildDataList.size() + " child nodes for parent " + actual.getIdentifier());
765 if (!expChildDataMap.isEmpty()) {
766 fail("Missing child nodes for parent " + actual.getIdentifier() + ": " + expChildDataMap.keySet());
770 assertEquals("Leaf value for node " + actual.getIdentifier(), expNodeData.nodeData, actual.getValue());
774 private NormalizedNodeData expectOrderedLeafSetNode(final QName nodeName, final NormalizedNodeData... childData) {
775 return new NormalizedNodeData(new NodeIdentifier(nodeName), OrderedLeafSetNode.class,
776 Lists.newArrayList(childData));
779 private NormalizedNodeData expectLeafSetNode(final QName nodeName, final NormalizedNodeData... childData) {
780 return new NormalizedNodeData(new NodeIdentifier(nodeName), LeafSetNode.class, Lists.newArrayList(childData));
783 private NormalizedNodeData expectLeafSetEntryNode(final QName nodeName, final Object value) {
784 return new NormalizedNodeData(new NodeWithValue(nodeName, value), LeafSetEntryNode.class, value);
787 private NormalizedNodeData expectUnkeyedListNode(final QName nodeName, final NormalizedNodeData... childData) {
788 return new NormalizedNodeData(new NodeIdentifier(nodeName), UnkeyedListNode.class,
789 Lists.newArrayList(childData));
792 private NormalizedNodeData expectUnkeyedListEntryNode(final QName nodeName, final NormalizedNodeData... childData) {
793 return new NormalizedNodeData(new NodeIdentifier(nodeName), UnkeyedListEntryNode.class,
794 Lists.newArrayList(childData));
797 private NormalizedNodeData expectAugmentation(final QName augmentedNodeName, final NormalizedNodeData... childData) {
798 return new NormalizedNodeData(new AugmentationIdentifier(Sets.newHashSet(augmentedNodeName)),
799 AugmentationNode.class, Lists.newArrayList(childData));
802 private NormalizedNodeData expectAnyXmlNode(final QName nodeName, final Object value) {
803 return new NormalizedNodeData(new NodeIdentifier(nodeName), AnyXmlNode.class, value);
806 private NormalizedNodeData expectContainerNode(final QName nodeName, final NormalizedNodeData... childData) {
807 return new NormalizedNodeData(new NodeIdentifier(nodeName), ContainerNode.class, Lists.newArrayList(childData));
810 private NormalizedNodeData expectChoiceNode(final QName nodeName, final NormalizedNodeData... childData) {
811 return new NormalizedNodeData(new NodeIdentifier(nodeName), ChoiceNode.class, Lists.newArrayList(childData));
814 private NormalizedNodeData expectLeafNode(final QName nodeName, final Object value) {
815 return new NormalizedNodeData(new NodeIdentifier(nodeName), LeafNode.class, value);
819 private NormalizedNodeData expectMapEntryNode(final QName nodeName, final QName key, final Object value,
820 final NormalizedNodeData... childData) {
821 return new NormalizedNodeData(new NodeIdentifierWithPredicates(nodeName, key, value), MapEntryNode.class,
822 Lists.newArrayList(childData));
825 private NormalizedNodeData expectMapNode(final QName key, final NormalizedNodeData... childData) {
826 return new NormalizedNodeData(new NodeIdentifier(key), MapNode.class, Lists.newArrayList(childData));
829 private NormalizedNodeData expectOrderedMapNode(final QName key, final NormalizedNodeData... childData) {
830 return new NormalizedNodeData(new NodeIdentifier(key), OrderedMapNode.class, Lists.newArrayList(childData));