2 * Copyright (c) 2016 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.stmt;
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 static org.opendaylight.yangtools.yang.stmt.StmtTestUtils.sourceForResource;
16 import com.google.common.collect.Iterables;
17 import com.google.common.collect.Range;
19 import java.util.ArrayList;
20 import java.util.Collection;
21 import java.util.List;
23 import java.util.Optional;
25 import org.junit.Test;
26 import org.opendaylight.yangtools.yang.common.QName;
27 import org.opendaylight.yangtools.yang.common.Revision;
28 import org.opendaylight.yangtools.yang.model.api.AnyXmlSchemaNode;
29 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
30 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
31 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
32 import org.opendaylight.yangtools.yang.model.api.DeviateKind;
33 import org.opendaylight.yangtools.yang.model.api.Deviation;
34 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
35 import org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode;
36 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
37 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
38 import org.opendaylight.yangtools.yang.model.api.Module;
39 import org.opendaylight.yangtools.yang.model.api.MustDefinition;
40 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
41 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
42 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
43 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
44 import org.opendaylight.yangtools.yang.model.api.UsesNode;
45 import org.opendaylight.yangtools.yang.model.api.type.Uint16TypeDefinition;
46 import org.opendaylight.yangtools.yang.model.api.type.Uint8TypeDefinition;
47 import org.opendaylight.yangtools.yang.parser.impl.DefaultReactors;
48 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
49 import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource;
51 public class YangParserWithContextTest {
52 private static final URI T1_NS = URI.create("urn:simple.demo.test1");
53 private static final URI T2_NS = URI.create("urn:simple.demo.test2");
54 private static final URI T3_NS = URI.create("urn:simple.demo.test3");
55 private static final Revision REV = Revision.of("2013-06-18");
57 private static final StatementStreamSource BAR = sourceForResource("/model/bar.yang");
58 private static final StatementStreamSource BAZ = sourceForResource("/model/baz.yang");
59 private static final StatementStreamSource FOO = sourceForResource("/model/foo.yang");
60 private static final StatementStreamSource SUBFOO = sourceForResource("/model/subfoo.yang");
62 private static final StatementStreamSource[] IETF = new StatementStreamSource[] {
63 sourceForResource("/ietf/iana-afn-safi@2012-06-04.yang"),
64 sourceForResource("/ietf/iana-if-type@2012-06-05.yang"),
65 sourceForResource("/ietf/iana-timezones@2012-07-09.yang"),
66 sourceForResource("/ietf/ietf-inet-types@2010-09-24.yang"),
67 sourceForResource("/ietf/ietf-yang-types@2010-09-24.yang"),
68 sourceForResource("/ietf/network-topology@2013-07-12.yang"),
69 sourceForResource("/ietf/network-topology@2013-10-21.yang") };
72 public void testTypeFromContext() throws ReactorException {
73 final SchemaContext context = DefaultReactors.defaultReactor().newBuild()
75 .addSource(sourceForResource("/types/custom-types-test@2012-04-04.yang"))
76 .addSource(sourceForResource("/context-test/test1.yang"))
79 final Module module = context.findModule("test1", Revision.of("2013-06-18")).get();
80 final LeafSchemaNode leaf = (LeafSchemaNode) module.getDataChildByName(QName.create(module.getQNameModule(),
83 assertTrue(leaf.getType() instanceof Uint16TypeDefinition);
84 final Uint16TypeDefinition leafType = (Uint16TypeDefinition) leaf.getType();
85 QName qname = leafType.getQName();
86 assertEquals(URI.create("urn:simple.demo.test1"), qname.getNamespace());
87 assertEquals(Revision.ofNullable("2013-06-18"), qname.getRevision());
88 assertEquals("port-number", qname.getLocalName());
90 final Uint16TypeDefinition leafBaseType = leafType.getBaseType();
91 qname = leafBaseType.getQName();
92 assertEquals(URI.create("urn:ietf:params:xml:ns:yang:ietf-inet-types"), qname.getNamespace());
93 assertEquals(Revision.ofNullable("2010-09-24"), qname.getRevision());
94 assertEquals("port-number", qname.getLocalName());
96 final Uint8TypeDefinition dscpExt = (Uint8TypeDefinition) TestUtils.findTypedef(module.getTypeDefinitions(),
98 final Set<? extends Range<?>> ranges = dscpExt.getRangeConstraint().get().getAllowedRanges().asRanges();
99 assertEquals(1, ranges.size());
100 final Range<?> range = ranges.iterator().next();
101 assertEquals((short)0, range.lowerEndpoint());
102 assertEquals((short)63, range.upperEndpoint());
106 public void testUsesFromContext() throws ReactorException {
107 final SchemaContext context = DefaultReactors.defaultReactor().newBuild()
108 .addSources(BAZ, FOO, BAR, SUBFOO, sourceForResource("/context-test/test2.yang"))
111 final Module testModule = context.findModule("test2", Revision.of("2013-06-18")).get();
112 final Module contextModule = context.findModules(URI.create("urn:opendaylight.baz")).iterator().next();
113 assertNotNull(contextModule);
114 final Set<GroupingDefinition> groupings = contextModule.getGroupings();
115 assertEquals(1, groupings.size());
116 final GroupingDefinition grouping = groupings.iterator().next();
118 // get node containing uses
119 final ContainerSchemaNode peer = (ContainerSchemaNode) testModule.getDataChildByName(QName.create(
120 testModule.getQNameModule(), "peer"));
121 final ContainerSchemaNode destination = (ContainerSchemaNode) peer.getDataChildByName(QName.create(
122 testModule.getQNameModule(), "destination"));
125 final Set<UsesNode> uses = destination.getUses();
126 assertEquals(1, uses.size());
128 // check uses process
129 final AnyXmlSchemaNode data_u = (AnyXmlSchemaNode) destination.getDataChildByName(QName.create(
130 testModule.getQNameModule(), "data"));
131 assertNotNull(data_u);
132 assertTrue(data_u.isAddedByUses());
134 final AnyXmlSchemaNode data_g = (AnyXmlSchemaNode) grouping.getDataChildByName(QName.create(
135 contextModule.getQNameModule(), "data"));
136 assertNotNull(data_g);
137 assertFalse(data_g.isAddedByUses());
138 assertFalse(data_u.equals(data_g));
140 final ChoiceSchemaNode how_u = (ChoiceSchemaNode) destination.getDataChildByName(QName.create(
141 testModule.getQNameModule(), "how"));
142 assertNotNull(how_u);
143 assertTrue(how_u.isAddedByUses());
145 final ChoiceSchemaNode how_g = (ChoiceSchemaNode) grouping.getDataChildByName(QName.create(
146 contextModule.getQNameModule(), "how"));
147 assertNotNull(how_g);
148 assertFalse(how_g.isAddedByUses());
149 assertFalse(how_u.equals(how_g));
151 final LeafSchemaNode address_u = (LeafSchemaNode) destination.getDataChildByName(QName.create(
152 testModule.getQNameModule(), "address"));
153 assertNotNull(address_u);
154 assertTrue(address_u.isAddedByUses());
156 final LeafSchemaNode address_g = (LeafSchemaNode) grouping.getDataChildByName(QName.create(
157 contextModule.getQNameModule(), "address"));
158 assertNotNull(address_g);
159 assertFalse(address_g.isAddedByUses());
160 assertFalse(address_u.equals(address_g));
162 final ContainerSchemaNode port_u = (ContainerSchemaNode) destination.getDataChildByName(QName.create(
163 testModule.getQNameModule(), "port"));
164 assertNotNull(port_u);
165 assertTrue(port_u.isAddedByUses());
167 final ContainerSchemaNode port_g = (ContainerSchemaNode) grouping.getDataChildByName(QName.create(
168 contextModule.getQNameModule(), "port"));
169 assertNotNull(port_g);
170 assertFalse(port_g.isAddedByUses());
171 assertFalse(port_u.equals(port_g));
173 final ListSchemaNode addresses_u = (ListSchemaNode) destination.getDataChildByName(QName.create(
174 testModule.getQNameModule(), "addresses"));
175 assertNotNull(addresses_u);
176 assertTrue(addresses_u.isAddedByUses());
178 final ListSchemaNode addresses_g = (ListSchemaNode) grouping.getDataChildByName(QName.create(
179 contextModule.getQNameModule(), "addresses"));
180 assertNotNull(addresses_g);
181 assertFalse(addresses_g.isAddedByUses());
182 assertFalse(addresses_u.equals(addresses_g));
184 // grouping defined by 'uses'
185 final Set<GroupingDefinition> groupings_u = destination.getGroupings();
186 assertEquals(1, groupings_u.size());
187 final GroupingDefinition grouping_u = groupings_u.iterator().next();
188 assertTrue(grouping_u.isAddedByUses());
190 // grouping defined in 'grouping' node
191 final Set<GroupingDefinition> groupings_g = grouping.getGroupings();
192 assertEquals(1, groupings_g.size());
193 final GroupingDefinition grouping_g = groupings_g.iterator().next();
194 assertFalse(grouping_g.isAddedByUses());
195 assertFalse(grouping_u.equals(grouping_g));
197 final List<UnknownSchemaNode> nodes_u = destination.getUnknownSchemaNodes();
198 assertEquals(1, nodes_u.size());
199 final UnknownSchemaNode node_u = nodes_u.get(0);
200 assertTrue(node_u.isAddedByUses());
202 final List<UnknownSchemaNode> nodes_g = grouping.getUnknownSchemaNodes();
203 assertEquals(1, nodes_g.size());
204 final UnknownSchemaNode node_g = nodes_g.get(0);
205 assertFalse(node_g.isAddedByUses());
206 assertFalse(node_u.equals(node_g));
210 public void testUsesRefineFromContext() throws ReactorException {
211 final SchemaContext context = DefaultReactors.defaultReactor().newBuild()
212 .addSources(BAZ, FOO, BAR, SUBFOO, sourceForResource("/context-test/test2.yang"))
215 final Module module = context.findModule("test2", Revision.of("2013-06-18")).get();
216 final ContainerSchemaNode peer = (ContainerSchemaNode) module.getDataChildByName(QName.create(
217 module.getQNameModule(), "peer"));
218 final ContainerSchemaNode destination = (ContainerSchemaNode) peer.getDataChildByName(QName.create(
219 module.getQNameModule(), "destination"));
220 final Set<UsesNode> usesNodes = destination.getUses();
221 assertEquals(1, usesNodes.size());
222 final UsesNode usesNode = usesNodes.iterator().next();
224 // test grouping path
225 final List<QName> path = new ArrayList<>();
226 final QName qname = QName.create(URI.create("urn:opendaylight.baz"), Revision.of("2013-02-27"), "target");
228 final SchemaPath expectedPath = SchemaPath.create(path, true);
229 assertEquals(expectedPath, usesNode.getGroupingPath());
232 final Map<SchemaPath, SchemaNode> refines = usesNode.getRefines();
233 assertEquals(3, refines.size());
235 LeafSchemaNode refineLeaf = null;
236 ContainerSchemaNode refineContainer = null;
237 ListSchemaNode refineList = null;
238 for (final Map.Entry<SchemaPath, SchemaNode> entry : refines.entrySet()) {
239 final 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;
250 assertNotNull(refineLeaf);
251 assertEquals("address", refineLeaf.getQName().getLocalName());
252 assertEquals(Optional.of("description of address defined by refine"), refineLeaf.getDescription());
253 assertEquals(Optional.of("address reference added by refine"), refineLeaf.getReference());
254 assertFalse(refineLeaf.isConfiguration());
255 assertTrue(refineLeaf.isMandatory());
256 final Collection<MustDefinition> leafMustConstraints = refineLeaf.getConstraints().getMustConstraints();
257 assertEquals(1, leafMustConstraints.size());
258 final MustDefinition leafMust = leafMustConstraints.iterator().next();
259 assertEquals("ifType != 'ethernet' or (ifType = 'ethernet' and ifMTU = 1500)", leafMust.toString());
262 assertNotNull(refineContainer);
263 final Collection<MustDefinition> mustConstraints = refineContainer.getConstraints().getMustConstraints();
264 assertTrue(mustConstraints.isEmpty());
265 assertEquals(Optional.of("description of port defined by refine"), refineContainer.getDescription());
266 assertEquals(Optional.of("port reference added by refine"), refineContainer.getReference());
267 assertFalse(refineContainer.isConfiguration());
268 assertTrue(refineContainer.isPresenceContainer());
271 assertNotNull(refineList);
272 assertEquals(Optional.of("description of addresses defined by refine"), refineList.getDescription());
273 assertEquals(Optional.of("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());
280 public void testIdentity() throws ReactorException {
281 final SchemaContext context = DefaultReactors.defaultReactor().newBuild()
283 .addSource(sourceForResource("/types/custom-types-test@2012-04-04.yang"))
284 .addSource(sourceForResource("/context-test/test3.yang"))
287 final Module module = context.findModule("test3", Revision.of("2013-06-18")).get();
288 final Set<IdentitySchemaNode> identities = module.getIdentities();
289 assertEquals(1, identities.size());
291 final IdentitySchemaNode identity = identities.iterator().next();
292 final QName idQName = identity.getQName();
293 assertEquals(URI.create("urn:simple.demo.test3"), idQName.getNamespace());
294 assertEquals(Revision.ofNullable("2013-06-18"), idQName.getRevision());
295 assertEquals("pt", idQName.getLocalName());
297 final IdentitySchemaNode baseIdentity = Iterables.getOnlyElement(identity.getBaseIdentities());
298 final QName idBaseQName = baseIdentity.getQName();
299 assertEquals(URI.create("urn:custom.types.demo"), idBaseQName.getNamespace());
300 assertEquals(Revision.ofNullable("2012-04-16"), idBaseQName.getRevision());
301 assertEquals("service-type", idBaseQName.getLocalName());
305 public void testUnknownNodes() throws ReactorException {
306 final SchemaContext context = DefaultReactors.defaultReactor().newBuild()
308 .addSource(sourceForResource("/types/custom-types-test@2012-04-04.yang"))
309 .addSource(sourceForResource("/context-test/test3.yang"))
312 final Module module = context.findModule("test3", Revision.of("2013-06-18")).get();
313 final ContainerSchemaNode network = (ContainerSchemaNode) module.getDataChildByName(QName.create(
314 module.getQNameModule(), "network"));
315 final List<UnknownSchemaNode> unknownNodes = network.getUnknownSchemaNodes();
316 assertEquals(1, unknownNodes.size());
318 final UnknownSchemaNode un = unknownNodes.get(0);
319 final QName unType = un.getNodeType();
320 assertEquals(URI.create("urn:custom.types.demo"), unType.getNamespace());
321 assertEquals(Revision.ofNullable("2012-04-16"), unType.getRevision());
322 assertEquals("mountpoint", unType.getLocalName());
323 assertEquals("point", un.getNodeParameter());
324 assertNotNull(un.getExtensionDefinition());
328 public void testAugment() throws ReactorException {
329 final StatementStreamSource resource = sourceForResource("/context-augment-test/test4.yang");
330 final StatementStreamSource test1 = sourceForResource("/context-augment-test/test1.yang");
331 final StatementStreamSource test2 = sourceForResource("/context-augment-test/test2.yang");
332 final StatementStreamSource test3 = sourceForResource("/context-augment-test/test3.yang");
334 final SchemaContext context = TestUtils.parseYangSources(resource, test1, test2, test3);
335 final Module t4 = TestUtils.findModule(context, "test4").get();
336 final ContainerSchemaNode interfaces = (ContainerSchemaNode) t4.getDataChildByName(QName.create(
337 t4.getQNameModule(), "interfaces"));
338 final ListSchemaNode ifEntry = (ListSchemaNode) interfaces.getDataChildByName(QName.create(t4.getQNameModule(),
341 // test augmentation process
342 final ContainerSchemaNode augmentHolder = (ContainerSchemaNode) ifEntry.getDataChildByName(QName.create(T3_NS,
343 REV, "augment-holder"));
344 assertNotNull(augmentHolder);
345 final DataSchemaNode ds0 = augmentHolder.getDataChildByName(QName.create(T2_NS, REV, "ds0ChannelNumber"));
347 final DataSchemaNode interfaceId = augmentHolder.getDataChildByName(QName.create(T2_NS, REV, "interface-id"));
348 assertNotNull(interfaceId);
349 final DataSchemaNode higherLayerIf = augmentHolder.getDataChildByName(QName.create(T2_NS, REV,
351 assertNotNull(higherLayerIf);
352 final ContainerSchemaNode schemas = (ContainerSchemaNode) augmentHolder.getDataChildByName(QName.create(T2_NS,
354 assertNotNull(schemas);
355 assertNotNull(schemas.getDataChildByName(QName.create(T1_NS, REV, "id")));
357 // test augment target after augmentation: check if it is same instance
358 final ListSchemaNode ifEntryAfterAugment = (ListSchemaNode) interfaces.getDataChildByName(QName.create(
359 t4.getQNameModule(), "ifEntry"));
360 assertTrue(ifEntry == ifEntryAfterAugment);
364 public void testDeviation() throws ReactorException {
365 final SchemaContext context = DefaultReactors.defaultReactor().newBuild()
366 .addSource(sourceForResource("/model/bar.yang"))
367 .addSource(sourceForResource("/context-test/deviation-test.yang"))
370 final Module testModule = context.findModule("deviation-test", Revision.of("2013-02-27")).get();
371 final Set<Deviation> deviations = testModule.getDeviations();
372 assertEquals(1, deviations.size());
373 final Deviation dev = deviations.iterator().next();
375 assertEquals(Optional.of("system/user ref"), dev.getReference());
377 final URI expectedNS = URI.create("urn:opendaylight.bar");
378 final Revision expectedRev = Revision.of("2013-07-03");
379 final List<QName> path = new ArrayList<>();
380 path.add(QName.create(expectedNS, expectedRev, "interfaces"));
381 path.add(QName.create(expectedNS, expectedRev, "ifEntry"));
382 final SchemaPath expectedPath = SchemaPath.create(path, true);
384 assertEquals(expectedPath, dev.getTargetPath());
385 assertEquals(DeviateKind.ADD, dev.getDeviates().iterator().next().getDeviateType());