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