BUG-1382: eliminate use of QName.getPrefix from yang parser
[yangtools.git] / yang / yang-parser-impl / src / test / java / org / opendaylight / yangtools / yang / parser / impl / YangParserWithContextTest.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.parser.impl;
9
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
15 import com.google.common.collect.Lists;
16 import java.io.File;
17 import java.io.FileInputStream;
18 import java.io.InputStream;
19 import java.math.BigInteger;
20 import java.net.URI;
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;
28 import java.util.Map;
29 import java.util.Set;
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;
52
53 public class YangParserWithContextTest {
54     private final DateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
55     private final YangParserImpl parser = new YangParserImpl();
56
57     @Test
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)));
62         stream.close();
63
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);
67         stream2.close();
68         assertNotNull(module);
69
70         LeafSchemaNode leaf = (LeafSchemaNode) module.getDataChildByName("id");
71
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("port-number", qname.getLocalName());
77
78         ExtendedType leafBaseType = (ExtendedType) leafType.getBaseType();
79         qname = leafBaseType.getQName();
80         assertEquals(URI.create("urn:ietf:params:xml:ns:yang:ietf-inet-types"), qname.getNamespace());
81         assertEquals(simpleDateFormat.parse("2010-09-24"), qname.getRevision());
82         assertEquals("port-number", qname.getLocalName());
83
84         ExtendedType dscpExt = (ExtendedType) TestUtils.findTypedef(module.getTypeDefinitions(), "dscp-ext");
85         List<RangeConstraint> ranges = dscpExt.getRangeConstraints();
86         assertEquals(1, ranges.size());
87         RangeConstraint range = ranges.get(0);
88         assertEquals(BigInteger.ZERO, range.getMin());
89         assertEquals(BigInteger.valueOf(63), range.getMax());
90     }
91
92     @Test
93     public void testUsesFromContext() throws Exception {
94         SchemaContext context;
95         try (InputStream stream1 = new FileInputStream(new File(getClass().getResource("/model/baz.yang").toURI()));
96                 InputStream stream2 = new FileInputStream(new File(getClass().getResource("/model/bar.yang").toURI()));
97                 InputStream stream3 = new FileInputStream(new File(getClass().getResource("/model/foo.yang").toURI()))) {
98             context = parser.resolveSchemaContext(TestUtils.loadModules(Lists.newArrayList(stream1, stream2, stream3)));
99         }
100         Module testModule;
101         try (InputStream stream = new FileInputStream(new File(getClass().getResource("/context-test/test2.yang")
102                 .toURI()))) {
103             testModule = TestUtils.loadModuleWithContext("test2", stream, context);
104         }
105         assertNotNull(testModule);
106
107         // suffix _u = added by uses
108         // suffix _g = defined in grouping from context
109
110         // get grouping
111         Module contextModule = context.findModuleByNamespace(URI.create("urn:opendaylight.baz")).iterator().next();
112         assertNotNull(contextModule);
113         Set<GroupingDefinition> groupings = contextModule.getGroupings();
114         assertEquals(1, groupings.size());
115         GroupingDefinition grouping = groupings.iterator().next();
116
117         // get node containing uses
118         ContainerSchemaNode peer = (ContainerSchemaNode) testModule.getDataChildByName("peer");
119         ContainerSchemaNode destination = (ContainerSchemaNode) peer.getDataChildByName("destination");
120
121         // check uses
122         Set<UsesNode> uses = destination.getUses();
123         assertEquals(1, uses.size());
124
125         // check uses process
126         AnyXmlSchemaNode data_u = (AnyXmlSchemaNode) destination.getDataChildByName("data");
127         assertNotNull(data_u);
128         assertTrue(data_u.isAddedByUses());
129
130         AnyXmlSchemaNode data_g = (AnyXmlSchemaNode) grouping.getDataChildByName("data");
131         assertNotNull(data_g);
132         assertFalse(data_g.isAddedByUses());
133         assertFalse(data_u.equals(data_g));
134
135         ChoiceNode how_u = (ChoiceNode) destination.getDataChildByName("how");
136         assertNotNull(how_u);
137         assertTrue(how_u.isAddedByUses());
138
139         ChoiceNode how_g = (ChoiceNode) grouping.getDataChildByName("how");
140         assertNotNull(how_g);
141         assertFalse(how_g.isAddedByUses());
142         assertFalse(how_u.equals(how_g));
143
144         LeafSchemaNode address_u = (LeafSchemaNode) destination.getDataChildByName("address");
145         assertNotNull(address_u);
146         assertTrue(address_u.isAddedByUses());
147
148         LeafSchemaNode address_g = (LeafSchemaNode) grouping.getDataChildByName("address");
149         assertNotNull(address_g);
150         assertFalse(address_g.isAddedByUses());
151         assertFalse(address_u.equals(address_g));
152
153         ContainerSchemaNode port_u = (ContainerSchemaNode) destination.getDataChildByName("port");
154         assertNotNull(port_u);
155         assertTrue(port_u.isAddedByUses());
156
157         ContainerSchemaNode port_g = (ContainerSchemaNode) grouping.getDataChildByName("port");
158         assertNotNull(port_g);
159         assertFalse(port_g.isAddedByUses());
160         assertFalse(port_u.equals(port_g));
161
162         ListSchemaNode addresses_u = (ListSchemaNode) destination.getDataChildByName("addresses");
163         assertNotNull(addresses_u);
164         assertTrue(addresses_u.isAddedByUses());
165
166         ListSchemaNode addresses_g = (ListSchemaNode) grouping.getDataChildByName("addresses");
167         assertNotNull(addresses_g);
168         assertFalse(addresses_g.isAddedByUses());
169         assertFalse(addresses_u.equals(addresses_g));
170
171         // grouping defined by 'uses'
172         Set<GroupingDefinition> groupings_u = destination.getGroupings();
173         assertEquals(1, groupings_u.size());
174         GroupingDefinition grouping_u = groupings_u.iterator().next();
175         assertTrue(grouping_u.isAddedByUses());
176
177         // grouping defined in 'grouping' node
178         Set<GroupingDefinition> groupings_g = grouping.getGroupings();
179         assertEquals(1, groupings_g.size());
180         GroupingDefinition grouping_g = groupings_g.iterator().next();
181         assertFalse(grouping_g.isAddedByUses());
182         assertFalse(grouping_u.equals(grouping_g));
183
184         List<UnknownSchemaNode> nodes_u = destination.getUnknownSchemaNodes();
185         assertEquals(1, nodes_u.size());
186         UnknownSchemaNode node_u = nodes_u.get(0);
187         assertTrue(node_u.isAddedByUses());
188
189         List<UnknownSchemaNode> nodes_g = grouping.getUnknownSchemaNodes();
190         assertEquals(1, nodes_g.size());
191         UnknownSchemaNode node_g = nodes_g.get(0);
192         assertFalse(node_g.isAddedByUses());
193         assertFalse(node_u.equals(node_g));
194     }
195
196     @Test
197     public void testUsesRefineFromContext() throws Exception {
198         SchemaContext context;
199         try (InputStream stream1 = new FileInputStream(new File(getClass().getResource("/model/baz.yang").toURI()));
200                 InputStream stream2 = new FileInputStream(new File(getClass().getResource("/model/bar.yang").toURI()));
201                 InputStream stream3 = new FileInputStream(new File(getClass().getResource("/model/foo.yang").toURI()))) {
202             context = parser.resolveSchemaContext(TestUtils.loadModules(Lists.newArrayList(stream1, stream2, stream3)));
203         }
204         Module module;
205         try (InputStream stream = new FileInputStream(new File(getClass().getResource("/context-test/test2.yang")
206                 .toURI()))) {
207             module = TestUtils.loadModuleWithContext("test2", stream, context);
208         }
209         assertNotNull(module);
210
211         ContainerSchemaNode peer = (ContainerSchemaNode) module.getDataChildByName("peer");
212         ContainerSchemaNode destination = (ContainerSchemaNode) peer.getDataChildByName("destination");
213         Set<UsesNode> usesNodes = destination.getUses();
214         assertEquals(1, usesNodes.size());
215         UsesNode usesNode = usesNodes.iterator().next();
216
217         // test grouping path
218         List<QName> path = new ArrayList<>();
219         QName qname = new QName(URI.create("urn:opendaylight.baz"), simpleDateFormat.parse("2013-02-27"), "baz",
220                 "target");
221         path.add(qname);
222         SchemaPath expectedPath = SchemaPath.create(path, true);
223         assertEquals(expectedPath, usesNode.getGroupingPath());
224
225         // test refine
226         Map<SchemaPath, SchemaNode> refines = usesNode.getRefines();
227         assertEquals(3, refines.size());
228
229         LeafSchemaNode refineLeaf = null;
230         ContainerSchemaNode refineContainer = null;
231         ListSchemaNode refineList = null;
232         for (Map.Entry<SchemaPath, SchemaNode> entry : refines.entrySet()) {
233             SchemaNode value = entry.getValue();
234             if (value instanceof LeafSchemaNode) {
235                 refineLeaf = (LeafSchemaNode) value;
236             } else if (value instanceof ContainerSchemaNode) {
237                 refineContainer = (ContainerSchemaNode) value;
238             } else if (value instanceof ListSchemaNode) {
239                 refineList = (ListSchemaNode) value;
240             }
241         }
242
243         // leaf address
244         assertNotNull(refineLeaf);
245         assertEquals("address", refineLeaf.getQName().getLocalName());
246         assertEquals("description of address defined by refine", refineLeaf.getDescription());
247         assertEquals("address reference added by refine", refineLeaf.getReference());
248         assertFalse(refineLeaf.isConfiguration());
249         assertTrue(refineLeaf.getConstraints().isMandatory());
250         Set<MustDefinition> leafMustConstraints = refineLeaf.getConstraints().getMustConstraints();
251         assertEquals(1, leafMustConstraints.size());
252         MustDefinition leafMust = leafMustConstraints.iterator().next();
253         assertEquals("\"ifType != 'ethernet' or (ifType = 'ethernet' and ifMTU = 1500)\"", leafMust.toString());
254
255         // container port
256         assertNotNull(refineContainer);
257         Set<MustDefinition> mustConstraints = refineContainer.getConstraints().getMustConstraints();
258         assertTrue(mustConstraints.isEmpty());
259         assertEquals("description of port defined by refine", refineContainer.getDescription());
260         assertEquals("port reference added by refine", refineContainer.getReference());
261         assertFalse(refineContainer.isConfiguration());
262         assertTrue(refineContainer.isPresenceContainer());
263
264         // list addresses
265         assertNotNull(refineList);
266         assertEquals("description of addresses defined by refine", refineList.getDescription());
267         assertEquals("addresses reference added by refine", refineList.getReference());
268         assertFalse(refineList.isConfiguration());
269         assertEquals(2, (int) refineList.getConstraints().getMinElements());
270         assertEquals(12, (int) refineList.getConstraints().getMaxElements());
271     }
272
273     @Test
274     public void testIdentity() throws Exception {
275         SchemaContext context;
276         File yangFile = new File(getClass().getResource("/types/custom-types-test@2012-4-4.yang").toURI());
277         File dependenciesDir = new File(getClass().getResource("/ietf").toURI());
278         YangContextParser parser = new YangParserImpl();
279         context = parser.parseFile(yangFile, dependenciesDir);
280
281         Module module;
282         try (InputStream stream = new FileInputStream(new File(getClass().getResource("/context-test/test3.yang")
283                 .toURI()))) {
284             module = TestUtils.loadModuleWithContext("test3", stream, context);
285         }
286         assertNotNull(module);
287
288         Set<IdentitySchemaNode> identities = module.getIdentities();
289         assertEquals(1, identities.size());
290
291         IdentitySchemaNode identity = identities.iterator().next();
292         QName idQName = identity.getQName();
293         assertEquals(URI.create("urn:simple.demo.test3"), idQName.getNamespace());
294         assertEquals(simpleDateFormat.parse("2013-06-18"), idQName.getRevision());
295         assertEquals("pt", idQName.getLocalName());
296
297         IdentitySchemaNode baseIdentity = identity.getBaseIdentity();
298         QName idBaseQName = baseIdentity.getQName();
299         assertEquals(URI.create("urn:custom.types.demo"), idBaseQName.getNamespace());
300         assertEquals(simpleDateFormat.parse("2012-04-16"), idBaseQName.getRevision());
301         assertEquals("service-type", idBaseQName.getLocalName());
302     }
303
304     @Test
305     public void testUnknownNodes() throws Exception {
306         SchemaContext context;
307         File yangFile = new File(getClass().getResource("/types/custom-types-test@2012-4-4.yang").toURI());
308         File dependenciesDir = new File(getClass().getResource("/ietf").toURI());
309         YangContextParser parser = new YangParserImpl();
310         context = parser.parseFile(yangFile, dependenciesDir);
311
312         Module module;
313         try (InputStream stream = new FileInputStream(new File(getClass().getResource("/context-test/test3.yang")
314                 .toURI()))) {
315             module = TestUtils.loadModuleWithContext("test3", stream, context);
316         }
317
318         ContainerSchemaNode network = (ContainerSchemaNode) module.getDataChildByName("network");
319         List<UnknownSchemaNode> unknownNodes = network.getUnknownSchemaNodes();
320         assertEquals(1, unknownNodes.size());
321
322         UnknownSchemaNode un = unknownNodes.get(0);
323         QName unType = un.getNodeType();
324         assertEquals(URI.create("urn:custom.types.demo"), unType.getNamespace());
325         assertEquals(simpleDateFormat.parse("2012-04-16"), unType.getRevision());
326         assertEquals("mountpoint", unType.getLocalName());
327         assertEquals("point", un.getNodeParameter());
328         assertNotNull(un.getExtensionDefinition());
329     }
330
331     @Test
332     public void testAugment() throws Exception {
333         // load first module
334         String resource = "/context-augment-test/test4.yang";
335         SchemaContext context = parser.parseFiles(Collections.singleton(new File(getClass().getResource(resource)
336                 .toURI())));
337
338         // load another modules and parse them against already existing context
339         File test1 = new File(getClass().getResource("/context-augment-test/test1.yang").toURI());
340         File test2 = new File(getClass().getResource("/context-augment-test/test2.yang").toURI());
341         File test3 = new File(getClass().getResource("/context-augment-test/test3.yang").toURI());
342         Set<Module> modules = parser.parseFiles(Arrays.asList(test1, test2, test3), context).getModules();
343         assertNotNull(modules);
344
345         Module t4 = TestUtils.findModule(modules, "test4");
346         ContainerSchemaNode interfaces = (ContainerSchemaNode) t4.getDataChildByName("interfaces");
347         ListSchemaNode ifEntry = (ListSchemaNode) interfaces.getDataChildByName("ifEntry");
348
349         // test augmentation process
350         ContainerSchemaNode augmentHolder = (ContainerSchemaNode) ifEntry.getDataChildByName("augment-holder");
351         assertNotNull(augmentHolder);
352         DataSchemaNode ds0 = augmentHolder.getDataChildByName("ds0ChannelNumber");
353         assertNotNull(ds0);
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"));
361
362         // test augment target after augmentation: check if it is same instance
363         ListSchemaNode ifEntryAfterAugment = (ListSchemaNode) interfaces.getDataChildByName("ifEntry");
364         assertTrue(ifEntry == ifEntryAfterAugment);
365     }
366
367     @Test
368     public void testDeviation() throws Exception {
369         // load first module
370         SchemaContext context;
371         String resource = "/model/bar.yang";
372
373         try (InputStream stream = new FileInputStream(new File(getClass().getResource(resource).toURI()))) {
374             context = parser.resolveSchemaContext(TestUtils.loadModules(Lists.newArrayList(stream)));
375         }
376
377         // load another modules and parse them against already existing context
378         Set<Module> modules;
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);
383         }
384         assertNotNull(modules);
385
386         // test deviation
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();
391
392         assertEquals("system/user ref", dev.getReference());
393
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 = SchemaPath.create(path, true);
401
402         assertEquals(expectedPath, dev.getTargetPath());
403         assertEquals(Deviate.ADD, dev.getDeviate());
404     }
405
406 }