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.common.Uint8;
29 import org.opendaylight.yangtools.yang.model.api.AnyxmlSchemaNode;
30 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
31 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
32 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
33 import org.opendaylight.yangtools.yang.model.api.DeviateKind;
34 import org.opendaylight.yangtools.yang.model.api.Deviation;
35 import org.opendaylight.yangtools.yang.model.api.ElementCountConstraint;
36 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
37 import org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode;
38 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
39 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
40 import org.opendaylight.yangtools.yang.model.api.Module;
41 import org.opendaylight.yangtools.yang.model.api.MustDefinition;
42 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
43 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
44 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
45 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
46 import org.opendaylight.yangtools.yang.model.api.UsesNode;
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 URI T1_NS = URI.create("urn:simple.demo.test1");
55 private static final URI T2_NS = URI.create("urn:simple.demo.test2");
56 private static final URI T3_NS = URI.create("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(URI.create("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(URI.create("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(URI.create("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(1, groupings_u.size());
189 final GroupingDefinition grouping_u = groupings_u.iterator().next();
190 assertTrue(grouping_u.isAddedByUses());
192 // grouping defined in 'grouping' node
193 final Collection<? extends GroupingDefinition> groupings_g = grouping.getGroupings();
194 assertEquals(1, groupings_g.size());
195 final GroupingDefinition grouping_g = groupings_g.iterator().next();
196 assertFalse(grouping_g.isAddedByUses());
197 assertFalse(grouping_u.equals(grouping_g));
199 final Collection<? extends UnknownSchemaNode> nodes_u = destination.getUnknownSchemaNodes();
200 assertEquals(1, nodes_u.size());
201 final UnknownSchemaNode node_u = nodes_u.iterator().next();
202 assertTrue(node_u.isAddedByUses());
204 final Collection<? extends UnknownSchemaNode> nodes_g = grouping.getUnknownSchemaNodes();
205 assertEquals(1, nodes_g.size());
206 final UnknownSchemaNode node_g = nodes_g.iterator().next();
207 assertFalse(node_g.isAddedByUses());
208 assertFalse(node_u.equals(node_g));
212 public void testUsesRefineFromContext() throws ReactorException {
213 final SchemaContext context = RFC7950Reactors.defaultReactor().newBuild()
214 .addSources(BAZ, FOO, BAR, SUBFOO, sourceForResource("/context-test/test2.yang"))
217 final Module module = context.findModule("test2", Revision.of("2013-06-18")).get();
218 final ContainerSchemaNode peer = (ContainerSchemaNode) module.getDataChildByName(QName.create(
219 module.getQNameModule(), "peer"));
220 final ContainerSchemaNode destination = (ContainerSchemaNode) peer.getDataChildByName(QName.create(
221 module.getQNameModule(), "destination"));
222 final Collection<? extends UsesNode> usesNodes = destination.getUses();
223 assertEquals(1, usesNodes.size());
224 final UsesNode usesNode = usesNodes.iterator().next();
226 // test grouping path
227 final List<QName> path = new ArrayList<>();
228 final QName qname = QName.create(URI.create("urn:opendaylight.baz"), Revision.of("2013-02-27"), "target");
230 final SchemaPath expectedPath = SchemaPath.create(path, true);
231 assertEquals(expectedPath, usesNode.getGroupingPath());
234 final Map<SchemaPath, SchemaNode> refines = usesNode.getRefines();
235 assertEquals(3, refines.size());
237 LeafSchemaNode refineLeaf = null;
238 ContainerSchemaNode refineContainer = null;
239 ListSchemaNode refineList = null;
240 for (final Map.Entry<SchemaPath, SchemaNode> entry : refines.entrySet()) {
241 final SchemaNode value = entry.getValue();
242 if (value instanceof LeafSchemaNode) {
243 refineLeaf = (LeafSchemaNode) value;
244 } else if (value instanceof ContainerSchemaNode) {
245 refineContainer = (ContainerSchemaNode) value;
246 } else if (value instanceof ListSchemaNode) {
247 refineList = (ListSchemaNode) value;
252 assertNotNull(refineLeaf);
253 assertEquals("address", refineLeaf.getQName().getLocalName());
254 assertEquals(Optional.of("description of address defined by refine"), refineLeaf.getDescription());
255 assertEquals(Optional.of("address reference added by refine"), refineLeaf.getReference());
256 assertFalse(refineLeaf.isConfiguration());
257 assertTrue(refineLeaf.isMandatory());
258 final Collection<? extends MustDefinition> leafMustConstraints = refineLeaf.getMustConstraints();
259 assertEquals(1, leafMustConstraints.size());
260 final MustDefinition leafMust = leafMustConstraints.iterator().next();
261 assertEquals("ifType != 'ethernet' or (ifType = 'ethernet' and ifMTU = 1500)", leafMust.toString());
264 assertNotNull(refineContainer);
265 final Collection<? extends MustDefinition> mustConstraints = refineContainer.getMustConstraints();
266 assertTrue(mustConstraints.isEmpty());
267 assertEquals(Optional.of("description of port defined by refine"), refineContainer.getDescription());
268 assertEquals(Optional.of("port reference added by refine"), refineContainer.getReference());
269 assertFalse(refineContainer.isConfiguration());
270 assertTrue(refineContainer.isPresenceContainer());
273 assertNotNull(refineList);
274 assertEquals(Optional.of("description of addresses defined by refine"), refineList.getDescription());
275 assertEquals(Optional.of("addresses reference added by refine"), refineList.getReference());
276 assertFalse(refineList.isConfiguration());
277 final ElementCountConstraint constraint = refineList.getElementCountConstraint().get();
278 assertEquals(2, constraint.getMinElements().intValue());
279 assertEquals(12, constraint.getMaxElements().intValue());
283 public void testIdentity() throws ReactorException {
284 final SchemaContext context = RFC7950Reactors.defaultReactor().newBuild()
286 .addSource(sourceForResource("/types/custom-types-test@2012-04-04.yang"))
287 .addSource(sourceForResource("/context-test/test3.yang"))
290 final Module module = context.findModule("test3", Revision.of("2013-06-18")).get();
291 final Collection<? extends IdentitySchemaNode> identities = module.getIdentities();
292 assertEquals(1, identities.size());
294 final IdentitySchemaNode identity = identities.iterator().next();
295 final QName idQName = identity.getQName();
296 assertEquals(URI.create("urn:simple.demo.test3"), idQName.getNamespace());
297 assertEquals(Revision.ofNullable("2013-06-18"), idQName.getRevision());
298 assertEquals("pt", idQName.getLocalName());
300 final IdentitySchemaNode baseIdentity = Iterables.getOnlyElement(identity.getBaseIdentities());
301 final QName idBaseQName = baseIdentity.getQName();
302 assertEquals(URI.create("urn:custom.types.demo"), idBaseQName.getNamespace());
303 assertEquals(Revision.ofNullable("2012-04-16"), idBaseQName.getRevision());
304 assertEquals("service-type", idBaseQName.getLocalName());
308 public void testUnknownNodes() throws ReactorException {
309 final SchemaContext context = RFC7950Reactors.defaultReactor().newBuild()
311 .addSource(sourceForResource("/types/custom-types-test@2012-04-04.yang"))
312 .addSource(sourceForResource("/context-test/test3.yang"))
315 final Module module = context.findModule("test3", Revision.of("2013-06-18")).get();
316 final ContainerSchemaNode network = (ContainerSchemaNode) module.getDataChildByName(QName.create(
317 module.getQNameModule(), "network"));
318 final Collection<? extends UnknownSchemaNode> unknownNodes = network.getUnknownSchemaNodes();
319 assertEquals(1, unknownNodes.size());
321 final UnknownSchemaNode un = unknownNodes.iterator().next();
322 final QName unType = un.getNodeType();
323 assertEquals(URI.create("urn:custom.types.demo"), unType.getNamespace());
324 assertEquals(Revision.ofNullable("2012-04-16"), unType.getRevision());
325 assertEquals("mountpoint", unType.getLocalName());
326 assertEquals("point", un.getNodeParameter());
327 assertNotNull(un.getExtensionDefinition());
331 public void testAugment() throws ReactorException {
332 final StatementStreamSource resource = sourceForResource("/context-augment-test/test4.yang");
333 final StatementStreamSource test1 = sourceForResource("/context-augment-test/test1.yang");
334 final StatementStreamSource test2 = sourceForResource("/context-augment-test/test2.yang");
335 final StatementStreamSource test3 = sourceForResource("/context-augment-test/test3.yang");
337 final SchemaContext context = TestUtils.parseYangSources(resource, test1, test2, test3);
338 final Module t4 = TestUtils.findModule(context, "test4").get();
339 final ContainerSchemaNode interfaces = (ContainerSchemaNode) t4.getDataChildByName(QName.create(
340 t4.getQNameModule(), "interfaces"));
341 final ListSchemaNode ifEntry = (ListSchemaNode) interfaces.getDataChildByName(QName.create(t4.getQNameModule(),
344 // test augmentation process
345 final ContainerSchemaNode augmentHolder = (ContainerSchemaNode) ifEntry.getDataChildByName(QName.create(T3_NS,
346 REV, "augment-holder"));
347 assertNotNull(augmentHolder);
348 final DataSchemaNode ds0 = augmentHolder.getDataChildByName(QName.create(T2_NS, REV, "ds0ChannelNumber"));
350 final DataSchemaNode interfaceId = augmentHolder.getDataChildByName(QName.create(T2_NS, REV, "interface-id"));
351 assertNotNull(interfaceId);
352 final DataSchemaNode higherLayerIf = augmentHolder.getDataChildByName(QName.create(T2_NS, REV,
354 assertNotNull(higherLayerIf);
355 final ContainerSchemaNode schemas = (ContainerSchemaNode) augmentHolder.getDataChildByName(QName.create(T2_NS,
357 assertNotNull(schemas);
358 assertNotNull(schemas.getDataChildByName(QName.create(T1_NS, REV, "id")));
360 // test augment target after augmentation: check if it is same instance
361 final ListSchemaNode ifEntryAfterAugment = (ListSchemaNode) interfaces.getDataChildByName(QName.create(
362 t4.getQNameModule(), "ifEntry"));
363 assertTrue(ifEntry == ifEntryAfterAugment);
367 public void testDeviation() throws ReactorException {
368 final SchemaContext context = RFC7950Reactors.defaultReactor().newBuild()
369 .addSource(sourceForResource("/model/bar.yang"))
370 .addSource(sourceForResource("/context-test/deviation-test.yang"))
373 final Module testModule = context.findModule("deviation-test", Revision.of("2013-02-27")).get();
374 final Collection<? extends Deviation> deviations = testModule.getDeviations();
375 assertEquals(1, deviations.size());
376 final Deviation dev = deviations.iterator().next();
378 assertEquals(Optional.of("system/user ref"), dev.getReference());
380 final URI expectedNS = URI.create("urn:opendaylight.bar");
381 final Revision expectedRev = Revision.of("2013-07-03");
382 final List<QName> path = new ArrayList<>();
383 path.add(QName.create(expectedNS, expectedRev, "interfaces"));
384 path.add(QName.create(expectedNS, expectedRev, "ifEntry"));
385 final SchemaPath expectedPath = SchemaPath.create(path, true);
387 assertEquals(expectedPath, dev.getTargetPath());
388 assertEquals(DeviateKind.ADD, dev.getDeviates().iterator().next().getDeviateType());