Merge "Bug 1391 - New DataBroker unexpected behaviour"
[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 = new QName(URI.create("urn:opendaylight.baz"), simpleDateFormat.parse("2013-02-27"), "baz",
226                 "target");
227         path.add(qname);
228         SchemaPath expectedPath = SchemaPath.create(path, true);
229         assertEquals(expectedPath, usesNode.getGroupingPath());
230
231         // test refine
232         Map<SchemaPath, SchemaNode> refines = usesNode.getRefines();
233         assertEquals(3, refines.size());
234
235         LeafSchemaNode refineLeaf = null;
236         ContainerSchemaNode refineContainer = null;
237         ListSchemaNode refineList = null;
238         for (Map.Entry<SchemaPath, SchemaNode> entry : refines.entrySet()) {
239             SchemaNode value = entry.getValue();
240             if (value instanceof LeafSchemaNode) {
241                 refineLeaf = (LeafSchemaNode) value;
242             } else if (value instanceof ContainerSchemaNode) {
243                 refineContainer = (ContainerSchemaNode) value;
244             } else if (value instanceof ListSchemaNode) {
245                 refineList = (ListSchemaNode) value;
246             }
247         }
248
249         // leaf address
250         assertNotNull(refineLeaf);
251         assertEquals("address", refineLeaf.getQName().getLocalName());
252         assertEquals("description of address defined by refine", refineLeaf.getDescription());
253         assertEquals("address reference added by refine", refineLeaf.getReference());
254         assertFalse(refineLeaf.isConfiguration());
255         assertTrue(refineLeaf.getConstraints().isMandatory());
256         Set<MustDefinition> leafMustConstraints = refineLeaf.getConstraints().getMustConstraints();
257         assertEquals(1, leafMustConstraints.size());
258         MustDefinition leafMust = leafMustConstraints.iterator().next();
259         assertEquals("\"ifType != 'ethernet' or (ifType = 'ethernet' and ifMTU = 1500)\"", leafMust.toString());
260
261         // container port
262         assertNotNull(refineContainer);
263         Set<MustDefinition> mustConstraints = refineContainer.getConstraints().getMustConstraints();
264         assertTrue(mustConstraints.isEmpty());
265         assertEquals("description of port defined by refine", refineContainer.getDescription());
266         assertEquals("port reference added by refine", refineContainer.getReference());
267         assertFalse(refineContainer.isConfiguration());
268         assertTrue(refineContainer.isPresenceContainer());
269
270         // list addresses
271         assertNotNull(refineList);
272         assertEquals("description of addresses defined by refine", refineList.getDescription());
273         assertEquals("addresses reference added by refine", refineList.getReference());
274         assertFalse(refineList.isConfiguration());
275         assertEquals(2, (int) refineList.getConstraints().getMinElements());
276         assertEquals(12, (int) refineList.getConstraints().getMaxElements());
277     }
278
279     @Test
280     public void testIdentity() throws Exception {
281         SchemaContext context;
282         File yangFile = new File(getClass().getResource("/types/custom-types-test@2012-4-4.yang").toURI());
283         File dependenciesDir = new File(getClass().getResource("/ietf").toURI());
284         YangContextParser parser = new YangParserImpl();
285         context = parser.parseFile(yangFile, dependenciesDir);
286
287         Module module;
288         try (InputStream stream = new FileInputStream(new File(getClass().getResource("/context-test/test3.yang")
289                 .toURI()))) {
290             module = TestUtils.loadModuleWithContext("test3", stream, context);
291         }
292         assertNotNull(module);
293
294         Set<IdentitySchemaNode> identities = module.getIdentities();
295         assertEquals(1, identities.size());
296
297         IdentitySchemaNode identity = identities.iterator().next();
298         QName idQName = identity.getQName();
299         assertEquals(URI.create("urn:simple.demo.test3"), idQName.getNamespace());
300         assertEquals(simpleDateFormat.parse("2013-06-18"), idQName.getRevision());
301         assertEquals("pt", idQName.getLocalName());
302
303         IdentitySchemaNode baseIdentity = identity.getBaseIdentity();
304         QName idBaseQName = baseIdentity.getQName();
305         assertEquals(URI.create("urn:custom.types.demo"), idBaseQName.getNamespace());
306         assertEquals(simpleDateFormat.parse("2012-04-16"), idBaseQName.getRevision());
307         assertEquals("service-type", idBaseQName.getLocalName());
308     }
309
310     @Test
311     public void testUnknownNodes() throws Exception {
312         SchemaContext context;
313         File yangFile = new File(getClass().getResource("/types/custom-types-test@2012-4-4.yang").toURI());
314         File dependenciesDir = new File(getClass().getResource("/ietf").toURI());
315         YangContextParser parser = new YangParserImpl();
316         context = parser.parseFile(yangFile, dependenciesDir);
317
318         Module module;
319         try (InputStream stream = new FileInputStream(new File(getClass().getResource("/context-test/test3.yang")
320                 .toURI()))) {
321             module = TestUtils.loadModuleWithContext("test3", stream, context);
322         }
323
324         ContainerSchemaNode network = (ContainerSchemaNode) module.getDataChildByName("network");
325         List<UnknownSchemaNode> unknownNodes = network.getUnknownSchemaNodes();
326         assertEquals(1, unknownNodes.size());
327
328         UnknownSchemaNode un = unknownNodes.get(0);
329         QName unType = un.getNodeType();
330         assertEquals(URI.create("urn:custom.types.demo"), unType.getNamespace());
331         assertEquals(simpleDateFormat.parse("2012-04-16"), unType.getRevision());
332         assertEquals("mountpoint", unType.getLocalName());
333         assertEquals("point", un.getNodeParameter());
334         assertNotNull(un.getExtensionDefinition());
335     }
336
337     @Test
338     public void testAugment() throws Exception {
339         // load first module
340         String resource = "/context-augment-test/test4.yang";
341         SchemaContext context = parser.parseFiles(Collections.singleton(new File(getClass().getResource(resource)
342                 .toURI())));
343
344         // load another modules and parse them against already existing context
345         File test1 = new File(getClass().getResource("/context-augment-test/test1.yang").toURI());
346         File test2 = new File(getClass().getResource("/context-augment-test/test2.yang").toURI());
347         File test3 = new File(getClass().getResource("/context-augment-test/test3.yang").toURI());
348         Set<Module> modules = parser.parseFiles(Arrays.asList(test1, test2, test3), context).getModules();
349         assertNotNull(modules);
350
351         Module t4 = TestUtils.findModule(modules, "test4");
352         ContainerSchemaNode interfaces = (ContainerSchemaNode) t4.getDataChildByName("interfaces");
353         ListSchemaNode ifEntry = (ListSchemaNode) interfaces.getDataChildByName("ifEntry");
354
355         // test augmentation process
356         ContainerSchemaNode augmentHolder = (ContainerSchemaNode) ifEntry.getDataChildByName("augment-holder");
357         assertNotNull(augmentHolder);
358         DataSchemaNode ds0 = augmentHolder.getDataChildByName("ds0ChannelNumber");
359         assertNotNull(ds0);
360         DataSchemaNode interfaceId = augmentHolder.getDataChildByName("interface-id");
361         assertNotNull(interfaceId);
362         DataSchemaNode higherLayerIf = augmentHolder.getDataChildByName("higher-layer-if");
363         assertNotNull(higherLayerIf);
364         ContainerSchemaNode schemas = (ContainerSchemaNode) augmentHolder.getDataChildByName("schemas");
365         assertNotNull(schemas);
366         assertNotNull(schemas.getDataChildByName("id"));
367
368         // test augment target after augmentation: check if it is same instance
369         ListSchemaNode ifEntryAfterAugment = (ListSchemaNode) interfaces.getDataChildByName("ifEntry");
370         assertTrue(ifEntry == ifEntryAfterAugment);
371     }
372
373     @Test
374     public void testDeviation() throws Exception {
375         // load first module
376         SchemaContext context;
377         String resource = "/model/bar.yang";
378
379         try (InputStream stream = new FileInputStream(new File(getClass().getResource(resource).toURI()))) {
380             context = parser.resolveSchemaContext(TestUtils.loadModules(Lists.newArrayList(stream)));
381         }
382
383         // load another modules and parse them against already existing context
384         Set<Module> modules;
385         try (InputStream stream = new FileInputStream(new File(getClass().getResource(
386                 "/context-test/deviation-test.yang").toURI()))) {
387             List<InputStream> input = Lists.newArrayList(stream);
388             modules = TestUtils.loadModulesWithContext(input, context);
389         }
390         assertNotNull(modules);
391
392         // test deviation
393         Module testModule = TestUtils.findModule(modules, "deviation-test");
394         Set<Deviation> deviations = testModule.getDeviations();
395         assertEquals(1, deviations.size());
396         Deviation dev = deviations.iterator().next();
397
398         assertEquals("system/user ref", dev.getReference());
399
400         URI expectedNS = URI.create("urn:opendaylight.bar");
401         DateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
402         Date expectedRev = simpleDateFormat.parse("2013-07-03");
403         List<QName> path = new ArrayList<>();
404         path.add(new QName(expectedNS, expectedRev, "bar", "interfaces"));
405         path.add(new QName(expectedNS, expectedRev, "bar", "ifEntry"));
406         SchemaPath expectedPath = SchemaPath.create(path, true);
407
408         assertEquals(expectedPath, dev.getTargetPath());
409         assertEquals(Deviate.ADD, dev.getDeviate());
410     }
411
412 }