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

©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.