Cleanup DataTree interfaces and InMemmoryDataTreeFactory
[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 java.io.File;
13 import java.net.URI;
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;
45
46 public class NormalizedDataBuilderTest {
47
48     private ContainerSchemaNode containerNode;
49     private SchemaContext schema;
50
51     @Before
52     public void setUp() throws URISyntaxException {
53         schema = YangParserTestUtils.parseYangFiles(new File(getClass().getResource("test.yang").toURI()));
54         containerNode = (ContainerSchemaNode) getSchemaNode(schema, "test", "container");
55     }
56
57     @Test
58     public void testSchemaUnaware() {
59         // Container
60         DataContainerNodeBuilder<NodeIdentifier, ContainerNode> builder = Builders
61                 .containerBuilder().withNodeIdentifier(getNodeIdentifier("container"));
62
63         // leaf
64         LeafNode<String> leafChild = Builders.<String>leafBuilder().withNodeIdentifier(getNodeIdentifier("leaf"))
65                 .withValue("String").build();
66         builder.withChild(leafChild);
67
68         // leafList
69         LeafSetNode<Integer> leafList = Builders.<Integer>leafSetBuilder()
70                 .withNodeIdentifier(getNodeIdentifier("leaf"))
71                 .withChildValue(1)
72                 .withChild(
73                         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(
86                         new NodeIdentifierWithPredicates(
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(Collections.singleton(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.getDataChildByName(qname) != null) {
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 }