Use Collections.singletonList() instead of Arrays.asList()
[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 import com.google.common.collect.Lists;
15 import com.google.common.io.ByteSource;
16 import com.google.common.io.Resources;
17 import java.io.File;
18 import java.io.FileInputStream;
19 import java.io.IOException;
20 import java.io.InputStream;
21 import java.math.BigInteger;
22 import java.net.URI;
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;
30 import java.util.Map;
31 import java.util.Set;
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;
55
56 public class YangParserWithContextTest {
57     private final DateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
58     private final YangParserImpl parser = new YangParserImpl();
59
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)));
64         }
65
66         return parser.parseSources(srcs);
67     }
68
69     @Test
70     public void testTypeFromContext() throws Exception {
71         SchemaContext context = createContext("/ietf/ietf-inet-types@2010-09-24.yang");
72
73         InputStream stream2 = new FileInputStream(new File(getClass().getResource("/context-test/test1.yang").toURI()));
74         Module module = TestUtils.loadModuleWithContext("test1", stream2, context);
75         stream2.close();
76         assertNotNull(module);
77
78         LeafSchemaNode leaf = (LeafSchemaNode) module.getDataChildByName("id");
79
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());
85
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());
91
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());
98     }
99
100     @Test
101     public void testUsesFromContext() throws Exception {
102         SchemaContext context = createContext("/model/baz.yang", "/model/bar.yang", "/model/foo.yang", "/model/subfoo.yang");
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         ChoiceSchemaNode how_u = (ChoiceSchemaNode) destination.getDataChildByName("how");
139         assertNotNull(how_u);
140         assertTrue(how_u.isAddedByUses());
141
142         ChoiceSchemaNode how_g = (ChoiceSchemaNode) 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 = createContext("/model/baz.yang", "/model/bar.yang", "/model/foo.yang", "/model/subfoo.yang");
202
203         Module module;
204         try (InputStream stream = new FileInputStream(new File(getClass().getResource("/context-test/test2.yang")
205                 .toURI()))) {
206             module = TestUtils.loadModuleWithContext("test2", stream, context);
207         }
208         assertNotNull(module);
209
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();
215
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");
219         path.add(qname);
220         SchemaPath expectedPath = SchemaPath.create(path, true);
221         assertEquals(expectedPath, usesNode.getGroupingPath());
222
223         // test refine
224         Map<SchemaPath, SchemaNode> refines = usesNode.getRefines();
225         assertEquals(3, refines.size());
226
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;
238             }
239         }
240
241         // leaf address
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());
252
253         // container port
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());
261
262         // list addresses
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());
269     }
270
271     @Test
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);
278
279         Module module;
280         try (InputStream stream = new FileInputStream(new File(getClass().getResource("/context-test/test3.yang")
281                 .toURI()))) {
282             module = TestUtils.loadModuleWithContext("test3", stream, context);
283         }
284         assertNotNull(module);
285
286         Set<IdentitySchemaNode> identities = module.getIdentities();
287         assertEquals(1, identities.size());
288
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());
294
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());
300     }
301
302     @Test
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);
309
310         Module module;
311         try (InputStream stream = new FileInputStream(new File(getClass().getResource("/context-test/test3.yang")
312                 .toURI()))) {
313             module = TestUtils.loadModuleWithContext("test3", stream, context);
314         }
315
316         ContainerSchemaNode network = (ContainerSchemaNode) module.getDataChildByName("network");
317         List<UnknownSchemaNode> unknownNodes = network.getUnknownSchemaNodes();
318         assertEquals(1, unknownNodes.size());
319
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());
327     }
328
329     @Test
330     public void testAugment() throws Exception {
331         // load first module
332         String resource = "/context-augment-test/test4.yang";
333         SchemaContext context = parser.parseFiles(Collections.singleton(new File(getClass().getResource(resource)
334                 .toURI())));
335
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);
342
343         Module t4 = TestUtils.findModule(modules, "test4");
344         ContainerSchemaNode interfaces = (ContainerSchemaNode) t4.getDataChildByName("interfaces");
345         ListSchemaNode ifEntry = (ListSchemaNode) interfaces.getDataChildByName("ifEntry");
346
347         // test augmentation process
348         ContainerSchemaNode augmentHolder = (ContainerSchemaNode) ifEntry.getDataChildByName("augment-holder");
349         assertNotNull(augmentHolder);
350         DataSchemaNode ds0 = augmentHolder.getDataChildByName("ds0ChannelNumber");
351         assertNotNull(ds0);
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"));
359
360         // test augment target after augmentation: check if it is same instance
361         ListSchemaNode ifEntryAfterAugment = (ListSchemaNode) interfaces.getDataChildByName("ifEntry");
362         assertTrue(ifEntry == ifEntryAfterAugment);
363     }
364
365     @Test
366     public void testDeviation() throws Exception {
367         // load first module
368         SchemaContext context = parser.parseFiles(Collections.singleton(
369             new File(getClass().getResource("/model/bar.yang").toURI())));
370
371         // load another modules and parse them against already existing context
372         Set<Module> modules;
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);
377         }
378         assertNotNull(modules);
379
380         // test deviation
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();
385
386         assertEquals("system/user ref", dev.getReference());
387
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);
395
396         assertEquals(expectedPath, dev.getTargetPath());
397         assertEquals(Deviate.ADD, dev.getDeviate());
398     }
399
400 }