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

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