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;
21 import java.util.SortedMap;
22 import org.junit.Test;
23 import org.opendaylight.yangtools.yang.common.QName;
24 import org.opendaylight.yangtools.yang.common.QNameModule;
25 import org.opendaylight.yangtools.yang.common.Revision;
26 import org.opendaylight.yangtools.yang.model.api.AugmentationSchemaNode;
27 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
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.DataNodeContainer;
31 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
32 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
33 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
34 import org.opendaylight.yangtools.yang.model.api.Module;
35 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
36 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
37 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
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 Set<AugmentationSchemaNode> augmentations = module1.getAugmentations();
64 assertEquals(1, augmentations.size());
65 final AugmentationSchemaNode augment = augmentations.iterator().next();
66 assertNotNull(augment);
68 SchemaPath expectedSchemaPath = SchemaPath.create(qnames, true);
69 assertEquals(expectedSchemaPath, augment.getTargetPath());
71 final Collection<DataSchemaNode> augmentChildren = augment.getChildNodes();
72 assertEquals(4, augmentChildren.size());
73 for (final DataSchemaNode dsn : augmentChildren) {
74 TestUtils.checkIsAugmenting(dsn, false);
77 final LeafSchemaNode ds0ChannelNumber = (LeafSchemaNode) augment.getDataChildByName(QName.create(
78 module1.getQNameModule(), "ds0ChannelNumber"));
79 final LeafSchemaNode interfaceId = (LeafSchemaNode) augment.getDataChildByName(QName.create(
80 module1.getQNameModule(), "interface-id"));
81 final ContainerSchemaNode schemas = (ContainerSchemaNode) augment.getDataChildByName(QName.create(
82 module1.getQNameModule(), "schemas"));
83 final ChoiceSchemaNode odl = (ChoiceSchemaNode) augment.getDataChildByName(QName.create(
84 module1.getQNameModule(), "odl"));
86 assertNotNull(ds0ChannelNumber);
87 assertNotNull(interfaceId);
88 assertNotNull(schemas);
91 // leaf ds0ChannelNumber
92 QName qname = QName.create(FOO, "ds0ChannelNumber");
94 assertEquals(qname, ds0ChannelNumber.getQName());
95 expectedSchemaPath = SchemaPath.create(qnames, true);
96 assertEquals(expectedSchemaPath, ds0ChannelNumber.getPath());
97 assertFalse(ds0ChannelNumber.isAugmenting());
98 // type of leaf ds0ChannelNumber
99 final List<QName> typePath = Collections.singletonList(BaseTypes.STRING_QNAME);
100 expectedSchemaPath = SchemaPath.create(typePath, true);
101 assertEquals(expectedSchemaPath, ds0ChannelNumber.getType().getPath());
104 qname = QName.create(FOO, "interface-id");
105 assertEquals(qname, interfaceId.getQName());
106 qnames.set(3, qname);
107 expectedSchemaPath = SchemaPath.create(qnames, true);
108 assertEquals(expectedSchemaPath, interfaceId.getPath());
109 assertFalse(interfaceId.isAugmenting());
112 qname = QName.create(FOO, "schemas");
113 assertEquals(qname, schemas.getQName());
114 qnames.set(3, qname);
115 expectedSchemaPath = SchemaPath.create(qnames, true);
116 assertEquals(expectedSchemaPath, schemas.getPath());
117 assertFalse(schemas.isAugmenting());
120 qname = QName.create(FOO, "odl");
121 assertEquals(qname, odl.getQName());
122 qnames.set(3, qname);
123 expectedSchemaPath = SchemaPath.create(qnames, true);
124 assertEquals(expectedSchemaPath, odl.getPath());
125 assertFalse(odl.isAugmenting());
128 final Module module3 = TestUtils.findModule(context, "baz").get();
129 augmentations = module3.getAugmentations();
130 assertEquals(3, augmentations.size());
131 AugmentationSchemaNode augment1 = null;
132 AugmentationSchemaNode augment2 = null;
133 AugmentationSchemaNode augment3 = null;
134 for (final AugmentationSchemaNode as : augmentations) {
135 if (!as.getWhenCondition().isPresent()) {
137 } else if ("if:ifType='ds0'".equals(as.getWhenCondition().get().toString())) {
139 } else if ("if:ifType='ds2'".equals(as.getWhenCondition().get().toString())) {
143 assertNotNull(augment1);
144 assertNotNull(augment2);
145 assertNotNull(augment3);
147 assertEquals(1, augment1.getChildNodes().size());
148 final ContainerSchemaNode augmentHolder = (ContainerSchemaNode) augment1.getDataChildByName(QName.create(
149 module3.getQNameModule(), "augment-holder"));
150 assertNotNull(augmentHolder);
152 assertEquals(1, augment2.getChildNodes().size());
153 final ContainerSchemaNode augmentHolder2 = (ContainerSchemaNode) augment2.getDataChildByName(QName.create(
154 module3.getQNameModule(), "augment-holder2"));
155 assertNotNull(augmentHolder2);
157 assertEquals(1, augment3.getChildNodes().size());
158 final ChoiceCaseNode pause = (ChoiceCaseNode) augment3.getDataChildByName(QName.create(
159 module3.getQNameModule(), "pause"));
160 assertNotNull(pause);
164 public void testAugmentResolving() throws Exception {
165 final SchemaContext context = TestUtils.loadModules(getClass().getResource("/augment-test/augment-in-augment")
167 final Module module2 = TestUtils.findModule(context, "bar").get();
168 final ContainerSchemaNode interfaces = (ContainerSchemaNode) module2.getDataChildByName(QName.create(
169 module2.getQNameModule(), "interfaces"));
170 final ListSchemaNode ifEntry = (ListSchemaNode) interfaces.getDataChildByName(QName.create(
171 module2.getQNameModule(), "ifEntry"));
173 final List<QName> qnames = new ArrayList<>();
179 // augment "/br:interfaces/br:ifEntry" {
180 final ContainerSchemaNode augmentHolder = (ContainerSchemaNode) ifEntry.getDataChildByName(QName.create(BAZ,
182 TestUtils.checkIsAugmenting(augmentHolder, true);
183 assertEquals(Q2, augmentHolder.getQName());
184 assertEquals(SchemaPath.create(qnames, true), augmentHolder.getPath());
187 // augment "/br:interfaces/br:ifEntry/bz:augment-holder"
188 final LeafSchemaNode ds0ChannelNumber = (LeafSchemaNode) augmentHolder.getDataChildByName(QName.create(FOO,
189 "ds0ChannelNumber"));
190 final LeafSchemaNode interfaceId = (LeafSchemaNode) augmentHolder.getDataChildByName(QName.create(FOO,
192 final ContainerSchemaNode schemas = (ContainerSchemaNode) augmentHolder.getDataChildByName(QName.create(FOO,
194 final ChoiceSchemaNode odl = (ChoiceSchemaNode) augmentHolder.getDataChildByName(QName.create(FOO, "odl"));
196 assertNotNull(ds0ChannelNumber);
197 assertNotNull(interfaceId);
198 assertNotNull(schemas);
201 // leaf ds0ChannelNumber
202 QName qname = QName.create(FOO, "ds0ChannelNumber");
203 assertEquals(qname, ds0ChannelNumber.getQName());
205 assertEquals(SchemaPath.create(qnames, true), ds0ChannelNumber.getPath());
208 qname = QName.create(FOO, "interface-id");
209 assertEquals(qname, interfaceId.getQName());
210 qnames.set(3, qname);
211 assertEquals(SchemaPath.create(qnames, true), interfaceId.getPath());
214 qname = QName.create(FOO, "schemas");
215 assertEquals(qname, schemas.getQName());
216 qnames.set(3, qname);
217 assertEquals(SchemaPath.create(qnames, true), schemas.getPath());
220 qname = QName.create(FOO, "odl");
221 assertEquals(qname, odl.getQName());
222 qnames.set(3, qname);
223 assertEquals(SchemaPath.create(qnames, true), odl.getPath());
227 public void testAugmentedChoice() throws Exception {
228 final SchemaContext context = TestUtils.loadModules(getClass().getResource("/augment-test/augment-in-augment")
230 final Module module2 = TestUtils.findModule(context, "bar").get();
231 final ContainerSchemaNode interfaces = (ContainerSchemaNode) module2.getDataChildByName(QName.create(
232 module2.getQNameModule(), "interfaces"));
233 final ListSchemaNode ifEntry = (ListSchemaNode) interfaces.getDataChildByName(QName.create(
234 module2.getQNameModule(), "ifEntry"));
235 final ContainerSchemaNode augmentedHolder = (ContainerSchemaNode) ifEntry.getDataChildByName(QName.create(
236 BAZ, "augment-holder"));
237 TestUtils.checkIsAugmenting(augmentedHolder, true);
240 // augment "/br:interfaces/br:ifEntry/bz:augment-holder"
241 final ChoiceSchemaNode odl = (ChoiceSchemaNode) augmentedHolder.getDataChildByName(QName.create(FOO, "odl"));
243 final SortedMap<QName, ChoiceCaseNode> cases = odl.getCases();
244 assertEquals(4, cases.size());
246 ChoiceCaseNode id = null;
247 ChoiceCaseNode node1 = null;
248 ChoiceCaseNode node2 = null;
249 ChoiceCaseNode node3 = null;
251 for (final ChoiceCaseNode ccn : cases.values()) {
252 if ("id".equals(ccn.getQName().getLocalName())) {
254 } else if ("node1".equals(ccn.getQName().getLocalName())) {
256 } else if ("node2".equals(ccn.getQName().getLocalName())) {
258 } else if ("node3".equals(ccn.getQName().getLocalName())) {
264 assertNotNull(node1);
265 assertNotNull(node2);
266 assertNotNull(node3);
268 final List<QName> qnames = new ArrayList<>();
272 qnames.add(QName.create(FOO, "odl"));
275 QName qname = QName.create(FOO, "id");
276 assertEquals(qname, id.getQName());
278 assertEquals(SchemaPath.create(qnames, true), id.getPath());
279 final Collection<DataSchemaNode> idChildren = id.getChildNodes();
280 assertEquals(1, idChildren.size());
283 qname = QName.create(FOO, "node1");
284 assertEquals(qname, node1.getQName());
285 qnames.set(4, qname);
286 assertEquals(SchemaPath.create(qnames, true), node1.getPath());
287 final Collection<DataSchemaNode> node1Children = node1.getChildNodes();
288 assertTrue(node1Children.isEmpty());
291 qname = QName.create(FOO, "node2");
292 assertEquals(qname, node2.getQName());
293 qnames.set(4, qname);
294 assertEquals(SchemaPath.create(qnames, true), node2.getPath());
295 final Collection<DataSchemaNode> node2Children = node2.getChildNodes();
296 assertTrue(node2Children.isEmpty());
299 qname = QName.create(FOO, "node3");
300 assertEquals(qname, node3.getQName());
301 qnames.set(4, qname);
302 assertEquals(SchemaPath.create(qnames, true), node3.getPath());
303 final Collection<DataSchemaNode> node3Children = node3.getChildNodes();
304 assertEquals(1, node3Children.size());
311 qnames.add(QName.create(FOO, "odl"));
314 qnames.add(QName.create(FOO, "id"));
315 qnames.add(QName.create(FOO, "id"));
316 final LeafSchemaNode caseIdChild = (LeafSchemaNode) idChildren.iterator().next();
317 assertNotNull(caseIdChild);
318 assertEquals(SchemaPath.create(qnames, true), caseIdChild.getPath());
321 qnames.set(4, QName.create(FOO, "node3"));
322 qnames.set(5, QName.create(FOO, "node3"));
323 final ContainerSchemaNode caseNode3Child = (ContainerSchemaNode) node3Children.iterator().next();
324 assertNotNull(caseNode3Child);
325 assertEquals(SchemaPath.create(qnames, true), caseNode3Child.getPath());
329 public void testAugmentRpc() throws Exception {
330 final SchemaContext context = TestUtils.loadModules(getClass().getResource("/augment-test/rpc").toURI());
331 final URI NS_BAR = URI.create("urn:opendaylight:bar");
332 final URI NS_FOO = URI.create("urn:opendaylight:foo");
333 final Revision revision = Revision.of("2013-10-11");
334 final Module bar = TestUtils.findModule(context, "bar").get();
335 final Set<RpcDefinition> rpcs = bar.getRpcs();
336 assertEquals(2, rpcs.size());
338 RpcDefinition submit = null;
339 for (final RpcDefinition rpc : rpcs) {
340 if ("submit".equals(rpc.getQName().getLocalName())) {
345 assertNotNull(submit);
347 final QName submitQName = QName.create(NS_BAR, revision, "submit");
348 assertEquals(submitQName, submit.getQName());
349 final ContainerSchemaNode input = submit.getInput();
350 final QName inputQName = QName.create(NS_BAR, revision, "input");
351 assertEquals(inputQName, input.getQName());
352 final ChoiceSchemaNode arguments = (ChoiceSchemaNode) input.getDataChildByName(QName.create(NS_BAR, revision,
354 final QName argumentsQName = QName.create(NS_BAR, revision, "arguments");
355 assertEquals(argumentsQName, arguments.getQName());
356 assertFalse(arguments.isAugmenting());
357 final SortedMap<QName, ChoiceCaseNode> cases = arguments.getCases();
358 assertEquals(3, cases.size());
360 ChoiceCaseNode attach = null;
361 ChoiceCaseNode create = null;
362 ChoiceCaseNode destroy = null;
363 for (final ChoiceCaseNode child : cases.values()) {
364 if ("attach".equals(child.getQName().getLocalName())) {
366 } else if ("create".equals(child.getQName().getLocalName())) {
368 } else if ("destroy".equals(child.getQName().getLocalName())) {
372 assertNotNull(attach);
373 assertNotNull(create);
374 assertNotNull(destroy);
376 assertTrue(attach.isAugmenting());
377 assertTrue(create.isAugmenting());
378 assertTrue(destroy.isAugmenting());
380 final QName[] qnames = new QName[4];
381 qnames[0] = submitQName;
382 qnames[1] = inputQName;
383 qnames[2] = argumentsQName;
386 qnames[3] = QName.create(NS_FOO, revision, "attach");
387 assertEquals(qnames[3], attach.getQName());
388 assertEquals(SchemaPath.create(true, qnames), attach.getPath());
389 final Collection<DataSchemaNode> attachChildren = attach.getChildNodes();
390 assertEquals(1, attachChildren.size());
393 qnames[3] = QName.create(NS_FOO, revision, "create");
394 assertEquals(qnames[3], create.getQName());
395 assertEquals(SchemaPath.create(true, qnames), create.getPath());
396 final Collection<DataSchemaNode> createChildren = create.getChildNodes();
397 assertEquals(1, createChildren.size());
400 qnames[3] = QName.create(NS_FOO, revision, "destroy");
401 assertEquals(qnames[3], destroy.getQName());
402 assertEquals(SchemaPath.create(true, qnames), destroy.getPath());
403 final Collection<DataSchemaNode> destroyChildren = destroy.getChildNodes();
404 assertEquals(1, destroyChildren.size());
408 public void testAugmentInUsesResolving() throws Exception {
409 final SchemaContext context = TestUtils.loadModules(getClass().getResource("/augment-test/augment-in-uses")
411 assertEquals(1, context.getModules().size());
413 final Module test = context.getModules().iterator().next();
414 final DataNodeContainer links = (DataNodeContainer) test.getDataChildByName(QName.create(test.getQNameModule(),
416 final DataNodeContainer link = (DataNodeContainer) links.getDataChildByName(QName.create(test.getQNameModule(),
418 final DataNodeContainer nodes = (DataNodeContainer) link.getDataChildByName(QName.create(test.getQNameModule(),
420 final ContainerSchemaNode node = (ContainerSchemaNode) nodes.getDataChildByName(QName.create(
421 test.getQNameModule(), "node"));
422 final Set<AugmentationSchemaNode> augments = node.getAvailableAugmentations();
423 assertEquals(1, augments.size());
424 assertEquals(1, node.getChildNodes().size());
425 final LeafSchemaNode id = (LeafSchemaNode) node.getDataChildByName(QName.create(test.getQNameModule(), "id"));
426 assertTrue(id.isAugmenting());