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