2 * Copyright (c) 2016 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 static com.google.common.base.Preconditions.checkState;
13 import java.io.FileNotFoundException;
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;
49 public class NormalizedDataBuilderTest {
51 private ContainerSchemaNode containerNode;
52 private SchemaContext schema;
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");
63 public void testSchemaUnaware() {
65 DataContainerNodeBuilder<NodeIdentifier, ContainerNode> builder = Builders
66 .containerBuilder().withNodeIdentifier(getNodeIdentifier("container"));
69 LeafNode<String> leafChild = Builders.<String>leafBuilder().withNodeIdentifier(getNodeIdentifier("leaf"))
70 .withValue("String").build();
71 builder.withChild(leafChild);
74 LeafSetNode<Integer> leafList = Builders.<Integer>leafSetBuilder()
75 .withNodeIdentifier(getNodeIdentifier("leaf"))
78 Builders.<Integer>leafSetEntryBuilder()
79 .withNodeIdentifier(getNodeWithValueIdentifier("leaf", 3)).withValue(3).build())
81 builder.withChild(leafList);
84 MapEntryNode listChild1 = Builders
87 Builders.<Integer>leafBuilder().withNodeIdentifier(getNodeIdentifier("uint32InList"))
88 .withValue(1).build())
89 .withChild(Builders.containerBuilder().withNodeIdentifier(getNodeIdentifier("containerInList")).build())
91 new NodeIdentifierWithPredicates(
92 getNodeIdentifier("list").getNodeType(), Collections.singletonMap(
93 getNodeIdentifier("uint32InList").getNodeType(), 1))).build();
95 MapNode list = Builders.mapBuilder().withChild(listChild1).withNodeIdentifier(getNodeIdentifier("list"))
97 builder.withChild(list);
99 AugmentationNode augmentation = Builders
100 .augmentationBuilder()
102 new AugmentationIdentifier(Collections.singleton(getQName("augmentUint32"))))
104 Builders.<Integer>leafBuilder().withNodeIdentifier(getNodeIdentifier("augmentUint32"))
105 .withValue(11).build()).build();
107 builder.withChild(augmentation);
109 // This works without schema (adding child from augment as a direct
111 builder.withChild(Builders.<Integer>leafBuilder().withNodeIdentifier(getNodeIdentifier("augmentUint32"))
112 .withValue(11).build());
116 public void testSchemaAware() {
117 DataContainerNodeBuilder<NodeIdentifier, ContainerNode> builder = Builders.containerBuilder(containerNode);
119 LeafSchemaNode schemaNode = (LeafSchemaNode) getSchemaNode(schema, "test", "uint32");
120 LeafNode<String> leafChild = Builders.<String>leafBuilder(schemaNode).withValue("String").build();
121 builder.withChild(leafChild);
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);
128 ListSchemaNode listSchema = (ListSchemaNode) getSchemaNode(schema, "test", "list");
129 LeafSchemaNode uint32InListSchemaNode = (LeafSchemaNode) getSchemaNode(schema, "test", "uint32InList");
130 ContainerSchemaNode containerInListSchemaNode = (ContainerSchemaNode) getSchemaNode(schema, "test",
133 MapEntryNode listChild1 = Builders.mapEntryBuilder(listSchema)
134 .withChild(Builders.<Integer>leafBuilder(uint32InListSchemaNode).withValue(1).build())
135 .withChild(Builders.containerBuilder(containerInListSchemaNode).build()).build();
137 MapNode list = ImmutableMapNodeSchemaAwareBuilder.create(listSchema).withChild(listChild1).build();
138 builder.withChild(list);
140 LeafSchemaNode augmentUint32SchemaNode = (LeafSchemaNode) getSchemaNode(schema, "test", "augmentUint32");
141 AugmentationSchema augmentationSchema = getAugmentationSchemaForChild(containerNode,
142 augmentUint32SchemaNode.getQName());
144 AugmentationNode augmentation = Builders.augmentationBuilder(augmentationSchema)
145 .withChild(Builders.<Integer>leafBuilder(augmentUint32SchemaNode).withValue(11).build()).build();
147 builder.withChild(augmentation);
149 // This should fail with schema, since the leaf comes from augmentation
150 // builder.withChild(ImmutableLeafNodeSchemaAwareBuilder.<Integer>get(augmentUint32SchemaNode).withValue(11)
153 LeafSchemaNode augumentString1SchemaNode = (LeafSchemaNode) getSchemaNode(schema, "test", "augmentString1");
154 LeafSchemaNode augumentString2SchemaNode = (LeafSchemaNode) getSchemaNode(schema, "test", "augmentString2");
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")
164 builder.withChild(choice);
166 // This should fail, child from case
167 // builder.withChild(Builders.<String>leafBuilder(augumentString1SchemaNode).withValue("case1")
171 private static AugmentationSchema getAugmentationSchemaForChild(final ContainerSchemaNode containerNode,
173 for (AugmentationSchema augmentationSchema : containerNode.getAvailableAugmentations()) {
174 if (augmentationSchema.getDataChildByName(qname) != null) {
175 return augmentationSchema;
178 throw new IllegalStateException("Unable to find child augmentation in " + containerNode);
181 private static <T> NodeWithValue<T> getNodeWithValueIdentifier(final String localName, final T value) {
182 return new NodeWithValue<>(getQName(localName), value);
185 private static QName getQName(final String localName) {
186 return QName.create(URI.create("namespace"), localName);
189 private static NodeIdentifier getNodeIdentifier(final String localName) {
190 return new NodeIdentifier(getQName(localName));
193 public static DataSchemaNode getSchemaNode(final SchemaContext context, final String moduleName,
194 final String childNodeName) {
195 for (Module module : context.getModules()) {
196 if (module.getName().equals(moduleName)) {
197 DataSchemaNode found = findChildNode(module.getChildNodes(), childNodeName);
198 checkState(found != null, "Unable to find %s", childNodeName);
202 throw new IllegalStateException("Unable to find child node " + childNodeName);
205 private static DataSchemaNode findChildNode(final Iterable<DataSchemaNode> children, final String name) {
206 List<DataNodeContainer> containers = new ArrayList<>();
208 for (DataSchemaNode dataSchemaNode : children) {
209 if (dataSchemaNode.getQName().getLocalName().equals(name)) {
210 return dataSchemaNode;
212 if (dataSchemaNode instanceof DataNodeContainer) {
213 containers.add((DataNodeContainer) dataSchemaNode);
214 } else if (dataSchemaNode instanceof ChoiceSchemaNode) {
215 containers.addAll(((ChoiceSchemaNode) dataSchemaNode).getCases());
219 for (DataNodeContainer container : containers) {
220 DataSchemaNode retVal = findChildNode(container.getChildNodes(), name);
221 if (retVal != null) {