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;
14 import static org.opendaylight.yangtools.yang.stmt.StmtTestUtils.assertPathEquals;
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.common.XMLNamespace;
24 import org.opendaylight.yangtools.yang.model.api.AugmentationSchemaNode;
25 import org.opendaylight.yangtools.yang.model.api.CaseSchemaNode;
26 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
27 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
28 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
29 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
30 import org.opendaylight.yangtools.yang.model.api.InputSchemaNode;
31 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
32 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
33 import org.opendaylight.yangtools.yang.model.api.Module;
34 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
35 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
36 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
37 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
38 import org.opendaylight.yangtools.yang.model.api.type.StringTypeDefinition;
40 public class AugmentTest {
41 private static final QNameModule FOO = QNameModule.create(
42 XMLNamespace.of("urn:opendaylight.foo"), Revision.of("2013-10-13"));
43 private static final QNameModule BAR = QNameModule.create(
44 XMLNamespace.of("urn:opendaylight.bar"), Revision.of("2013-10-14"));
45 private static final QNameModule BAZ = QNameModule.create(
46 XMLNamespace.of("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 Collection<? extends AugmentationSchemaNode> augmentations = module1.getAugmentations();
64 assertEquals(1, augmentations.size());
65 final AugmentationSchemaNode augment = augmentations.iterator().next();
66 assertNotNull(augment);
68 assertEquals(Absolute.of(qnames), augment.getTargetPath());
70 final Collection<? extends DataSchemaNode> augmentChildren = augment.getChildNodes();
71 assertEquals(4, augmentChildren.size());
72 for (final DataSchemaNode dsn : augmentChildren) {
73 TestUtils.checkIsAugmenting(dsn, false);
76 final LeafSchemaNode ds0ChannelNumber = (LeafSchemaNode) augment.getDataChildByName(QName.create(
77 module1.getQNameModule(), "ds0ChannelNumber"));
78 final LeafSchemaNode interfaceId = (LeafSchemaNode) augment.getDataChildByName(QName.create(
79 module1.getQNameModule(), "interface-id"));
80 final ContainerSchemaNode schemas = (ContainerSchemaNode) augment.getDataChildByName(QName.create(
81 module1.getQNameModule(), "schemas"));
82 final ChoiceSchemaNode odl = (ChoiceSchemaNode) augment.getDataChildByName(QName.create(
83 module1.getQNameModule(), "odl"));
85 assertNotNull(ds0ChannelNumber);
86 assertNotNull(interfaceId);
87 assertNotNull(schemas);
90 // leaf ds0ChannelNumber
91 QName qname = QName.create(FOO, "ds0ChannelNumber");
93 assertEquals(qname, ds0ChannelNumber.getQName());
94 SchemaPath expectedSchemaPath = SchemaPath.create(qnames, true);
95 assertPathEquals(expectedSchemaPath, ds0ChannelNumber);
96 assertFalse(ds0ChannelNumber.isAugmenting());
97 // type of leaf ds0ChannelNumber
98 assertEquals(StringTypeDefinition.QNAME, ds0ChannelNumber.getType().getQName());
101 qname = QName.create(FOO, "interface-id");
102 assertEquals(qname, interfaceId.getQName());
103 qnames.set(3, qname);
104 expectedSchemaPath = SchemaPath.create(qnames, true);
105 assertPathEquals(expectedSchemaPath, interfaceId);
106 assertFalse(interfaceId.isAugmenting());
109 qname = QName.create(FOO, "schemas");
110 assertEquals(qname, schemas.getQName());
111 qnames.set(3, qname);
112 expectedSchemaPath = SchemaPath.create(qnames, true);
113 assertPathEquals(expectedSchemaPath, schemas);
114 assertFalse(schemas.isAugmenting());
117 qname = QName.create(FOO, "odl");
118 assertEquals(qname, odl.getQName());
119 qnames.set(3, qname);
120 expectedSchemaPath = SchemaPath.create(qnames, true);
121 assertPathEquals(expectedSchemaPath, odl);
122 assertFalse(odl.isAugmenting());
125 final Module module3 = TestUtils.findModule(context, "baz").get();
126 augmentations = module3.getAugmentations();
127 assertEquals(3, augmentations.size());
128 AugmentationSchemaNode augment1 = null;
129 AugmentationSchemaNode augment2 = null;
130 AugmentationSchemaNode augment3 = null;
131 for (final AugmentationSchemaNode as : augmentations) {
132 if (!as.getWhenCondition().isPresent()) {
134 } else if ("br:ifType='ds0'".equals(as.getWhenCondition().orElseThrow().toString())) {
136 } else if ("br:ifType='ds2'".equals(as.getWhenCondition().orElseThrow().toString())) {
140 assertNotNull(augment1);
141 assertNotNull(augment2);
142 assertNotNull(augment3);
144 assertEquals(1, augment1.getChildNodes().size());
145 final ContainerSchemaNode augmentHolder = (ContainerSchemaNode) augment1.getDataChildByName(QName.create(
146 module3.getQNameModule(), "augment-holder"));
147 assertNotNull(augmentHolder);
149 assertEquals(1, augment2.getChildNodes().size());
150 final ContainerSchemaNode augmentHolder2 = (ContainerSchemaNode) augment2.getDataChildByName(QName.create(
151 module3.getQNameModule(), "augment-holder2"));
152 assertNotNull(augmentHolder2);
154 assertEquals(1, augment3.getChildNodes().size());
155 final CaseSchemaNode pause = (CaseSchemaNode) augment3.getDataChildByName(QName.create(
156 module3.getQNameModule(), "pause"));
157 assertNotNull(pause);
161 public void testAugmentResolving() throws Exception {
162 final SchemaContext context = TestUtils.loadModules(getClass().getResource("/augment-test/augment-in-augment")
164 final Module module2 = TestUtils.findModule(context, "bar").get();
165 final ContainerSchemaNode interfaces = (ContainerSchemaNode) module2.getDataChildByName(QName.create(
166 module2.getQNameModule(), "interfaces"));
167 final ListSchemaNode ifEntry = (ListSchemaNode) interfaces.getDataChildByName(QName.create(
168 module2.getQNameModule(), "ifEntry"));
170 final List<QName> qnames = new ArrayList<>();
176 // augment "/br:interfaces/br:ifEntry" {
177 final ContainerSchemaNode augmentHolder = (ContainerSchemaNode) ifEntry.getDataChildByName(QName.create(BAZ,
179 TestUtils.checkIsAugmenting(augmentHolder, true);
180 assertEquals(Q2, augmentHolder.getQName());
181 assertPathEquals(SchemaPath.create(qnames, true), augmentHolder);
184 // augment "/br:interfaces/br:ifEntry/bz:augment-holder"
185 final LeafSchemaNode ds0ChannelNumber = (LeafSchemaNode) augmentHolder.getDataChildByName(QName.create(FOO,
186 "ds0ChannelNumber"));
187 final LeafSchemaNode interfaceId = (LeafSchemaNode) augmentHolder.getDataChildByName(QName.create(FOO,
189 final ContainerSchemaNode schemas = (ContainerSchemaNode) augmentHolder.getDataChildByName(QName.create(FOO,
191 final ChoiceSchemaNode odl = (ChoiceSchemaNode) augmentHolder.getDataChildByName(QName.create(FOO, "odl"));
193 assertNotNull(ds0ChannelNumber);
194 assertNotNull(interfaceId);
195 assertNotNull(schemas);
198 // leaf ds0ChannelNumber
199 QName qname = QName.create(FOO, "ds0ChannelNumber");
200 assertEquals(qname, ds0ChannelNumber.getQName());
202 assertPathEquals(SchemaPath.create(qnames, true), ds0ChannelNumber);
205 qname = QName.create(FOO, "interface-id");
206 assertEquals(qname, interfaceId.getQName());
207 qnames.set(3, qname);
208 assertPathEquals(SchemaPath.create(qnames, true), interfaceId);
211 qname = QName.create(FOO, "schemas");
212 assertEquals(qname, schemas.getQName());
213 qnames.set(3, qname);
214 assertPathEquals(SchemaPath.create(qnames, true), schemas);
217 qname = QName.create(FOO, "odl");
218 assertEquals(qname, odl.getQName());
219 qnames.set(3, qname);
220 assertPathEquals(SchemaPath.create(qnames, true), odl);
224 public void testAugmentedChoice() throws Exception {
225 final SchemaContext context = TestUtils.loadModules(getClass().getResource("/augment-test/augment-in-augment")
227 final Module module2 = TestUtils.findModule(context, "bar").get();
228 final ContainerSchemaNode interfaces = (ContainerSchemaNode) module2.getDataChildByName(QName.create(
229 module2.getQNameModule(), "interfaces"));
230 final ListSchemaNode ifEntry = (ListSchemaNode) interfaces.getDataChildByName(QName.create(
231 module2.getQNameModule(), "ifEntry"));
232 final ContainerSchemaNode augmentedHolder = (ContainerSchemaNode) ifEntry.getDataChildByName(QName.create(
233 BAZ, "augment-holder"));
234 TestUtils.checkIsAugmenting(augmentedHolder, true);
237 // augment "/br:interfaces/br:ifEntry/bz:augment-holder"
238 final ChoiceSchemaNode odl = (ChoiceSchemaNode) augmentedHolder.getDataChildByName(QName.create(FOO, "odl"));
240 final Collection<? extends CaseSchemaNode> cases = odl.getCases();
241 assertEquals(4, cases.size());
243 CaseSchemaNode id = null;
244 CaseSchemaNode node1 = null;
245 CaseSchemaNode node2 = null;
246 CaseSchemaNode node3 = null;
248 for (final CaseSchemaNode ccn : cases) {
249 if ("id".equals(ccn.getQName().getLocalName())) {
251 } else if ("node1".equals(ccn.getQName().getLocalName())) {
253 } else if ("node2".equals(ccn.getQName().getLocalName())) {
255 } else if ("node3".equals(ccn.getQName().getLocalName())) {
261 assertNotNull(node1);
262 assertNotNull(node2);
263 assertNotNull(node3);
265 final List<QName> qnames = new ArrayList<>();
269 qnames.add(QName.create(FOO, "odl"));
272 QName qname = QName.create(FOO, "id");
273 assertEquals(qname, id.getQName());
275 assertPathEquals(SchemaPath.create(qnames, true), id);
276 final Collection<? extends DataSchemaNode> idChildren = id.getChildNodes();
277 assertEquals(1, idChildren.size());
280 qname = QName.create(FOO, "node1");
281 assertEquals(qname, node1.getQName());
282 qnames.set(4, qname);
283 assertPathEquals(SchemaPath.create(qnames, true), node1);
284 final Collection<? extends DataSchemaNode> node1Children = node1.getChildNodes();
285 assertTrue(node1Children.isEmpty());
288 qname = QName.create(FOO, "node2");
289 assertEquals(qname, node2.getQName());
290 qnames.set(4, qname);
291 assertPathEquals(SchemaPath.create(qnames, true), node2);
292 final Collection<? extends DataSchemaNode> node2Children = node2.getChildNodes();
293 assertTrue(node2Children.isEmpty());
296 qname = QName.create(FOO, "node3");
297 assertEquals(qname, node3.getQName());
298 qnames.set(4, qname);
299 assertPathEquals(SchemaPath.create(qnames, true), node3);
300 final Collection<? extends DataSchemaNode> node3Children = node3.getChildNodes();
301 assertEquals(1, node3Children.size());
308 qnames.add(QName.create(FOO, "odl"));
311 qnames.add(QName.create(FOO, "id"));
312 qnames.add(QName.create(FOO, "id"));
313 final LeafSchemaNode caseIdChild = (LeafSchemaNode) idChildren.iterator().next();
314 assertNotNull(caseIdChild);
315 assertPathEquals(SchemaPath.create(qnames, true), caseIdChild);
318 qnames.set(4, QName.create(FOO, "node3"));
319 qnames.set(5, QName.create(FOO, "node3"));
320 final ContainerSchemaNode caseNode3Child = (ContainerSchemaNode) node3Children.iterator().next();
321 assertNotNull(caseNode3Child);
322 assertPathEquals(SchemaPath.create(qnames, true), caseNode3Child);
326 public void testAugmentRpc() throws Exception {
327 final SchemaContext context = TestUtils.loadModules(getClass().getResource("/augment-test/rpc").toURI());
328 final XMLNamespace NS_BAR = XMLNamespace.of("urn:opendaylight:bar");
329 final XMLNamespace NS_FOO = XMLNamespace.of("urn:opendaylight:foo");
330 final Revision revision = Revision.of("2013-10-11");
331 final Module bar = TestUtils.findModule(context, "bar").get();
332 final Collection<? extends RpcDefinition> rpcs = bar.getRpcs();
333 assertEquals(2, rpcs.size());
335 RpcDefinition submit = null;
336 for (final RpcDefinition rpc : rpcs) {
337 if ("submit".equals(rpc.getQName().getLocalName())) {
342 assertNotNull(submit);
344 final QName submitQName = QName.create(NS_BAR, revision, "submit");
345 assertEquals(submitQName, submit.getQName());
346 final InputSchemaNode input = submit.getInput();
347 final QName inputQName = QName.create(NS_BAR, revision, "input");
348 assertEquals(inputQName, input.getQName());
349 final ChoiceSchemaNode arguments = (ChoiceSchemaNode) input.getDataChildByName(QName.create(NS_BAR, revision,
351 final QName argumentsQName = QName.create(NS_BAR, revision, "arguments");
352 assertEquals(argumentsQName, arguments.getQName());
353 assertFalse(arguments.isAugmenting());
354 final Collection<? extends CaseSchemaNode> cases = arguments.getCases();
355 assertEquals(3, cases.size());
357 CaseSchemaNode attach = null;
358 CaseSchemaNode create = null;
359 CaseSchemaNode destroy = null;
360 for (final CaseSchemaNode child : cases) {
361 if ("attach".equals(child.getQName().getLocalName())) {
363 } else if ("create".equals(child.getQName().getLocalName())) {
365 } else if ("destroy".equals(child.getQName().getLocalName())) {
369 assertNotNull(attach);
370 assertNotNull(create);
371 assertNotNull(destroy);
373 assertTrue(attach.isAugmenting());
374 assertTrue(create.isAugmenting());
375 assertTrue(destroy.isAugmenting());
377 final QName[] qnames = new QName[4];
378 qnames[0] = submitQName;
379 qnames[1] = inputQName;
380 qnames[2] = argumentsQName;
383 qnames[3] = QName.create(NS_FOO, revision, "attach");
384 assertEquals(qnames[3], attach.getQName());
385 assertPathEquals(SchemaPath.create(true, qnames), attach);
386 final Collection<? extends DataSchemaNode> attachChildren = attach.getChildNodes();
387 assertEquals(1, attachChildren.size());
390 qnames[3] = QName.create(NS_FOO, revision, "create");
391 assertEquals(qnames[3], create.getQName());
392 assertPathEquals(SchemaPath.create(true, qnames), create);
393 final Collection<? extends DataSchemaNode> createChildren = create.getChildNodes();
394 assertEquals(1, createChildren.size());
397 qnames[3] = QName.create(NS_FOO, revision, "destroy");
398 assertEquals(qnames[3], destroy.getQName());
399 assertPathEquals(SchemaPath.create(true, qnames), destroy);
400 final Collection<? extends DataSchemaNode> destroyChildren = destroy.getChildNodes();
401 assertEquals(1, destroyChildren.size());
405 public void testAugmentInUsesResolving() throws Exception {
406 final SchemaContext context = TestUtils.loadModules(getClass().getResource("/augment-test/augment-in-uses")
408 assertEquals(1, context.getModules().size());
410 final Module test = context.getModules().iterator().next();
411 final DataNodeContainer links = (DataNodeContainer) test.getDataChildByName(QName.create(test.getQNameModule(),
413 final DataNodeContainer link = (DataNodeContainer) links.getDataChildByName(QName.create(test.getQNameModule(),
415 final DataNodeContainer nodes = (DataNodeContainer) link.getDataChildByName(QName.create(test.getQNameModule(),
417 final ContainerSchemaNode node = (ContainerSchemaNode) nodes.getDataChildByName(QName.create(
418 test.getQNameModule(), "node"));
419 final Collection<? extends AugmentationSchemaNode> augments = node.getAvailableAugmentations();
420 assertEquals(1, augments.size());
421 assertEquals(1, node.getChildNodes().size());
422 final LeafSchemaNode id = (LeafSchemaNode) node.getDataChildByName(QName.create(test.getQNameModule(), "id"));
423 assertTrue(id.isAugmenting());