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

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