Merge "Split out codecs from BindingCodecContext"
[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                 InputStream stream4 = new FileInputStream(
99                         new File(getClass().getResource("/model/subfoo.yang").toURI()))) {
100             context = parser.resolveSchemaContext(TestUtils.loadModules(Lists.newArrayList(stream1, stream2, stream3,
101                     stream4)));
102         }
103         Module testModule;
104         try (InputStream stream = new FileInputStream(new File(getClass().getResource("/context-test/test2.yang")
105                 .toURI()))) {
106             testModule = TestUtils.loadModuleWithContext("test2", stream, context);
107         }
108         assertNotNull(testModule);
109
110         // suffix _u = added by uses
111         // suffix _g = defined in grouping from context
112
113         // get grouping
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();
119
120         // get node containing uses
121         ContainerSchemaNode peer = (ContainerSchemaNode) testModule.getDataChildByName("peer");
122         ContainerSchemaNode destination = (ContainerSchemaNode) peer.getDataChildByName("destination");
123
124         // check uses
125         Set<UsesNode> uses = destination.getUses();
126         assertEquals(1, uses.size());
127
128         // check uses process
129         AnyXmlSchemaNode data_u = (AnyXmlSchemaNode) destination.getDataChildByName("data");
130         assertNotNull(data_u);
131         assertTrue(data_u.isAddedByUses());
132
133         AnyXmlSchemaNode data_g = (AnyXmlSchemaNode) grouping.getDataChildByName("data");
134         assertNotNull(data_g);
135         assertFalse(data_g.isAddedByUses());
136         assertFalse(data_u.equals(data_g));
137
138         ChoiceNode how_u = (ChoiceNode) destination.getDataChildByName("how");
139         assertNotNull(how_u);
140         assertTrue(how_u.isAddedByUses());
141
142         ChoiceNode how_g = (ChoiceNode) grouping.getDataChildByName("how");
143         assertNotNull(how_g);
144         assertFalse(how_g.isAddedByUses());
145         assertFalse(how_u.equals(how_g));
146
147         LeafSchemaNode address_u = (LeafSchemaNode) destination.getDataChildByName("address");
148         assertNotNull(address_u);
149         assertTrue(address_u.isAddedByUses());
150
151         LeafSchemaNode address_g = (LeafSchemaNode) grouping.getDataChildByName("address");
152         assertNotNull(address_g);
153         assertFalse(address_g.isAddedByUses());
154         assertFalse(address_u.equals(address_g));
155
156         ContainerSchemaNode port_u = (ContainerSchemaNode) destination.getDataChildByName("port");
157         assertNotNull(port_u);
158         assertTrue(port_u.isAddedByUses());
159
160         ContainerSchemaNode port_g = (ContainerSchemaNode) grouping.getDataChildByName("port");
161         assertNotNull(port_g);
162         assertFalse(port_g.isAddedByUses());
163         assertFalse(port_u.equals(port_g));
164
165         ListSchemaNode addresses_u = (ListSchemaNode) destination.getDataChildByName("addresses");
166         assertNotNull(addresses_u);
167         assertTrue(addresses_u.isAddedByUses());
168
169         ListSchemaNode addresses_g = (ListSchemaNode) grouping.getDataChildByName("addresses");
170         assertNotNull(addresses_g);
171         assertFalse(addresses_g.isAddedByUses());
172         assertFalse(addresses_u.equals(addresses_g));
173
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());
179
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));
186
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());
191
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));
197     }
198
199     @Test
200     public void testUsesRefineFromContext() throws Exception {
201         SchemaContext context;
202         try (InputStream stream1 = new FileInputStream(new File(getClass().getResource("/model/baz.yang").toURI()));
203                 InputStream stream2 = new FileInputStream(new File(getClass().getResource("/model/bar.yang").toURI()));
204                 InputStream stream3 = new FileInputStream(new File(getClass().getResource("/model/foo.yang").toURI()));
205                 InputStream stream4 = new FileInputStream(
206                         new File(getClass().getResource("/model/subfoo.yang").toURI()))) {
207             context = parser.resolveSchemaContext(TestUtils.loadModules(Lists.newArrayList(stream1, stream2, stream3,
208                     stream4)));
209         }
210         Module module;
211         try (InputStream stream = new FileInputStream(new File(getClass().getResource("/context-test/test2.yang")
212                 .toURI()))) {
213             module = TestUtils.loadModuleWithContext("test2", stream, context);
214         }
215         assertNotNull(module);
216
217         ContainerSchemaNode peer = (ContainerSchemaNode) module.getDataChildByName("peer");
218         ContainerSchemaNode destination = (ContainerSchemaNode) peer.getDataChildByName("destination");
219         Set<UsesNode> usesNodes = destination.getUses();
220         assertEquals(1, usesNodes.size());
221         UsesNode usesNode = usesNodes.iterator().next();
222
223         // test grouping path
224         List<QName> path = new ArrayList<>();
225         QName qname = QName.create(URI.create("urn:opendaylight.baz"), simpleDateFormat.parse("2013-02-27"), "target");
226         path.add(qname);
227         SchemaPath expectedPath = SchemaPath.create(path, true);
228         assertEquals(expectedPath, usesNode.getGroupingPath());
229
230         // test refine
231         Map<SchemaPath, SchemaNode> refines = usesNode.getRefines();
232         assertEquals(3, refines.size());
233
234         LeafSchemaNode refineLeaf = null;
235         ContainerSchemaNode refineContainer = null;
236         ListSchemaNode refineList = null;
237         for (Map.Entry<SchemaPath, SchemaNode> entry : refines.entrySet()) {
238             SchemaNode value = entry.getValue();
239             if (value instanceof LeafSchemaNode) {
240                 refineLeaf = (LeafSchemaNode) value;
241             } else if (value instanceof ContainerSchemaNode) {
242                 refineContainer = (ContainerSchemaNode) value;
243             } else if (value instanceof ListSchemaNode) {
244                 refineList = (ListSchemaNode) value;
245             }
246         }
247
248         // leaf address
249         assertNotNull(refineLeaf);
250         assertEquals("address", refineLeaf.getQName().getLocalName());
251         assertEquals("description of address defined by refine", refineLeaf.getDescription());
252         assertEquals("address reference added by refine", refineLeaf.getReference());
253         assertFalse(refineLeaf.isConfiguration());
254         assertTrue(refineLeaf.getConstraints().isMandatory());
255         Set<MustDefinition> leafMustConstraints = refineLeaf.getConstraints().getMustConstraints();
256         assertEquals(1, leafMustConstraints.size());
257         MustDefinition leafMust = leafMustConstraints.iterator().next();
258         assertEquals("\"ifType != 'ethernet' or (ifType = 'ethernet' and ifMTU = 1500)\"", leafMust.toString());
259
260         // container port
261         assertNotNull(refineContainer);
262         Set<MustDefinition> mustConstraints = refineContainer.getConstraints().getMustConstraints();
263         assertTrue(mustConstraints.isEmpty());
264         assertEquals("description of port defined by refine", refineContainer.getDescription());
265         assertEquals("port reference added by refine", refineContainer.getReference());
266         assertFalse(refineContainer.isConfiguration());
267         assertTrue(refineContainer.isPresenceContainer());
268
269         // list addresses
270         assertNotNull(refineList);
271         assertEquals("description of addresses defined by refine", refineList.getDescription());
272         assertEquals("addresses reference added by refine", refineList.getReference());
273         assertFalse(refineList.isConfiguration());
274         assertEquals(2, (int) refineList.getConstraints().getMinElements());
275         assertEquals(12, (int) refineList.getConstraints().getMaxElements());
276     }
277
278     @Test
279     public void testIdentity() throws Exception {
280         SchemaContext context;
281         File yangFile = new File(getClass().getResource("/types/custom-types-test@2012-4-4.yang").toURI());
282         File dependenciesDir = new File(getClass().getResource("/ietf").toURI());
283         YangContextParser parser = new YangParserImpl();
284         context = parser.parseFile(yangFile, dependenciesDir);
285
286         Module module;
287         try (InputStream stream = new FileInputStream(new File(getClass().getResource("/context-test/test3.yang")
288                 .toURI()))) {
289             module = TestUtils.loadModuleWithContext("test3", stream, context);
290         }
291         assertNotNull(module);
292
293         Set<IdentitySchemaNode> identities = module.getIdentities();
294         assertEquals(1, identities.size());
295
296         IdentitySchemaNode identity = identities.iterator().next();
297         QName idQName = identity.getQName();
298         assertEquals(URI.create("urn:simple.demo.test3"), idQName.getNamespace());
299         assertEquals(simpleDateFormat.parse("2013-06-18"), idQName.getRevision());
300         assertEquals("pt", idQName.getLocalName());
301
302         IdentitySchemaNode baseIdentity = identity.getBaseIdentity();
303         QName idBaseQName = baseIdentity.getQName();
304         assertEquals(URI.create("urn:custom.types.demo"), idBaseQName.getNamespace());
305         assertEquals(simpleDateFormat.parse("2012-04-16"), idBaseQName.getRevision());
306         assertEquals("service-type", idBaseQName.getLocalName());
307     }
308
309     @Test
310     public void testUnknownNodes() throws Exception {
311         SchemaContext context;
312         File yangFile = new File(getClass().getResource("/types/custom-types-test@2012-4-4.yang").toURI());
313         File dependenciesDir = new File(getClass().getResource("/ietf").toURI());
314         YangContextParser parser = new YangParserImpl();
315         context = parser.parseFile(yangFile, dependenciesDir);
316
317         Module module;
318         try (InputStream stream = new FileInputStream(new File(getClass().getResource("/context-test/test3.yang")
319                 .toURI()))) {
320             module = TestUtils.loadModuleWithContext("test3", stream, context);
321         }
322
323         ContainerSchemaNode network = (ContainerSchemaNode) module.getDataChildByName("network");
324         List<UnknownSchemaNode> unknownNodes = network.getUnknownSchemaNodes();
325         assertEquals(1, unknownNodes.size());
326
327         UnknownSchemaNode un = unknownNodes.get(0);
328         QName unType = un.getNodeType();
329         assertEquals(URI.create("urn:custom.types.demo"), unType.getNamespace());
330         assertEquals(simpleDateFormat.parse("2012-04-16"), unType.getRevision());
331         assertEquals("mountpoint", unType.getLocalName());
332         assertEquals("point", un.getNodeParameter());
333         assertNotNull(un.getExtensionDefinition());
334     }
335
336     @Test
337     public void testAugment() throws Exception {
338         // load first module
339         String resource = "/context-augment-test/test4.yang";
340         SchemaContext context = parser.parseFiles(Collections.singleton(new File(getClass().getResource(resource)
341                 .toURI())));
342
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);
349
350         Module t4 = TestUtils.findModule(modules, "test4");
351         ContainerSchemaNode interfaces = (ContainerSchemaNode) t4.getDataChildByName("interfaces");
352         ListSchemaNode ifEntry = (ListSchemaNode) interfaces.getDataChildByName("ifEntry");
353
354         // test augmentation process
355         ContainerSchemaNode augmentHolder = (ContainerSchemaNode) ifEntry.getDataChildByName("augment-holder");
356         assertNotNull(augmentHolder);
357         DataSchemaNode ds0 = augmentHolder.getDataChildByName("ds0ChannelNumber");
358         assertNotNull(ds0);
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"));
366
367         // test augment target after augmentation: check if it is same instance
368         ListSchemaNode ifEntryAfterAugment = (ListSchemaNode) interfaces.getDataChildByName("ifEntry");
369         assertTrue(ifEntry == ifEntryAfterAugment);
370     }
371
372     @Test
373     public void testDeviation() throws Exception {
374         // load first module
375         SchemaContext context;
376         String resource = "/model/bar.yang";
377
378         try (InputStream stream = new FileInputStream(new File(getClass().getResource(resource).toURI()))) {
379             context = parser.resolveSchemaContext(TestUtils.loadModules(Lists.newArrayList(stream)));
380         }
381
382         // load another modules and parse them against already existing context
383         Set<Module> modules;
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);
388         }
389         assertNotNull(modules);
390
391         // test deviation
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();
396
397         assertEquals("system/user ref", dev.getReference());
398
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(QName.create(expectedNS, expectedRev, "interfaces"));
404         path.add(QName.create(expectedNS, expectedRev, "ifEntry"));
405         SchemaPath expectedPath = SchemaPath.create(path, true);
406
407         assertEquals(expectedPath, dev.getTargetPath());
408         assertEquals(Deviate.ADD, dev.getDeviate());
409     }
410
411 }