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;
15 import java.io.FileNotFoundException;
17 import java.text.DateFormat;
18 import java.text.ParseException;
19 import java.util.ArrayList;
20 import java.util.Arrays;
21 import java.util.Collection;
22 import java.util.Collections;
23 import java.util.Date;
24 import java.util.List;
26 import org.junit.BeforeClass;
27 import org.junit.Test;
28 import org.opendaylight.yangtools.yang.common.QName;
29 import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil;
30 import org.opendaylight.yangtools.yang.common.YangConstants;
31 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
32 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
33 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
34 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
35 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
36 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
37 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
38 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
39 import org.opendaylight.yangtools.yang.model.api.Module;
40 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
41 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
42 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.TypeUtils;
44 public class AugmentTest {
45 private static final URI fooNS = URI.create("urn:opendaylight.foo");
46 private static final URI barNS = URI.create("urn:opendaylight.bar");
47 private static final URI bazNS = URI.create("urn:opendaylight.baz");
48 private static Date fooRev;
49 private static Date barRev;
50 private static Date bazRev;
51 private static QName q0;
52 private static QName q1;
53 private static QName q2;
55 private Set<Module> modules;
58 public static void init() throws FileNotFoundException, ParseException {
59 final DateFormat simpleDateFormat = SimpleDateFormatUtil.getRevisionFormat();
60 fooRev = simpleDateFormat.parse("2013-10-13");
61 barRev = simpleDateFormat.parse("2013-10-14");
62 bazRev = simpleDateFormat.parse("2013-10-15");
64 q0 = QName.create(barNS, barRev, "interfaces");
65 q1 = QName.create(barNS, barRev, "ifEntry");
66 q2 = QName.create(bazNS, bazRev, "augment-holder");
70 public void testAugmentParsing() throws Exception {
71 modules = TestUtils.loadModules(getClass().getResource("/augment-test/augment-in-augment").toURI());
72 SchemaPath expectedSchemaPath;
73 final List<QName> qnames = new ArrayList<>();
79 final Module module1 = TestUtils.findModule(modules, "foo");
80 Set<AugmentationSchema> augmentations = module1.getAugmentations();
81 assertEquals(1, augmentations.size());
82 final AugmentationSchema augment = augmentations.iterator().next();
83 assertNotNull(augment);
85 expectedSchemaPath = SchemaPath.create(qnames, true);
86 assertEquals(expectedSchemaPath, augment.getTargetPath());
88 final Collection<DataSchemaNode> augmentChildren = augment.getChildNodes();
89 assertEquals(4, augmentChildren.size());
90 for (final DataSchemaNode dsn : augmentChildren) {
91 TestUtils.checkIsAugmenting(dsn, false);
94 final LeafSchemaNode ds0ChannelNumber = (LeafSchemaNode) augment.getDataChildByName(QName.create(
95 module1.getQNameModule(), "ds0ChannelNumber"));
96 final LeafSchemaNode interfaceId = (LeafSchemaNode) augment.getDataChildByName(QName.create(
97 module1.getQNameModule(), "interface-id"));
98 final ContainerSchemaNode schemas = (ContainerSchemaNode) augment.getDataChildByName(QName.create(
99 module1.getQNameModule(), "schemas"));
100 final ChoiceSchemaNode odl = (ChoiceSchemaNode) augment.getDataChildByName(QName.create(
101 module1.getQNameModule(), "odl"));
103 assertNotNull(ds0ChannelNumber);
104 assertNotNull(interfaceId);
105 assertNotNull(schemas);
108 // leaf ds0ChannelNumber
109 QName qname = QName.create(fooNS, fooRev, "ds0ChannelNumber");
111 assertEquals(qname, ds0ChannelNumber.getQName());
112 expectedSchemaPath = SchemaPath.create(qnames, true);
113 assertEquals(expectedSchemaPath, ds0ChannelNumber.getPath());
114 assertFalse(ds0ChannelNumber.isAugmenting());
115 // type of leaf ds0ChannelNumber
116 final QName typeQName = QName.create(YangConstants.RFC6020_YANG_MODULE, TypeUtils.STRING);
117 final List<QName> typePath = Collections.singletonList(typeQName);
118 expectedSchemaPath = SchemaPath.create(typePath, true);
119 assertEquals(expectedSchemaPath, ds0ChannelNumber.getType().getPath());
122 qname = QName.create(fooNS, fooRev, "interface-id");
123 assertEquals(qname, interfaceId.getQName());
124 qnames.set(3, qname);
125 expectedSchemaPath = SchemaPath.create(qnames, true);
126 assertEquals(expectedSchemaPath, interfaceId.getPath());
127 assertFalse(interfaceId.isAugmenting());
130 qname = QName.create(fooNS, fooRev, "schemas");
131 assertEquals(qname, schemas.getQName());
132 qnames.set(3, qname);
133 expectedSchemaPath = SchemaPath.create(qnames, true);
134 assertEquals(expectedSchemaPath, schemas.getPath());
135 assertFalse(schemas.isAugmenting());
138 qname = QName.create(fooNS, fooRev, "odl");
139 assertEquals(qname, odl.getQName());
140 qnames.set(3, qname);
141 expectedSchemaPath = SchemaPath.create(qnames, true);
142 assertEquals(expectedSchemaPath, odl.getPath());
143 assertFalse(odl.isAugmenting());
146 final Module module3 = TestUtils.findModule(modules, "baz");
147 augmentations = module3.getAugmentations();
148 assertEquals(3, augmentations.size());
149 AugmentationSchema augment1 = null;
150 AugmentationSchema augment2 = null;
151 AugmentationSchema augment3 = null;
152 for (final AugmentationSchema as : augmentations) {
153 if (as.getWhenCondition() == null) {
155 } else if ("if:ifType='ds0'".equals(as.getWhenCondition().toString())) {
157 } else if ("if:ifType='ds2'".equals(as.getWhenCondition().toString())) {
161 assertNotNull(augment1);
162 assertNotNull(augment2);
163 assertNotNull(augment3);
165 assertEquals(1, augment1.getChildNodes().size());
166 final ContainerSchemaNode augmentHolder = (ContainerSchemaNode) augment1.getDataChildByName(QName.create(
167 module3.getQNameModule(), "augment-holder"));
168 assertNotNull(augmentHolder);
170 assertEquals(1, augment2.getChildNodes().size());
171 final ContainerSchemaNode augmentHolder2 = (ContainerSchemaNode) augment2.getDataChildByName(QName.create(
172 module3.getQNameModule(), "augment-holder2"));
173 assertNotNull(augmentHolder2);
175 assertEquals(1, augment3.getChildNodes().size());
176 final ChoiceCaseNode pause = (ChoiceCaseNode) augment3.getDataChildByName(QName.create(
177 module3.getQNameModule(), "pause"));
178 assertNotNull(pause);
182 public void testAugmentResolving() throws Exception {
183 modules = TestUtils.loadModules(getClass().getResource("/augment-test/augment-in-augment").toURI());
184 final Module module2 = TestUtils.findModule(modules, "bar");
185 final ContainerSchemaNode interfaces = (ContainerSchemaNode) module2.getDataChildByName(QName.create(
186 module2.getQNameModule(), "interfaces"));
187 final ListSchemaNode ifEntry = (ListSchemaNode) interfaces.getDataChildByName(QName.create(
188 module2.getQNameModule(), "ifEntry"));
190 SchemaPath expectedPath;
191 final List<QName> qnames = new ArrayList<>();
197 // augment "/br:interfaces/br:ifEntry" {
198 final ContainerSchemaNode augmentHolder = (ContainerSchemaNode) ifEntry.getDataChildByName(QName.create(bazNS,
199 bazRev, "augment-holder"));
200 TestUtils.checkIsAugmenting(augmentHolder, true);
201 assertEquals(q2, augmentHolder.getQName());
202 expectedPath = SchemaPath.create(qnames, true);
203 assertEquals(expectedPath, augmentHolder.getPath());
206 // augment "/br:interfaces/br:ifEntry/bz:augment-holder"
207 final LeafSchemaNode ds0ChannelNumber = (LeafSchemaNode) augmentHolder.getDataChildByName(QName.create(fooNS,
208 fooRev, "ds0ChannelNumber"));
209 final LeafSchemaNode interfaceId = (LeafSchemaNode) augmentHolder.getDataChildByName(QName.create(fooNS,
210 fooRev, "interface-id"));
211 final ContainerSchemaNode schemas = (ContainerSchemaNode) augmentHolder.getDataChildByName(QName.create(fooNS,
213 final ChoiceSchemaNode odl = (ChoiceSchemaNode) augmentHolder.getDataChildByName(QName.create(fooNS, fooRev,
216 assertNotNull(ds0ChannelNumber);
217 assertNotNull(interfaceId);
218 assertNotNull(schemas);
221 // leaf ds0ChannelNumber
222 QName qname = QName.create(fooNS, fooRev, "ds0ChannelNumber");
223 assertEquals(qname, ds0ChannelNumber.getQName());
225 expectedPath = SchemaPath.create(qnames, true);
226 assertEquals(expectedPath, ds0ChannelNumber.getPath());
229 qname = QName.create(fooNS, fooRev, "interface-id");
230 assertEquals(qname, interfaceId.getQName());
231 qnames.set(3, qname);
232 expectedPath = SchemaPath.create(qnames, true);
233 assertEquals(expectedPath, interfaceId.getPath());
236 qname = QName.create(fooNS, fooRev, "schemas");
237 assertEquals(qname, schemas.getQName());
238 qnames.set(3, qname);
239 expectedPath = SchemaPath.create(qnames, true);
240 assertEquals(expectedPath, schemas.getPath());
243 qname = QName.create(fooNS, fooRev, "odl");
244 assertEquals(qname, odl.getQName());
245 qnames.set(3, qname);
246 expectedPath = SchemaPath.create(qnames, true);
247 assertEquals(expectedPath, odl.getPath());
251 public void testAugmentedChoice() throws Exception {
252 modules = TestUtils.loadModules(getClass().getResource("/augment-test/augment-in-augment").toURI());
253 final Module module2 = TestUtils.findModule(modules, "bar");
254 final ContainerSchemaNode interfaces = (ContainerSchemaNode) module2.getDataChildByName(QName.create(
255 module2.getQNameModule(), "interfaces"));
256 final ListSchemaNode ifEntry = (ListSchemaNode) interfaces.getDataChildByName(QName.create(
257 module2.getQNameModule(), "ifEntry"));
258 final ContainerSchemaNode augmentedHolder = (ContainerSchemaNode) ifEntry.getDataChildByName(QName.create(
259 bazNS, bazRev, "augment-holder"));
260 TestUtils.checkIsAugmenting(augmentedHolder, true);
263 // augment "/br:interfaces/br:ifEntry/bz:augment-holder"
264 final ChoiceSchemaNode odl = (ChoiceSchemaNode) augmentedHolder.getDataChildByName(QName.create(fooNS, fooRev,
267 final Set<ChoiceCaseNode> cases = odl.getCases();
268 assertEquals(4, cases.size());
270 ChoiceCaseNode id = null;
271 ChoiceCaseNode node1 = null;
272 ChoiceCaseNode node2 = null;
273 ChoiceCaseNode node3 = null;
275 for (final ChoiceCaseNode ccn : cases) {
276 if ("id".equals(ccn.getQName().getLocalName())) {
278 } else if ("node1".equals(ccn.getQName().getLocalName())) {
280 } else if ("node2".equals(ccn.getQName().getLocalName())) {
282 } else if ("node3".equals(ccn.getQName().getLocalName())) {
288 assertNotNull(node1);
289 assertNotNull(node2);
290 assertNotNull(node3);
292 SchemaPath expectedPath;
293 final List<QName> qnames = new ArrayList<>();
297 qnames.add(QName.create(fooNS, fooRev, "odl"));
300 QName qname = QName.create(fooNS, fooRev, "id");
301 assertEquals(qname, id.getQName());
303 expectedPath = SchemaPath.create(qnames, true);
304 assertEquals(expectedPath, id.getPath());
305 final Collection<DataSchemaNode> idChildren = id.getChildNodes();
306 assertEquals(1, idChildren.size());
309 qname = QName.create(fooNS, fooRev, "node1");
310 assertEquals(qname, node1.getQName());
311 qnames.set(4, qname);
312 expectedPath = SchemaPath.create(qnames, true);
313 assertEquals(expectedPath, node1.getPath());
314 final Collection<DataSchemaNode> node1Children = node1.getChildNodes();
315 assertTrue(node1Children.isEmpty());
318 qname = QName.create(fooNS, fooRev, "node2");
319 assertEquals(qname, node2.getQName());
320 qnames.set(4, qname);
321 expectedPath = SchemaPath.create(qnames, true);
322 assertEquals(expectedPath, node2.getPath());
323 final Collection<DataSchemaNode> node2Children = node2.getChildNodes();
324 assertTrue(node2Children.isEmpty());
327 qname = QName.create(fooNS, fooRev, "node3");
328 assertEquals(qname, node3.getQName());
329 qnames.set(4, qname);
330 expectedPath = SchemaPath.create(qnames, true);
331 assertEquals(expectedPath, node3.getPath());
332 final Collection<DataSchemaNode> node3Children = node3.getChildNodes();
333 assertEquals(1, node3Children.size());
340 qnames.add(QName.create(fooNS, fooRev, "odl"));
343 qnames.add(QName.create(fooNS, fooRev, "id"));
344 qnames.add(QName.create(fooNS, fooRev, "id"));
345 final LeafSchemaNode caseIdChild = (LeafSchemaNode) idChildren.iterator().next();
346 assertNotNull(caseIdChild);
347 expectedPath = SchemaPath.create(qnames, true);
348 assertEquals(expectedPath, caseIdChild.getPath());
351 qnames.set(4, QName.create(fooNS, fooRev, "node3"));
352 qnames.set(5, QName.create(fooNS, fooRev, "node3"));
353 final ContainerSchemaNode caseNode3Child = (ContainerSchemaNode) node3Children.iterator().next();
354 assertNotNull(caseNode3Child);
355 expectedPath = SchemaPath.create(qnames, true);
356 assertEquals(expectedPath, caseNode3Child.getPath());
360 public void testAugmentRpc() throws Exception {
361 modules = TestUtils.loadModules(getClass().getResource("/augment-test/rpc").toURI());
362 final URI NS_BAR = URI.create("urn:opendaylight:bar");
363 final URI NS_FOO = URI.create("urn:opendaylight:foo");
364 final Date revision = SimpleDateFormatUtil.getRevisionFormat().parse("2013-10-11");
366 final Module bar = TestUtils.findModule(modules, "bar");
367 final Set<RpcDefinition> rpcs = bar.getRpcs();
368 assertEquals(2, rpcs.size());
370 RpcDefinition submit = null;
371 for (final RpcDefinition rpc : rpcs) {
372 if ("submit".equals(rpc.getQName().getLocalName())) {
377 assertNotNull(submit);
379 final QName submitQName = QName.create(NS_BAR, revision, "submit");
380 assertEquals(submitQName, submit.getQName());
381 final ContainerSchemaNode input = submit.getInput();
382 final QName inputQName = QName.create(NS_BAR, revision, "input");
383 assertEquals(inputQName, input.getQName());
384 final ChoiceSchemaNode arguments = (ChoiceSchemaNode) input.getDataChildByName(QName.create(NS_BAR, revision,
386 final QName argumentsQName = QName.create(NS_BAR, revision, "arguments");
387 assertEquals(argumentsQName, arguments.getQName());
388 assertFalse(arguments.isAugmenting());
389 final Set<ChoiceCaseNode> cases = arguments.getCases();
390 assertEquals(3, cases.size());
392 ChoiceCaseNode attach = null;
393 ChoiceCaseNode create = null;
394 ChoiceCaseNode destroy = null;
395 for (final ChoiceCaseNode child : cases) {
396 if ("attach".equals(child.getQName().getLocalName())) {
398 } else if ("create".equals(child.getQName().getLocalName())) {
400 } else if ("destroy".equals(child.getQName().getLocalName())) {
404 assertNotNull(attach);
405 assertNotNull(create);
406 assertNotNull(destroy);
408 assertTrue(attach.isAugmenting());
409 assertTrue(create.isAugmenting());
410 assertTrue(destroy.isAugmenting());
412 SchemaPath expectedPath;
413 final QName[] qnames = new QName[4];
414 qnames[0] = submitQName;
415 qnames[1] = inputQName;
416 qnames[2] = argumentsQName;
419 qnames[3] = QName.create(NS_FOO, revision, "attach");
420 assertEquals(qnames[3], attach.getQName());
421 expectedPath = SchemaPath.create(Arrays.asList(qnames), true);
422 assertEquals(expectedPath, attach.getPath());
423 final Collection<DataSchemaNode> attachChildren = attach.getChildNodes();
424 assertEquals(1, attachChildren.size());
427 qnames[3] = QName.create(NS_FOO, revision, "create");
428 assertEquals(qnames[3], create.getQName());
429 expectedPath = SchemaPath.create(Arrays.asList(qnames), true);
430 assertEquals(expectedPath, create.getPath());
431 final Collection<DataSchemaNode> createChildren = create.getChildNodes();
432 assertEquals(1, createChildren.size());
435 qnames[3] = QName.create(NS_FOO, revision, "destroy");
436 assertEquals(qnames[3], destroy.getQName());
437 expectedPath = SchemaPath.create(Arrays.asList(qnames), true);
438 assertEquals(expectedPath, destroy.getPath());
439 final Collection<DataSchemaNode> destroyChildren = destroy.getChildNodes();
440 assertEquals(1, destroyChildren.size());
444 public void testAugmentInUsesResolving() throws Exception {
445 modules = TestUtils.loadModules(getClass().getResource("/augment-test/augment-in-uses").toURI());
446 assertEquals(1, modules.size());
448 final Module test = modules.iterator().next();
449 final DataNodeContainer links = (DataNodeContainer) test.getDataChildByName(QName.create(test.getQNameModule(),
451 final DataNodeContainer link = (DataNodeContainer) links.getDataChildByName(QName.create(test.getQNameModule(),
453 final DataNodeContainer nodes = (DataNodeContainer) link.getDataChildByName(QName.create(test.getQNameModule(),
455 final ContainerSchemaNode node = (ContainerSchemaNode) nodes.getDataChildByName(QName.create(
456 test.getQNameModule(), "node"));
457 final Set<AugmentationSchema> augments = node.getAvailableAugmentations();
458 assertEquals(1, augments.size());
459 assertEquals(1, node.getChildNodes().size());
460 final LeafSchemaNode id = (LeafSchemaNode) node.getDataChildByName(QName.create(test.getQNameModule(), "id"));
461 assertTrue(id.isAugmenting());