2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.yangtools.yang.stmt.test;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertTrue;
14 import java.io.FileNotFoundException;
16 import java.net.URISyntaxException;
17 import java.text.ParseException;
18 import java.util.Date;
20 import org.junit.Test;
21 import org.opendaylight.yangtools.yang.common.QName;
22 import org.opendaylight.yangtools.yang.common.QNameModule;
23 import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil;
24 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
25 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
26 import org.opendaylight.yangtools.yang.model.api.Module;
27 import org.opendaylight.yangtools.yang.model.api.ModuleImport;
28 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
29 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
30 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
31 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
32 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor;
33 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangInferencePipeline;
34 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangStatementSourceImpl;
35 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.EffectiveSchemaContext;
37 public class MoreRevisionsTest {
39 private static final YangStatementSourceImpl REVFILE = new YangStatementSourceImpl(
40 "/semantic-statement-parser/revisions/more-revisions-test.yang",
43 private static final YangStatementSourceImpl TED_20130712 = new YangStatementSourceImpl(
44 "/semantic-statement-parser/two-revisions/ted@2013-07-12.yang",
47 private static final YangStatementSourceImpl TED_20131021 = new YangStatementSourceImpl(
48 "/semantic-statement-parser/two-revisions/ted@2013-10-21.yang",
51 private static final YangStatementSourceImpl IETF_TYPES = new YangStatementSourceImpl(
52 "/ietf/ietf-inet-types@2010-09-24.yang", false);
54 private static final YangStatementSourceImpl NETWORK_TOPOLOGY_20130712 = new YangStatementSourceImpl(
55 "/ietf/network-topology@2013-07-12.yang", false);
57 private static final YangStatementSourceImpl NETWORK_TOPOLOGY_20131021 = new YangStatementSourceImpl(
58 "/ietf/network-topology@2013-10-21.yang", false);
60 private static final YangStatementSourceImpl ISIS_20130712 = new YangStatementSourceImpl(
61 "/semantic-statement-parser/two-revisions/isis-topology@2013-07-12.yang",
64 private static final YangStatementSourceImpl ISIS_20131021 = new YangStatementSourceImpl(
65 "/semantic-statement-parser/two-revisions/isis-topology@2013-10-21.yang",
68 private static final YangStatementSourceImpl L3_20130712 = new YangStatementSourceImpl(
69 "/semantic-statement-parser/two-revisions/l3-unicast-igp-topology@2013-07-12.yang",
72 private static final YangStatementSourceImpl L3_20131021 = new YangStatementSourceImpl(
73 "/semantic-statement-parser/two-revisions/l3-unicast-igp-topology@2013-10-21.yang",
77 public void readAndParseYangFileTest() throws SourceException,
79 CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
81 reactor.addSource(REVFILE);
82 EffectiveSchemaContext result = reactor.buildEffective();
83 assertNotNull(result);
84 final Module moduleByName = result.getModules().iterator().next();
85 final QNameModule qNameModule = moduleByName.getQNameModule();
86 final String formattedRevision = qNameModule.getFormattedRevision();
87 assertEquals(formattedRevision, "2015-06-07");
91 public void twoRevisionsTest() throws SourceException, ReactorException {
92 CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
95 reactor.addSources(TED_20130712, TED_20131021, IETF_TYPES);
97 EffectiveSchemaContext result = reactor.buildEffective();
98 assertNotNull(result);
103 public void twoRevisionsTest2() throws SourceException, ReactorException {
104 CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
107 reactor.addSources(NETWORK_TOPOLOGY_20130712,
108 NETWORK_TOPOLOGY_20131021, IETF_TYPES);
110 EffectiveSchemaContext result = reactor.buildEffective();
111 assertNotNull(result);
112 Set<Module> modules = result.getModules();
114 assertEquals(3, modules.size());
115 assertEquals(2, StmtTestUtils.findModules(modules, "network-topology")
120 public void moreRevisionsListKeyTest() throws SourceException,
122 CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
125 reactor.addSources(TED_20130712, TED_20131021, ISIS_20130712,
126 ISIS_20131021, L3_20130712, L3_20131021, IETF_TYPES,
127 NETWORK_TOPOLOGY_20130712, NETWORK_TOPOLOGY_20131021);
129 EffectiveSchemaContext result = reactor.buildEffective();
130 assertNotNull(result);
134 public void multipleRevisionsTest() throws SourceException,
135 ReactorException, FileNotFoundException, URISyntaxException {
136 for (int i = 0; i < 100; i++) {
137 SchemaContext context = StmtTestUtils
138 .parseYangSources("/semantic-statement-parser/multiple-revisions");
139 assertNotNull(context);
144 public void multipleRevisionsFullTest() throws SourceException,
145 ReactorException, FileNotFoundException, URISyntaxException,
147 for (int i = 0; i < 100; i++) {
148 SchemaContext context = StmtTestUtils
149 .parseYangSources("/semantic-statement-parser/multiple-revisions/full");
150 assertNotNull(context);
151 assertEquals(6, context.getModules().size());
152 checkContentFullTest(context);
156 private static void checkContentFullTest(final SchemaContext context) throws ParseException,
159 String yangTypesNSStr = "urn:ietf:params:xml:ns:yang:ietf-yang-types";
160 URI yangTypesNS = new URI(yangTypesNSStr);
162 Date rev20100924 = SimpleDateFormatUtil.getRevisionFormat().parse(
164 Date rev20130516 = SimpleDateFormatUtil.getRevisionFormat().parse(
166 Date rev20130715 = SimpleDateFormatUtil.getRevisionFormat().parse(
169 final QNameModule yangTypes_20100924 = QNameModule.create(yangTypesNS,
171 final QNameModule yangTypes_20130516 = QNameModule.create(yangTypesNS,
173 final QNameModule yangTypes_20130715 = QNameModule.create(yangTypesNS,
176 final QName dateTimeTypeDef_20100924 = QName.create(yangTypes_20100924,
178 final QName dateTimeTypeDef_20130516 = QName.create(yangTypes_20130516,
180 final QName dateTimeTypeDef_20130715 = QName.create(yangTypes_20130715,
183 Module yangTypesModule_20100924 = context.findModuleByName(
184 "ietf-yang-types", rev20100924);
185 Module yangTypesModule_20130516 = context.findModuleByName(
186 "ietf-yang-types", rev20130516);
187 Module yangTypesModule_20130715 = context.findModuleByName(
188 "ietf-yang-types", rev20130715);
190 assertNotNull(yangTypesModule_20100924);
191 assertNotNull(yangTypesModule_20130516);
192 assertNotNull(yangTypesModule_20130715);
194 assertTrue(findTypeDef(yangTypesModule_20100924,
195 dateTimeTypeDef_20100924));
196 assertTrue(findTypeDef(yangTypesModule_20130516,
197 dateTimeTypeDef_20130516));
198 assertTrue(findTypeDef(yangTypesModule_20130715,
199 dateTimeTypeDef_20130715));
201 checkNetconfMonitoringModuleFullTest(context, rev20130715,
202 dateTimeTypeDef_20130715);
204 checkInterfacesModuleFullTest(context, rev20100924, dateTimeTypeDef_20100924);
208 private static void checkInterfacesModuleFullTest(final SchemaContext context, final Date rev20100924,
209 final QName dateTimeTypeDef_20100924) throws URISyntaxException,
211 Date rev20121115 = SimpleDateFormatUtil.getRevisionFormat().parse(
214 Module interfacesModule_20121115 = context.findModuleByName(
215 "ietf-interfaces", rev20121115);
216 assertNotNull(interfacesModule_20121115);
218 Set<ModuleImport> imports = interfacesModule_20121115.getImports();
219 assertEquals(1, imports.size());
220 ModuleImport interfacesImport = imports.iterator().next();
221 assertEquals("ietf-yang-types", interfacesImport.getModuleName());
222 assertEquals(rev20100924, interfacesImport.getRevision());
225 private static void checkNetconfMonitoringModuleFullTest(final SchemaContext context,
226 final Date rev20130715, final QName dateTimeTypeDef_20130715)
227 throws ParseException, URISyntaxException {
228 Date rev20101004 = SimpleDateFormatUtil.getRevisionFormat().parse(
231 Module monitoringModule_20101004 = context.findModuleByName(
232 "ietf-netconf-monitoring", rev20101004);
233 assertNotNull(monitoringModule_20101004);
235 Set<ModuleImport> imports = monitoringModule_20101004.getImports();
236 assertEquals(2, imports.size());
237 for (ModuleImport monitoringImport : imports) {
238 if (monitoringImport.getModuleName().equals("ietf-yang-types")) {
239 assertEquals(rev20130715, monitoringImport.getRevision());
245 public void multipleRevisionsSimpleTest() throws SourceException,
246 ReactorException, FileNotFoundException, URISyntaxException,
248 for (int i = 0; i < 1000; i++) {
249 SchemaContext context = StmtTestUtils
250 .parseYangSources("/semantic-statement-parser/multiple-revisions/simple");
251 assertNotNull(context);
252 assertEquals(5, context.getModules().size());
253 checkContentSimpleTest(context);
257 private static void checkContentSimpleTest(final SchemaContext context)
258 throws ParseException, URISyntaxException {
260 String yangTypesNSStr = "urn:ietf:params:xml:ns:yang:ietf-yang-types";
261 URI yangTypesNS = new URI(yangTypesNSStr);
263 Date rev20100924 = SimpleDateFormatUtil.getRevisionFormat().parse(
265 Date rev20130516 = SimpleDateFormatUtil.getRevisionFormat().parse(
267 Date rev20130715 = SimpleDateFormatUtil.getRevisionFormat().parse(
270 final QNameModule yangTypes_20100924 = QNameModule.create(yangTypesNS,
272 final QNameModule yangTypes_20130516 = QNameModule.create(yangTypesNS,
274 final QNameModule yangTypes_20130715 = QNameModule.create(yangTypesNS,
277 final QName dateTimeTypeDef_20100924 = QName.create(yangTypes_20100924,
279 final QName dateTimeTypeDef_20130516 = QName.create(yangTypes_20130516,
281 final QName dateTimeTypeDef_20130715 = QName.create(yangTypes_20130715,
284 Module yangTypesModule_20100924 = context.findModuleByName(
285 "ietf-yang-types", rev20100924);
286 Module yangTypesModule_20130516 = context.findModuleByName(
287 "ietf-yang-types", rev20130516);
288 Module yangTypesModule_20130715 = context.findModuleByName(
289 "ietf-yang-types", rev20130715);
291 assertNotNull(yangTypesModule_20100924);
292 assertNotNull(yangTypesModule_20130516);
293 assertNotNull(yangTypesModule_20130715);
295 assertTrue(findTypeDef(yangTypesModule_20100924,
296 dateTimeTypeDef_20100924));
297 assertTrue(findTypeDef(yangTypesModule_20130516,
298 dateTimeTypeDef_20130516));
299 assertTrue(findTypeDef(yangTypesModule_20130715,
300 dateTimeTypeDef_20130715));
302 checkNetconfMonitoringModuleSimpleTest(context, rev20130715,
303 dateTimeTypeDef_20130715);
305 checkInterfacesModuleSimpleTest(context, rev20100924,
306 dateTimeTypeDef_20100924);
310 private static void checkInterfacesModuleSimpleTest(final SchemaContext context,
311 final Date rev20100924, final QName dateTimeTypeDef_20100924)
312 throws URISyntaxException, ParseException {
313 String interfacesNSStr = "urn:ietf:params:xml:ns:yang:ietf-interfaces";
314 URI interfacesNS = new URI(interfacesNSStr);
315 Date rev20121115 = SimpleDateFormatUtil.getRevisionFormat().parse(
317 final QNameModule interfacesNS_20121115 = QNameModule.create(
318 interfacesNS, rev20121115);
319 QName lastChange = QName.create(interfacesNS_20121115, "last-change");
321 Module interfacesModule_20121115 = context.findModuleByName(
322 "ietf-interfaces", rev20121115);
323 assertNotNull(interfacesModule_20121115);
325 DataSchemaNode leafLastChange = interfacesModule_20121115
326 .getDataChildByName(lastChange);
327 assertNotNull(leafLastChange);
329 assertTrue(leafLastChange instanceof LeafSchemaNode);
330 QName lastChangeTypeQName = ((LeafSchemaNode) leafLastChange).getType()
332 assertEquals(dateTimeTypeDef_20100924, lastChangeTypeQName);
334 Set<ModuleImport> imports = interfacesModule_20121115.getImports();
335 assertEquals(1, imports.size());
336 ModuleImport interfacesImport = imports.iterator().next();
337 assertEquals("ietf-yang-types", interfacesImport.getModuleName());
338 assertEquals(rev20100924, interfacesImport.getRevision());
341 private static void checkNetconfMonitoringModuleSimpleTest(final SchemaContext context,
342 final Date rev20130715, final QName dateTimeTypeDef_20130715)
343 throws ParseException, URISyntaxException {
344 String monitoringNSStr = "urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring";
345 URI monitoringNS = new URI(monitoringNSStr);
347 Date rev19700101 = SimpleDateFormatUtil.getRevisionFormat().parse(
349 final QNameModule monitoring_19700101 = QNameModule.create(
350 monitoringNS, rev19700101);
351 QName lockedTime = QName.create(monitoring_19700101, "locked-time");
353 Module monitoringModule_19700101 = context.findModuleByName(
354 "ietf-netconf-monitoring", rev19700101);
355 assertNotNull(monitoringModule_19700101);
357 DataSchemaNode leafLockedTime = monitoringModule_19700101
358 .getDataChildByName(lockedTime);
359 assertNotNull(leafLockedTime);
361 assertTrue(leafLockedTime instanceof LeafSchemaNode);
362 QName lockedTimeTypeQName = ((LeafSchemaNode) leafLockedTime).getType()
364 assertEquals(dateTimeTypeDef_20130715, lockedTimeTypeQName);
366 Set<ModuleImport> imports = monitoringModule_19700101.getImports();
367 assertEquals(1, imports.size());
368 ModuleImport monitoringImport = imports.iterator().next();
369 assertEquals("ietf-yang-types", monitoringImport.getModuleName());
370 assertEquals(rev20130715, monitoringImport.getRevision());
373 private static boolean findTypeDef(final Module module, final QName typedef) {
374 Set<TypeDefinition<?>> typeDefinitions = module.getTypeDefinitions();
375 for (TypeDefinition<?> typeDefinition : typeDefinitions) {
376 if (typeDefinition.getQName().equals(typedef)) {