0757c0c17f2b656d8d076ff459c91019db595065
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / md / cluster / datastore / model / CompositeModel.java
1 /*
2  * Copyright (c) 2014, 2015 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
9 package org.opendaylight.controller.md.cluster.datastore.model;
10
11 import static org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes.mapEntry;
12 import static org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes.mapEntryBuilder;
13 import static org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes.mapNodeBuilder;
14 import java.io.InputStream;
15 import java.util.ArrayList;
16 import java.util.HashSet;
17 import java.util.List;
18 import java.util.Set;
19 import org.opendaylight.yangtools.yang.common.QName;
20 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
21 import org.opendaylight.yangtools.yang.data.api.schema.AugmentationNode;
22 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
23 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
24 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetNode;
25 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
26 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
27 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
28 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
29 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
30 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.CollectionNodeBuilder;
31 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeAttrBuilder;
32 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeBuilder;
33 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
34 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafSetEntryNodeBuilder;
35 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafSetNodeBuilder;
36 import org.opendaylight.yangtools.yang.model.api.Module;
37 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
38 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
39
40 public class CompositeModel {
41
42   public static final QName TEST_QNAME = QName.create(
43       "urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom:store:test",
44       "2014-03-13", "test");
45
46   public static final QName AUG_QNAME = QName.create(
47       "urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom:store:aug",
48       "2014-03-13", "name");
49
50   public static final QName AUG_CONTAINER = QName.create(AUG_QNAME, "aug-container");
51   public static final QName AUG_INNER_CONTAINER = QName.create(AUG_QNAME, "aug-inner-container");
52   public static final QName DESC_QNAME = QName.create(TEST_QNAME, "desc");
53   public static final QName OUTER_LIST_QNAME = QName.create(TEST_QNAME,
54       "outer-list");
55   public static final QName INNER_LIST_QNAME = QName.create(TEST_QNAME,
56       "inner-list");
57   public static final QName OUTER_CHOICE_QNAME = QName.create(TEST_QNAME,
58       "outer-choice");
59   public static final QName ID_QNAME = QName.create(TEST_QNAME, "id");
60   public static final QName NAME_QNAME = QName.create(TEST_QNAME, "name");
61   public static final QName VALUE_QNAME = QName.create(TEST_QNAME, "value");
62   private static final String DATASTORE_TEST_YANG = "/odl-datastore-test.yang";
63   private static final String DATASTORE_AUG_YANG =
64       "/odl-datastore-augmentation.yang";
65   private static final String DATASTORE_TEST_NOTIFICATION_YANG =
66       "/odl-datastore-test-notification.yang";
67
68
69   public static final YangInstanceIdentifier TEST_PATH = YangInstanceIdentifier
70       .of(TEST_QNAME);
71   public static final YangInstanceIdentifier DESC_PATH = YangInstanceIdentifier
72       .builder(TEST_PATH).node(DESC_QNAME).build();
73   public static final YangInstanceIdentifier OUTER_LIST_PATH = YangInstanceIdentifier
74       .builder(TEST_PATH).node(OUTER_LIST_QNAME).build();
75   public static final QName TWO_QNAME = QName.create(TEST_QNAME, "two");
76   public static final QName THREE_QNAME = QName.create(TEST_QNAME, "three");
77
78   private static final Integer ONE_ID = 1;
79   private static final Integer TWO_ID = 2;
80   private static final String TWO_ONE_NAME = "one";
81   private static final String TWO_TWO_NAME = "two";
82   private static final String DESC = "Hello there";
83
84   // Family specific constants
85   public static final QName FAMILY_QNAME =
86       QName
87           .create(
88               "urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom:store:notification-test",
89               "2014-04-17", "family");
90   public static final QName CHILDREN_QNAME = QName.create(FAMILY_QNAME,
91       "children");
92   public static final QName GRAND_CHILDREN_QNAME = QName.create(FAMILY_QNAME,
93       "grand-children");
94   public static final QName CHILD_NUMBER_QNAME = QName.create(FAMILY_QNAME,
95       "child-number");
96   public static final QName CHILD_NAME_QNAME = QName.create(FAMILY_QNAME,
97       "child-name");
98   public static final QName GRAND_CHILD_NUMBER_QNAME = QName.create(
99       FAMILY_QNAME, "grand-child-number");
100   public static final QName GRAND_CHILD_NAME_QNAME = QName.create(FAMILY_QNAME,
101       "grand-child-name");
102
103   public static final YangInstanceIdentifier FAMILY_PATH = YangInstanceIdentifier
104       .of(FAMILY_QNAME);
105   public static final YangInstanceIdentifier FAMILY_DESC_PATH = YangInstanceIdentifier
106       .builder(FAMILY_PATH).node(DESC_QNAME).build();
107   public static final YangInstanceIdentifier CHILDREN_PATH = YangInstanceIdentifier
108       .builder(FAMILY_PATH).node(CHILDREN_QNAME).build();
109
110   private static final Integer FIRST_CHILD_ID = 1;
111   private static final Integer SECOND_CHILD_ID = 2;
112
113   private static final String FIRST_CHILD_NAME = "first child";
114   private static final String SECOND_CHILD_NAME = "second child";
115
116   private static final Integer FIRST_GRAND_CHILD_ID = 1;
117   private static final Integer SECOND_GRAND_CHILD_ID = 2;
118
119   private static final String FIRST_GRAND_CHILD_NAME = "first grand child";
120   private static final String SECOND_GRAND_CHILD_NAME = "second grand child";
121
122   // first child
123   private static final YangInstanceIdentifier CHILDREN_1_PATH = YangInstanceIdentifier
124       .builder(CHILDREN_PATH)
125       .nodeWithKey(CHILDREN_QNAME, CHILD_NUMBER_QNAME, FIRST_CHILD_ID) //
126       .build();
127   private static final YangInstanceIdentifier CHILDREN_1_NAME_PATH =
128       YangInstanceIdentifier.builder(CHILDREN_PATH)
129           .nodeWithKey(CHILDREN_QNAME, CHILD_NAME_QNAME, FIRST_CHILD_NAME) //
130           .build();
131
132   private static final YangInstanceIdentifier CHILDREN_2_PATH = YangInstanceIdentifier
133       .builder(CHILDREN_PATH)
134       .nodeWithKey(CHILDREN_QNAME, CHILD_NUMBER_QNAME, SECOND_CHILD_ID) //
135       .build();
136   private static final YangInstanceIdentifier CHILDREN_2_NAME_PATH =
137       YangInstanceIdentifier.builder(CHILDREN_PATH)
138           .nodeWithKey(CHILDREN_QNAME, CHILD_NAME_QNAME, SECOND_CHILD_NAME) //
139           .build();
140
141
142   private static final YangInstanceIdentifier GRAND_CHILD_1_PATH =
143       YangInstanceIdentifier.builder(CHILDREN_1_PATH)
144           .node(GRAND_CHILDREN_QNAME)
145           //
146           .nodeWithKey(GRAND_CHILDREN_QNAME, GRAND_CHILD_NUMBER_QNAME,
147               FIRST_GRAND_CHILD_ID) //
148           .build();
149
150   private static final YangInstanceIdentifier GRAND_CHILD_1_NAME_PATH =
151       YangInstanceIdentifier.builder(CHILDREN_1_PATH)
152           .node(GRAND_CHILDREN_QNAME)
153           //
154           .nodeWithKey(GRAND_CHILDREN_QNAME, GRAND_CHILD_NAME_QNAME,
155               FIRST_GRAND_CHILD_NAME) //
156           .build();
157
158   private static final YangInstanceIdentifier GRAND_CHILD_2_PATH =
159       YangInstanceIdentifier.builder(CHILDREN_2_PATH)
160           .node(GRAND_CHILDREN_QNAME)
161           //
162           .nodeWithKey(GRAND_CHILDREN_QNAME, GRAND_CHILD_NUMBER_QNAME,
163               SECOND_GRAND_CHILD_ID) //
164           .build();
165
166   private static final YangInstanceIdentifier GRAND_CHILD_2_NAME_PATH =
167       YangInstanceIdentifier.builder(CHILDREN_2_PATH)
168           .node(GRAND_CHILDREN_QNAME)
169           //
170           .nodeWithKey(GRAND_CHILDREN_QNAME, GRAND_CHILD_NAME_QNAME,
171               SECOND_GRAND_CHILD_NAME) //
172           .build();
173
174   private static final YangInstanceIdentifier DESC_PATH_ID = YangInstanceIdentifier
175       .builder(DESC_PATH).build();
176   private static final YangInstanceIdentifier OUTER_LIST_1_PATH =
177       YangInstanceIdentifier.builder(OUTER_LIST_PATH)
178           .nodeWithKey(OUTER_LIST_QNAME, ID_QNAME, ONE_ID) //
179           .build();
180
181   private static final YangInstanceIdentifier OUTER_LIST_2_PATH =
182       YangInstanceIdentifier.builder(OUTER_LIST_PATH)
183           .nodeWithKey(OUTER_LIST_QNAME, ID_QNAME, TWO_ID) //
184           .build();
185
186   private static final YangInstanceIdentifier TWO_TWO_PATH = YangInstanceIdentifier
187       .builder(OUTER_LIST_2_PATH).node(INNER_LIST_QNAME) //
188       .nodeWithKey(INNER_LIST_QNAME, NAME_QNAME, TWO_TWO_NAME) //
189       .build();
190
191   private static final YangInstanceIdentifier TWO_TWO_VALUE_PATH =
192       YangInstanceIdentifier.builder(TWO_TWO_PATH).node(VALUE_QNAME) //
193           .build();
194
195   private static final MapEntryNode BAR_NODE = mapEntryBuilder(
196       OUTER_LIST_QNAME, ID_QNAME, TWO_ID) //
197       .withChild(mapNodeBuilder(INNER_LIST_QNAME) //
198           .withChild(mapEntry(INNER_LIST_QNAME, NAME_QNAME, TWO_ONE_NAME)) //
199           .withChild(mapEntry(INNER_LIST_QNAME, NAME_QNAME, TWO_TWO_NAME)) //
200           .build()) //
201       .build();
202
203   public static final InputStream getDatastoreTestInputStream() {
204     return getInputStream(DATASTORE_TEST_YANG);
205   }
206
207   public static final InputStream getDatastoreAugInputStream() {
208     return getInputStream(DATASTORE_AUG_YANG);
209   }
210
211   public static final InputStream getDatastoreTestNotificationInputStream() {
212     return getInputStream(DATASTORE_TEST_NOTIFICATION_YANG);
213   }
214
215   private static InputStream getInputStream(final String resourceName) {
216     return TestModel.class.getResourceAsStream(resourceName);
217   }
218
219   public static SchemaContext createTestContext() {
220     List<InputStream> inputStreams = new ArrayList<>();
221     inputStreams.add(getDatastoreTestInputStream());
222     inputStreams.add(getDatastoreAugInputStream());
223     inputStreams.add(getDatastoreTestNotificationInputStream());
224
225     YangParserImpl parser = new YangParserImpl();
226     Set<Module> modules = parser.parseYangModelsFromStreams(inputStreams);
227     return parser.resolveSchemaContext(modules);
228   }
229
230   /**
231    * Returns a test document
232    *
233    * <pre>
234    * test
235    *     outer-list
236    *          id 1
237    *     outer-list
238    *          id 2
239    *          inner-list
240    *                  name "one"
241    *          inner-list
242    *                  name "two"
243    *
244    * </pre>
245    *
246    * @return
247    */
248   public static NormalizedNode<?, ?> createDocumentOne(
249       SchemaContext schemaContext) {
250     return ImmutableContainerNodeBuilder
251         .create()
252         .withNodeIdentifier(
253             new YangInstanceIdentifier.NodeIdentifier(schemaContext.getQName()))
254         .withChild(createTestContainer()).build();
255
256   }
257
258   public static ContainerNode createTestContainer() {
259
260
261     final LeafSetEntryNode<Object> nike =
262         ImmutableLeafSetEntryNodeBuilder
263             .create()
264             .withNodeIdentifier(
265                 new YangInstanceIdentifier.NodeWithValue<>(QName.create(TEST_QNAME,
266                     "shoe"), "nike")).withValue("nike").build();
267     final LeafSetEntryNode<Object> puma =
268         ImmutableLeafSetEntryNodeBuilder
269             .create()
270             .withNodeIdentifier(
271                 new YangInstanceIdentifier.NodeWithValue<>(QName.create(TEST_QNAME,
272                     "shoe"), "puma")).withValue("puma").build();
273     final LeafSetNode<Object> shoes =
274         ImmutableLeafSetNodeBuilder
275             .create()
276             .withNodeIdentifier(
277                 new YangInstanceIdentifier.NodeIdentifier(QName.create(TEST_QNAME,
278                     "shoe"))).withChild(nike).withChild(puma).build();
279
280
281     final LeafSetEntryNode<Object> five =
282         ImmutableLeafSetEntryNodeBuilder
283             .create()
284             .withNodeIdentifier(
285                 (new YangInstanceIdentifier.NodeWithValue<>(QName.create(TEST_QNAME,
286                     "number"), 5))).withValue(5).build();
287     final LeafSetEntryNode<Object> fifteen =
288         ImmutableLeafSetEntryNodeBuilder
289             .create()
290             .withNodeIdentifier(
291                 (new YangInstanceIdentifier.NodeWithValue<>(QName.create(TEST_QNAME,
292                     "number"), 15))).withValue(15).build();
293     final LeafSetNode<Object> numbers =
294         ImmutableLeafSetNodeBuilder
295             .create()
296             .withNodeIdentifier(
297                 new YangInstanceIdentifier.NodeIdentifier(QName.create(TEST_QNAME,
298                     "number"))).withChild(five).withChild(fifteen).build();
299
300
301     Set<QName> childAugmentations = new HashSet<>();
302     childAugmentations.add(AUG_QNAME);
303     final YangInstanceIdentifier.AugmentationIdentifier augmentationIdentifier =
304         new YangInstanceIdentifier.AugmentationIdentifier(childAugmentations);
305     final AugmentationNode augmentationNode =
306         Builders.augmentationBuilder()
307             .withNodeIdentifier(augmentationIdentifier)
308             .withChild(ImmutableNodes.leafNode(AUG_QNAME, "First Test"))
309             .build();
310     return ImmutableContainerNodeBuilder
311         .create()
312         .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TEST_QNAME))
313         .withChild(ImmutableNodes.leafNode(DESC_QNAME, DESC))
314         .withChild(augmentationNode)
315         .withChild(shoes)
316         .withChild(numbers)
317         .withChild(
318             mapNodeBuilder(OUTER_LIST_QNAME)
319                 .withChild(mapEntry(OUTER_LIST_QNAME, ID_QNAME, ONE_ID))
320                 .withChild(BAR_NODE).build()).build();
321
322   }
323
324
325   public static ContainerNode createFamily() {
326     final DataContainerNodeAttrBuilder<YangInstanceIdentifier.NodeIdentifier, ContainerNode> familyContainerBuilder =
327         ImmutableContainerNodeBuilder.create().withNodeIdentifier(
328             new YangInstanceIdentifier.NodeIdentifier(FAMILY_QNAME));
329
330     final CollectionNodeBuilder<MapEntryNode, MapNode> childrenBuilder =
331         mapNodeBuilder(CHILDREN_QNAME);
332
333     final DataContainerNodeBuilder<YangInstanceIdentifier.NodeIdentifierWithPredicates, MapEntryNode> firstChildBuilder =
334         mapEntryBuilder(CHILDREN_QNAME, CHILD_NUMBER_QNAME, FIRST_CHILD_ID);
335     final DataContainerNodeBuilder<YangInstanceIdentifier.NodeIdentifierWithPredicates, MapEntryNode> secondChildBuilder =
336         mapEntryBuilder(CHILDREN_QNAME, CHILD_NUMBER_QNAME, SECOND_CHILD_ID);
337
338     final DataContainerNodeBuilder<YangInstanceIdentifier.NodeIdentifierWithPredicates, MapEntryNode> firstGrandChildBuilder =
339         mapEntryBuilder(GRAND_CHILDREN_QNAME, GRAND_CHILD_NUMBER_QNAME,
340             FIRST_GRAND_CHILD_ID);
341     final DataContainerNodeBuilder<YangInstanceIdentifier.NodeIdentifierWithPredicates, MapEntryNode> secondGrandChildBuilder =
342         mapEntryBuilder(GRAND_CHILDREN_QNAME, GRAND_CHILD_NUMBER_QNAME,
343             SECOND_GRAND_CHILD_ID);
344
345     firstGrandChildBuilder
346         .withChild(
347             ImmutableNodes.leafNode(GRAND_CHILD_NUMBER_QNAME,
348                 FIRST_GRAND_CHILD_ID)).withChild(
349         ImmutableNodes.leafNode(GRAND_CHILD_NAME_QNAME,
350             FIRST_GRAND_CHILD_NAME));
351
352     secondGrandChildBuilder.withChild(
353         ImmutableNodes
354             .leafNode(GRAND_CHILD_NUMBER_QNAME, SECOND_GRAND_CHILD_ID))
355         .withChild(
356             ImmutableNodes.leafNode(GRAND_CHILD_NAME_QNAME,
357                 SECOND_GRAND_CHILD_NAME));
358
359     firstChildBuilder
360         .withChild(ImmutableNodes.leafNode(CHILD_NUMBER_QNAME, FIRST_CHILD_ID))
361         .withChild(ImmutableNodes.leafNode(CHILD_NAME_QNAME, FIRST_CHILD_NAME))
362         .withChild(
363             mapNodeBuilder(GRAND_CHILDREN_QNAME).withChild(
364                 firstGrandChildBuilder.build()).build());
365
366
367     secondChildBuilder
368         .withChild(ImmutableNodes.leafNode(CHILD_NUMBER_QNAME, SECOND_CHILD_ID))
369         .withChild(ImmutableNodes.leafNode(CHILD_NAME_QNAME, SECOND_CHILD_NAME))
370         .withChild(
371             mapNodeBuilder(GRAND_CHILDREN_QNAME).withChild(
372                 firstGrandChildBuilder.build()).build());
373
374     childrenBuilder.withChild(firstChildBuilder.build());
375     childrenBuilder.withChild(secondChildBuilder.build());
376
377     return familyContainerBuilder.withChild(childrenBuilder.build()).build();
378   }
379
380 }

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