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;
14 import java.net.URISyntaxException;
15 import java.util.Collections;
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.YangInstanceIdentifier.AugmentationIdentifier;
20 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
21 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
22 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue;
23 import org.opendaylight.yangtools.yang.data.api.schema.AugmentationNode;
24 import org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode;
25 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
26 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
27 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetNode;
28 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
29 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
30 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeBuilder;
31 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableChoiceNodeSchemaAwareBuilder;
32 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableMapNodeSchemaAwareBuilder;
33 import org.opendaylight.yangtools.yang.model.api.AugmentationSchemaNode;
34 import org.opendaylight.yangtools.yang.model.api.CaseSchemaNode;
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.test.util.YangParserTestUtils;
46 public class NormalizedDataBuilderTest {
48 private ContainerSchemaNode containerNode;
49 private SchemaContext schema;
52 public void setUp() throws URISyntaxException {
53 schema = YangParserTestUtils.parseYangFiles(new File(getClass().getResource("test.yang").toURI()));
54 containerNode = (ContainerSchemaNode) getSchemaNode(schema, "test", "container");
58 public void testSchemaUnaware() {
60 DataContainerNodeBuilder<NodeIdentifier, ContainerNode> builder = Builders
61 .containerBuilder().withNodeIdentifier(getNodeIdentifier("container"));
64 LeafNode<String> leafChild = Builders.<String>leafBuilder().withNodeIdentifier(getNodeIdentifier("leaf"))
65 .withValue("String").build();
66 builder.withChild(leafChild);
69 LeafSetNode<Integer> leafList = Builders.<Integer>leafSetBuilder()
70 .withNodeIdentifier(getNodeIdentifier("leaf"))
73 Builders.<Integer>leafSetEntryBuilder()
74 .withNodeIdentifier(getNodeWithValueIdentifier("leaf", 3)).withValue(3).build())
76 builder.withChild(leafList);
79 MapEntryNode listChild1 = Builders
82 Builders.<Integer>leafBuilder().withNodeIdentifier(getNodeIdentifier("uint32InList"))
83 .withValue(1).build())
84 .withChild(Builders.containerBuilder().withNodeIdentifier(getNodeIdentifier("containerInList")).build())
86 new NodeIdentifierWithPredicates(
87 getNodeIdentifier("list").getNodeType(), Collections.singletonMap(
88 getNodeIdentifier("uint32InList").getNodeType(), 1))).build();
90 MapNode list = Builders.mapBuilder().withChild(listChild1).withNodeIdentifier(getNodeIdentifier("list"))
92 builder.withChild(list);
94 AugmentationNode augmentation = Builders
95 .augmentationBuilder()
97 new AugmentationIdentifier(Collections.singleton(getQName("augmentUint32"))))
99 Builders.<Integer>leafBuilder().withNodeIdentifier(getNodeIdentifier("augmentUint32"))
100 .withValue(11).build()).build();
102 builder.withChild(augmentation);
104 // This works without schema (adding child from augment as a direct
106 builder.withChild(Builders.<Integer>leafBuilder().withNodeIdentifier(getNodeIdentifier("augmentUint32"))
107 .withValue(11).build());
111 public void testSchemaAware() {
112 DataContainerNodeBuilder<NodeIdentifier, ContainerNode> builder = Builders.containerBuilder(containerNode);
114 LeafSchemaNode schemaNode = (LeafSchemaNode) getSchemaNode(schema, "test", "uint32");
115 LeafNode<String> leafChild = Builders.<String>leafBuilder(schemaNode).withValue("String").build();
116 builder.withChild(leafChild);
118 LeafListSchemaNode leafListSchemaNode = (LeafListSchemaNode) getSchemaNode(schema, "test", "leafList");
119 LeafSetNode<Integer> leafList = Builders.<Integer>leafSetBuilder(leafListSchemaNode).withChildValue(1)
120 .withChild(Builders.<Integer>leafSetEntryBuilder(leafListSchemaNode).withValue(3).build()).build();
121 builder.withChild(leafList);
123 ListSchemaNode listSchema = (ListSchemaNode) getSchemaNode(schema, "test", "list");
124 LeafSchemaNode uint32InListSchemaNode = (LeafSchemaNode) getSchemaNode(schema, "test", "uint32InList");
125 ContainerSchemaNode containerInListSchemaNode = (ContainerSchemaNode) getSchemaNode(schema, "test",
128 MapEntryNode listChild1 = Builders.mapEntryBuilder(listSchema)
129 .withChild(Builders.<Integer>leafBuilder(uint32InListSchemaNode).withValue(1).build())
130 .withChild(Builders.containerBuilder(containerInListSchemaNode).build()).build();
132 MapNode list = ImmutableMapNodeSchemaAwareBuilder.create(listSchema).withChild(listChild1).build();
133 builder.withChild(list);
135 LeafSchemaNode augmentUint32SchemaNode = (LeafSchemaNode) getSchemaNode(schema, "test", "augmentUint32");
136 AugmentationSchemaNode augmentationSchema = getAugmentationSchemaForChild(containerNode,
137 augmentUint32SchemaNode.getQName());
139 AugmentationNode augmentation = Builders.augmentationBuilder(augmentationSchema)
140 .withChild(Builders.<Integer>leafBuilder(augmentUint32SchemaNode).withValue(11).build()).build();
142 builder.withChild(augmentation);
144 // This should fail with schema, since the leaf comes from augmentation
145 // builder.withChild(ImmutableLeafNodeSchemaAwareBuilder.<Integer>get(augmentUint32SchemaNode).withValue(11)
148 LeafSchemaNode augumentString1SchemaNode = (LeafSchemaNode) getSchemaNode(schema, "test", "augmentString1");
149 LeafSchemaNode augumentString2SchemaNode = (LeafSchemaNode) getSchemaNode(schema, "test", "augmentString2");
151 ChoiceSchemaNode choice1SchemaNode = (ChoiceSchemaNode) getSchemaNode(schema, "test", "choice");
152 ChoiceNode choice = ImmutableChoiceNodeSchemaAwareBuilder.create(choice1SchemaNode)
153 .withChild(Builders.<String>leafBuilder(augumentString1SchemaNode).withValue("case1").build())
154 // This should fail, since child node belongs to different case
155 // .withChild(Builders.<String>leafBuilder(augumentString2SchemaNode).withValue("case2")
159 builder.withChild(choice);
161 // This should fail, child from case
162 // builder.withChild(Builders.<String>leafBuilder(augumentString1SchemaNode).withValue("case1")
166 private static AugmentationSchemaNode getAugmentationSchemaForChild(final ContainerSchemaNode containerNode,
168 for (AugmentationSchemaNode augmentationSchema : containerNode.getAvailableAugmentations()) {
169 if (augmentationSchema.getDataChildByName(qname) != null) {
170 return augmentationSchema;
173 throw new IllegalStateException("Unable to find child augmentation in " + containerNode);
176 private static <T> NodeWithValue<T> getNodeWithValueIdentifier(final String localName, final T value) {
177 return new NodeWithValue<>(getQName(localName), value);
180 private static QName getQName(final String localName) {
181 return QName.create(URI.create("namespace"), localName);
184 private static NodeIdentifier getNodeIdentifier(final String localName) {
185 return new NodeIdentifier(getQName(localName));
188 public static DataSchemaNode getSchemaNode(final SchemaContext context, final String moduleName,
189 final String childNodeName) {
190 for (Module module : context.getModules()) {
191 if (module.getName().equals(moduleName)) {
192 DataSchemaNode found = findChildNode(module, childNodeName);
193 checkState(found != null, "Unable to find %s", childNodeName);
197 throw new IllegalStateException("Unable to find child node " + childNodeName);
200 private static DataSchemaNode findChildNode(final DataNodeContainer container, final String name) {
201 for (DataSchemaNode dataSchemaNode : container.getChildNodes()) {
202 if (dataSchemaNode.getQName().getLocalName().equals(name)) {
203 return dataSchemaNode;
205 if (dataSchemaNode instanceof DataNodeContainer) {
206 DataSchemaNode retVal = findChildNode((DataNodeContainer) dataSchemaNode, name);
207 if (retVal != null) {
210 } else if (dataSchemaNode instanceof ChoiceSchemaNode) {
211 for (CaseSchemaNode caseNode : ((ChoiceSchemaNode) dataSchemaNode).getCases().values()) {
212 DataSchemaNode retVal = findChildNode(caseNode, name);
213 if (retVal != null) {