970425e1f51715b5b50922425b1c52d10eb9f39f
[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.Sets;
12 import java.io.File;
13 import java.io.FileNotFoundException;
14 import java.net.URI;
15 import java.net.URISyntaxException;
16 import java.util.ArrayList;
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.parseYangFiles(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.<Integer>leafSetBuilder()
75                 .withNodeIdentifier(getNodeIdentifier("leaf"))
76                 .withChildValue(1)
77                 .withChild(
78                         Builders.<Integer>leafSetEntryBuilder()
79                                 .withNodeIdentifier(getNodeWithValueIdentifier("leaf", 3)).withValue(3).build())
80                 .build();
81         builder.withChild(leafList);
82
83         // list
84         MapEntryNode listChild1 = Builders
85                 .mapEntryBuilder()
86                 .withChild(
87                         Builders.<Integer>leafBuilder().withNodeIdentifier(getNodeIdentifier("uint32InList"))
88                                 .withValue(1).build())
89                 .withChild(Builders.containerBuilder().withNodeIdentifier(getNodeIdentifier("containerInList")).build())
90                 .withNodeIdentifier(
91                         new NodeIdentifierWithPredicates(
92                                 getNodeIdentifier("list").getNodeType(), Collections.singletonMap(
93                                 getNodeIdentifier("uint32InList").getNodeType(), 1))).build();
94
95         MapNode list = Builders.mapBuilder().withChild(listChild1).withNodeIdentifier(getNodeIdentifier("list"))
96                 .build();
97         builder.withChild(list);
98
99         AugmentationNode augmentation = Builders
100                 .augmentationBuilder()
101                 .withNodeIdentifier(
102                         new AugmentationIdentifier(Sets.newHashSet(getQName("augmentUint32"))))
103                 .withChild(
104                         Builders.<Integer>leafBuilder().withNodeIdentifier(getNodeIdentifier("augmentUint32"))
105                                 .withValue(11).build()).build();
106
107         builder.withChild(augmentation);
108
109         // This works without schema (adding child from augment as a direct
110         // child)
111         builder.withChild(Builders.<Integer>leafBuilder().withNodeIdentifier(getNodeIdentifier("augmentUint32"))
112                 .withValue(11).build());
113     }
114
115     @Test
116     public void testSchemaAware() {
117         DataContainerNodeBuilder<NodeIdentifier, ContainerNode> builder = Builders.containerBuilder(containerNode);
118
119         LeafSchemaNode schemaNode = (LeafSchemaNode) getSchemaNode(schema, "test", "uint32");
120         LeafNode<String> leafChild = Builders.<String>leafBuilder(schemaNode).withValue("String").build();
121         builder.withChild(leafChild);
122
123         LeafListSchemaNode leafListSchemaNode = (LeafListSchemaNode) getSchemaNode(schema, "test", "leafList");
124         LeafSetNode<Integer> leafList = Builders.<Integer>leafSetBuilder(leafListSchemaNode).withChildValue(1)
125                 .withChild(Builders.<Integer>leafSetEntryBuilder(leafListSchemaNode).withValue(3).build()).build();
126         builder.withChild(leafList);
127
128         ListSchemaNode listSchema = (ListSchemaNode) getSchemaNode(schema, "test", "list");
129         LeafSchemaNode uint32InListSchemaNode = (LeafSchemaNode) getSchemaNode(schema, "test", "uint32InList");
130         ContainerSchemaNode containerInListSchemaNode = (ContainerSchemaNode) getSchemaNode(schema, "test",
131                 "containerInList");
132
133         MapEntryNode listChild1 = Builders.mapEntryBuilder(listSchema)
134                 .withChild(Builders.<Integer>leafBuilder(uint32InListSchemaNode).withValue(1).build())
135                 .withChild(Builders.containerBuilder(containerInListSchemaNode).build()).build();
136
137         MapNode list = ImmutableMapNodeSchemaAwareBuilder.create(listSchema).withChild(listChild1).build();
138         builder.withChild(list);
139
140         LeafSchemaNode augmentUint32SchemaNode = (LeafSchemaNode) getSchemaNode(schema, "test", "augmentUint32");
141         AugmentationSchema augmentationSchema = getAugmentationSchemaForChild(containerNode,
142                 augmentUint32SchemaNode.getQName());
143
144         AugmentationNode augmentation = Builders.augmentationBuilder(augmentationSchema)
145                 .withChild(Builders.<Integer>leafBuilder(augmentUint32SchemaNode).withValue(11).build()).build();
146
147         builder.withChild(augmentation);
148
149         // This should fail with schema, since the leaf comes from augmentation
150         // builder.withChild(ImmutableLeafNodeSchemaAwareBuilder.<Integer>get(augmentUint32SchemaNode).withValue(11)
151         // .build());
152
153         LeafSchemaNode augumentString1SchemaNode = (LeafSchemaNode) getSchemaNode(schema, "test", "augmentString1");
154         LeafSchemaNode augumentString2SchemaNode = (LeafSchemaNode) getSchemaNode(schema, "test", "augmentString2");
155
156         ChoiceSchemaNode choice1SchemaNode = (ChoiceSchemaNode) getSchemaNode(schema, "test", "choice");
157         ChoiceNode choice = ImmutableChoiceNodeSchemaAwareBuilder.create(choice1SchemaNode)
158                 .withChild(Builders.<String>leafBuilder(augumentString1SchemaNode).withValue("case1").build())
159                 // This should fail, since child node belongs to different case
160                 // .withChild(Builders.<String>leafBuilder(augumentString2SchemaNode).withValue("case2")
161                 // .build())
162                 .build();
163
164         builder.withChild(choice);
165
166         // This should fail, child from case
167         // builder.withChild(Builders.<String>leafBuilder(augumentString1SchemaNode).withValue("case1")
168         // .build());
169     }
170
171     private static AugmentationSchema getAugmentationSchemaForChild(final ContainerSchemaNode containerNode,
172             final QName qname) {
173         for (AugmentationSchema augmentationSchema : containerNode.getAvailableAugmentations()) {
174             if (augmentationSchema.getDataChildByName(qname) != null) {
175                 return augmentationSchema;
176             }
177         }
178         throw new IllegalStateException("Unable to find child augmentation in " + containerNode);
179     }
180
181     private static <T> NodeWithValue<T> getNodeWithValueIdentifier(final String localName, final T value) {
182         return new NodeWithValue<>(getQName(localName), value);
183     }
184
185     private static QName getQName(final String localName) {
186         String namespace = "namespace";
187         return new QName(URI.create(namespace), localName);
188     }
189
190     private static NodeIdentifier getNodeIdentifier(final String localName) {
191         return new NodeIdentifier(getQName(localName));
192     }
193
194     public static DataSchemaNode getSchemaNode(final SchemaContext context, final String moduleName,
195             final String childNodeName) {
196         for (Module module : context.getModules()) {
197             if (module.getName().equals(moduleName)) {
198                 DataSchemaNode found = findChildNode(module.getChildNodes(), childNodeName);
199                 Preconditions.checkState(found != null, "Unable to find %s", childNodeName);
200                 return found;
201             }
202         }
203         throw new IllegalStateException("Unable to find child node " + childNodeName);
204     }
205
206     private static DataSchemaNode findChildNode(final Iterable<DataSchemaNode> children, final String name) {
207         List<DataNodeContainer> containers = new ArrayList<>();
208
209         for (DataSchemaNode dataSchemaNode : children) {
210             if (dataSchemaNode.getQName().getLocalName().equals(name)) {
211                 return dataSchemaNode;
212             }
213             if (dataSchemaNode instanceof DataNodeContainer) {
214                 containers.add((DataNodeContainer) dataSchemaNode);
215             } else if (dataSchemaNode instanceof ChoiceSchemaNode) {
216                 containers.addAll(((ChoiceSchemaNode) dataSchemaNode).getCases());
217             }
218         }
219
220         for (DataNodeContainer container : containers) {
221             DataSchemaNode retVal = findChildNode(container.getChildNodes(), name);
222             if (retVal != null) {
223                 return retVal;
224             }
225         }
226
227         return null;
228     }
229 }