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.parser.impl;
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;
15 import com.google.common.collect.Lists;
17 import java.io.FileInputStream;
18 import java.io.InputStream;
19 import java.math.BigInteger;
21 import java.text.DateFormat;
22 import java.text.SimpleDateFormat;
23 import java.util.ArrayList;
24 import java.util.Date;
25 import java.util.List;
28 import org.junit.Ignore;
29 import org.junit.Test;
30 import org.opendaylight.yangtools.yang.common.QName;
31 import org.opendaylight.yangtools.yang.model.api.AnyXmlSchemaNode;
32 import org.opendaylight.yangtools.yang.model.api.ChoiceNode;
33 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
34 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
35 import org.opendaylight.yangtools.yang.model.api.Deviation;
36 import org.opendaylight.yangtools.yang.model.api.Deviation.Deviate;
37 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
38 import org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode;
39 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
40 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
41 import org.opendaylight.yangtools.yang.model.api.Module;
42 import org.opendaylight.yangtools.yang.model.api.MustDefinition;
43 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
44 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
45 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
46 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
47 import org.opendaylight.yangtools.yang.model.api.UsesNode;
48 import org.opendaylight.yangtools.yang.model.api.type.RangeConstraint;
49 import org.opendaylight.yangtools.yang.model.parser.api.YangContextParser;
50 import org.opendaylight.yangtools.yang.model.util.ExtendedType;
52 public class YangParserWithContextTest {
53 private final DateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
54 private final YangParserImpl parser = new YangParserImpl();
57 public void testTypeFromContext() throws Exception {
58 String resource = "/ietf/ietf-inet-types@2010-09-24.yang";
59 InputStream stream = new FileInputStream(new File(getClass().getResource(resource).toURI()));
60 SchemaContext context = parser.resolveSchemaContext(TestUtils.loadModules(Lists.newArrayList(stream)));
63 resource = "/context-test/test1.yang";
64 InputStream stream2 = new FileInputStream(new File(getClass().getResource(resource).toURI()));
65 Module module = TestUtils.loadModuleWithContext("test1", stream2, context);
67 assertNotNull(module);
69 LeafSchemaNode leaf = (LeafSchemaNode) module.getDataChildByName("id");
71 ExtendedType leafType = (ExtendedType) leaf.getType();
72 QName qname = leafType.getQName();
73 assertEquals(URI.create("urn:simple.demo.test1"), qname.getNamespace());
74 assertEquals(simpleDateFormat.parse("2013-06-18"), qname.getRevision());
75 assertEquals("t1", qname.getPrefix());
76 assertEquals("port-number", qname.getLocalName());
78 ExtendedType leafBaseType = (ExtendedType) leafType.getBaseType();
79 qname = leafBaseType.getQName();
80 assertEquals(URI.create("urn:ietf:params:xml:ns:yang:ietf-inet-types"), qname.getNamespace());
81 assertEquals(simpleDateFormat.parse("2010-09-24"), qname.getRevision());
82 assertEquals("inet", qname.getPrefix());
83 assertEquals("port-number", qname.getLocalName());
85 ExtendedType dscpExt = (ExtendedType) TestUtils.findTypedef(module.getTypeDefinitions(), "dscp-ext");
86 List<RangeConstraint> ranges = dscpExt.getRangeConstraints();
87 assertEquals(1, ranges.size());
88 RangeConstraint range = ranges.get(0);
89 assertEquals(BigInteger.ZERO, range.getMin());
90 assertEquals(BigInteger.valueOf(63), range.getMax());
94 public void testUsesFromContext() throws Exception {
95 SchemaContext context;
96 try (InputStream stream1 = new FileInputStream(new File(getClass().getResource("/model/baz.yang").toURI()));
97 InputStream stream2 = new FileInputStream(new File(getClass().getResource("/model/bar.yang").toURI()));
98 InputStream stream3 = new FileInputStream(new File(getClass().getResource("/model/foo.yang").toURI()))) {
99 context = parser.resolveSchemaContext(TestUtils.loadModules(Lists.newArrayList(stream1, stream2, stream3)));
102 try (InputStream stream = new FileInputStream(new File(getClass().getResource("/context-test/test2.yang")
104 testModule = TestUtils.loadModuleWithContext("test2", stream, context);
106 assertNotNull(testModule);
108 // suffix _u = added by uses
109 // suffix _g = defined in grouping from context
112 Module contextModule = context.findModuleByNamespace(URI.create("urn:opendaylight.baz")).iterator().next();
113 assertNotNull(contextModule);
114 Set<GroupingDefinition> groupings = contextModule.getGroupings();
115 assertEquals(1, groupings.size());
116 GroupingDefinition grouping = groupings.iterator().next();
118 // get node containing uses
119 ContainerSchemaNode peer = (ContainerSchemaNode) testModule.getDataChildByName("peer");
120 ContainerSchemaNode destination = (ContainerSchemaNode) peer.getDataChildByName("destination");
123 Set<UsesNode> uses = destination.getUses();
124 assertEquals(1, uses.size());
126 // check uses process
127 AnyXmlSchemaNode data_u = (AnyXmlSchemaNode) destination.getDataChildByName("data");
128 assertNotNull(data_u);
129 assertTrue(data_u.isAddedByUses());
131 AnyXmlSchemaNode data_g = (AnyXmlSchemaNode) grouping.getDataChildByName("data");
132 assertNotNull(data_g);
133 assertFalse(data_g.isAddedByUses());
134 assertFalse(data_u.equals(data_g));
136 ChoiceNode how_u = (ChoiceNode) destination.getDataChildByName("how");
137 assertNotNull(how_u);
138 assertTrue(how_u.isAddedByUses());
140 ChoiceNode how_g = (ChoiceNode) grouping.getDataChildByName("how");
141 assertNotNull(how_g);
142 assertFalse(how_g.isAddedByUses());
143 assertFalse(how_u.equals(how_g));
145 LeafSchemaNode address_u = (LeafSchemaNode) destination.getDataChildByName("address");
146 assertNotNull(address_u);
147 assertTrue(address_u.isAddedByUses());
149 LeafSchemaNode address_g = (LeafSchemaNode) grouping.getDataChildByName("address");
150 assertNotNull(address_g);
151 assertFalse(address_g.isAddedByUses());
152 assertFalse(address_u.equals(address_g));
154 ContainerSchemaNode port_u = (ContainerSchemaNode) destination.getDataChildByName("port");
155 assertNotNull(port_u);
156 assertTrue(port_u.isAddedByUses());
158 ContainerSchemaNode port_g = (ContainerSchemaNode) grouping.getDataChildByName("port");
159 assertNotNull(port_g);
160 assertFalse(port_g.isAddedByUses());
161 assertFalse(port_u.equals(port_g));
163 ListSchemaNode addresses_u = (ListSchemaNode) destination.getDataChildByName("addresses");
164 assertNotNull(addresses_u);
165 assertTrue(addresses_u.isAddedByUses());
167 ListSchemaNode addresses_g = (ListSchemaNode) grouping.getDataChildByName("addresses");
168 assertNotNull(addresses_g);
169 assertFalse(addresses_g.isAddedByUses());
170 assertFalse(addresses_u.equals(addresses_g));
172 // grouping defined by 'uses'
173 Set<GroupingDefinition> groupings_u = destination.getGroupings();
174 assertEquals(1, groupings_u.size());
175 GroupingDefinition grouping_u = groupings_u.iterator().next();
176 assertTrue(grouping_u.isAddedByUses());
178 // grouping defined in 'grouping' node
179 Set<GroupingDefinition> groupings_g = grouping.getGroupings();
180 assertEquals(1, groupings_g.size());
181 GroupingDefinition grouping_g = groupings_g.iterator().next();
182 assertFalse(grouping_g.isAddedByUses());
183 assertFalse(grouping_u.equals(grouping_g));
185 List<UnknownSchemaNode> nodes_u = destination.getUnknownSchemaNodes();
186 assertEquals(1, nodes_u.size());
187 UnknownSchemaNode node_u = nodes_u.get(0);
188 assertTrue(node_u.isAddedByUses());
190 List<UnknownSchemaNode> nodes_g = grouping.getUnknownSchemaNodes();
191 assertEquals(1, nodes_g.size());
192 UnknownSchemaNode node_g = nodes_g.get(0);
193 assertFalse(node_g.isAddedByUses());
194 assertFalse(node_u.equals(node_g));
198 public void testUsesRefineFromContext() throws Exception {
199 SchemaContext context;
200 try (InputStream stream1 = new FileInputStream(new File(getClass().getResource("/model/baz.yang").toURI()));
201 InputStream stream2 = new FileInputStream(new File(getClass().getResource("/model/bar.yang").toURI()));
202 InputStream stream3 = new FileInputStream(new File(getClass().getResource("/model/foo.yang").toURI()))) {
203 context = parser.resolveSchemaContext(TestUtils.loadModules(Lists.newArrayList(stream1, stream2, stream3)));
206 try (InputStream stream = new FileInputStream(new File(getClass().getResource("/context-test/test2.yang")
208 module = TestUtils.loadModuleWithContext("test2", stream, context);
210 assertNotNull(module);
212 ContainerSchemaNode peer = (ContainerSchemaNode) module.getDataChildByName("peer");
213 ContainerSchemaNode destination = (ContainerSchemaNode) peer.getDataChildByName("destination");
214 Set<UsesNode> usesNodes = destination.getUses();
215 assertEquals(1, usesNodes.size());
216 UsesNode usesNode = usesNodes.iterator().next();
218 // test grouping path
219 List<QName> path = new ArrayList<>();
220 QName qname = new QName(URI.create("urn:opendaylight.baz"), simpleDateFormat.parse("2013-02-27"), "baz",
223 SchemaPath expectedPath = SchemaPath.create(path, true);
224 assertEquals(expectedPath, usesNode.getGroupingPath());
227 Map<SchemaPath, SchemaNode> refines = usesNode.getRefines();
228 assertEquals(3, refines.size());
230 LeafSchemaNode refineLeaf = null;
231 ContainerSchemaNode refineContainer = null;
232 ListSchemaNode refineList = null;
233 for (Map.Entry<SchemaPath, SchemaNode> entry : refines.entrySet()) {
234 SchemaNode value = entry.getValue();
235 if (value instanceof LeafSchemaNode) {
236 refineLeaf = (LeafSchemaNode) value;
237 } else if (value instanceof ContainerSchemaNode) {
238 refineContainer = (ContainerSchemaNode) value;
239 } else if (value instanceof ListSchemaNode) {
240 refineList = (ListSchemaNode) value;
245 assertNotNull(refineLeaf);
246 assertEquals("address", refineLeaf.getQName().getLocalName());
247 assertEquals("description of address defined by refine", refineLeaf.getDescription());
248 assertEquals("address reference added by refine", refineLeaf.getReference());
249 assertFalse(refineLeaf.isConfiguration());
250 assertTrue(refineLeaf.getConstraints().isMandatory());
251 Set<MustDefinition> leafMustConstraints = refineLeaf.getConstraints().getMustConstraints();
252 assertEquals(1, leafMustConstraints.size());
253 MustDefinition leafMust = leafMustConstraints.iterator().next();
254 assertEquals("\"ifType != 'ethernet' or (ifType = 'ethernet' and ifMTU = 1500)\"", leafMust.toString());
257 assertNotNull(refineContainer);
258 Set<MustDefinition> mustConstraints = refineContainer.getConstraints().getMustConstraints();
259 assertTrue(mustConstraints.isEmpty());
260 assertEquals("description of port defined by refine", refineContainer.getDescription());
261 assertEquals("port reference added by refine", refineContainer.getReference());
262 assertFalse(refineContainer.isConfiguration());
263 assertTrue(refineContainer.isPresenceContainer());
266 assertNotNull(refineList);
267 assertEquals("description of addresses defined by refine", refineList.getDescription());
268 assertEquals("addresses reference added by refine", refineList.getReference());
269 assertFalse(refineList.isConfiguration());
270 assertEquals(2, (int) refineList.getConstraints().getMinElements());
271 assertEquals(12, (int) refineList.getConstraints().getMaxElements());
275 public void testIdentity() throws Exception {
276 SchemaContext context;
277 File yangFile = new File(getClass().getResource("/types/custom-types-test@2012-4-4.yang").toURI());
278 File dependenciesDir = new File(getClass().getResource("/ietf").toURI());
279 YangContextParser parser = new YangParserImpl();
280 context = parser.parseFile(yangFile, dependenciesDir);
283 try (InputStream stream = new FileInputStream(new File(getClass().getResource("/context-test/test3.yang")
285 module = TestUtils.loadModuleWithContext("test3", stream, context);
287 assertNotNull(module);
289 Set<IdentitySchemaNode> identities = module.getIdentities();
290 assertEquals(1, identities.size());
292 IdentitySchemaNode identity = identities.iterator().next();
293 QName idQName = identity.getQName();
294 assertEquals(URI.create("urn:simple.demo.test3"), idQName.getNamespace());
295 assertEquals(simpleDateFormat.parse("2013-06-18"), idQName.getRevision());
296 assertEquals("t3", idQName.getPrefix());
297 assertEquals("pt", idQName.getLocalName());
299 IdentitySchemaNode baseIdentity = identity.getBaseIdentity();
300 QName idBaseQName = baseIdentity.getQName();
301 assertEquals(URI.create("urn:custom.types.demo"), idBaseQName.getNamespace());
302 assertEquals(simpleDateFormat.parse("2012-04-16"), idBaseQName.getRevision());
303 assertEquals("iit", idBaseQName.getPrefix());
304 assertEquals("service-type", idBaseQName.getLocalName());
308 public void testUnknownNodes() throws Exception {
309 SchemaContext context;
310 File yangFile = new File(getClass().getResource("/types/custom-types-test@2012-4-4.yang").toURI());
311 File dependenciesDir = new File(getClass().getResource("/ietf").toURI());
312 YangContextParser parser = new YangParserImpl();
313 context = parser.parseFile(yangFile, dependenciesDir);
316 try (InputStream stream = new FileInputStream(new File(getClass().getResource("/context-test/test3.yang")
318 module = TestUtils.loadModuleWithContext("test3", stream, context);
321 ContainerSchemaNode network = (ContainerSchemaNode) module.getDataChildByName("network");
322 List<UnknownSchemaNode> unknownNodes = network.getUnknownSchemaNodes();
323 assertEquals(1, unknownNodes.size());
325 UnknownSchemaNode un = unknownNodes.get(0);
326 QName unType = un.getNodeType();
327 assertEquals(URI.create("urn:custom.types.demo"), unType.getNamespace());
328 assertEquals(simpleDateFormat.parse("2012-04-16"), unType.getRevision());
329 assertEquals("custom", unType.getPrefix());
330 assertEquals("mountpoint", unType.getLocalName());
331 assertEquals("point", un.getNodeParameter());
332 assertNotNull(un.getExtensionDefinition());
337 public void testAugment() throws Exception {
339 SchemaContext context;
340 String resource = "/context-augment-test/test4.yang";
342 try (InputStream stream = new FileInputStream(new File(getClass().getResource(resource).toURI()))) {
343 context = parser.resolveSchemaContext(TestUtils.loadModules(Lists.newArrayList(stream)));
346 // load another modules and parse them against already existing context
348 try (InputStream stream1 = new FileInputStream(new File(getClass().getResource(
349 "/context-augment-test/test1.yang").toURI()));
350 InputStream stream2 = new FileInputStream(new File(getClass().getResource(
351 "/context-augment-test/test2.yang").toURI()));
352 InputStream stream3 = new FileInputStream(new File(getClass().getResource(
353 "/context-augment-test/test3.yang").toURI()))) {
354 List<InputStream> input = Lists.newArrayList(stream1, stream2, stream3);
355 modules = TestUtils.loadModulesWithContext(input, context);
357 assertNotNull(modules);
359 Module t3 = TestUtils.findModule(modules, "test4");
360 ContainerSchemaNode interfaces = (ContainerSchemaNode) t3.getDataChildByName("interfaces");
361 ListSchemaNode ifEntry = (ListSchemaNode) interfaces.getDataChildByName("ifEntry");
363 // test augmentation process
364 ContainerSchemaNode augmentHolder = (ContainerSchemaNode) ifEntry.getDataChildByName("augment-holder");
365 assertNotNull(augmentHolder);
366 DataSchemaNode ds0 = augmentHolder.getDataChildByName("ds0ChannelNumber");
368 DataSchemaNode interfaceId = augmentHolder.getDataChildByName("interface-id");
369 assertNotNull(interfaceId);
370 DataSchemaNode higherLayerIf = augmentHolder.getDataChildByName("higher-layer-if");
371 assertNotNull(higherLayerIf);
372 ContainerSchemaNode schemas = (ContainerSchemaNode) augmentHolder.getDataChildByName("schemas");
373 assertNotNull(schemas);
374 assertNotNull(schemas.getDataChildByName("id"));
376 // test augment target after augmentation: check if it is same instance
377 ListSchemaNode ifEntryAfterAugment = (ListSchemaNode) interfaces.getDataChildByName("ifEntry");
378 assertTrue(ifEntry == ifEntryAfterAugment);
382 public void testDeviation() throws Exception {
384 SchemaContext context;
385 String resource = "/model/bar.yang";
387 try (InputStream stream = new FileInputStream(new File(getClass().getResource(resource).toURI()))) {
388 context = parser.resolveSchemaContext(TestUtils.loadModules(Lists.newArrayList(stream)));
391 // load another modules and parse them against already existing context
393 try (InputStream stream = new FileInputStream(new File(getClass().getResource(
394 "/context-test/deviation-test.yang").toURI()))) {
395 List<InputStream> input = Lists.newArrayList(stream);
396 modules = TestUtils.loadModulesWithContext(input, context);
398 assertNotNull(modules);
401 Module testModule = TestUtils.findModule(modules, "deviation-test");
402 Set<Deviation> deviations = testModule.getDeviations();
403 assertEquals(1, deviations.size());
404 Deviation dev = deviations.iterator().next();
406 assertEquals("system/user ref", dev.getReference());
408 URI expectedNS = URI.create("urn:opendaylight.bar");
409 DateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
410 Date expectedRev = simpleDateFormat.parse("2013-07-03");
411 List<QName> path = new ArrayList<>();
412 path.add(new QName(expectedNS, expectedRev, "bar", "interfaces"));
413 path.add(new QName(expectedNS, expectedRev, "bar", "ifEntry"));
414 SchemaPath expectedPath = SchemaPath.create(path, true);
416 assertEquals(expectedPath, dev.getTargetPath());
417 assertEquals(Deviate.ADD, dev.getDeviate());