2 * Copyright (c) 2014 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.mdsal.binding.dom.codec.test;
10 import static java.util.Collections.singleton;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotEquals;
13 import static org.junit.Assert.assertNotNull;
14 import static org.junit.Assert.fail;
15 import static org.opendaylight.mdsal.binding.test.model.util.ListsBindingUtils.top;
16 import static org.opendaylight.mdsal.binding.test.model.util.ListsBindingUtils.topLevelList;
17 import static org.opendaylight.yangtools.yang.data.impl.schema.Builders.augmentationBuilder;
18 import static org.opendaylight.yangtools.yang.data.impl.schema.Builders.choiceBuilder;
19 import static org.opendaylight.yangtools.yang.data.impl.schema.Builders.containerBuilder;
20 import static org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes.leafNode;
21 import static org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes.mapEntry;
22 import static org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes.mapEntryBuilder;
23 import static org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes.mapNodeBuilder;
25 import com.google.common.collect.Maps;
26 import com.google.common.collect.Sets;
27 import java.util.ArrayList;
28 import java.util.Collections;
29 import java.util.HashSet;
30 import java.util.List;
33 import javassist.ClassPool;
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.opendaylight.mdsal.binding.dom.codec.gen.impl.StreamWriterGenerator;
37 import org.opendaylight.mdsal.binding.dom.codec.impl.BindingNormalizedNodeCodecRegistry;
38 import org.opendaylight.mdsal.binding.generator.util.JavassistUtils;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.augment.rev140709.TopChoiceAugment1;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.augment.rev140709.TopChoiceAugment1Builder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.augment.rev140709.TopChoiceAugment2;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.augment.rev140709.TopChoiceAugment2Builder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.augment.rev140709.TreeComplexUsesAugment;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.augment.rev140709.TreeLeafOnlyAugment;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.augment.rev140709.TreeLeafOnlyAugmentBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.augment.rev140709.top.AugmentChoice1;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.augment.rev140709.top.augment.choice1.Case1Builder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.augment.rev140709.top.augment.choice1.case1.augment.choice2.Case11Builder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.augment.rev140709.top.augment.choice1.case1.augment.choice2.case11.Case11ChoiceCaseContainerBuilder;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.ChoiceContainer;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.ChoiceContainerBuilder;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.Top;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.Top1;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.Top1Builder;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.Top2;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.Top2Builder;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.TopBuilder;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.choice.identifier.ExtendedBuilder;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.choice.identifier.extended.ExtendedIdBuilder;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.two.level.list.TopLevelList;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.two.level.list.TopLevelListBuilder;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.two.level.list.TopLevelListKey;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.two.level.list.top.level.list.NestedList;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.two.level.list.top.level.list.NestedListBuilder;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.two.level.list.top.level.list.NestedListKey;
66 import org.opendaylight.yang.gen.v1.urn.test.foo4798.rev160101.Root;
67 import org.opendaylight.yangtools.yang.binding.Augmentation;
68 import org.opendaylight.yangtools.yang.binding.DataObject;
69 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
70 import org.opendaylight.yangtools.yang.common.QName;
71 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
72 import org.opendaylight.yangtools.yang.data.api.schema.AugmentationNode;
73 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
74 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
75 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
76 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
77 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeAttrBuilder;
78 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableAugmentationNodeBuilder;
79 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableChoiceNodeBuilder;
80 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
81 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafSetEntryNodeBuilder;
82 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafSetNodeBuilder;
83 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableMapEntryNodeBuilder;
84 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableMapNodeBuilder;
85 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableOrderedLeafSetNodeBuilder;
86 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableOrderedMapNodeBuilder;
88 public class NormalizedNodeSerializeDeserializeTest extends AbstractBindingRuntimeTest{
90 public static final String TOP_LEVEL_LIST_FOO_KEY_VALUE = "foo";
91 public static final TopLevelListKey TOP_LEVEL_LIST_FOO_KEY = new TopLevelListKey(TOP_LEVEL_LIST_FOO_KEY_VALUE);
93 public static final QName TOP_QNAME = Top.QNAME;
94 public static final QName TOP_LEVEL_LIST_QNAME = QName.create(TOP_QNAME, "top-level-list");
95 public static final QName TOP_LEVEL_LIST_KEY_QNAME = QName.create(TOP_QNAME, "name");
96 public static final QName TOP_LEVEL_LEAF_LIST_QNAME = QName.create(TOP_QNAME, "top-level-leaf-list");
97 public static final QName TOP_LEVEL_ORDERED_LEAF_LIST_QNAME = QName.create(TOP_QNAME, "top-level-ordered-leaf-list");
98 public static final QName NESTED_LIST_QNAME = QName.create(TOP_QNAME, "nested-list");
99 public static final QName NESTED_LIST_KEY_QNAME = QName.create(TOP_QNAME, "name");
100 public static final QName CHOICE_CONTAINER_QNAME = ChoiceContainer.QNAME;
101 public static final QName CHOICE_IDENTIFIER_QNAME = QName.create(CHOICE_CONTAINER_QNAME, "identifier");
102 public static final QName CHOICE_IDENTIFIER_ID_QNAME = QName.create(CHOICE_CONTAINER_QNAME, "id");
103 public static final QName SIMPLE_ID_QNAME = QName.create(CHOICE_CONTAINER_QNAME, "simple-id");
104 public static final QName EXTENDED_ID_QNAME = QName.create(CHOICE_CONTAINER_QNAME, "extended-id");
105 private static final QName SIMPLE_VALUE_QNAME = QName.create(TreeComplexUsesAugment.QNAME, "simple-value");
107 private static final InstanceIdentifier<TopLevelList> BA_TOP_LEVEL_LIST = InstanceIdentifier
108 .builder(Top.class).child(TopLevelList.class, TOP_LEVEL_LIST_FOO_KEY).build();
109 private static final InstanceIdentifier<TreeLeafOnlyAugment> BA_TREE_LEAF_ONLY =
110 BA_TOP_LEVEL_LIST.augmentation(TreeLeafOnlyAugment.class);
111 private static final InstanceIdentifier<TreeComplexUsesAugment> BA_TREE_COMPLEX_USES =
112 BA_TOP_LEVEL_LIST.augmentation(TreeComplexUsesAugment.class);
114 public static final YangInstanceIdentifier BI_TOP_PATH = YangInstanceIdentifier.of(TOP_QNAME);
115 public static final YangInstanceIdentifier BI_TOP_LEVEL_LIST_PATH = BI_TOP_PATH.node(TOP_LEVEL_LIST_QNAME);
116 public static final YangInstanceIdentifier BI_TOP_LEVEL_LIST_FOO_PATH = BI_TOP_LEVEL_LIST_PATH
117 .node(new YangInstanceIdentifier.NodeIdentifierWithPredicates(TOP_LEVEL_LIST_QNAME, TOP_LEVEL_LIST_KEY_QNAME, TOP_LEVEL_LIST_FOO_KEY_VALUE));
118 public static final YangInstanceIdentifier BI_CHOICE_CONTAINER_PATH = YangInstanceIdentifier.of(CHOICE_CONTAINER_QNAME);
120 private BindingNormalizedNodeCodecRegistry registry;
124 public void setup() {
126 final JavassistUtils utils = JavassistUtils.forClassPool(ClassPool.getDefault());
127 registry = new BindingNormalizedNodeCodecRegistry(StreamWriterGenerator.create(utils));
128 registry.onBindingRuntimeContextUpdated(getRuntimeContext());
132 public void containerToNormalized() {
133 final Map.Entry<YangInstanceIdentifier, NormalizedNode<?, ?>> entry =
134 registry.toNormalizedNode(InstanceIdentifier.create(Top.class), top());
135 final ContainerNode topNormalized = getEmptyTop();
136 assertEquals(topNormalized, entry.getValue());
140 public void containerFromNormalized() {
141 final ContainerNode topNormalized = getEmptyTop();
142 final Map.Entry<InstanceIdentifier<?>, DataObject> entry = registry.fromNormalizedNode(BI_TOP_PATH, topNormalized);
143 assertEquals(top(), entry.getValue());
146 private static ContainerNode getEmptyTop() {
147 return ImmutableContainerNodeBuilder.create()
148 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TOP_QNAME))
149 .withChild(mapNodeBuilder(TOP_LEVEL_LIST_QNAME).build()).build();
152 private static final QName AGUMENT_STRING_Q = QName.create(TOP_QNAME, "augmented-string");
153 private static final String AUGMENT_STRING_VALUE = "testingEquals";
154 private static final QName AUGMENT_INT_Q = QName.create(TOP_QNAME, "augmented-int");
155 private static final int AUGMENT_INT_VALUE = 44;
158 public void equalsWithAugment() {
159 final ContainerNode topNormalizedWithAugments = getNormalizedTopWithAugments(
160 augmentationBuilder()
161 .withNodeIdentifier(new YangInstanceIdentifier.AugmentationIdentifier(singleton(AGUMENT_STRING_Q)))
162 .withChild(ImmutableNodes.leafNode(AGUMENT_STRING_Q, AUGMENT_STRING_VALUE))
164 final ContainerNode topNormalized = getEmptyTop();
166 final Map.Entry<InstanceIdentifier<?>, DataObject> entry = registry.fromNormalizedNode(BI_TOP_PATH, topNormalized);
167 final Map.Entry<InstanceIdentifier<?>, DataObject> entryWithAugments = registry.fromNormalizedNode(BI_TOP_PATH, topNormalizedWithAugments);
169 // Equals on other with no augmentation should be false
170 assertNotEquals(top(), entryWithAugments.getValue());
171 // Equals on other(reversed) with no augmentation should be false
172 assertNotEquals(entryWithAugments.getValue(), top());
173 // Equals on other(lazy) with no augmentation should be false
174 assertNotEquals(entry.getValue(), entryWithAugments.getValue());
175 // Equals on other(lazy, reversed) with no augmentation should be false
176 assertNotEquals(entryWithAugments.getValue(), entry.getValue());
178 final Top topWithAugments = topWithAugments(Collections.<Class<? extends Augmentation<Top>>, Augmentation<Top>>
179 singletonMap(Top1.class, new Top1Builder().setAugmentedString(AUGMENT_STRING_VALUE).build()));
180 // Equals other with same augment should be true
181 assertEquals(topWithAugments, entryWithAugments.getValue());
182 // Equals other with same augment should be true
183 assertEquals(entryWithAugments.getValue(), topWithAugments);
184 // Equals on self should be true
185 assertEquals(entryWithAugments.getValue(), entryWithAugments.getValue());
187 final Top topWithAugmentsDiffValue = topWithAugments(Collections.<Class<? extends Augmentation<Top>>, Augmentation<Top>>
188 singletonMap(Top1.class, new Top1Builder().setAugmentedString("differentValue").build()));
189 assertNotEquals(topWithAugmentsDiffValue, entryWithAugments.getValue());
190 assertNotEquals(entryWithAugments.getValue(), topWithAugmentsDiffValue);
194 public void equalsWithMultipleAugments() {
195 final ContainerNode topNormalizedWithAugments = getNormalizedTopWithAugments(
196 augmentationBuilder()
198 new YangInstanceIdentifier.AugmentationIdentifier(singleton(AGUMENT_STRING_Q)))
199 .withChild(ImmutableNodes.leafNode(AGUMENT_STRING_Q, AUGMENT_STRING_VALUE))
201 augmentationBuilder()
202 .withNodeIdentifier(new YangInstanceIdentifier.AugmentationIdentifier(singleton(AUGMENT_INT_Q)))
203 .withChild(ImmutableNodes.leafNode(AUGMENT_INT_Q, AUGMENT_INT_VALUE))
206 final Map.Entry<InstanceIdentifier<?>, DataObject> entryWithAugments = registry.fromNormalizedNode(BI_TOP_PATH, topNormalizedWithAugments);
207 Map<Class<? extends Augmentation<Top>>, Augmentation<Top>> augments = Maps.newHashMap();
208 augments.put(Top1.class, new Top1Builder().setAugmentedString(AUGMENT_STRING_VALUE).build());
209 augments.put(Top2.class, new Top2Builder().setAugmentedInt(AUGMENT_INT_VALUE).build());
210 Top topWithAugments = topWithAugments(augments);
212 assertEquals(topWithAugments, entryWithAugments.getValue());
213 assertEquals(entryWithAugments.getValue(), topWithAugments);
215 augments = Maps.newHashMap();
216 augments.put(Top1.class, new Top1Builder().setAugmentedString(AUGMENT_STRING_VALUE).build());
217 augments.put(Top2.class, new Top2Builder().setAugmentedInt(999).build());
218 topWithAugments = topWithAugments(augments);
220 assertNotEquals(topWithAugments, entryWithAugments.getValue());
221 assertNotEquals(entryWithAugments.getValue(), topWithAugments);
224 private static ContainerNode getNormalizedTopWithAugments(final AugmentationNode... augChild) {
225 final DataContainerNodeAttrBuilder<YangInstanceIdentifier.NodeIdentifier, ContainerNode>
226 builder = ImmutableContainerNodeBuilder.create();
228 for (AugmentationNode augmentationNode : augChild) {
229 builder.withChild(augmentationNode);
231 return builder.withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TOP_QNAME))
232 .withChild(mapNodeBuilder(TOP_LEVEL_LIST_QNAME).build()).build();
235 private static Top topWithAugments(final Map<Class<? extends Augmentation<Top>>, ? extends Augmentation<Top>> augments) {
236 final TopBuilder topBuilder = new TopBuilder();
237 for (Map.Entry<Class<? extends Augmentation<Top>>, ? extends Augmentation<Top>> augment : augments.entrySet()) {
238 topBuilder.addAugmentation(augment.getKey(), augment.getValue());
240 return topBuilder.setTopLevelList(Collections.<TopLevelList>emptyList()).build();
244 public void listWithKeysToNormalized() {
245 final Map.Entry<YangInstanceIdentifier, NormalizedNode<?, ?>> entry =
246 registry.toNormalizedNode(BA_TOP_LEVEL_LIST, topLevelList(TOP_LEVEL_LIST_FOO_KEY));
247 final MapEntryNode topLevelListNormalized = ImmutableMapEntryNodeBuilder.create()
249 new YangInstanceIdentifier.NodeIdentifierWithPredicates(
250 TOP_LEVEL_LIST_QNAME, TOP_LEVEL_LIST_KEY_QNAME, TOP_LEVEL_LIST_FOO_KEY_VALUE))
251 .withChild(leafNode(TOP_LEVEL_LIST_KEY_QNAME, TOP_LEVEL_LIST_FOO_KEY_VALUE))
253 assertEquals(topLevelListNormalized, entry.getValue());
257 public void listWithKeysFromNormalized() {
258 final MapEntryNode topLevelListNormalized = ImmutableMapEntryNodeBuilder.create()
260 new YangInstanceIdentifier.NodeIdentifierWithPredicates(
261 TOP_LEVEL_LIST_QNAME, TOP_LEVEL_LIST_KEY_QNAME, TOP_LEVEL_LIST_FOO_KEY_VALUE))
262 .withChild(leafNode(TOP_LEVEL_LIST_KEY_QNAME, TOP_LEVEL_LIST_FOO_KEY_VALUE))
264 final Map.Entry<InstanceIdentifier<?>, DataObject> entry =
265 registry.fromNormalizedNode(BI_TOP_LEVEL_LIST_FOO_PATH, topLevelListNormalized);
266 assertEquals(topLevelList(TOP_LEVEL_LIST_FOO_KEY), entry.getValue());
270 public void leafOnlyAugmentationToNormalized() {
271 final Map.Entry<YangInstanceIdentifier, NormalizedNode<?, ?>> entry =
272 registry.toNormalizedNode(BA_TREE_LEAF_ONLY, new TreeLeafOnlyAugmentBuilder().setSimpleValue("simpleValue").build());
273 final Set<QName> augmentationChildren = new HashSet<>();
274 augmentationChildren.add(SIMPLE_VALUE_QNAME);
275 final AugmentationNode augmentationNode = ImmutableAugmentationNodeBuilder.create()
276 .withNodeIdentifier(new YangInstanceIdentifier.AugmentationIdentifier(augmentationChildren))
277 .withChild(leafNode(SIMPLE_VALUE_QNAME, "simpleValue"))
279 assertEquals(augmentationNode, entry.getValue());
283 public void leafOnlyAugmentationFromNormalized() {
284 final Set<QName> augmentationChildren = new HashSet<>();
285 augmentationChildren.add(SIMPLE_VALUE_QNAME);
286 final AugmentationNode augmentationNode = ImmutableAugmentationNodeBuilder.create()
287 .withNodeIdentifier(new YangInstanceIdentifier.AugmentationIdentifier(augmentationChildren))
288 .withChild(leafNode(SIMPLE_VALUE_QNAME, "simpleValue"))
290 final Map.Entry<InstanceIdentifier<?>, DataObject> entry = registry.fromNormalizedNode(BI_TOP_LEVEL_LIST_FOO_PATH.node(
291 new YangInstanceIdentifier.AugmentationIdentifier(augmentationChildren)), augmentationNode);
292 assertEquals(new TreeLeafOnlyAugmentBuilder().setSimpleValue("simpleValue").build(), entry.getValue());
296 public void orderedleafListToNormalized() {
297 List<String> topLevelLeafList = new ArrayList<>();
298 topLevelLeafList.add("foo");
299 Top top = new TopBuilder().setTopLevelOrderedLeafList(topLevelLeafList).build();
301 Map.Entry<YangInstanceIdentifier, NormalizedNode<?, ?>> entry =
302 registry.toNormalizedNode(InstanceIdentifier.create(Top.class), top);
303 ContainerNode containerNode = ImmutableContainerNodeBuilder.create()
304 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TOP_QNAME))
305 .withChild(ImmutableOrderedLeafSetNodeBuilder.create()
306 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TOP_LEVEL_ORDERED_LEAF_LIST_QNAME))
308 ImmutableLeafSetEntryNodeBuilder.create()
309 .withNodeIdentifier(new YangInstanceIdentifier.NodeWithValue<>(TOP_LEVEL_ORDERED_LEAF_LIST_QNAME, "foo"))
314 assertEquals(containerNode, entry.getValue());
318 public void leafListToNormalized() {
319 final List<String> topLevelLeafList = new ArrayList<>();
320 topLevelLeafList.add("foo");
321 final Top top = new TopBuilder().setTopLevelLeafList(topLevelLeafList).build();
323 final Map.Entry<YangInstanceIdentifier, NormalizedNode<?, ?>> entry =
324 registry.toNormalizedNode(InstanceIdentifier.create(Top.class), top);
325 final ContainerNode containerNode = ImmutableContainerNodeBuilder.create()
326 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TOP_QNAME))
327 .withChild(ImmutableLeafSetNodeBuilder.create()
328 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TOP_LEVEL_LEAF_LIST_QNAME))
330 ImmutableLeafSetEntryNodeBuilder.create()
331 .withNodeIdentifier(new YangInstanceIdentifier.NodeWithValue<>(TOP_LEVEL_LEAF_LIST_QNAME, "foo"))
336 assertEquals(containerNode, entry.getValue());
340 public void leafListFromNormalized() {
341 final ContainerNode topWithLeafList = ImmutableContainerNodeBuilder.create()
342 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TOP_QNAME))
343 .withChild(ImmutableLeafSetNodeBuilder.create().withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TOP_LEVEL_LEAF_LIST_QNAME))
344 .withChild(ImmutableLeafSetEntryNodeBuilder.create().withNodeIdentifier(
345 new YangInstanceIdentifier.NodeWithValue<>(TOP_LEVEL_LEAF_LIST_QNAME, "foo")).withValue("foo").build()).build())
347 final Map.Entry<InstanceIdentifier<?>, DataObject> entry = registry.fromNormalizedNode(BI_TOP_PATH, topWithLeafList);
348 final List<String> topLevelLeafList = new ArrayList<>();
349 topLevelLeafList.add("foo");
350 final Top top = new TopBuilder().setTopLevelLeafList(topLevelLeafList).build();
351 assertEquals(top, entry.getValue());
355 public void orderedLeafListFromNormalized() {
356 ContainerNode topWithLeafList = ImmutableContainerNodeBuilder.create()
357 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TOP_QNAME))
358 .withChild(ImmutableOrderedLeafSetNodeBuilder.create().withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier
359 (TOP_LEVEL_ORDERED_LEAF_LIST_QNAME))
360 .withChild(ImmutableLeafSetEntryNodeBuilder.create().withNodeIdentifier(
361 new YangInstanceIdentifier.NodeWithValue<>(TOP_LEVEL_ORDERED_LEAF_LIST_QNAME, "foo")).withValue("foo").build()).build())
363 Map.Entry<InstanceIdentifier<?>, DataObject> entry = registry.fromNormalizedNode(BI_TOP_PATH, topWithLeafList);
364 List<String> topLevelLeafList = new ArrayList<>();
365 topLevelLeafList.add("foo");
366 Top top = new TopBuilder().setTopLevelOrderedLeafList(topLevelLeafList).build();
367 assertEquals(top, entry.getValue());
371 public void choiceToNormalized() {
372 final ChoiceContainer choiceContainerBA = new ChoiceContainerBuilder().setIdentifier(new ExtendedBuilder().setExtendedId(
373 new ExtendedIdBuilder().setId("identifier_value").build()).build()).build();
374 final Map.Entry<YangInstanceIdentifier, NormalizedNode<?, ?>> entry =
375 registry.toNormalizedNode(InstanceIdentifier.create(ChoiceContainer.class), choiceContainerBA);
376 final ContainerNode choiceContainer = ImmutableContainerNodeBuilder.create()
377 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(CHOICE_CONTAINER_QNAME))
378 .withChild(ImmutableChoiceNodeBuilder.create().withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(CHOICE_IDENTIFIER_QNAME))
379 .withChild(ImmutableContainerNodeBuilder.create().withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(EXTENDED_ID_QNAME))
380 .withChild(leafNode(CHOICE_IDENTIFIER_ID_QNAME, "identifier_value")).build()).build())
382 assertEquals(choiceContainer, entry.getValue());
386 public void test4798() {
387 final QName containerIdentifierQname4798 = Root.QNAME;
388 final QName choiceIdentifierQname4798 = QName.create(containerIdentifierQname4798, "bug4798-choice");
389 final QName nestedListQname4798 = QName.create(containerIdentifierQname4798, "list-in-case");
390 final QName nestedListKeyQname4798 = QName.create(containerIdentifierQname4798, "test-leaf");
391 final QName nestedContainerValidQname = QName.create(containerIdentifierQname4798, "case-b-container");
392 final QName nestedContainerOuterQname = QName.create(containerIdentifierQname4798, "outer-container");
393 final QName nestedContainerLeafOuterQname = QName.create(containerIdentifierQname4798,
394 "leaf-in-outer-container");
396 final YangInstanceIdentifier yangInstanceIdentifierOuter = YangInstanceIdentifier.of(containerIdentifierQname4798);
397 final ContainerNode containerNodeOuter = ImmutableContainerNodeBuilder.create()
398 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(containerIdentifierQname4798))
399 .withChild(ImmutableContainerNodeBuilder.create()
400 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(nestedContainerOuterQname))
401 .withChild(leafNode(nestedContainerLeafOuterQname, "bar"))
404 final Map.Entry<InstanceIdentifier<?>, DataObject> entryContainer = registry.fromNormalizedNode
405 (yangInstanceIdentifierOuter, containerNodeOuter);
406 assertNotNull(entryContainer.getValue());
407 assertNotNull(entryContainer.getKey());
409 final YangInstanceIdentifier.NodeIdentifierWithPredicates nodeIdentifierWithPredicates4798 = new YangInstanceIdentifier
410 .NodeIdentifierWithPredicates(nestedListQname4798, nestedListKeyQname4798, "foo");
411 final YangInstanceIdentifier yangInstanceIdentifier4798 = YangInstanceIdentifier.of(containerIdentifierQname4798)
412 .node(choiceIdentifierQname4798)
413 .node(nestedListQname4798)
414 .node(nodeIdentifierWithPredicates4798);
416 final YangInstanceIdentifier yangInstanceIdentifierValid = YangInstanceIdentifier.of(containerIdentifierQname4798)
417 .node(choiceIdentifierQname4798)
418 .node(nestedContainerValidQname)
419 .node(nestedListQname4798)
420 .node(nodeIdentifierWithPredicates4798);
421 final ContainerNode containerNodeValid = ImmutableContainerNodeBuilder.create()
422 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(containerIdentifierQname4798))
423 .withChild(ImmutableChoiceNodeBuilder.create()
424 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(choiceIdentifierQname4798))
425 .withChild(ImmutableContainerNodeBuilder.create()
426 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(nestedContainerValidQname))
427 .withChild(ImmutableMapNodeBuilder.create().withNodeIdentifier(new YangInstanceIdentifier
428 .NodeIdentifier(nestedListQname4798))
429 .withChild(mapEntry(nestedListQname4798, nestedListKeyQname4798, "foo"))
430 .withChild(mapEntry(nestedListQname4798, nestedListKeyQname4798, "bar"))
436 registry.fromNormalizedNode
437 (yangInstanceIdentifierValid, containerNodeValid);
438 fail("Incorect YangInstanceIdentifier should fail");
439 } catch (IllegalStateException e) {
442 final ContainerNode containerNode4798 = ImmutableContainerNodeBuilder.create()
443 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(containerIdentifierQname4798))
444 .withChild(ImmutableChoiceNodeBuilder.create()
445 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(choiceIdentifierQname4798))
446 .withChild(ImmutableMapNodeBuilder.create().withNodeIdentifier(new YangInstanceIdentifier
447 .NodeIdentifier(nestedListQname4798))
448 .withChild(mapEntry(nestedListQname4798, nestedListKeyQname4798, "foo"))
449 .withChild(mapEntry(nestedListQname4798, nestedListKeyQname4798, "bar"))
454 registry.fromNormalizedNode
455 (yangInstanceIdentifier4798, containerNode4798);
456 fail("Incorect YangInstanceIdentifier should fail");
457 } catch (IllegalStateException e) {
462 public void choiceFromNormalized() {
463 final ContainerNode choiceContainerBI = ImmutableContainerNodeBuilder.create()
464 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(CHOICE_CONTAINER_QNAME))
465 .withChild(ImmutableChoiceNodeBuilder.create().withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(CHOICE_IDENTIFIER_QNAME))
466 .withChild(ImmutableContainerNodeBuilder.create().withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(EXTENDED_ID_QNAME))
467 .withChild(leafNode(CHOICE_IDENTIFIER_ID_QNAME, "identifier_value")).build()).build())
469 final Map.Entry<InstanceIdentifier<?>, DataObject> entry = registry.fromNormalizedNode(BI_CHOICE_CONTAINER_PATH, choiceContainerBI);
470 final ChoiceContainer choiceContainerBA = new ChoiceContainerBuilder().setIdentifier(new ExtendedBuilder().setExtendedId(
471 new ExtendedIdBuilder().setId("identifier_value").build()).build()).build();
472 assertEquals(choiceContainerBA, entry.getValue());
476 public void orderedLisToNormalized() {
477 final InstanceIdentifier<TopLevelList> ii = BA_TOP_LEVEL_LIST;
478 final List<NestedList> nestedLists = new ArrayList<>();
479 nestedLists.add(new NestedListBuilder().setKey(new NestedListKey("foo")).build());
480 nestedLists.add(new NestedListBuilder().setKey(new NestedListKey("bar")).build());
481 final TopLevelList topLevelList = new TopLevelListBuilder().setKey(TOP_LEVEL_LIST_FOO_KEY).setNestedList(nestedLists).build();
482 final Map.Entry<YangInstanceIdentifier, NormalizedNode<?, ?>> entry = registry.toNormalizedNode(ii, topLevelList);
483 final MapEntryNode foo = mapEntryBuilder().withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifierWithPredicates(
484 TOP_LEVEL_LIST_QNAME, TOP_LEVEL_LIST_KEY_QNAME, TOP_LEVEL_LIST_FOO_KEY_VALUE))
485 .withChild(leafNode(TOP_LEVEL_LIST_KEY_QNAME, TOP_LEVEL_LIST_FOO_KEY_VALUE))
487 ImmutableOrderedMapNodeBuilder.create().withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(NESTED_LIST_QNAME))
488 .withChild(mapEntry(NESTED_LIST_QNAME, NESTED_LIST_KEY_QNAME, "foo"))
489 .withChild(mapEntry(NESTED_LIST_QNAME, NESTED_LIST_KEY_QNAME, "bar")).build()).build();
490 assertEquals(foo, entry.getValue());
494 public void orderedLisFromNormalized() {
495 final MapEntryNode foo = mapEntryBuilder().withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifierWithPredicates(
496 TOP_LEVEL_LIST_QNAME, TOP_LEVEL_LIST_KEY_QNAME, TOP_LEVEL_LIST_FOO_KEY_VALUE))
497 .withChild(leafNode(TOP_LEVEL_LIST_KEY_QNAME, TOP_LEVEL_LIST_FOO_KEY_VALUE))
499 ImmutableOrderedMapNodeBuilder.create().withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(NESTED_LIST_QNAME))
500 .withChild(mapEntry(NESTED_LIST_QNAME, NESTED_LIST_KEY_QNAME, "foo"))
501 .withChild(mapEntry(NESTED_LIST_QNAME, NESTED_LIST_KEY_QNAME, "bar")).build()).build();
502 final Map.Entry<InstanceIdentifier<?>, DataObject> entry = registry.fromNormalizedNode(BI_TOP_LEVEL_LIST_FOO_PATH, foo);
503 final List<NestedList> nestedLists = new ArrayList<>();
504 nestedLists.add(new NestedListBuilder().setKey(new NestedListKey("foo")).build());
505 nestedLists.add(new NestedListBuilder().setKey(new NestedListKey("bar")).build());
506 final TopLevelList topLevelList = new TopLevelListBuilder().setKey(TOP_LEVEL_LIST_FOO_KEY).setNestedList(nestedLists).build();
507 assertEquals(topLevelList, entry.getValue());
511 public void augmentMultipleChoices() {
512 final QName augmentChoice1QName = AugmentChoice1.QNAME;
513 final QName augmentChoice2QName = QName.create(augmentChoice1QName, "augment-choice2");
514 final QName containerQName = QName.create(augmentChoice1QName, "case11-choice-case-container");
515 final QName leafQName = QName.create(augmentChoice1QName, "case11-choice-case-leaf");
517 final YangInstanceIdentifier.AugmentationIdentifier aug1Id =
518 new YangInstanceIdentifier.AugmentationIdentifier(Sets.newHashSet(augmentChoice1QName));
519 final YangInstanceIdentifier.AugmentationIdentifier aug2Id =
520 new YangInstanceIdentifier.AugmentationIdentifier(Sets.newHashSet(augmentChoice2QName));
521 final YangInstanceIdentifier.NodeIdentifier augmentChoice1Id =
522 new YangInstanceIdentifier.NodeIdentifier(augmentChoice1QName);
523 final YangInstanceIdentifier.NodeIdentifier augmentChoice2Id =
524 new YangInstanceIdentifier.NodeIdentifier(augmentChoice2QName);
525 final YangInstanceIdentifier.NodeIdentifier containerId =
526 new YangInstanceIdentifier.NodeIdentifier(containerQName);
528 final TopBuilder tBuilder = new TopBuilder();
529 final TopChoiceAugment1Builder tca1Builder = new TopChoiceAugment1Builder();
530 final Case1Builder c1Builder = new Case1Builder();
531 final TopChoiceAugment2Builder tca2Builder = new TopChoiceAugment2Builder();
532 final Case11Builder c11Builder = new Case11Builder();
533 final Case11ChoiceCaseContainerBuilder cccc1Builder = new Case11ChoiceCaseContainerBuilder();
534 cccc1Builder.setCase11ChoiceCaseLeaf("leaf-value");
535 c11Builder.setCase11ChoiceCaseContainer(cccc1Builder.build());
536 tca2Builder.setAugmentChoice2(c11Builder.build());
537 c1Builder.addAugmentation(TopChoiceAugment2.class, tca2Builder.build());
538 tca1Builder.setAugmentChoice1(c1Builder.build());
539 tBuilder.addAugmentation(TopChoiceAugment1.class, tca1Builder.build());
540 final Top top = tBuilder.build();
542 final Map.Entry<YangInstanceIdentifier, NormalizedNode<?, ?>> biResult =
543 registry.toNormalizedNode(InstanceIdentifier.create(Top.class), top);
545 final NormalizedNode<?, ?> topNormalized =
546 containerBuilder().withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TOP_QNAME))
547 .withChild(augmentationBuilder().withNodeIdentifier(aug1Id)
548 .withChild(choiceBuilder().withNodeIdentifier(augmentChoice1Id)
549 .withChild(augmentationBuilder().withNodeIdentifier(aug2Id)
550 .withChild(choiceBuilder().withNodeIdentifier(augmentChoice2Id)
551 .withChild(containerBuilder().withNodeIdentifier(containerId)
552 .withChild(leafNode(leafQName, "leaf-value"))
559 assertEquals(BI_TOP_PATH, biResult.getKey());
560 assertEquals(topNormalized, biResult.getValue());
562 final Map.Entry<InstanceIdentifier<?>, DataObject> baResult = registry.fromNormalizedNode(BI_TOP_PATH, topNormalized);
564 assertEquals(InstanceIdentifier.create(Top.class), baResult.getKey());
565 assertEquals(top, baResult.getValue());