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.Date;
21 import java.util.List;
23 import org.junit.Test;
24 import org.opendaylight.yangtools.yang.common.QName;
25 import org.opendaylight.yangtools.yang.common.QNameModule;
26 import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil;
27 import org.opendaylight.yangtools.yang.common.YangConstants;
28 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
29 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
30 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
31 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
32 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
33 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
34 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
35 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
36 import org.opendaylight.yangtools.yang.model.api.Module;
37 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
38 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
39 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
40 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.TypeUtils;
42 public class AugmentTest {
43 private static final QNameModule FOO = QNameModule.create(
44 URI.create("urn:opendaylight.foo"), QName.parseRevision("2013-10-13"));
45 private static final QNameModule BAR = QNameModule.create(
46 URI.create("urn:opendaylight.bar"), QName.parseRevision("2013-10-14"));
47 private static final QNameModule BAZ = QNameModule.create(
48 URI.create("urn:opendaylight.baz"), QName.parseRevision("2013-10-15"));
50 private static final QName Q0 = QName.create(BAR, "interfaces");
51 private static final QName Q1 = QName.create(BAR, "ifEntry");
52 private static final QName Q2 = QName.create(BAZ, "augment-holder");
55 public void testAugmentParsing() throws Exception {
56 final SchemaContext context = TestUtils.loadModules(getClass().getResource("/augment-test/augment-in-augment")
58 SchemaPath expectedSchemaPath;
59 final List<QName> qnames = new ArrayList<>();
65 final Module module1 = TestUtils.findModule(context, "foo").get();
66 Set<AugmentationSchema> augmentations = module1.getAugmentations();
67 assertEquals(1, augmentations.size());
68 final AugmentationSchema augment = augmentations.iterator().next();
69 assertNotNull(augment);
71 expectedSchemaPath = SchemaPath.create(qnames, true);
72 assertEquals(expectedSchemaPath, augment.getTargetPath());
74 final Collection<DataSchemaNode> augmentChildren = augment.getChildNodes();
75 assertEquals(4, augmentChildren.size());
76 for (final DataSchemaNode dsn : augmentChildren) {
77 TestUtils.checkIsAugmenting(dsn, false);
80 final LeafSchemaNode ds0ChannelNumber = (LeafSchemaNode) augment.getDataChildByName(QName.create(
81 module1.getQNameModule(), "ds0ChannelNumber"));
82 final LeafSchemaNode interfaceId = (LeafSchemaNode) augment.getDataChildByName(QName.create(
83 module1.getQNameModule(), "interface-id"));
84 final ContainerSchemaNode schemas = (ContainerSchemaNode) augment.getDataChildByName(QName.create(
85 module1.getQNameModule(), "schemas"));
86 final ChoiceSchemaNode odl = (ChoiceSchemaNode) augment.getDataChildByName(QName.create(
87 module1.getQNameModule(), "odl"));
89 assertNotNull(ds0ChannelNumber);
90 assertNotNull(interfaceId);
91 assertNotNull(schemas);
94 // leaf ds0ChannelNumber
95 QName qname = QName.create(FOO, "ds0ChannelNumber");
97 assertEquals(qname, ds0ChannelNumber.getQName());
98 expectedSchemaPath = SchemaPath.create(qnames, true);
99 assertEquals(expectedSchemaPath, ds0ChannelNumber.getPath());
100 assertFalse(ds0ChannelNumber.isAugmenting());
101 // type of leaf ds0ChannelNumber
102 final QName typeQName = QName.create(YangConstants.RFC6020_YANG_MODULE, TypeUtils.STRING);
103 final List<QName> typePath = Collections.singletonList(typeQName);
104 expectedSchemaPath = SchemaPath.create(typePath, true);
105 assertEquals(expectedSchemaPath, ds0ChannelNumber.getType().getPath());
108 qname = QName.create(FOO, "interface-id");
109 assertEquals(qname, interfaceId.getQName());
110 qnames.set(3, qname);
111 expectedSchemaPath = SchemaPath.create(qnames, true);
112 assertEquals(expectedSchemaPath, interfaceId.getPath());
113 assertFalse(interfaceId.isAugmenting());
116 qname = QName.create(FOO, "schemas");
117 assertEquals(qname, schemas.getQName());
118 qnames.set(3, qname);
119 expectedSchemaPath = SchemaPath.create(qnames, true);
120 assertEquals(expectedSchemaPath, schemas.getPath());
121 assertFalse(schemas.isAugmenting());
124 qname = QName.create(FOO, "odl");
125 assertEquals(qname, odl.getQName());
126 qnames.set(3, qname);
127 expectedSchemaPath = SchemaPath.create(qnames, true);
128 assertEquals(expectedSchemaPath, odl.getPath());
129 assertFalse(odl.isAugmenting());
132 final Module module3 = TestUtils.findModule(context, "baz").get();
133 augmentations = module3.getAugmentations();
134 assertEquals(3, augmentations.size());
135 AugmentationSchema augment1 = null;
136 AugmentationSchema augment2 = null;
137 AugmentationSchema augment3 = null;
138 for (final AugmentationSchema as : augmentations) {
139 if (as.getWhenCondition() == null) {
141 } else if ("if:ifType='ds0'".equals(as.getWhenCondition().toString())) {
143 } else if ("if:ifType='ds2'".equals(as.getWhenCondition().toString())) {
147 assertNotNull(augment1);
148 assertNotNull(augment2);
149 assertNotNull(augment3);
151 assertEquals(1, augment1.getChildNodes().size());
152 final ContainerSchemaNode augmentHolder = (ContainerSchemaNode) augment1.getDataChildByName(QName.create(
153 module3.getQNameModule(), "augment-holder"));
154 assertNotNull(augmentHolder);
156 assertEquals(1, augment2.getChildNodes().size());
157 final ContainerSchemaNode augmentHolder2 = (ContainerSchemaNode) augment2.getDataChildByName(QName.create(
158 module3.getQNameModule(), "augment-holder2"));
159 assertNotNull(augmentHolder2);
161 assertEquals(1, augment3.getChildNodes().size());
162 final ChoiceCaseNode pause = (ChoiceCaseNode) augment3.getDataChildByName(QName.create(
163 module3.getQNameModule(), "pause"));
164 assertNotNull(pause);
168 public void testAugmentResolving() throws Exception {
169 final SchemaContext context = TestUtils.loadModules(getClass().getResource("/augment-test/augment-in-augment")
171 final Module module2 = TestUtils.findModule(context, "bar").get();
172 final ContainerSchemaNode interfaces = (ContainerSchemaNode) module2.getDataChildByName(QName.create(
173 module2.getQNameModule(), "interfaces"));
174 final ListSchemaNode ifEntry = (ListSchemaNode) interfaces.getDataChildByName(QName.create(
175 module2.getQNameModule(), "ifEntry"));
177 SchemaPath expectedPath;
178 final List<QName> qnames = new ArrayList<>();
184 // augment "/br:interfaces/br:ifEntry" {
185 final ContainerSchemaNode augmentHolder = (ContainerSchemaNode) ifEntry.getDataChildByName(QName.create(BAZ,
187 TestUtils.checkIsAugmenting(augmentHolder, true);
188 assertEquals(Q2, augmentHolder.getQName());
189 expectedPath = SchemaPath.create(qnames, true);
190 assertEquals(expectedPath, augmentHolder.getPath());
193 // augment "/br:interfaces/br:ifEntry/bz:augment-holder"
194 final LeafSchemaNode ds0ChannelNumber = (LeafSchemaNode) augmentHolder.getDataChildByName(QName.create(FOO,
195 "ds0ChannelNumber"));
196 final LeafSchemaNode interfaceId = (LeafSchemaNode) augmentHolder.getDataChildByName(QName.create(FOO,
198 final ContainerSchemaNode schemas = (ContainerSchemaNode) augmentHolder.getDataChildByName(QName.create(FOO,
200 final ChoiceSchemaNode odl = (ChoiceSchemaNode) augmentHolder.getDataChildByName(QName.create(FOO, "odl"));
202 assertNotNull(ds0ChannelNumber);
203 assertNotNull(interfaceId);
204 assertNotNull(schemas);
207 // leaf ds0ChannelNumber
208 QName qname = QName.create(FOO, "ds0ChannelNumber");
209 assertEquals(qname, ds0ChannelNumber.getQName());
211 expectedPath = SchemaPath.create(qnames, true);
212 assertEquals(expectedPath, ds0ChannelNumber.getPath());
215 qname = QName.create(FOO, "interface-id");
216 assertEquals(qname, interfaceId.getQName());
217 qnames.set(3, qname);
218 expectedPath = SchemaPath.create(qnames, true);
219 assertEquals(expectedPath, interfaceId.getPath());
222 qname = QName.create(FOO, "schemas");
223 assertEquals(qname, schemas.getQName());
224 qnames.set(3, qname);
225 expectedPath = SchemaPath.create(qnames, true);
226 assertEquals(expectedPath, schemas.getPath());
229 qname = QName.create(FOO, "odl");
230 assertEquals(qname, odl.getQName());
231 qnames.set(3, qname);
232 expectedPath = SchemaPath.create(qnames, true);
233 assertEquals(expectedPath, odl.getPath());
237 public void testAugmentedChoice() throws Exception {
238 final SchemaContext context = TestUtils.loadModules(getClass().getResource("/augment-test/augment-in-augment")
240 final Module module2 = TestUtils.findModule(context, "bar").get();
241 final ContainerSchemaNode interfaces = (ContainerSchemaNode) module2.getDataChildByName(QName.create(
242 module2.getQNameModule(), "interfaces"));
243 final ListSchemaNode ifEntry = (ListSchemaNode) interfaces.getDataChildByName(QName.create(
244 module2.getQNameModule(), "ifEntry"));
245 final ContainerSchemaNode augmentedHolder = (ContainerSchemaNode) ifEntry.getDataChildByName(QName.create(
246 BAZ, "augment-holder"));
247 TestUtils.checkIsAugmenting(augmentedHolder, true);
250 // augment "/br:interfaces/br:ifEntry/bz:augment-holder"
251 final ChoiceSchemaNode odl = (ChoiceSchemaNode) augmentedHolder.getDataChildByName(QName.create(FOO, "odl"));
253 final Set<ChoiceCaseNode> cases = odl.getCases();
254 assertEquals(4, cases.size());
256 ChoiceCaseNode id = null;
257 ChoiceCaseNode node1 = null;
258 ChoiceCaseNode node2 = null;
259 ChoiceCaseNode node3 = null;
261 for (final ChoiceCaseNode ccn : cases) {
262 if ("id".equals(ccn.getQName().getLocalName())) {
264 } else if ("node1".equals(ccn.getQName().getLocalName())) {
266 } else if ("node2".equals(ccn.getQName().getLocalName())) {
268 } else if ("node3".equals(ccn.getQName().getLocalName())) {
274 assertNotNull(node1);
275 assertNotNull(node2);
276 assertNotNull(node3);
278 SchemaPath expectedPath;
279 final List<QName> qnames = new ArrayList<>();
283 qnames.add(QName.create(FOO, "odl"));
286 QName qname = QName.create(FOO, "id");
287 assertEquals(qname, id.getQName());
289 expectedPath = SchemaPath.create(qnames, true);
290 assertEquals(expectedPath, id.getPath());
291 final Collection<DataSchemaNode> idChildren = id.getChildNodes();
292 assertEquals(1, idChildren.size());
295 qname = QName.create(FOO, "node1");
296 assertEquals(qname, node1.getQName());
297 qnames.set(4, qname);
298 expectedPath = SchemaPath.create(qnames, true);
299 assertEquals(expectedPath, node1.getPath());
300 final Collection<DataSchemaNode> node1Children = node1.getChildNodes();
301 assertTrue(node1Children.isEmpty());
304 qname = QName.create(FOO, "node2");
305 assertEquals(qname, node2.getQName());
306 qnames.set(4, qname);
307 expectedPath = SchemaPath.create(qnames, true);
308 assertEquals(expectedPath, node2.getPath());
309 final Collection<DataSchemaNode> node2Children = node2.getChildNodes();
310 assertTrue(node2Children.isEmpty());
313 qname = QName.create(FOO, "node3");
314 assertEquals(qname, node3.getQName());
315 qnames.set(4, qname);
316 expectedPath = SchemaPath.create(qnames, true);
317 assertEquals(expectedPath, node3.getPath());
318 final Collection<DataSchemaNode> node3Children = node3.getChildNodes();
319 assertEquals(1, node3Children.size());
326 qnames.add(QName.create(FOO, "odl"));
329 qnames.add(QName.create(FOO, "id"));
330 qnames.add(QName.create(FOO, "id"));
331 final LeafSchemaNode caseIdChild = (LeafSchemaNode) idChildren.iterator().next();
332 assertNotNull(caseIdChild);
333 expectedPath = SchemaPath.create(qnames, true);
334 assertEquals(expectedPath, caseIdChild.getPath());
337 qnames.set(4, QName.create(FOO, "node3"));
338 qnames.set(5, QName.create(FOO, "node3"));
339 final ContainerSchemaNode caseNode3Child = (ContainerSchemaNode) node3Children.iterator().next();
340 assertNotNull(caseNode3Child);
341 expectedPath = SchemaPath.create(qnames, true);
342 assertEquals(expectedPath, caseNode3Child.getPath());
346 public void testAugmentRpc() throws Exception {
347 final SchemaContext context = TestUtils.loadModules(getClass().getResource("/augment-test/rpc").toURI());
348 final URI NS_BAR = URI.create("urn:opendaylight:bar");
349 final URI NS_FOO = URI.create("urn:opendaylight:foo");
350 final Date revision = SimpleDateFormatUtil.getRevisionFormat().parse("2013-10-11");
352 final Module bar = TestUtils.findModule(context, "bar").get();
353 final Set<RpcDefinition> rpcs = bar.getRpcs();
354 assertEquals(2, rpcs.size());
356 RpcDefinition submit = null;
357 for (final RpcDefinition rpc : rpcs) {
358 if ("submit".equals(rpc.getQName().getLocalName())) {
363 assertNotNull(submit);
365 final QName submitQName = QName.create(NS_BAR, revision, "submit");
366 assertEquals(submitQName, submit.getQName());
367 final ContainerSchemaNode input = submit.getInput();
368 final QName inputQName = QName.create(NS_BAR, revision, "input");
369 assertEquals(inputQName, input.getQName());
370 final ChoiceSchemaNode arguments = (ChoiceSchemaNode) input.getDataChildByName(QName.create(NS_BAR, revision,
372 final QName argumentsQName = QName.create(NS_BAR, revision, "arguments");
373 assertEquals(argumentsQName, arguments.getQName());
374 assertFalse(arguments.isAugmenting());
375 final Set<ChoiceCaseNode> cases = arguments.getCases();
376 assertEquals(3, cases.size());
378 ChoiceCaseNode attach = null;
379 ChoiceCaseNode create = null;
380 ChoiceCaseNode destroy = null;
381 for (final ChoiceCaseNode child : cases) {
382 if ("attach".equals(child.getQName().getLocalName())) {
384 } else if ("create".equals(child.getQName().getLocalName())) {
386 } else if ("destroy".equals(child.getQName().getLocalName())) {
390 assertNotNull(attach);
391 assertNotNull(create);
392 assertNotNull(destroy);
394 assertTrue(attach.isAugmenting());
395 assertTrue(create.isAugmenting());
396 assertTrue(destroy.isAugmenting());
398 SchemaPath expectedPath;
399 final QName[] qnames = new QName[4];
400 qnames[0] = submitQName;
401 qnames[1] = inputQName;
402 qnames[2] = argumentsQName;
405 qnames[3] = QName.create(NS_FOO, revision, "attach");
406 assertEquals(qnames[3], attach.getQName());
407 expectedPath = SchemaPath.create(Arrays.asList(qnames), true);
408 assertEquals(expectedPath, attach.getPath());
409 final Collection<DataSchemaNode> attachChildren = attach.getChildNodes();
410 assertEquals(1, attachChildren.size());
413 qnames[3] = QName.create(NS_FOO, revision, "create");
414 assertEquals(qnames[3], create.getQName());
415 expectedPath = SchemaPath.create(Arrays.asList(qnames), true);
416 assertEquals(expectedPath, create.getPath());
417 final Collection<DataSchemaNode> createChildren = create.getChildNodes();
418 assertEquals(1, createChildren.size());
421 qnames[3] = QName.create(NS_FOO, revision, "destroy");
422 assertEquals(qnames[3], destroy.getQName());
423 expectedPath = SchemaPath.create(Arrays.asList(qnames), true);
424 assertEquals(expectedPath, destroy.getPath());
425 final Collection<DataSchemaNode> destroyChildren = destroy.getChildNodes();
426 assertEquals(1, destroyChildren.size());
430 public void testAugmentInUsesResolving() throws Exception {
431 final SchemaContext context = TestUtils.loadModules(getClass().getResource("/augment-test/augment-in-uses")
433 assertEquals(1, context.getModules().size());
435 final Module test = context.getModules().iterator().next();
436 final DataNodeContainer links = (DataNodeContainer) test.getDataChildByName(QName.create(test.getQNameModule(),
438 final DataNodeContainer link = (DataNodeContainer) links.getDataChildByName(QName.create(test.getQNameModule(),
440 final DataNodeContainer nodes = (DataNodeContainer) link.getDataChildByName(QName.create(test.getQNameModule(),
442 final ContainerSchemaNode node = (ContainerSchemaNode) nodes.getDataChildByName(QName.create(
443 test.getQNameModule(), "node"));
444 final Set<AugmentationSchema> augments = node.getAvailableAugmentations();
445 assertEquals(1, augments.size());
446 assertEquals(1, node.getChildNodes().size());
447 final LeafSchemaNode id = (LeafSchemaNode) node.getDataChildByName(QName.create(test.getQNameModule(), "id"));
448 assertTrue(id.isAugmenting());