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.yangtools.binding.data.codec.test;
10 import javassist.ClassPool;
11 import org.junit.Before;
12 import org.junit.Test;
13 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.yangtools.test.augment.rev140709.TreeComplexUsesAugment;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.yangtools.test.augment.rev140709.TreeLeafOnlyAugment;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.yangtools.test.augment.rev140709.TreeLeafOnlyAugmentBuilder;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.yangtools.test.binding.rev140701.ChoiceContainer;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.yangtools.test.binding.rev140701.ChoiceContainerBuilder;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.yangtools.test.binding.rev140701.Top;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.yangtools.test.binding.rev140701.TopBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.yangtools.test.binding.rev140701.choice.identifier.ExtendedBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.yangtools.test.binding.rev140701.choice.identifier.extended.ExtendedIdBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.yangtools.test.binding.rev140701.two.level.list.TopLevelList;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.yangtools.test.binding.rev140701.two.level.list.TopLevelListBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.yangtools.test.binding.rev140701.two.level.list.TopLevelListKey;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.yangtools.test.binding.rev140701.two.level.list.top.level.list.NestedList;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.yangtools.test.binding.rev140701.two.level.list.top.level.list.NestedListBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.yangtools.test.binding.rev140701.two.level.list.top.level.list.NestedListKey;
28 import org.opendaylight.yangtools.binding.data.codec.gen.impl.StreamWriterGenerator;
29 import org.opendaylight.yangtools.binding.data.codec.impl.BindingNormalizedNodeCodecRegistry;
30 import org.opendaylight.yangtools.sal.binding.generator.util.JavassistUtils;
31 import org.opendaylight.yangtools.yang.binding.DataObject;
32 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
33 import org.opendaylight.yangtools.yang.common.QName;
34 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
35 import org.opendaylight.yangtools.yang.data.api.schema.AugmentationNode;
36 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
37 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
38 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
39 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableAugmentationNodeBuilder;
40 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableChoiceNodeBuilder;
41 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
42 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafSetEntryNodeBuilder;
43 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafSetNodeBuilder;
44 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableMapEntryNodeBuilder;
45 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableOrderedMapNodeBuilder;
47 import java.util.ArrayList;
48 import java.util.HashSet;
49 import java.util.List;
53 import static org.junit.Assert.assertEquals;
54 import static org.opendaylight.controller.md.sal.test.model.util.ListsBindingUtils.top;
55 import static org.opendaylight.controller.md.sal.test.model.util.ListsBindingUtils.topLevelList;
56 import static org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes.leafNode;
57 import static org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes.mapEntry;
58 import static org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes.mapEntryBuilder;
59 import static org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes.mapNodeBuilder;
61 public class NormalizedNodeSerializeDeserializeTest extends AbstractBindingRuntimeTest{
63 public static final String TOP_LEVEL_LIST_FOO_KEY_VALUE = "foo";
64 public static final TopLevelListKey TOP_LEVEL_LIST_FOO_KEY = new TopLevelListKey(TOP_LEVEL_LIST_FOO_KEY_VALUE);
66 public static final QName TOP_QNAME =
67 QName.create("urn:opendaylight:params:xml:ns:yang:yangtools:test:binding", "2014-07-01", "top");
68 public static final QName TOP_LEVEL_LIST_QNAME = QName.create(TOP_QNAME, "top-level-list");
69 public static final QName TOP_LEVEL_LIST_KEY_QNAME = QName.create(TOP_QNAME, "name");
70 public static final QName TOP_LEVEL_LEAF_LIST_QNAME = QName.create(TOP_QNAME, "top-level-leaf-list");
71 public static final QName NESTED_LIST_QNAME = QName.create(TOP_QNAME, "nested-list");
72 public static final QName NESTED_LIST_KEY_QNAME = QName.create(TOP_QNAME, "name");
73 public static final QName CHOICE_CONTAINER_QNAME =
74 QName.create("urn:opendaylight:params:xml:ns:yang:yangtools:test:binding", "2014-07-01", "choice-container");
75 public static final QName CHOICE_IDENTIFIER_QNAME = QName.create(CHOICE_CONTAINER_QNAME, "identifier");
76 public static final QName CHOICE_IDENTIFIER_ID_QNAME = QName.create(CHOICE_CONTAINER_QNAME, "id");
77 public static final QName SIMPLE_ID_QNAME = QName.create(CHOICE_CONTAINER_QNAME, "simple-id");
78 public static final QName EXTENDED_ID_QNAME = QName.create(CHOICE_CONTAINER_QNAME, "extended-id");
79 private static final QName SIMPLE_VALUE_QNAME = QName.create(TreeComplexUsesAugment.QNAME, "simple-value");
81 private static final InstanceIdentifier<TopLevelList> BA_TOP_LEVEL_LIST = InstanceIdentifier
82 .builder(Top.class).child(TopLevelList.class, TOP_LEVEL_LIST_FOO_KEY).toInstance();
83 private static final InstanceIdentifier<TreeLeafOnlyAugment> BA_TREE_LEAF_ONLY =
84 BA_TOP_LEVEL_LIST.augmentation(TreeLeafOnlyAugment.class);
85 private static final InstanceIdentifier<TreeComplexUsesAugment> BA_TREE_COMPLEX_USES =
86 BA_TOP_LEVEL_LIST.augmentation(TreeComplexUsesAugment.class);
88 public static final YangInstanceIdentifier BI_TOP_PATH = YangInstanceIdentifier.of(TOP_QNAME);
89 public static final YangInstanceIdentifier BI_TOP_LEVEL_LIST_PATH = BI_TOP_PATH.node(TOP_LEVEL_LIST_QNAME);
90 public static final YangInstanceIdentifier BI_TOP_LEVEL_LIST_FOO_PATH = BI_TOP_LEVEL_LIST_PATH
91 .node(new YangInstanceIdentifier.NodeIdentifierWithPredicates(TOP_LEVEL_LIST_QNAME, TOP_LEVEL_LIST_KEY_QNAME, TOP_LEVEL_LIST_FOO_KEY_VALUE));
92 public static final YangInstanceIdentifier BI_CHOICE_CONTAINER_PATH = YangInstanceIdentifier.of(CHOICE_CONTAINER_QNAME);
94 private BindingNormalizedNodeCodecRegistry registry;
99 JavassistUtils utils = JavassistUtils.forClassPool(ClassPool.getDefault());
100 registry = new BindingNormalizedNodeCodecRegistry(StreamWriterGenerator.create(utils));
101 registry.onBindingRuntimeContextUpdated(getRuntimeContext());
105 public void containerToNormalized() {
106 Map.Entry<YangInstanceIdentifier, NormalizedNode<?, ?>> entry =
107 registry.toNormalizedNode(InstanceIdentifier.create(Top.class), top());
108 ContainerNode topNormalized = ImmutableContainerNodeBuilder.create()
109 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TOP_QNAME))
110 .withChild(mapNodeBuilder(TOP_LEVEL_LIST_QNAME).build()).build();
111 assertEquals(topNormalized, entry.getValue());
115 public void containerFromNormalized() {
116 ContainerNode topNormalized = ImmutableContainerNodeBuilder.create()
117 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TOP_QNAME))
118 .withChild(mapNodeBuilder(TOP_LEVEL_LIST_QNAME).build()).build();
119 Map.Entry<InstanceIdentifier<?>, DataObject> entry = registry.fromNormalizedNode(BI_TOP_PATH, topNormalized);
120 assertEquals(top(), entry.getValue());
124 public void listWithKeysToNormalized() {
125 Map.Entry<YangInstanceIdentifier, NormalizedNode<?, ?>> entry =
126 registry.toNormalizedNode(BA_TOP_LEVEL_LIST, topLevelList(TOP_LEVEL_LIST_FOO_KEY));
127 MapEntryNode topLevelListNormalized = ImmutableMapEntryNodeBuilder.create()
129 new YangInstanceIdentifier.NodeIdentifierWithPredicates(
130 TOP_LEVEL_LIST_QNAME, TOP_LEVEL_LIST_KEY_QNAME, TOP_LEVEL_LIST_FOO_KEY_VALUE))
131 .withChild(leafNode(TOP_LEVEL_LIST_KEY_QNAME, TOP_LEVEL_LIST_FOO_KEY_VALUE))
133 assertEquals(topLevelListNormalized, entry.getValue());
137 public void listWithKeysFromNormalized() {
138 MapEntryNode topLevelListNormalized = ImmutableMapEntryNodeBuilder.create()
140 new YangInstanceIdentifier.NodeIdentifierWithPredicates(
141 TOP_LEVEL_LIST_QNAME, TOP_LEVEL_LIST_KEY_QNAME, TOP_LEVEL_LIST_FOO_KEY_VALUE))
142 .withChild(leafNode(TOP_LEVEL_LIST_KEY_QNAME, TOP_LEVEL_LIST_FOO_KEY_VALUE))
144 Map.Entry<InstanceIdentifier<?>, DataObject> entry =
145 registry.fromNormalizedNode(BI_TOP_LEVEL_LIST_FOO_PATH, topLevelListNormalized);
146 assertEquals(topLevelList(TOP_LEVEL_LIST_FOO_KEY), entry.getValue());
150 public void leafOnlyAugmentationToNormalized() {
151 Map.Entry<YangInstanceIdentifier, NormalizedNode<?, ?>> entry =
152 registry.toNormalizedNode(BA_TREE_LEAF_ONLY, new TreeLeafOnlyAugmentBuilder().setSimpleValue("simpleValue").build());
153 Set<QName> augmentationChildren = new HashSet<>();
154 augmentationChildren.add(SIMPLE_VALUE_QNAME);
155 AugmentationNode augmentationNode = ImmutableAugmentationNodeBuilder.create()
156 .withNodeIdentifier(new YangInstanceIdentifier.AugmentationIdentifier(augmentationChildren))
157 .withChild(leafNode(SIMPLE_VALUE_QNAME, "simpleValue"))
159 assertEquals(augmentationNode, entry.getValue());
163 public void leafOnlyAugmentationFromNormalized() {
164 Set<QName> augmentationChildren = new HashSet<>();
165 augmentationChildren.add(SIMPLE_VALUE_QNAME);
166 AugmentationNode augmentationNode = ImmutableAugmentationNodeBuilder.create()
167 .withNodeIdentifier(new YangInstanceIdentifier.AugmentationIdentifier(augmentationChildren))
168 .withChild(leafNode(SIMPLE_VALUE_QNAME, "simpleValue"))
170 Map.Entry<InstanceIdentifier<?>, DataObject> entry = registry.fromNormalizedNode(BI_TOP_LEVEL_LIST_FOO_PATH.node(
171 new YangInstanceIdentifier.AugmentationIdentifier(augmentationChildren)), augmentationNode);
172 assertEquals(new TreeLeafOnlyAugmentBuilder().setSimpleValue("simpleValue").build(), entry.getValue());
176 public void leafListToNormalized() {
177 List<String> topLevelLeafList = new ArrayList<>();
178 topLevelLeafList.add("foo");
179 Top top = new TopBuilder().setTopLevelLeafList(topLevelLeafList).build();
181 Map.Entry<YangInstanceIdentifier, NormalizedNode<?, ?>> entry =
182 registry.toNormalizedNode(InstanceIdentifier.create(Top.class), top);
183 ContainerNode containerNode = ImmutableContainerNodeBuilder.create()
184 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TOP_QNAME))
185 .withChild(ImmutableLeafSetNodeBuilder.create()
186 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TOP_LEVEL_LEAF_LIST_QNAME))
188 ImmutableLeafSetEntryNodeBuilder.create()
189 .withNodeIdentifier(new YangInstanceIdentifier.NodeWithValue(TOP_LEVEL_LEAF_LIST_QNAME, "foo"))
194 assertEquals(containerNode, entry.getValue());
198 public void leafListFromNormalized() {
199 ContainerNode topWithLeafList = ImmutableContainerNodeBuilder.create()
200 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TOP_QNAME))
201 .withChild(ImmutableLeafSetNodeBuilder.create().withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TOP_LEVEL_LEAF_LIST_QNAME))
202 .withChild(ImmutableLeafSetEntryNodeBuilder.create().withNodeIdentifier(
203 new YangInstanceIdentifier.NodeWithValue(TOP_LEVEL_LEAF_LIST_QNAME, "foo")).withValue("foo").build()).build())
205 Map.Entry<InstanceIdentifier<?>, DataObject> entry = registry.fromNormalizedNode(BI_TOP_PATH, topWithLeafList);
206 List<String> topLevelLeafList = new ArrayList<>();
207 topLevelLeafList.add("foo");
208 Top top = new TopBuilder().setTopLevelLeafList(topLevelLeafList).build();
209 assertEquals(top, entry.getValue());
213 public void choiceToNormalized() {
214 ChoiceContainer choiceContainerBA = new ChoiceContainerBuilder().setIdentifier(new ExtendedBuilder().setExtendedId(
215 new ExtendedIdBuilder().setId("identifier_value").build()).build()).build();
216 Map.Entry<YangInstanceIdentifier, NormalizedNode<?, ?>> entry =
217 registry.toNormalizedNode(InstanceIdentifier.create(ChoiceContainer.class), choiceContainerBA);
218 ContainerNode choiceContainer = ImmutableContainerNodeBuilder.create()
219 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(CHOICE_CONTAINER_QNAME))
220 .withChild(ImmutableChoiceNodeBuilder.create().withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(CHOICE_IDENTIFIER_QNAME))
221 .withChild(ImmutableContainerNodeBuilder.create().withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(EXTENDED_ID_QNAME))
222 .withChild(leafNode(CHOICE_IDENTIFIER_ID_QNAME, "identifier_value")).build()).build())
224 assertEquals(choiceContainer, entry.getValue());
228 public void choiceFromNormalized() {
229 ContainerNode choiceContainerBI = ImmutableContainerNodeBuilder.create()
230 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(CHOICE_CONTAINER_QNAME))
231 .withChild(ImmutableChoiceNodeBuilder.create().withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(CHOICE_IDENTIFIER_QNAME))
232 .withChild(ImmutableContainerNodeBuilder.create().withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(EXTENDED_ID_QNAME))
233 .withChild(leafNode(CHOICE_IDENTIFIER_ID_QNAME, "identifier_value")).build()).build())
235 Map.Entry<InstanceIdentifier<?>, DataObject> entry = registry.fromNormalizedNode(BI_CHOICE_CONTAINER_PATH, choiceContainerBI);
236 ChoiceContainer choiceContainerBA = new ChoiceContainerBuilder().setIdentifier(new ExtendedBuilder().setExtendedId(
237 new ExtendedIdBuilder().setId("identifier_value").build()).build()).build();
238 assertEquals(choiceContainerBA, entry.getValue());
242 public void orderedLisToNormalized() {
243 InstanceIdentifier<TopLevelList> ii = BA_TOP_LEVEL_LIST;
244 List<NestedList> nestedLists = new ArrayList<>();
245 nestedLists.add(new NestedListBuilder().setKey(new NestedListKey("foo")).build());
246 nestedLists.add(new NestedListBuilder().setKey(new NestedListKey("bar")).build());
247 TopLevelList topLevelList = new TopLevelListBuilder().setKey(TOP_LEVEL_LIST_FOO_KEY).setNestedList(nestedLists).build();
248 Map.Entry<YangInstanceIdentifier, NormalizedNode<?, ?>> entry = registry.toNormalizedNode(ii, topLevelList);
249 MapEntryNode foo = mapEntryBuilder().withNodeIdentifier(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 ImmutableOrderedMapNodeBuilder.create().withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(NESTED_LIST_QNAME))
254 .withChild(mapEntry(NESTED_LIST_QNAME, NESTED_LIST_KEY_QNAME, "foo"))
255 .withChild(mapEntry(NESTED_LIST_QNAME, NESTED_LIST_KEY_QNAME, "bar")).build()).build();
256 assertEquals(foo, entry.getValue());
260 public void orderedLisFromNormalized() {
261 MapEntryNode foo = mapEntryBuilder().withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifierWithPredicates(
262 TOP_LEVEL_LIST_QNAME, TOP_LEVEL_LIST_KEY_QNAME, TOP_LEVEL_LIST_FOO_KEY_VALUE))
263 .withChild(leafNode(TOP_LEVEL_LIST_KEY_QNAME, TOP_LEVEL_LIST_FOO_KEY_VALUE))
265 ImmutableOrderedMapNodeBuilder.create().withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(NESTED_LIST_QNAME))
266 .withChild(mapEntry(NESTED_LIST_QNAME, NESTED_LIST_KEY_QNAME, "foo"))
267 .withChild(mapEntry(NESTED_LIST_QNAME, NESTED_LIST_KEY_QNAME, "bar")).build()).build();
268 InstanceIdentifier<TopLevelList> ii = BA_TOP_LEVEL_LIST;
270 Map.Entry<InstanceIdentifier<?>, DataObject> entry = registry.fromNormalizedNode(BI_TOP_LEVEL_LIST_FOO_PATH, foo);
271 List<NestedList> nestedLists = new ArrayList<>();
272 nestedLists.add(new NestedListBuilder().setKey(new NestedListKey("foo")).build());
273 nestedLists.add(new NestedListBuilder().setKey(new NestedListKey("bar")).build());
274 TopLevelList topLevelList = new TopLevelListBuilder().setKey(TOP_LEVEL_LIST_FOO_KEY).setNestedList(nestedLists).build();
275 assertEquals(topLevelList, entry.getValue());