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.Collections;
17 import org.junit.Before;
18 import org.junit.Test;
19 import org.opendaylight.yangtools.yang.common.QName;
20 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
21 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
22 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
23 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue;
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.AugmentationSchemaNode;
35 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
36 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
37 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
38 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
39 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
40 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
41 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
42 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
43 import org.opendaylight.yangtools.yang.model.api.Module;
44 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
45 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
46 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
48 public class NormalizedDataBuilderTest {
50 private ContainerSchemaNode containerNode;
51 private SchemaContext schema;
54 public void setUp() throws URISyntaxException, FileNotFoundException, ReactorException {
55 final File resourceFile = new File(getClass().getResource(
56 "test.yang").toURI());
57 schema = YangParserTestUtils.parseYangFiles(resourceFile);
58 containerNode = (ContainerSchemaNode) getSchemaNode(schema, "test", "container");
62 public void testSchemaUnaware() {
64 DataContainerNodeBuilder<NodeIdentifier, ContainerNode> builder = Builders
65 .containerBuilder().withNodeIdentifier(getNodeIdentifier("container"));
68 LeafNode<String> leafChild = Builders.<String>leafBuilder().withNodeIdentifier(getNodeIdentifier("leaf"))
69 .withValue("String").build();
70 builder.withChild(leafChild);
73 LeafSetNode<Integer> leafList = Builders.<Integer>leafSetBuilder()
74 .withNodeIdentifier(getNodeIdentifier("leaf"))
77 Builders.<Integer>leafSetEntryBuilder()
78 .withNodeIdentifier(getNodeWithValueIdentifier("leaf", 3)).withValue(3).build())
80 builder.withChild(leafList);
83 MapEntryNode listChild1 = Builders
86 Builders.<Integer>leafBuilder().withNodeIdentifier(getNodeIdentifier("uint32InList"))
87 .withValue(1).build())
88 .withChild(Builders.containerBuilder().withNodeIdentifier(getNodeIdentifier("containerInList")).build())
90 new NodeIdentifierWithPredicates(
91 getNodeIdentifier("list").getNodeType(), Collections.singletonMap(
92 getNodeIdentifier("uint32InList").getNodeType(), 1))).build();
94 MapNode list = Builders.mapBuilder().withChild(listChild1).withNodeIdentifier(getNodeIdentifier("list"))
96 builder.withChild(list);
98 AugmentationNode augmentation = Builders
99 .augmentationBuilder()
101 new AugmentationIdentifier(Collections.singleton(getQName("augmentUint32"))))
103 Builders.<Integer>leafBuilder().withNodeIdentifier(getNodeIdentifier("augmentUint32"))
104 .withValue(11).build()).build();
106 builder.withChild(augmentation);
108 // This works without schema (adding child from augment as a direct
110 builder.withChild(Builders.<Integer>leafBuilder().withNodeIdentifier(getNodeIdentifier("augmentUint32"))
111 .withValue(11).build());
115 public void testSchemaAware() {
116 DataContainerNodeBuilder<NodeIdentifier, ContainerNode> builder = Builders.containerBuilder(containerNode);
118 LeafSchemaNode schemaNode = (LeafSchemaNode) getSchemaNode(schema, "test", "uint32");
119 LeafNode<String> leafChild = Builders.<String>leafBuilder(schemaNode).withValue("String").build();
120 builder.withChild(leafChild);
122 LeafListSchemaNode leafListSchemaNode = (LeafListSchemaNode) getSchemaNode(schema, "test", "leafList");
123 LeafSetNode<Integer> leafList = Builders.<Integer>leafSetBuilder(leafListSchemaNode).withChildValue(1)
124 .withChild(Builders.<Integer>leafSetEntryBuilder(leafListSchemaNode).withValue(3).build()).build();
125 builder.withChild(leafList);
127 ListSchemaNode listSchema = (ListSchemaNode) getSchemaNode(schema, "test", "list");
128 LeafSchemaNode uint32InListSchemaNode = (LeafSchemaNode) getSchemaNode(schema, "test", "uint32InList");
129 ContainerSchemaNode containerInListSchemaNode = (ContainerSchemaNode) getSchemaNode(schema, "test",
132 MapEntryNode listChild1 = Builders.mapEntryBuilder(listSchema)
133 .withChild(Builders.<Integer>leafBuilder(uint32InListSchemaNode).withValue(1).build())
134 .withChild(Builders.containerBuilder(containerInListSchemaNode).build()).build();
136 MapNode list = ImmutableMapNodeSchemaAwareBuilder.create(listSchema).withChild(listChild1).build();
137 builder.withChild(list);
139 LeafSchemaNode augmentUint32SchemaNode = (LeafSchemaNode) getSchemaNode(schema, "test", "augmentUint32");
140 AugmentationSchemaNode augmentationSchema = getAugmentationSchemaForChild(containerNode,
141 augmentUint32SchemaNode.getQName());
143 AugmentationNode augmentation = Builders.augmentationBuilder(augmentationSchema)
144 .withChild(Builders.<Integer>leafBuilder(augmentUint32SchemaNode).withValue(11).build()).build();
146 builder.withChild(augmentation);
148 // This should fail with schema, since the leaf comes from augmentation
149 // builder.withChild(ImmutableLeafNodeSchemaAwareBuilder.<Integer>get(augmentUint32SchemaNode).withValue(11)
152 LeafSchemaNode augumentString1SchemaNode = (LeafSchemaNode) getSchemaNode(schema, "test", "augmentString1");
153 LeafSchemaNode augumentString2SchemaNode = (LeafSchemaNode) getSchemaNode(schema, "test", "augmentString2");
155 ChoiceSchemaNode choice1SchemaNode = (ChoiceSchemaNode) getSchemaNode(schema, "test", "choice");
156 ChoiceNode choice = ImmutableChoiceNodeSchemaAwareBuilder.create(choice1SchemaNode)
157 .withChild(Builders.<String>leafBuilder(augumentString1SchemaNode).withValue("case1").build())
158 // This should fail, since child node belongs to different case
159 // .withChild(Builders.<String>leafBuilder(augumentString2SchemaNode).withValue("case2")
163 builder.withChild(choice);
165 // This should fail, child from case
166 // builder.withChild(Builders.<String>leafBuilder(augumentString1SchemaNode).withValue("case1")
170 private static AugmentationSchemaNode getAugmentationSchemaForChild(final ContainerSchemaNode containerNode,
172 for (AugmentationSchemaNode augmentationSchema : containerNode.getAvailableAugmentations()) {
173 if (augmentationSchema.getDataChildByName(qname) != null) {
174 return augmentationSchema;
177 throw new IllegalStateException("Unable to find child augmentation in " + containerNode);
180 private static <T> NodeWithValue<T> getNodeWithValueIdentifier(final String localName, final T value) {
181 return new NodeWithValue<>(getQName(localName), value);
184 private static QName getQName(final String localName) {
185 return QName.create(URI.create("namespace"), localName);
188 private static NodeIdentifier getNodeIdentifier(final String localName) {
189 return new NodeIdentifier(getQName(localName));
192 public static DataSchemaNode getSchemaNode(final SchemaContext context, final String moduleName,
193 final String childNodeName) {
194 for (Module module : context.getModules()) {
195 if (module.getName().equals(moduleName)) {
196 DataSchemaNode found = findChildNode(module, childNodeName);
197 checkState(found != null, "Unable to find %s", childNodeName);
201 throw new IllegalStateException("Unable to find child node " + childNodeName);
204 private static DataSchemaNode findChildNode(final DataNodeContainer container, final String name) {
205 for (DataSchemaNode dataSchemaNode : container.getChildNodes()) {
206 if (dataSchemaNode.getQName().getLocalName().equals(name)) {
207 return dataSchemaNode;
209 if (dataSchemaNode instanceof DataNodeContainer) {
210 DataSchemaNode retVal = findChildNode((DataNodeContainer) dataSchemaNode, name);
211 if (retVal != null) {
214 } else if (dataSchemaNode instanceof ChoiceSchemaNode) {
215 for (ChoiceCaseNode caseNode : ((ChoiceSchemaNode) dataSchemaNode).getCases().values()) {
216 DataSchemaNode retVal = findChildNode(caseNode, name);
217 if (retVal != null) {