Bug 3224 - Parsing data with choice in case via augmentation results in
[yangtools.git] / yang / yang-data-codec-gson / src / test / java / org / opendaylight / yangtools / yang / data / codec / gson / TestingNormalizedNodeStructuresCreator.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.yang.data.codec.gson;
9
10 import com.google.common.collect.Lists;
11 import com.google.common.collect.Sets;
12 import java.util.HashMap;
13 import java.util.Map;
14 import org.opendaylight.yangtools.yang.common.QName;
15 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
16 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
17 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
18 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
19 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue;
20 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
21 import org.opendaylight.yangtools.yang.data.api.schema.AugmentationNode;
22 import org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode;
23 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
24 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
25 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
26 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
27 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
28 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
29 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
30 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListEntryNode;
31 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListNode;
32 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
33 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.CollectionNodeBuilder;
34 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeAttrBuilder;
35 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeBuilder;
36 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.ListNodeBuilder;
37
38 public class TestingNormalizedNodeStructuresCreator {
39
40     static NormalizedNode<?, ?> cont1Node(
41             DataContainerChild<? extends YangInstanceIdentifier.PathArgument, ?>... children) {
42         DataContainerNodeAttrBuilder<NodeIdentifier, ContainerNode> cont1 = Builders.containerBuilder();
43         cont1.withNodeIdentifier(new NodeIdentifier(QName.create("ns:complex:json", "2014-08-11", "cont1")));
44
45         cont1.withValue(Lists.newArrayList(children));
46         return cont1.build();
47     }
48
49     private static DataContainerChild<? extends PathArgument, ?> lst12Node() {
50         CollectionNodeBuilder<UnkeyedListEntryNode, UnkeyedListNode> lst12Builder = Builders.unkeyedListBuilder()
51                 .withNodeIdentifier(new NodeIdentifier(QName.create("ns:complex:json", "2014-08-11", "lst12")));
52         lst12Builder.withChild(lst12Entry1Node());
53         return lst12Builder.build();
54     }
55
56     private static UnkeyedListEntryNode lst12Entry1Node() {
57         DataContainerNodeAttrBuilder<NodeIdentifier, UnkeyedListEntryNode> lst12Entry1Builder = Builders
58                 .unkeyedListEntryBuilder();
59         lst12Entry1Builder
60                 .withNodeIdentifier(new NodeIdentifier(QName.create("ns:complex:json", "2014-08-11", "lst12")));
61         lst12Entry1Builder.withChild(Builders.leafBuilder()
62                 .withNodeIdentifier(new NodeIdentifier(QName.create("ns:complex:json", "2014-08-11", "lf121")))
63                 .withValue("lf121 value").build());
64         return lst12Entry1Builder.build();
65     }
66
67     private static DataContainerChild<? extends PathArgument, ?> choc12Node() {
68         DataContainerNodeBuilder<NodeIdentifier, ChoiceNode> choc12Builder = Builders.choiceBuilder()
69                 .withNodeIdentifier(new NodeIdentifier(QName.create("ns:complex:json", "2014-08-11", "choc12")));
70
71         choc12Builder.withChild(lf17Node());
72         return choc12Builder.build();
73     }
74
75     protected static LeafNode<Object> lf17Node() {
76         return Builders.leafBuilder()
77                 .withNodeIdentifier(new NodeIdentifier(QName.create("ns:complex:json", "2014-08-11", "lf17")))
78                 .withValue("lf17 value").build();
79     }
80
81     private static DataContainerChild<? extends PathArgument, ?> externalAugmentC11AWithLf15_11AndLf15_12Node() {
82         DataContainerNodeBuilder<AugmentationIdentifier, AugmentationNode> augmentationBuilder = Builders
83                 .augmentationBuilder();
84         augmentationBuilder.withNodeIdentifier(new AugmentationIdentifier(Sets.newHashSet(
85                 QName.create("ns:complex:json:augmentation", "2014-08-14", "lf15_11"),
86                 QName.create("ns:complex:json:augmentation", "2014-08-14", "lf15_12"))));
87         augmentationBuilder.withChild(lf15_11NodeExternal());
88         augmentationBuilder.withChild(lf15_12NodeExternal());
89         return augmentationBuilder.build();
90     }
91
92     private static LeafNode<Object> lf15_12NodeExternal() {
93         return Builders
94                 .leafBuilder()
95                 .withNodeIdentifier(
96                         new NodeIdentifier(QName.create("ns:complex:json:augmentation", "2014-08-14", "lf15_12")))
97                 .withValue("lf15_12 value from augmentation").build();
98     }
99
100     private static LeafNode<Object> lf15_11NodeExternal() {
101         return Builders
102                 .leafBuilder()
103                 .withNodeIdentifier(
104                         new NodeIdentifier(QName.create("ns:complex:json:augmentation", "2014-08-14", "lf15_11")))
105                 .withValue("lf15_11 value from augmentation").build();
106     }
107
108     private static DataContainerChild<? extends PathArgument, ?> choc11Node(
109             DataContainerChild<? extends PathArgument, ?>... children) {
110         DataContainerNodeBuilder<NodeIdentifier, ChoiceNode> choc11Builder = Builders.choiceBuilder()
111                 .withNodeIdentifier(new NodeIdentifier(QName.create("ns:complex:json", "2014-08-11", "choc11")));
112         choc11Builder.withValue(Lists.newArrayList(children));
113         // choc11Builder.addChild(lf13Node());
114         // choc11Builder.addChild(augmentChoc11_c11A_lf1511AndLf1512Children());
115         // choc11Builder.addChild(augmentChoc11_c11_lf1521Children());
116         return choc11Builder.build();
117     }
118
119     private static LeafNode<Object> lf13Node() {
120         return Builders.leafBuilder()
121                 .withNodeIdentifier(new NodeIdentifier(QName.create("ns:complex:json", "2014-08-11", "lf13")))
122                 .withValue("lf13 value").build();
123     }
124
125     private static DataContainerChild<? extends PathArgument, ?> augmentC11AWithLf15_21Node() {
126         DataContainerNodeBuilder<AugmentationIdentifier, AugmentationNode> choc11_c11AugmentBuilder = Builders
127                 .augmentationBuilder();
128         choc11_c11AugmentBuilder.withNodeIdentifier(new AugmentationIdentifier(Sets.newHashSet(QName.create(
129                 "ns:complex:json", "2014-08-11", "lf15_21"))));
130
131         choc11_c11AugmentBuilder.withChild(lf15_21Node());
132         return choc11_c11AugmentBuilder.build();
133     }
134
135     private static LeafNode<Object> lf15_21Node() {
136         return Builders.leafBuilder()
137                 .withNodeIdentifier(new NodeIdentifier(QName.create("ns:complex:json", "2014-08-11", "lf15_21")))
138                 .withValue("lf15_21 value").build();
139     }
140
141     private static DataContainerChild<? extends PathArgument, ?> augmentC11AWithLf15_11AndLf15_12Node() {
142         DataContainerNodeBuilder<AugmentationIdentifier, AugmentationNode> choc11_c11AugmentBuilder = Builders
143                 .augmentationBuilder();
144         choc11_c11AugmentBuilder.withNodeIdentifier(new AugmentationIdentifier(Sets.newHashSet(
145                 QName.create("ns:complex:json", "2014-08-11", "lf15_11"),
146                 QName.create("ns:complex:json", "2014-08-11", "lf15_12"))));
147         choc11_c11AugmentBuilder.withChild(lf15_11Node());
148         choc11_c11AugmentBuilder.withChild(lf15_12Node());
149         return choc11_c11AugmentBuilder.build();
150     }
151
152     private static LeafNode<Object> lf15_12Node() {
153         return Builders.leafBuilder()
154                 .withNodeIdentifier(new NodeIdentifier(QName.create("ns:complex:json", "2014-08-11", "lf15_12")))
155                 .withValue(QName.create("ns:complex:json", "2014-08-11", "lf11")).build();
156     }
157
158     private static LeafNode<Object> lf15_11Node() {
159         return Builders.leafBuilder()
160                 .withNodeIdentifier(new NodeIdentifier(QName.create("ns:complex:json", "2014-08-11", "lf15_11")))
161                 .withValue(Sets.newHashSet("one", "two")).build();
162     }
163
164     private static DataContainerChild<? extends PathArgument, ?> lf12_1Node() {
165         DataContainerNodeBuilder<AugmentationIdentifier, AugmentationNode> augmentBuilder = Builders
166                 .augmentationBuilder().withNodeIdentifier(
167                         new AugmentationIdentifier(Sets.newHashSet(
168                                 QName.create("ns:complex:json", "2014-08-11", "lf12_1"),
169                                 QName.create("ns:complex:json", "2014-08-11", "lf12_2"))));
170         augmentBuilder.withChild(Builders.leafBuilder()
171                 .withNodeIdentifier(new NodeIdentifier(QName.create("ns:complex:json", "2014-08-11", "lf12_1")))
172                 .withValue("lf12 value").build());
173         return augmentBuilder.build();
174     }
175
176     private static DataContainerChild<? extends PathArgument, ?> childLst11() {
177         CollectionNodeBuilder<MapEntryNode, MapNode> lst11 = Builders.mapBuilder().withNodeIdentifier(
178                 new NodeIdentifier(QName.create("ns:complex:json", "2014-08-11", "lst11")));
179
180         DataContainerNodeAttrBuilder<NodeIdentifierWithPredicates, MapEntryNode> lst11Entry1Builder = Builders
181                 .mapEntryBuilder();
182
183         Map<QName, Object> key = new HashMap<>();
184         key.put(QName.create("ns:complex:json", "2014-08-11", "key111"), "key111 value");
185         key.put(QName.create("ns:complex:json", "2014-08-11", "lf111"), "lf111 value");
186
187         lst11Entry1Builder.withNodeIdentifier(new NodeIdentifierWithPredicates(QName.create("ns:complex:json",
188                 "2014-08-11", "lst11"), key));
189         lst11Entry1Builder.withChild(Builders.leafBuilder()
190                 .withNodeIdentifier(new NodeIdentifier(QName.create("ns:complex:json", "2014-08-11", "key111")))
191                 .withValue("key111 value").build());
192         lst11Entry1Builder.withChild(Builders.leafBuilder()
193                 .withNodeIdentifier(new NodeIdentifier(QName.create("ns:complex:json", "2014-08-11", "lf112")))
194                 .withValue(lf112Value()).build());
195         lst11Entry1Builder.withChild(Builders.leafBuilder()
196                 .withNodeIdentifier(new NodeIdentifier(QName.create("ns:complex:json", "2014-08-11", "lf113")))
197                 .withValue("lf113 value").build());
198         lst11Entry1Builder.withChild(Builders.leafBuilder()
199                 .withNodeIdentifier(new NodeIdentifier(QName.create("ns:complex:json", "2014-08-11", "lf111")))
200                 .withValue("lf111 value").build());
201         lst11.withChild(lst11Entry1Builder.build());
202         return lst11.build();
203     }
204
205     private static Object lf112Value() {
206         return YangInstanceIdentifier.create(
207                 new NodeIdentifier(QName.create("ns:complex:json", "2014-08-11", "cont1")),
208                 new NodeIdentifier(QName.create("ns:complex:json", "2014-08-11", "lflst11")),
209                 new NodeWithValue(QName.create("ns:complex:json", "2014-08-11", "lflst11"),"foo")
210         );
211     }
212
213     private static DataContainerChild<? extends PathArgument, ?> childLflst11() {
214         ListNodeBuilder<Object, LeafSetEntryNode<Object>> lflst11 = Builders.leafSetBuilder().withNodeIdentifier(
215                 new NodeIdentifier(QName.create("ns:complex:json", "2014-08-11", "lflst11")));
216         lflst11.withChild(Builders
217                 .leafSetEntryBuilder()
218                 .withNodeIdentifier(
219                         new NodeWithValue(QName.create("ns:complex:json", "2014-08-11", "lflst11"), "lflst11 value1"))
220                 .withValue("lflst11 value1").build());
221         lflst11.withChild(Builders
222                 .leafSetEntryBuilder()
223                 .withNodeIdentifier(
224                         new NodeWithValue(QName.create("ns:complex:json", "2014-08-11", "lflst11"), "lflst11 value2"))
225                 .withValue("lflst11 value2").build());
226         return lflst11.build();
227     }
228
229     private static DataContainerChild<? extends PathArgument, ?> childLflst11Multiline() {
230         ListNodeBuilder<Object, LeafSetEntryNode<Object>> lflst11 = Builders.leafSetBuilder().withNodeIdentifier(
231                 new NodeIdentifier(QName.create("ns:complex:json", "2014-08-11", "lflst11")));
232         lflst11.withChild(Builders
233                 .leafSetEntryBuilder()
234                 .withNodeIdentifier(
235                         new NodeWithValue(QName.create("ns:complex:json", "2014-08-11", "lflst11"), "lflst11 value1\nanother line 1"))
236                 .withValue("lflst11 value1\nanother line 1").build());
237         lflst11.withChild(Builders
238                 .leafSetEntryBuilder()
239                 .withNodeIdentifier(
240                         new NodeWithValue(QName.create("ns:complex:json", "2014-08-11", "lflst11"), "lflst11 value2\r\nanother line 2"))
241                 .withValue("lflst11 value2\r\nanother line 2").build());
242         return lflst11.build();
243     }
244
245     public static NormalizedNode<?, ?> leafNodeInContainer() {
246         LeafNode<Object> lf11 = Builders.leafBuilder()
247                 .withNodeIdentifier(new NodeIdentifier(QName.create("ns:complex:json", "2014-08-11", "lf11")))
248                 .withValue(453).build();
249         return cont1Node(lf11);
250     }
251
252     public static NormalizedNode<?, ?> leafListNodeInContainer() {
253         return cont1Node(childLflst11());
254     }
255     public static NormalizedNode<?, ?> leafListNodeInContainerMultiline() {
256         return cont1Node(childLflst11Multiline());
257     }
258
259     public static NormalizedNode<?, ?> keyedListNodeInContainer() {
260         return cont1Node(childLst11());
261     }
262
263     public static NormalizedNode<?, ?> leafNodeViaAugmentationInContainer() {
264         return cont1Node(lf12_1Node());
265     }
266
267     public static NormalizedNode<?, ?> choiceNodeInContainer() {
268         return cont1Node(choc11Node(lf13Node()));
269     }
270
271     /**
272      * choc11 contains lf13, lf15_11 and lf15_12 are added via external augmentation
273      *
274      * @return
275      */
276     public static NormalizedNode<?, ?> caseNodeAugmentationInChoiceInContainer() {
277         return cont1Node(choc11Node(augmentC11AWithLf15_11AndLf15_12Node(), lf13Node(), augmentC11AWithLf15_21Node()));
278     }
279
280     public static NormalizedNode<?, ?> caseNodeExternalAugmentationInChoiceInContainer() {
281         return cont1Node(choc11Node(lf13Node(), augmentC11AWithLf15_11AndLf15_12Node(), externalAugmentC11AWithLf15_11AndLf15_12Node()));
282     }
283
284     public static NormalizedNode<?, ?> choiceNodeAugmentationInContainer() {
285         return cont1Node(choc12Node());
286     }
287
288     public static NormalizedNode<?, ?> unkeyedNodeInContainer() {
289         return cont1Node(lst12Node());
290     }
291
292     public static NormalizedNode<?, ?> topLevelContainer() {
293         return cont1Node();
294     }
295 }