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