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;
18 import java.util.Collection;
20 import java.util.Optional;
22 import org.junit.Test;
23 import org.opendaylight.yangtools.yang.common.QName;
24 import org.opendaylight.yangtools.yang.common.Revision;
25 import org.opendaylight.yangtools.yang.common.Uint8;
26 import org.opendaylight.yangtools.yang.common.XMLNamespace;
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.UsesNode;
43 import org.opendaylight.yangtools.yang.model.api.stmt.ContainerStatement;
44 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
45 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Descendant;
46 import org.opendaylight.yangtools.yang.model.api.stmt.UnrecognizedStatement;
47 import org.opendaylight.yangtools.yang.model.api.type.Uint16TypeDefinition;
48 import org.opendaylight.yangtools.yang.model.api.type.Uint8TypeDefinition;
49 import org.opendaylight.yangtools.yang.parser.rfc7950.reactor.RFC7950Reactors;
50 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
51 import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource;
53 public class YangParserWithContextTest {
54 private static final XMLNamespace T1_NS = XMLNamespace.of("urn:simple.demo.test1");
55 private static final XMLNamespace T2_NS = XMLNamespace.of("urn:simple.demo.test2");
56 private static final XMLNamespace T3_NS = XMLNamespace.of("urn:simple.demo.test3");
57 private static final Revision REV = Revision.of("2013-06-18");
59 private static final StatementStreamSource BAR = sourceForResource("/model/bar.yang");
60 private static final StatementStreamSource BAZ = sourceForResource("/model/baz.yang");
61 private static final StatementStreamSource FOO = sourceForResource("/model/foo.yang");
62 private static final StatementStreamSource SUBFOO = sourceForResource("/model/subfoo.yang");
64 private static final StatementStreamSource[] IETF = new StatementStreamSource[] {
65 sourceForResource("/ietf/iana-afn-safi@2012-06-04.yang"),
66 sourceForResource("/ietf/iana-if-type@2012-06-05.yang"),
67 sourceForResource("/ietf/iana-timezones@2012-07-09.yang"),
68 sourceForResource("/ietf/ietf-inet-types@2010-09-24.yang"),
69 sourceForResource("/ietf/ietf-yang-types@2010-09-24.yang"),
70 sourceForResource("/ietf/network-topology@2013-07-12.yang"),
71 sourceForResource("/ietf/network-topology@2013-10-21.yang") };
74 public void testTypeFromContext() throws ReactorException {
75 final SchemaContext context = RFC7950Reactors.defaultReactor().newBuild()
77 .addSource(sourceForResource("/types/custom-types-test@2012-04-04.yang"))
78 .addSource(sourceForResource("/context-test/test1.yang"))
81 final Module module = context.findModule("test1", Revision.of("2013-06-18")).get();
82 final LeafSchemaNode leaf = (LeafSchemaNode) module.getDataChildByName(QName.create(module.getQNameModule(),
85 assertTrue(leaf.getType() instanceof Uint16TypeDefinition);
86 final Uint16TypeDefinition leafType = (Uint16TypeDefinition) leaf.getType();
87 QName qname = leafType.getQName();
88 assertEquals(XMLNamespace.of("urn:simple.demo.test1"), qname.getNamespace());
89 assertEquals(Revision.ofNullable("2013-06-18"), qname.getRevision());
90 assertEquals("port-number", qname.getLocalName());
92 final Uint16TypeDefinition leafBaseType = leafType.getBaseType();
93 qname = leafBaseType.getQName();
94 assertEquals(XMLNamespace.of("urn:ietf:params:xml:ns:yang:ietf-inet-types"), qname.getNamespace());
95 assertEquals(Revision.ofNullable("2010-09-24"), qname.getRevision());
96 assertEquals("port-number", qname.getLocalName());
98 final Uint8TypeDefinition dscpExt = (Uint8TypeDefinition) TestUtils.findTypedef(module.getTypeDefinitions(),
100 final Set<? extends Range<?>> ranges = dscpExt.getRangeConstraint().get().getAllowedRanges().asRanges();
101 assertEquals(1, ranges.size());
102 final Range<?> range = ranges.iterator().next();
103 assertEquals(Uint8.valueOf(0), range.lowerEndpoint());
104 assertEquals(Uint8.valueOf(63), range.upperEndpoint());
108 public void testUsesFromContext() throws ReactorException {
109 final SchemaContext context = RFC7950Reactors.defaultReactor().newBuild()
110 .addSources(BAZ, FOO, BAR, SUBFOO, sourceForResource("/context-test/test2.yang"))
113 final Module testModule = context.findModule("test2", Revision.of("2013-06-18")).get();
114 final Module contextModule = context.findModules(XMLNamespace.of("urn:opendaylight.baz")).iterator().next();
115 assertNotNull(contextModule);
116 final Collection<? extends GroupingDefinition> groupings = contextModule.getGroupings();
117 assertEquals(1, groupings.size());
118 final GroupingDefinition grouping = groupings.iterator().next();
120 // get node containing uses
121 final ContainerSchemaNode peer = (ContainerSchemaNode) testModule.getDataChildByName(QName.create(
122 testModule.getQNameModule(), "peer"));
123 final ContainerSchemaNode destination = (ContainerSchemaNode) peer.getDataChildByName(QName.create(
124 testModule.getQNameModule(), "destination"));
127 final Collection<? extends UsesNode> uses = destination.getUses();
128 assertEquals(1, uses.size());
130 // check uses process
131 final AnyxmlSchemaNode data_u = (AnyxmlSchemaNode) destination.getDataChildByName(QName.create(
132 testModule.getQNameModule(), "data"));
133 assertNotNull(data_u);
134 assertTrue(data_u.isAddedByUses());
136 final AnyxmlSchemaNode data_g = (AnyxmlSchemaNode) grouping.getDataChildByName(QName.create(
137 contextModule.getQNameModule(), "data"));
138 assertNotNull(data_g);
139 assertFalse(data_g.isAddedByUses());
140 assertFalse(data_u.equals(data_g));
142 final ChoiceSchemaNode how_u = (ChoiceSchemaNode) destination.getDataChildByName(QName.create(
143 testModule.getQNameModule(), "how"));
144 assertNotNull(how_u);
145 assertTrue(how_u.isAddedByUses());
147 final ChoiceSchemaNode how_g = (ChoiceSchemaNode) grouping.getDataChildByName(QName.create(
148 contextModule.getQNameModule(), "how"));
149 assertNotNull(how_g);
150 assertFalse(how_g.isAddedByUses());
151 assertFalse(how_u.equals(how_g));
153 final LeafSchemaNode address_u = (LeafSchemaNode) destination.getDataChildByName(QName.create(
154 testModule.getQNameModule(), "address"));
155 assertNotNull(address_u);
156 assertTrue(address_u.isAddedByUses());
158 final LeafSchemaNode address_g = (LeafSchemaNode) grouping.getDataChildByName(QName.create(
159 contextModule.getQNameModule(), "address"));
160 assertNotNull(address_g);
161 assertFalse(address_g.isAddedByUses());
162 assertFalse(address_u.equals(address_g));
164 final ContainerSchemaNode port_u = (ContainerSchemaNode) destination.getDataChildByName(QName.create(
165 testModule.getQNameModule(), "port"));
166 assertNotNull(port_u);
167 assertTrue(port_u.isAddedByUses());
169 final ContainerSchemaNode port_g = (ContainerSchemaNode) grouping.getDataChildByName(QName.create(
170 contextModule.getQNameModule(), "port"));
171 assertNotNull(port_g);
172 assertFalse(port_g.isAddedByUses());
173 assertFalse(port_u.equals(port_g));
175 final ListSchemaNode addresses_u = (ListSchemaNode) destination.getDataChildByName(QName.create(
176 testModule.getQNameModule(), "addresses"));
177 assertNotNull(addresses_u);
178 assertTrue(addresses_u.isAddedByUses());
180 final ListSchemaNode addresses_g = (ListSchemaNode) grouping.getDataChildByName(QName.create(
181 contextModule.getQNameModule(), "addresses"));
182 assertNotNull(addresses_g);
183 assertFalse(addresses_g.isAddedByUses());
184 assertFalse(addresses_u.equals(addresses_g));
186 // grouping defined by 'uses'
187 final Collection<? extends GroupingDefinition> groupings_u = destination.getGroupings();
188 assertEquals(0, groupings_u.size());
190 // grouping defined in 'grouping' node
191 final Collection<? extends 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());
198 public void testUsesRefineFromContext() throws ReactorException {
199 final SchemaContext context = RFC7950Reactors.defaultReactor().newBuild()
200 .addSources(BAZ, FOO, BAR, SUBFOO, sourceForResource("/context-test/test2.yang"))
203 final Module module = context.findModule("test2", Revision.of("2013-06-18")).get();
204 final ContainerSchemaNode peer = (ContainerSchemaNode) module.getDataChildByName(QName.create(
205 module.getQNameModule(), "peer"));
206 final ContainerSchemaNode destination = (ContainerSchemaNode) peer.getDataChildByName(QName.create(
207 module.getQNameModule(), "destination"));
208 final Collection<? extends UsesNode> usesNodes = destination.getUses();
209 assertEquals(1, usesNodes.size());
210 final UsesNode usesNode = usesNodes.iterator().next();
212 // test grouping path
213 assertEquals(QName.create(XMLNamespace.of("urn:opendaylight.baz"), Revision.of("2013-02-27"), "target"),
214 usesNode.getSourceGrouping().getQName());
217 final Map<Descendant, SchemaNode> refines = usesNode.getRefines();
218 assertEquals(3, refines.size());
220 LeafSchemaNode refineLeaf = null;
221 ContainerSchemaNode refineContainer = null;
222 ListSchemaNode refineList = null;
223 for (final Map.Entry<Descendant, SchemaNode> entry : refines.entrySet()) {
224 final SchemaNode value = entry.getValue();
225 if (value instanceof LeafSchemaNode) {
226 refineLeaf = (LeafSchemaNode) value;
227 } else if (value instanceof ContainerSchemaNode) {
228 refineContainer = (ContainerSchemaNode) value;
229 } else if (value instanceof ListSchemaNode) {
230 refineList = (ListSchemaNode) value;
235 assertNotNull(refineLeaf);
236 assertEquals("address", refineLeaf.getQName().getLocalName());
237 assertEquals(Optional.of("description of address defined by refine"), refineLeaf.getDescription());
238 assertEquals(Optional.of("address reference added by refine"), refineLeaf.getReference());
239 assertEquals(Optional.of(Boolean.FALSE), refineLeaf.effectiveConfig());
240 assertTrue(refineLeaf.isMandatory());
241 final Collection<? extends MustDefinition> leafMustConstraints = refineLeaf.getMustConstraints();
242 assertEquals(1, leafMustConstraints.size());
243 final MustDefinition leafMust = leafMustConstraints.iterator().next();
244 assertEquals("ifType != 'ethernet' or (ifType = 'ethernet' and ifMTU = 1500)", leafMust.getXpath().toString());
247 assertNotNull(refineContainer);
248 final Collection<? extends MustDefinition> mustConstraints = refineContainer.getMustConstraints();
249 assertTrue(mustConstraints.isEmpty());
250 assertEquals(Optional.of("description of port defined by refine"), refineContainer.getDescription());
251 assertEquals(Optional.of("port reference added by refine"), refineContainer.getReference());
252 assertEquals(Optional.of(Boolean.FALSE), refineContainer.effectiveConfig());
253 assertTrue(refineContainer.isPresenceContainer());
256 assertNotNull(refineList);
257 assertEquals(Optional.of("description of addresses defined by refine"), refineList.getDescription());
258 assertEquals(Optional.of("addresses reference added by refine"), refineList.getReference());
259 assertEquals(Optional.of(Boolean.FALSE), refineList.effectiveConfig());
260 final ElementCountConstraint constraint = refineList.getElementCountConstraint().get();
261 assertEquals((Object) 2, constraint.getMinElements());
262 assertEquals((Object) 12, constraint.getMaxElements());
266 public void testIdentity() throws ReactorException {
267 final SchemaContext context = RFC7950Reactors.defaultReactor().newBuild()
269 .addSource(sourceForResource("/types/custom-types-test@2012-04-04.yang"))
270 .addSource(sourceForResource("/context-test/test3.yang"))
273 final Module module = context.findModule("test3", Revision.of("2013-06-18")).get();
274 final Collection<? extends IdentitySchemaNode> identities = module.getIdentities();
275 assertEquals(1, identities.size());
277 final IdentitySchemaNode identity = identities.iterator().next();
278 final QName idQName = identity.getQName();
279 assertEquals(XMLNamespace.of("urn:simple.demo.test3"), idQName.getNamespace());
280 assertEquals(Revision.ofNullable("2013-06-18"), idQName.getRevision());
281 assertEquals("pt", idQName.getLocalName());
283 final IdentitySchemaNode baseIdentity = Iterables.getOnlyElement(identity.getBaseIdentities());
284 final QName idBaseQName = baseIdentity.getQName();
285 assertEquals(XMLNamespace.of("urn:custom.types.demo"), idBaseQName.getNamespace());
286 assertEquals(Revision.ofNullable("2012-04-16"), idBaseQName.getRevision());
287 assertEquals("service-type", idBaseQName.getLocalName());
291 public void testUnknownNodes() throws ReactorException {
292 final SchemaContext context = RFC7950Reactors.defaultReactor().newBuild()
294 .addSource(sourceForResource("/types/custom-types-test@2012-04-04.yang"))
295 .addSource(sourceForResource("/context-test/test3.yang"))
298 final Module module = context.findModule("test3", Revision.of("2013-06-18")).get();
299 final ContainerStatement network = ((ContainerSchemaNode) module.getDataChildByName(
300 QName.create(module.getQNameModule(), "network"))).asEffectiveStatement().getDeclared();
301 final Collection<? extends UnrecognizedStatement> unknownNodes =
302 network.declaredSubstatements(UnrecognizedStatement.class);
303 assertEquals(1, unknownNodes.size());
305 final UnrecognizedStatement un = unknownNodes.iterator().next();
306 final QName unType = un.statementDefinition().getStatementName();
307 assertEquals(XMLNamespace.of("urn:custom.types.demo"), unType.getNamespace());
308 assertEquals(Revision.ofNullable("2012-04-16"), unType.getRevision());
309 assertEquals("mountpoint", unType.getLocalName());
310 assertEquals("point", un.argument());
314 public void testAugment() throws ReactorException {
315 final StatementStreamSource resource = sourceForResource("/context-augment-test/test4.yang");
316 final StatementStreamSource test1 = sourceForResource("/context-augment-test/test1.yang");
317 final StatementStreamSource test2 = sourceForResource("/context-augment-test/test2.yang");
318 final StatementStreamSource test3 = sourceForResource("/context-augment-test/test3.yang");
320 final SchemaContext context = TestUtils.parseYangSources(resource, test1, test2, test3);
321 final Module t4 = TestUtils.findModule(context, "test4").get();
322 final ContainerSchemaNode interfaces = (ContainerSchemaNode) t4.getDataChildByName(QName.create(
323 t4.getQNameModule(), "interfaces"));
324 final ListSchemaNode ifEntry = (ListSchemaNode) interfaces.getDataChildByName(QName.create(t4.getQNameModule(),
327 // test augmentation process
328 final ContainerSchemaNode augmentHolder = (ContainerSchemaNode) ifEntry.getDataChildByName(QName.create(T3_NS,
329 REV, "augment-holder"));
330 assertNotNull(augmentHolder);
331 final DataSchemaNode ds0 = augmentHolder.getDataChildByName(QName.create(T2_NS, REV, "ds0ChannelNumber"));
333 final DataSchemaNode interfaceId = augmentHolder.getDataChildByName(QName.create(T2_NS, REV, "interface-id"));
334 assertNotNull(interfaceId);
335 final DataSchemaNode higherLayerIf = augmentHolder.getDataChildByName(QName.create(T2_NS, REV,
337 assertNotNull(higherLayerIf);
338 final ContainerSchemaNode schemas = (ContainerSchemaNode) augmentHolder.getDataChildByName(QName.create(T2_NS,
340 assertNotNull(schemas);
341 assertNotNull(schemas.getDataChildByName(QName.create(T1_NS, REV, "id")));
343 // test augment target after augmentation: check if it is same instance
344 final ListSchemaNode ifEntryAfterAugment = (ListSchemaNode) interfaces.getDataChildByName(QName.create(
345 t4.getQNameModule(), "ifEntry"));
346 assertTrue(ifEntry == ifEntryAfterAugment);
350 public void testDeviation() throws ReactorException {
351 final SchemaContext context = RFC7950Reactors.defaultReactor().newBuild()
352 .addSource(sourceForResource("/model/bar.yang"))
353 .addSource(sourceForResource("/context-test/deviation-test.yang"))
356 final Module testModule = context.findModule("deviation-test", Revision.of("2013-02-27")).get();
357 final Collection<? extends Deviation> deviations = testModule.getDeviations();
358 assertEquals(1, deviations.size());
359 final Deviation dev = deviations.iterator().next();
361 assertEquals(Optional.of("system/user ref"), dev.getReference());
363 final XMLNamespace expectedNS = XMLNamespace.of("urn:opendaylight.bar");
364 final Revision expectedRev = Revision.of("2013-07-03");
366 assertEquals(Absolute.of(
367 QName.create(expectedNS, expectedRev, "interfaces"), QName.create(expectedNS, expectedRev, "ifEntry")),
368 dev.getTargetPath());
369 assertEquals(DeviateKind.ADD, dev.getDeviates().iterator().next().getDeviateType());