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.Arrays;
18 import java.util.Collection;
19 import java.util.Collections;
20 import java.util.List;
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.common.YangConstants;
27 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
28 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
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.DataNodeContainer;
32 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
33 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
34 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
35 import org.opendaylight.yangtools.yang.model.api.Module;
36 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
37 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
38 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
39 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.TypeUtils;
41 public class AugmentTest {
42 private static final QNameModule FOO = QNameModule.create(
43 URI.create("urn:opendaylight.foo"), Revision.valueOf("2013-10-13"));
44 private static final QNameModule BAR = QNameModule.create(
45 URI.create("urn:opendaylight.bar"), Revision.valueOf("2013-10-14"));
46 private static final QNameModule BAZ = QNameModule.create(
47 URI.create("urn:opendaylight.baz"), Revision.valueOf("2013-10-15"));
49 private static final QName Q0 = QName.create(BAR, "interfaces");
50 private static final QName Q1 = QName.create(BAR, "ifEntry");
51 private static final QName Q2 = QName.create(BAZ, "augment-holder");
54 public void testAugmentParsing() throws Exception {
55 final SchemaContext context = TestUtils.loadModules(getClass().getResource("/augment-test/augment-in-augment")
57 SchemaPath expectedSchemaPath;
58 final List<QName> qnames = new ArrayList<>();
64 final Module module1 = TestUtils.findModule(context, "foo").get();
65 Set<AugmentationSchema> augmentations = module1.getAugmentations();
66 assertEquals(1, augmentations.size());
67 final AugmentationSchema augment = augmentations.iterator().next();
68 assertNotNull(augment);
70 expectedSchemaPath = SchemaPath.create(qnames, true);
71 assertEquals(expectedSchemaPath, augment.getTargetPath());
73 final Collection<DataSchemaNode> augmentChildren = augment.getChildNodes();
74 assertEquals(4, augmentChildren.size());
75 for (final DataSchemaNode dsn : augmentChildren) {
76 TestUtils.checkIsAugmenting(dsn, false);
79 final LeafSchemaNode ds0ChannelNumber = (LeafSchemaNode) augment.getDataChildByName(QName.create(
80 module1.getQNameModule(), "ds0ChannelNumber"));
81 final LeafSchemaNode interfaceId = (LeafSchemaNode) augment.getDataChildByName(QName.create(
82 module1.getQNameModule(), "interface-id"));
83 final ContainerSchemaNode schemas = (ContainerSchemaNode) augment.getDataChildByName(QName.create(
84 module1.getQNameModule(), "schemas"));
85 final ChoiceSchemaNode odl = (ChoiceSchemaNode) augment.getDataChildByName(QName.create(
86 module1.getQNameModule(), "odl"));
88 assertNotNull(ds0ChannelNumber);
89 assertNotNull(interfaceId);
90 assertNotNull(schemas);
93 // leaf ds0ChannelNumber
94 QName qname = QName.create(FOO, "ds0ChannelNumber");
96 assertEquals(qname, ds0ChannelNumber.getQName());
97 expectedSchemaPath = SchemaPath.create(qnames, true);
98 assertEquals(expectedSchemaPath, ds0ChannelNumber.getPath());
99 assertFalse(ds0ChannelNumber.isAugmenting());
100 // type of leaf ds0ChannelNumber
101 final QName typeQName = QName.create(YangConstants.RFC6020_YANG_MODULE, TypeUtils.STRING);
102 final List<QName> typePath = Collections.singletonList(typeQName);
103 expectedSchemaPath = SchemaPath.create(typePath, true);
104 assertEquals(expectedSchemaPath, ds0ChannelNumber.getType().getPath());
107 qname = QName.create(FOO, "interface-id");
108 assertEquals(qname, interfaceId.getQName());
109 qnames.set(3, qname);
110 expectedSchemaPath = SchemaPath.create(qnames, true);
111 assertEquals(expectedSchemaPath, interfaceId.getPath());
112 assertFalse(interfaceId.isAugmenting());
115 qname = QName.create(FOO, "schemas");
116 assertEquals(qname, schemas.getQName());
117 qnames.set(3, qname);
118 expectedSchemaPath = SchemaPath.create(qnames, true);
119 assertEquals(expectedSchemaPath, schemas.getPath());
120 assertFalse(schemas.isAugmenting());
123 qname = QName.create(FOO, "odl");
124 assertEquals(qname, odl.getQName());
125 qnames.set(3, qname);
126 expectedSchemaPath = SchemaPath.create(qnames, true);
127 assertEquals(expectedSchemaPath, odl.getPath());
128 assertFalse(odl.isAugmenting());
131 final Module module3 = TestUtils.findModule(context, "baz").get();
132 augmentations = module3.getAugmentations();
133 assertEquals(3, augmentations.size());
134 AugmentationSchema augment1 = null;
135 AugmentationSchema augment2 = null;
136 AugmentationSchema augment3 = null;
137 for (final AugmentationSchema as : augmentations) {
138 if (as.getWhenCondition() == null) {
140 } else if ("if:ifType='ds0'".equals(as.getWhenCondition().toString())) {
142 } else if ("if:ifType='ds2'".equals(as.getWhenCondition().toString())) {
146 assertNotNull(augment1);
147 assertNotNull(augment2);
148 assertNotNull(augment3);
150 assertEquals(1, augment1.getChildNodes().size());
151 final ContainerSchemaNode augmentHolder = (ContainerSchemaNode) augment1.getDataChildByName(QName.create(
152 module3.getQNameModule(), "augment-holder"));
153 assertNotNull(augmentHolder);
155 assertEquals(1, augment2.getChildNodes().size());
156 final ContainerSchemaNode augmentHolder2 = (ContainerSchemaNode) augment2.getDataChildByName(QName.create(
157 module3.getQNameModule(), "augment-holder2"));
158 assertNotNull(augmentHolder2);
160 assertEquals(1, augment3.getChildNodes().size());
161 final ChoiceCaseNode pause = (ChoiceCaseNode) augment3.getDataChildByName(QName.create(
162 module3.getQNameModule(), "pause"));
163 assertNotNull(pause);
167 public void testAugmentResolving() throws Exception {
168 final SchemaContext context = TestUtils.loadModules(getClass().getResource("/augment-test/augment-in-augment")
170 final Module module2 = TestUtils.findModule(context, "bar").get();
171 final ContainerSchemaNode interfaces = (ContainerSchemaNode) module2.getDataChildByName(QName.create(
172 module2.getQNameModule(), "interfaces"));
173 final ListSchemaNode ifEntry = (ListSchemaNode) interfaces.getDataChildByName(QName.create(
174 module2.getQNameModule(), "ifEntry"));
176 SchemaPath expectedPath;
177 final List<QName> qnames = new ArrayList<>();
183 // augment "/br:interfaces/br:ifEntry" {
184 final ContainerSchemaNode augmentHolder = (ContainerSchemaNode) ifEntry.getDataChildByName(QName.create(BAZ,
186 TestUtils.checkIsAugmenting(augmentHolder, true);
187 assertEquals(Q2, augmentHolder.getQName());
188 expectedPath = SchemaPath.create(qnames, true);
189 assertEquals(expectedPath, augmentHolder.getPath());
192 // augment "/br:interfaces/br:ifEntry/bz:augment-holder"
193 final LeafSchemaNode ds0ChannelNumber = (LeafSchemaNode) augmentHolder.getDataChildByName(QName.create(FOO,
194 "ds0ChannelNumber"));
195 final LeafSchemaNode interfaceId = (LeafSchemaNode) augmentHolder.getDataChildByName(QName.create(FOO,
197 final ContainerSchemaNode schemas = (ContainerSchemaNode) augmentHolder.getDataChildByName(QName.create(FOO,
199 final ChoiceSchemaNode odl = (ChoiceSchemaNode) augmentHolder.getDataChildByName(QName.create(FOO, "odl"));
201 assertNotNull(ds0ChannelNumber);
202 assertNotNull(interfaceId);
203 assertNotNull(schemas);
206 // leaf ds0ChannelNumber
207 QName qname = QName.create(FOO, "ds0ChannelNumber");
208 assertEquals(qname, ds0ChannelNumber.getQName());
210 expectedPath = SchemaPath.create(qnames, true);
211 assertEquals(expectedPath, ds0ChannelNumber.getPath());
214 qname = QName.create(FOO, "interface-id");
215 assertEquals(qname, interfaceId.getQName());
216 qnames.set(3, qname);
217 expectedPath = SchemaPath.create(qnames, true);
218 assertEquals(expectedPath, interfaceId.getPath());
221 qname = QName.create(FOO, "schemas");
222 assertEquals(qname, schemas.getQName());
223 qnames.set(3, qname);
224 expectedPath = SchemaPath.create(qnames, true);
225 assertEquals(expectedPath, schemas.getPath());
228 qname = QName.create(FOO, "odl");
229 assertEquals(qname, odl.getQName());
230 qnames.set(3, qname);
231 expectedPath = SchemaPath.create(qnames, true);
232 assertEquals(expectedPath, odl.getPath());
236 public void testAugmentedChoice() throws Exception {
237 final SchemaContext context = TestUtils.loadModules(getClass().getResource("/augment-test/augment-in-augment")
239 final Module module2 = TestUtils.findModule(context, "bar").get();
240 final ContainerSchemaNode interfaces = (ContainerSchemaNode) module2.getDataChildByName(QName.create(
241 module2.getQNameModule(), "interfaces"));
242 final ListSchemaNode ifEntry = (ListSchemaNode) interfaces.getDataChildByName(QName.create(
243 module2.getQNameModule(), "ifEntry"));
244 final ContainerSchemaNode augmentedHolder = (ContainerSchemaNode) ifEntry.getDataChildByName(QName.create(
245 BAZ, "augment-holder"));
246 TestUtils.checkIsAugmenting(augmentedHolder, true);
249 // augment "/br:interfaces/br:ifEntry/bz:augment-holder"
250 final ChoiceSchemaNode odl = (ChoiceSchemaNode) augmentedHolder.getDataChildByName(QName.create(FOO, "odl"));
252 final Set<ChoiceCaseNode> cases = odl.getCases();
253 assertEquals(4, cases.size());
255 ChoiceCaseNode id = null;
256 ChoiceCaseNode node1 = null;
257 ChoiceCaseNode node2 = null;
258 ChoiceCaseNode node3 = null;
260 for (final ChoiceCaseNode ccn : cases) {
261 if ("id".equals(ccn.getQName().getLocalName())) {
263 } else if ("node1".equals(ccn.getQName().getLocalName())) {
265 } else if ("node2".equals(ccn.getQName().getLocalName())) {
267 } else if ("node3".equals(ccn.getQName().getLocalName())) {
273 assertNotNull(node1);
274 assertNotNull(node2);
275 assertNotNull(node3);
277 SchemaPath expectedPath;
278 final List<QName> qnames = new ArrayList<>();
282 qnames.add(QName.create(FOO, "odl"));
285 QName qname = QName.create(FOO, "id");
286 assertEquals(qname, id.getQName());
288 expectedPath = SchemaPath.create(qnames, true);
289 assertEquals(expectedPath, id.getPath());
290 final Collection<DataSchemaNode> idChildren = id.getChildNodes();
291 assertEquals(1, idChildren.size());
294 qname = QName.create(FOO, "node1");
295 assertEquals(qname, node1.getQName());
296 qnames.set(4, qname);
297 expectedPath = SchemaPath.create(qnames, true);
298 assertEquals(expectedPath, node1.getPath());
299 final Collection<DataSchemaNode> node1Children = node1.getChildNodes();
300 assertTrue(node1Children.isEmpty());
303 qname = QName.create(FOO, "node2");
304 assertEquals(qname, node2.getQName());
305 qnames.set(4, qname);
306 expectedPath = SchemaPath.create(qnames, true);
307 assertEquals(expectedPath, node2.getPath());
308 final Collection<DataSchemaNode> node2Children = node2.getChildNodes();
309 assertTrue(node2Children.isEmpty());
312 qname = QName.create(FOO, "node3");
313 assertEquals(qname, node3.getQName());
314 qnames.set(4, qname);
315 expectedPath = SchemaPath.create(qnames, true);
316 assertEquals(expectedPath, node3.getPath());
317 final Collection<DataSchemaNode> node3Children = node3.getChildNodes();
318 assertEquals(1, node3Children.size());
325 qnames.add(QName.create(FOO, "odl"));
328 qnames.add(QName.create(FOO, "id"));
329 qnames.add(QName.create(FOO, "id"));
330 final LeafSchemaNode caseIdChild = (LeafSchemaNode) idChildren.iterator().next();
331 assertNotNull(caseIdChild);
332 expectedPath = SchemaPath.create(qnames, true);
333 assertEquals(expectedPath, caseIdChild.getPath());
336 qnames.set(4, QName.create(FOO, "node3"));
337 qnames.set(5, QName.create(FOO, "node3"));
338 final ContainerSchemaNode caseNode3Child = (ContainerSchemaNode) node3Children.iterator().next();
339 assertNotNull(caseNode3Child);
340 expectedPath = SchemaPath.create(qnames, true);
341 assertEquals(expectedPath, caseNode3Child.getPath());
345 public void testAugmentRpc() throws Exception {
346 final SchemaContext context = TestUtils.loadModules(getClass().getResource("/augment-test/rpc").toURI());
347 final URI NS_BAR = URI.create("urn:opendaylight:bar");
348 final URI NS_FOO = URI.create("urn:opendaylight:foo");
349 final Revision revision = Revision.valueOf("2013-10-11");
350 final Module bar = TestUtils.findModule(context, "bar").get();
351 final Set<RpcDefinition> rpcs = bar.getRpcs();
352 assertEquals(2, rpcs.size());
354 RpcDefinition submit = null;
355 for (final RpcDefinition rpc : rpcs) {
356 if ("submit".equals(rpc.getQName().getLocalName())) {
361 assertNotNull(submit);
363 final QName submitQName = QName.create(NS_BAR, revision, "submit");
364 assertEquals(submitQName, submit.getQName());
365 final ContainerSchemaNode input = submit.getInput();
366 final QName inputQName = QName.create(NS_BAR, revision, "input");
367 assertEquals(inputQName, input.getQName());
368 final ChoiceSchemaNode arguments = (ChoiceSchemaNode) input.getDataChildByName(QName.create(NS_BAR, revision,
370 final QName argumentsQName = QName.create(NS_BAR, revision, "arguments");
371 assertEquals(argumentsQName, arguments.getQName());
372 assertFalse(arguments.isAugmenting());
373 final Set<ChoiceCaseNode> cases = arguments.getCases();
374 assertEquals(3, cases.size());
376 ChoiceCaseNode attach = null;
377 ChoiceCaseNode create = null;
378 ChoiceCaseNode destroy = null;
379 for (final ChoiceCaseNode child : cases) {
380 if ("attach".equals(child.getQName().getLocalName())) {
382 } else if ("create".equals(child.getQName().getLocalName())) {
384 } else if ("destroy".equals(child.getQName().getLocalName())) {
388 assertNotNull(attach);
389 assertNotNull(create);
390 assertNotNull(destroy);
392 assertTrue(attach.isAugmenting());
393 assertTrue(create.isAugmenting());
394 assertTrue(destroy.isAugmenting());
396 SchemaPath expectedPath;
397 final QName[] qnames = new QName[4];
398 qnames[0] = submitQName;
399 qnames[1] = inputQName;
400 qnames[2] = argumentsQName;
403 qnames[3] = QName.create(NS_FOO, revision, "attach");
404 assertEquals(qnames[3], attach.getQName());
405 expectedPath = SchemaPath.create(Arrays.asList(qnames), true);
406 assertEquals(expectedPath, attach.getPath());
407 final Collection<DataSchemaNode> attachChildren = attach.getChildNodes();
408 assertEquals(1, attachChildren.size());
411 qnames[3] = QName.create(NS_FOO, revision, "create");
412 assertEquals(qnames[3], create.getQName());
413 expectedPath = SchemaPath.create(Arrays.asList(qnames), true);
414 assertEquals(expectedPath, create.getPath());
415 final Collection<DataSchemaNode> createChildren = create.getChildNodes();
416 assertEquals(1, createChildren.size());
419 qnames[3] = QName.create(NS_FOO, revision, "destroy");
420 assertEquals(qnames[3], destroy.getQName());
421 expectedPath = SchemaPath.create(Arrays.asList(qnames), true);
422 assertEquals(expectedPath, destroy.getPath());
423 final Collection<DataSchemaNode> destroyChildren = destroy.getChildNodes();
424 assertEquals(1, destroyChildren.size());
428 public void testAugmentInUsesResolving() throws Exception {
429 final SchemaContext context = TestUtils.loadModules(getClass().getResource("/augment-test/augment-in-uses")
431 assertEquals(1, context.getModules().size());
433 final Module test = context.getModules().iterator().next();
434 final DataNodeContainer links = (DataNodeContainer) test.getDataChildByName(QName.create(test.getQNameModule(),
436 final DataNodeContainer link = (DataNodeContainer) links.getDataChildByName(QName.create(test.getQNameModule(),
438 final DataNodeContainer nodes = (DataNodeContainer) link.getDataChildByName(QName.create(test.getQNameModule(),
440 final ContainerSchemaNode node = (ContainerSchemaNode) nodes.getDataChildByName(QName.create(
441 test.getQNameModule(), "node"));
442 final Set<AugmentationSchema> augments = node.getAvailableAugmentations();
443 assertEquals(1, augments.size());
444 assertEquals(1, node.getChildNodes().size());
445 final LeafSchemaNode id = (LeafSchemaNode) node.getDataChildByName(QName.create(test.getQNameModule(), "id"));
446 assertTrue(id.isAugmenting());