2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.data.impl.schema;
10 import java.io.InputStream;
12 import java.util.Collections;
13 import java.util.List;
16 import org.junit.Before;
17 import org.junit.Test;
18 import org.opendaylight.yangtools.yang.common.QName;
19 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
20 import org.opendaylight.yangtools.yang.data.api.schema.AugmentationNode;
21 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
22 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
23 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetNode;
24 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
25 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
26 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeBuilder;
27 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableChoiceNodeSchemaAwareBuilder;
28 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableMapNodeSchemaAwareBuilder;
29 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
30 import org.opendaylight.yangtools.yang.model.api.ChoiceNode;
31 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
32 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
33 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
34 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
35 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
36 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
37 import org.opendaylight.yangtools.yang.model.api.Module;
38 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
39 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
41 import com.google.common.base.Function;
42 import com.google.common.base.Preconditions;
43 import com.google.common.collect.Collections2;
44 import com.google.common.collect.Lists;
45 import com.google.common.collect.Sets;
47 public class NormalizedDataBuilderTest {
49 private ContainerSchemaNode containerNode;
50 private SchemaContext schema;
52 SchemaContext parseTestSchema(String... yangPath) {
53 YangParserImpl yangParserImpl = new YangParserImpl();
54 Set<Module> modules = yangParserImpl.parseYangModelsFromStreams(getTestYangs(yangPath));
55 return yangParserImpl.resolveSchemaContext(modules);
58 List<InputStream> getTestYangs(String... yangPaths) {
60 return Lists.newArrayList(Collections2.transform(Lists.newArrayList(yangPaths),
61 new Function<String, InputStream>() {
63 public InputStream apply(String input) {
64 InputStream resourceAsStream = getClass().getResourceAsStream(input);
65 Preconditions.checkNotNull(resourceAsStream, "File %s was null", resourceAsStream);
66 return resourceAsStream;
72 public void setUp() throws Exception {
73 schema = parseTestSchema("test.yang");
74 containerNode = (ContainerSchemaNode) getSchemaNode(schema, "test", "container");
78 public void testSchemaUnaware() throws Exception {
80 DataContainerNodeBuilder<InstanceIdentifier.NodeIdentifier, ContainerNode> builder = Builders
81 .containerBuilder().withNodeIdentifier(getNodeIdentifier("container"));
84 LeafNode<String> leafChild = Builders.<String> leafBuilder().withNodeIdentifier(getNodeIdentifier("leaf"))
85 .withValue("String").build();
86 builder.withChild(leafChild);
89 LeafSetNode<Integer> leafList = Builders
90 .<Integer> leafSetBuilder()
91 .withNodeIdentifier(getNodeIdentifier("leaf"))
94 Builders.<Integer> leafSetEntryBuilder()
95 .withNodeIdentifier(getNodeWithValueIdentifier("leaf", 3)).withValue(3).build())
97 builder.withChild(leafList);
100 MapEntryNode listChild1 = Builders
103 Builders.<Integer> leafBuilder().withNodeIdentifier(getNodeIdentifier("uint32InList"))
104 .withValue(1).build())
105 .withChild(Builders.containerBuilder().withNodeIdentifier(getNodeIdentifier("containerInList")).build())
107 new InstanceIdentifier.NodeIdentifierWithPredicates(getNodeIdentifier("list").getNodeType(),
108 Collections.singletonMap(getNodeIdentifier("uint32InList").getNodeType(), (Object) 1)))
111 MapNode list = Builders.mapBuilder().withChild(listChild1).withNodeIdentifier(getNodeIdentifier("list"))
113 builder.withChild(list);
115 AugmentationNode augmentation = Builders
116 .augmentationBuilder()
118 new InstanceIdentifier.AugmentationIdentifier(Sets.newHashSet(getQName("augmentUint32"))))
120 Builders.<Integer> leafBuilder().withNodeIdentifier(getNodeIdentifier("augmentUint32"))
121 .withValue(11).build()).build();
123 builder.withChild(augmentation);
125 // This works without schema (adding child from augment as a direct
127 builder.withChild(Builders.<Integer> leafBuilder().withNodeIdentifier(getNodeIdentifier("augmentUint32"))
128 .withValue(11).build());
132 public void testSchemaAware() throws Exception {
133 DataContainerNodeBuilder<InstanceIdentifier.NodeIdentifier, ContainerNode> builder = Builders
134 .containerBuilder(containerNode);
136 LeafSchemaNode schemaNode = (LeafSchemaNode) getSchemaNode(schema, "test", "uint32");
137 LeafNode<String> leafChild = Builders.<String> leafBuilder(schemaNode).withValue("String").build();
138 builder.withChild(leafChild);
140 LeafListSchemaNode leafListSchemaNode = (LeafListSchemaNode) getSchemaNode(schema, "test", "leafList");
141 LeafSetNode<Integer> leafList = Builders.<Integer> leafSetBuilder(leafListSchemaNode).withChildValue(1)
142 .withChild(Builders.<Integer> leafSetEntryBuilder(leafListSchemaNode).withValue(3).build()).build();
143 builder.withChild(leafList);
145 ListSchemaNode listSchema = (ListSchemaNode) getSchemaNode(schema, "test", "list");
146 LeafSchemaNode uint32InListSchemaNode = (LeafSchemaNode) getSchemaNode(schema, "test", "uint32InList");
147 ContainerSchemaNode containerInListSchemaNode = (ContainerSchemaNode) getSchemaNode(schema, "test",
150 MapEntryNode listChild1 = Builders.mapEntryBuilder(listSchema)
151 .withChild(Builders.<Integer> leafBuilder(uint32InListSchemaNode).withValue(1).build())
152 .withChild(Builders.containerBuilder(containerInListSchemaNode).build()).build();
154 MapNode list = ImmutableMapNodeSchemaAwareBuilder.create(listSchema).withChild(listChild1).build();
155 builder.withChild(list);
157 LeafSchemaNode augmentUint32SchemaNode = (LeafSchemaNode) getSchemaNode(schema, "test", "augmentUint32");
158 AugmentationSchema augmentationSchema = getAugmentationSchemaForChild(containerNode,
159 augmentUint32SchemaNode.getQName());
161 AugmentationNode augmentation = Builders.augmentationBuilder(augmentationSchema)
162 .withChild(Builders.<Integer>leafBuilder(augmentUint32SchemaNode).withValue(11).build()).build();
164 builder.withChild(augmentation);
166 // This should fail with schema, since the leaf comes from augmentation
167 // builder.withChild(ImmutableLeafNodeSchemaAwareBuilder.<Integer>get(augmentUint32SchemaNode).withValue(11).build());
169 LeafSchemaNode augumentString1SchemaNode = (LeafSchemaNode) getSchemaNode(schema, "test", "augmentString1");
170 LeafSchemaNode augumentString2SchemaNode = (LeafSchemaNode) getSchemaNode(schema, "test", "augmentString2");
172 ChoiceNode choice1SchemaNode = (ChoiceNode) getSchemaNode(schema, "test", "choice");
173 org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode choice = ImmutableChoiceNodeSchemaAwareBuilder
174 .create(choice1SchemaNode)
175 .withChild(Builders.<String> leafBuilder(augumentString1SchemaNode).withValue("case1").build())
176 // This should fail, since child node belongs to different case
177 // .withChild(Builders.<String>leafBuilder(augumentString2SchemaNode).withValue("case2")
182 builder.withChild(choice);
184 // This should fail, child from case
185 // builder.withChild(Builders.<String>leafBuilder(augumentString1SchemaNode).withValue("case1")
189 private AugmentationSchema getAugmentationSchemaForChild(ContainerSchemaNode containerNode, QName qName) {
190 for (AugmentationSchema augmentationSchema : containerNode.getAvailableAugmentations()) {
191 if (augmentationSchema.getDataChildByName(qName) != null) {
192 return augmentationSchema;
195 throw new IllegalStateException("Unable to find child augmentation in " + containerNode);
198 private InstanceIdentifier.NodeWithValue getNodeWithValueIdentifier(String localName, Object value) {
199 return new InstanceIdentifier.NodeWithValue(getQName(localName), value);
202 private QName getQName(String localName) {
203 String namespace = "namespace";
204 return new QName(URI.create(namespace), localName);
207 private InstanceIdentifier.NodeIdentifier getNodeIdentifier(String localName) {
208 return new InstanceIdentifier.NodeIdentifier(getQName(localName));
211 public static DataSchemaNode getSchemaNode(SchemaContext context, String moduleName, String childNodeName) {
212 for (Module module : context.getModules()) {
213 if (module.getName().equals(moduleName)) {
214 DataSchemaNode found = findChildNode(module.getChildNodes(), childNodeName);
215 Preconditions.checkState(found != null, "Unable to find %s", childNodeName);
219 throw new IllegalStateException("Unable to find child node " + childNodeName);
222 static DataSchemaNode findChildNode(Set<DataSchemaNode> children, String name) {
223 List<DataNodeContainer> containers = Lists.newArrayList();
225 for (DataSchemaNode dataSchemaNode : children) {
226 if (dataSchemaNode.getQName().getLocalName().equals(name))
227 return dataSchemaNode;
228 if (dataSchemaNode instanceof DataNodeContainer) {
229 containers.add((DataNodeContainer) dataSchemaNode);
230 } else if (dataSchemaNode instanceof ChoiceNode) {
231 containers.addAll(((ChoiceNode) dataSchemaNode).getCases());
235 for (DataNodeContainer container : containers) {
236 DataSchemaNode retVal = findChildNode(container.getChildNodes(), name);
237 if (retVal != null) {