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;
16 import java.util.ArrayList;
17 import java.util.Collection;
18 import java.util.Collections;
19 import java.util.List;
20 import org.junit.Test;
21 import org.opendaylight.yangtools.yang.common.QName;
22 import org.opendaylight.yangtools.yang.common.QNameModule;
23 import org.opendaylight.yangtools.yang.common.Revision;
24 import org.opendaylight.yangtools.yang.model.api.AugmentationSchemaNode;
25 import org.opendaylight.yangtools.yang.model.api.CaseSchemaNode;
26 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
27 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
28 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
29 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
30 import org.opendaylight.yangtools.yang.model.api.InputSchemaNode;
31 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
32 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
33 import org.opendaylight.yangtools.yang.model.api.Module;
34 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
35 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
36 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
37 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
38 import org.opendaylight.yangtools.yang.model.util.BaseTypes;
40 public class AugmentTest {
41 private static final QNameModule FOO = QNameModule.create(
42 URI.create("urn:opendaylight.foo"), Revision.of("2013-10-13"));
43 private static final QNameModule BAR = QNameModule.create(
44 URI.create("urn:opendaylight.bar"), Revision.of("2013-10-14"));
45 private static final QNameModule BAZ = QNameModule.create(
46 URI.create("urn:opendaylight.baz"), Revision.of("2013-10-15"));
48 private static final QName Q0 = QName.create(BAR, "interfaces");
49 private static final QName Q1 = QName.create(BAR, "ifEntry");
50 private static final QName Q2 = QName.create(BAZ, "augment-holder");
53 public void testAugmentParsing() throws Exception {
54 final SchemaContext context = TestUtils.loadModules(getClass().getResource("/augment-test/augment-in-augment")
56 final List<QName> qnames = new ArrayList<>();
62 final Module module1 = TestUtils.findModule(context, "foo").get();
63 Collection<? extends AugmentationSchemaNode> augmentations = module1.getAugmentations();
64 assertEquals(1, augmentations.size());
65 final AugmentationSchemaNode augment = augmentations.iterator().next();
66 assertNotNull(augment);
68 assertEquals(Absolute.of(qnames), augment.getTargetPath());
70 final Collection<? extends DataSchemaNode> augmentChildren = augment.getChildNodes();
71 assertEquals(4, augmentChildren.size());
72 for (final DataSchemaNode dsn : augmentChildren) {
73 TestUtils.checkIsAugmenting(dsn, false);
76 final LeafSchemaNode ds0ChannelNumber = (LeafSchemaNode) augment.getDataChildByName(QName.create(
77 module1.getQNameModule(), "ds0ChannelNumber"));
78 final LeafSchemaNode interfaceId = (LeafSchemaNode) augment.getDataChildByName(QName.create(
79 module1.getQNameModule(), "interface-id"));
80 final ContainerSchemaNode schemas = (ContainerSchemaNode) augment.getDataChildByName(QName.create(
81 module1.getQNameModule(), "schemas"));
82 final ChoiceSchemaNode odl = (ChoiceSchemaNode) augment.getDataChildByName(QName.create(
83 module1.getQNameModule(), "odl"));
85 assertNotNull(ds0ChannelNumber);
86 assertNotNull(interfaceId);
87 assertNotNull(schemas);
90 // leaf ds0ChannelNumber
91 QName qname = QName.create(FOO, "ds0ChannelNumber");
93 assertEquals(qname, ds0ChannelNumber.getQName());
94 SchemaPath expectedSchemaPath = SchemaPath.create(qnames, true);
95 assertEquals(expectedSchemaPath, ds0ChannelNumber.getPath());
96 assertFalse(ds0ChannelNumber.isAugmenting());
97 // type of leaf ds0ChannelNumber
98 final List<QName> typePath = Collections.singletonList(BaseTypes.STRING_QNAME);
99 expectedSchemaPath = SchemaPath.create(typePath, true);
100 assertEquals(expectedSchemaPath, ds0ChannelNumber.getType().getPath());
103 qname = QName.create(FOO, "interface-id");
104 assertEquals(qname, interfaceId.getQName());
105 qnames.set(3, qname);
106 expectedSchemaPath = SchemaPath.create(qnames, true);
107 assertEquals(expectedSchemaPath, interfaceId.getPath());
108 assertFalse(interfaceId.isAugmenting());
111 qname = QName.create(FOO, "schemas");
112 assertEquals(qname, schemas.getQName());
113 qnames.set(3, qname);
114 expectedSchemaPath = SchemaPath.create(qnames, true);
115 assertEquals(expectedSchemaPath, schemas.getPath());
116 assertFalse(schemas.isAugmenting());
119 qname = QName.create(FOO, "odl");
120 assertEquals(qname, odl.getQName());
121 qnames.set(3, qname);
122 expectedSchemaPath = SchemaPath.create(qnames, true);
123 assertEquals(expectedSchemaPath, odl.getPath());
124 assertFalse(odl.isAugmenting());
127 final Module module3 = TestUtils.findModule(context, "baz").get();
128 augmentations = module3.getAugmentations();
129 assertEquals(3, augmentations.size());
130 AugmentationSchemaNode augment1 = null;
131 AugmentationSchemaNode augment2 = null;
132 AugmentationSchemaNode augment3 = null;
133 for (final AugmentationSchemaNode as : augmentations) {
134 if (!as.getWhenCondition().isPresent()) {
136 } else if ("br:ifType='ds0'".equals(as.getWhenCondition().orElseThrow().toString())) {
138 } else if ("br:ifType='ds2'".equals(as.getWhenCondition().orElseThrow().toString())) {
142 assertNotNull(augment1);
143 assertNotNull(augment2);
144 assertNotNull(augment3);
146 assertEquals(1, augment1.getChildNodes().size());
147 final ContainerSchemaNode augmentHolder = (ContainerSchemaNode) augment1.getDataChildByName(QName.create(
148 module3.getQNameModule(), "augment-holder"));
149 assertNotNull(augmentHolder);
151 assertEquals(1, augment2.getChildNodes().size());
152 final ContainerSchemaNode augmentHolder2 = (ContainerSchemaNode) augment2.getDataChildByName(QName.create(
153 module3.getQNameModule(), "augment-holder2"));
154 assertNotNull(augmentHolder2);
156 assertEquals(1, augment3.getChildNodes().size());
157 final CaseSchemaNode pause = (CaseSchemaNode) augment3.getDataChildByName(QName.create(
158 module3.getQNameModule(), "pause"));
159 assertNotNull(pause);
163 public void testAugmentResolving() throws Exception {
164 final SchemaContext context = TestUtils.loadModules(getClass().getResource("/augment-test/augment-in-augment")
166 final Module module2 = TestUtils.findModule(context, "bar").get();
167 final ContainerSchemaNode interfaces = (ContainerSchemaNode) module2.getDataChildByName(QName.create(
168 module2.getQNameModule(), "interfaces"));
169 final ListSchemaNode ifEntry = (ListSchemaNode) interfaces.getDataChildByName(QName.create(
170 module2.getQNameModule(), "ifEntry"));
172 final List<QName> qnames = new ArrayList<>();
178 // augment "/br:interfaces/br:ifEntry" {
179 final ContainerSchemaNode augmentHolder = (ContainerSchemaNode) ifEntry.getDataChildByName(QName.create(BAZ,
181 TestUtils.checkIsAugmenting(augmentHolder, true);
182 assertEquals(Q2, augmentHolder.getQName());
183 assertEquals(SchemaPath.create(qnames, true), augmentHolder.getPath());
186 // augment "/br:interfaces/br:ifEntry/bz:augment-holder"
187 final LeafSchemaNode ds0ChannelNumber = (LeafSchemaNode) augmentHolder.getDataChildByName(QName.create(FOO,
188 "ds0ChannelNumber"));
189 final LeafSchemaNode interfaceId = (LeafSchemaNode) augmentHolder.getDataChildByName(QName.create(FOO,
191 final ContainerSchemaNode schemas = (ContainerSchemaNode) augmentHolder.getDataChildByName(QName.create(FOO,
193 final ChoiceSchemaNode odl = (ChoiceSchemaNode) augmentHolder.getDataChildByName(QName.create(FOO, "odl"));
195 assertNotNull(ds0ChannelNumber);
196 assertNotNull(interfaceId);
197 assertNotNull(schemas);
200 // leaf ds0ChannelNumber
201 QName qname = QName.create(FOO, "ds0ChannelNumber");
202 assertEquals(qname, ds0ChannelNumber.getQName());
204 assertEquals(SchemaPath.create(qnames, true), ds0ChannelNumber.getPath());
207 qname = QName.create(FOO, "interface-id");
208 assertEquals(qname, interfaceId.getQName());
209 qnames.set(3, qname);
210 assertEquals(SchemaPath.create(qnames, true), interfaceId.getPath());
213 qname = QName.create(FOO, "schemas");
214 assertEquals(qname, schemas.getQName());
215 qnames.set(3, qname);
216 assertEquals(SchemaPath.create(qnames, true), schemas.getPath());
219 qname = QName.create(FOO, "odl");
220 assertEquals(qname, odl.getQName());
221 qnames.set(3, qname);
222 assertEquals(SchemaPath.create(qnames, true), odl.getPath());
226 public void testAugmentedChoice() throws Exception {
227 final SchemaContext context = TestUtils.loadModules(getClass().getResource("/augment-test/augment-in-augment")
229 final Module module2 = TestUtils.findModule(context, "bar").get();
230 final ContainerSchemaNode interfaces = (ContainerSchemaNode) module2.getDataChildByName(QName.create(
231 module2.getQNameModule(), "interfaces"));
232 final ListSchemaNode ifEntry = (ListSchemaNode) interfaces.getDataChildByName(QName.create(
233 module2.getQNameModule(), "ifEntry"));
234 final ContainerSchemaNode augmentedHolder = (ContainerSchemaNode) ifEntry.getDataChildByName(QName.create(
235 BAZ, "augment-holder"));
236 TestUtils.checkIsAugmenting(augmentedHolder, true);
239 // augment "/br:interfaces/br:ifEntry/bz:augment-holder"
240 final ChoiceSchemaNode odl = (ChoiceSchemaNode) augmentedHolder.getDataChildByName(QName.create(FOO, "odl"));
242 final Collection<? extends CaseSchemaNode> cases = odl.getCases();
243 assertEquals(4, cases.size());
245 CaseSchemaNode id = null;
246 CaseSchemaNode node1 = null;
247 CaseSchemaNode node2 = null;
248 CaseSchemaNode node3 = null;
250 for (final CaseSchemaNode ccn : cases) {
251 if ("id".equals(ccn.getQName().getLocalName())) {
253 } else if ("node1".equals(ccn.getQName().getLocalName())) {
255 } else if ("node2".equals(ccn.getQName().getLocalName())) {
257 } else if ("node3".equals(ccn.getQName().getLocalName())) {
263 assertNotNull(node1);
264 assertNotNull(node2);
265 assertNotNull(node3);
267 final List<QName> qnames = new ArrayList<>();
271 qnames.add(QName.create(FOO, "odl"));
274 QName qname = QName.create(FOO, "id");
275 assertEquals(qname, id.getQName());
277 assertEquals(SchemaPath.create(qnames, true), id.getPath());
278 final Collection<? extends DataSchemaNode> idChildren = id.getChildNodes();
279 assertEquals(1, idChildren.size());
282 qname = QName.create(FOO, "node1");
283 assertEquals(qname, node1.getQName());
284 qnames.set(4, qname);
285 assertEquals(SchemaPath.create(qnames, true), node1.getPath());
286 final Collection<? extends DataSchemaNode> node1Children = node1.getChildNodes();
287 assertTrue(node1Children.isEmpty());
290 qname = QName.create(FOO, "node2");
291 assertEquals(qname, node2.getQName());
292 qnames.set(4, qname);
293 assertEquals(SchemaPath.create(qnames, true), node2.getPath());
294 final Collection<? extends DataSchemaNode> node2Children = node2.getChildNodes();
295 assertTrue(node2Children.isEmpty());
298 qname = QName.create(FOO, "node3");
299 assertEquals(qname, node3.getQName());
300 qnames.set(4, qname);
301 assertEquals(SchemaPath.create(qnames, true), node3.getPath());
302 final Collection<? extends DataSchemaNode> node3Children = node3.getChildNodes();
303 assertEquals(1, node3Children.size());
310 qnames.add(QName.create(FOO, "odl"));
313 qnames.add(QName.create(FOO, "id"));
314 qnames.add(QName.create(FOO, "id"));
315 final LeafSchemaNode caseIdChild = (LeafSchemaNode) idChildren.iterator().next();
316 assertNotNull(caseIdChild);
317 assertEquals(SchemaPath.create(qnames, true), caseIdChild.getPath());
320 qnames.set(4, QName.create(FOO, "node3"));
321 qnames.set(5, QName.create(FOO, "node3"));
322 final ContainerSchemaNode caseNode3Child = (ContainerSchemaNode) node3Children.iterator().next();
323 assertNotNull(caseNode3Child);
324 assertEquals(SchemaPath.create(qnames, true), caseNode3Child.getPath());
328 public void testAugmentRpc() throws Exception {
329 final SchemaContext context = TestUtils.loadModules(getClass().getResource("/augment-test/rpc").toURI());
330 final URI NS_BAR = URI.create("urn:opendaylight:bar");
331 final URI NS_FOO = URI.create("urn:opendaylight:foo");
332 final Revision revision = Revision.of("2013-10-11");
333 final Module bar = TestUtils.findModule(context, "bar").get();
334 final Collection<? extends RpcDefinition> rpcs = bar.getRpcs();
335 assertEquals(2, rpcs.size());
337 RpcDefinition submit = null;
338 for (final RpcDefinition rpc : rpcs) {
339 if ("submit".equals(rpc.getQName().getLocalName())) {
344 assertNotNull(submit);
346 final QName submitQName = QName.create(NS_BAR, revision, "submit");
347 assertEquals(submitQName, submit.getQName());
348 final InputSchemaNode input = submit.getInput();
349 final QName inputQName = QName.create(NS_BAR, revision, "input");
350 assertEquals(inputQName, input.getQName());
351 final ChoiceSchemaNode arguments = (ChoiceSchemaNode) input.getDataChildByName(QName.create(NS_BAR, revision,
353 final QName argumentsQName = QName.create(NS_BAR, revision, "arguments");
354 assertEquals(argumentsQName, arguments.getQName());
355 assertFalse(arguments.isAugmenting());
356 final Collection<? extends CaseSchemaNode> cases = arguments.getCases();
357 assertEquals(3, cases.size());
359 CaseSchemaNode attach = null;
360 CaseSchemaNode create = null;
361 CaseSchemaNode destroy = null;
362 for (final CaseSchemaNode child : cases) {
363 if ("attach".equals(child.getQName().getLocalName())) {
365 } else if ("create".equals(child.getQName().getLocalName())) {
367 } else if ("destroy".equals(child.getQName().getLocalName())) {
371 assertNotNull(attach);
372 assertNotNull(create);
373 assertNotNull(destroy);
375 assertTrue(attach.isAugmenting());
376 assertTrue(create.isAugmenting());
377 assertTrue(destroy.isAugmenting());
379 final QName[] qnames = new QName[4];
380 qnames[0] = submitQName;
381 qnames[1] = inputQName;
382 qnames[2] = argumentsQName;
385 qnames[3] = QName.create(NS_FOO, revision, "attach");
386 assertEquals(qnames[3], attach.getQName());
387 assertEquals(SchemaPath.create(true, qnames), attach.getPath());
388 final Collection<? extends DataSchemaNode> attachChildren = attach.getChildNodes();
389 assertEquals(1, attachChildren.size());
392 qnames[3] = QName.create(NS_FOO, revision, "create");
393 assertEquals(qnames[3], create.getQName());
394 assertEquals(SchemaPath.create(true, qnames), create.getPath());
395 final Collection<? extends DataSchemaNode> createChildren = create.getChildNodes();
396 assertEquals(1, createChildren.size());
399 qnames[3] = QName.create(NS_FOO, revision, "destroy");
400 assertEquals(qnames[3], destroy.getQName());
401 assertEquals(SchemaPath.create(true, qnames), destroy.getPath());
402 final Collection<? extends DataSchemaNode> destroyChildren = destroy.getChildNodes();
403 assertEquals(1, destroyChildren.size());
407 public void testAugmentInUsesResolving() throws Exception {
408 final SchemaContext context = TestUtils.loadModules(getClass().getResource("/augment-test/augment-in-uses")
410 assertEquals(1, context.getModules().size());
412 final Module test = context.getModules().iterator().next();
413 final DataNodeContainer links = (DataNodeContainer) test.getDataChildByName(QName.create(test.getQNameModule(),
415 final DataNodeContainer link = (DataNodeContainer) links.getDataChildByName(QName.create(test.getQNameModule(),
417 final DataNodeContainer nodes = (DataNodeContainer) link.getDataChildByName(QName.create(test.getQNameModule(),
419 final ContainerSchemaNode node = (ContainerSchemaNode) nodes.getDataChildByName(QName.create(
420 test.getQNameModule(), "node"));
421 final Collection<? extends AugmentationSchemaNode> augments = node.getAvailableAugmentations();
422 assertEquals(1, augments.size());
423 assertEquals(1, node.getChildNodes().size());
424 final LeafSchemaNode id = (LeafSchemaNode) node.getDataChildByName(QName.create(test.getQNameModule(), "id"));
425 assertTrue(id.isAugmenting());