Merge "Small fix to xsql dependencies"
[controller.git] / opendaylight / md-sal / sal-clustering-commons / src / test / java / org / opendaylight / controller / cluster / datastore / util / TestModel.java
1 /*
2  *
3  *  Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
4  *
5  *  This program and the accompanying materials are made available under the
6  *  terms of the Eclipse Public License v1.0 which accompanies this distribution,
7  *  and is available at http://www.eclipse.org/legal/epl-v10.html
8  *
9  */
10
11 package org.opendaylight.controller.cluster.datastore.util;
12
13 import com.google.common.collect.ImmutableSet;
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.schema.AugmentationNode;
17 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
18 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
19 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
20 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetNode;
21 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
22 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
23 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
24 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
25 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
26 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.CollectionNodeBuilder;
27 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeAttrBuilder;
28 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeBuilder;
29 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.NormalizedNodeAttrBuilder;
30 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
31 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafSetEntryNodeBuilder;
32 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafSetNodeBuilder;
33 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableMapEntryNodeBuilder;
34 import org.opendaylight.yangtools.yang.model.api.Module;
35 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
36 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
37
38 import java.io.InputStream;
39 import java.util.ArrayList;
40 import java.util.HashSet;
41 import java.util.List;
42 import java.util.Set;
43
44 import static org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes.mapEntry;
45 import static org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes.mapEntryBuilder;
46 import static org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes.mapNodeBuilder;
47
48 public class TestModel {
49
50   public static final QName TEST_QNAME = QName.create(
51       "urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom:store:test",
52       "2014-03-13", "test");
53
54   public static final QName AUG_NAME_QNAME = QName.create(
55       "urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom:store:aug",
56       "2014-03-13", "name");
57
58   public static final QName AUG_CONT_QNAME = QName.create(
59       "urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom:store:aug",
60       "2014-03-13", "cont");
61
62
63   public static final QName DESC_QNAME = QName.create(TEST_QNAME, "desc");
64   public static final QName POINTER_QNAME = QName.create(TEST_QNAME, "pointer");
65   public static final QName SOME_REF_QNAME = QName.create(TEST_QNAME,
66       "some-ref");
67   public static final QName MYIDENTITY_QNAME = QName.create(TEST_QNAME,
68       "myidentity");
69   public static final QName SWITCH_FEATURES_QNAME = QName.create(TEST_QNAME,
70       "switch-features");
71
72   public static final QName AUGMENTED_LIST_QNAME = QName.create(TEST_QNAME,
73       "augmented-list");
74
75   public static final QName OUTER_LIST_QNAME = QName.create(TEST_QNAME,
76       "outer-list");
77   public static final QName INNER_LIST_QNAME = QName.create(TEST_QNAME,
78       "inner-list");
79   public static final QName OUTER_CHOICE_QNAME = QName.create(TEST_QNAME,
80       "outer-choice");
81   public static final QName ID_QNAME = QName.create(TEST_QNAME, "id");
82   public static final QName NAME_QNAME = QName.create(TEST_QNAME, "name");
83   public static final QName VALUE_QNAME = QName.create(TEST_QNAME, "value");
84   private static final String DATASTORE_TEST_YANG = "/odl-datastore-test.yang";
85   private static final String DATASTORE_AUG_YANG =
86       "/odl-datastore-augmentation.yang";
87   private static final String DATASTORE_TEST_NOTIFICATION_YANG =
88       "/odl-datastore-test-notification.yang";
89
90
91   public static final YangInstanceIdentifier TEST_PATH = YangInstanceIdentifier
92       .of(TEST_QNAME);
93   public static final YangInstanceIdentifier DESC_PATH = YangInstanceIdentifier
94       .builder(TEST_PATH).node(DESC_QNAME).build();
95   public static final YangInstanceIdentifier OUTER_LIST_PATH =
96       YangInstanceIdentifier.builder(TEST_PATH).node(OUTER_LIST_QNAME).build();
97   public static final QName TWO_QNAME = QName.create(TEST_QNAME, "two");
98   public static final QName THREE_QNAME = QName.create(TEST_QNAME, "three");
99
100   private static final Integer ONE_ID = 1;
101   private static final Integer TWO_ID = 2;
102   private static final String TWO_ONE_NAME = "one";
103   private static final String TWO_TWO_NAME = "two";
104   private static final String DESC = "Hello there";
105
106   // Family specific constants
107   public static final QName FAMILY_QNAME =
108       QName
109           .create(
110               "urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom:store:notification-test",
111               "2014-04-17", "family");
112   public static final QName CHILDREN_QNAME = QName.create(FAMILY_QNAME,
113       "children");
114   public static final QName GRAND_CHILDREN_QNAME = QName.create(FAMILY_QNAME,
115       "grand-children");
116   public static final QName CHILD_NUMBER_QNAME = QName.create(FAMILY_QNAME,
117       "child-number");
118   public static final QName CHILD_NAME_QNAME = QName.create(FAMILY_QNAME,
119       "child-name");
120   public static final QName GRAND_CHILD_NUMBER_QNAME = QName.create(
121       FAMILY_QNAME, "grand-child-number");
122   public static final QName GRAND_CHILD_NAME_QNAME = QName.create(FAMILY_QNAME,
123       "grand-child-name");
124
125   public static final YangInstanceIdentifier FAMILY_PATH =
126       YangInstanceIdentifier.of(FAMILY_QNAME);
127   public static final YangInstanceIdentifier FAMILY_DESC_PATH =
128       YangInstanceIdentifier.builder(FAMILY_PATH).node(DESC_QNAME).build();
129   public static final YangInstanceIdentifier CHILDREN_PATH =
130       YangInstanceIdentifier.builder(FAMILY_PATH).node(CHILDREN_QNAME).build();
131
132   private static final Integer FIRST_CHILD_ID = 1;
133   private static final Integer SECOND_CHILD_ID = 2;
134
135   private static final String FIRST_CHILD_NAME = "first child";
136   private static final String SECOND_CHILD_NAME = "second child";
137
138   private static final Integer FIRST_GRAND_CHILD_ID = 1;
139   private static final Integer SECOND_GRAND_CHILD_ID = 2;
140
141   private static final String FIRST_GRAND_CHILD_NAME = "first grand child";
142   private static final String SECOND_GRAND_CHILD_NAME = "second grand child";
143
144   // first child
145   private static final YangInstanceIdentifier CHILDREN_1_PATH =
146       YangInstanceIdentifier.builder(CHILDREN_PATH)
147           .nodeWithKey(CHILDREN_QNAME, CHILD_NUMBER_QNAME, FIRST_CHILD_ID) //
148           .build();
149   private static final YangInstanceIdentifier CHILDREN_1_NAME_PATH =
150       YangInstanceIdentifier.builder(CHILDREN_PATH)
151           .nodeWithKey(CHILDREN_QNAME, CHILD_NAME_QNAME, FIRST_CHILD_NAME) //
152           .build();
153
154   private static final YangInstanceIdentifier CHILDREN_2_PATH =
155       YangInstanceIdentifier.builder(CHILDREN_PATH)
156           .nodeWithKey(CHILDREN_QNAME, CHILD_NUMBER_QNAME, SECOND_CHILD_ID) //
157           .build();
158   private static final YangInstanceIdentifier CHILDREN_2_NAME_PATH =
159       YangInstanceIdentifier.builder(CHILDREN_PATH)
160           .nodeWithKey(CHILDREN_QNAME, CHILD_NAME_QNAME, SECOND_CHILD_NAME) //
161           .build();
162
163
164   private static final YangInstanceIdentifier GRAND_CHILD_1_PATH =
165       YangInstanceIdentifier
166           .builder(CHILDREN_1_PATH)
167           .node(GRAND_CHILDREN_QNAME)
168           //
169           .nodeWithKey(GRAND_CHILDREN_QNAME, GRAND_CHILD_NUMBER_QNAME,
170               FIRST_GRAND_CHILD_ID) //
171           .build();
172
173   private static final YangInstanceIdentifier GRAND_CHILD_1_NAME_PATH =
174       YangInstanceIdentifier
175           .builder(CHILDREN_1_PATH)
176           .node(GRAND_CHILDREN_QNAME)
177           //
178           .nodeWithKey(GRAND_CHILDREN_QNAME, GRAND_CHILD_NAME_QNAME,
179               FIRST_GRAND_CHILD_NAME) //
180           .build();
181
182   private static final YangInstanceIdentifier GRAND_CHILD_2_PATH =
183       YangInstanceIdentifier
184           .builder(CHILDREN_2_PATH)
185           .node(GRAND_CHILDREN_QNAME)
186           //
187           .nodeWithKey(GRAND_CHILDREN_QNAME, GRAND_CHILD_NUMBER_QNAME,
188               SECOND_GRAND_CHILD_ID) //
189           .build();
190
191   private static final YangInstanceIdentifier GRAND_CHILD_2_NAME_PATH =
192       YangInstanceIdentifier
193           .builder(CHILDREN_2_PATH)
194           .node(GRAND_CHILDREN_QNAME)
195           //
196           .nodeWithKey(GRAND_CHILDREN_QNAME, GRAND_CHILD_NAME_QNAME,
197               SECOND_GRAND_CHILD_NAME) //
198           .build();
199
200   private static final YangInstanceIdentifier DESC_PATH_ID =
201       YangInstanceIdentifier.builder(DESC_PATH).build();
202   private static final YangInstanceIdentifier OUTER_LIST_1_PATH =
203       YangInstanceIdentifier.builder(OUTER_LIST_PATH)
204           .nodeWithKey(OUTER_LIST_QNAME, ID_QNAME, ONE_ID) //
205           .build();
206
207   private static final YangInstanceIdentifier OUTER_LIST_2_PATH =
208       YangInstanceIdentifier.builder(OUTER_LIST_PATH)
209           .nodeWithKey(OUTER_LIST_QNAME, ID_QNAME, TWO_ID) //
210           .build();
211
212   private static final YangInstanceIdentifier TWO_TWO_PATH =
213       YangInstanceIdentifier.builder(OUTER_LIST_2_PATH).node(INNER_LIST_QNAME) //
214           .nodeWithKey(INNER_LIST_QNAME, NAME_QNAME, TWO_TWO_NAME) //
215           .build();
216
217   private static final YangInstanceIdentifier TWO_TWO_VALUE_PATH =
218       YangInstanceIdentifier.builder(TWO_TWO_PATH).node(VALUE_QNAME) //
219           .build();
220
221   private static final MapEntryNode BAR_NODE = mapEntryBuilder(
222       OUTER_LIST_QNAME, ID_QNAME, TWO_ID) //
223       .withChild(mapNodeBuilder(INNER_LIST_QNAME) //
224           .withChild(mapEntry(INNER_LIST_QNAME, NAME_QNAME, TWO_ONE_NAME)) //
225           .withChild(mapEntry(INNER_LIST_QNAME, NAME_QNAME, TWO_TWO_NAME)) //
226           .build()) //
227       .build();
228
229   public static final InputStream getDatastoreTestInputStream() {
230     return getInputStream(DATASTORE_TEST_YANG);
231   }
232
233   public static final InputStream getDatastoreAugInputStream() {
234     return getInputStream(DATASTORE_AUG_YANG);
235   }
236
237   public static final InputStream getDatastoreTestNotificationInputStream() {
238     return getInputStream(DATASTORE_TEST_NOTIFICATION_YANG);
239   }
240
241   private static InputStream getInputStream(final String resourceName) {
242     return TestModel.class.getResourceAsStream(resourceName);
243   }
244
245   public static SchemaContext createTestContext() {
246     List<InputStream> inputStreams = new ArrayList<>();
247     inputStreams.add(getDatastoreTestInputStream());
248     inputStreams.add(getDatastoreAugInputStream());
249     inputStreams.add(getDatastoreTestNotificationInputStream());
250
251     YangParserImpl parser = new YangParserImpl();
252     Set<Module> modules = parser.parseYangModelsFromStreams(inputStreams);
253     return parser.resolveSchemaContext(modules);
254   }
255
256   /**
257    * Returns a test document
258    * <p/>
259    *
260    * <pre>
261    * test
262    *     outer-list
263    *          id 1
264    *     outer-list
265    *          id 2
266    *          inner-list
267    *                  name "one"
268    *          inner-list
269    *                  name "two"
270    *
271    * </pre>
272    *
273    * @return
274    */
275   public static NormalizedNode<?, ?> createDocumentOne(
276       SchemaContext schemaContext) {
277     return ImmutableContainerNodeBuilder
278         .create()
279         .withNodeIdentifier(
280             new YangInstanceIdentifier.NodeIdentifier(schemaContext.getQName()))
281         .withChild(createTestContainer()).build();
282
283   }
284
285   public static ContainerNode createTestContainer() {
286
287
288     // Create a list of shoes
289     // This is to test leaf list entry
290     final LeafSetEntryNode<Object> nike =
291         ImmutableLeafSetEntryNodeBuilder
292             .create()
293             .withNodeIdentifier(
294                 new YangInstanceIdentifier.NodeWithValue(QName.create(
295                     TEST_QNAME, "shoe"), "nike")).withValue("nike").build();
296
297     final LeafSetEntryNode<Object> puma =
298         ImmutableLeafSetEntryNodeBuilder
299             .create()
300             .withNodeIdentifier(
301                 new YangInstanceIdentifier.NodeWithValue(QName.create(
302                     TEST_QNAME, "shoe"), "puma")).withValue("puma").build();
303
304     final LeafSetNode<Object> shoes =
305         ImmutableLeafSetNodeBuilder
306             .create()
307             .withNodeIdentifier(
308                 new YangInstanceIdentifier.NodeIdentifier(QName.create(
309                     TEST_QNAME, "shoe"))).withChild(nike).withChild(puma)
310             .build();
311
312
313     // Test a leaf-list where each entry contains an identity
314     final LeafSetEntryNode<Object> cap1 =
315         ImmutableLeafSetEntryNodeBuilder
316             .create()
317             .withNodeIdentifier(
318                 new YangInstanceIdentifier.NodeWithValue(QName.create(
319                     TEST_QNAME, "capability"), DESC_QNAME))
320             .withValue(DESC_QNAME).build();
321
322     final LeafSetNode<Object> capabilities =
323         ImmutableLeafSetNodeBuilder
324             .create()
325             .withNodeIdentifier(
326                 new YangInstanceIdentifier.NodeIdentifier(QName.create(
327                     TEST_QNAME, "capability"))).withChild(cap1).build();
328
329     ContainerNode switchFeatures =
330         ImmutableContainerNodeBuilder
331             .create()
332             .withNodeIdentifier(
333                 new YangInstanceIdentifier.NodeIdentifier(SWITCH_FEATURES_QNAME))
334             .withChild(capabilities).build();
335
336     // Create a leaf list with numbers
337     final LeafSetEntryNode<Object> five =
338         ImmutableLeafSetEntryNodeBuilder
339             .create()
340             .withNodeIdentifier(
341                 (new YangInstanceIdentifier.NodeWithValue(QName.create(
342                     TEST_QNAME, "number"), 5))).withValue(5).build();
343     final LeafSetEntryNode<Object> fifteen =
344         ImmutableLeafSetEntryNodeBuilder
345             .create()
346             .withNodeIdentifier(
347                 (new YangInstanceIdentifier.NodeWithValue(QName.create(
348                     TEST_QNAME, "number"), 15))).withValue(15).build();
349     final LeafSetNode<Object> numbers =
350         ImmutableLeafSetNodeBuilder
351             .create()
352             .withNodeIdentifier(
353                 new YangInstanceIdentifier.NodeIdentifier(QName.create(
354                     TEST_QNAME, "number"))).withChild(five).withChild(fifteen)
355             .build();
356
357
358     // Create augmentations
359     MapEntryNode mapEntry = createAugmentedListEntry(1, "First Test");
360
361     // Create a bits leaf
362       NormalizedNodeAttrBuilder<YangInstanceIdentifier.NodeIdentifier, Object, LeafNode<Object>>
363           myBits = Builders.leafBuilder().withNodeIdentifier(
364           new YangInstanceIdentifier.NodeIdentifier(
365               QName.create(TEST_QNAME, "my-bits"))).withValue(
366           ImmutableSet.of("foo", "bar"));
367
368
369       // Create the document
370     return ImmutableContainerNodeBuilder
371         .create()
372         .withNodeIdentifier(
373             new YangInstanceIdentifier.NodeIdentifier(TEST_QNAME))
374         .withChild(myBits.build())
375         .withChild(ImmutableNodes.leafNode(DESC_QNAME, DESC))
376         .withChild(ImmutableNodes.leafNode(POINTER_QNAME, "pointer"))
377         .withChild(
378             ImmutableNodes.leafNode(SOME_REF_QNAME, YangInstanceIdentifier
379                 .builder().build()))
380         .withChild(ImmutableNodes.leafNode(MYIDENTITY_QNAME, DESC_QNAME))
381
382         // .withChild(augmentationNode)
383         .withChild(shoes)
384         .withChild(numbers)
385         .withChild(switchFeatures)
386         .withChild(
387             mapNodeBuilder(AUGMENTED_LIST_QNAME).withChild(mapEntry).build())
388         .withChild(
389             mapNodeBuilder(OUTER_LIST_QNAME)
390                 .withChild(mapEntry(OUTER_LIST_QNAME, ID_QNAME, ONE_ID))
391                 .withChild(BAR_NODE).build()).build();
392
393   }
394
395   public static MapEntryNode createAugmentedListEntry(int id, String name) {
396
397     Set<QName> childAugmentations = new HashSet<>();
398     childAugmentations.add(AUG_CONT_QNAME);
399
400     ContainerNode augCont =
401         ImmutableContainerNodeBuilder
402             .create()
403             .withNodeIdentifier(
404                 new YangInstanceIdentifier.NodeIdentifier(AUG_CONT_QNAME))
405             .withChild(ImmutableNodes.leafNode(AUG_NAME_QNAME, name)).build();
406
407
408     final YangInstanceIdentifier.AugmentationIdentifier augmentationIdentifier =
409         new YangInstanceIdentifier.AugmentationIdentifier(childAugmentations);
410
411     final AugmentationNode augmentationNode =
412         Builders.augmentationBuilder()
413             .withNodeIdentifier(augmentationIdentifier).withChild(augCont)
414             .build();
415
416     return ImmutableMapEntryNodeBuilder
417         .create()
418         .withNodeIdentifier(
419             new YangInstanceIdentifier.NodeIdentifierWithPredicates(
420                 AUGMENTED_LIST_QNAME, ID_QNAME, id))
421         .withChild(ImmutableNodes.leafNode(ID_QNAME, id))
422         .withChild(augmentationNode).build();
423   }
424
425
426   public static ContainerNode createFamily() {
427     final DataContainerNodeAttrBuilder<YangInstanceIdentifier.NodeIdentifier, ContainerNode> familyContainerBuilder =
428         ImmutableContainerNodeBuilder.create().withNodeIdentifier(
429             new YangInstanceIdentifier.NodeIdentifier(FAMILY_QNAME));
430
431     final CollectionNodeBuilder<MapEntryNode, MapNode> childrenBuilder =
432         mapNodeBuilder(CHILDREN_QNAME);
433
434     final DataContainerNodeBuilder<YangInstanceIdentifier.NodeIdentifierWithPredicates, MapEntryNode> firstChildBuilder =
435         mapEntryBuilder(CHILDREN_QNAME, CHILD_NUMBER_QNAME, FIRST_CHILD_ID);
436     final DataContainerNodeBuilder<YangInstanceIdentifier.NodeIdentifierWithPredicates, MapEntryNode> secondChildBuilder =
437         mapEntryBuilder(CHILDREN_QNAME, CHILD_NUMBER_QNAME, SECOND_CHILD_ID);
438
439     final DataContainerNodeBuilder<YangInstanceIdentifier.NodeIdentifierWithPredicates, MapEntryNode> firstGrandChildBuilder =
440         mapEntryBuilder(GRAND_CHILDREN_QNAME, GRAND_CHILD_NUMBER_QNAME,
441             FIRST_GRAND_CHILD_ID);
442     final DataContainerNodeBuilder<YangInstanceIdentifier.NodeIdentifierWithPredicates, MapEntryNode> secondGrandChildBuilder =
443         mapEntryBuilder(GRAND_CHILDREN_QNAME, GRAND_CHILD_NUMBER_QNAME,
444             SECOND_GRAND_CHILD_ID);
445
446     firstGrandChildBuilder
447         .withChild(
448             ImmutableNodes.leafNode(GRAND_CHILD_NUMBER_QNAME,
449                 FIRST_GRAND_CHILD_ID)).withChild(
450             ImmutableNodes.leafNode(GRAND_CHILD_NAME_QNAME,
451                 FIRST_GRAND_CHILD_NAME));
452
453     secondGrandChildBuilder.withChild(
454         ImmutableNodes
455             .leafNode(GRAND_CHILD_NUMBER_QNAME, SECOND_GRAND_CHILD_ID))
456         .withChild(
457             ImmutableNodes.leafNode(GRAND_CHILD_NAME_QNAME,
458                 SECOND_GRAND_CHILD_NAME));
459
460     firstChildBuilder
461         .withChild(ImmutableNodes.leafNode(CHILD_NUMBER_QNAME, FIRST_CHILD_ID))
462         .withChild(ImmutableNodes.leafNode(CHILD_NAME_QNAME, FIRST_CHILD_NAME))
463         .withChild(
464             mapNodeBuilder(GRAND_CHILDREN_QNAME).withChild(
465                 firstGrandChildBuilder.build()).build());
466
467
468     secondChildBuilder
469         .withChild(ImmutableNodes.leafNode(CHILD_NUMBER_QNAME, SECOND_CHILD_ID))
470         .withChild(ImmutableNodes.leafNode(CHILD_NAME_QNAME, SECOND_CHILD_NAME))
471         .withChild(
472             mapNodeBuilder(GRAND_CHILDREN_QNAME).withChild(
473                 firstGrandChildBuilder.build()).build());
474
475     childrenBuilder.withChild(firstChildBuilder.build());
476     childrenBuilder.withChild(secondChildBuilder.build());
477
478     return familyContainerBuilder.withChild(childrenBuilder.build()).build();
479   }
480
481 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.