8073f21ab608384621bdac48a9c39b74bd4ce9d0
[yangtools.git] / yang / yang-data-impl / src / test / java / org / opendaylight / yangtools / yang / data / impl / schema / NormalizedDataBuilderTest.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.data.impl.schema;
9
10 import static com.google.common.base.Preconditions.checkState;
11
12 import com.google.common.collect.ImmutableSet;
13 import java.io.File;
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;
46
47 public class NormalizedDataBuilderTest {
48
49     private ContainerSchemaNode containerNode;
50     private SchemaContext schema;
51
52     @Before
53     public void setUp() throws URISyntaxException {
54         schema = YangParserTestUtils.parseYangFiles(new File(getClass().getResource("test.yang").toURI()));
55         containerNode = (ContainerSchemaNode) getSchemaNode(schema, "test", "container");
56     }
57
58     @Test
59     public void testSchemaUnaware() {
60         // Container
61         DataContainerNodeBuilder<NodeIdentifier, ContainerNode> builder = Builders
62                 .containerBuilder().withNodeIdentifier(getNodeIdentifier("container"));
63
64         // leaf
65         LeafNode<String> leafChild = Builders.<String>leafBuilder().withNodeIdentifier(getNodeIdentifier("leaf"))
66                 .withValue("String").build();
67         builder.withChild(leafChild);
68
69         // leafList
70         LeafSetNode<Integer> leafList = Builders.<Integer>leafSetBuilder()
71                 .withNodeIdentifier(getNodeIdentifier("leaf"))
72                 .withChildValue(1)
73                 .withChild(Builders.<Integer>leafSetEntryBuilder()
74                         .withNodeIdentifier(getNodeWithValueIdentifier("leaf", 3)).withValue(3).build())
75                 .build();
76         builder.withChild(leafList);
77
78         // list
79         MapEntryNode listChild1 = Builders
80                 .mapEntryBuilder()
81                 .withChild(
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();
88
89         MapNode list = Builders.mapBuilder().withChild(listChild1).withNodeIdentifier(getNodeIdentifier("list"))
90                 .build();
91         builder.withChild(list);
92
93         AugmentationNode augmentation = Builders
94                 .augmentationBuilder()
95                 .withNodeIdentifier(
96                         new AugmentationIdentifier(ImmutableSet.of(getQName("augmentUint32"))))
97                 .withChild(
98                         Builders.<Integer>leafBuilder().withNodeIdentifier(getNodeIdentifier("augmentUint32"))
99                                 .withValue(11).build()).build();
100
101         builder.withChild(augmentation);
102
103         // This works without schema (adding child from augment as a direct
104         // child)
105         builder.withChild(Builders.<Integer>leafBuilder().withNodeIdentifier(getNodeIdentifier("augmentUint32"))
106                 .withValue(11).build());
107     }
108
109     @Test
110     public void testSchemaAware() {
111         DataContainerNodeBuilder<NodeIdentifier, ContainerNode> builder = Builders.containerBuilder(containerNode);
112
113         LeafSchemaNode schemaNode = (LeafSchemaNode) getSchemaNode(schema, "test", "uint32");
114         LeafNode<String> leafChild = Builders.<String>leafBuilder(schemaNode).withValue("String").build();
115         builder.withChild(leafChild);
116
117         LeafListSchemaNode leafListSchemaNode = (LeafListSchemaNode) getSchemaNode(schema, "test", "leafList");
118         LeafSetNode<Integer> leafList = Builders.<Integer>leafSetBuilder(leafListSchemaNode)
119                 .withChildValue(1)
120                 .withChild(Builders.<Integer>leafSetEntryBuilder(leafListSchemaNode).withValue(3).build())
121                 .build();
122         builder.withChild(leafList);
123
124         ListSchemaNode listSchema = (ListSchemaNode) getSchemaNode(schema, "test", "list");
125         LeafSchemaNode uint32InListSchemaNode = (LeafSchemaNode) getSchemaNode(schema, "test", "uint32InList");
126         ContainerSchemaNode containerInListSchemaNode = (ContainerSchemaNode) getSchemaNode(schema, "test",
127                 "containerInList");
128
129         MapEntryNode listChild1 = Builders.mapEntryBuilder(listSchema)
130                 .withChild(Builders.<Integer>leafBuilder(uint32InListSchemaNode).withValue(1).build())
131                 .withChild(Builders.containerBuilder(containerInListSchemaNode).build()).build();
132
133         MapNode list = ImmutableMapNodeSchemaAwareBuilder.create(listSchema).withChild(listChild1).build();
134         builder.withChild(list);
135
136         LeafSchemaNode augmentUint32SchemaNode = (LeafSchemaNode) getSchemaNode(schema, "test", "augmentUint32");
137         AugmentationSchemaNode augmentationSchema = getAugmentationSchemaForChild(containerNode,
138                 augmentUint32SchemaNode.getQName());
139
140         AugmentationNode augmentation = Builders.augmentationBuilder(augmentationSchema)
141                 .withChild(Builders.<Integer>leafBuilder(augmentUint32SchemaNode).withValue(11).build()).build();
142
143         builder.withChild(augmentation);
144
145         // This should fail with schema, since the leaf comes from augmentation
146         // builder.withChild(ImmutableLeafNodeSchemaAwareBuilder.<Integer>get(augmentUint32SchemaNode).withValue(11)
147         // .build());
148
149         LeafSchemaNode augumentString1SchemaNode = (LeafSchemaNode) getSchemaNode(schema, "test", "augmentString1");
150         LeafSchemaNode augumentString2SchemaNode = (LeafSchemaNode) getSchemaNode(schema, "test", "augmentString2");
151
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")
157                 // .build())
158                 .build();
159
160         builder.withChild(choice);
161
162         // This should fail, child from case
163         // builder.withChild(Builders.<String>leafBuilder(augumentString1SchemaNode).withValue("case1")
164         // .build());
165     }
166
167     private static AugmentationSchemaNode getAugmentationSchemaForChild(final ContainerSchemaNode containerNode,
168             final QName qname) {
169         for (AugmentationSchemaNode augmentationSchema : containerNode.getAvailableAugmentations()) {
170             if (augmentationSchema.findDataChildByName(qname).isPresent()) {
171                 return augmentationSchema;
172             }
173         }
174         throw new IllegalStateException("Unable to find child augmentation in " + containerNode);
175     }
176
177     private static <T> NodeWithValue<T> getNodeWithValueIdentifier(final String localName, final T value) {
178         return new NodeWithValue<>(getQName(localName), value);
179     }
180
181     private static QName getQName(final String localName) {
182         return QName.create(XMLNamespace.of("namespace"), localName);
183     }
184
185     private static NodeIdentifier getNodeIdentifier(final String localName) {
186         return new NodeIdentifier(getQName(localName));
187     }
188
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);
195                 return found;
196             }
197         }
198         throw new IllegalStateException("Unable to find child node " + childNodeName);
199     }
200
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;
205             }
206             if (dataSchemaNode instanceof DataNodeContainer) {
207                 DataSchemaNode retVal = findChildNode((DataNodeContainer) dataSchemaNode, name);
208                 if (retVal != null) {
209                     return retVal;
210                 }
211             } else if (dataSchemaNode instanceof ChoiceSchemaNode) {
212                 for (CaseSchemaNode caseNode : ((ChoiceSchemaNode) dataSchemaNode).getCases()) {
213                     DataSchemaNode retVal = findChildNode(caseNode, name);
214                     if (retVal != null) {
215                         return retVal;
216                     }
217                 }
218             }
219         }
220         return null;
221     }
222 }