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;
14 import com.google.common.collect.Lists;
15 import com.google.common.io.ByteSource;
16 import com.google.common.io.Resources;
18 import java.io.FileInputStream;
19 import java.io.IOException;
20 import java.io.InputStream;
21 import java.math.BigInteger;
23 import java.text.DateFormat;
24 import java.text.SimpleDateFormat;
25 import java.util.ArrayList;
26 import java.util.Arrays;
27 import java.util.Collections;
28 import java.util.Date;
29 import java.util.List;
32 import org.junit.Test;
33 import org.opendaylight.yangtools.yang.common.QName;
34 import org.opendaylight.yangtools.yang.model.api.AnyXmlSchemaNode;
35 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
36 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
37 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
38 import org.opendaylight.yangtools.yang.model.api.Deviation;
39 import org.opendaylight.yangtools.yang.model.api.Deviation.Deviate;
40 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
41 import org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode;
42 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
43 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
44 import org.opendaylight.yangtools.yang.model.api.Module;
45 import org.opendaylight.yangtools.yang.model.api.MustDefinition;
46 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
47 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
48 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
49 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
50 import org.opendaylight.yangtools.yang.model.api.UsesNode;
51 import org.opendaylight.yangtools.yang.model.api.type.RangeConstraint;
52 import org.opendaylight.yangtools.yang.model.parser.api.YangContextParser;
53 import org.opendaylight.yangtools.yang.model.parser.api.YangSyntaxErrorException;
54 import org.opendaylight.yangtools.yang.model.util.ExtendedType;
56 public class YangParserWithContextTest {
57 private final DateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
58 private final YangParserImpl parser = new YangParserImpl();
60 private final SchemaContext createContext(final String... resources) throws IOException, YangSyntaxErrorException {
61 final List<ByteSource> srcs = new ArrayList<>(resources.length);
62 for (String resource : resources) {
63 srcs.add(Resources.asByteSource(getClass().getResource(resource)));
66 return parser.parseSources(srcs);
70 public void testTypeFromContext() throws Exception {
71 SchemaContext context = createContext("/ietf/ietf-inet-types@2010-09-24.yang");
73 InputStream stream2 = new FileInputStream(new File(getClass().getResource("/context-test/test1.yang").toURI()));
74 Module module = TestUtils.loadModuleWithContext("test1", stream2, context);
76 assertNotNull(module);
78 LeafSchemaNode leaf = (LeafSchemaNode) module.getDataChildByName("id");
80 ExtendedType leafType = (ExtendedType) leaf.getType();
81 QName qname = leafType.getQName();
82 assertEquals(URI.create("urn:simple.demo.test1"), qname.getNamespace());
83 assertEquals(simpleDateFormat.parse("2013-06-18"), qname.getRevision());
84 assertEquals("port-number", qname.getLocalName());
86 ExtendedType leafBaseType = (ExtendedType) leafType.getBaseType();
87 qname = leafBaseType.getQName();
88 assertEquals(URI.create("urn:ietf:params:xml:ns:yang:ietf-inet-types"), qname.getNamespace());
89 assertEquals(simpleDateFormat.parse("2010-09-24"), qname.getRevision());
90 assertEquals("port-number", qname.getLocalName());
92 ExtendedType dscpExt = (ExtendedType) TestUtils.findTypedef(module.getTypeDefinitions(), "dscp-ext");
93 List<RangeConstraint> ranges = dscpExt.getRangeConstraints();
94 assertEquals(1, ranges.size());
95 RangeConstraint range = ranges.get(0);
96 assertEquals(BigInteger.ZERO, range.getMin());
97 assertEquals(BigInteger.valueOf(63), range.getMax());
101 public void testUsesFromContext() throws Exception {
102 SchemaContext context = createContext("/model/baz.yang", "/model/bar.yang", "/model/foo.yang", "/model/subfoo.yang");
104 try (InputStream stream = new FileInputStream(new File(getClass().getResource("/context-test/test2.yang")
106 testModule = TestUtils.loadModuleWithContext("test2", stream, context);
108 assertNotNull(testModule);
110 // suffix _u = added by uses
111 // suffix _g = defined in grouping from context
114 Module contextModule = context.findModuleByNamespace(URI.create("urn:opendaylight.baz")).iterator().next();
115 assertNotNull(contextModule);
116 Set<GroupingDefinition> groupings = contextModule.getGroupings();
117 assertEquals(1, groupings.size());
118 GroupingDefinition grouping = groupings.iterator().next();
120 // get node containing uses
121 ContainerSchemaNode peer = (ContainerSchemaNode) testModule.getDataChildByName("peer");
122 ContainerSchemaNode destination = (ContainerSchemaNode) peer.getDataChildByName("destination");
125 Set<UsesNode> uses = destination.getUses();
126 assertEquals(1, uses.size());
128 // check uses process
129 AnyXmlSchemaNode data_u = (AnyXmlSchemaNode) destination.getDataChildByName("data");
130 assertNotNull(data_u);
131 assertTrue(data_u.isAddedByUses());
133 AnyXmlSchemaNode data_g = (AnyXmlSchemaNode) grouping.getDataChildByName("data");
134 assertNotNull(data_g);
135 assertFalse(data_g.isAddedByUses());
136 assertFalse(data_u.equals(data_g));
138 ChoiceSchemaNode how_u = (ChoiceSchemaNode) destination.getDataChildByName("how");
139 assertNotNull(how_u);
140 assertTrue(how_u.isAddedByUses());
142 ChoiceSchemaNode how_g = (ChoiceSchemaNode) grouping.getDataChildByName("how");
143 assertNotNull(how_g);
144 assertFalse(how_g.isAddedByUses());
145 assertFalse(how_u.equals(how_g));
147 LeafSchemaNode address_u = (LeafSchemaNode) destination.getDataChildByName("address");
148 assertNotNull(address_u);
149 assertTrue(address_u.isAddedByUses());
151 LeafSchemaNode address_g = (LeafSchemaNode) grouping.getDataChildByName("address");
152 assertNotNull(address_g);
153 assertFalse(address_g.isAddedByUses());
154 assertFalse(address_u.equals(address_g));
156 ContainerSchemaNode port_u = (ContainerSchemaNode) destination.getDataChildByName("port");
157 assertNotNull(port_u);
158 assertTrue(port_u.isAddedByUses());
160 ContainerSchemaNode port_g = (ContainerSchemaNode) grouping.getDataChildByName("port");
161 assertNotNull(port_g);
162 assertFalse(port_g.isAddedByUses());
163 assertFalse(port_u.equals(port_g));
165 ListSchemaNode addresses_u = (ListSchemaNode) destination.getDataChildByName("addresses");
166 assertNotNull(addresses_u);
167 assertTrue(addresses_u.isAddedByUses());
169 ListSchemaNode addresses_g = (ListSchemaNode) grouping.getDataChildByName("addresses");
170 assertNotNull(addresses_g);
171 assertFalse(addresses_g.isAddedByUses());
172 assertFalse(addresses_u.equals(addresses_g));
174 // grouping defined by 'uses'
175 Set<GroupingDefinition> groupings_u = destination.getGroupings();
176 assertEquals(1, groupings_u.size());
177 GroupingDefinition grouping_u = groupings_u.iterator().next();
178 assertTrue(grouping_u.isAddedByUses());
180 // grouping defined in 'grouping' node
181 Set<GroupingDefinition> groupings_g = grouping.getGroupings();
182 assertEquals(1, groupings_g.size());
183 GroupingDefinition grouping_g = groupings_g.iterator().next();
184 assertFalse(grouping_g.isAddedByUses());
185 assertFalse(grouping_u.equals(grouping_g));
187 List<UnknownSchemaNode> nodes_u = destination.getUnknownSchemaNodes();
188 assertEquals(1, nodes_u.size());
189 UnknownSchemaNode node_u = nodes_u.get(0);
190 assertTrue(node_u.isAddedByUses());
192 List<UnknownSchemaNode> nodes_g = grouping.getUnknownSchemaNodes();
193 assertEquals(1, nodes_g.size());
194 UnknownSchemaNode node_g = nodes_g.get(0);
195 assertFalse(node_g.isAddedByUses());
196 assertFalse(node_u.equals(node_g));
200 public void testUsesRefineFromContext() throws Exception {
201 SchemaContext context = createContext("/model/baz.yang", "/model/bar.yang", "/model/foo.yang", "/model/subfoo.yang");
204 try (InputStream stream = new FileInputStream(new File(getClass().getResource("/context-test/test2.yang")
206 module = TestUtils.loadModuleWithContext("test2", stream, context);
208 assertNotNull(module);
210 ContainerSchemaNode peer = (ContainerSchemaNode) module.getDataChildByName("peer");
211 ContainerSchemaNode destination = (ContainerSchemaNode) peer.getDataChildByName("destination");
212 Set<UsesNode> usesNodes = destination.getUses();
213 assertEquals(1, usesNodes.size());
214 UsesNode usesNode = usesNodes.iterator().next();
216 // test grouping path
217 List<QName> path = new ArrayList<>();
218 QName qname = QName.create(URI.create("urn:opendaylight.baz"), simpleDateFormat.parse("2013-02-27"), "target");
220 SchemaPath expectedPath = SchemaPath.create(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;
274 File yangFile = new File(getClass().getResource("/types/custom-types-test@2012-4-4.yang").toURI());
275 File dependenciesDir = new File(getClass().getResource("/ietf").toURI());
276 YangContextParser parser = new YangParserImpl();
277 context = parser.parseFile(yangFile, dependenciesDir);
280 try (InputStream stream = new FileInputStream(new File(getClass().getResource("/context-test/test3.yang")
282 module = TestUtils.loadModuleWithContext("test3", stream, context);
284 assertNotNull(module);
286 Set<IdentitySchemaNode> identities = module.getIdentities();
287 assertEquals(1, identities.size());
289 IdentitySchemaNode identity = identities.iterator().next();
290 QName idQName = identity.getQName();
291 assertEquals(URI.create("urn:simple.demo.test3"), idQName.getNamespace());
292 assertEquals(simpleDateFormat.parse("2013-06-18"), idQName.getRevision());
293 assertEquals("pt", idQName.getLocalName());
295 IdentitySchemaNode baseIdentity = identity.getBaseIdentity();
296 QName idBaseQName = baseIdentity.getQName();
297 assertEquals(URI.create("urn:custom.types.demo"), idBaseQName.getNamespace());
298 assertEquals(simpleDateFormat.parse("2012-04-16"), idBaseQName.getRevision());
299 assertEquals("service-type", idBaseQName.getLocalName());
303 public void testUnknownNodes() throws Exception {
304 SchemaContext context;
305 File yangFile = new File(getClass().getResource("/types/custom-types-test@2012-4-4.yang").toURI());
306 File dependenciesDir = new File(getClass().getResource("/ietf").toURI());
307 YangContextParser parser = new YangParserImpl();
308 context = parser.parseFile(yangFile, dependenciesDir);
311 try (InputStream stream = new FileInputStream(new File(getClass().getResource("/context-test/test3.yang")
313 module = TestUtils.loadModuleWithContext("test3", stream, context);
316 ContainerSchemaNode network = (ContainerSchemaNode) module.getDataChildByName("network");
317 List<UnknownSchemaNode> unknownNodes = network.getUnknownSchemaNodes();
318 assertEquals(1, unknownNodes.size());
320 UnknownSchemaNode un = unknownNodes.get(0);
321 QName unType = un.getNodeType();
322 assertEquals(URI.create("urn:custom.types.demo"), unType.getNamespace());
323 assertEquals(simpleDateFormat.parse("2012-04-16"), unType.getRevision());
324 assertEquals("mountpoint", unType.getLocalName());
325 assertEquals("point", un.getNodeParameter());
326 assertNotNull(un.getExtensionDefinition());
330 public void testAugment() throws Exception {
332 String resource = "/context-augment-test/test4.yang";
333 SchemaContext context = parser.parseFiles(Collections.singleton(new File(getClass().getResource(resource)
336 // load another modules and parse them against already existing context
337 File test1 = new File(getClass().getResource("/context-augment-test/test1.yang").toURI());
338 File test2 = new File(getClass().getResource("/context-augment-test/test2.yang").toURI());
339 File test3 = new File(getClass().getResource("/context-augment-test/test3.yang").toURI());
340 Set<Module> modules = parser.parseFiles(Arrays.asList(test1, test2, test3), context).getModules();
341 assertNotNull(modules);
343 Module t4 = TestUtils.findModule(modules, "test4");
344 ContainerSchemaNode interfaces = (ContainerSchemaNode) t4.getDataChildByName("interfaces");
345 ListSchemaNode ifEntry = (ListSchemaNode) interfaces.getDataChildByName("ifEntry");
347 // test augmentation process
348 ContainerSchemaNode augmentHolder = (ContainerSchemaNode) ifEntry.getDataChildByName("augment-holder");
349 assertNotNull(augmentHolder);
350 DataSchemaNode ds0 = augmentHolder.getDataChildByName("ds0ChannelNumber");
352 DataSchemaNode interfaceId = augmentHolder.getDataChildByName("interface-id");
353 assertNotNull(interfaceId);
354 DataSchemaNode higherLayerIf = augmentHolder.getDataChildByName("higher-layer-if");
355 assertNotNull(higherLayerIf);
356 ContainerSchemaNode schemas = (ContainerSchemaNode) augmentHolder.getDataChildByName("schemas");
357 assertNotNull(schemas);
358 assertNotNull(schemas.getDataChildByName("id"));
360 // test augment target after augmentation: check if it is same instance
361 ListSchemaNode ifEntryAfterAugment = (ListSchemaNode) interfaces.getDataChildByName("ifEntry");
362 assertTrue(ifEntry == ifEntryAfterAugment);
366 public void testDeviation() throws Exception {
368 SchemaContext context = parser.parseFiles(Collections.singleton(
369 new File(getClass().getResource("/model/bar.yang").toURI())));
371 // load another modules and parse them against already existing context
373 try (InputStream stream = new FileInputStream(new File(getClass().getResource(
374 "/context-test/deviation-test.yang").toURI()))) {
375 List<InputStream> input = Lists.newArrayList(stream);
376 modules = TestUtils.loadModulesWithContext(input, context);
378 assertNotNull(modules);
381 Module testModule = TestUtils.findModule(modules, "deviation-test");
382 Set<Deviation> deviations = testModule.getDeviations();
383 assertEquals(1, deviations.size());
384 Deviation dev = deviations.iterator().next();
386 assertEquals("system/user ref", dev.getReference());
388 URI expectedNS = URI.create("urn:opendaylight.bar");
389 DateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
390 Date expectedRev = simpleDateFormat.parse("2013-07-03");
391 List<QName> path = new ArrayList<>();
392 path.add(QName.create(expectedNS, expectedRev, "interfaces"));
393 path.add(QName.create(expectedNS, expectedRev, "ifEntry"));
394 SchemaPath expectedPath = SchemaPath.create(path, true);
396 assertEquals(expectedPath, dev.getTargetPath());
397 assertEquals(Deviate.ADD, dev.getDeviate());