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