Merge branch 'master' of ../controller
[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.URI;
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.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(
74                         Builders.<Integer>leafSetEntryBuilder()
75                                 .withNodeIdentifier(getNodeWithValueIdentifier("leaf", 3)).withValue(3).build())
76                 .build();
77         builder.withChild(leafList);
78
79         // list
80         MapEntryNode listChild1 = Builders
81                 .mapEntryBuilder()
82                 .withChild(
83                         Builders.<Integer>leafBuilder().withNodeIdentifier(getNodeIdentifier("uint32InList"))
84                                 .withValue(1).build())
85                 .withChild(Builders.containerBuilder().withNodeIdentifier(getNodeIdentifier("containerInList")).build())
86                 .withNodeIdentifier(NodeIdentifierWithPredicates.of(
87                                 getNodeIdentifier("list").getNodeType(), Collections.singletonMap(
88                                 getNodeIdentifier("uint32InList").getNodeType(), 1))).build();
89
90         MapNode list = Builders.mapBuilder().withChild(listChild1).withNodeIdentifier(getNodeIdentifier("list"))
91                 .build();
92         builder.withChild(list);
93
94         AugmentationNode augmentation = Builders
95                 .augmentationBuilder()
96                 .withNodeIdentifier(
97                         new AugmentationIdentifier(ImmutableSet.of(getQName("augmentUint32"))))
98                 .withChild(
99                         Builders.<Integer>leafBuilder().withNodeIdentifier(getNodeIdentifier("augmentUint32"))
100                                 .withValue(11).build()).build();
101
102         builder.withChild(augmentation);
103
104         // This works without schema (adding child from augment as a direct
105         // child)
106         builder.withChild(Builders.<Integer>leafBuilder().withNodeIdentifier(getNodeIdentifier("augmentUint32"))
107                 .withValue(11).build());
108     }
109
110     @Test
111     public void testSchemaAware() {
112         DataContainerNodeBuilder<NodeIdentifier, ContainerNode> builder = Builders.containerBuilder(containerNode);
113
114         LeafSchemaNode schemaNode = (LeafSchemaNode) getSchemaNode(schema, "test", "uint32");
115         LeafNode<String> leafChild = Builders.<String>leafBuilder(schemaNode).withValue("String").build();
116         builder.withChild(leafChild);
117
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);
122
123         ListSchemaNode listSchema = (ListSchemaNode) getSchemaNode(schema, "test", "list");
124         LeafSchemaNode uint32InListSchemaNode = (LeafSchemaNode) getSchemaNode(schema, "test", "uint32InList");
125         ContainerSchemaNode containerInListSchemaNode = (ContainerSchemaNode) getSchemaNode(schema, "test",
126                 "containerInList");
127
128         MapEntryNode listChild1 = Builders.mapEntryBuilder(listSchema)
129                 .withChild(Builders.<Integer>leafBuilder(uint32InListSchemaNode).withValue(1).build())
130                 .withChild(Builders.containerBuilder(containerInListSchemaNode).build()).build();
131
132         MapNode list = ImmutableMapNodeSchemaAwareBuilder.create(listSchema).withChild(listChild1).build();
133         builder.withChild(list);
134
135         LeafSchemaNode augmentUint32SchemaNode = (LeafSchemaNode) getSchemaNode(schema, "test", "augmentUint32");
136         AugmentationSchemaNode augmentationSchema = getAugmentationSchemaForChild(containerNode,
137                 augmentUint32SchemaNode.getQName());
138
139         AugmentationNode augmentation = Builders.augmentationBuilder(augmentationSchema)
140                 .withChild(Builders.<Integer>leafBuilder(augmentUint32SchemaNode).withValue(11).build()).build();
141
142         builder.withChild(augmentation);
143
144         // This should fail with schema, since the leaf comes from augmentation
145         // builder.withChild(ImmutableLeafNodeSchemaAwareBuilder.<Integer>get(augmentUint32SchemaNode).withValue(11)
146         // .build());
147
148         LeafSchemaNode augumentString1SchemaNode = (LeafSchemaNode) getSchemaNode(schema, "test", "augmentString1");
149         LeafSchemaNode augumentString2SchemaNode = (LeafSchemaNode) getSchemaNode(schema, "test", "augmentString2");
150
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")
156                 // .build())
157                 .build();
158
159         builder.withChild(choice);
160
161         // This should fail, child from case
162         // builder.withChild(Builders.<String>leafBuilder(augumentString1SchemaNode).withValue("case1")
163         // .build());
164     }
165
166     private static AugmentationSchemaNode getAugmentationSchemaForChild(final ContainerSchemaNode containerNode,
167             final QName qname) {
168         for (AugmentationSchemaNode augmentationSchema : containerNode.getAvailableAugmentations()) {
169             if (augmentationSchema.findDataChildByName(qname).isPresent()) {
170                 return augmentationSchema;
171             }
172         }
173         throw new IllegalStateException("Unable to find child augmentation in " + containerNode);
174     }
175
176     private static <T> NodeWithValue<T> getNodeWithValueIdentifier(final String localName, final T value) {
177         return new NodeWithValue<>(getQName(localName), value);
178     }
179
180     private static QName getQName(final String localName) {
181         return QName.create(URI.create("namespace"), localName);
182     }
183
184     private static NodeIdentifier getNodeIdentifier(final String localName) {
185         return new NodeIdentifier(getQName(localName));
186     }
187
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);
194                 return found;
195             }
196         }
197         throw new IllegalStateException("Unable to find child node " + childNodeName);
198     }
199
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;
204             }
205             if (dataSchemaNode instanceof DataNodeContainer) {
206                 DataSchemaNode retVal = findChildNode((DataNodeContainer) dataSchemaNode, name);
207                 if (retVal != null) {
208                     return retVal;
209                 }
210             } else if (dataSchemaNode instanceof ChoiceSchemaNode) {
211                 for (CaseSchemaNode caseNode : ((ChoiceSchemaNode) dataSchemaNode).getCases().values()) {
212                     DataSchemaNode retVal = findChildNode(caseNode, name);
213                     if (retVal != null) {
214                         return retVal;
215                     }
216                 }
217             }
218         }
219         return null;
220     }
221 }