Bug 4540: Yang parser exceptions should follow consistent path
[yangtools.git] / yang / yang-parser-impl / src / test / java / org / opendaylight / yangtools / yang / stmt / retest / YangParserWithContextTest.java
1 /*
2  * Copyright (c) 2013 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.retest;
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 java.io.File;
15 import java.net.URI;
16 import java.text.DateFormat;
17 import java.text.SimpleDateFormat;
18 import java.util.ArrayList;
19 import java.util.Arrays;
20 import java.util.Collections;
21 import java.util.Date;
22 import java.util.List;
23 import java.util.Map;
24 import java.util.Set;
25 import org.junit.Test;
26 import org.opendaylight.yangtools.yang.common.QName;
27 import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil;
28 import org.opendaylight.yangtools.yang.model.api.AnyXmlSchemaNode;
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.DataSchemaNode;
32 import org.opendaylight.yangtools.yang.model.api.Deviation;
33 import org.opendaylight.yangtools.yang.model.api.Deviation.Deviate;
34 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
35 import org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode;
36 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
37 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
38 import org.opendaylight.yangtools.yang.model.api.Module;
39 import org.opendaylight.yangtools.yang.model.api.MustDefinition;
40 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
41 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
42 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
43 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
44 import org.opendaylight.yangtools.yang.model.api.UsesNode;
45 import org.opendaylight.yangtools.yang.model.api.type.RangeConstraint;
46 import org.opendaylight.yangtools.yang.model.api.type.UnsignedIntegerTypeDefinition;
47 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
48 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor;
49 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangInferencePipeline;
50 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangStatementSourceImpl;
51 import org.opendaylight.yangtools.yang.stmt.test.StmtTestUtils;
52
53 public class YangParserWithContextTest {
54     private final DateFormat simpleDateFormat = new SimpleDateFormat(
55             "yyyy-MM-dd");
56     private final YangParserImpl parser = new YangParserImpl();
57
58     private static final YangStatementSourceImpl BAR = new YangStatementSourceImpl(
59             "/model/bar.yang", false);
60     private static final YangStatementSourceImpl BAZ = new YangStatementSourceImpl(
61             "/model/baz.yang", false);
62     private static final YangStatementSourceImpl FOO = new YangStatementSourceImpl(
63             "/model/foo.yang", false);
64     private static final YangStatementSourceImpl SUBFOO = new YangStatementSourceImpl(
65             "/model/subfoo.yang", false);
66
67     private static final YangStatementSourceImpl[] IETF = new YangStatementSourceImpl[] {
68         new YangStatementSourceImpl("/ietf/iana-afn-safi@2012-06-04.yang", false),
69         new YangStatementSourceImpl("/ietf/iana-if-type@2012-06-05.yang", false),
70         new YangStatementSourceImpl("/ietf/iana-timezones@2012-07-09.yang", false),
71         new YangStatementSourceImpl("/ietf/ietf-inet-types@2010-09-24.yang", false),
72         new YangStatementSourceImpl("/ietf/ietf-yang-types@2010-09-24.yang", false),
73         new YangStatementSourceImpl("/ietf/network-topology@2013-07-12.yang", false),
74         new YangStatementSourceImpl("/ietf/network-topology@2013-10-21.yang", false)
75     };
76
77     @Test
78     public void testTypeFromContext() throws Exception {
79
80         CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
81                 .newBuild();
82
83         YangStatementSourceImpl types = new YangStatementSourceImpl(
84                 "/types/custom-types-test@2012-4-4.yang", false);
85         YangStatementSourceImpl test1 = new YangStatementSourceImpl(
86                 "/context-test/test1.yang", false);
87
88         StmtTestUtils.addSources(reactor, IETF);
89         StmtTestUtils.addSources(reactor, types, test1);
90
91         SchemaContext context = reactor.buildEffective();
92
93         Module module = context.findModuleByName("test1",
94                 SimpleDateFormatUtil.getRevisionFormat().parse("2013-06-18"));
95         assertNotNull(module);
96
97         LeafSchemaNode leaf = (LeafSchemaNode) module.getDataChildByName("id");
98
99         assertTrue(leaf.getType() instanceof UnsignedIntegerTypeDefinition);
100         UnsignedIntegerTypeDefinition leafType = (UnsignedIntegerTypeDefinition) leaf.getType();
101         QName qname = leafType.getQName();
102         assertEquals(URI.create("urn:simple.demo.test1"), qname.getNamespace());
103         assertEquals(simpleDateFormat.parse("2013-06-18"), qname.getRevision());
104         assertEquals("port-number", qname.getLocalName());
105
106         UnsignedIntegerTypeDefinition leafBaseType = leafType.getBaseType();
107         qname = leafBaseType.getQName();
108         assertEquals(URI.create("urn:ietf:params:xml:ns:yang:ietf-inet-types"),
109                 qname.getNamespace());
110         assertEquals(simpleDateFormat.parse("2010-09-24"), qname.getRevision());
111         assertEquals("port-number", qname.getLocalName());
112
113         UnsignedIntegerTypeDefinition dscpExt = (UnsignedIntegerTypeDefinition) TestUtils.findTypedef(
114                 module.getTypeDefinitions(), "dscp-ext");
115         List<RangeConstraint> ranges = dscpExt.getRangeConstraints();
116         assertEquals(1, ranges.size());
117         RangeConstraint range = ranges.get(0);
118         assertEquals(0, range.getMin().intValue());
119         assertEquals(63, range.getMax().intValue());
120     }
121
122     @Test
123     public void testUsesFromContext() throws Exception {
124         CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
125                 .newBuild();
126
127         YangStatementSourceImpl test2 = new YangStatementSourceImpl(
128                 "/context-test/test2.yang", false);
129         StmtTestUtils.addSources(reactor, BAZ, FOO, BAR, SUBFOO, test2);
130         SchemaContext context = reactor.buildEffective();
131
132         Module testModule = context.findModuleByName("test2",
133                 SimpleDateFormatUtil.getRevisionFormat().parse("2013-06-18"));
134         assertNotNull(testModule);
135
136         Module contextModule = context
137                 .findModuleByNamespace(URI.create("urn:opendaylight.baz"))
138                 .iterator().next();
139         assertNotNull(contextModule);
140         Set<GroupingDefinition> groupings = contextModule.getGroupings();
141         assertEquals(1, groupings.size());
142         GroupingDefinition grouping = groupings.iterator().next();
143
144         // get node containing uses
145         ContainerSchemaNode peer = (ContainerSchemaNode) testModule
146                 .getDataChildByName("peer");
147         ContainerSchemaNode destination = (ContainerSchemaNode) peer
148                 .getDataChildByName("destination");
149
150         // check uses
151         Set<UsesNode> uses = destination.getUses();
152         assertEquals(1, uses.size());
153
154         // check uses process
155         AnyXmlSchemaNode data_u = (AnyXmlSchemaNode) destination
156                 .getDataChildByName("data");
157         assertNotNull(data_u);
158         assertTrue(data_u.isAddedByUses());
159
160         AnyXmlSchemaNode data_g = (AnyXmlSchemaNode) grouping
161                 .getDataChildByName("data");
162         assertNotNull(data_g);
163         assertFalse(data_g.isAddedByUses());
164         assertFalse(data_u.equals(data_g));
165
166         ChoiceSchemaNode how_u = (ChoiceSchemaNode) destination
167                 .getDataChildByName("how");
168         assertNotNull(how_u);
169         assertTrue(how_u.isAddedByUses());
170
171         ChoiceSchemaNode how_g = (ChoiceSchemaNode) grouping
172                 .getDataChildByName("how");
173         assertNotNull(how_g);
174         assertFalse(how_g.isAddedByUses());
175         assertFalse(how_u.equals(how_g));
176
177         LeafSchemaNode address_u = (LeafSchemaNode) destination
178                 .getDataChildByName("address");
179         assertNotNull(address_u);
180         assertTrue(address_u.isAddedByUses());
181
182         LeafSchemaNode address_g = (LeafSchemaNode) grouping
183                 .getDataChildByName("address");
184         assertNotNull(address_g);
185         assertFalse(address_g.isAddedByUses());
186         assertFalse(address_u.equals(address_g));
187
188         ContainerSchemaNode port_u = (ContainerSchemaNode) destination
189                 .getDataChildByName("port");
190         assertNotNull(port_u);
191         assertTrue(port_u.isAddedByUses());
192
193         ContainerSchemaNode port_g = (ContainerSchemaNode) grouping
194                 .getDataChildByName("port");
195         assertNotNull(port_g);
196         assertFalse(port_g.isAddedByUses());
197         assertFalse(port_u.equals(port_g));
198
199         ListSchemaNode addresses_u = (ListSchemaNode) destination
200                 .getDataChildByName("addresses");
201         assertNotNull(addresses_u);
202         assertTrue(addresses_u.isAddedByUses());
203
204         ListSchemaNode addresses_g = (ListSchemaNode) grouping
205                 .getDataChildByName("addresses");
206         assertNotNull(addresses_g);
207         assertFalse(addresses_g.isAddedByUses());
208         assertFalse(addresses_u.equals(addresses_g));
209
210         // grouping defined by 'uses'
211         Set<GroupingDefinition> groupings_u = destination.getGroupings();
212         assertEquals(1, groupings_u.size());
213         GroupingDefinition grouping_u = groupings_u.iterator().next();
214         assertTrue(grouping_u.isAddedByUses());
215
216         // grouping defined in 'grouping' node
217         Set<GroupingDefinition> groupings_g = grouping.getGroupings();
218         assertEquals(1, groupings_g.size());
219         GroupingDefinition grouping_g = groupings_g.iterator().next();
220         assertFalse(grouping_g.isAddedByUses());
221         assertFalse(grouping_u.equals(grouping_g));
222
223         List<UnknownSchemaNode> nodes_u = destination.getUnknownSchemaNodes();
224         assertEquals(1, nodes_u.size());
225         UnknownSchemaNode node_u = nodes_u.get(0);
226         assertTrue(node_u.isAddedByUses());
227
228         List<UnknownSchemaNode> nodes_g = grouping.getUnknownSchemaNodes();
229         assertEquals(1, nodes_g.size());
230         UnknownSchemaNode node_g = nodes_g.get(0);
231         assertFalse(node_g.isAddedByUses());
232         assertFalse(node_u.equals(node_g));
233     }
234
235     @Test
236     public void testUsesRefineFromContext() throws Exception {
237
238         CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
239                 .newBuild();
240
241         YangStatementSourceImpl test2 = new YangStatementSourceImpl(
242                 "/context-test/test2.yang", false);
243         StmtTestUtils.addSources(reactor, BAZ, FOO, BAR, SUBFOO, test2);
244         SchemaContext context = reactor.buildEffective();
245
246         Module module = context.findModuleByName("test2",
247                 SimpleDateFormatUtil.getRevisionFormat().parse("2013-06-18"));
248         assertNotNull(module);
249         ContainerSchemaNode peer = (ContainerSchemaNode) module
250                 .getDataChildByName("peer");
251         ContainerSchemaNode destination = (ContainerSchemaNode) peer
252                 .getDataChildByName("destination");
253         Set<UsesNode> usesNodes = destination.getUses();
254         assertEquals(1, usesNodes.size());
255         UsesNode usesNode = usesNodes.iterator().next();
256
257         // test grouping path
258         List<QName> path = new ArrayList<>();
259         QName qname = QName.create(URI.create("urn:opendaylight.baz"),
260                 simpleDateFormat.parse("2013-02-27"), "target");
261         path.add(qname);
262         SchemaPath expectedPath = SchemaPath.create(path, true);
263         assertEquals(expectedPath, usesNode.getGroupingPath());
264
265         // test refine
266         Map<SchemaPath, SchemaNode> refines = usesNode.getRefines();
267         assertEquals(3, refines.size());
268
269         LeafSchemaNode refineLeaf = null;
270         ContainerSchemaNode refineContainer = null;
271         ListSchemaNode refineList = null;
272         for (Map.Entry<SchemaPath, SchemaNode> entry : refines.entrySet()) {
273             SchemaNode value = entry.getValue();
274             if (value instanceof LeafSchemaNode) {
275                 refineLeaf = (LeafSchemaNode) value;
276             } else if (value instanceof ContainerSchemaNode) {
277                 refineContainer = (ContainerSchemaNode) value;
278             } else if (value instanceof ListSchemaNode) {
279                 refineList = (ListSchemaNode) value;
280             }
281         }
282
283         // leaf address
284         assertNotNull(refineLeaf);
285         assertEquals("address", refineLeaf.getQName().getLocalName());
286         assertEquals("description of address defined by refine",
287                 refineLeaf.getDescription());
288         assertEquals("address reference added by refine",
289                 refineLeaf.getReference());
290         assertFalse(refineLeaf.isConfiguration());
291         assertTrue(refineLeaf.getConstraints().isMandatory());
292         Set<MustDefinition> leafMustConstraints = refineLeaf.getConstraints()
293                 .getMustConstraints();
294         assertEquals(1, leafMustConstraints.size());
295         MustDefinition leafMust = leafMustConstraints.iterator().next();
296         assertEquals(
297                 "ifType != 'ethernet' or (ifType = 'ethernet' and ifMTU = 1500)",
298                 leafMust.toString());
299
300         // container port
301         assertNotNull(refineContainer);
302         Set<MustDefinition> mustConstraints = refineContainer.getConstraints()
303                 .getMustConstraints();
304         assertTrue(mustConstraints.isEmpty());
305         assertEquals("description of port defined by refine",
306                 refineContainer.getDescription());
307         assertEquals("port reference added by refine",
308                 refineContainer.getReference());
309         assertFalse(refineContainer.isConfiguration());
310         assertTrue(refineContainer.isPresenceContainer());
311
312         // list addresses
313         assertNotNull(refineList);
314         assertEquals("description of addresses defined by refine",
315                 refineList.getDescription());
316         assertEquals("addresses reference added by refine",
317                 refineList.getReference());
318         assertFalse(refineList.isConfiguration());
319         assertEquals(2, (int) refineList.getConstraints().getMinElements());
320         assertEquals(12, (int) refineList.getConstraints().getMaxElements());
321     }
322
323     @Test
324     public void testIdentity() throws Exception {
325
326         CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
327                 .newBuild();
328
329         YangStatementSourceImpl types = new YangStatementSourceImpl(
330                 "/types/custom-types-test@2012-4-4.yang", false);
331         YangStatementSourceImpl test3 = new YangStatementSourceImpl(
332                 "/context-test/test3.yang", false);
333
334         StmtTestUtils.addSources(reactor, IETF);
335         StmtTestUtils.addSources(reactor, types, test3);
336         SchemaContext context = reactor.buildEffective();
337
338         Module module = context.findModuleByName("test3",
339                 SimpleDateFormatUtil.getRevisionFormat().parse("2013-06-18"));
340         assertNotNull(module);
341
342         Set<IdentitySchemaNode> identities = module.getIdentities();
343         assertEquals(1, identities.size());
344
345         IdentitySchemaNode identity = identities.iterator().next();
346         QName idQName = identity.getQName();
347         assertEquals(URI.create("urn:simple.demo.test3"),
348                 idQName.getNamespace());
349         assertEquals(simpleDateFormat.parse("2013-06-18"),
350                 idQName.getRevision());
351         assertEquals("pt", idQName.getLocalName());
352
353         IdentitySchemaNode baseIdentity = identity.getBaseIdentity();
354         QName idBaseQName = baseIdentity.getQName();
355         assertEquals(URI.create("urn:custom.types.demo"),
356                 idBaseQName.getNamespace());
357         assertEquals(simpleDateFormat.parse("2012-04-16"),
358                 idBaseQName.getRevision());
359         assertEquals("service-type", idBaseQName.getLocalName());
360     }
361
362     @Test
363     public void testUnknownNodes() throws Exception {
364
365         CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
366                 .newBuild();
367
368         YangStatementSourceImpl types = new YangStatementSourceImpl(
369                 "/types/custom-types-test@2012-4-4.yang", false);
370         YangStatementSourceImpl test3 = new YangStatementSourceImpl(
371                 "/context-test/test3.yang", false);
372
373         StmtTestUtils.addSources(reactor, IETF);
374         StmtTestUtils.addSources(reactor, types, test3);
375
376         SchemaContext context = reactor.buildEffective();
377
378         Module module = context.findModuleByName("test3",
379                 SimpleDateFormatUtil.getRevisionFormat().parse("2013-06-18"));
380         assertNotNull(module);
381
382         ContainerSchemaNode network = (ContainerSchemaNode) module
383                 .getDataChildByName("network");
384         List<UnknownSchemaNode> unknownNodes = network.getUnknownSchemaNodes();
385         assertEquals(1, unknownNodes.size());
386
387         UnknownSchemaNode un = unknownNodes.get(0);
388         QName unType = un.getNodeType();
389         assertEquals(URI.create("urn:custom.types.demo"), unType.getNamespace());
390         assertEquals(simpleDateFormat.parse("2012-04-16"), unType.getRevision());
391         assertEquals("mountpoint", unType.getLocalName());
392         assertEquals("point", un.getNodeParameter());
393         assertNotNull(un.getExtensionDefinition());
394     }
395
396     @Test
397     public void testAugment() throws Exception {
398         // load first module
399         String resource = "/context-augment-test/test4.yang";
400         SchemaContext context = parser.parseFiles(Collections
401                 .singleton(new File(getClass().getResource(resource).toURI())));
402
403         // load another modules and parse them against already existing context
404         File test1 = new File(getClass().getResource(
405                 "/context-augment-test/test1.yang").toURI());
406         File test2 = new File(getClass().getResource(
407                 "/context-augment-test/test2.yang").toURI());
408         File test3 = new File(getClass().getResource(
409                 "/context-augment-test/test3.yang").toURI());
410         Set<Module> modules = parser.parseFiles(
411                 Arrays.asList(test1, test2, test3), context).getModules();
412         assertNotNull(modules);
413
414         Module t4 = TestUtils.findModule(modules, "test4");
415         ContainerSchemaNode interfaces = (ContainerSchemaNode) t4
416                 .getDataChildByName("interfaces");
417         ListSchemaNode ifEntry = (ListSchemaNode) interfaces
418                 .getDataChildByName("ifEntry");
419
420         // test augmentation process
421         ContainerSchemaNode augmentHolder = (ContainerSchemaNode) ifEntry
422                 .getDataChildByName("augment-holder");
423         assertNotNull(augmentHolder);
424         DataSchemaNode ds0 = augmentHolder
425                 .getDataChildByName("ds0ChannelNumber");
426         assertNotNull(ds0);
427         DataSchemaNode interfaceId = augmentHolder
428                 .getDataChildByName("interface-id");
429         assertNotNull(interfaceId);
430         DataSchemaNode higherLayerIf = augmentHolder
431                 .getDataChildByName("higher-layer-if");
432         assertNotNull(higherLayerIf);
433         ContainerSchemaNode schemas = (ContainerSchemaNode) augmentHolder
434                 .getDataChildByName("schemas");
435         assertNotNull(schemas);
436         assertNotNull(schemas.getDataChildByName("id"));
437
438         // test augment target after augmentation: check if it is same instance
439         ListSchemaNode ifEntryAfterAugment = (ListSchemaNode) interfaces
440                 .getDataChildByName("ifEntry");
441         assertTrue(ifEntry == ifEntryAfterAugment);
442     }
443
444     @Test
445     public void testDeviation() throws Exception {
446
447         CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
448                 .newBuild();
449
450         YangStatementSourceImpl bar = new YangStatementSourceImpl(
451                 "/model/bar.yang", false);
452         YangStatementSourceImpl deviationTest = new YangStatementSourceImpl(
453                 "/context-test/deviation-test.yang", false);
454
455         StmtTestUtils.addSources(reactor, bar, deviationTest);
456         SchemaContext context = reactor.buildEffective();
457
458         Module testModule = context.findModuleByName("deviation-test",
459                 SimpleDateFormatUtil.getRevisionFormat().parse("2013-02-27"));
460         assertNotNull(testModule);
461
462         Set<Deviation> deviations = testModule.getDeviations();
463         assertEquals(1, deviations.size());
464         Deviation dev = deviations.iterator().next();
465
466         assertEquals("system/user ref", dev.getReference());
467
468         URI expectedNS = URI.create("urn:opendaylight.bar");
469         DateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
470         Date expectedRev = simpleDateFormat.parse("2013-07-03");
471         List<QName> path = new ArrayList<>();
472         path.add(QName.create(expectedNS, expectedRev, "interfaces"));
473         path.add(QName.create(expectedNS, expectedRev, "ifEntry"));
474         SchemaPath expectedPath = SchemaPath.create(path, true);
475
476         assertEquals(expectedPath, dev.getTargetPath());
477         assertEquals(Deviate.ADD, dev.getDeviate());
478     }
479
480 }