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