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.*;
12 import java.io.FileInputStream;
13 import java.io.InputStream;
15 import java.text.DateFormat;
16 import java.text.SimpleDateFormat;
17 import java.util.ArrayList;
18 import java.util.Date;
19 import java.util.List;
23 import org.junit.Ignore;
24 import org.junit.Test;
25 import org.opendaylight.yangtools.yang.common.QName;
26 import org.opendaylight.yangtools.yang.model.api.AnyXmlSchemaNode;
27 import org.opendaylight.yangtools.yang.model.api.ChoiceNode;
28 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
29 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
30 import org.opendaylight.yangtools.yang.model.api.Deviation;
31 import org.opendaylight.yangtools.yang.model.api.Deviation.Deviate;
32 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
33 import org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode;
34 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
35 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
36 import org.opendaylight.yangtools.yang.model.api.Module;
37 import org.opendaylight.yangtools.yang.model.api.MustDefinition;
38 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
39 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
40 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
41 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
42 import org.opendaylight.yangtools.yang.model.api.UsesNode;
43 import org.opendaylight.yangtools.yang.model.api.type.RangeConstraint;
44 import org.opendaylight.yangtools.yang.model.util.ExtendedType;
46 import com.google.common.collect.Lists;
49 public class YangParserWithContextTest {
50 private final DateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
51 private final YangParserImpl parser = new YangParserImpl();
54 public void testTypeFromContext() throws Exception {
55 SchemaContext context = null;
56 String resource = "/types/ietf-inet-types@2010-09-24.yang";
57 InputStream stream = new FileInputStream(getClass().getResource(resource).getPath());
58 context = parser.resolveSchemaContext(TestUtils.loadModules(Lists.newArrayList(stream)));
62 resource = "/context-test/test1.yang";
63 InputStream stream2 = new FileInputStream(getClass().getResource(resource).getPath());
64 module = TestUtils.loadModuleWithContext(stream2, context);
66 assertNotNull(module);
68 LeafSchemaNode leaf = (LeafSchemaNode) module.getDataChildByName("id");
70 ExtendedType leafType = (ExtendedType) leaf.getType();
71 QName qname = leafType.getQName();
72 assertEquals(URI.create("urn:simple.demo.test1"), qname.getNamespace());
73 assertEquals(simpleDateFormat.parse("2013-06-18"), qname.getRevision());
74 assertEquals("t1", qname.getPrefix());
75 assertEquals("port-number", qname.getLocalName());
77 ExtendedType leafBaseType = (ExtendedType) leafType.getBaseType();
78 qname = leafBaseType.getQName();
79 assertEquals(URI.create("urn:ietf:params:xml:ns:yang:ietf-inet-types"), qname.getNamespace());
80 assertEquals(simpleDateFormat.parse("2010-09-24"), qname.getRevision());
81 assertEquals("inet", qname.getPrefix());
82 assertEquals("port-number", qname.getLocalName());
84 ExtendedType dscpExt = (ExtendedType) TestUtils.findTypedef(module.getTypeDefinitions(), "dscp-ext");
85 List<RangeConstraint> ranges = dscpExt.getRanges();
86 assertEquals(1, ranges.size());
87 RangeConstraint range = ranges.get(0);
88 assertEquals(0L, range.getMin());
89 assertEquals(63L, range.getMax());
93 public void testUsesFromContext() throws Exception {
94 SchemaContext context = null;
95 try (InputStream stream1 = new FileInputStream(getClass().getResource("/model/custom.yang").getPath());
96 InputStream stream2 = new FileInputStream(getClass().getResource("/model/types.yang").getPath());
97 InputStream stream3 = new FileInputStream(getClass().getResource("/model/nodes.yang").getPath())) {
98 context = parser.resolveSchemaContext(TestUtils.loadModules(Lists.newArrayList(stream1, stream2, stream3)));
100 Module testModule = null;
101 try (InputStream stream = new FileInputStream(getClass().getResource("/context-test/test2.yang").getPath())) {
102 testModule = TestUtils.loadModuleWithContext(stream, context);
104 assertNotNull(testModule);
106 // suffix _u = added by uses
107 // suffix _g = defined in grouping from context
110 Module contextModule = context.findModuleByNamespace(URI.create("urn:custom.nodes.test"));
111 assertNotNull(contextModule);
112 Set<GroupingDefinition> groupings = contextModule.getGroupings();
113 assertEquals(1, groupings.size());
114 GroupingDefinition grouping = groupings.iterator().next();
116 // get node containing uses
117 ContainerSchemaNode peer = (ContainerSchemaNode) testModule.getDataChildByName("peer");
118 ContainerSchemaNode destination = (ContainerSchemaNode) peer.getDataChildByName("destination");
121 Set<UsesNode> uses = destination.getUses();
122 assertEquals(1, uses.size());
124 // check uses process
125 AnyXmlSchemaNode data_u = (AnyXmlSchemaNode) destination.getDataChildByName("data");
126 assertNotNull(data_u);
127 assertTrue(data_u.isAddedByUses());
129 AnyXmlSchemaNode data_g = (AnyXmlSchemaNode) grouping.getDataChildByName("data");
130 assertNotNull(data_g);
131 assertFalse(data_g.isAddedByUses());
132 assertFalse(data_u.equals(data_g));
134 ChoiceNode how_u = (ChoiceNode) destination.getDataChildByName("how");
135 assertNotNull(how_u);
136 assertTrue(how_u.isAddedByUses());
138 ChoiceNode how_g = (ChoiceNode) grouping.getDataChildByName("how");
139 assertNotNull(how_g);
140 assertFalse(how_g.isAddedByUses());
141 assertFalse(how_u.equals(how_g));
143 LeafSchemaNode address_u = (LeafSchemaNode) destination.getDataChildByName("address");
144 assertNotNull(address_u);
145 assertTrue(address_u.isAddedByUses());
147 LeafSchemaNode address_g = (LeafSchemaNode) grouping.getDataChildByName("address");
148 assertNotNull(address_g);
149 assertFalse(address_g.isAddedByUses());
150 assertFalse(address_u.equals(address_g));
152 ContainerSchemaNode port_u = (ContainerSchemaNode) destination.getDataChildByName("port");
153 assertNotNull(port_u);
154 assertTrue(port_u.isAddedByUses());
156 ContainerSchemaNode port_g = (ContainerSchemaNode) grouping.getDataChildByName("port");
157 assertNotNull(port_g);
158 assertFalse(port_g.isAddedByUses());
159 assertFalse(port_u.equals(port_g));
161 ListSchemaNode addresses_u = (ListSchemaNode) destination.getDataChildByName("addresses");
162 assertNotNull(addresses_u);
163 assertTrue(addresses_u.isAddedByUses());
165 ListSchemaNode addresses_g = (ListSchemaNode) grouping.getDataChildByName("addresses");
166 assertNotNull(addresses_g);
167 assertFalse(addresses_g.isAddedByUses());
168 assertFalse(addresses_u.equals(addresses_g));
170 // grouping defined by 'uses'
171 Set<GroupingDefinition> groupings_u = destination.getGroupings();
172 assertEquals(1, groupings_u.size());
173 GroupingDefinition grouping_u = groupings_u.iterator().next();
174 assertTrue(grouping_u.isAddedByUses());
176 // grouping defined in 'grouping' node
177 Set<GroupingDefinition> groupings_g = grouping.getGroupings();
178 assertEquals(1, groupings_g.size());
179 GroupingDefinition grouping_g = groupings_g.iterator().next();
180 assertFalse(grouping_g.isAddedByUses());
181 assertFalse(grouping_u.equals(grouping_g));
183 List<UnknownSchemaNode> nodes_u = destination.getUnknownSchemaNodes();
184 assertEquals(1, nodes_u.size());
185 UnknownSchemaNode node_u = nodes_u.get(0);
186 assertTrue(node_u.isAddedByUses());
188 List<UnknownSchemaNode> nodes_g = grouping.getUnknownSchemaNodes();
189 assertEquals(1, nodes_g.size());
190 UnknownSchemaNode node_g = nodes_g.get(0);
191 assertFalse(node_g.isAddedByUses());
192 assertFalse(node_u.equals(node_g));
196 public void testUsesRefineFromContext() throws Exception {
197 SchemaContext context = null;
198 try (InputStream stream1 = new FileInputStream(getClass().getResource("/model/custom.yang").getPath());
199 InputStream stream2 = new FileInputStream(getClass().getResource("/model/types.yang").getPath());
200 InputStream stream3 = new FileInputStream(getClass().getResource("/model/nodes.yang").getPath())) {
201 context = parser.resolveSchemaContext(TestUtils.loadModules(Lists.newArrayList(stream1, stream2, stream3)));
203 Module module = null;
204 try (InputStream stream = new FileInputStream(getClass().getResource("/context-test/test2.yang").getPath())) {
205 module = TestUtils.loadModuleWithContext(stream, context);
207 assertNotNull(module);
209 ContainerSchemaNode peer = (ContainerSchemaNode) module.getDataChildByName("peer");
210 ContainerSchemaNode destination = (ContainerSchemaNode) peer.getDataChildByName("destination");
211 Set<UsesNode> usesNodes = destination.getUses();
212 assertEquals(1, usesNodes.size());
213 UsesNode usesNode = usesNodes.iterator().next();
215 // test grouping path
216 List<QName> path = new ArrayList<QName>();
217 QName qname = new QName(URI.create("urn:custom.nodes.test"), simpleDateFormat.parse("2013-02-27"), "c",
220 SchemaPath expectedPath = new SchemaPath(path, true);
221 assertEquals(expectedPath, usesNode.getGroupingPath());
224 Map<SchemaPath, SchemaNode> refines = usesNode.getRefines();
225 assertEquals(3, refines.size());
227 LeafSchemaNode refineLeaf = null;
228 ContainerSchemaNode refineContainer = null;
229 ListSchemaNode refineList = null;
230 for (Map.Entry<SchemaPath, SchemaNode> entry : refines.entrySet()) {
231 SchemaNode value = entry.getValue();
232 if (value instanceof LeafSchemaNode) {
233 refineLeaf = (LeafSchemaNode) value;
234 } else if (value instanceof ContainerSchemaNode) {
235 refineContainer = (ContainerSchemaNode) value;
236 } else if (value instanceof ListSchemaNode) {
237 refineList = (ListSchemaNode) value;
242 assertNotNull(refineLeaf);
243 assertEquals("address", refineLeaf.getQName().getLocalName());
244 assertEquals("description of address defined by refine", refineLeaf.getDescription());
245 assertEquals("address reference added by refine", refineLeaf.getReference());
246 assertFalse(refineLeaf.isConfiguration());
247 assertTrue(refineLeaf.getConstraints().isMandatory());
248 Set<MustDefinition> leafMustConstraints = refineLeaf.getConstraints().getMustConstraints();
249 assertEquals(1, leafMustConstraints.size());
250 MustDefinition leafMust = leafMustConstraints.iterator().next();
251 assertEquals("\"ifType != 'ethernet' or (ifType = 'ethernet' and ifMTU = 1500)\"", leafMust.toString());
254 assertNotNull(refineContainer);
255 Set<MustDefinition> mustConstraints = refineContainer.getConstraints().getMustConstraints();
256 assertTrue(mustConstraints.isEmpty());
257 assertEquals("description of port defined by refine", refineContainer.getDescription());
258 assertEquals("port reference added by refine", refineContainer.getReference());
259 assertFalse(refineContainer.isConfiguration());
260 assertTrue(refineContainer.isPresenceContainer());
263 assertNotNull(refineList);
264 assertEquals("description of addresses defined by refine", refineList.getDescription());
265 assertEquals("addresses reference added by refine", refineList.getReference());
266 assertFalse(refineList.isConfiguration());
267 assertEquals(2, (int) refineList.getConstraints().getMinElements());
268 assertEquals(12, (int) refineList.getConstraints().getMaxElements());
272 public void testIdentity() throws Exception {
273 SchemaContext context = null;
274 try (InputStream stream = new FileInputStream(getClass().getResource("/types/custom-types-test@2012-4-4.yang")
276 context = parser.resolveSchemaContext(TestUtils.loadModules(Lists.newArrayList(stream)));
278 Module module = null;
279 try (InputStream stream = new FileInputStream(getClass().getResource("/context-test/test3.yang").getPath())) {
280 module = TestUtils.loadModuleWithContext(stream, context);
282 assertNotNull(module);
284 Set<IdentitySchemaNode> identities = module.getIdentities();
285 assertEquals(1, identities.size());
287 IdentitySchemaNode identity = identities.iterator().next();
288 QName idQName = identity.getQName();
289 assertEquals(URI.create("urn:simple.demo.test3"), idQName.getNamespace());
290 assertEquals(simpleDateFormat.parse("2013-06-18"), idQName.getRevision());
291 assertEquals("t3", idQName.getPrefix());
292 assertEquals("pt", idQName.getLocalName());
294 IdentitySchemaNode baseIdentity = identity.getBaseIdentity();
295 QName idBaseQName = baseIdentity.getQName();
296 assertEquals(URI.create("urn:custom.types.demo"), idBaseQName.getNamespace());
297 assertEquals(simpleDateFormat.parse("2012-04-16"), idBaseQName.getRevision());
298 assertEquals("iit", idBaseQName.getPrefix());
299 assertEquals("service-type", idBaseQName.getLocalName());
303 public void testUnknownNodes() throws Exception {
304 SchemaContext context = null;
305 try (InputStream stream = new FileInputStream(getClass().getResource("/types/custom-types-test@2012-4-4.yang")
307 context = parser.resolveSchemaContext(TestUtils.loadModules(Lists.newArrayList(stream)));
310 Module module = null;
311 try (InputStream stream = new FileInputStream(getClass().getResource("/context-test/test3.yang").getPath())) {
312 module = TestUtils.loadModuleWithContext(stream, context);
315 ContainerSchemaNode network = (ContainerSchemaNode) module.getDataChildByName("network");
316 List<UnknownSchemaNode> unknownNodes = network.getUnknownSchemaNodes();
317 assertEquals(1, unknownNodes.size());
319 UnknownSchemaNode un = unknownNodes.get(0);
320 QName unType = un.getNodeType();
321 assertEquals(URI.create("urn:custom.types.demo"), unType.getNamespace());
322 assertEquals(simpleDateFormat.parse("2012-04-16"), unType.getRevision());
323 assertEquals("custom", unType.getPrefix());
324 assertEquals("mountpoint", unType.getLocalName());
325 assertEquals("point", un.getNodeParameter());
329 public void testAugment() throws Exception {
331 SchemaContext context = null;
332 String resource = "/context-augment-test/test4.yang";
334 try (InputStream stream = new FileInputStream(getClass().getResource(resource).getPath())) {
335 context = parser.resolveSchemaContext(TestUtils.loadModules(Lists.newArrayList(stream)));
338 Set<Module> contextModules = context.getModules();
339 Module t3 = TestUtils.findModule(contextModules, "test4");
340 ContainerSchemaNode interfaces = (ContainerSchemaNode) t3.getDataChildByName("interfaces");
341 ListSchemaNode ifEntry = (ListSchemaNode) interfaces.getDataChildByName("ifEntry");
343 // load another modules and parse them against already existing context
344 Set<Module> modules = null;
345 try (InputStream stream1 = new FileInputStream(getClass().getResource("/context-augment-test/test1.yang")
347 InputStream stream2 = new FileInputStream(getClass().getResource("/context-augment-test/test2.yang")
349 InputStream stream3 = new FileInputStream(getClass().getResource("/context-augment-test/test3.yang")
351 List<InputStream> input = Lists.newArrayList(stream1, stream2, stream3);
352 modules = TestUtils.loadModulesWithContext(input, context);
354 assertNotNull(modules);
356 // test augmentation process
357 ContainerSchemaNode augmentHolder = (ContainerSchemaNode) ifEntry.getDataChildByName("augment-holder");
358 assertNotNull(augmentHolder);
359 DataSchemaNode ds0 = augmentHolder.getDataChildByName("ds0ChannelNumber");
361 DataSchemaNode interfaceId = augmentHolder.getDataChildByName("interface-id");
362 assertNotNull(interfaceId);
363 DataSchemaNode higherLayerIf = augmentHolder.getDataChildByName("higher-layer-if");
364 assertNotNull(higherLayerIf);
365 ContainerSchemaNode schemas = (ContainerSchemaNode) augmentHolder.getDataChildByName("schemas");
366 assertNotNull(schemas);
367 assertNotNull(schemas.getDataChildByName("id"));
369 // test augment target after augmentation: check if it is same instance
370 ListSchemaNode ifEntryAfterAugment = (ListSchemaNode) interfaces.getDataChildByName("ifEntry");
371 assertTrue(ifEntry == ifEntryAfterAugment);
375 public void testDeviation() throws Exception {
377 SchemaContext context = null;
378 String resource = "/model/types.yang";
380 try (InputStream stream = new FileInputStream(getClass().getResource(resource).getPath())) {
381 context = parser.resolveSchemaContext(TestUtils.loadModules(Lists.newArrayList(stream)));
384 // load another modules and parse them against already existing context
385 Set<Module> modules = null;
386 try (InputStream stream = new FileInputStream(getClass().getResource("/context-test/deviation-test.yang")
388 List<InputStream> input = Lists.newArrayList(stream);
389 modules = TestUtils.loadModulesWithContext(input, context);
391 assertNotNull(modules);
394 Module testModule = TestUtils.findModule(modules, "deviation-test");
395 Set<Deviation> deviations = testModule.getDeviations();
396 assertEquals(1, deviations.size());
397 Deviation dev = deviations.iterator().next();
399 assertEquals("system/user ref", dev.getReference());
401 URI expectedNS = URI.create("urn:simple.types.test");
402 DateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
403 Date expectedRev = simpleDateFormat.parse("2013-07-03");
404 List<QName> path = new ArrayList<QName>();
405 path.add(new QName(expectedNS, expectedRev, "t", "interfaces"));
406 path.add(new QName(expectedNS, expectedRev, "t", "ifEntry"));
407 SchemaPath expectedPath = new SchemaPath(path, true);
409 assertEquals(expectedPath, dev.getTargetPath());
410 assertEquals(Deviate.ADD, dev.getDeviate());