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;
16 import java.io.FileInputStream;
17 import java.io.InputStream;
19 import java.text.DateFormat;
20 import java.text.SimpleDateFormat;
21 import java.util.ArrayList;
22 import java.util.Date;
23 import java.util.List;
27 import org.junit.Ignore;
28 import org.junit.Test;
29 import org.opendaylight.yangtools.yang.common.QName;
30 import org.opendaylight.yangtools.yang.model.api.*;
31 import org.opendaylight.yangtools.yang.model.api.Deviation.Deviate;
32 import org.opendaylight.yangtools.yang.model.api.type.RangeConstraint;
33 import org.opendaylight.yangtools.yang.model.parser.api.YangModelParser;
34 import org.opendaylight.yangtools.yang.model.util.ExtendedType;
36 import com.google.common.collect.Lists;
38 public class YangParserWithContextTest {
39 private final DateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
40 private final YangParserImpl parser = new YangParserImpl();
43 public void testTypeFromContext() throws Exception {
44 String resource = "/ietf/ietf-inet-types@2010-09-24.yang";
45 InputStream stream = new FileInputStream(new File(getClass().getResource(resource).toURI()));
46 SchemaContext context = parser.resolveSchemaContext(TestUtils.loadModules(Lists.newArrayList(stream)));
49 resource = "/context-test/test1.yang";
50 InputStream stream2 = new FileInputStream(new File(getClass().getResource(resource).toURI()));
51 Module module = TestUtils.loadModuleWithContext("test1", stream2, context);
53 assertNotNull(module);
55 LeafSchemaNode leaf = (LeafSchemaNode) module.getDataChildByName("id");
57 ExtendedType leafType = (ExtendedType) leaf.getType();
58 QName qname = leafType.getQName();
59 assertEquals(URI.create("urn:simple.demo.test1"), qname.getNamespace());
60 assertEquals(simpleDateFormat.parse("2013-06-18"), qname.getRevision());
61 assertEquals("t1", qname.getPrefix());
62 assertEquals("port-number", qname.getLocalName());
64 ExtendedType leafBaseType = (ExtendedType) leafType.getBaseType();
65 qname = leafBaseType.getQName();
66 assertEquals(URI.create("urn:ietf:params:xml:ns:yang:ietf-inet-types"), qname.getNamespace());
67 assertEquals(simpleDateFormat.parse("2010-09-24"), qname.getRevision());
68 assertEquals("inet", qname.getPrefix());
69 assertEquals("port-number", qname.getLocalName());
71 ExtendedType dscpExt = (ExtendedType) TestUtils.findTypedef(module.getTypeDefinitions(), "dscp-ext");
72 List<RangeConstraint> ranges = dscpExt.getRangeConstraints();
73 assertEquals(1, ranges.size());
74 RangeConstraint range = ranges.get(0);
75 assertEquals(0L, range.getMin());
76 assertEquals(63L, range.getMax());
80 public void testUsesFromContext() throws Exception {
81 SchemaContext context;
82 try (InputStream stream1 = new FileInputStream(new File(getClass().getResource("/model/baz.yang").toURI()));
83 InputStream stream2 = new FileInputStream(new File(getClass().getResource("/model/bar.yang").toURI()));
84 InputStream stream3 = new FileInputStream(new File(getClass().getResource("/model/foo.yang").toURI()))) {
85 context = parser.resolveSchemaContext(TestUtils.loadModules(Lists.newArrayList(stream1, stream2, stream3)));
88 try (InputStream stream = new FileInputStream(new File(getClass().getResource("/context-test/test2.yang")
90 testModule = TestUtils.loadModuleWithContext("test2", stream, context);
92 assertNotNull(testModule);
94 // suffix _u = added by uses
95 // suffix _g = defined in grouping from context
98 Module contextModule = context.findModuleByNamespace(URI.create("urn:opendaylight.baz")).iterator().next();
99 assertNotNull(contextModule);
100 Set<GroupingDefinition> groupings = contextModule.getGroupings();
101 assertEquals(1, groupings.size());
102 GroupingDefinition grouping = groupings.iterator().next();
104 // get node containing uses
105 ContainerSchemaNode peer = (ContainerSchemaNode) testModule.getDataChildByName("peer");
106 ContainerSchemaNode destination = (ContainerSchemaNode) peer.getDataChildByName("destination");
109 Set<UsesNode> uses = destination.getUses();
110 assertEquals(1, uses.size());
112 // check uses process
113 AnyXmlSchemaNode data_u = (AnyXmlSchemaNode) destination.getDataChildByName("data");
114 assertNotNull(data_u);
115 assertTrue(data_u.isAddedByUses());
117 AnyXmlSchemaNode data_g = (AnyXmlSchemaNode) grouping.getDataChildByName("data");
118 assertNotNull(data_g);
119 assertFalse(data_g.isAddedByUses());
120 assertFalse(data_u.equals(data_g));
122 ChoiceNode how_u = (ChoiceNode) destination.getDataChildByName("how");
123 assertNotNull(how_u);
124 assertTrue(how_u.isAddedByUses());
126 ChoiceNode how_g = (ChoiceNode) grouping.getDataChildByName("how");
127 assertNotNull(how_g);
128 assertFalse(how_g.isAddedByUses());
129 assertFalse(how_u.equals(how_g));
131 LeafSchemaNode address_u = (LeafSchemaNode) destination.getDataChildByName("address");
132 assertNotNull(address_u);
133 assertTrue(address_u.isAddedByUses());
135 LeafSchemaNode address_g = (LeafSchemaNode) grouping.getDataChildByName("address");
136 assertNotNull(address_g);
137 assertFalse(address_g.isAddedByUses());
138 assertFalse(address_u.equals(address_g));
140 ContainerSchemaNode port_u = (ContainerSchemaNode) destination.getDataChildByName("port");
141 assertNotNull(port_u);
142 assertTrue(port_u.isAddedByUses());
144 ContainerSchemaNode port_g = (ContainerSchemaNode) grouping.getDataChildByName("port");
145 assertNotNull(port_g);
146 assertFalse(port_g.isAddedByUses());
147 assertFalse(port_u.equals(port_g));
149 ListSchemaNode addresses_u = (ListSchemaNode) destination.getDataChildByName("addresses");
150 assertNotNull(addresses_u);
151 assertTrue(addresses_u.isAddedByUses());
153 ListSchemaNode addresses_g = (ListSchemaNode) grouping.getDataChildByName("addresses");
154 assertNotNull(addresses_g);
155 assertFalse(addresses_g.isAddedByUses());
156 assertFalse(addresses_u.equals(addresses_g));
158 // grouping defined by 'uses'
159 Set<GroupingDefinition> groupings_u = destination.getGroupings();
160 assertEquals(1, groupings_u.size());
161 GroupingDefinition grouping_u = groupings_u.iterator().next();
162 assertTrue(grouping_u.isAddedByUses());
164 // grouping defined in 'grouping' node
165 Set<GroupingDefinition> groupings_g = grouping.getGroupings();
166 assertEquals(1, groupings_g.size());
167 GroupingDefinition grouping_g = groupings_g.iterator().next();
168 assertFalse(grouping_g.isAddedByUses());
169 assertFalse(grouping_u.equals(grouping_g));
171 List<UnknownSchemaNode> nodes_u = destination.getUnknownSchemaNodes();
172 assertEquals(1, nodes_u.size());
173 UnknownSchemaNode node_u = nodes_u.get(0);
174 assertTrue(node_u.isAddedByUses());
176 List<UnknownSchemaNode> nodes_g = grouping.getUnknownSchemaNodes();
177 assertEquals(1, nodes_g.size());
178 UnknownSchemaNode node_g = nodes_g.get(0);
179 assertFalse(node_g.isAddedByUses());
180 assertFalse(node_u.equals(node_g));
184 public void testUsesRefineFromContext() throws Exception {
185 SchemaContext context;
186 try (InputStream stream1 = new FileInputStream(new File(getClass().getResource("/model/baz.yang").toURI()));
187 InputStream stream2 = new FileInputStream(new File(getClass().getResource("/model/bar.yang").toURI()));
188 InputStream stream3 = new FileInputStream(new File(getClass().getResource("/model/foo.yang").toURI()))) {
189 context = parser.resolveSchemaContext(TestUtils.loadModules(Lists.newArrayList(stream1, stream2, stream3)));
192 try (InputStream stream = new FileInputStream(new File(getClass().getResource("/context-test/test2.yang")
194 module = TestUtils.loadModuleWithContext("test2", stream, context);
196 assertNotNull(module);
198 ContainerSchemaNode peer = (ContainerSchemaNode) module.getDataChildByName("peer");
199 ContainerSchemaNode destination = (ContainerSchemaNode) peer.getDataChildByName("destination");
200 Set<UsesNode> usesNodes = destination.getUses();
201 assertEquals(1, usesNodes.size());
202 UsesNode usesNode = usesNodes.iterator().next();
204 // test grouping path
205 List<QName> path = new ArrayList<>();
206 QName qname = new QName(URI.create("urn:opendaylight.baz"), simpleDateFormat.parse("2013-02-27"), "baz",
209 SchemaPath expectedPath = new SchemaPath(path, true);
210 assertEquals(expectedPath, usesNode.getGroupingPath());
213 Map<SchemaPath, SchemaNode> refines = usesNode.getRefines();
214 assertEquals(3, refines.size());
216 LeafSchemaNode refineLeaf = null;
217 ContainerSchemaNode refineContainer = null;
218 ListSchemaNode refineList = null;
219 for (Map.Entry<SchemaPath, SchemaNode> entry : refines.entrySet()) {
220 SchemaNode value = entry.getValue();
221 if (value instanceof LeafSchemaNode) {
222 refineLeaf = (LeafSchemaNode) value;
223 } else if (value instanceof ContainerSchemaNode) {
224 refineContainer = (ContainerSchemaNode) value;
225 } else if (value instanceof ListSchemaNode) {
226 refineList = (ListSchemaNode) value;
231 assertNotNull(refineLeaf);
232 assertEquals("address", refineLeaf.getQName().getLocalName());
233 assertEquals("description of address defined by refine", refineLeaf.getDescription());
234 assertEquals("address reference added by refine", refineLeaf.getReference());
235 assertFalse(refineLeaf.isConfiguration());
236 assertTrue(refineLeaf.getConstraints().isMandatory());
237 Set<MustDefinition> leafMustConstraints = refineLeaf.getConstraints().getMustConstraints();
238 assertEquals(1, leafMustConstraints.size());
239 MustDefinition leafMust = leafMustConstraints.iterator().next();
240 assertEquals("\"ifType != 'ethernet' or (ifType = 'ethernet' and ifMTU = 1500)\"", leafMust.toString());
243 assertNotNull(refineContainer);
244 Set<MustDefinition> mustConstraints = refineContainer.getConstraints().getMustConstraints();
245 assertTrue(mustConstraints.isEmpty());
246 assertEquals("description of port defined by refine", refineContainer.getDescription());
247 assertEquals("port reference added by refine", refineContainer.getReference());
248 assertFalse(refineContainer.isConfiguration());
249 assertTrue(refineContainer.isPresenceContainer());
252 assertNotNull(refineList);
253 assertEquals("description of addresses defined by refine", refineList.getDescription());
254 assertEquals("addresses reference added by refine", refineList.getReference());
255 assertFalse(refineList.isConfiguration());
256 assertEquals(2, (int) refineList.getConstraints().getMinElements());
257 assertEquals(12, (int) refineList.getConstraints().getMaxElements());
261 public void testIdentity() throws Exception {
262 SchemaContext context;
263 File yangFile = new File(getClass().getResource("/types/custom-types-test@2012-4-4.yang").toURI());
264 File dependenciesDir = new File(getClass().getResource("/ietf").toURI());
265 YangModelParser parser = new YangParserImpl();
266 context = parser.resolveSchemaContext(parser.parseYangModels(yangFile, dependenciesDir));
269 try (InputStream stream = new FileInputStream(new File(getClass().getResource("/context-test/test3.yang")
271 module = TestUtils.loadModuleWithContext("test3", stream, context);
273 assertNotNull(module);
275 Set<IdentitySchemaNode> identities = module.getIdentities();
276 assertEquals(1, identities.size());
278 IdentitySchemaNode identity = identities.iterator().next();
279 QName idQName = identity.getQName();
280 assertEquals(URI.create("urn:simple.demo.test3"), idQName.getNamespace());
281 assertEquals(simpleDateFormat.parse("2013-06-18"), idQName.getRevision());
282 assertEquals("t3", idQName.getPrefix());
283 assertEquals("pt", idQName.getLocalName());
285 IdentitySchemaNode baseIdentity = identity.getBaseIdentity();
286 QName idBaseQName = baseIdentity.getQName();
287 assertEquals(URI.create("urn:custom.types.demo"), idBaseQName.getNamespace());
288 assertEquals(simpleDateFormat.parse("2012-04-16"), idBaseQName.getRevision());
289 assertEquals("iit", idBaseQName.getPrefix());
290 assertEquals("service-type", idBaseQName.getLocalName());
294 public void testUnknownNodes() throws Exception {
295 SchemaContext context;
296 File yangFile = new File(getClass().getResource("/types/custom-types-test@2012-4-4.yang").toURI());
297 File dependenciesDir = new File(getClass().getResource("/ietf").toURI());
298 YangModelParser parser = new YangParserImpl();
299 context = parser.resolveSchemaContext(parser.parseYangModels(yangFile, dependenciesDir));
302 try (InputStream stream = new FileInputStream(new File(getClass().getResource("/context-test/test3.yang")
304 module = TestUtils.loadModuleWithContext("test3", stream, context);
307 ContainerSchemaNode network = (ContainerSchemaNode) module.getDataChildByName("network");
308 List<UnknownSchemaNode> unknownNodes = network.getUnknownSchemaNodes();
309 assertEquals(1, unknownNodes.size());
311 UnknownSchemaNode un = unknownNodes.get(0);
312 QName unType = un.getNodeType();
313 assertEquals(URI.create("urn:custom.types.demo"), unType.getNamespace());
314 assertEquals(simpleDateFormat.parse("2012-04-16"), unType.getRevision());
315 assertEquals("custom", unType.getPrefix());
316 assertEquals("mountpoint", unType.getLocalName());
317 assertEquals("point", un.getNodeParameter());
318 assertNotNull(un.getExtensionDefinition());
323 public void testAugment() throws Exception {
325 SchemaContext context;
326 String resource = "/context-augment-test/test4.yang";
328 try (InputStream stream = new FileInputStream(new File(getClass().getResource(resource).toURI()))) {
329 context = parser.resolveSchemaContext(TestUtils.loadModules(Lists.newArrayList(stream)));
332 // load another modules and parse them against already existing context
334 try (InputStream stream1 = new FileInputStream(new File(getClass().getResource(
335 "/context-augment-test/test1.yang").toURI()));
336 InputStream stream2 = new FileInputStream(new File(getClass().getResource(
337 "/context-augment-test/test2.yang").toURI()));
338 InputStream stream3 = new FileInputStream(new File(getClass().getResource(
339 "/context-augment-test/test3.yang").toURI()))) {
340 List<InputStream> input = Lists.newArrayList(stream1, stream2, stream3);
341 modules = TestUtils.loadModulesWithContext(input, context);
343 assertNotNull(modules);
345 Module t3 = TestUtils.findModule(modules, "test4");
346 ContainerSchemaNode interfaces = (ContainerSchemaNode) t3.getDataChildByName("interfaces");
347 ListSchemaNode ifEntry = (ListSchemaNode) interfaces.getDataChildByName("ifEntry");
349 // test augmentation process
350 ContainerSchemaNode augmentHolder = (ContainerSchemaNode) ifEntry.getDataChildByName("augment-holder");
351 assertNotNull(augmentHolder);
352 DataSchemaNode ds0 = augmentHolder.getDataChildByName("ds0ChannelNumber");
354 DataSchemaNode interfaceId = augmentHolder.getDataChildByName("interface-id");
355 assertNotNull(interfaceId);
356 DataSchemaNode higherLayerIf = augmentHolder.getDataChildByName("higher-layer-if");
357 assertNotNull(higherLayerIf);
358 ContainerSchemaNode schemas = (ContainerSchemaNode) augmentHolder.getDataChildByName("schemas");
359 assertNotNull(schemas);
360 assertNotNull(schemas.getDataChildByName("id"));
362 // test augment target after augmentation: check if it is same instance
363 ListSchemaNode ifEntryAfterAugment = (ListSchemaNode) interfaces.getDataChildByName("ifEntry");
364 assertTrue(ifEntry == ifEntryAfterAugment);
368 public void testDeviation() throws Exception {
370 SchemaContext context;
371 String resource = "/model/bar.yang";
373 try (InputStream stream = new FileInputStream(new File(getClass().getResource(resource).toURI()))) {
374 context = parser.resolveSchemaContext(TestUtils.loadModules(Lists.newArrayList(stream)));
377 // load another modules and parse them against already existing context
379 try (InputStream stream = new FileInputStream(new File(getClass().getResource(
380 "/context-test/deviation-test.yang").toURI()))) {
381 List<InputStream> input = Lists.newArrayList(stream);
382 modules = TestUtils.loadModulesWithContext(input, context);
384 assertNotNull(modules);
387 Module testModule = TestUtils.findModule(modules, "deviation-test");
388 Set<Deviation> deviations = testModule.getDeviations();
389 assertEquals(1, deviations.size());
390 Deviation dev = deviations.iterator().next();
392 assertEquals("system/user ref", dev.getReference());
394 URI expectedNS = URI.create("urn:opendaylight.bar");
395 DateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
396 Date expectedRev = simpleDateFormat.parse("2013-07-03");
397 List<QName> path = new ArrayList<>();
398 path.add(new QName(expectedNS, expectedRev, "bar", "interfaces"));
399 path.add(new QName(expectedNS, expectedRev, "bar", "ifEntry"));
400 SchemaPath expectedPath = new SchemaPath(path, true);
402 assertEquals(expectedPath, dev.getTargetPath());
403 assertEquals(Deviate.ADD, dev.getDeviate());