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 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;
17 import java.util.Collections;
18 import java.util.List;
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;
46 public class NormalizedDataBuilderTest {
48 private ContainerSchemaNode containerNode;
49 private SchemaContext schema;
51 SchemaContext parseTestSchema(final String... yangPath) {
52 YangParserImpl yangParserImpl = new YangParserImpl();
53 Set<Module> modules = yangParserImpl.parseYangModelsFromStreams(getTestYangs(yangPath));
54 return yangParserImpl.resolveSchemaContext(modules);
57 List<InputStream> getTestYangs(final String... yangPaths) {
59 return Lists.newArrayList(Collections2.transform(Lists.newArrayList(yangPaths),
60 new Function<String, InputStream>() {
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;
71 public void setUp() throws Exception {
72 schema = parseTestSchema("test.yang");
73 containerNode = (ContainerSchemaNode) getSchemaNode(schema, "test", "container");
77 public void testSchemaUnaware() throws Exception {
79 DataContainerNodeBuilder<YangInstanceIdentifier.NodeIdentifier, ContainerNode> builder = Builders
80 .containerBuilder().withNodeIdentifier(getNodeIdentifier("container"));
83 LeafNode<String> leafChild = Builders.<String> leafBuilder().withNodeIdentifier(getNodeIdentifier("leaf"))
84 .withValue("String").build();
85 builder.withChild(leafChild);
88 LeafSetNode<Integer> leafList = Builders
89 .<Integer> leafSetBuilder()
90 .withNodeIdentifier(getNodeIdentifier("leaf"))
93 Builders.<Integer> leafSetEntryBuilder()
94 .withNodeIdentifier(getNodeWithValueIdentifier("leaf", 3)).withValue(3).build())
96 builder.withChild(leafList);
99 MapEntryNode listChild1 = Builders
102 Builders.<Integer> leafBuilder().withNodeIdentifier(getNodeIdentifier("uint32InList"))
103 .withValue(1).build())
104 .withChild(Builders.containerBuilder().withNodeIdentifier(getNodeIdentifier("containerInList")).build())
106 new YangInstanceIdentifier.NodeIdentifierWithPredicates(getNodeIdentifier("list").getNodeType(),
107 Collections.singletonMap(getNodeIdentifier("uint32InList").getNodeType(), (Object) 1)))
110 MapNode list = Builders.mapBuilder().withChild(listChild1).withNodeIdentifier(getNodeIdentifier("list"))
112 builder.withChild(list);
114 AugmentationNode augmentation = Builders
115 .augmentationBuilder()
117 new YangInstanceIdentifier.AugmentationIdentifier(Sets.newHashSet(getQName("augmentUint32"))))
119 Builders.<Integer> leafBuilder().withNodeIdentifier(getNodeIdentifier("augmentUint32"))
120 .withValue(11).build()).build();
122 builder.withChild(augmentation);
124 // This works without schema (adding child from augment as a direct
126 builder.withChild(Builders.<Integer> leafBuilder().withNodeIdentifier(getNodeIdentifier("augmentUint32"))
127 .withValue(11).build());
131 public void testSchemaAware() throws Exception {
132 DataContainerNodeBuilder<YangInstanceIdentifier.NodeIdentifier, ContainerNode> builder = Builders
133 .containerBuilder(containerNode);
135 LeafSchemaNode schemaNode = (LeafSchemaNode) getSchemaNode(schema, "test", "uint32");
136 LeafNode<String> leafChild = Builders.<String> leafBuilder(schemaNode).withValue("String").build();
137 builder.withChild(leafChild);
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);
144 ListSchemaNode listSchema = (ListSchemaNode) getSchemaNode(schema, "test", "list");
145 LeafSchemaNode uint32InListSchemaNode = (LeafSchemaNode) getSchemaNode(schema, "test", "uint32InList");
146 ContainerSchemaNode containerInListSchemaNode = (ContainerSchemaNode) getSchemaNode(schema, "test",
149 MapEntryNode listChild1 = Builders.mapEntryBuilder(listSchema)
150 .withChild(Builders.<Integer> leafBuilder(uint32InListSchemaNode).withValue(1).build())
151 .withChild(Builders.containerBuilder(containerInListSchemaNode).build()).build();
153 MapNode list = ImmutableMapNodeSchemaAwareBuilder.create(listSchema).withChild(listChild1).build();
154 builder.withChild(list);
156 LeafSchemaNode augmentUint32SchemaNode = (LeafSchemaNode) getSchemaNode(schema, "test", "augmentUint32");
157 AugmentationSchema augmentationSchema = getAugmentationSchemaForChild(containerNode,
158 augmentUint32SchemaNode.getQName());
160 AugmentationNode augmentation = Builders.augmentationBuilder(augmentationSchema)
161 .withChild(Builders.<Integer>leafBuilder(augmentUint32SchemaNode).withValue(11).build()).build();
163 builder.withChild(augmentation);
165 // This should fail with schema, since the leaf comes from augmentation
166 // builder.withChild(ImmutableLeafNodeSchemaAwareBuilder.<Integer>get(augmentUint32SchemaNode).withValue(11).build());
168 LeafSchemaNode augumentString1SchemaNode = (LeafSchemaNode) getSchemaNode(schema, "test", "augmentString1");
169 LeafSchemaNode augumentString2SchemaNode = (LeafSchemaNode) getSchemaNode(schema, "test", "augmentString2");
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")
181 builder.withChild(choice);
183 // This should fail, child from case
184 // builder.withChild(Builders.<String>leafBuilder(augumentString1SchemaNode).withValue("case1")
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;
194 throw new IllegalStateException("Unable to find child augmentation in " + containerNode);
197 private YangInstanceIdentifier.NodeWithValue getNodeWithValueIdentifier(final String localName, final Object value) {
198 return new YangInstanceIdentifier.NodeWithValue(getQName(localName), value);
201 private QName getQName(final String localName) {
202 String namespace = "namespace";
203 return new QName(URI.create(namespace), localName);
206 private YangInstanceIdentifier.NodeIdentifier getNodeIdentifier(final String localName) {
207 return new YangInstanceIdentifier.NodeIdentifier(getQName(localName));
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);
218 throw new IllegalStateException("Unable to find child node " + childNodeName);
221 private static DataSchemaNode findChildNode(final Iterable<DataSchemaNode> children, final String name) {
222 List<DataNodeContainer> containers = Lists.newArrayList();
224 for (DataSchemaNode dataSchemaNode : children) {
225 if (dataSchemaNode.getQName().getLocalName().equals(name)) {
226 return dataSchemaNode;
228 if (dataSchemaNode instanceof DataNodeContainer) {
229 containers.add((DataNodeContainer) dataSchemaNode);
230 } else if (dataSchemaNode instanceof ChoiceSchemaNode) {
231 containers.addAll(((ChoiceSchemaNode) dataSchemaNode).getCases());
235 for (DataNodeContainer container : containers) {
236 DataSchemaNode retVal = findChildNode(container.getChildNodes(), name);
237 if (retVal != null) {