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