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 org.junit.Test;
22 import org.opendaylight.yangtools.yang.common.QName;
23 import org.opendaylight.yangtools.yang.common.QNameModule;
24 import org.opendaylight.yangtools.yang.common.Revision;
25 import org.opendaylight.yangtools.yang.common.YangConstants;
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.parser.stmt.rfc6020.TypeUtils;
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 QName typeQName = QName.create(YangConstants.RFC6020_YANG_MODULE, TypeUtils.STRING);
100 final List<QName> typePath = Collections.singletonList(typeQName);
101 expectedSchemaPath = SchemaPath.create(typePath, true);
102 assertEquals(expectedSchemaPath, ds0ChannelNumber.getType().getPath());
105 qname = QName.create(FOO, "interface-id");
106 assertEquals(qname, interfaceId.getQName());
107 qnames.set(3, qname);
108 expectedSchemaPath = SchemaPath.create(qnames, true);
109 assertEquals(expectedSchemaPath, interfaceId.getPath());
110 assertFalse(interfaceId.isAugmenting());
113 qname = QName.create(FOO, "schemas");
114 assertEquals(qname, schemas.getQName());
115 qnames.set(3, qname);
116 expectedSchemaPath = SchemaPath.create(qnames, true);
117 assertEquals(expectedSchemaPath, schemas.getPath());
118 assertFalse(schemas.isAugmenting());
121 qname = QName.create(FOO, "odl");
122 assertEquals(qname, odl.getQName());
123 qnames.set(3, qname);
124 expectedSchemaPath = SchemaPath.create(qnames, true);
125 assertEquals(expectedSchemaPath, odl.getPath());
126 assertFalse(odl.isAugmenting());
129 final Module module3 = TestUtils.findModule(context, "baz").get();
130 augmentations = module3.getAugmentations();
131 assertEquals(3, augmentations.size());
132 AugmentationSchemaNode augment1 = null;
133 AugmentationSchemaNode augment2 = null;
134 AugmentationSchemaNode augment3 = null;
135 for (final AugmentationSchemaNode as : augmentations) {
136 if (as.getWhenCondition() == null) {
138 } else if ("if:ifType='ds0'".equals(as.getWhenCondition().toString())) {
140 } else if ("if:ifType='ds2'".equals(as.getWhenCondition().toString())) {
144 assertNotNull(augment1);
145 assertNotNull(augment2);
146 assertNotNull(augment3);
148 assertEquals(1, augment1.getChildNodes().size());
149 final ContainerSchemaNode augmentHolder = (ContainerSchemaNode) augment1.getDataChildByName(QName.create(
150 module3.getQNameModule(), "augment-holder"));
151 assertNotNull(augmentHolder);
153 assertEquals(1, augment2.getChildNodes().size());
154 final ContainerSchemaNode augmentHolder2 = (ContainerSchemaNode) augment2.getDataChildByName(QName.create(
155 module3.getQNameModule(), "augment-holder2"));
156 assertNotNull(augmentHolder2);
158 assertEquals(1, augment3.getChildNodes().size());
159 final ChoiceCaseNode pause = (ChoiceCaseNode) augment3.getDataChildByName(QName.create(
160 module3.getQNameModule(), "pause"));
161 assertNotNull(pause);
165 public void testAugmentResolving() throws Exception {
166 final SchemaContext context = TestUtils.loadModules(getClass().getResource("/augment-test/augment-in-augment")
168 final Module module2 = TestUtils.findModule(context, "bar").get();
169 final ContainerSchemaNode interfaces = (ContainerSchemaNode) module2.getDataChildByName(QName.create(
170 module2.getQNameModule(), "interfaces"));
171 final ListSchemaNode ifEntry = (ListSchemaNode) interfaces.getDataChildByName(QName.create(
172 module2.getQNameModule(), "ifEntry"));
174 final List<QName> qnames = new ArrayList<>();
180 // augment "/br:interfaces/br:ifEntry" {
181 final ContainerSchemaNode augmentHolder = (ContainerSchemaNode) ifEntry.getDataChildByName(QName.create(BAZ,
183 TestUtils.checkIsAugmenting(augmentHolder, true);
184 assertEquals(Q2, augmentHolder.getQName());
185 assertEquals(SchemaPath.create(qnames, true), augmentHolder.getPath());
188 // augment "/br:interfaces/br:ifEntry/bz:augment-holder"
189 final LeafSchemaNode ds0ChannelNumber = (LeafSchemaNode) augmentHolder.getDataChildByName(QName.create(FOO,
190 "ds0ChannelNumber"));
191 final LeafSchemaNode interfaceId = (LeafSchemaNode) augmentHolder.getDataChildByName(QName.create(FOO,
193 final ContainerSchemaNode schemas = (ContainerSchemaNode) augmentHolder.getDataChildByName(QName.create(FOO,
195 final ChoiceSchemaNode odl = (ChoiceSchemaNode) augmentHolder.getDataChildByName(QName.create(FOO, "odl"));
197 assertNotNull(ds0ChannelNumber);
198 assertNotNull(interfaceId);
199 assertNotNull(schemas);
202 // leaf ds0ChannelNumber
203 QName qname = QName.create(FOO, "ds0ChannelNumber");
204 assertEquals(qname, ds0ChannelNumber.getQName());
206 assertEquals(SchemaPath.create(qnames, true), ds0ChannelNumber.getPath());
209 qname = QName.create(FOO, "interface-id");
210 assertEquals(qname, interfaceId.getQName());
211 qnames.set(3, qname);
212 assertEquals(SchemaPath.create(qnames, true), interfaceId.getPath());
215 qname = QName.create(FOO, "schemas");
216 assertEquals(qname, schemas.getQName());
217 qnames.set(3, qname);
218 assertEquals(SchemaPath.create(qnames, true), schemas.getPath());
221 qname = QName.create(FOO, "odl");
222 assertEquals(qname, odl.getQName());
223 qnames.set(3, qname);
224 assertEquals(SchemaPath.create(qnames, true), odl.getPath());
228 public void testAugmentedChoice() throws Exception {
229 final SchemaContext context = TestUtils.loadModules(getClass().getResource("/augment-test/augment-in-augment")
231 final Module module2 = TestUtils.findModule(context, "bar").get();
232 final ContainerSchemaNode interfaces = (ContainerSchemaNode) module2.getDataChildByName(QName.create(
233 module2.getQNameModule(), "interfaces"));
234 final ListSchemaNode ifEntry = (ListSchemaNode) interfaces.getDataChildByName(QName.create(
235 module2.getQNameModule(), "ifEntry"));
236 final ContainerSchemaNode augmentedHolder = (ContainerSchemaNode) ifEntry.getDataChildByName(QName.create(
237 BAZ, "augment-holder"));
238 TestUtils.checkIsAugmenting(augmentedHolder, true);
241 // augment "/br:interfaces/br:ifEntry/bz:augment-holder"
242 final ChoiceSchemaNode odl = (ChoiceSchemaNode) augmentedHolder.getDataChildByName(QName.create(FOO, "odl"));
244 final Set<ChoiceCaseNode> cases = odl.getCases();
245 assertEquals(4, cases.size());
247 ChoiceCaseNode id = null;
248 ChoiceCaseNode node1 = null;
249 ChoiceCaseNode node2 = null;
250 ChoiceCaseNode node3 = null;
252 for (final ChoiceCaseNode ccn : cases) {
253 if ("id".equals(ccn.getQName().getLocalName())) {
255 } else if ("node1".equals(ccn.getQName().getLocalName())) {
257 } else if ("node2".equals(ccn.getQName().getLocalName())) {
259 } else if ("node3".equals(ccn.getQName().getLocalName())) {
265 assertNotNull(node1);
266 assertNotNull(node2);
267 assertNotNull(node3);
269 final List<QName> qnames = new ArrayList<>();
273 qnames.add(QName.create(FOO, "odl"));
276 QName qname = QName.create(FOO, "id");
277 assertEquals(qname, id.getQName());
279 assertEquals(SchemaPath.create(qnames, true), id.getPath());
280 final Collection<DataSchemaNode> idChildren = id.getChildNodes();
281 assertEquals(1, idChildren.size());
284 qname = QName.create(FOO, "node1");
285 assertEquals(qname, node1.getQName());
286 qnames.set(4, qname);
287 assertEquals(SchemaPath.create(qnames, true), node1.getPath());
288 final Collection<DataSchemaNode> node1Children = node1.getChildNodes();
289 assertTrue(node1Children.isEmpty());
292 qname = QName.create(FOO, "node2");
293 assertEquals(qname, node2.getQName());
294 qnames.set(4, qname);
295 assertEquals(SchemaPath.create(qnames, true), node2.getPath());
296 final Collection<DataSchemaNode> node2Children = node2.getChildNodes();
297 assertTrue(node2Children.isEmpty());
300 qname = QName.create(FOO, "node3");
301 assertEquals(qname, node3.getQName());
302 qnames.set(4, qname);
303 assertEquals(SchemaPath.create(qnames, true), node3.getPath());
304 final Collection<DataSchemaNode> node3Children = node3.getChildNodes();
305 assertEquals(1, node3Children.size());
312 qnames.add(QName.create(FOO, "odl"));
315 qnames.add(QName.create(FOO, "id"));
316 qnames.add(QName.create(FOO, "id"));
317 final LeafSchemaNode caseIdChild = (LeafSchemaNode) idChildren.iterator().next();
318 assertNotNull(caseIdChild);
319 assertEquals(SchemaPath.create(qnames, true), caseIdChild.getPath());
322 qnames.set(4, QName.create(FOO, "node3"));
323 qnames.set(5, QName.create(FOO, "node3"));
324 final ContainerSchemaNode caseNode3Child = (ContainerSchemaNode) node3Children.iterator().next();
325 assertNotNull(caseNode3Child);
326 assertEquals(SchemaPath.create(qnames, true), caseNode3Child.getPath());
330 public void testAugmentRpc() throws Exception {
331 final SchemaContext context = TestUtils.loadModules(getClass().getResource("/augment-test/rpc").toURI());
332 final URI NS_BAR = URI.create("urn:opendaylight:bar");
333 final URI NS_FOO = URI.create("urn:opendaylight:foo");
334 final Revision revision = Revision.of("2013-10-11");
335 final Module bar = TestUtils.findModule(context, "bar").get();
336 final Set<RpcDefinition> rpcs = bar.getRpcs();
337 assertEquals(2, rpcs.size());
339 RpcDefinition submit = null;
340 for (final RpcDefinition rpc : rpcs) {
341 if ("submit".equals(rpc.getQName().getLocalName())) {
346 assertNotNull(submit);
348 final QName submitQName = QName.create(NS_BAR, revision, "submit");
349 assertEquals(submitQName, submit.getQName());
350 final ContainerSchemaNode input = submit.getInput();
351 final QName inputQName = QName.create(NS_BAR, revision, "input");
352 assertEquals(inputQName, input.getQName());
353 final ChoiceSchemaNode arguments = (ChoiceSchemaNode) input.getDataChildByName(QName.create(NS_BAR, revision,
355 final QName argumentsQName = QName.create(NS_BAR, revision, "arguments");
356 assertEquals(argumentsQName, arguments.getQName());
357 assertFalse(arguments.isAugmenting());
358 final Set<ChoiceCaseNode> cases = arguments.getCases();
359 assertEquals(3, cases.size());
361 ChoiceCaseNode attach = null;
362 ChoiceCaseNode create = null;
363 ChoiceCaseNode destroy = null;
364 for (final ChoiceCaseNode child : cases) {
365 if ("attach".equals(child.getQName().getLocalName())) {
367 } else if ("create".equals(child.getQName().getLocalName())) {
369 } else if ("destroy".equals(child.getQName().getLocalName())) {
373 assertNotNull(attach);
374 assertNotNull(create);
375 assertNotNull(destroy);
377 assertTrue(attach.isAugmenting());
378 assertTrue(create.isAugmenting());
379 assertTrue(destroy.isAugmenting());
381 final QName[] qnames = new QName[4];
382 qnames[0] = submitQName;
383 qnames[1] = inputQName;
384 qnames[2] = argumentsQName;
387 qnames[3] = QName.create(NS_FOO, revision, "attach");
388 assertEquals(qnames[3], attach.getQName());
389 assertEquals(SchemaPath.create(true, qnames), attach.getPath());
390 final Collection<DataSchemaNode> attachChildren = attach.getChildNodes();
391 assertEquals(1, attachChildren.size());
394 qnames[3] = QName.create(NS_FOO, revision, "create");
395 assertEquals(qnames[3], create.getQName());
396 assertEquals(SchemaPath.create(true, qnames), create.getPath());
397 final Collection<DataSchemaNode> createChildren = create.getChildNodes();
398 assertEquals(1, createChildren.size());
401 qnames[3] = QName.create(NS_FOO, revision, "destroy");
402 assertEquals(qnames[3], destroy.getQName());
403 assertEquals(SchemaPath.create(true, qnames), destroy.getPath());
404 final Collection<DataSchemaNode> destroyChildren = destroy.getChildNodes();
405 assertEquals(1, destroyChildren.size());
409 public void testAugmentInUsesResolving() throws Exception {
410 final SchemaContext context = TestUtils.loadModules(getClass().getResource("/augment-test/augment-in-uses")
412 assertEquals(1, context.getModules().size());
414 final Module test = context.getModules().iterator().next();
415 final DataNodeContainer links = (DataNodeContainer) test.getDataChildByName(QName.create(test.getQNameModule(),
417 final DataNodeContainer link = (DataNodeContainer) links.getDataChildByName(QName.create(test.getQNameModule(),
419 final DataNodeContainer nodes = (DataNodeContainer) link.getDataChildByName(QName.create(test.getQNameModule(),
421 final ContainerSchemaNode node = (ContainerSchemaNode) nodes.getDataChildByName(QName.create(
422 test.getQNameModule(), "node"));
423 final Set<AugmentationSchemaNode> augments = node.getAvailableAugmentations();
424 assertEquals(1, augments.size());
425 assertEquals(1, node.getChildNodes().size());
426 final LeafSchemaNode id = (LeafSchemaNode) node.getDataChildByName(QName.create(test.getQNameModule(), "id"));
427 assertTrue(id.isAugmenting());