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