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