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