Update ChoiceSchemaNode design
[yangtools.git] / yang / yang-data-impl / src / test / java / org / opendaylight / yangtools / yang / data / impl / schema / NormalizedDataBuilderTest.java
1 /*
2  * Copyright (c) 2016 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 package org.opendaylight.yangtools.yang.data.impl.schema;
9
10 import static com.google.common.base.Preconditions.checkState;
11
12 import java.io.File;
13 import java.io.FileNotFoundException;
14 import java.net.URI;
15 import java.net.URISyntaxException;
16 import java.util.Collections;
17 import org.junit.Before;
18 import org.junit.Test;
19 import org.opendaylight.yangtools.yang.common.QName;
20 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
21 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
22 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
23 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue;
24 import org.opendaylight.yangtools.yang.data.api.schema.AugmentationNode;
25 import org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode;
26 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
27 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
28 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetNode;
29 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
30 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
31 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeBuilder;
32 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableChoiceNodeSchemaAwareBuilder;
33 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableMapNodeSchemaAwareBuilder;
34 import org.opendaylight.yangtools.yang.model.api.AugmentationSchemaNode;
35 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
36 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
37 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
38 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
39 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
40 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
41 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
42 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
43 import org.opendaylight.yangtools.yang.model.api.Module;
44 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
45 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
46 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
47
48 public class NormalizedDataBuilderTest {
49
50     private ContainerSchemaNode containerNode;
51     private SchemaContext schema;
52
53     @Before
54     public void setUp() throws URISyntaxException, FileNotFoundException, ReactorException {
55         final File resourceFile = new File(getClass().getResource(
56                 "test.yang").toURI());
57         schema = YangParserTestUtils.parseYangFiles(resourceFile);
58         containerNode = (ContainerSchemaNode) getSchemaNode(schema, "test", "container");
59     }
60
61     @Test
62     public void testSchemaUnaware() {
63         // Container
64         DataContainerNodeBuilder<NodeIdentifier, ContainerNode> builder = Builders
65                 .containerBuilder().withNodeIdentifier(getNodeIdentifier("container"));
66
67         // leaf
68         LeafNode<String> leafChild = Builders.<String>leafBuilder().withNodeIdentifier(getNodeIdentifier("leaf"))
69                 .withValue("String").build();
70         builder.withChild(leafChild);
71
72         // leafList
73         LeafSetNode<Integer> leafList = Builders.<Integer>leafSetBuilder()
74                 .withNodeIdentifier(getNodeIdentifier("leaf"))
75                 .withChildValue(1)
76                 .withChild(
77                         Builders.<Integer>leafSetEntryBuilder()
78                                 .withNodeIdentifier(getNodeWithValueIdentifier("leaf", 3)).withValue(3).build())
79                 .build();
80         builder.withChild(leafList);
81
82         // list
83         MapEntryNode listChild1 = Builders
84                 .mapEntryBuilder()
85                 .withChild(
86                         Builders.<Integer>leafBuilder().withNodeIdentifier(getNodeIdentifier("uint32InList"))
87                                 .withValue(1).build())
88                 .withChild(Builders.containerBuilder().withNodeIdentifier(getNodeIdentifier("containerInList")).build())
89                 .withNodeIdentifier(
90                         new NodeIdentifierWithPredicates(
91                                 getNodeIdentifier("list").getNodeType(), Collections.singletonMap(
92                                 getNodeIdentifier("uint32InList").getNodeType(), 1))).build();
93
94         MapNode list = Builders.mapBuilder().withChild(listChild1).withNodeIdentifier(getNodeIdentifier("list"))
95                 .build();
96         builder.withChild(list);
97
98         AugmentationNode augmentation = Builders
99                 .augmentationBuilder()
100                 .withNodeIdentifier(
101                         new AugmentationIdentifier(Collections.singleton(getQName("augmentUint32"))))
102                 .withChild(
103                         Builders.<Integer>leafBuilder().withNodeIdentifier(getNodeIdentifier("augmentUint32"))
104                                 .withValue(11).build()).build();
105
106         builder.withChild(augmentation);
107
108         // This works without schema (adding child from augment as a direct
109         // child)
110         builder.withChild(Builders.<Integer>leafBuilder().withNodeIdentifier(getNodeIdentifier("augmentUint32"))
111                 .withValue(11).build());
112     }
113
114     @Test
115     public void testSchemaAware() {
116         DataContainerNodeBuilder<NodeIdentifier, ContainerNode> builder = Builders.containerBuilder(containerNode);
117
118         LeafSchemaNode schemaNode = (LeafSchemaNode) getSchemaNode(schema, "test", "uint32");
119         LeafNode<String> leafChild = Builders.<String>leafBuilder(schemaNode).withValue("String").build();
120         builder.withChild(leafChild);
121
122         LeafListSchemaNode leafListSchemaNode = (LeafListSchemaNode) getSchemaNode(schema, "test", "leafList");
123         LeafSetNode<Integer> leafList = Builders.<Integer>leafSetBuilder(leafListSchemaNode).withChildValue(1)
124                 .withChild(Builders.<Integer>leafSetEntryBuilder(leafListSchemaNode).withValue(3).build()).build();
125         builder.withChild(leafList);
126
127         ListSchemaNode listSchema = (ListSchemaNode) getSchemaNode(schema, "test", "list");
128         LeafSchemaNode uint32InListSchemaNode = (LeafSchemaNode) getSchemaNode(schema, "test", "uint32InList");
129         ContainerSchemaNode containerInListSchemaNode = (ContainerSchemaNode) getSchemaNode(schema, "test",
130                 "containerInList");
131
132         MapEntryNode listChild1 = Builders.mapEntryBuilder(listSchema)
133                 .withChild(Builders.<Integer>leafBuilder(uint32InListSchemaNode).withValue(1).build())
134                 .withChild(Builders.containerBuilder(containerInListSchemaNode).build()).build();
135
136         MapNode list = ImmutableMapNodeSchemaAwareBuilder.create(listSchema).withChild(listChild1).build();
137         builder.withChild(list);
138
139         LeafSchemaNode augmentUint32SchemaNode = (LeafSchemaNode) getSchemaNode(schema, "test", "augmentUint32");
140         AugmentationSchemaNode augmentationSchema = getAugmentationSchemaForChild(containerNode,
141                 augmentUint32SchemaNode.getQName());
142
143         AugmentationNode augmentation = Builders.augmentationBuilder(augmentationSchema)
144                 .withChild(Builders.<Integer>leafBuilder(augmentUint32SchemaNode).withValue(11).build()).build();
145
146         builder.withChild(augmentation);
147
148         // This should fail with schema, since the leaf comes from augmentation
149         // builder.withChild(ImmutableLeafNodeSchemaAwareBuilder.<Integer>get(augmentUint32SchemaNode).withValue(11)
150         // .build());
151
152         LeafSchemaNode augumentString1SchemaNode = (LeafSchemaNode) getSchemaNode(schema, "test", "augmentString1");
153         LeafSchemaNode augumentString2SchemaNode = (LeafSchemaNode) getSchemaNode(schema, "test", "augmentString2");
154
155         ChoiceSchemaNode choice1SchemaNode = (ChoiceSchemaNode) getSchemaNode(schema, "test", "choice");
156         ChoiceNode choice = ImmutableChoiceNodeSchemaAwareBuilder.create(choice1SchemaNode)
157                 .withChild(Builders.<String>leafBuilder(augumentString1SchemaNode).withValue("case1").build())
158                 // This should fail, since child node belongs to different case
159                 // .withChild(Builders.<String>leafBuilder(augumentString2SchemaNode).withValue("case2")
160                 // .build())
161                 .build();
162
163         builder.withChild(choice);
164
165         // This should fail, child from case
166         // builder.withChild(Builders.<String>leafBuilder(augumentString1SchemaNode).withValue("case1")
167         // .build());
168     }
169
170     private static AugmentationSchemaNode getAugmentationSchemaForChild(final ContainerSchemaNode containerNode,
171             final QName qname) {
172         for (AugmentationSchemaNode augmentationSchema : containerNode.getAvailableAugmentations()) {
173             if (augmentationSchema.getDataChildByName(qname) != null) {
174                 return augmentationSchema;
175             }
176         }
177         throw new IllegalStateException("Unable to find child augmentation in " + containerNode);
178     }
179
180     private static <T> NodeWithValue<T> getNodeWithValueIdentifier(final String localName, final T value) {
181         return new NodeWithValue<>(getQName(localName), value);
182     }
183
184     private static QName getQName(final String localName) {
185         return QName.create(URI.create("namespace"), localName);
186     }
187
188     private static NodeIdentifier getNodeIdentifier(final String localName) {
189         return new NodeIdentifier(getQName(localName));
190     }
191
192     public static DataSchemaNode getSchemaNode(final SchemaContext context, final String moduleName,
193             final String childNodeName) {
194         for (Module module : context.getModules()) {
195             if (module.getName().equals(moduleName)) {
196                 DataSchemaNode found = findChildNode(module, childNodeName);
197                 checkState(found != null, "Unable to find %s", childNodeName);
198                 return found;
199             }
200         }
201         throw new IllegalStateException("Unable to find child node " + childNodeName);
202     }
203
204     private static DataSchemaNode findChildNode(final DataNodeContainer container, final String name) {
205         for (DataSchemaNode dataSchemaNode : container.getChildNodes()) {
206             if (dataSchemaNode.getQName().getLocalName().equals(name)) {
207                 return dataSchemaNode;
208             }
209             if (dataSchemaNode instanceof DataNodeContainer) {
210                 DataSchemaNode retVal = findChildNode((DataNodeContainer) dataSchemaNode, name);
211                 if (retVal != null) {
212                     return retVal;
213                 }
214             } else if (dataSchemaNode instanceof ChoiceSchemaNode) {
215                 for (ChoiceCaseNode caseNode : ((ChoiceSchemaNode) dataSchemaNode).getCases().values()) {
216                     DataSchemaNode retVal = findChildNode(caseNode, name);
217                     if (retVal != null) {
218                         return retVal;
219                     }
220                 }
221             }
222         }
223         return null;
224     }
225 }