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.UnsignedIntegerTypeDefinition;
46 import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource;
47 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor;
48 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangInferencePipeline;
50 public class YangParserWithContextTest {
51 private static final URI T1_NS = URI.create("urn:simple.demo.test1");
52 private static final URI T2_NS = URI.create("urn:simple.demo.test2");
53 private static final URI T3_NS = URI.create("urn:simple.demo.test3");
54 private static final Revision REV = Revision.of("2013-06-18");
56 private static final StatementStreamSource BAR = sourceForResource("/model/bar.yang");
57 private static final StatementStreamSource BAZ = sourceForResource("/model/baz.yang");
58 private static final StatementStreamSource FOO = sourceForResource("/model/foo.yang");
59 private static final StatementStreamSource SUBFOO = sourceForResource("/model/subfoo.yang");
61 private static final StatementStreamSource[] IETF = new StatementStreamSource[] {
62 sourceForResource("/ietf/iana-afn-safi@2012-06-04.yang"),
63 sourceForResource("/ietf/iana-if-type@2012-06-05.yang"),
64 sourceForResource("/ietf/iana-timezones@2012-07-09.yang"),
65 sourceForResource("/ietf/ietf-inet-types@2010-09-24.yang"),
66 sourceForResource("/ietf/ietf-yang-types@2010-09-24.yang"),
67 sourceForResource("/ietf/network-topology@2013-07-12.yang"),
68 sourceForResource("/ietf/network-topology@2013-10-21.yang") };
71 public void testTypeFromContext() throws Exception {
73 final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
75 final StatementStreamSource types = sourceForResource("/types/custom-types-test@2012-04-04.yang");
76 final StatementStreamSource test1 = sourceForResource("/context-test/test1.yang");
78 reactor.addSources(IETF);
79 reactor.addSources(types, test1);
81 final SchemaContext context = reactor.buildEffective();
83 final Module module = context.findModule("test1", Revision.of("2013-06-18")).get();
84 final LeafSchemaNode leaf = (LeafSchemaNode) module.getDataChildByName(QName.create(module.getQNameModule(),
87 assertTrue(leaf.getType() instanceof UnsignedIntegerTypeDefinition);
88 final UnsignedIntegerTypeDefinition leafType = (UnsignedIntegerTypeDefinition) leaf.getType();
89 QName qname = leafType.getQName();
90 assertEquals(URI.create("urn:simple.demo.test1"), qname.getNamespace());
91 assertEquals(Revision.ofNullable("2013-06-18"), qname.getRevision());
92 assertEquals("port-number", qname.getLocalName());
94 final UnsignedIntegerTypeDefinition leafBaseType = leafType.getBaseType();
95 qname = leafBaseType.getQName();
96 assertEquals(URI.create("urn:ietf:params:xml:ns:yang:ietf-inet-types"), qname.getNamespace());
97 assertEquals(Revision.ofNullable("2010-09-24"), qname.getRevision());
98 assertEquals("port-number", qname.getLocalName());
100 final UnsignedIntegerTypeDefinition dscpExt = (UnsignedIntegerTypeDefinition) TestUtils.findTypedef(
101 module.getTypeDefinitions(), "dscp-ext");
102 final Set<? extends Range<?>> ranges = dscpExt.getRangeConstraint().get().getAllowedRanges().asRanges();
103 assertEquals(1, ranges.size());
104 final Range<?> range = ranges.iterator().next();
105 assertEquals((short)0, range.lowerEndpoint());
106 assertEquals((short)63, range.upperEndpoint());
110 public void testUsesFromContext() throws Exception {
111 final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
113 final StatementStreamSource test2 = sourceForResource("/context-test/test2.yang");
114 reactor.addSources(BAZ, FOO, BAR, SUBFOO, test2);
115 final SchemaContext context = reactor.buildEffective();
117 final Module testModule = context.findModule("test2", Revision.of("2013-06-18")).get();
118 final Module contextModule = context.findModules(URI.create("urn:opendaylight.baz")).iterator().next();
119 assertNotNull(contextModule);
120 final Set<GroupingDefinition> groupings = contextModule.getGroupings();
121 assertEquals(1, groupings.size());
122 final GroupingDefinition grouping = groupings.iterator().next();
124 // get node containing uses
125 final ContainerSchemaNode peer = (ContainerSchemaNode) testModule.getDataChildByName(QName.create(
126 testModule.getQNameModule(), "peer"));
127 final ContainerSchemaNode destination = (ContainerSchemaNode) peer.getDataChildByName(QName.create(
128 testModule.getQNameModule(), "destination"));
131 final Set<UsesNode> uses = destination.getUses();
132 assertEquals(1, uses.size());
134 // check uses process
135 final AnyXmlSchemaNode data_u = (AnyXmlSchemaNode) destination.getDataChildByName(QName.create(
136 testModule.getQNameModule(), "data"));
137 assertNotNull(data_u);
138 assertTrue(data_u.isAddedByUses());
140 final AnyXmlSchemaNode data_g = (AnyXmlSchemaNode) grouping.getDataChildByName(QName.create(
141 contextModule.getQNameModule(), "data"));
142 assertNotNull(data_g);
143 assertFalse(data_g.isAddedByUses());
144 assertFalse(data_u.equals(data_g));
146 final ChoiceSchemaNode how_u = (ChoiceSchemaNode) destination.getDataChildByName(QName.create(
147 testModule.getQNameModule(), "how"));
148 assertNotNull(how_u);
149 assertTrue(how_u.isAddedByUses());
151 final ChoiceSchemaNode how_g = (ChoiceSchemaNode) grouping.getDataChildByName(QName.create(
152 contextModule.getQNameModule(), "how"));
153 assertNotNull(how_g);
154 assertFalse(how_g.isAddedByUses());
155 assertFalse(how_u.equals(how_g));
157 final LeafSchemaNode address_u = (LeafSchemaNode) destination.getDataChildByName(QName.create(
158 testModule.getQNameModule(), "address"));
159 assertNotNull(address_u);
160 assertTrue(address_u.isAddedByUses());
162 final LeafSchemaNode address_g = (LeafSchemaNode) grouping.getDataChildByName(QName.create(
163 contextModule.getQNameModule(), "address"));
164 assertNotNull(address_g);
165 assertFalse(address_g.isAddedByUses());
166 assertFalse(address_u.equals(address_g));
168 final ContainerSchemaNode port_u = (ContainerSchemaNode) destination.getDataChildByName(QName.create(
169 testModule.getQNameModule(), "port"));
170 assertNotNull(port_u);
171 assertTrue(port_u.isAddedByUses());
173 final ContainerSchemaNode port_g = (ContainerSchemaNode) grouping.getDataChildByName(QName.create(
174 contextModule.getQNameModule(), "port"));
175 assertNotNull(port_g);
176 assertFalse(port_g.isAddedByUses());
177 assertFalse(port_u.equals(port_g));
179 final ListSchemaNode addresses_u = (ListSchemaNode) destination.getDataChildByName(QName.create(
180 testModule.getQNameModule(), "addresses"));
181 assertNotNull(addresses_u);
182 assertTrue(addresses_u.isAddedByUses());
184 final ListSchemaNode addresses_g = (ListSchemaNode) grouping.getDataChildByName(QName.create(
185 contextModule.getQNameModule(), "addresses"));
186 assertNotNull(addresses_g);
187 assertFalse(addresses_g.isAddedByUses());
188 assertFalse(addresses_u.equals(addresses_g));
190 // grouping defined by 'uses'
191 final Set<GroupingDefinition> groupings_u = destination.getGroupings();
192 assertEquals(1, groupings_u.size());
193 final GroupingDefinition grouping_u = groupings_u.iterator().next();
194 assertTrue(grouping_u.isAddedByUses());
196 // grouping defined in 'grouping' node
197 final Set<GroupingDefinition> groupings_g = grouping.getGroupings();
198 assertEquals(1, groupings_g.size());
199 final GroupingDefinition grouping_g = groupings_g.iterator().next();
200 assertFalse(grouping_g.isAddedByUses());
201 assertFalse(grouping_u.equals(grouping_g));
203 final List<UnknownSchemaNode> nodes_u = destination.getUnknownSchemaNodes();
204 assertEquals(1, nodes_u.size());
205 final UnknownSchemaNode node_u = nodes_u.get(0);
206 assertTrue(node_u.isAddedByUses());
208 final List<UnknownSchemaNode> nodes_g = grouping.getUnknownSchemaNodes();
209 assertEquals(1, nodes_g.size());
210 final UnknownSchemaNode node_g = nodes_g.get(0);
211 assertFalse(node_g.isAddedByUses());
212 assertFalse(node_u.equals(node_g));
216 public void testUsesRefineFromContext() throws Exception {
218 final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
220 final StatementStreamSource test2 = sourceForResource("/context-test/test2.yang");
221 reactor.addSources(BAZ, FOO, BAR, SUBFOO, test2);
222 final SchemaContext context = reactor.buildEffective();
224 final Module module = context.findModule("test2", Revision.of("2013-06-18")).get();
225 final ContainerSchemaNode peer = (ContainerSchemaNode) module.getDataChildByName(QName.create(
226 module.getQNameModule(), "peer"));
227 final ContainerSchemaNode destination = (ContainerSchemaNode) peer.getDataChildByName(QName.create(
228 module.getQNameModule(), "destination"));
229 final Set<UsesNode> usesNodes = destination.getUses();
230 assertEquals(1, usesNodes.size());
231 final UsesNode usesNode = usesNodes.iterator().next();
233 // test grouping path
234 final List<QName> path = new ArrayList<>();
235 final QName qname = QName.create(URI.create("urn:opendaylight.baz"), Revision.of("2013-02-27"), "target");
237 final SchemaPath expectedPath = SchemaPath.create(path, true);
238 assertEquals(expectedPath, usesNode.getGroupingPath());
241 final Map<SchemaPath, SchemaNode> refines = usesNode.getRefines();
242 assertEquals(3, refines.size());
244 LeafSchemaNode refineLeaf = null;
245 ContainerSchemaNode refineContainer = null;
246 ListSchemaNode refineList = null;
247 for (final Map.Entry<SchemaPath, SchemaNode> entry : refines.entrySet()) {
248 final SchemaNode value = entry.getValue();
249 if (value instanceof LeafSchemaNode) {
250 refineLeaf = (LeafSchemaNode) value;
251 } else if (value instanceof ContainerSchemaNode) {
252 refineContainer = (ContainerSchemaNode) value;
253 } else if (value instanceof ListSchemaNode) {
254 refineList = (ListSchemaNode) value;
259 assertNotNull(refineLeaf);
260 assertEquals("address", refineLeaf.getQName().getLocalName());
261 assertEquals(Optional.of("description of address defined by refine"), refineLeaf.getDescription());
262 assertEquals(Optional.of("address reference added by refine"), refineLeaf.getReference());
263 assertFalse(refineLeaf.isConfiguration());
264 assertTrue(refineLeaf.getConstraints().isMandatory());
265 final Collection<MustDefinition> leafMustConstraints = refineLeaf.getConstraints().getMustConstraints();
266 assertEquals(1, leafMustConstraints.size());
267 final MustDefinition leafMust = leafMustConstraints.iterator().next();
268 assertEquals("ifType != 'ethernet' or (ifType = 'ethernet' and ifMTU = 1500)", leafMust.toString());
271 assertNotNull(refineContainer);
272 final Collection<MustDefinition> mustConstraints = refineContainer.getConstraints().getMustConstraints();
273 assertTrue(mustConstraints.isEmpty());
274 assertEquals(Optional.of("description of port defined by refine"), refineContainer.getDescription());
275 assertEquals(Optional.of("port reference added by refine"), refineContainer.getReference());
276 assertFalse(refineContainer.isConfiguration());
277 assertTrue(refineContainer.isPresenceContainer());
280 assertNotNull(refineList);
281 assertEquals(Optional.of("description of addresses defined by refine"), refineList.getDescription());
282 assertEquals(Optional.of("addresses reference added by refine"), refineList.getReference());
283 assertFalse(refineList.isConfiguration());
284 assertEquals(2, (int) refineList.getConstraints().getMinElements());
285 assertEquals(12, (int) refineList.getConstraints().getMaxElements());
289 public void testIdentity() throws Exception {
291 final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
293 final StatementStreamSource types = sourceForResource("/types/custom-types-test@2012-04-04.yang");
294 final StatementStreamSource test3 = sourceForResource("/context-test/test3.yang");
296 reactor.addSources(IETF);
297 reactor.addSources(types, test3);
298 final SchemaContext context = reactor.buildEffective();
300 final Module module = context.findModule("test3", Revision.of("2013-06-18")).get();
301 final Set<IdentitySchemaNode> identities = module.getIdentities();
302 assertEquals(1, identities.size());
304 final IdentitySchemaNode identity = identities.iterator().next();
305 final QName idQName = identity.getQName();
306 assertEquals(URI.create("urn:simple.demo.test3"), idQName.getNamespace());
307 assertEquals(Revision.ofNullable("2013-06-18"), idQName.getRevision());
308 assertEquals("pt", idQName.getLocalName());
310 final IdentitySchemaNode baseIdentity = Iterables.getOnlyElement(identity.getBaseIdentities());
311 final QName idBaseQName = baseIdentity.getQName();
312 assertEquals(URI.create("urn:custom.types.demo"), idBaseQName.getNamespace());
313 assertEquals(Revision.ofNullable("2012-04-16"), idBaseQName.getRevision());
314 assertEquals("service-type", idBaseQName.getLocalName());
318 public void testUnknownNodes() throws Exception {
320 final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
322 final StatementStreamSource types = sourceForResource("/types/custom-types-test@2012-04-04.yang");
323 final StatementStreamSource test3 = sourceForResource("/context-test/test3.yang");
325 reactor.addSources(IETF);
326 reactor.addSources(types, test3);
328 final SchemaContext context = reactor.buildEffective();
330 final Module module = context.findModule("test3", Revision.of("2013-06-18")).get();
331 final ContainerSchemaNode network = (ContainerSchemaNode) module.getDataChildByName(QName.create(
332 module.getQNameModule(), "network"));
333 final List<UnknownSchemaNode> unknownNodes = network.getUnknownSchemaNodes();
334 assertEquals(1, unknownNodes.size());
336 final UnknownSchemaNode un = unknownNodes.get(0);
337 final QName unType = un.getNodeType();
338 assertEquals(URI.create("urn:custom.types.demo"), unType.getNamespace());
339 assertEquals(Revision.ofNullable("2012-04-16"), unType.getRevision());
340 assertEquals("mountpoint", unType.getLocalName());
341 assertEquals("point", un.getNodeParameter());
342 assertNotNull(un.getExtensionDefinition());
346 public void testAugment() throws Exception {
347 final StatementStreamSource resource = sourceForResource("/context-augment-test/test4.yang");
348 final StatementStreamSource test1 = sourceForResource("/context-augment-test/test1.yang");
349 final StatementStreamSource test2 = sourceForResource("/context-augment-test/test2.yang");
350 final StatementStreamSource test3 = sourceForResource("/context-augment-test/test3.yang");
352 final SchemaContext context = TestUtils.parseYangSources(resource, test1, test2, test3);
353 final Module t4 = TestUtils.findModule(context, "test4").get();
354 final ContainerSchemaNode interfaces = (ContainerSchemaNode) t4.getDataChildByName(QName.create(
355 t4.getQNameModule(), "interfaces"));
356 final ListSchemaNode ifEntry = (ListSchemaNode) interfaces.getDataChildByName(QName.create(t4.getQNameModule(),
359 // test augmentation process
360 final ContainerSchemaNode augmentHolder = (ContainerSchemaNode) ifEntry.getDataChildByName(QName.create(T3_NS,
361 REV, "augment-holder"));
362 assertNotNull(augmentHolder);
363 final DataSchemaNode ds0 = augmentHolder.getDataChildByName(QName.create(T2_NS, REV, "ds0ChannelNumber"));
365 final DataSchemaNode interfaceId = augmentHolder.getDataChildByName(QName.create(T2_NS, REV, "interface-id"));
366 assertNotNull(interfaceId);
367 final DataSchemaNode higherLayerIf = augmentHolder.getDataChildByName(QName.create(T2_NS, REV,
369 assertNotNull(higherLayerIf);
370 final ContainerSchemaNode schemas = (ContainerSchemaNode) augmentHolder.getDataChildByName(QName.create(T2_NS,
372 assertNotNull(schemas);
373 assertNotNull(schemas.getDataChildByName(QName.create(T1_NS, REV, "id")));
375 // test augment target after augmentation: check if it is same instance
376 final ListSchemaNode ifEntryAfterAugment = (ListSchemaNode) interfaces.getDataChildByName(QName.create(
377 t4.getQNameModule(), "ifEntry"));
378 assertTrue(ifEntry == ifEntryAfterAugment);
382 public void testDeviation() throws Exception {
384 final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
386 final StatementStreamSource bar = sourceForResource("/model/bar.yang");
387 final StatementStreamSource deviationTest = sourceForResource("/context-test/deviation-test.yang");
389 reactor.addSources(bar, deviationTest);
390 final SchemaContext context = reactor.buildEffective();
392 final Module testModule = context.findModule("deviation-test", Revision.of("2013-02-27")).get();
393 final Set<Deviation> deviations = testModule.getDeviations();
394 assertEquals(1, deviations.size());
395 final Deviation dev = deviations.iterator().next();
397 assertEquals(Optional.of("system/user ref"), dev.getReference());
399 final URI expectedNS = URI.create("urn:opendaylight.bar");
400 final Revision expectedRev = Revision.of("2013-07-03");
401 final List<QName> path = new ArrayList<>();
402 path.add(QName.create(expectedNS, expectedRev, "interfaces"));
403 path.add(QName.create(expectedNS, expectedRev, "ifEntry"));
404 final SchemaPath expectedPath = SchemaPath.create(path, true);
406 assertEquals(expectedPath, dev.getTargetPath());
407 assertEquals(DeviateKind.ADD, dev.getDeviates().iterator().next().getDeviateType());