Introduce WhenConditionAware
[yangtools.git] / yang / yang-parser-impl / src / test / java / org / opendaylight / yangtools / yang / stmt / AugmentTest.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.stmt;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertTrue;
14
15 import java.net.URI;
16 import java.util.ArrayList;
17 import java.util.Collection;
18 import java.util.Collections;
19 import java.util.List;
20 import java.util.Set;
21 import org.junit.Test;
22 import org.opendaylight.yangtools.yang.common.QName;
23 import org.opendaylight.yangtools.yang.common.QNameModule;
24 import org.opendaylight.yangtools.yang.common.Revision;
25 import org.opendaylight.yangtools.yang.common.YangConstants;
26 import org.opendaylight.yangtools.yang.model.api.AugmentationSchemaNode;
27 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
28 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
29 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
30 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
31 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
32 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
33 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
34 import org.opendaylight.yangtools.yang.model.api.Module;
35 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
36 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
37 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
38 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.TypeUtils;
39
40 public class AugmentTest {
41     private static final QNameModule FOO = QNameModule.create(
42         URI.create("urn:opendaylight.foo"), Revision.of("2013-10-13"));
43     private static final QNameModule BAR = QNameModule.create(
44         URI.create("urn:opendaylight.bar"), Revision.of("2013-10-14"));
45     private static final QNameModule BAZ = QNameModule.create(
46         URI.create("urn:opendaylight.baz"), Revision.of("2013-10-15"));
47
48     private static final QName Q0 = QName.create(BAR, "interfaces");
49     private static final QName Q1 = QName.create(BAR, "ifEntry");
50     private static final QName Q2 = QName.create(BAZ, "augment-holder");
51
52     @Test
53     public void testAugmentParsing() throws Exception {
54         final SchemaContext context = TestUtils.loadModules(getClass().getResource("/augment-test/augment-in-augment")
55             .toURI());
56         final List<QName> qnames = new ArrayList<>();
57         qnames.add(Q0);
58         qnames.add(Q1);
59         qnames.add(Q2);
60
61         // foo.yang
62         final Module module1 = TestUtils.findModule(context, "foo").get();
63         Set<AugmentationSchemaNode> augmentations = module1.getAugmentations();
64         assertEquals(1, augmentations.size());
65         final AugmentationSchemaNode augment = augmentations.iterator().next();
66         assertNotNull(augment);
67
68         SchemaPath expectedSchemaPath = SchemaPath.create(qnames, true);
69         assertEquals(expectedSchemaPath, augment.getTargetPath());
70
71         final Collection<DataSchemaNode> augmentChildren = augment.getChildNodes();
72         assertEquals(4, augmentChildren.size());
73         for (final DataSchemaNode dsn : augmentChildren) {
74             TestUtils.checkIsAugmenting(dsn, false);
75         }
76
77         final LeafSchemaNode ds0ChannelNumber = (LeafSchemaNode) augment.getDataChildByName(QName.create(
78                 module1.getQNameModule(), "ds0ChannelNumber"));
79         final LeafSchemaNode interfaceId = (LeafSchemaNode) augment.getDataChildByName(QName.create(
80                 module1.getQNameModule(), "interface-id"));
81         final ContainerSchemaNode schemas = (ContainerSchemaNode) augment.getDataChildByName(QName.create(
82                 module1.getQNameModule(), "schemas"));
83         final ChoiceSchemaNode odl = (ChoiceSchemaNode) augment.getDataChildByName(QName.create(
84                 module1.getQNameModule(), "odl"));
85
86         assertNotNull(ds0ChannelNumber);
87         assertNotNull(interfaceId);
88         assertNotNull(schemas);
89         assertNotNull(odl);
90
91         // leaf ds0ChannelNumber
92         QName qname = QName.create(FOO, "ds0ChannelNumber");
93         qnames.add(qname);
94         assertEquals(qname, ds0ChannelNumber.getQName());
95         expectedSchemaPath = SchemaPath.create(qnames, true);
96         assertEquals(expectedSchemaPath, ds0ChannelNumber.getPath());
97         assertFalse(ds0ChannelNumber.isAugmenting());
98         // type of leaf ds0ChannelNumber
99         final QName typeQName = QName.create(YangConstants.RFC6020_YANG_MODULE, TypeUtils.STRING);
100         final List<QName> typePath = Collections.singletonList(typeQName);
101         expectedSchemaPath = SchemaPath.create(typePath, true);
102         assertEquals(expectedSchemaPath, ds0ChannelNumber.getType().getPath());
103
104         // leaf interface-id
105         qname = QName.create(FOO, "interface-id");
106         assertEquals(qname, interfaceId.getQName());
107         qnames.set(3, qname);
108         expectedSchemaPath = SchemaPath.create(qnames, true);
109         assertEquals(expectedSchemaPath, interfaceId.getPath());
110         assertFalse(interfaceId.isAugmenting());
111
112         // container schemas
113         qname = QName.create(FOO, "schemas");
114         assertEquals(qname, schemas.getQName());
115         qnames.set(3, qname);
116         expectedSchemaPath = SchemaPath.create(qnames, true);
117         assertEquals(expectedSchemaPath, schemas.getPath());
118         assertFalse(schemas.isAugmenting());
119
120         // choice odl
121         qname = QName.create(FOO, "odl");
122         assertEquals(qname, odl.getQName());
123         qnames.set(3, qname);
124         expectedSchemaPath = SchemaPath.create(qnames, true);
125         assertEquals(expectedSchemaPath, odl.getPath());
126         assertFalse(odl.isAugmenting());
127
128         // baz.yang
129         final Module module3 = TestUtils.findModule(context, "baz").get();
130         augmentations = module3.getAugmentations();
131         assertEquals(3, augmentations.size());
132         AugmentationSchemaNode augment1 = null;
133         AugmentationSchemaNode augment2 = null;
134         AugmentationSchemaNode augment3 = null;
135         for (final AugmentationSchemaNode as : augmentations) {
136             if (!as.getWhenCondition().isPresent()) {
137                 augment3 = as;
138             } else if ("if:ifType='ds0'".equals(as.getWhenCondition().get().toString())) {
139                 augment1 = as;
140             } else if ("if:ifType='ds2'".equals(as.getWhenCondition().get().toString())) {
141                 augment2 = as;
142             }
143         }
144         assertNotNull(augment1);
145         assertNotNull(augment2);
146         assertNotNull(augment3);
147
148         assertEquals(1, augment1.getChildNodes().size());
149         final ContainerSchemaNode augmentHolder = (ContainerSchemaNode) augment1.getDataChildByName(QName.create(
150                 module3.getQNameModule(), "augment-holder"));
151         assertNotNull(augmentHolder);
152
153         assertEquals(1, augment2.getChildNodes().size());
154         final ContainerSchemaNode augmentHolder2 = (ContainerSchemaNode) augment2.getDataChildByName(QName.create(
155                 module3.getQNameModule(), "augment-holder2"));
156         assertNotNull(augmentHolder2);
157
158         assertEquals(1, augment3.getChildNodes().size());
159         final ChoiceCaseNode pause = (ChoiceCaseNode) augment3.getDataChildByName(QName.create(
160                 module3.getQNameModule(), "pause"));
161         assertNotNull(pause);
162     }
163
164     @Test
165     public void testAugmentResolving() throws Exception {
166         final SchemaContext context = TestUtils.loadModules(getClass().getResource("/augment-test/augment-in-augment")
167             .toURI());
168         final Module module2 = TestUtils.findModule(context, "bar").get();
169         final ContainerSchemaNode interfaces = (ContainerSchemaNode) module2.getDataChildByName(QName.create(
170                 module2.getQNameModule(), "interfaces"));
171         final ListSchemaNode ifEntry = (ListSchemaNode) interfaces.getDataChildByName(QName.create(
172                 module2.getQNameModule(), "ifEntry"));
173
174         final List<QName> qnames = new ArrayList<>();
175         qnames.add(Q0);
176         qnames.add(Q1);
177         qnames.add(Q2);
178
179         // baz.yang
180         // augment "/br:interfaces/br:ifEntry" {
181         final ContainerSchemaNode augmentHolder = (ContainerSchemaNode) ifEntry.getDataChildByName(QName.create(BAZ,
182                 "augment-holder"));
183         TestUtils.checkIsAugmenting(augmentHolder, true);
184         assertEquals(Q2, augmentHolder.getQName());
185         assertEquals(SchemaPath.create(qnames, true), augmentHolder.getPath());
186
187         // foo.yang
188         // augment "/br:interfaces/br:ifEntry/bz:augment-holder"
189         final LeafSchemaNode ds0ChannelNumber = (LeafSchemaNode) augmentHolder.getDataChildByName(QName.create(FOO,
190                 "ds0ChannelNumber"));
191         final LeafSchemaNode interfaceId = (LeafSchemaNode) augmentHolder.getDataChildByName(QName.create(FOO,
192                 "interface-id"));
193         final ContainerSchemaNode schemas = (ContainerSchemaNode) augmentHolder.getDataChildByName(QName.create(FOO,
194                 "schemas"));
195         final ChoiceSchemaNode odl = (ChoiceSchemaNode) augmentHolder.getDataChildByName(QName.create(FOO, "odl"));
196
197         assertNotNull(ds0ChannelNumber);
198         assertNotNull(interfaceId);
199         assertNotNull(schemas);
200         assertNotNull(odl);
201
202         // leaf ds0ChannelNumber
203         QName qname = QName.create(FOO, "ds0ChannelNumber");
204         assertEquals(qname, ds0ChannelNumber.getQName());
205         qnames.add(qname);
206         assertEquals(SchemaPath.create(qnames, true), ds0ChannelNumber.getPath());
207
208         // leaf interface-id
209         qname = QName.create(FOO, "interface-id");
210         assertEquals(qname, interfaceId.getQName());
211         qnames.set(3, qname);
212         assertEquals(SchemaPath.create(qnames, true), interfaceId.getPath());
213
214         // container schemas
215         qname = QName.create(FOO, "schemas");
216         assertEquals(qname, schemas.getQName());
217         qnames.set(3, qname);
218         assertEquals(SchemaPath.create(qnames, true), schemas.getPath());
219
220         // choice odl
221         qname = QName.create(FOO, "odl");
222         assertEquals(qname, odl.getQName());
223         qnames.set(3, qname);
224         assertEquals(SchemaPath.create(qnames, true), odl.getPath());
225     }
226
227     @Test
228     public void testAugmentedChoice() throws Exception {
229         final SchemaContext context = TestUtils.loadModules(getClass().getResource("/augment-test/augment-in-augment")
230             .toURI());
231         final Module module2 = TestUtils.findModule(context, "bar").get();
232         final ContainerSchemaNode interfaces = (ContainerSchemaNode) module2.getDataChildByName(QName.create(
233                 module2.getQNameModule(), "interfaces"));
234         final ListSchemaNode ifEntry = (ListSchemaNode) interfaces.getDataChildByName(QName.create(
235                 module2.getQNameModule(), "ifEntry"));
236         final ContainerSchemaNode augmentedHolder = (ContainerSchemaNode) ifEntry.getDataChildByName(QName.create(
237                 BAZ, "augment-holder"));
238         TestUtils.checkIsAugmenting(augmentedHolder, true);
239
240         // foo.yang
241         // augment "/br:interfaces/br:ifEntry/bz:augment-holder"
242         final ChoiceSchemaNode odl = (ChoiceSchemaNode) augmentedHolder.getDataChildByName(QName.create(FOO, "odl"));
243         assertNotNull(odl);
244         final Set<ChoiceCaseNode> cases = odl.getCases();
245         assertEquals(4, cases.size());
246
247         ChoiceCaseNode id = null;
248         ChoiceCaseNode node1 = null;
249         ChoiceCaseNode node2 = null;
250         ChoiceCaseNode node3 = null;
251
252         for (final ChoiceCaseNode ccn : cases) {
253             if ("id".equals(ccn.getQName().getLocalName())) {
254                 id = ccn;
255             } else if ("node1".equals(ccn.getQName().getLocalName())) {
256                 node1 = ccn;
257             } else if ("node2".equals(ccn.getQName().getLocalName())) {
258                 node2 = ccn;
259             } else if ("node3".equals(ccn.getQName().getLocalName())) {
260                 node3 = ccn;
261             }
262         }
263
264         assertNotNull(id);
265         assertNotNull(node1);
266         assertNotNull(node2);
267         assertNotNull(node3);
268
269         final List<QName> qnames = new ArrayList<>();
270         qnames.add(Q0);
271         qnames.add(Q1);
272         qnames.add(Q2);
273         qnames.add(QName.create(FOO, "odl"));
274
275         // case id
276         QName qname = QName.create(FOO, "id");
277         assertEquals(qname, id.getQName());
278         qnames.add(qname);
279         assertEquals(SchemaPath.create(qnames, true), id.getPath());
280         final Collection<DataSchemaNode> idChildren = id.getChildNodes();
281         assertEquals(1, idChildren.size());
282
283         // case node1
284         qname = QName.create(FOO, "node1");
285         assertEquals(qname, node1.getQName());
286         qnames.set(4, qname);
287         assertEquals(SchemaPath.create(qnames, true), node1.getPath());
288         final Collection<DataSchemaNode> node1Children = node1.getChildNodes();
289         assertTrue(node1Children.isEmpty());
290
291         // case node2
292         qname = QName.create(FOO, "node2");
293         assertEquals(qname, node2.getQName());
294         qnames.set(4, qname);
295         assertEquals(SchemaPath.create(qnames, true), node2.getPath());
296         final Collection<DataSchemaNode> node2Children = node2.getChildNodes();
297         assertTrue(node2Children.isEmpty());
298
299         // case node3
300         qname = QName.create(FOO, "node3");
301         assertEquals(qname, node3.getQName());
302         qnames.set(4, qname);
303         assertEquals(SchemaPath.create(qnames, true), node3.getPath());
304         final Collection<DataSchemaNode> node3Children = node3.getChildNodes();
305         assertEquals(1, node3Children.size());
306
307         // test cases
308         qnames.clear();
309         qnames.add(Q0);
310         qnames.add(Q1);
311         qnames.add(Q2);
312         qnames.add(QName.create(FOO, "odl"));
313
314         // case id child
315         qnames.add(QName.create(FOO, "id"));
316         qnames.add(QName.create(FOO, "id"));
317         final LeafSchemaNode caseIdChild = (LeafSchemaNode) idChildren.iterator().next();
318         assertNotNull(caseIdChild);
319         assertEquals(SchemaPath.create(qnames, true), caseIdChild.getPath());
320
321         // case node3 child
322         qnames.set(4, QName.create(FOO, "node3"));
323         qnames.set(5, QName.create(FOO, "node3"));
324         final ContainerSchemaNode caseNode3Child = (ContainerSchemaNode) node3Children.iterator().next();
325         assertNotNull(caseNode3Child);
326         assertEquals(SchemaPath.create(qnames, true), caseNode3Child.getPath());
327     }
328
329     @Test
330     public void testAugmentRpc() throws Exception {
331         final SchemaContext context = TestUtils.loadModules(getClass().getResource("/augment-test/rpc").toURI());
332         final URI NS_BAR = URI.create("urn:opendaylight:bar");
333         final URI NS_FOO = URI.create("urn:opendaylight:foo");
334         final Revision revision = Revision.of("2013-10-11");
335         final Module bar = TestUtils.findModule(context, "bar").get();
336         final Set<RpcDefinition> rpcs = bar.getRpcs();
337         assertEquals(2, rpcs.size());
338
339         RpcDefinition submit = null;
340         for (final RpcDefinition rpc : rpcs) {
341             if ("submit".equals(rpc.getQName().getLocalName())) {
342                 submit = rpc;
343                 break;
344             }
345         }
346         assertNotNull(submit);
347
348         final QName submitQName = QName.create(NS_BAR, revision, "submit");
349         assertEquals(submitQName, submit.getQName());
350         final ContainerSchemaNode input = submit.getInput();
351         final QName inputQName = QName.create(NS_BAR, revision, "input");
352         assertEquals(inputQName, input.getQName());
353         final ChoiceSchemaNode arguments = (ChoiceSchemaNode) input.getDataChildByName(QName.create(NS_BAR, revision,
354                 "arguments"));
355         final QName argumentsQName = QName.create(NS_BAR, revision, "arguments");
356         assertEquals(argumentsQName, arguments.getQName());
357         assertFalse(arguments.isAugmenting());
358         final Set<ChoiceCaseNode> cases = arguments.getCases();
359         assertEquals(3, cases.size());
360
361         ChoiceCaseNode attach = null;
362         ChoiceCaseNode create = null;
363         ChoiceCaseNode destroy = null;
364         for (final ChoiceCaseNode child : cases) {
365             if ("attach".equals(child.getQName().getLocalName())) {
366                 attach = child;
367             } else if ("create".equals(child.getQName().getLocalName())) {
368                 create = child;
369             } else if ("destroy".equals(child.getQName().getLocalName())) {
370                 destroy = child;
371             }
372         }
373         assertNotNull(attach);
374         assertNotNull(create);
375         assertNotNull(destroy);
376
377         assertTrue(attach.isAugmenting());
378         assertTrue(create.isAugmenting());
379         assertTrue(destroy.isAugmenting());
380
381         final QName[] qnames = new QName[4];
382         qnames[0] = submitQName;
383         qnames[1] = inputQName;
384         qnames[2] = argumentsQName;
385
386         // case attach
387         qnames[3] = QName.create(NS_FOO, revision, "attach");
388         assertEquals(qnames[3], attach.getQName());
389         assertEquals(SchemaPath.create(true, qnames), attach.getPath());
390         final Collection<DataSchemaNode> attachChildren = attach.getChildNodes();
391         assertEquals(1, attachChildren.size());
392
393         // case create
394         qnames[3] = QName.create(NS_FOO, revision, "create");
395         assertEquals(qnames[3], create.getQName());
396         assertEquals(SchemaPath.create(true, qnames), create.getPath());
397         final Collection<DataSchemaNode> createChildren = create.getChildNodes();
398         assertEquals(1, createChildren.size());
399
400         // case attach
401         qnames[3] = QName.create(NS_FOO, revision, "destroy");
402         assertEquals(qnames[3], destroy.getQName());
403         assertEquals(SchemaPath.create(true, qnames), destroy.getPath());
404         final Collection<DataSchemaNode> destroyChildren = destroy.getChildNodes();
405         assertEquals(1, destroyChildren.size());
406     }
407
408     @Test
409     public void testAugmentInUsesResolving() throws Exception {
410         final SchemaContext context = TestUtils.loadModules(getClass().getResource("/augment-test/augment-in-uses")
411             .toURI());
412         assertEquals(1, context.getModules().size());
413
414         final Module test = context.getModules().iterator().next();
415         final DataNodeContainer links = (DataNodeContainer) test.getDataChildByName(QName.create(test.getQNameModule(),
416                 "links"));
417         final DataNodeContainer link = (DataNodeContainer) links.getDataChildByName(QName.create(test.getQNameModule(),
418                 "link"));
419         final DataNodeContainer nodes = (DataNodeContainer) link.getDataChildByName(QName.create(test.getQNameModule(),
420                 "nodes"));
421         final ContainerSchemaNode node = (ContainerSchemaNode) nodes.getDataChildByName(QName.create(
422                 test.getQNameModule(), "node"));
423         final Set<AugmentationSchemaNode> augments = node.getAvailableAugmentations();
424         assertEquals(1, augments.size());
425         assertEquals(1, node.getChildNodes().size());
426         final LeafSchemaNode id = (LeafSchemaNode) node.getDataChildByName(QName.create(test.getQNameModule(), "id"));
427         assertTrue(id.isAugmenting());
428     }
429
430 }