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.Arrays;
25 import java.util.Collections;
26 import java.util.Date;
27 import java.util.List;
30 import org.junit.Test;
31 import org.opendaylight.yangtools.yang.common.QName;
32 import org.opendaylight.yangtools.yang.model.api.AnyXmlSchemaNode;
33 import org.opendaylight.yangtools.yang.model.api.ChoiceNode;
34 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
35 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
36 import org.opendaylight.yangtools.yang.model.api.Deviation;
37 import org.opendaylight.yangtools.yang.model.api.Deviation.Deviate;
38 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
39 import org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode;
40 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
41 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
42 import org.opendaylight.yangtools.yang.model.api.Module;
43 import org.opendaylight.yangtools.yang.model.api.MustDefinition;
44 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
45 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
46 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
47 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
48 import org.opendaylight.yangtools.yang.model.api.UsesNode;
49 import org.opendaylight.yangtools.yang.model.api.type.RangeConstraint;
50 import org.opendaylight.yangtools.yang.model.parser.api.YangContextParser;
51 import org.opendaylight.yangtools.yang.model.util.ExtendedType;
53 public class YangParserWithContextTest {
54 private final DateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
55 private final YangParserImpl parser = new YangParserImpl();
58 public void testTypeFromContext() throws Exception {
59 String resource = "/ietf/ietf-inet-types@2010-09-24.yang";
60 InputStream stream = new FileInputStream(new File(getClass().getResource(resource).toURI()));
61 SchemaContext context = parser.resolveSchemaContext(TestUtils.loadModules(Lists.newArrayList(stream)));
64 resource = "/context-test/test1.yang";
65 InputStream stream2 = new FileInputStream(new File(getClass().getResource(resource).toURI()));
66 Module module = TestUtils.loadModuleWithContext("test1", stream2, context);
68 assertNotNull(module);
70 LeafSchemaNode leaf = (LeafSchemaNode) module.getDataChildByName("id");
72 ExtendedType leafType = (ExtendedType) leaf.getType();
73 QName qname = leafType.getQName();
74 assertEquals(URI.create("urn:simple.demo.test1"), qname.getNamespace());
75 assertEquals(simpleDateFormat.parse("2013-06-18"), qname.getRevision());
76 assertEquals("t1", qname.getPrefix());
77 assertEquals("port-number", qname.getLocalName());
79 ExtendedType leafBaseType = (ExtendedType) leafType.getBaseType();
80 qname = leafBaseType.getQName();
81 assertEquals(URI.create("urn:ietf:params:xml:ns:yang:ietf-inet-types"), qname.getNamespace());
82 assertEquals(simpleDateFormat.parse("2010-09-24"), qname.getRevision());
83 assertEquals("inet", qname.getPrefix());
84 assertEquals("port-number", qname.getLocalName());
86 ExtendedType dscpExt = (ExtendedType) TestUtils.findTypedef(module.getTypeDefinitions(), "dscp-ext");
87 List<RangeConstraint> ranges = dscpExt.getRangeConstraints();
88 assertEquals(1, ranges.size());
89 RangeConstraint range = ranges.get(0);
90 assertEquals(BigInteger.ZERO, range.getMin());
91 assertEquals(BigInteger.valueOf(63), range.getMax());
95 public void testUsesFromContext() throws Exception {
96 SchemaContext context;
97 try (InputStream stream1 = new FileInputStream(new File(getClass().getResource("/model/baz.yang").toURI()));
98 InputStream stream2 = new FileInputStream(new File(getClass().getResource("/model/bar.yang").toURI()));
99 InputStream stream3 = new FileInputStream(new File(getClass().getResource("/model/foo.yang").toURI()))) {
100 context = parser.resolveSchemaContext(TestUtils.loadModules(Lists.newArrayList(stream1, stream2, stream3)));
103 try (InputStream stream = new FileInputStream(new File(getClass().getResource("/context-test/test2.yang")
105 testModule = TestUtils.loadModuleWithContext("test2", stream, context);
107 assertNotNull(testModule);
109 // suffix _u = added by uses
110 // suffix _g = defined in grouping from context
113 Module contextModule = context.findModuleByNamespace(URI.create("urn:opendaylight.baz")).iterator().next();
114 assertNotNull(contextModule);
115 Set<GroupingDefinition> groupings = contextModule.getGroupings();
116 assertEquals(1, groupings.size());
117 GroupingDefinition grouping = groupings.iterator().next();
119 // get node containing uses
120 ContainerSchemaNode peer = (ContainerSchemaNode) testModule.getDataChildByName("peer");
121 ContainerSchemaNode destination = (ContainerSchemaNode) peer.getDataChildByName("destination");
124 Set<UsesNode> uses = destination.getUses();
125 assertEquals(1, uses.size());
127 // check uses process
128 AnyXmlSchemaNode data_u = (AnyXmlSchemaNode) destination.getDataChildByName("data");
129 assertNotNull(data_u);
130 assertTrue(data_u.isAddedByUses());
132 AnyXmlSchemaNode data_g = (AnyXmlSchemaNode) grouping.getDataChildByName("data");
133 assertNotNull(data_g);
134 assertFalse(data_g.isAddedByUses());
135 assertFalse(data_u.equals(data_g));
137 ChoiceNode how_u = (ChoiceNode) destination.getDataChildByName("how");
138 assertNotNull(how_u);
139 assertTrue(how_u.isAddedByUses());
141 ChoiceNode how_g = (ChoiceNode) grouping.getDataChildByName("how");
142 assertNotNull(how_g);
143 assertFalse(how_g.isAddedByUses());
144 assertFalse(how_u.equals(how_g));
146 LeafSchemaNode address_u = (LeafSchemaNode) destination.getDataChildByName("address");
147 assertNotNull(address_u);
148 assertTrue(address_u.isAddedByUses());
150 LeafSchemaNode address_g = (LeafSchemaNode) grouping.getDataChildByName("address");
151 assertNotNull(address_g);
152 assertFalse(address_g.isAddedByUses());
153 assertFalse(address_u.equals(address_g));
155 ContainerSchemaNode port_u = (ContainerSchemaNode) destination.getDataChildByName("port");
156 assertNotNull(port_u);
157 assertTrue(port_u.isAddedByUses());
159 ContainerSchemaNode port_g = (ContainerSchemaNode) grouping.getDataChildByName("port");
160 assertNotNull(port_g);
161 assertFalse(port_g.isAddedByUses());
162 assertFalse(port_u.equals(port_g));
164 ListSchemaNode addresses_u = (ListSchemaNode) destination.getDataChildByName("addresses");
165 assertNotNull(addresses_u);
166 assertTrue(addresses_u.isAddedByUses());
168 ListSchemaNode addresses_g = (ListSchemaNode) grouping.getDataChildByName("addresses");
169 assertNotNull(addresses_g);
170 assertFalse(addresses_g.isAddedByUses());
171 assertFalse(addresses_u.equals(addresses_g));
173 // grouping defined by 'uses'
174 Set<GroupingDefinition> groupings_u = destination.getGroupings();
175 assertEquals(1, groupings_u.size());
176 GroupingDefinition grouping_u = groupings_u.iterator().next();
177 assertTrue(grouping_u.isAddedByUses());
179 // grouping defined in 'grouping' node
180 Set<GroupingDefinition> groupings_g = grouping.getGroupings();
181 assertEquals(1, groupings_g.size());
182 GroupingDefinition grouping_g = groupings_g.iterator().next();
183 assertFalse(grouping_g.isAddedByUses());
184 assertFalse(grouping_u.equals(grouping_g));
186 List<UnknownSchemaNode> nodes_u = destination.getUnknownSchemaNodes();
187 assertEquals(1, nodes_u.size());
188 UnknownSchemaNode node_u = nodes_u.get(0);
189 assertTrue(node_u.isAddedByUses());
191 List<UnknownSchemaNode> nodes_g = grouping.getUnknownSchemaNodes();
192 assertEquals(1, nodes_g.size());
193 UnknownSchemaNode node_g = nodes_g.get(0);
194 assertFalse(node_g.isAddedByUses());
195 assertFalse(node_u.equals(node_g));
199 public void testUsesRefineFromContext() throws Exception {
200 SchemaContext context;
201 try (InputStream stream1 = new FileInputStream(new File(getClass().getResource("/model/baz.yang").toURI()));
202 InputStream stream2 = new FileInputStream(new File(getClass().getResource("/model/bar.yang").toURI()));
203 InputStream stream3 = new FileInputStream(new File(getClass().getResource("/model/foo.yang").toURI()))) {
204 context = parser.resolveSchemaContext(TestUtils.loadModules(Lists.newArrayList(stream1, stream2, stream3)));
207 try (InputStream stream = new FileInputStream(new File(getClass().getResource("/context-test/test2.yang")
209 module = TestUtils.loadModuleWithContext("test2", stream, context);
211 assertNotNull(module);
213 ContainerSchemaNode peer = (ContainerSchemaNode) module.getDataChildByName("peer");
214 ContainerSchemaNode destination = (ContainerSchemaNode) peer.getDataChildByName("destination");
215 Set<UsesNode> usesNodes = destination.getUses();
216 assertEquals(1, usesNodes.size());
217 UsesNode usesNode = usesNodes.iterator().next();
219 // test grouping path
220 List<QName> path = new ArrayList<>();
221 QName qname = new QName(URI.create("urn:opendaylight.baz"), simpleDateFormat.parse("2013-02-27"), "baz",
224 SchemaPath expectedPath = SchemaPath.create(path, true);
225 assertEquals(expectedPath, usesNode.getGroupingPath());
228 Map<SchemaPath, SchemaNode> refines = usesNode.getRefines();
229 assertEquals(3, refines.size());
231 LeafSchemaNode refineLeaf = null;
232 ContainerSchemaNode refineContainer = null;
233 ListSchemaNode refineList = null;
234 for (Map.Entry<SchemaPath, SchemaNode> entry : refines.entrySet()) {
235 SchemaNode value = entry.getValue();
236 if (value instanceof LeafSchemaNode) {
237 refineLeaf = (LeafSchemaNode) value;
238 } else if (value instanceof ContainerSchemaNode) {
239 refineContainer = (ContainerSchemaNode) value;
240 } else if (value instanceof ListSchemaNode) {
241 refineList = (ListSchemaNode) value;
246 assertNotNull(refineLeaf);
247 assertEquals("address", refineLeaf.getQName().getLocalName());
248 assertEquals("description of address defined by refine", refineLeaf.getDescription());
249 assertEquals("address reference added by refine", refineLeaf.getReference());
250 assertFalse(refineLeaf.isConfiguration());
251 assertTrue(refineLeaf.getConstraints().isMandatory());
252 Set<MustDefinition> leafMustConstraints = refineLeaf.getConstraints().getMustConstraints();
253 assertEquals(1, leafMustConstraints.size());
254 MustDefinition leafMust = leafMustConstraints.iterator().next();
255 assertEquals("\"ifType != 'ethernet' or (ifType = 'ethernet' and ifMTU = 1500)\"", leafMust.toString());
258 assertNotNull(refineContainer);
259 Set<MustDefinition> mustConstraints = refineContainer.getConstraints().getMustConstraints();
260 assertTrue(mustConstraints.isEmpty());
261 assertEquals("description of port defined by refine", refineContainer.getDescription());
262 assertEquals("port reference added by refine", refineContainer.getReference());
263 assertFalse(refineContainer.isConfiguration());
264 assertTrue(refineContainer.isPresenceContainer());
267 assertNotNull(refineList);
268 assertEquals("description of addresses defined by refine", refineList.getDescription());
269 assertEquals("addresses reference added by refine", refineList.getReference());
270 assertFalse(refineList.isConfiguration());
271 assertEquals(2, (int) refineList.getConstraints().getMinElements());
272 assertEquals(12, (int) refineList.getConstraints().getMaxElements());
276 public void testIdentity() throws Exception {
277 SchemaContext context;
278 File yangFile = new File(getClass().getResource("/types/custom-types-test@2012-4-4.yang").toURI());
279 File dependenciesDir = new File(getClass().getResource("/ietf").toURI());
280 YangContextParser parser = new YangParserImpl();
281 context = parser.parseFile(yangFile, dependenciesDir);
284 try (InputStream stream = new FileInputStream(new File(getClass().getResource("/context-test/test3.yang")
286 module = TestUtils.loadModuleWithContext("test3", stream, context);
288 assertNotNull(module);
290 Set<IdentitySchemaNode> identities = module.getIdentities();
291 assertEquals(1, identities.size());
293 IdentitySchemaNode identity = identities.iterator().next();
294 QName idQName = identity.getQName();
295 assertEquals(URI.create("urn:simple.demo.test3"), idQName.getNamespace());
296 assertEquals(simpleDateFormat.parse("2013-06-18"), idQName.getRevision());
297 assertEquals("t3", idQName.getPrefix());
298 assertEquals("pt", idQName.getLocalName());
300 IdentitySchemaNode baseIdentity = identity.getBaseIdentity();
301 QName idBaseQName = baseIdentity.getQName();
302 assertEquals(URI.create("urn:custom.types.demo"), idBaseQName.getNamespace());
303 assertEquals(simpleDateFormat.parse("2012-04-16"), idBaseQName.getRevision());
304 assertEquals("iit", idBaseQName.getPrefix());
305 assertEquals("service-type", idBaseQName.getLocalName());
309 public void testUnknownNodes() throws Exception {
310 SchemaContext context;
311 File yangFile = new File(getClass().getResource("/types/custom-types-test@2012-4-4.yang").toURI());
312 File dependenciesDir = new File(getClass().getResource("/ietf").toURI());
313 YangContextParser parser = new YangParserImpl();
314 context = parser.parseFile(yangFile, dependenciesDir);
317 try (InputStream stream = new FileInputStream(new File(getClass().getResource("/context-test/test3.yang")
319 module = TestUtils.loadModuleWithContext("test3", stream, context);
322 ContainerSchemaNode network = (ContainerSchemaNode) module.getDataChildByName("network");
323 List<UnknownSchemaNode> unknownNodes = network.getUnknownSchemaNodes();
324 assertEquals(1, unknownNodes.size());
326 UnknownSchemaNode un = unknownNodes.get(0);
327 QName unType = un.getNodeType();
328 assertEquals(URI.create("urn:custom.types.demo"), unType.getNamespace());
329 assertEquals(simpleDateFormat.parse("2012-04-16"), unType.getRevision());
330 assertEquals("custom", unType.getPrefix());
331 assertEquals("mountpoint", unType.getLocalName());
332 assertEquals("point", un.getNodeParameter());
333 assertNotNull(un.getExtensionDefinition());
337 public void testAugment() throws Exception {
339 String resource = "/context-augment-test/test4.yang";
340 SchemaContext context = parser.parseFiles(Collections.singleton(new File(getClass().getResource(resource)
343 // load another modules and parse them against already existing context
344 File test1 = new File(getClass().getResource("/context-augment-test/test1.yang").toURI());
345 File test2 = new File(getClass().getResource("/context-augment-test/test2.yang").toURI());
346 File test3 = new File(getClass().getResource("/context-augment-test/test3.yang").toURI());
347 Set<Module> modules = parser.parseFiles(Arrays.asList(test1, test2, test3), context).getModules();
348 assertNotNull(modules);
350 Module t4 = TestUtils.findModule(modules, "test4");
351 ContainerSchemaNode interfaces = (ContainerSchemaNode) t4.getDataChildByName("interfaces");
352 ListSchemaNode ifEntry = (ListSchemaNode) interfaces.getDataChildByName("ifEntry");
354 // test augmentation process
355 ContainerSchemaNode augmentHolder = (ContainerSchemaNode) ifEntry.getDataChildByName("augment-holder");
356 assertNotNull(augmentHolder);
357 DataSchemaNode ds0 = augmentHolder.getDataChildByName("ds0ChannelNumber");
359 DataSchemaNode interfaceId = augmentHolder.getDataChildByName("interface-id");
360 assertNotNull(interfaceId);
361 DataSchemaNode higherLayerIf = augmentHolder.getDataChildByName("higher-layer-if");
362 assertNotNull(higherLayerIf);
363 ContainerSchemaNode schemas = (ContainerSchemaNode) augmentHolder.getDataChildByName("schemas");
364 assertNotNull(schemas);
365 assertNotNull(schemas.getDataChildByName("id"));
367 // test augment target after augmentation: check if it is same instance
368 ListSchemaNode ifEntryAfterAugment = (ListSchemaNode) interfaces.getDataChildByName("ifEntry");
369 assertTrue(ifEntry == ifEntryAfterAugment);
373 public void testDeviation() throws Exception {
375 SchemaContext context;
376 String resource = "/model/bar.yang";
378 try (InputStream stream = new FileInputStream(new File(getClass().getResource(resource).toURI()))) {
379 context = parser.resolveSchemaContext(TestUtils.loadModules(Lists.newArrayList(stream)));
382 // load another modules and parse them against already existing context
384 try (InputStream stream = new FileInputStream(new File(getClass().getResource(
385 "/context-test/deviation-test.yang").toURI()))) {
386 List<InputStream> input = Lists.newArrayList(stream);
387 modules = TestUtils.loadModulesWithContext(input, context);
389 assertNotNull(modules);
392 Module testModule = TestUtils.findModule(modules, "deviation-test");
393 Set<Deviation> deviations = testModule.getDeviations();
394 assertEquals(1, deviations.size());
395 Deviation dev = deviations.iterator().next();
397 assertEquals("system/user ref", dev.getReference());
399 URI expectedNS = URI.create("urn:opendaylight.bar");
400 DateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
401 Date expectedRev = simpleDateFormat.parse("2013-07-03");
402 List<QName> path = new ArrayList<>();
403 path.add(new QName(expectedNS, expectedRev, "bar", "interfaces"));
404 path.add(new QName(expectedNS, expectedRev, "bar", "ifEntry"));
405 SchemaPath expectedPath = SchemaPath.create(path, true);
407 assertEquals(expectedPath, dev.getTargetPath());
408 assertEquals(Deviate.ADD, dev.getDeviate());