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.controller.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.Test;
24 import org.opendaylight.controller.yang.common.QName;
25 import org.opendaylight.controller.yang.model.api.AnyXmlSchemaNode;
26 import org.opendaylight.controller.yang.model.api.ChoiceNode;
27 import org.opendaylight.controller.yang.model.api.ContainerSchemaNode;
28 import org.opendaylight.controller.yang.model.api.DataSchemaNode;
29 import org.opendaylight.controller.yang.model.api.Deviation;
30 import org.opendaylight.controller.yang.model.api.Deviation.Deviate;
31 import org.opendaylight.controller.yang.model.api.GroupingDefinition;
32 import org.opendaylight.controller.yang.model.api.IdentitySchemaNode;
33 import org.opendaylight.controller.yang.model.api.LeafSchemaNode;
34 import org.opendaylight.controller.yang.model.api.ListSchemaNode;
35 import org.opendaylight.controller.yang.model.api.Module;
36 import org.opendaylight.controller.yang.model.api.MustDefinition;
37 import org.opendaylight.controller.yang.model.api.SchemaContext;
38 import org.opendaylight.controller.yang.model.api.SchemaNode;
39 import org.opendaylight.controller.yang.model.api.SchemaPath;
40 import org.opendaylight.controller.yang.model.api.TypeDefinition;
41 import org.opendaylight.controller.yang.model.api.UnknownSchemaNode;
42 import org.opendaylight.controller.yang.model.api.UsesNode;
43 import org.opendaylight.controller.yang.model.api.type.RangeConstraint;
44 import org.opendaylight.controller.yang.model.util.ExtendedType;
46 import com.google.common.collect.Lists;
48 public class YangParserWithContextTest {
49 private final DateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
50 private final YangParserImpl parser = new YangParserImpl();
53 public void testTypeFromContext() throws Exception {
54 SchemaContext context = null;
55 String resource = "/types/ietf-inet-types@2010-09-24.yang";
56 InputStream stream = new FileInputStream(getClass().getResource(resource).getPath());
57 context = parser.resolveSchemaContext(TestUtils.loadModules(Lists.newArrayList(stream)));
61 resource = "/context-test/test1.yang";
62 InputStream stream2 = new FileInputStream(getClass().getResource(resource).getPath());
63 module = TestUtils.loadModuleWithContext(stream2, context);
65 assertNotNull(module);
67 LeafSchemaNode leaf = (LeafSchemaNode) module.getDataChildByName("id");
69 ExtendedType leafType = (ExtendedType) leaf.getType();
70 QName qname = leafType.getQName();
71 assertEquals(URI.create("urn:simple.demo.test1"), qname.getNamespace());
72 assertEquals(simpleDateFormat.parse("2013-06-18"), qname.getRevision());
73 assertEquals("t1", qname.getPrefix());
74 assertEquals("port-number", qname.getLocalName());
76 ExtendedType leafBaseType = (ExtendedType) leafType.getBaseType();
77 qname = leafBaseType.getQName();
78 assertEquals(URI.create("urn:ietf:params:xml:ns:yang:ietf-inet-types"), qname.getNamespace());
79 assertEquals(simpleDateFormat.parse("2010-09-24"), qname.getRevision());
80 assertEquals("inet", qname.getPrefix());
81 assertEquals("port-number", qname.getLocalName());
83 ExtendedType dscpExt = (ExtendedType) TestUtils.findTypedef(module.getTypeDefinitions(), "dscp-ext");
84 List<RangeConstraint> ranges = dscpExt.getRanges();
85 assertEquals(1, ranges.size());
86 RangeConstraint range = ranges.get(0);
87 assertEquals(0L, range.getMin());
88 assertEquals(63L, range.getMax());
92 public void testUsesFromContext() throws Exception {
93 SchemaContext context = null;
94 try (InputStream stream1 = new FileInputStream(getClass().getResource("/model/custom.yang").getPath());
95 InputStream stream2 = new FileInputStream(getClass().getResource("/model/types.yang").getPath());
96 InputStream stream3 = new FileInputStream(getClass().getResource("/model/nodes.yang").getPath())) {
97 context = parser.resolveSchemaContext(TestUtils.loadModules(Lists.newArrayList(stream1, stream2, stream3)));
99 Module testModule = null;
100 try (InputStream stream = new FileInputStream(getClass().getResource("/context-test/test2.yang").getPath())) {
101 testModule = TestUtils.loadModuleWithContext(stream, context);
103 assertNotNull(testModule);
105 // suffix _u = added by uses
106 // suffix _g = defined in grouping from context
109 Module contextModule = context.findModuleByNamespace(URI.create("urn:custom.nodes.test"));
110 assertNotNull(contextModule);
111 Set<GroupingDefinition> groupings = contextModule.getGroupings();
112 assertEquals(1, groupings.size());
113 GroupingDefinition grouping = groupings.iterator().next();
115 // get node containing uses
116 ContainerSchemaNode peer = (ContainerSchemaNode) testModule.getDataChildByName("peer");
117 ContainerSchemaNode destination = (ContainerSchemaNode) peer.getDataChildByName("destination");
120 Set<UsesNode> uses = destination.getUses();
121 assertEquals(1, uses.size());
123 // check uses process
124 AnyXmlSchemaNode data_u = (AnyXmlSchemaNode) destination.getDataChildByName("data");
125 assertNotNull(data_u);
126 assertTrue(data_u.isAddedByUses());
128 AnyXmlSchemaNode data_g = (AnyXmlSchemaNode) grouping.getDataChildByName("data");
129 assertNotNull(data_g);
130 assertFalse(data_g.isAddedByUses());
131 assertFalse(data_u.equals(data_g));
133 ChoiceNode how_u = (ChoiceNode) destination.getDataChildByName("how");
134 assertNotNull(how_u);
135 assertTrue(how_u.isAddedByUses());
137 ChoiceNode how_g = (ChoiceNode) grouping.getDataChildByName("how");
138 assertNotNull(how_g);
139 assertFalse(how_g.isAddedByUses());
140 assertFalse(how_u.equals(how_g));
142 LeafSchemaNode address_u = (LeafSchemaNode) destination.getDataChildByName("address");
143 assertNotNull(address_u);
144 assertTrue(address_u.isAddedByUses());
146 LeafSchemaNode address_g = (LeafSchemaNode) grouping.getDataChildByName("address");
147 assertNotNull(address_g);
148 assertFalse(address_g.isAddedByUses());
149 assertFalse(address_u.equals(address_g));
151 ContainerSchemaNode port_u = (ContainerSchemaNode) destination.getDataChildByName("port");
152 assertNotNull(port_u);
153 assertTrue(port_u.isAddedByUses());
155 ContainerSchemaNode port_g = (ContainerSchemaNode) grouping.getDataChildByName("port");
156 assertNotNull(port_g);
157 assertFalse(port_g.isAddedByUses());
158 assertFalse(port_u.equals(port_g));
160 ListSchemaNode addresses_u = (ListSchemaNode) destination.getDataChildByName("addresses");
161 assertNotNull(addresses_u);
162 assertTrue(addresses_u.isAddedByUses());
164 ListSchemaNode addresses_g = (ListSchemaNode) grouping.getDataChildByName("addresses");
165 assertNotNull(addresses_g);
166 assertFalse(addresses_g.isAddedByUses());
167 assertFalse(addresses_u.equals(addresses_g));
169 // grouping defined by 'uses'
170 Set<GroupingDefinition> groupings_u = destination.getGroupings();
171 assertEquals(1, groupings_u.size());
172 GroupingDefinition grouping_u = groupings_u.iterator().next();
173 assertTrue(grouping_u.isAddedByUses());
175 // grouping defined in 'grouping' node
176 Set<GroupingDefinition> groupings_g = grouping.getGroupings();
177 assertEquals(1, groupings_g.size());
178 GroupingDefinition grouping_g = groupings_g.iterator().next();
179 assertFalse(grouping_g.isAddedByUses());
180 assertFalse(grouping_u.equals(grouping_g));
182 List<UnknownSchemaNode> nodes_u = destination.getUnknownSchemaNodes();
183 assertEquals(1, nodes_u.size());
184 UnknownSchemaNode node_u = nodes_u.get(0);
185 assertTrue(node_u.isAddedByUses());
187 List<UnknownSchemaNode> nodes_g = grouping.getUnknownSchemaNodes();
188 assertEquals(1, nodes_g.size());
189 UnknownSchemaNode node_g = nodes_g.get(0);
190 assertFalse(node_g.isAddedByUses());
191 assertFalse(node_u.equals(node_g));
195 public void testUsesRefineFromContext() throws Exception {
196 SchemaContext context = null;
197 try (InputStream stream1 = new FileInputStream(getClass().getResource("/model/custom.yang").getPath());
198 InputStream stream2 = new FileInputStream(getClass().getResource("/model/types.yang").getPath());
199 InputStream stream3 = new FileInputStream(getClass().getResource("/model/nodes.yang").getPath())) {
200 context = parser.resolveSchemaContext(TestUtils.loadModules(Lists.newArrayList(stream1, stream2, stream3)));
202 Module module = null;
203 try (InputStream stream = new FileInputStream(getClass().getResource("/context-test/test2.yang").getPath())) {
204 module = TestUtils.loadModuleWithContext(stream, context);
206 assertNotNull(module);
208 ContainerSchemaNode peer = (ContainerSchemaNode) module.getDataChildByName("peer");
209 ContainerSchemaNode destination = (ContainerSchemaNode) peer.getDataChildByName("destination");
210 Set<UsesNode> usesNodes = destination.getUses();
211 assertEquals(1, usesNodes.size());
212 UsesNode usesNode = usesNodes.iterator().next();
214 // test grouping path
215 List<QName> path = new ArrayList<QName>();
216 QName qname = new QName(URI.create("urn:custom.nodes.test"), simpleDateFormat.parse("2013-02-27"), "c",
219 SchemaPath expectedPath = new SchemaPath(path, true);
220 assertEquals(expectedPath, usesNode.getGroupingPath());
223 Map<SchemaPath, SchemaNode> refines = usesNode.getRefines();
224 assertEquals(5, refines.size());
226 LeafSchemaNode refineLeaf = null;
227 ContainerSchemaNode refineContainer = null;
228 ListSchemaNode refineList = null;
229 GroupingDefinition refineGrouping = null;
230 TypeDefinition<?> typedef = null;
231 for (Map.Entry<SchemaPath, SchemaNode> entry : refines.entrySet()) {
232 SchemaNode value = entry.getValue();
233 if (value instanceof LeafSchemaNode) {
234 refineLeaf = (LeafSchemaNode) value;
235 } else if (value instanceof ContainerSchemaNode) {
236 refineContainer = (ContainerSchemaNode) value;
237 } else if (value instanceof ListSchemaNode) {
238 refineList = (ListSchemaNode) value;
239 } else if (value instanceof GroupingDefinition) {
240 refineGrouping = (GroupingDefinition) value;
241 } else if (value instanceof TypeDefinition<?>) {
242 typedef = (TypeDefinition<?>) value;
247 assertNotNull(refineLeaf);
248 assertEquals("address", refineLeaf.getQName().getLocalName());
249 assertEquals("description of address defined by refine", refineLeaf.getDescription());
250 assertEquals("address reference added by refine", refineLeaf.getReference());
251 assertFalse(refineLeaf.isConfiguration());
252 assertTrue(refineLeaf.getConstraints().isMandatory());
253 Set<MustDefinition> leafMustConstraints = refineLeaf.getConstraints().getMustConstraints();
254 assertEquals(1, leafMustConstraints.size());
255 MustDefinition leafMust = leafMustConstraints.iterator().next();
256 assertEquals("\"ifType != 'ethernet' or (ifType = 'ethernet' and ifMTU = 1500)\"", leafMust.toString());
259 assertNotNull(refineContainer);
260 Set<MustDefinition> mustConstraints = refineContainer.getConstraints().getMustConstraints();
261 assertTrue(mustConstraints.isEmpty());
262 assertEquals("description of port defined by refine", refineContainer.getDescription());
263 assertEquals("port reference added by refine", refineContainer.getReference());
264 assertFalse(refineContainer.isConfiguration());
265 assertTrue(refineContainer.isPresenceContainer());
268 assertNotNull(refineList);
269 assertEquals("description of addresses defined by refine", refineList.getDescription());
270 assertEquals("addresses reference added by refine", refineList.getReference());
271 assertFalse(refineList.isConfiguration());
272 assertEquals(2, (int) refineList.getConstraints().getMinElements());
273 assertEquals(12, (int) refineList.getConstraints().getMaxElements());
275 // grouping target-inner
276 assertNotNull(refineGrouping);
277 Set<DataSchemaNode> refineGroupingChildren = refineGrouping.getChildNodes();
278 assertEquals(1, refineGroupingChildren.size());
279 LeafSchemaNode refineGroupingLeaf = (LeafSchemaNode) refineGroupingChildren.iterator().next();
280 assertEquals("inner-grouping-id", refineGroupingLeaf.getQName().getLocalName());
281 assertEquals("new target-inner grouping description", refineGrouping.getDescription());
283 // typedef group-type
284 assertNotNull(typedef);
285 assertEquals("new group-type description", typedef.getDescription());
286 assertEquals("new group-type reference", typedef.getReference());
287 assertTrue(typedef.getBaseType() instanceof ExtendedType);
291 public void testIdentity() throws Exception {
292 SchemaContext context = null;
293 try (InputStream stream = new FileInputStream(getClass().getResource("/types/custom-types-test@2012-4-4.yang")
295 context = parser.resolveSchemaContext(TestUtils.loadModules(Lists.newArrayList(stream)));
297 Module module = null;
298 try (InputStream stream = new FileInputStream(getClass().getResource("/context-test/test3.yang").getPath())) {
299 module = TestUtils.loadModuleWithContext(stream, context);
301 assertNotNull(module);
303 Set<IdentitySchemaNode> identities = module.getIdentities();
304 assertEquals(1, identities.size());
306 IdentitySchemaNode identity = identities.iterator().next();
307 QName idQName = identity.getQName();
308 assertEquals(URI.create("urn:simple.demo.test3"), idQName.getNamespace());
309 assertEquals(simpleDateFormat.parse("2013-06-18"), idQName.getRevision());
310 assertEquals("t3", idQName.getPrefix());
311 assertEquals("pt", idQName.getLocalName());
313 IdentitySchemaNode baseIdentity = identity.getBaseIdentity();
314 QName idBaseQName = baseIdentity.getQName();
315 assertEquals(URI.create("urn:custom.types.demo"), idBaseQName.getNamespace());
316 assertEquals(simpleDateFormat.parse("2012-04-16"), idBaseQName.getRevision());
317 assertEquals("iit", idBaseQName.getPrefix());
318 assertEquals("service-type", idBaseQName.getLocalName());
322 public void testUnknownNodes() throws Exception {
323 SchemaContext context = null;
324 try (InputStream stream = new FileInputStream(getClass().getResource("/types/custom-types-test@2012-4-4.yang")
326 context = parser.resolveSchemaContext(TestUtils.loadModules(Lists.newArrayList(stream)));
329 Module module = null;
330 try (InputStream stream = new FileInputStream(getClass().getResource("/context-test/test3.yang").getPath())) {
331 module = TestUtils.loadModuleWithContext(stream, context);
334 ContainerSchemaNode network = (ContainerSchemaNode) module.getDataChildByName("network");
335 List<UnknownSchemaNode> unknownNodes = network.getUnknownSchemaNodes();
336 assertEquals(1, unknownNodes.size());
338 UnknownSchemaNode un = unknownNodes.get(0);
339 QName unType = un.getNodeType();
340 assertEquals(URI.create("urn:custom.types.demo"), unType.getNamespace());
341 assertEquals(simpleDateFormat.parse("2012-04-16"), unType.getRevision());
342 assertEquals("custom", unType.getPrefix());
343 assertEquals("mountpoint", unType.getLocalName());
344 assertEquals("point", un.getNodeParameter());
348 public void testAugment() throws Exception {
350 SchemaContext context = null;
351 String resource = "/context-augment-test/test4.yang";
353 try (InputStream stream = new FileInputStream(getClass().getResource(resource).getPath())) {
354 context = parser.resolveSchemaContext(TestUtils.loadModules(Lists.newArrayList(stream)));
357 Set<Module> contextModules = context.getModules();
358 Module t3 = TestUtils.findModule(contextModules, "test4");
359 ContainerSchemaNode interfaces = (ContainerSchemaNode) t3.getDataChildByName("interfaces");
360 ListSchemaNode ifEntry = (ListSchemaNode) interfaces.getDataChildByName("ifEntry");
362 // load another modules and parse them against already existing context
363 Set<Module> modules = null;
364 try (InputStream stream1 = new FileInputStream(getClass().getResource("/context-augment-test/test1.yang")
366 InputStream stream2 = new FileInputStream(getClass().getResource("/context-augment-test/test2.yang")
368 InputStream stream3 = new FileInputStream(getClass().getResource("/context-augment-test/test3.yang")
370 List<InputStream> input = Lists.newArrayList(stream1, stream2, stream3);
371 modules = TestUtils.loadModulesWithContext(input, context);
373 assertNotNull(modules);
375 // test augmentation process
376 ContainerSchemaNode augmentHolder = (ContainerSchemaNode) ifEntry.getDataChildByName("augment-holder");
377 assertNotNull(augmentHolder);
378 DataSchemaNode ds0 = augmentHolder.getDataChildByName("ds0ChannelNumber");
380 DataSchemaNode interfaceId = augmentHolder.getDataChildByName("interface-id");
381 assertNotNull(interfaceId);
382 DataSchemaNode higherLayerIf = augmentHolder.getDataChildByName("higher-layer-if");
383 assertNotNull(higherLayerIf);
384 ContainerSchemaNode schemas = (ContainerSchemaNode) augmentHolder.getDataChildByName("schemas");
385 assertNotNull(schemas);
386 assertNotNull(schemas.getDataChildByName("id"));
388 // test augment target after augmentation: check if it is same instance
389 ListSchemaNode ifEntryAfterAugment = (ListSchemaNode) interfaces.getDataChildByName("ifEntry");
390 assertTrue(ifEntry == ifEntryAfterAugment);
394 public void testDeviation() throws Exception {
396 SchemaContext context = null;
397 String resource = "/model/types.yang";
399 try (InputStream stream = new FileInputStream(getClass().getResource(resource).getPath())) {
400 context = parser.resolveSchemaContext(TestUtils.loadModules(Lists.newArrayList(stream)));
403 // load another modules and parse them against already existing context
404 Set<Module> modules = null;
405 try (InputStream stream = new FileInputStream(getClass().getResource("/context-test/deviation-test.yang")
407 List<InputStream> input = Lists.newArrayList(stream);
408 modules = TestUtils.loadModulesWithContext(input, context);
410 assertNotNull(modules);
413 Module testModule = TestUtils.findModule(modules, "deviation-test");
414 Set<Deviation> deviations = testModule.getDeviations();
415 assertEquals(1, deviations.size());
416 Deviation dev = deviations.iterator().next();
418 assertEquals("system/user ref", dev.getReference());
420 URI expectedNS = URI.create("urn:simple.types.test");
421 DateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
422 Date expectedRev = simpleDateFormat.parse("2013-07-03");
423 List<QName> path = new ArrayList<QName>();
424 path.add(new QName(expectedNS, expectedRev, "t", "interfaces"));
425 path.add(new QName(expectedNS, expectedRev, "t", "ifEntry"));
426 SchemaPath expectedPath = new SchemaPath(path, true);
428 assertEquals(expectedPath, dev.getTargetPath());
429 assertEquals(Deviate.ADD, dev.getDeviate());