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