Bug 6961: SchemaContext.getAllModuleIdentifiers() doesnt work for submodules
[yangtools.git] / yang / yang-parser-impl / src / test / java / org / opendaylight / yangtools / yang / stmt / MoreRevisionsTest.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
9 package org.opendaylight.yangtools.yang.stmt;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertNull;
14 import static org.junit.Assert.assertTrue;
15
16 import java.io.FileNotFoundException;
17 import java.net.URI;
18 import java.net.URISyntaxException;
19 import java.text.ParseException;
20 import java.util.Date;
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.QNameModule;
25 import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil;
26 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
27 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
28 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
29 import org.opendaylight.yangtools.yang.model.api.Module;
30 import org.opendaylight.yangtools.yang.model.api.ModuleImport;
31 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
32 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
33 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
34 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
35 import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil;
36 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
37 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
38 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor;
39 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangInferencePipeline;
40 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangStatementSourceImpl;
41 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.EffectiveSchemaContext;
42
43 public class MoreRevisionsTest {
44
45     private static final YangStatementSourceImpl REVFILE = new YangStatementSourceImpl(
46             "/semantic-statement-parser/revisions/more-revisions-test.yang",
47             false);
48
49     private static final YangStatementSourceImpl TED_20130712 = new YangStatementSourceImpl(
50             "/semantic-statement-parser/two-revisions/ted@2013-07-12.yang",
51             false);
52
53     private static final YangStatementSourceImpl TED_20131021 = new YangStatementSourceImpl(
54             "/semantic-statement-parser/two-revisions/ted@2013-10-21.yang",
55             false);
56
57     private static final YangStatementSourceImpl IETF_TYPES = new YangStatementSourceImpl(
58             "/ietf/ietf-inet-types@2010-09-24.yang", false);
59
60     private static final YangStatementSourceImpl NETWORK_TOPOLOGY_20130712 = new YangStatementSourceImpl(
61             "/ietf/network-topology@2013-07-12.yang", false);
62
63     private static final YangStatementSourceImpl NETWORK_TOPOLOGY_20131021 = new YangStatementSourceImpl(
64             "/ietf/network-topology@2013-10-21.yang", false);
65
66     private static final YangStatementSourceImpl ISIS_20130712 = new YangStatementSourceImpl(
67             "/semantic-statement-parser/two-revisions/isis-topology@2013-07-12.yang",
68             false);
69
70     private static final YangStatementSourceImpl ISIS_20131021 = new YangStatementSourceImpl(
71             "/semantic-statement-parser/two-revisions/isis-topology@2013-10-21.yang",
72             false);
73
74     private static final YangStatementSourceImpl L3_20130712 = new YangStatementSourceImpl(
75             "/semantic-statement-parser/two-revisions/l3-unicast-igp-topology@2013-07-12.yang",
76             false);
77
78     private static final YangStatementSourceImpl L3_20131021 = new YangStatementSourceImpl(
79             "/semantic-statement-parser/two-revisions/l3-unicast-igp-topology@2013-10-21.yang",
80             false);
81
82     @Test
83     public void readAndParseYangFileTest() throws SourceException,
84             ReactorException {
85         CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
86                 .newBuild();
87         reactor.addSource(REVFILE);
88         EffectiveSchemaContext result = reactor.buildEffective();
89         assertNotNull(result);
90         final Module moduleByName = result.getModules().iterator().next();
91         final QNameModule qNameModule = moduleByName.getQNameModule();
92         final String formattedRevision = qNameModule.getFormattedRevision();
93         assertEquals(formattedRevision, "2015-06-07");
94     }
95
96     @Test
97     public void twoRevisionsTest() throws SourceException, ReactorException {
98         CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
99                 .newBuild();
100
101         reactor.addSources(TED_20130712, TED_20131021, IETF_TYPES);
102
103         EffectiveSchemaContext result = reactor.buildEffective();
104         assertNotNull(result);
105
106     }
107
108     @Test
109     public void twoRevisionsTest2() throws SourceException, ReactorException {
110         CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
111                 .newBuild();
112
113         reactor.addSources(NETWORK_TOPOLOGY_20130712,
114                 NETWORK_TOPOLOGY_20131021, IETF_TYPES);
115
116         EffectiveSchemaContext result = reactor.buildEffective();
117         assertNotNull(result);
118         Set<Module> modules = result.getModules();
119
120         assertEquals(3, modules.size());
121         assertEquals(2, StmtTestUtils.findModules(modules, "network-topology")
122                 .size());
123     }
124
125     @Test
126     public void moreRevisionsListKeyTest() throws SourceException,
127             ReactorException {
128         CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
129                 .newBuild();
130
131         reactor.addSources(TED_20130712, TED_20131021, ISIS_20130712,
132                 ISIS_20131021, L3_20130712, L3_20131021, IETF_TYPES,
133                 NETWORK_TOPOLOGY_20130712, NETWORK_TOPOLOGY_20131021);
134
135         EffectiveSchemaContext result = reactor.buildEffective();
136         assertNotNull(result);
137     }
138
139     @Test
140     public void multipleRevisionsTest() throws SourceException,
141             ReactorException, FileNotFoundException, URISyntaxException {
142         for (int i = 0; i < 100; i++) {
143             SchemaContext context = StmtTestUtils
144                     .parseYangSources("/semantic-statement-parser/multiple-revisions");
145             assertNotNull(context);
146         }
147     }
148
149     @Test
150     public void multipleRevisionsFullTest() throws SourceException,
151             ReactorException, FileNotFoundException, URISyntaxException,
152             ParseException {
153         for (int i = 0; i < 100; i++) {
154             SchemaContext context = StmtTestUtils
155                     .parseYangSources("/semantic-statement-parser/multiple-revisions/full");
156             assertNotNull(context);
157             assertEquals(6, context.getModules().size());
158             checkContentFullTest(context);
159         }
160     }
161
162     private static void checkContentFullTest(final SchemaContext context) throws ParseException,
163             URISyntaxException {
164
165         String yangTypesNSStr = "urn:ietf:params:xml:ns:yang:ietf-yang-types";
166         URI yangTypesNS = new URI(yangTypesNSStr);
167
168         Date rev20100924 = SimpleDateFormatUtil.getRevisionFormat().parse(
169                 "2010-09-24");
170         Date rev20130516 = SimpleDateFormatUtil.getRevisionFormat().parse(
171                 "2013-05-16");
172         Date rev20130715 = SimpleDateFormatUtil.getRevisionFormat().parse(
173                 "2013-07-15");
174
175         final QNameModule yangTypes_20100924 = QNameModule.create(yangTypesNS,
176                 rev20100924);
177         final QNameModule yangTypes_20130516 = QNameModule.create(yangTypesNS,
178                 rev20130516);
179         final QNameModule yangTypes_20130715 = QNameModule.create(yangTypesNS,
180                 rev20130715);
181
182         final QName dateTimeTypeDef_20100924 = QName.create(yangTypes_20100924,
183                 "date-and-time");
184         final QName dateTimeTypeDef_20130516 = QName.create(yangTypes_20130516,
185                 "date-and-time");
186         final QName dateTimeTypeDef_20130715 = QName.create(yangTypes_20130715,
187                 "date-and-time");
188
189         Module yangTypesModule_20100924 = context.findModuleByName(
190                 "ietf-yang-types", rev20100924);
191         Module yangTypesModule_20130516 = context.findModuleByName(
192                 "ietf-yang-types", rev20130516);
193         Module yangTypesModule_20130715 = context.findModuleByName(
194                 "ietf-yang-types", rev20130715);
195
196         assertNotNull(yangTypesModule_20100924);
197         assertNotNull(yangTypesModule_20130516);
198         assertNotNull(yangTypesModule_20130715);
199
200         assertTrue(findTypeDef(yangTypesModule_20100924,
201                 dateTimeTypeDef_20100924));
202         assertTrue(findTypeDef(yangTypesModule_20130516,
203                 dateTimeTypeDef_20130516));
204         assertTrue(findTypeDef(yangTypesModule_20130715,
205                 dateTimeTypeDef_20130715));
206
207         checkNetconfMonitoringModuleFullTest(context, rev20130715,
208                 dateTimeTypeDef_20130715);
209
210         checkInterfacesModuleFullTest(context, rev20100924, dateTimeTypeDef_20100924);
211
212     }
213
214     private static void checkInterfacesModuleFullTest(final SchemaContext context, final Date rev20100924,
215             final QName dateTimeTypeDef_20100924) throws URISyntaxException,
216             ParseException {
217         Date rev20121115 = SimpleDateFormatUtil.getRevisionFormat().parse(
218                 "2012-11-15");
219
220         Module interfacesModule_20121115 = context.findModuleByName(
221                 "ietf-interfaces", rev20121115);
222         assertNotNull(interfacesModule_20121115);
223
224         Set<ModuleImport> imports = interfacesModule_20121115.getImports();
225         assertEquals(1, imports.size());
226         ModuleImport interfacesImport = imports.iterator().next();
227         assertEquals("ietf-yang-types", interfacesImport.getModuleName());
228         assertEquals(rev20100924, interfacesImport.getRevision());
229     }
230
231     private static void checkNetconfMonitoringModuleFullTest(final SchemaContext context,
232             final Date rev20130715, final QName dateTimeTypeDef_20130715)
233             throws ParseException, URISyntaxException {
234         Date rev20101004 = SimpleDateFormatUtil.getRevisionFormat().parse(
235                 "2010-10-04");
236
237         Module monitoringModule_20101004 = context.findModuleByName(
238                 "ietf-netconf-monitoring", rev20101004);
239         assertNotNull(monitoringModule_20101004);
240
241         Set<ModuleImport> imports = monitoringModule_20101004.getImports();
242         assertEquals(2, imports.size());
243         for (ModuleImport monitoringImport : imports) {
244             if (monitoringImport.getModuleName().equals("ietf-yang-types")) {
245                 assertEquals(rev20130715, monitoringImport.getRevision());
246             }
247         }
248     }
249
250     @Test
251     public void multipleRevisionsSimpleTest() throws SourceException,
252             ReactorException, FileNotFoundException, URISyntaxException,
253             ParseException {
254         for (int i = 0; i < 1000; i++) {
255             SchemaContext context = StmtTestUtils
256                     .parseYangSources("/semantic-statement-parser/multiple-revisions/simple");
257             assertNotNull(context);
258             assertEquals(5, context.getModules().size());
259             checkContentSimpleTest(context);
260         }
261     }
262
263     private static void checkContentSimpleTest(final SchemaContext context)
264             throws ParseException, URISyntaxException {
265
266         String yangTypesNSStr = "urn:ietf:params:xml:ns:yang:ietf-yang-types";
267         URI yangTypesNS = new URI(yangTypesNSStr);
268
269         Date rev20100924 = SimpleDateFormatUtil.getRevisionFormat().parse(
270                 "2010-09-24");
271         Date rev20130516 = SimpleDateFormatUtil.getRevisionFormat().parse(
272                 "2013-05-16");
273         Date rev20130715 = SimpleDateFormatUtil.getRevisionFormat().parse(
274                 "2013-07-15");
275
276         final QNameModule yangTypes_20100924 = QNameModule.create(yangTypesNS,
277                 rev20100924);
278         final QNameModule yangTypes_20130516 = QNameModule.create(yangTypesNS,
279                 rev20130516);
280         final QNameModule yangTypes_20130715 = QNameModule.create(yangTypesNS,
281                 rev20130715);
282
283         final QName dateTimeTypeDef_20100924 = QName.create(yangTypes_20100924,
284                 "date-and-time");
285         final QName dateTimeTypeDef_20130516 = QName.create(yangTypes_20130516,
286                 "date-and-time");
287         final QName dateTimeTypeDef_20130715 = QName.create(yangTypes_20130715,
288                 "date-and-time");
289
290         Module yangTypesModule_20100924 = context.findModuleByName(
291                 "ietf-yang-types", rev20100924);
292         Module yangTypesModule_20130516 = context.findModuleByName(
293                 "ietf-yang-types", rev20130516);
294         Module yangTypesModule_20130715 = context.findModuleByName(
295                 "ietf-yang-types", rev20130715);
296
297         assertNotNull(yangTypesModule_20100924);
298         assertNotNull(yangTypesModule_20130516);
299         assertNotNull(yangTypesModule_20130715);
300
301         assertTrue(findTypeDef(yangTypesModule_20100924,
302                 dateTimeTypeDef_20100924));
303         assertTrue(findTypeDef(yangTypesModule_20130516,
304                 dateTimeTypeDef_20130516));
305         assertTrue(findTypeDef(yangTypesModule_20130715,
306                 dateTimeTypeDef_20130715));
307
308         checkNetconfMonitoringModuleSimpleTest(context, rev20130715,
309                 dateTimeTypeDef_20130715);
310
311         checkInterfacesModuleSimpleTest(context, rev20100924,
312                 dateTimeTypeDef_20100924);
313
314     }
315
316     private static void checkInterfacesModuleSimpleTest(final SchemaContext context,
317             final Date rev20100924, final QName dateTimeTypeDef_20100924)
318             throws URISyntaxException, ParseException {
319         String interfacesNSStr = "urn:ietf:params:xml:ns:yang:ietf-interfaces";
320         URI interfacesNS = new URI(interfacesNSStr);
321         Date rev20121115 = SimpleDateFormatUtil.getRevisionFormat().parse(
322                 "2012-11-15");
323         final QNameModule interfacesNS_20121115 = QNameModule.create(
324                 interfacesNS, rev20121115);
325         QName lastChange = QName.create(interfacesNS_20121115, "last-change");
326
327         Module interfacesModule_20121115 = context.findModuleByName(
328                 "ietf-interfaces", rev20121115);
329         assertNotNull(interfacesModule_20121115);
330
331         DataSchemaNode leafLastChange = interfacesModule_20121115
332                 .getDataChildByName(lastChange);
333         assertNotNull(leafLastChange);
334
335         assertTrue(leafLastChange instanceof LeafSchemaNode);
336         QName lastChangeTypeQName = ((LeafSchemaNode) leafLastChange).getType()
337                 .getQName();
338         assertEquals(dateTimeTypeDef_20100924, lastChangeTypeQName);
339
340         Set<ModuleImport> imports = interfacesModule_20121115.getImports();
341         assertEquals(1, imports.size());
342         ModuleImport interfacesImport = imports.iterator().next();
343         assertEquals("ietf-yang-types", interfacesImport.getModuleName());
344         assertEquals(rev20100924, interfacesImport.getRevision());
345     }
346
347     private static void checkNetconfMonitoringModuleSimpleTest(final SchemaContext context,
348             final Date rev20130715, final QName dateTimeTypeDef_20130715)
349             throws ParseException, URISyntaxException {
350         String monitoringNSStr = "urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring";
351         URI monitoringNS = new URI(monitoringNSStr);
352
353         Date rev19700101 = SimpleDateFormatUtil.getRevisionFormat().parse(
354                 "1970-01-01");
355         final QNameModule monitoring_19700101 = QNameModule.create(
356                 monitoringNS, rev19700101);
357         QName lockedTime = QName.create(monitoring_19700101, "locked-time");
358
359         Module monitoringModule_19700101 = context.findModuleByName(
360                 "ietf-netconf-monitoring", rev19700101);
361         assertNotNull(monitoringModule_19700101);
362
363         DataSchemaNode leafLockedTime = monitoringModule_19700101
364                 .getDataChildByName(lockedTime);
365         assertNotNull(leafLockedTime);
366
367         assertTrue(leafLockedTime instanceof LeafSchemaNode);
368         QName lockedTimeTypeQName = ((LeafSchemaNode) leafLockedTime).getType()
369                 .getQName();
370         assertEquals(dateTimeTypeDef_20130715, lockedTimeTypeQName);
371
372         Set<ModuleImport> imports = monitoringModule_19700101.getImports();
373         assertEquals(1, imports.size());
374         ModuleImport monitoringImport = imports.iterator().next();
375         assertEquals("ietf-yang-types", monitoringImport.getModuleName());
376         assertEquals(rev20130715, monitoringImport.getRevision());
377     }
378
379     private static boolean findTypeDef(final Module module, final QName typedef) {
380         Set<TypeDefinition<?>> typeDefinitions = module.getTypeDefinitions();
381         for (TypeDefinition<?> typeDefinition : typeDefinitions) {
382             if (typeDefinition.getQName().equals(typedef)) {
383                 return true;
384             }
385         }
386         return false;
387     }
388
389     @Test
390     public void nodeTest() throws Exception {
391         SchemaContext context = StmtTestUtils.parseYangSources("/semantic-statement-parser/multiple-revisions/node-test");
392         assertNotNull(context);
393
394         QName root = QName.create("foo", "2016-04-06", "foo-root");
395         QName container20160404 = QName.create("foo", "2016-04-06", "con20160404");
396         SchemaNode findDataSchemaNode = SchemaContextUtil.findDataSchemaNode(context, SchemaPath.create(true, root, container20160404));
397         assertTrue(findDataSchemaNode instanceof ContainerSchemaNode);
398
399         QName container20160405 = QName.create("foo", "2016-04-06", "con20160405");
400         findDataSchemaNode = SchemaContextUtil.findDataSchemaNode(context, SchemaPath.create(true, root, container20160405));
401         assertTrue(findDataSchemaNode instanceof ContainerSchemaNode);
402
403         QName container20160406 = QName.create("foo", "2016-04-06", "con20160406");
404         findDataSchemaNode = SchemaContextUtil.findDataSchemaNode(context, SchemaPath.create(true, root, container20160406));
405         assertNull(findDataSchemaNode);
406     }
407 }