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