4b08fbe17f5d3283f4748adc4dd9a48af7f90fca
[yangtools.git] / code-generator / binding-data-codec / src / test / java / org / opendaylight / yangtools / binding / data / codec / test / NormalizedNodeSerializeDeserializeTest.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.binding.data.codec.test;
9
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;
46
47 import java.util.ArrayList;
48 import java.util.HashSet;
49 import java.util.List;
50 import java.util.Map;
51 import java.util.Set;
52
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;
60
61 public class NormalizedNodeSerializeDeserializeTest extends AbstractBindingRuntimeTest{
62
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);
65
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");
80
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);
87
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);
93
94     private BindingNormalizedNodeCodecRegistry registry;
95
96     @Before
97     public void setup() {
98         super.setup();
99         JavassistUtils utils = JavassistUtils.forClassPool(ClassPool.getDefault());
100         registry = new BindingNormalizedNodeCodecRegistry(StreamWriterGenerator.create(utils));
101         registry.onBindingRuntimeContextUpdated(getRuntimeContext());
102     }
103
104     @Test
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());
112     }
113
114     @Test
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());
121     }
122
123     @Test
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()
128                 .withNodeIdentifier(
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))
132                 .build();
133         assertEquals(topLevelListNormalized, entry.getValue());
134     }
135
136     @Test
137     public void listWithKeysFromNormalized() {
138         MapEntryNode topLevelListNormalized = ImmutableMapEntryNodeBuilder.create()
139                 .withNodeIdentifier(
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))
143                 .build();
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());
147     }
148
149     @Test
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"))
158                 .build();
159         assertEquals(augmentationNode, entry.getValue());
160     }
161
162     @Test
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"))
169                 .build();
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());
173     }
174
175     @Test
176     public void leafListToNormalized() {
177         List<String> topLevelLeafList = new ArrayList<>();
178         topLevelLeafList.add("foo");
179         Top top = new TopBuilder().setTopLevelLeafList(topLevelLeafList).build();
180
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))
187                         .withChild(
188                                 ImmutableLeafSetEntryNodeBuilder.create()
189                                         .withNodeIdentifier(new YangInstanceIdentifier.NodeWithValue(TOP_LEVEL_LEAF_LIST_QNAME, "foo"))
190                                         .withValue("foo")
191                                         .build())
192                         .build())
193                 .build();
194         assertEquals(containerNode, entry.getValue());
195     }
196
197     @Test
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())
204                 .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());
210     }
211
212     @Test
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())
223                 .build();
224         assertEquals(choiceContainer, entry.getValue());
225     }
226
227     @Test
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())
234                 .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());
239     }
240
241     @Test
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))
252                 .withChild(
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());
257     }
258
259     @Test
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))
264                 .withChild(
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;
269
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());
276     }
277 }