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.List;
19 import org.junit.Test;
20 import org.opendaylight.yangtools.yang.common.QName;
21 import org.opendaylight.yangtools.yang.common.QNameModule;
22 import org.opendaylight.yangtools.yang.common.Revision;
23 import org.opendaylight.yangtools.yang.model.api.AugmentationSchemaNode;
24 import org.opendaylight.yangtools.yang.model.api.CaseSchemaNode;
25 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
26 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
27 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
28 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
29 import org.opendaylight.yangtools.yang.model.api.InputSchemaNode;
30 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
31 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
32 import org.opendaylight.yangtools.yang.model.api.Module;
33 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
34 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
35 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
36 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
37 import org.opendaylight.yangtools.yang.model.util.BaseTypes;
39 public class AugmentTest {
40 private static final QNameModule FOO = QNameModule.create(
41 URI.create("urn:opendaylight.foo"), Revision.of("2013-10-13"));
42 private static final QNameModule BAR = QNameModule.create(
43 URI.create("urn:opendaylight.bar"), Revision.of("2013-10-14"));
44 private static final QNameModule BAZ = QNameModule.create(
45 URI.create("urn:opendaylight.baz"), Revision.of("2013-10-15"));
47 private static final QName Q0 = QName.create(BAR, "interfaces");
48 private static final QName Q1 = QName.create(BAR, "ifEntry");
49 private static final QName Q2 = QName.create(BAZ, "augment-holder");
52 public void testAugmentParsing() throws Exception {
53 final SchemaContext context = TestUtils.loadModules(getClass().getResource("/augment-test/augment-in-augment")
55 final List<QName> qnames = new ArrayList<>();
61 final Module module1 = TestUtils.findModule(context, "foo").get();
62 Collection<? extends AugmentationSchemaNode> augmentations = module1.getAugmentations();
63 assertEquals(1, augmentations.size());
64 final AugmentationSchemaNode augment = augmentations.iterator().next();
65 assertNotNull(augment);
67 assertEquals(Absolute.of(qnames), augment.getTargetPath());
69 final Collection<? extends DataSchemaNode> augmentChildren = augment.getChildNodes();
70 assertEquals(4, augmentChildren.size());
71 for (final DataSchemaNode dsn : augmentChildren) {
72 TestUtils.checkIsAugmenting(dsn, false);
75 final LeafSchemaNode ds0ChannelNumber = (LeafSchemaNode) augment.getDataChildByName(QName.create(
76 module1.getQNameModule(), "ds0ChannelNumber"));
77 final LeafSchemaNode interfaceId = (LeafSchemaNode) augment.getDataChildByName(QName.create(
78 module1.getQNameModule(), "interface-id"));
79 final ContainerSchemaNode schemas = (ContainerSchemaNode) augment.getDataChildByName(QName.create(
80 module1.getQNameModule(), "schemas"));
81 final ChoiceSchemaNode odl = (ChoiceSchemaNode) augment.getDataChildByName(QName.create(
82 module1.getQNameModule(), "odl"));
84 assertNotNull(ds0ChannelNumber);
85 assertNotNull(interfaceId);
86 assertNotNull(schemas);
89 // leaf ds0ChannelNumber
90 QName qname = QName.create(FOO, "ds0ChannelNumber");
92 assertEquals(qname, ds0ChannelNumber.getQName());
93 SchemaPath expectedSchemaPath = SchemaPath.create(qnames, true);
94 assertEquals(expectedSchemaPath, ds0ChannelNumber.getPath());
95 assertFalse(ds0ChannelNumber.isAugmenting());
96 // type of leaf ds0ChannelNumber
97 assertEquals(BaseTypes.STRING_QNAME, ds0ChannelNumber.getType().getQName());
100 qname = QName.create(FOO, "interface-id");
101 assertEquals(qname, interfaceId.getQName());
102 qnames.set(3, qname);
103 expectedSchemaPath = SchemaPath.create(qnames, true);
104 assertEquals(expectedSchemaPath, interfaceId.getPath());
105 assertFalse(interfaceId.isAugmenting());
108 qname = QName.create(FOO, "schemas");
109 assertEquals(qname, schemas.getQName());
110 qnames.set(3, qname);
111 expectedSchemaPath = SchemaPath.create(qnames, true);
112 assertEquals(expectedSchemaPath, schemas.getPath());
113 assertFalse(schemas.isAugmenting());
116 qname = QName.create(FOO, "odl");
117 assertEquals(qname, odl.getQName());
118 qnames.set(3, qname);
119 expectedSchemaPath = SchemaPath.create(qnames, true);
120 assertEquals(expectedSchemaPath, odl.getPath());
121 assertFalse(odl.isAugmenting());
124 final Module module3 = TestUtils.findModule(context, "baz").get();
125 augmentations = module3.getAugmentations();
126 assertEquals(3, augmentations.size());
127 AugmentationSchemaNode augment1 = null;
128 AugmentationSchemaNode augment2 = null;
129 AugmentationSchemaNode augment3 = null;
130 for (final AugmentationSchemaNode as : augmentations) {
131 if (!as.getWhenCondition().isPresent()) {
133 } else if ("br:ifType='ds0'".equals(as.getWhenCondition().orElseThrow().toString())) {
135 } else if ("br:ifType='ds2'".equals(as.getWhenCondition().orElseThrow().toString())) {
139 assertNotNull(augment1);
140 assertNotNull(augment2);
141 assertNotNull(augment3);
143 assertEquals(1, augment1.getChildNodes().size());
144 final ContainerSchemaNode augmentHolder = (ContainerSchemaNode) augment1.getDataChildByName(QName.create(
145 module3.getQNameModule(), "augment-holder"));
146 assertNotNull(augmentHolder);
148 assertEquals(1, augment2.getChildNodes().size());
149 final ContainerSchemaNode augmentHolder2 = (ContainerSchemaNode) augment2.getDataChildByName(QName.create(
150 module3.getQNameModule(), "augment-holder2"));
151 assertNotNull(augmentHolder2);
153 assertEquals(1, augment3.getChildNodes().size());
154 final CaseSchemaNode pause = (CaseSchemaNode) augment3.getDataChildByName(QName.create(
155 module3.getQNameModule(), "pause"));
156 assertNotNull(pause);
160 public void testAugmentResolving() throws Exception {
161 final SchemaContext context = TestUtils.loadModules(getClass().getResource("/augment-test/augment-in-augment")
163 final Module module2 = TestUtils.findModule(context, "bar").get();
164 final ContainerSchemaNode interfaces = (ContainerSchemaNode) module2.getDataChildByName(QName.create(
165 module2.getQNameModule(), "interfaces"));
166 final ListSchemaNode ifEntry = (ListSchemaNode) interfaces.getDataChildByName(QName.create(
167 module2.getQNameModule(), "ifEntry"));
169 final List<QName> qnames = new ArrayList<>();
175 // augment "/br:interfaces/br:ifEntry" {
176 final ContainerSchemaNode augmentHolder = (ContainerSchemaNode) ifEntry.getDataChildByName(QName.create(BAZ,
178 TestUtils.checkIsAugmenting(augmentHolder, true);
179 assertEquals(Q2, augmentHolder.getQName());
180 assertEquals(SchemaPath.create(qnames, true), augmentHolder.getPath());
183 // augment "/br:interfaces/br:ifEntry/bz:augment-holder"
184 final LeafSchemaNode ds0ChannelNumber = (LeafSchemaNode) augmentHolder.getDataChildByName(QName.create(FOO,
185 "ds0ChannelNumber"));
186 final LeafSchemaNode interfaceId = (LeafSchemaNode) augmentHolder.getDataChildByName(QName.create(FOO,
188 final ContainerSchemaNode schemas = (ContainerSchemaNode) augmentHolder.getDataChildByName(QName.create(FOO,
190 final ChoiceSchemaNode odl = (ChoiceSchemaNode) augmentHolder.getDataChildByName(QName.create(FOO, "odl"));
192 assertNotNull(ds0ChannelNumber);
193 assertNotNull(interfaceId);
194 assertNotNull(schemas);
197 // leaf ds0ChannelNumber
198 QName qname = QName.create(FOO, "ds0ChannelNumber");
199 assertEquals(qname, ds0ChannelNumber.getQName());
201 assertEquals(SchemaPath.create(qnames, true), ds0ChannelNumber.getPath());
204 qname = QName.create(FOO, "interface-id");
205 assertEquals(qname, interfaceId.getQName());
206 qnames.set(3, qname);
207 assertEquals(SchemaPath.create(qnames, true), interfaceId.getPath());
210 qname = QName.create(FOO, "schemas");
211 assertEquals(qname, schemas.getQName());
212 qnames.set(3, qname);
213 assertEquals(SchemaPath.create(qnames, true), schemas.getPath());
216 qname = QName.create(FOO, "odl");
217 assertEquals(qname, odl.getQName());
218 qnames.set(3, qname);
219 assertEquals(SchemaPath.create(qnames, true), odl.getPath());
223 public void testAugmentedChoice() throws Exception {
224 final SchemaContext context = TestUtils.loadModules(getClass().getResource("/augment-test/augment-in-augment")
226 final Module module2 = TestUtils.findModule(context, "bar").get();
227 final ContainerSchemaNode interfaces = (ContainerSchemaNode) module2.getDataChildByName(QName.create(
228 module2.getQNameModule(), "interfaces"));
229 final ListSchemaNode ifEntry = (ListSchemaNode) interfaces.getDataChildByName(QName.create(
230 module2.getQNameModule(), "ifEntry"));
231 final ContainerSchemaNode augmentedHolder = (ContainerSchemaNode) ifEntry.getDataChildByName(QName.create(
232 BAZ, "augment-holder"));
233 TestUtils.checkIsAugmenting(augmentedHolder, true);
236 // augment "/br:interfaces/br:ifEntry/bz:augment-holder"
237 final ChoiceSchemaNode odl = (ChoiceSchemaNode) augmentedHolder.getDataChildByName(QName.create(FOO, "odl"));
239 final Collection<? extends CaseSchemaNode> cases = odl.getCases();
240 assertEquals(4, cases.size());
242 CaseSchemaNode id = null;
243 CaseSchemaNode node1 = null;
244 CaseSchemaNode node2 = null;
245 CaseSchemaNode node3 = null;
247 for (final CaseSchemaNode ccn : cases) {
248 if ("id".equals(ccn.getQName().getLocalName())) {
250 } else if ("node1".equals(ccn.getQName().getLocalName())) {
252 } else if ("node2".equals(ccn.getQName().getLocalName())) {
254 } else if ("node3".equals(ccn.getQName().getLocalName())) {
260 assertNotNull(node1);
261 assertNotNull(node2);
262 assertNotNull(node3);
264 final List<QName> qnames = new ArrayList<>();
268 qnames.add(QName.create(FOO, "odl"));
271 QName qname = QName.create(FOO, "id");
272 assertEquals(qname, id.getQName());
274 assertEquals(SchemaPath.create(qnames, true), id.getPath());
275 final Collection<? extends DataSchemaNode> idChildren = id.getChildNodes();
276 assertEquals(1, idChildren.size());
279 qname = QName.create(FOO, "node1");
280 assertEquals(qname, node1.getQName());
281 qnames.set(4, qname);
282 assertEquals(SchemaPath.create(qnames, true), node1.getPath());
283 final Collection<? extends DataSchemaNode> node1Children = node1.getChildNodes();
284 assertTrue(node1Children.isEmpty());
287 qname = QName.create(FOO, "node2");
288 assertEquals(qname, node2.getQName());
289 qnames.set(4, qname);
290 assertEquals(SchemaPath.create(qnames, true), node2.getPath());
291 final Collection<? extends DataSchemaNode> node2Children = node2.getChildNodes();
292 assertTrue(node2Children.isEmpty());
295 qname = QName.create(FOO, "node3");
296 assertEquals(qname, node3.getQName());
297 qnames.set(4, qname);
298 assertEquals(SchemaPath.create(qnames, true), node3.getPath());
299 final Collection<? extends DataSchemaNode> node3Children = node3.getChildNodes();
300 assertEquals(1, node3Children.size());
307 qnames.add(QName.create(FOO, "odl"));
310 qnames.add(QName.create(FOO, "id"));
311 qnames.add(QName.create(FOO, "id"));
312 final LeafSchemaNode caseIdChild = (LeafSchemaNode) idChildren.iterator().next();
313 assertNotNull(caseIdChild);
314 assertEquals(SchemaPath.create(qnames, true), caseIdChild.getPath());
317 qnames.set(4, QName.create(FOO, "node3"));
318 qnames.set(5, QName.create(FOO, "node3"));
319 final ContainerSchemaNode caseNode3Child = (ContainerSchemaNode) node3Children.iterator().next();
320 assertNotNull(caseNode3Child);
321 assertEquals(SchemaPath.create(qnames, true), caseNode3Child.getPath());
325 public void testAugmentRpc() throws Exception {
326 final SchemaContext context = TestUtils.loadModules(getClass().getResource("/augment-test/rpc").toURI());
327 final URI NS_BAR = URI.create("urn:opendaylight:bar");
328 final URI NS_FOO = URI.create("urn:opendaylight:foo");
329 final Revision revision = Revision.of("2013-10-11");
330 final Module bar = TestUtils.findModule(context, "bar").get();
331 final Collection<? extends RpcDefinition> rpcs = bar.getRpcs();
332 assertEquals(2, rpcs.size());
334 RpcDefinition submit = null;
335 for (final RpcDefinition rpc : rpcs) {
336 if ("submit".equals(rpc.getQName().getLocalName())) {
341 assertNotNull(submit);
343 final QName submitQName = QName.create(NS_BAR, revision, "submit");
344 assertEquals(submitQName, submit.getQName());
345 final InputSchemaNode input = submit.getInput();
346 final QName inputQName = QName.create(NS_BAR, revision, "input");
347 assertEquals(inputQName, input.getQName());
348 final ChoiceSchemaNode arguments = (ChoiceSchemaNode) input.getDataChildByName(QName.create(NS_BAR, revision,
350 final QName argumentsQName = QName.create(NS_BAR, revision, "arguments");
351 assertEquals(argumentsQName, arguments.getQName());
352 assertFalse(arguments.isAugmenting());
353 final Collection<? extends CaseSchemaNode> cases = arguments.getCases();
354 assertEquals(3, cases.size());
356 CaseSchemaNode attach = null;
357 CaseSchemaNode create = null;
358 CaseSchemaNode destroy = null;
359 for (final CaseSchemaNode child : cases) {
360 if ("attach".equals(child.getQName().getLocalName())) {
362 } else if ("create".equals(child.getQName().getLocalName())) {
364 } else if ("destroy".equals(child.getQName().getLocalName())) {
368 assertNotNull(attach);
369 assertNotNull(create);
370 assertNotNull(destroy);
372 assertTrue(attach.isAugmenting());
373 assertTrue(create.isAugmenting());
374 assertTrue(destroy.isAugmenting());
376 final QName[] qnames = new QName[4];
377 qnames[0] = submitQName;
378 qnames[1] = inputQName;
379 qnames[2] = argumentsQName;
382 qnames[3] = QName.create(NS_FOO, revision, "attach");
383 assertEquals(qnames[3], attach.getQName());
384 assertEquals(SchemaPath.create(true, qnames), attach.getPath());
385 final Collection<? extends DataSchemaNode> attachChildren = attach.getChildNodes();
386 assertEquals(1, attachChildren.size());
389 qnames[3] = QName.create(NS_FOO, revision, "create");
390 assertEquals(qnames[3], create.getQName());
391 assertEquals(SchemaPath.create(true, qnames), create.getPath());
392 final Collection<? extends DataSchemaNode> createChildren = create.getChildNodes();
393 assertEquals(1, createChildren.size());
396 qnames[3] = QName.create(NS_FOO, revision, "destroy");
397 assertEquals(qnames[3], destroy.getQName());
398 assertEquals(SchemaPath.create(true, qnames), destroy.getPath());
399 final Collection<? extends DataSchemaNode> destroyChildren = destroy.getChildNodes();
400 assertEquals(1, destroyChildren.size());
404 public void testAugmentInUsesResolving() throws Exception {
405 final SchemaContext context = TestUtils.loadModules(getClass().getResource("/augment-test/augment-in-uses")
407 assertEquals(1, context.getModules().size());
409 final Module test = context.getModules().iterator().next();
410 final DataNodeContainer links = (DataNodeContainer) test.getDataChildByName(QName.create(test.getQNameModule(),
412 final DataNodeContainer link = (DataNodeContainer) links.getDataChildByName(QName.create(test.getQNameModule(),
414 final DataNodeContainer nodes = (DataNodeContainer) link.getDataChildByName(QName.create(test.getQNameModule(),
416 final ContainerSchemaNode node = (ContainerSchemaNode) nodes.getDataChildByName(QName.create(
417 test.getQNameModule(), "node"));
418 final Collection<? extends AugmentationSchemaNode> augments = node.getAvailableAugmentations();
419 assertEquals(1, augments.size());
420 assertEquals(1, node.getChildNodes().size());
421 final LeafSchemaNode id = (LeafSchemaNode) node.getDataChildByName(QName.create(test.getQNameModule(), "id"));
422 assertTrue(id.isAugmenting());