2 * Copyright (c) 2013 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
8 package org.opendaylight.yangtools.yang.stmt.retest;
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;
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;
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;
53 public class YangParserWithContextTest {
54 private final DateFormat simpleDateFormat = new SimpleDateFormat(
56 private final YangParserImpl parser = new YangParserImpl();
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);
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)
78 public void testTypeFromContext() throws Exception {
80 CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
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);
88 StmtTestUtils.addSources(reactor, IETF);
89 StmtTestUtils.addSources(reactor, types, test1);
91 SchemaContext context = reactor.buildEffective();
93 Module module = context.findModuleByName("test1",
94 SimpleDateFormatUtil.getRevisionFormat().parse("2013-06-18"));
95 assertNotNull(module);
97 // String resource = "/ietf/ietf-inet-types@2010-09-24.yang";
98 // InputStream stream = new FileInputStream(new File(getClass()
99 // .getResource(resource).toURI()));
100 // SchemaContext context = parser.resolveSchemaContext(TestUtils
101 // .loadModules(Lists.newArrayList(stream)));
104 // resource = "/context-test/test1.yang";
105 // InputStream stream2 = new FileInputStream(new File(getClass()
106 // .getResource(resource).toURI()));
107 // Module module = TestUtils.loadModuleWithContext("test1", stream2,
110 // assertNotNull(module);
112 LeafSchemaNode leaf = (LeafSchemaNode) module.getDataChildByName("id");
114 assertTrue(leaf.getType() instanceof UnsignedIntegerTypeDefinition);
115 UnsignedIntegerTypeDefinition leafType = (UnsignedIntegerTypeDefinition) leaf.getType();
116 QName qname = leafType.getQName();
117 assertEquals(URI.create("urn:simple.demo.test1"), qname.getNamespace());
118 assertEquals(simpleDateFormat.parse("2013-06-18"), qname.getRevision());
119 assertEquals("port-number", qname.getLocalName());
121 UnsignedIntegerTypeDefinition leafBaseType = leafType.getBaseType();
122 qname = leafBaseType.getQName();
123 assertEquals(URI.create("urn:ietf:params:xml:ns:yang:ietf-inet-types"),
124 qname.getNamespace());
125 assertEquals(simpleDateFormat.parse("2010-09-24"), qname.getRevision());
126 assertEquals("port-number", qname.getLocalName());
128 UnsignedIntegerTypeDefinition dscpExt = (UnsignedIntegerTypeDefinition) TestUtils.findTypedef(
129 module.getTypeDefinitions(), "dscp-ext");
130 List<RangeConstraint> ranges = dscpExt.getRangeConstraints();
131 assertEquals(1, ranges.size());
132 RangeConstraint range = ranges.get(0);
133 assertEquals(0, range.getMin().intValue());
134 assertEquals(63, range.getMax().intValue());
138 public void testUsesFromContext() throws Exception {
139 CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
142 YangStatementSourceImpl test2 = new YangStatementSourceImpl(
143 "/context-test/test2.yang", false);
144 StmtTestUtils.addSources(reactor, BAZ, FOO, BAR, SUBFOO, test2);
145 SchemaContext context = reactor.buildEffective();
147 // Module testModule;
148 // try (InputStream stream = new FileInputStream(new
149 // File(getClass().getResource("/context-test/test2.yang")
151 // testModule = TestUtils.loadModuleWithContext("test2", stream,
154 // assertNotNull(testModule);
156 // suffix _u = added by uses
157 // suffix _g = defined in grouping from context
160 Module testModule = context.findModuleByName("test2",
161 SimpleDateFormatUtil.getRevisionFormat().parse("2013-06-18"));
162 assertNotNull(testModule);
164 Module contextModule = context
165 .findModuleByNamespace(URI.create("urn:opendaylight.baz"))
167 assertNotNull(contextModule);
168 Set<GroupingDefinition> groupings = contextModule.getGroupings();
169 assertEquals(1, groupings.size());
170 GroupingDefinition grouping = groupings.iterator().next();
172 // get node containing uses
173 ContainerSchemaNode peer = (ContainerSchemaNode) testModule
174 .getDataChildByName("peer");
175 ContainerSchemaNode destination = (ContainerSchemaNode) peer
176 .getDataChildByName("destination");
179 Set<UsesNode> uses = destination.getUses();
180 assertEquals(1, uses.size());
182 // check uses process
183 AnyXmlSchemaNode data_u = (AnyXmlSchemaNode) destination
184 .getDataChildByName("data");
185 assertNotNull(data_u);
186 assertTrue(data_u.isAddedByUses());
188 AnyXmlSchemaNode data_g = (AnyXmlSchemaNode) grouping
189 .getDataChildByName("data");
190 assertNotNull(data_g);
191 assertFalse(data_g.isAddedByUses());
192 assertFalse(data_u.equals(data_g));
194 ChoiceSchemaNode how_u = (ChoiceSchemaNode) destination
195 .getDataChildByName("how");
196 assertNotNull(how_u);
197 assertTrue(how_u.isAddedByUses());
199 ChoiceSchemaNode how_g = (ChoiceSchemaNode) grouping
200 .getDataChildByName("how");
201 assertNotNull(how_g);
202 assertFalse(how_g.isAddedByUses());
203 assertFalse(how_u.equals(how_g));
205 LeafSchemaNode address_u = (LeafSchemaNode) destination
206 .getDataChildByName("address");
207 assertNotNull(address_u);
208 assertTrue(address_u.isAddedByUses());
210 LeafSchemaNode address_g = (LeafSchemaNode) grouping
211 .getDataChildByName("address");
212 assertNotNull(address_g);
213 assertFalse(address_g.isAddedByUses());
214 assertFalse(address_u.equals(address_g));
216 ContainerSchemaNode port_u = (ContainerSchemaNode) destination
217 .getDataChildByName("port");
218 assertNotNull(port_u);
219 assertTrue(port_u.isAddedByUses());
221 ContainerSchemaNode port_g = (ContainerSchemaNode) grouping
222 .getDataChildByName("port");
223 assertNotNull(port_g);
224 assertFalse(port_g.isAddedByUses());
225 assertFalse(port_u.equals(port_g));
227 ListSchemaNode addresses_u = (ListSchemaNode) destination
228 .getDataChildByName("addresses");
229 assertNotNull(addresses_u);
230 assertTrue(addresses_u.isAddedByUses());
232 ListSchemaNode addresses_g = (ListSchemaNode) grouping
233 .getDataChildByName("addresses");
234 assertNotNull(addresses_g);
235 assertFalse(addresses_g.isAddedByUses());
236 assertFalse(addresses_u.equals(addresses_g));
238 // grouping defined by 'uses'
239 Set<GroupingDefinition> groupings_u = destination.getGroupings();
240 assertEquals(1, groupings_u.size());
241 GroupingDefinition grouping_u = groupings_u.iterator().next();
242 assertTrue(grouping_u.isAddedByUses());
244 // grouping defined in 'grouping' node
245 Set<GroupingDefinition> groupings_g = grouping.getGroupings();
246 assertEquals(1, groupings_g.size());
247 GroupingDefinition grouping_g = groupings_g.iterator().next();
248 assertFalse(grouping_g.isAddedByUses());
249 assertFalse(grouping_u.equals(grouping_g));
251 List<UnknownSchemaNode> nodes_u = destination.getUnknownSchemaNodes();
252 assertEquals(1, nodes_u.size());
253 UnknownSchemaNode node_u = nodes_u.get(0);
254 assertTrue(node_u.isAddedByUses());
256 List<UnknownSchemaNode> nodes_g = grouping.getUnknownSchemaNodes();
257 assertEquals(1, nodes_g.size());
258 UnknownSchemaNode node_g = nodes_g.get(0);
259 assertFalse(node_g.isAddedByUses());
260 assertFalse(node_u.equals(node_g));
264 public void testUsesRefineFromContext() throws Exception {
266 CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
269 YangStatementSourceImpl test2 = new YangStatementSourceImpl(
270 "/context-test/test2.yang", false);
271 StmtTestUtils.addSources(reactor, BAZ, FOO, BAR, SUBFOO, test2);
272 SchemaContext context = reactor.buildEffective();
275 // try (InputStream stream = new FileInputStream(new
276 // File(getClass().getResource("/context-test/test2.yang")
278 // module = TestUtils.loadModuleWithContext("test2", stream, context);
280 // assertNotNull(module);
282 Module module = context.findModuleByName("test2",
283 SimpleDateFormatUtil.getRevisionFormat().parse("2013-06-18"));
284 assertNotNull(module);
285 ContainerSchemaNode peer = (ContainerSchemaNode) module
286 .getDataChildByName("peer");
287 ContainerSchemaNode destination = (ContainerSchemaNode) peer
288 .getDataChildByName("destination");
289 Set<UsesNode> usesNodes = destination.getUses();
290 assertEquals(1, usesNodes.size());
291 UsesNode usesNode = usesNodes.iterator().next();
293 // test grouping path
294 List<QName> path = new ArrayList<>();
295 QName qname = QName.create(URI.create("urn:opendaylight.baz"),
296 simpleDateFormat.parse("2013-02-27"), "target");
298 SchemaPath expectedPath = SchemaPath.create(path, true);
299 assertEquals(expectedPath, usesNode.getGroupingPath());
302 Map<SchemaPath, SchemaNode> refines = usesNode.getRefines();
303 assertEquals(3, refines.size());
305 LeafSchemaNode refineLeaf = null;
306 ContainerSchemaNode refineContainer = null;
307 ListSchemaNode refineList = null;
308 for (Map.Entry<SchemaPath, SchemaNode> entry : refines.entrySet()) {
309 SchemaNode value = entry.getValue();
310 if (value instanceof LeafSchemaNode) {
311 refineLeaf = (LeafSchemaNode) value;
312 } else if (value instanceof ContainerSchemaNode) {
313 refineContainer = (ContainerSchemaNode) value;
314 } else if (value instanceof ListSchemaNode) {
315 refineList = (ListSchemaNode) value;
320 assertNotNull(refineLeaf);
321 assertEquals("address", refineLeaf.getQName().getLocalName());
322 assertEquals("description of address defined by refine",
323 refineLeaf.getDescription());
324 assertEquals("address reference added by refine",
325 refineLeaf.getReference());
326 assertFalse(refineLeaf.isConfiguration());
327 assertTrue(refineLeaf.getConstraints().isMandatory());
328 Set<MustDefinition> leafMustConstraints = refineLeaf.getConstraints()
329 .getMustConstraints();
330 assertEquals(1, leafMustConstraints.size());
331 MustDefinition leafMust = leafMustConstraints.iterator().next();
333 "ifType != 'ethernet' or (ifType = 'ethernet' and ifMTU = 1500)",
334 leafMust.toString());
337 assertNotNull(refineContainer);
338 Set<MustDefinition> mustConstraints = refineContainer.getConstraints()
339 .getMustConstraints();
340 assertTrue(mustConstraints.isEmpty());
341 assertEquals("description of port defined by refine",
342 refineContainer.getDescription());
343 assertEquals("port reference added by refine",
344 refineContainer.getReference());
345 assertFalse(refineContainer.isConfiguration());
346 assertTrue(refineContainer.isPresenceContainer());
349 assertNotNull(refineList);
350 assertEquals("description of addresses defined by refine",
351 refineList.getDescription());
352 assertEquals("addresses reference added by refine",
353 refineList.getReference());
354 assertFalse(refineList.isConfiguration());
355 assertEquals(2, (int) refineList.getConstraints().getMinElements());
356 assertEquals(12, (int) refineList.getConstraints().getMaxElements());
360 public void testIdentity() throws Exception {
362 CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
365 YangStatementSourceImpl types = new YangStatementSourceImpl(
366 "/types/custom-types-test@2012-4-4.yang", false);
367 YangStatementSourceImpl test3 = new YangStatementSourceImpl(
368 "/context-test/test3.yang", false);
370 StmtTestUtils.addSources(reactor, IETF);
371 StmtTestUtils.addSources(reactor, types, test3);
372 SchemaContext context = reactor.buildEffective();
374 Module module = context.findModuleByName("test3",
375 SimpleDateFormatUtil.getRevisionFormat().parse("2013-06-18"));
376 assertNotNull(module);
378 // SchemaContext context;
379 // File yangFile = new File(getClass().getResource(
380 // "/types/custom-types-test@2012-4-4.yang").toURI());
381 // File dependenciesDir = new File(getClass().getResource("/ietf").toURI());
382 // YangContextParser parser = new YangParserImpl();
383 // context = parser.parseFile(yangFile, dependenciesDir);
386 // try (InputStream stream = new FileInputStream(new File(getClass()
387 // .getResource("/context-test/test3.yang").toURI()))) {
388 // module = TestUtils.loadModuleWithContext("test3", stream, context);
390 // assertNotNull(module);
392 Set<IdentitySchemaNode> identities = module.getIdentities();
393 assertEquals(1, identities.size());
395 IdentitySchemaNode identity = identities.iterator().next();
396 QName idQName = identity.getQName();
397 assertEquals(URI.create("urn:simple.demo.test3"),
398 idQName.getNamespace());
399 assertEquals(simpleDateFormat.parse("2013-06-18"),
400 idQName.getRevision());
401 assertEquals("pt", idQName.getLocalName());
403 IdentitySchemaNode baseIdentity = identity.getBaseIdentity();
404 QName idBaseQName = baseIdentity.getQName();
405 assertEquals(URI.create("urn:custom.types.demo"),
406 idBaseQName.getNamespace());
407 assertEquals(simpleDateFormat.parse("2012-04-16"),
408 idBaseQName.getRevision());
409 assertEquals("service-type", idBaseQName.getLocalName());
413 public void testUnknownNodes() throws Exception {
415 CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
418 YangStatementSourceImpl types = new YangStatementSourceImpl(
419 "/types/custom-types-test@2012-4-4.yang", false);
420 YangStatementSourceImpl test3 = new YangStatementSourceImpl(
421 "/context-test/test3.yang", false);
423 StmtTestUtils.addSources(reactor, IETF);
424 StmtTestUtils.addSources(reactor, types, test3);
426 SchemaContext context = reactor.buildEffective();
428 Module module = context.findModuleByName("test3",
429 SimpleDateFormatUtil.getRevisionFormat().parse("2013-06-18"));
430 assertNotNull(module);
432 // SchemaContext context;
433 // File yangFile = new File(getClass().getResource(
434 // "/types/custom-types-test@2012-4-4.yang").toURI());
435 // File dependenciesDir = new File(getClass().getResource("/ietf").toURI());
436 // YangContextParser parser = new YangParserImpl();
437 // context = parser.parseFile(yangFile, dependenciesDir);
440 // try (InputStream stream = new FileInputStream(new File(getClass()
441 // .getResource("/context-test/test3.yang").toURI()))) {
442 // module = TestUtils.loadModuleWithContext("test3", stream, context);
445 ContainerSchemaNode network = (ContainerSchemaNode) module
446 .getDataChildByName("network");
447 List<UnknownSchemaNode> unknownNodes = network.getUnknownSchemaNodes();
448 assertEquals(1, unknownNodes.size());
450 UnknownSchemaNode un = unknownNodes.get(0);
451 QName unType = un.getNodeType();
452 assertEquals(URI.create("urn:custom.types.demo"), unType.getNamespace());
453 assertEquals(simpleDateFormat.parse("2012-04-16"), unType.getRevision());
454 assertEquals("mountpoint", unType.getLocalName());
455 assertEquals("point", un.getNodeParameter());
456 assertNotNull(un.getExtensionDefinition());
460 public void testAugment() throws Exception {
462 String resource = "/context-augment-test/test4.yang";
463 SchemaContext context = parser.parseFiles(Collections
464 .singleton(new File(getClass().getResource(resource).toURI())));
466 // load another modules and parse them against already existing context
467 File test1 = new File(getClass().getResource(
468 "/context-augment-test/test1.yang").toURI());
469 File test2 = new File(getClass().getResource(
470 "/context-augment-test/test2.yang").toURI());
471 File test3 = new File(getClass().getResource(
472 "/context-augment-test/test3.yang").toURI());
473 Set<Module> modules = parser.parseFiles(
474 Arrays.asList(test1, test2, test3), context).getModules();
475 assertNotNull(modules);
477 Module t4 = TestUtils.findModule(modules, "test4");
478 ContainerSchemaNode interfaces = (ContainerSchemaNode) t4
479 .getDataChildByName("interfaces");
480 ListSchemaNode ifEntry = (ListSchemaNode) interfaces
481 .getDataChildByName("ifEntry");
483 // test augmentation process
484 ContainerSchemaNode augmentHolder = (ContainerSchemaNode) ifEntry
485 .getDataChildByName("augment-holder");
486 assertNotNull(augmentHolder);
487 DataSchemaNode ds0 = augmentHolder
488 .getDataChildByName("ds0ChannelNumber");
490 DataSchemaNode interfaceId = augmentHolder
491 .getDataChildByName("interface-id");
492 assertNotNull(interfaceId);
493 DataSchemaNode higherLayerIf = augmentHolder
494 .getDataChildByName("higher-layer-if");
495 assertNotNull(higherLayerIf);
496 ContainerSchemaNode schemas = (ContainerSchemaNode) augmentHolder
497 .getDataChildByName("schemas");
498 assertNotNull(schemas);
499 assertNotNull(schemas.getDataChildByName("id"));
501 // test augment target after augmentation: check if it is same instance
502 ListSchemaNode ifEntryAfterAugment = (ListSchemaNode) interfaces
503 .getDataChildByName("ifEntry");
504 assertTrue(ifEntry == ifEntryAfterAugment);
508 public void testDeviation() throws Exception {
510 CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
513 YangStatementSourceImpl bar = new YangStatementSourceImpl(
514 "/model/bar.yang", false);
515 YangStatementSourceImpl deviationTest = new YangStatementSourceImpl(
516 "/context-test/deviation-test.yang", false);
518 StmtTestUtils.addSources(reactor, bar, deviationTest);
519 SchemaContext context = reactor.buildEffective();
521 Module testModule = context.findModuleByName("deviation-test",
522 SimpleDateFormatUtil.getRevisionFormat().parse("2013-02-27"));
523 assertNotNull(testModule);
526 // SchemaContext context;
527 // String resource = "/model/bar.yang";
529 // try (InputStream stream = new FileInputStream(new File(getClass()
530 // .getResource(resource).toURI()))) {
531 // context = parser.resolveSchemaContext(TestUtils.loadModules(Lists
532 // .newArrayList(stream)));
535 // load another modules and parse them against already existing context
536 // Set<Module> modules;
537 // try (InputStream stream = new FileInputStream(new File(getClass()
538 // .getResource("/context-test/deviation-test.yang").toURI()))) {
539 // List<InputStream> input = Lists.newArrayList(stream);
540 // modules = TestUtils.loadModulesWithContext(input, context);
542 // assertNotNull(modules);
545 //Module testModule = TestUtils.findModule(modules, "deviation-test");
548 Set<Deviation> deviations = testModule.getDeviations();
549 assertEquals(1, deviations.size());
550 Deviation dev = deviations.iterator().next();
552 assertEquals("system/user ref", dev.getReference());
554 URI expectedNS = URI.create("urn:opendaylight.bar");
555 DateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
556 Date expectedRev = simpleDateFormat.parse("2013-07-03");
557 List<QName> path = new ArrayList<>();
558 path.add(QName.create(expectedNS, expectedRev, "interfaces"));
559 path.add(QName.create(expectedNS, expectedRev, "ifEntry"));
560 SchemaPath expectedPath = SchemaPath.create(path, true);
562 assertEquals(expectedPath, dev.getTargetPath());
563 assertEquals(Deviate.ADD, dev.getDeviate());