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.Collection;
21 import java.util.Optional;
23 import org.junit.Test;
24 import org.opendaylight.yangtools.yang.common.QName;
25 import org.opendaylight.yangtools.yang.common.Revision;
26 import org.opendaylight.yangtools.yang.common.Uint8;
27 import org.opendaylight.yangtools.yang.model.api.AnyxmlSchemaNode;
28 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
29 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
30 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
31 import org.opendaylight.yangtools.yang.model.api.DeviateKind;
32 import org.opendaylight.yangtools.yang.model.api.Deviation;
33 import org.opendaylight.yangtools.yang.model.api.ElementCountConstraint;
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.UnknownSchemaNode;
43 import org.opendaylight.yangtools.yang.model.api.UsesNode;
44 import org.opendaylight.yangtools.yang.model.api.stmt.ContainerStatement;
45 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
46 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Descendant;
47 import org.opendaylight.yangtools.yang.model.api.stmt.UnrecognizedStatement;
48 import org.opendaylight.yangtools.yang.model.api.type.Uint16TypeDefinition;
49 import org.opendaylight.yangtools.yang.model.api.type.Uint8TypeDefinition;
50 import org.opendaylight.yangtools.yang.parser.rfc7950.reactor.RFC7950Reactors;
51 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
52 import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource;
54 public class YangParserWithContextTest {
55 private static final URI T1_NS = URI.create("urn:simple.demo.test1");
56 private static final URI T2_NS = URI.create("urn:simple.demo.test2");
57 private static final URI T3_NS = URI.create("urn:simple.demo.test3");
58 private static final Revision REV = Revision.of("2013-06-18");
60 private static final StatementStreamSource BAR = sourceForResource("/model/bar.yang");
61 private static final StatementStreamSource BAZ = sourceForResource("/model/baz.yang");
62 private static final StatementStreamSource FOO = sourceForResource("/model/foo.yang");
63 private static final StatementStreamSource SUBFOO = sourceForResource("/model/subfoo.yang");
65 private static final StatementStreamSource[] IETF = new StatementStreamSource[] {
66 sourceForResource("/ietf/iana-afn-safi@2012-06-04.yang"),
67 sourceForResource("/ietf/iana-if-type@2012-06-05.yang"),
68 sourceForResource("/ietf/iana-timezones@2012-07-09.yang"),
69 sourceForResource("/ietf/ietf-inet-types@2010-09-24.yang"),
70 sourceForResource("/ietf/ietf-yang-types@2010-09-24.yang"),
71 sourceForResource("/ietf/network-topology@2013-07-12.yang"),
72 sourceForResource("/ietf/network-topology@2013-10-21.yang") };
75 public void testTypeFromContext() throws ReactorException {
76 final SchemaContext context = RFC7950Reactors.defaultReactor().newBuild()
78 .addSource(sourceForResource("/types/custom-types-test@2012-04-04.yang"))
79 .addSource(sourceForResource("/context-test/test1.yang"))
82 final Module module = context.findModule("test1", Revision.of("2013-06-18")).get();
83 final LeafSchemaNode leaf = (LeafSchemaNode) module.getDataChildByName(QName.create(module.getQNameModule(),
86 assertTrue(leaf.getType() instanceof Uint16TypeDefinition);
87 final Uint16TypeDefinition leafType = (Uint16TypeDefinition) leaf.getType();
88 QName qname = leafType.getQName();
89 assertEquals(URI.create("urn:simple.demo.test1"), qname.getNamespace());
90 assertEquals(Revision.ofNullable("2013-06-18"), qname.getRevision());
91 assertEquals("port-number", qname.getLocalName());
93 final Uint16TypeDefinition leafBaseType = leafType.getBaseType();
94 qname = leafBaseType.getQName();
95 assertEquals(URI.create("urn:ietf:params:xml:ns:yang:ietf-inet-types"), qname.getNamespace());
96 assertEquals(Revision.ofNullable("2010-09-24"), qname.getRevision());
97 assertEquals("port-number", qname.getLocalName());
99 final Uint8TypeDefinition dscpExt = (Uint8TypeDefinition) TestUtils.findTypedef(module.getTypeDefinitions(),
101 final Set<? extends Range<?>> ranges = dscpExt.getRangeConstraint().get().getAllowedRanges().asRanges();
102 assertEquals(1, ranges.size());
103 final Range<?> range = ranges.iterator().next();
104 assertEquals(Uint8.valueOf(0), range.lowerEndpoint());
105 assertEquals(Uint8.valueOf(63), range.upperEndpoint());
109 public void testUsesFromContext() throws ReactorException {
110 final SchemaContext context = RFC7950Reactors.defaultReactor().newBuild()
111 .addSources(BAZ, FOO, BAR, SUBFOO, sourceForResource("/context-test/test2.yang"))
114 final Module testModule = context.findModule("test2", Revision.of("2013-06-18")).get();
115 final Module contextModule = context.findModules(URI.create("urn:opendaylight.baz")).iterator().next();
116 assertNotNull(contextModule);
117 final Collection<? extends GroupingDefinition> groupings = contextModule.getGroupings();
118 assertEquals(1, groupings.size());
119 final GroupingDefinition grouping = groupings.iterator().next();
121 // get node containing uses
122 final ContainerSchemaNode peer = (ContainerSchemaNode) testModule.getDataChildByName(QName.create(
123 testModule.getQNameModule(), "peer"));
124 final ContainerSchemaNode destination = (ContainerSchemaNode) peer.getDataChildByName(QName.create(
125 testModule.getQNameModule(), "destination"));
128 final Collection<? extends UsesNode> uses = destination.getUses();
129 assertEquals(1, uses.size());
131 // check uses process
132 final AnyxmlSchemaNode data_u = (AnyxmlSchemaNode) destination.getDataChildByName(QName.create(
133 testModule.getQNameModule(), "data"));
134 assertNotNull(data_u);
135 assertTrue(data_u.isAddedByUses());
137 final AnyxmlSchemaNode data_g = (AnyxmlSchemaNode) grouping.getDataChildByName(QName.create(
138 contextModule.getQNameModule(), "data"));
139 assertNotNull(data_g);
140 assertFalse(data_g.isAddedByUses());
141 assertFalse(data_u.equals(data_g));
143 final ChoiceSchemaNode how_u = (ChoiceSchemaNode) destination.getDataChildByName(QName.create(
144 testModule.getQNameModule(), "how"));
145 assertNotNull(how_u);
146 assertTrue(how_u.isAddedByUses());
148 final ChoiceSchemaNode how_g = (ChoiceSchemaNode) grouping.getDataChildByName(QName.create(
149 contextModule.getQNameModule(), "how"));
150 assertNotNull(how_g);
151 assertFalse(how_g.isAddedByUses());
152 assertFalse(how_u.equals(how_g));
154 final LeafSchemaNode address_u = (LeafSchemaNode) destination.getDataChildByName(QName.create(
155 testModule.getQNameModule(), "address"));
156 assertNotNull(address_u);
157 assertTrue(address_u.isAddedByUses());
159 final LeafSchemaNode address_g = (LeafSchemaNode) grouping.getDataChildByName(QName.create(
160 contextModule.getQNameModule(), "address"));
161 assertNotNull(address_g);
162 assertFalse(address_g.isAddedByUses());
163 assertFalse(address_u.equals(address_g));
165 final ContainerSchemaNode port_u = (ContainerSchemaNode) destination.getDataChildByName(QName.create(
166 testModule.getQNameModule(), "port"));
167 assertNotNull(port_u);
168 assertTrue(port_u.isAddedByUses());
170 final ContainerSchemaNode port_g = (ContainerSchemaNode) grouping.getDataChildByName(QName.create(
171 contextModule.getQNameModule(), "port"));
172 assertNotNull(port_g);
173 assertFalse(port_g.isAddedByUses());
174 assertFalse(port_u.equals(port_g));
176 final ListSchemaNode addresses_u = (ListSchemaNode) destination.getDataChildByName(QName.create(
177 testModule.getQNameModule(), "addresses"));
178 assertNotNull(addresses_u);
179 assertTrue(addresses_u.isAddedByUses());
181 final ListSchemaNode addresses_g = (ListSchemaNode) grouping.getDataChildByName(QName.create(
182 contextModule.getQNameModule(), "addresses"));
183 assertNotNull(addresses_g);
184 assertFalse(addresses_g.isAddedByUses());
185 assertFalse(addresses_u.equals(addresses_g));
187 // grouping defined by 'uses'
188 final Collection<? extends GroupingDefinition> groupings_u = destination.getGroupings();
189 assertEquals(0, groupings_u.size());
191 // grouping defined in 'grouping' node
192 final Collection<? extends GroupingDefinition> groupings_g = grouping.getGroupings();
193 assertEquals(1, groupings_g.size());
194 final GroupingDefinition grouping_g = groupings_g.iterator().next();
195 assertFalse(grouping_g.isAddedByUses());
197 final Collection<? extends UnknownSchemaNode> nodes_u = destination.getUnknownSchemaNodes();
198 assertEquals(1, nodes_u.size());
199 final UnknownSchemaNode node_u = nodes_u.iterator().next();
200 assertTrue(node_u.isAddedByUses());
202 final Collection<? extends UnknownSchemaNode> nodes_g = grouping.getUnknownSchemaNodes();
203 assertEquals(1, nodes_g.size());
204 final UnknownSchemaNode node_g = nodes_g.iterator().next();
205 assertFalse(node_g.isAddedByUses());
206 assertFalse(node_u.equals(node_g));
210 public void testUsesRefineFromContext() throws ReactorException {
211 final SchemaContext context = RFC7950Reactors.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 Collection<? extends UsesNode> usesNodes = destination.getUses();
221 assertEquals(1, usesNodes.size());
222 final UsesNode usesNode = usesNodes.iterator().next();
224 // test grouping path
225 assertEquals(QName.create(URI.create("urn:opendaylight.baz"), Revision.of("2013-02-27"), "target"),
226 usesNode.getSourceGrouping().getQName());
229 final Map<Descendant, SchemaNode> refines = usesNode.getRefines();
230 assertEquals(3, refines.size());
232 LeafSchemaNode refineLeaf = null;
233 ContainerSchemaNode refineContainer = null;
234 ListSchemaNode refineList = null;
235 for (final Map.Entry<Descendant, SchemaNode> entry : refines.entrySet()) {
236 final SchemaNode value = entry.getValue();
237 if (value instanceof LeafSchemaNode) {
238 refineLeaf = (LeafSchemaNode) value;
239 } else if (value instanceof ContainerSchemaNode) {
240 refineContainer = (ContainerSchemaNode) value;
241 } else if (value instanceof ListSchemaNode) {
242 refineList = (ListSchemaNode) value;
247 assertNotNull(refineLeaf);
248 assertEquals("address", refineLeaf.getQName().getLocalName());
249 assertEquals(Optional.of("description of address defined by refine"), refineLeaf.getDescription());
250 assertEquals(Optional.of("address reference added by refine"), refineLeaf.getReference());
251 assertEquals(Optional.of(Boolean.FALSE), refineLeaf.effectiveConfig());
252 assertTrue(refineLeaf.isMandatory());
253 final Collection<? extends MustDefinition> leafMustConstraints = refineLeaf.getMustConstraints();
254 assertEquals(1, leafMustConstraints.size());
255 final MustDefinition leafMust = leafMustConstraints.iterator().next();
256 assertEquals("ifType != 'ethernet' or (ifType = 'ethernet' and ifMTU = 1500)", leafMust.getXpath().toString());
259 assertNotNull(refineContainer);
260 final Collection<? extends MustDefinition> mustConstraints = refineContainer.getMustConstraints();
261 assertTrue(mustConstraints.isEmpty());
262 assertEquals(Optional.of("description of port defined by refine"), refineContainer.getDescription());
263 assertEquals(Optional.of("port reference added by refine"), refineContainer.getReference());
264 assertEquals(Optional.of(Boolean.FALSE), refineContainer.effectiveConfig());
265 assertTrue(refineContainer.isPresenceContainer());
268 assertNotNull(refineList);
269 assertEquals(Optional.of("description of addresses defined by refine"), refineList.getDescription());
270 assertEquals(Optional.of("addresses reference added by refine"), refineList.getReference());
271 assertEquals(Optional.of(Boolean.FALSE), refineList.effectiveConfig());
272 final ElementCountConstraint constraint = refineList.getElementCountConstraint().get();
273 assertEquals((Object) 2, constraint.getMinElements());
274 assertEquals((Object) 12, constraint.getMaxElements());
278 public void testIdentity() throws ReactorException {
279 final SchemaContext context = RFC7950Reactors.defaultReactor().newBuild()
281 .addSource(sourceForResource("/types/custom-types-test@2012-04-04.yang"))
282 .addSource(sourceForResource("/context-test/test3.yang"))
285 final Module module = context.findModule("test3", Revision.of("2013-06-18")).get();
286 final Collection<? extends IdentitySchemaNode> identities = module.getIdentities();
287 assertEquals(1, identities.size());
289 final IdentitySchemaNode identity = identities.iterator().next();
290 final QName idQName = identity.getQName();
291 assertEquals(URI.create("urn:simple.demo.test3"), idQName.getNamespace());
292 assertEquals(Revision.ofNullable("2013-06-18"), idQName.getRevision());
293 assertEquals("pt", idQName.getLocalName());
295 final IdentitySchemaNode baseIdentity = Iterables.getOnlyElement(identity.getBaseIdentities());
296 final QName idBaseQName = baseIdentity.getQName();
297 assertEquals(URI.create("urn:custom.types.demo"), idBaseQName.getNamespace());
298 assertEquals(Revision.ofNullable("2012-04-16"), idBaseQName.getRevision());
299 assertEquals("service-type", idBaseQName.getLocalName());
303 public void testUnknownNodes() throws ReactorException {
304 final SchemaContext context = RFC7950Reactors.defaultReactor().newBuild()
306 .addSource(sourceForResource("/types/custom-types-test@2012-04-04.yang"))
307 .addSource(sourceForResource("/context-test/test3.yang"))
310 final Module module = context.findModule("test3", Revision.of("2013-06-18")).get();
311 final ContainerStatement network = ((ContainerSchemaNode) module.getDataChildByName(
312 QName.create(module.getQNameModule(), "network"))).asEffectiveStatement().getDeclared();
313 final Collection<? extends UnrecognizedStatement> unknownNodes =
314 network.declaredSubstatements(UnrecognizedStatement.class);
315 assertEquals(1, unknownNodes.size());
317 final UnrecognizedStatement un = unknownNodes.iterator().next();
318 final QName unType = un.statementDefinition().getStatementName();
319 assertEquals(URI.create("urn:custom.types.demo"), unType.getNamespace());
320 assertEquals(Revision.ofNullable("2012-04-16"), unType.getRevision());
321 assertEquals("mountpoint", unType.getLocalName());
322 assertEquals("point", un.argument());
326 public void testAugment() throws ReactorException {
327 final StatementStreamSource resource = sourceForResource("/context-augment-test/test4.yang");
328 final StatementStreamSource test1 = sourceForResource("/context-augment-test/test1.yang");
329 final StatementStreamSource test2 = sourceForResource("/context-augment-test/test2.yang");
330 final StatementStreamSource test3 = sourceForResource("/context-augment-test/test3.yang");
332 final SchemaContext context = TestUtils.parseYangSources(resource, test1, test2, test3);
333 final Module t4 = TestUtils.findModule(context, "test4").get();
334 final ContainerSchemaNode interfaces = (ContainerSchemaNode) t4.getDataChildByName(QName.create(
335 t4.getQNameModule(), "interfaces"));
336 final ListSchemaNode ifEntry = (ListSchemaNode) interfaces.getDataChildByName(QName.create(t4.getQNameModule(),
339 // test augmentation process
340 final ContainerSchemaNode augmentHolder = (ContainerSchemaNode) ifEntry.getDataChildByName(QName.create(T3_NS,
341 REV, "augment-holder"));
342 assertNotNull(augmentHolder);
343 final DataSchemaNode ds0 = augmentHolder.getDataChildByName(QName.create(T2_NS, REV, "ds0ChannelNumber"));
345 final DataSchemaNode interfaceId = augmentHolder.getDataChildByName(QName.create(T2_NS, REV, "interface-id"));
346 assertNotNull(interfaceId);
347 final DataSchemaNode higherLayerIf = augmentHolder.getDataChildByName(QName.create(T2_NS, REV,
349 assertNotNull(higherLayerIf);
350 final ContainerSchemaNode schemas = (ContainerSchemaNode) augmentHolder.getDataChildByName(QName.create(T2_NS,
352 assertNotNull(schemas);
353 assertNotNull(schemas.getDataChildByName(QName.create(T1_NS, REV, "id")));
355 // test augment target after augmentation: check if it is same instance
356 final ListSchemaNode ifEntryAfterAugment = (ListSchemaNode) interfaces.getDataChildByName(QName.create(
357 t4.getQNameModule(), "ifEntry"));
358 assertTrue(ifEntry == ifEntryAfterAugment);
362 public void testDeviation() throws ReactorException {
363 final SchemaContext context = RFC7950Reactors.defaultReactor().newBuild()
364 .addSource(sourceForResource("/model/bar.yang"))
365 .addSource(sourceForResource("/context-test/deviation-test.yang"))
368 final Module testModule = context.findModule("deviation-test", Revision.of("2013-02-27")).get();
369 final Collection<? extends Deviation> deviations = testModule.getDeviations();
370 assertEquals(1, deviations.size());
371 final Deviation dev = deviations.iterator().next();
373 assertEquals(Optional.of("system/user ref"), dev.getReference());
375 final URI expectedNS = URI.create("urn:opendaylight.bar");
376 final Revision expectedRev = Revision.of("2013-07-03");
378 assertEquals(Absolute.of(
379 QName.create(expectedNS, expectedRev, "interfaces"), QName.create(expectedNS, expectedRev, "ifEntry")),
380 dev.getTargetPath());
381 assertEquals(DeviateKind.ADD, dev.getDeviates().iterator().next().getDeviateType());