BUG-7159: eliminate use of CrossStatementSourceReactor
[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.test.util.YangParserTestUtils;
40
41 public class CompositeModel {
42
43     public static final QName TEST_QNAME = QName
44             .create("urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom:store:test", "2014-03-13", "test");
45
46     public static final QName AUG_QNAME = QName
47             .create("urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom:store:aug", "2014-03-13", "name");
48
49     public static final QName AUG_CONTAINER = QName.create(AUG_QNAME, "aug-container");
50     public static final QName AUG_INNER_CONTAINER = QName.create(AUG_QNAME, "aug-inner-container");
51     public static final QName DESC_QNAME = QName.create(TEST_QNAME, "desc");
52     public static final QName OUTER_LIST_QNAME = QName.create(TEST_QNAME, "outer-list");
53     public static final QName INNER_LIST_QNAME = QName.create(TEST_QNAME, "inner-list");
54     public static final QName OUTER_CHOICE_QNAME = QName.create(TEST_QNAME, "outer-choice");
55     public static final QName ID_QNAME = QName.create(TEST_QNAME, "id");
56     public static final QName NAME_QNAME = QName.create(TEST_QNAME, "name");
57     public static final QName VALUE_QNAME = QName.create(TEST_QNAME, "value");
58     private static final String DATASTORE_TEST_YANG = "/odl-datastore-test.yang";
59     private static final String DATASTORE_AUG_YANG = "/odl-datastore-augmentation.yang";
60     private static final String DATASTORE_TEST_NOTIFICATION_YANG = "/odl-datastore-test-notification.yang";
61
62     public static final YangInstanceIdentifier TEST_PATH = YangInstanceIdentifier.of(TEST_QNAME);
63     public static final YangInstanceIdentifier DESC_PATH = YangInstanceIdentifier.builder(TEST_PATH).node(DESC_QNAME)
64             .build();
65     public static final YangInstanceIdentifier OUTER_LIST_PATH = YangInstanceIdentifier.builder(TEST_PATH)
66             .node(OUTER_LIST_QNAME).build();
67     public static final QName TWO_QNAME = QName.create(TEST_QNAME, "two");
68     public static final QName THREE_QNAME = QName.create(TEST_QNAME, "three");
69
70     private static final Integer ONE_ID = 1;
71     private static final Integer TWO_ID = 2;
72     private static final String TWO_ONE_NAME = "one";
73     private static final String TWO_TWO_NAME = "two";
74     private static final String DESC = "Hello there";
75
76     // Family specific constants
77     public static final QName FAMILY_QNAME = QName.create(
78             "urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom:store:notification-test", "2014-04-17",
79             "family");
80     public static final QName CHILDREN_QNAME = QName.create(FAMILY_QNAME, "children");
81     public static final QName GRAND_CHILDREN_QNAME = QName.create(FAMILY_QNAME, "grand-children");
82     public static final QName CHILD_NUMBER_QNAME = QName.create(FAMILY_QNAME, "child-number");
83     public static final QName CHILD_NAME_QNAME = QName.create(FAMILY_QNAME, "child-name");
84     public static final QName GRAND_CHILD_NUMBER_QNAME = QName.create(FAMILY_QNAME, "grand-child-number");
85     public static final QName GRAND_CHILD_NAME_QNAME = QName.create(FAMILY_QNAME, "grand-child-name");
86
87     public static final YangInstanceIdentifier FAMILY_PATH = YangInstanceIdentifier.of(FAMILY_QNAME);
88     public static final YangInstanceIdentifier FAMILY_DESC_PATH = YangInstanceIdentifier.builder(FAMILY_PATH)
89             .node(DESC_QNAME).build();
90     public static final YangInstanceIdentifier CHILDREN_PATH = YangInstanceIdentifier.builder(FAMILY_PATH)
91             .node(CHILDREN_QNAME).build();
92
93     private static final Integer FIRST_CHILD_ID = 1;
94     private static final Integer SECOND_CHILD_ID = 2;
95
96     private static final String FIRST_CHILD_NAME = "first child";
97     private static final String SECOND_CHILD_NAME = "second child";
98
99     private static final Integer FIRST_GRAND_CHILD_ID = 1;
100     private static final Integer SECOND_GRAND_CHILD_ID = 2;
101
102     private static final String FIRST_GRAND_CHILD_NAME = "first grand child";
103     private static final String SECOND_GRAND_CHILD_NAME = "second grand child";
104
105     private static final MapEntryNode BAR_NODE = mapEntryBuilder(OUTER_LIST_QNAME, ID_QNAME, TWO_ID) //
106             .withChild(mapNodeBuilder(INNER_LIST_QNAME) //
107                     .withChild(mapEntry(INNER_LIST_QNAME, NAME_QNAME, TWO_ONE_NAME)) //
108                     .withChild(mapEntry(INNER_LIST_QNAME, NAME_QNAME, TWO_TWO_NAME)) //
109                     .build()) //
110             .build();
111
112     public static final InputStream getDatastoreTestInputStream() {
113         return getInputStream(DATASTORE_TEST_YANG);
114     }
115
116     public static final InputStream getDatastoreAugInputStream() {
117         return getInputStream(DATASTORE_AUG_YANG);
118     }
119
120     public static final InputStream getDatastoreTestNotificationInputStream() {
121         return getInputStream(DATASTORE_TEST_NOTIFICATION_YANG);
122     }
123
124     private static InputStream getInputStream(final String resourceName) {
125         return CompositeModel.class.getResourceAsStream(resourceName);
126     }
127
128     public static SchemaContext createTestContext() {
129         List<InputStream> inputStreams = new ArrayList<>();
130         inputStreams.add(getDatastoreTestInputStream());
131         inputStreams.add(getDatastoreAugInputStream());
132         inputStreams.add(getDatastoreTestNotificationInputStream());
133
134         try {
135             return YangParserTestUtils.parseYangStreams(inputStreams);
136         } catch (ReactorException e) {
137             throw new RuntimeException("Unable to build schema context from " + inputStreams, e);
138         }
139     }
140
141     /**
142      * Returns a test document.
143      *
144      * <pre>
145      * test
146      *     outer-list
147      *          id 1
148      *     outer-list
149      *          id 2
150      *          inner-list
151      *                  name "one"
152      *          inner-list
153      *                  name "two"
154      *
155      * </pre>
156      */
157     public static NormalizedNode<?, ?> createDocumentOne(final SchemaContext schemaContext) {
158         return ImmutableContainerNodeBuilder.create()
159                 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(schemaContext.getQName()))
160                 .withChild(createTestContainer()).build();
161
162     }
163
164     public static ContainerNode createTestContainer() {
165
166         final LeafSetEntryNode<Object> nike = ImmutableLeafSetEntryNodeBuilder.create()
167                 .withNodeIdentifier(
168                         new YangInstanceIdentifier.NodeWithValue<>(QName.create(TEST_QNAME, "shoe"), "nike"))
169                 .withValue("nike").build();
170         final LeafSetEntryNode<Object> puma = ImmutableLeafSetEntryNodeBuilder.create()
171                 .withNodeIdentifier(
172                         new YangInstanceIdentifier.NodeWithValue<>(QName.create(TEST_QNAME, "shoe"), "puma"))
173                 .withValue("puma").build();
174         final LeafSetNode<Object> shoes = ImmutableLeafSetNodeBuilder.create()
175                 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(QName.create(TEST_QNAME, "shoe")))
176                 .withChild(nike).withChild(puma).build();
177
178         final LeafSetEntryNode<Object> five = ImmutableLeafSetEntryNodeBuilder.create()
179                 .withNodeIdentifier(new YangInstanceIdentifier.NodeWithValue<>(QName.create(TEST_QNAME, "number"), 5))
180                 .withValue(5).build();
181         final LeafSetEntryNode<Object> fifteen = ImmutableLeafSetEntryNodeBuilder.create()
182                 .withNodeIdentifier(
183                         new YangInstanceIdentifier.NodeWithValue<>(QName.create(TEST_QNAME, "number"), 15))
184                 .withValue(15).build();
185         final LeafSetNode<Object> numbers = ImmutableLeafSetNodeBuilder.create()
186                 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(QName.create(TEST_QNAME, "number")))
187                 .withChild(five).withChild(fifteen).build();
188
189         Set<QName> childAugmentations = new HashSet<>();
190         childAugmentations.add(AUG_QNAME);
191         final YangInstanceIdentifier.AugmentationIdentifier augmentationIdentifier =
192                 new YangInstanceIdentifier.AugmentationIdentifier(childAugmentations);
193         final AugmentationNode augmentationNode = Builders.augmentationBuilder()
194                 .withNodeIdentifier(augmentationIdentifier).withChild(ImmutableNodes.leafNode(AUG_QNAME, "First Test"))
195                 .build();
196         return ImmutableContainerNodeBuilder.create()
197                 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TEST_QNAME))
198                 .withChild(ImmutableNodes.leafNode(DESC_QNAME, DESC)).withChild(augmentationNode).withChild(shoes)
199                 .withChild(numbers).withChild(mapNodeBuilder(OUTER_LIST_QNAME)
200                         .withChild(mapEntry(OUTER_LIST_QNAME, ID_QNAME, ONE_ID)).withChild(BAR_NODE).build())
201                 .build();
202
203     }
204
205     public static ContainerNode createFamily() {
206         final DataContainerNodeAttrBuilder<YangInstanceIdentifier.NodeIdentifier, ContainerNode>
207             familyContainerBuilder = ImmutableContainerNodeBuilder.create().withNodeIdentifier(
208                     new YangInstanceIdentifier.NodeIdentifier(FAMILY_QNAME));
209
210         final CollectionNodeBuilder<MapEntryNode, MapNode> childrenBuilder = mapNodeBuilder(CHILDREN_QNAME);
211
212         final DataContainerNodeBuilder<YangInstanceIdentifier.NodeIdentifierWithPredicates, MapEntryNode>
213             firstChildBuilder = mapEntryBuilder(CHILDREN_QNAME, CHILD_NUMBER_QNAME, FIRST_CHILD_ID);
214         final DataContainerNodeBuilder<YangInstanceIdentifier.NodeIdentifierWithPredicates, MapEntryNode>
215             secondChildBuilder = mapEntryBuilder(CHILDREN_QNAME, CHILD_NUMBER_QNAME, SECOND_CHILD_ID);
216
217         final DataContainerNodeBuilder<YangInstanceIdentifier.NodeIdentifierWithPredicates, MapEntryNode>
218             firstGrandChildBuilder = mapEntryBuilder(GRAND_CHILDREN_QNAME, GRAND_CHILD_NUMBER_QNAME,
219                     FIRST_GRAND_CHILD_ID);
220         final DataContainerNodeBuilder<YangInstanceIdentifier.NodeIdentifierWithPredicates, MapEntryNode>
221             secondGrandChildBuilder = mapEntryBuilder(GRAND_CHILDREN_QNAME, GRAND_CHILD_NUMBER_QNAME,
222                     SECOND_GRAND_CHILD_ID);
223
224         firstGrandChildBuilder.withChild(ImmutableNodes.leafNode(GRAND_CHILD_NUMBER_QNAME, FIRST_GRAND_CHILD_ID))
225                 .withChild(ImmutableNodes.leafNode(GRAND_CHILD_NAME_QNAME, FIRST_GRAND_CHILD_NAME));
226
227         secondGrandChildBuilder.withChild(ImmutableNodes.leafNode(GRAND_CHILD_NUMBER_QNAME, SECOND_GRAND_CHILD_ID))
228                 .withChild(ImmutableNodes.leafNode(GRAND_CHILD_NAME_QNAME, SECOND_GRAND_CHILD_NAME));
229
230         firstChildBuilder.withChild(ImmutableNodes.leafNode(CHILD_NUMBER_QNAME, FIRST_CHILD_ID))
231                 .withChild(ImmutableNodes.leafNode(CHILD_NAME_QNAME, FIRST_CHILD_NAME))
232                 .withChild(mapNodeBuilder(GRAND_CHILDREN_QNAME).withChild(firstGrandChildBuilder.build()).build());
233
234         secondChildBuilder.withChild(ImmutableNodes.leafNode(CHILD_NUMBER_QNAME, SECOND_CHILD_ID))
235                 .withChild(ImmutableNodes.leafNode(CHILD_NAME_QNAME, SECOND_CHILD_NAME))
236                 .withChild(mapNodeBuilder(GRAND_CHILDREN_QNAME).withChild(firstGrandChildBuilder.build()).build());
237
238         childrenBuilder.withChild(firstChildBuilder.build());
239         childrenBuilder.withChild(secondChildBuilder.build());
240
241         return familyContainerBuilder.withChild(childrenBuilder.build()).build();
242     }
243
244 }

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