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.util.Collection;
16 import org.junit.Test;
17 import org.opendaylight.yangtools.yang.common.QName;
18 import org.opendaylight.yangtools.yang.common.QNameModule;
19 import org.opendaylight.yangtools.yang.common.Revision;
20 import org.opendaylight.yangtools.yang.common.XMLNamespace;
21 import org.opendaylight.yangtools.yang.model.api.AugmentationSchemaNode;
22 import org.opendaylight.yangtools.yang.model.api.CaseSchemaNode;
23 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
24 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
25 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
26 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
27 import org.opendaylight.yangtools.yang.model.api.InputSchemaNode;
28 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
29 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
30 import org.opendaylight.yangtools.yang.model.api.Module;
31 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
32 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
33 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
34 import org.opendaylight.yangtools.yang.model.api.type.TypeDefinitions;
36 public class AugmentTest {
37 private static final QNameModule FOO = QNameModule.create(
38 XMLNamespace.of("urn:opendaylight.foo"), Revision.of("2013-10-13"));
39 private static final QNameModule BAR = QNameModule.create(
40 XMLNamespace.of("urn:opendaylight.bar"), Revision.of("2013-10-14"));
41 private static final QNameModule BAZ = QNameModule.create(
42 XMLNamespace.of("urn:opendaylight.baz"), Revision.of("2013-10-15"));
44 private static final QName Q0 = QName.create(BAR, "interfaces");
45 private static final QName Q1 = QName.create(BAR, "ifEntry");
46 private static final QName Q2 = QName.create(BAZ, "augment-holder");
49 public void testAugmentParsing() throws Exception {
50 final SchemaContext context = TestUtils.loadModules(getClass().getResource("/augment-test/augment-in-augment")
54 final Module module1 = TestUtils.findModule(context, "foo").get();
55 Collection<? extends AugmentationSchemaNode> augmentations = module1.getAugmentations();
56 assertEquals(1, augmentations.size());
57 final AugmentationSchemaNode augment = augmentations.iterator().next();
58 assertNotNull(augment);
60 assertEquals(Absolute.of(Q0, Q1, Q2), augment.getTargetPath());
62 final Collection<? extends DataSchemaNode> augmentChildren = augment.getChildNodes();
63 assertEquals(4, augmentChildren.size());
64 for (final DataSchemaNode dsn : augmentChildren) {
65 TestUtils.checkIsAugmenting(dsn, false);
68 final LeafSchemaNode ds0ChannelNumber = (LeafSchemaNode) augment.getDataChildByName(QName.create(
69 module1.getQNameModule(), "ds0ChannelNumber"));
70 final LeafSchemaNode interfaceId = (LeafSchemaNode) augment.getDataChildByName(QName.create(
71 module1.getQNameModule(), "interface-id"));
72 final ContainerSchemaNode schemas = (ContainerSchemaNode) augment.getDataChildByName(QName.create(
73 module1.getQNameModule(), "schemas"));
74 final ChoiceSchemaNode odl = (ChoiceSchemaNode) augment.getDataChildByName(QName.create(
75 module1.getQNameModule(), "odl"));
77 assertNotNull(ds0ChannelNumber);
78 assertNotNull(interfaceId);
79 assertNotNull(schemas);
82 // leaf ds0ChannelNumber
83 assertEquals(QName.create(FOO, "ds0ChannelNumber"), ds0ChannelNumber.getQName());
84 assertFalse(ds0ChannelNumber.isAugmenting());
85 // type of leaf ds0ChannelNumber
86 assertEquals(TypeDefinitions.STRING, ds0ChannelNumber.getType().getQName());
89 assertEquals(QName.create(FOO, "interface-id"), interfaceId.getQName());
90 assertFalse(interfaceId.isAugmenting());
93 assertEquals(QName.create(FOO, "schemas"), schemas.getQName());
94 assertFalse(schemas.isAugmenting());
97 assertEquals(QName.create(FOO, "odl"), odl.getQName());
98 assertFalse(odl.isAugmenting());
101 final Module module3 = TestUtils.findModule(context, "baz").get();
102 augmentations = module3.getAugmentations();
103 assertEquals(3, augmentations.size());
104 AugmentationSchemaNode augment1 = null;
105 AugmentationSchemaNode augment2 = null;
106 AugmentationSchemaNode augment3 = null;
107 for (final AugmentationSchemaNode as : augmentations) {
108 if (!as.getWhenCondition().isPresent()) {
110 } else if ("br:ifType='ds0'".equals(as.getWhenCondition().orElseThrow().toString())) {
112 } else if ("br:ifType='ds2'".equals(as.getWhenCondition().orElseThrow().toString())) {
116 assertNotNull(augment1);
117 assertNotNull(augment2);
118 assertNotNull(augment3);
120 assertEquals(1, augment1.getChildNodes().size());
121 final ContainerSchemaNode augmentHolder = (ContainerSchemaNode) augment1.getDataChildByName(QName.create(
122 module3.getQNameModule(), "augment-holder"));
123 assertNotNull(augmentHolder);
125 assertEquals(1, augment2.getChildNodes().size());
126 final ContainerSchemaNode augmentHolder2 = (ContainerSchemaNode) augment2.getDataChildByName(QName.create(
127 module3.getQNameModule(), "augment-holder2"));
128 assertNotNull(augmentHolder2);
130 assertEquals(1, augment3.getChildNodes().size());
131 final CaseSchemaNode pause = (CaseSchemaNode) augment3.getDataChildByName(QName.create(
132 module3.getQNameModule(), "pause"));
133 assertNotNull(pause);
137 public void testAugmentResolving() throws Exception {
138 final SchemaContext context = TestUtils.loadModules(getClass().getResource("/augment-test/augment-in-augment")
140 final Module module2 = TestUtils.findModule(context, "bar").get();
141 final ContainerSchemaNode interfaces = (ContainerSchemaNode) module2.getDataChildByName(QName.create(
142 module2.getQNameModule(), "interfaces"));
143 final ListSchemaNode ifEntry = (ListSchemaNode) interfaces.getDataChildByName(QName.create(
144 module2.getQNameModule(), "ifEntry"));
147 // augment "/br:interfaces/br:ifEntry" {
148 final ContainerSchemaNode augmentHolder = (ContainerSchemaNode) ifEntry.getDataChildByName(QName.create(BAZ,
150 TestUtils.checkIsAugmenting(augmentHolder, true);
151 assertEquals(Q2, augmentHolder.getQName());
154 // augment "/br:interfaces/br:ifEntry/bz:augment-holder"
155 final LeafSchemaNode ds0ChannelNumber = (LeafSchemaNode) augmentHolder.getDataChildByName(QName.create(FOO,
156 "ds0ChannelNumber"));
157 final LeafSchemaNode interfaceId = (LeafSchemaNode) augmentHolder.getDataChildByName(QName.create(FOO,
159 final ContainerSchemaNode schemas = (ContainerSchemaNode) augmentHolder.getDataChildByName(QName.create(FOO,
161 final ChoiceSchemaNode odl = (ChoiceSchemaNode) augmentHolder.getDataChildByName(QName.create(FOO, "odl"));
163 assertNotNull(ds0ChannelNumber);
164 assertNotNull(interfaceId);
165 assertNotNull(schemas);
168 // leaf ds0ChannelNumber
169 assertEquals(QName.create(FOO, "ds0ChannelNumber"), ds0ChannelNumber.getQName());
172 assertEquals(QName.create(FOO, "interface-id"), interfaceId.getQName());
175 assertEquals(QName.create(FOO, "schemas"), schemas.getQName());
178 assertEquals(QName.create(FOO, "odl"), odl.getQName());
182 public void testAugmentedChoice() throws Exception {
183 final SchemaContext context = TestUtils.loadModules(getClass().getResource("/augment-test/augment-in-augment")
185 final Module module2 = TestUtils.findModule(context, "bar").get();
186 final ContainerSchemaNode interfaces = (ContainerSchemaNode) module2.getDataChildByName(QName.create(
187 module2.getQNameModule(), "interfaces"));
188 final ListSchemaNode ifEntry = (ListSchemaNode) interfaces.getDataChildByName(QName.create(
189 module2.getQNameModule(), "ifEntry"));
190 final ContainerSchemaNode augmentedHolder = (ContainerSchemaNode) ifEntry.getDataChildByName(QName.create(
191 BAZ, "augment-holder"));
192 TestUtils.checkIsAugmenting(augmentedHolder, true);
195 // augment "/br:interfaces/br:ifEntry/bz:augment-holder"
196 final ChoiceSchemaNode odl = (ChoiceSchemaNode) augmentedHolder.getDataChildByName(QName.create(FOO, "odl"));
198 final Collection<? extends CaseSchemaNode> cases = odl.getCases();
199 assertEquals(4, cases.size());
201 CaseSchemaNode id = null;
202 CaseSchemaNode node1 = null;
203 CaseSchemaNode node2 = null;
204 CaseSchemaNode node3 = null;
206 for (final CaseSchemaNode ccn : cases) {
207 if ("id".equals(ccn.getQName().getLocalName())) {
209 } else if ("node1".equals(ccn.getQName().getLocalName())) {
211 } else if ("node2".equals(ccn.getQName().getLocalName())) {
213 } else if ("node3".equals(ccn.getQName().getLocalName())) {
219 assertNotNull(node1);
220 assertNotNull(node2);
221 assertNotNull(node3);
224 assertEquals(QName.create(FOO, "id"), id.getQName());
225 final Collection<? extends DataSchemaNode> idChildren = id.getChildNodes();
226 assertEquals(1, idChildren.size());
229 assertEquals(QName.create(FOO, "node1"), node1.getQName());
230 final Collection<? extends DataSchemaNode> node1Children = node1.getChildNodes();
231 assertTrue(node1Children.isEmpty());
234 assertEquals(QName.create(FOO, "node2"), node2.getQName());
235 final Collection<? extends DataSchemaNode> node2Children = node2.getChildNodes();
236 assertTrue(node2Children.isEmpty());
239 assertEquals(QName.create(FOO, "node3"), node3.getQName());
240 final Collection<? extends DataSchemaNode> node3Children = node3.getChildNodes();
241 assertEquals(1, node3Children.size());
245 final LeafSchemaNode caseIdChild = (LeafSchemaNode) idChildren.iterator().next();
246 assertNotNull(caseIdChild);
247 assertEquals(QName.create(FOO, "id"), caseIdChild.getQName());
250 final ContainerSchemaNode caseNode3Child = (ContainerSchemaNode) node3Children.iterator().next();
251 assertNotNull(caseNode3Child);
252 assertEquals(QName.create(FOO, "node3"), caseNode3Child.getQName());
256 public void testAugmentRpc() throws Exception {
257 final SchemaContext context = TestUtils.loadModules(getClass().getResource("/augment-test/rpc").toURI());
258 final XMLNamespace NS_BAR = XMLNamespace.of("urn:opendaylight:bar");
259 final XMLNamespace NS_FOO = XMLNamespace.of("urn:opendaylight:foo");
260 final Revision revision = Revision.of("2013-10-11");
261 final Module bar = TestUtils.findModule(context, "bar").get();
262 final Collection<? extends RpcDefinition> rpcs = bar.getRpcs();
263 assertEquals(2, rpcs.size());
265 RpcDefinition submit = null;
266 for (final RpcDefinition rpc : rpcs) {
267 if ("submit".equals(rpc.getQName().getLocalName())) {
272 assertNotNull(submit);
274 final QName submitQName = QName.create(NS_BAR, revision, "submit");
275 assertEquals(submitQName, submit.getQName());
276 final InputSchemaNode input = submit.getInput();
277 final QName inputQName = QName.create(NS_BAR, revision, "input");
278 assertEquals(inputQName, input.getQName());
279 final ChoiceSchemaNode arguments = (ChoiceSchemaNode) input.getDataChildByName(QName.create(NS_BAR, revision,
281 final QName argumentsQName = QName.create(NS_BAR, revision, "arguments");
282 assertEquals(argumentsQName, arguments.getQName());
283 assertFalse(arguments.isAugmenting());
284 final Collection<? extends CaseSchemaNode> cases = arguments.getCases();
285 assertEquals(3, cases.size());
287 CaseSchemaNode attach = null;
288 CaseSchemaNode create = null;
289 CaseSchemaNode destroy = null;
290 for (final CaseSchemaNode child : cases) {
291 if ("attach".equals(child.getQName().getLocalName())) {
293 } else if ("create".equals(child.getQName().getLocalName())) {
295 } else if ("destroy".equals(child.getQName().getLocalName())) {
299 assertNotNull(attach);
300 assertNotNull(create);
301 assertNotNull(destroy);
303 assertTrue(attach.isAugmenting());
304 assertTrue(create.isAugmenting());
305 assertTrue(destroy.isAugmenting());
308 assertEquals(QName.create(NS_FOO, revision, "attach"), attach.getQName());
309 final Collection<? extends DataSchemaNode> attachChildren = attach.getChildNodes();
310 assertEquals(1, attachChildren.size());
313 assertEquals(QName.create(NS_FOO, revision, "create"), create.getQName());
314 final Collection<? extends DataSchemaNode> createChildren = create.getChildNodes();
315 assertEquals(1, createChildren.size());
318 assertEquals(QName.create(NS_FOO, revision, "destroy"), destroy.getQName());
319 final Collection<? extends DataSchemaNode> destroyChildren = destroy.getChildNodes();
320 assertEquals(1, destroyChildren.size());
324 public void testAugmentInUsesResolving() throws Exception {
325 final SchemaContext context = TestUtils.loadModules(getClass().getResource("/augment-test/augment-in-uses")
327 assertEquals(1, context.getModules().size());
329 final Module test = context.getModules().iterator().next();
330 final DataNodeContainer links = (DataNodeContainer) test.getDataChildByName(QName.create(test.getQNameModule(),
332 final DataNodeContainer link = (DataNodeContainer) links.getDataChildByName(QName.create(test.getQNameModule(),
334 final DataNodeContainer nodes = (DataNodeContainer) link.getDataChildByName(QName.create(test.getQNameModule(),
336 final ContainerSchemaNode node = (ContainerSchemaNode) nodes.getDataChildByName(QName.create(
337 test.getQNameModule(), "node"));
338 final Collection<? extends AugmentationSchemaNode> augments = node.getAvailableAugmentations();
339 assertEquals(1, augments.size());
340 assertEquals(1, node.getChildNodes().size());
341 final LeafSchemaNode id = (LeafSchemaNode) node.getDataChildByName(QName.create(test.getQNameModule(), "id"));
342 assertTrue(id.isAugmenting());