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;
12 import com.google.common.collect.ImmutableSet;
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.common.XMLNamespace;
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.CaseSchemaNode;
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.test.util.YangParserTestUtils;
47 public class NormalizedDataBuilderTest {
49 private ContainerSchemaNode containerNode;
50 private SchemaContext schema;
53 public void setUp() throws URISyntaxException {
54 schema = YangParserTestUtils.parseYangFiles(new File(getClass().getResource("test.yang").toURI()));
55 containerNode = (ContainerSchemaNode) getSchemaNode(schema, "test", "container");
59 public void testSchemaUnaware() {
61 DataContainerNodeBuilder<NodeIdentifier, ContainerNode> builder = Builders
62 .containerBuilder().withNodeIdentifier(getNodeIdentifier("container"));
65 LeafNode<String> leafChild = Builders.<String>leafBuilder().withNodeIdentifier(getNodeIdentifier("leaf"))
66 .withValue("String").build();
67 builder.withChild(leafChild);
70 LeafSetNode<Integer> leafList = Builders.<Integer>leafSetBuilder()
71 .withNodeIdentifier(getNodeIdentifier("leaf"))
73 .withChild(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())
85 .withNodeIdentifier(NodeIdentifierWithPredicates.of(
86 getNodeIdentifier("list").getNodeType(), Collections.singletonMap(
87 getNodeIdentifier("uint32InList").getNodeType(), 1))).build();
89 MapNode list = Builders.mapBuilder().withChild(listChild1).withNodeIdentifier(getNodeIdentifier("list"))
91 builder.withChild(list);
93 AugmentationNode augmentation = Builders
94 .augmentationBuilder()
96 new AugmentationIdentifier(ImmutableSet.of(getQName("augmentUint32"))))
98 Builders.<Integer>leafBuilder().withNodeIdentifier(getNodeIdentifier("augmentUint32"))
99 .withValue(11).build()).build();
101 builder.withChild(augmentation);
103 // This works without schema (adding child from augment as a direct
105 builder.withChild(Builders.<Integer>leafBuilder().withNodeIdentifier(getNodeIdentifier("augmentUint32"))
106 .withValue(11).build());
110 public void testSchemaAware() {
111 DataContainerNodeBuilder<NodeIdentifier, ContainerNode> builder = Builders.containerBuilder(containerNode);
113 LeafSchemaNode schemaNode = (LeafSchemaNode) getSchemaNode(schema, "test", "uint32");
114 LeafNode<String> leafChild = Builders.<String>leafBuilder(schemaNode).withValue("String").build();
115 builder.withChild(leafChild);
117 LeafListSchemaNode leafListSchemaNode = (LeafListSchemaNode) getSchemaNode(schema, "test", "leafList");
118 LeafSetNode<Integer> leafList = Builders.<Integer>leafSetBuilder(leafListSchemaNode)
120 .withChild(Builders.<Integer>leafSetEntryBuilder(leafListSchemaNode).withValue(3).build())
122 builder.withChild(leafList);
124 ListSchemaNode listSchema = (ListSchemaNode) getSchemaNode(schema, "test", "list");
125 LeafSchemaNode uint32InListSchemaNode = (LeafSchemaNode) getSchemaNode(schema, "test", "uint32InList");
126 ContainerSchemaNode containerInListSchemaNode = (ContainerSchemaNode) getSchemaNode(schema, "test",
129 MapEntryNode listChild1 = Builders.mapEntryBuilder(listSchema)
130 .withChild(Builders.<Integer>leafBuilder(uint32InListSchemaNode).withValue(1).build())
131 .withChild(Builders.containerBuilder(containerInListSchemaNode).build()).build();
133 MapNode list = ImmutableMapNodeSchemaAwareBuilder.create(listSchema).withChild(listChild1).build();
134 builder.withChild(list);
136 LeafSchemaNode augmentUint32SchemaNode = (LeafSchemaNode) getSchemaNode(schema, "test", "augmentUint32");
137 AugmentationSchemaNode augmentationSchema = getAugmentationSchemaForChild(containerNode,
138 augmentUint32SchemaNode.getQName());
140 AugmentationNode augmentation = Builders.augmentationBuilder(augmentationSchema)
141 .withChild(Builders.<Integer>leafBuilder(augmentUint32SchemaNode).withValue(11).build()).build();
143 builder.withChild(augmentation);
145 // This should fail with schema, since the leaf comes from augmentation
146 // builder.withChild(ImmutableLeafNodeSchemaAwareBuilder.<Integer>get(augmentUint32SchemaNode).withValue(11)
149 LeafSchemaNode augumentString1SchemaNode = (LeafSchemaNode) getSchemaNode(schema, "test", "augmentString1");
150 LeafSchemaNode augumentString2SchemaNode = (LeafSchemaNode) getSchemaNode(schema, "test", "augmentString2");
152 ChoiceSchemaNode choice1SchemaNode = (ChoiceSchemaNode) getSchemaNode(schema, "test", "choice");
153 ChoiceNode choice = ImmutableChoiceNodeSchemaAwareBuilder.create(choice1SchemaNode)
154 .withChild(Builders.<String>leafBuilder(augumentString1SchemaNode).withValue("case1").build())
155 // This should fail, since child node belongs to different case
156 // .withChild(Builders.<String>leafBuilder(augumentString2SchemaNode).withValue("case2")
160 builder.withChild(choice);
162 // This should fail, child from case
163 // builder.withChild(Builders.<String>leafBuilder(augumentString1SchemaNode).withValue("case1")
167 private static AugmentationSchemaNode getAugmentationSchemaForChild(final ContainerSchemaNode containerNode,
169 for (AugmentationSchemaNode augmentationSchema : containerNode.getAvailableAugmentations()) {
170 if (augmentationSchema.findDataChildByName(qname).isPresent()) {
171 return augmentationSchema;
174 throw new IllegalStateException("Unable to find child augmentation in " + containerNode);
177 private static <T> NodeWithValue<T> getNodeWithValueIdentifier(final String localName, final T value) {
178 return new NodeWithValue<>(getQName(localName), value);
181 private static QName getQName(final String localName) {
182 return QName.create(XMLNamespace.of("namespace"), localName);
185 private static NodeIdentifier getNodeIdentifier(final String localName) {
186 return new NodeIdentifier(getQName(localName));
189 public static DataSchemaNode getSchemaNode(final SchemaContext context, final String moduleName,
190 final String childNodeName) {
191 for (Module module : context.getModules()) {
192 if (module.getName().equals(moduleName)) {
193 DataSchemaNode found = findChildNode(module, childNodeName);
194 checkState(found != null, "Unable to find %s", childNodeName);
198 throw new IllegalStateException("Unable to find child node " + childNodeName);
201 private static DataSchemaNode findChildNode(final DataNodeContainer container, final String name) {
202 for (DataSchemaNode dataSchemaNode : container.getChildNodes()) {
203 if (dataSchemaNode.getQName().getLocalName().equals(name)) {
204 return dataSchemaNode;
206 if (dataSchemaNode instanceof DataNodeContainer) {
207 DataSchemaNode retVal = findChildNode((DataNodeContainer) dataSchemaNode, name);
208 if (retVal != null) {
211 } else if (dataSchemaNode instanceof ChoiceSchemaNode) {
212 for (CaseSchemaNode caseNode : ((ChoiceSchemaNode) dataSchemaNode).getCases()) {
213 DataSchemaNode retVal = findChildNode(caseNode, name);
214 if (retVal != null) {