Remove SchemaNode.getPath()
[yangtools.git] / parser / yang-parser-rfc7950 / src / test / java / org / opendaylight / yangtools / yang / stmt / AugmentTest.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.stmt;
9
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
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;
35
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"));
43
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");
47
48     @Test
49     public void testAugmentParsing() throws Exception {
50         final SchemaContext context = TestUtils.loadModules(getClass().getResource("/augment-test/augment-in-augment")
51             .toURI());
52
53         // foo.yang
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);
59
60         assertEquals(Absolute.of(Q0, Q1, Q2), augment.getTargetPath());
61
62         final Collection<? extends DataSchemaNode> augmentChildren = augment.getChildNodes();
63         assertEquals(4, augmentChildren.size());
64         for (final DataSchemaNode dsn : augmentChildren) {
65             TestUtils.checkIsAugmenting(dsn, false);
66         }
67
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"));
76
77         assertNotNull(ds0ChannelNumber);
78         assertNotNull(interfaceId);
79         assertNotNull(schemas);
80         assertNotNull(odl);
81
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());
87
88         // leaf interface-id
89         assertEquals(QName.create(FOO, "interface-id"), interfaceId.getQName());
90         assertFalse(interfaceId.isAugmenting());
91
92         // container schemas
93         assertEquals(QName.create(FOO, "schemas"), schemas.getQName());
94         assertFalse(schemas.isAugmenting());
95
96         // choice odl
97         assertEquals(QName.create(FOO, "odl"), odl.getQName());
98         assertFalse(odl.isAugmenting());
99
100         // baz.yang
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()) {
109                 augment3 = as;
110             } else if ("br:ifType='ds0'".equals(as.getWhenCondition().orElseThrow().toString())) {
111                 augment1 = as;
112             } else if ("br:ifType='ds2'".equals(as.getWhenCondition().orElseThrow().toString())) {
113                 augment2 = as;
114             }
115         }
116         assertNotNull(augment1);
117         assertNotNull(augment2);
118         assertNotNull(augment3);
119
120         assertEquals(1, augment1.getChildNodes().size());
121         final ContainerSchemaNode augmentHolder = (ContainerSchemaNode) augment1.getDataChildByName(QName.create(
122                 module3.getQNameModule(), "augment-holder"));
123         assertNotNull(augmentHolder);
124
125         assertEquals(1, augment2.getChildNodes().size());
126         final ContainerSchemaNode augmentHolder2 = (ContainerSchemaNode) augment2.getDataChildByName(QName.create(
127                 module3.getQNameModule(), "augment-holder2"));
128         assertNotNull(augmentHolder2);
129
130         assertEquals(1, augment3.getChildNodes().size());
131         final CaseSchemaNode pause = (CaseSchemaNode) augment3.getDataChildByName(QName.create(
132                 module3.getQNameModule(), "pause"));
133         assertNotNull(pause);
134     }
135
136     @Test
137     public void testAugmentResolving() throws Exception {
138         final SchemaContext context = TestUtils.loadModules(getClass().getResource("/augment-test/augment-in-augment")
139             .toURI());
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"));
145
146         // baz.yang
147         // augment "/br:interfaces/br:ifEntry" {
148         final ContainerSchemaNode augmentHolder = (ContainerSchemaNode) ifEntry.getDataChildByName(QName.create(BAZ,
149                 "augment-holder"));
150         TestUtils.checkIsAugmenting(augmentHolder, true);
151         assertEquals(Q2, augmentHolder.getQName());
152
153         // foo.yang
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,
158                 "interface-id"));
159         final ContainerSchemaNode schemas = (ContainerSchemaNode) augmentHolder.getDataChildByName(QName.create(FOO,
160                 "schemas"));
161         final ChoiceSchemaNode odl = (ChoiceSchemaNode) augmentHolder.getDataChildByName(QName.create(FOO, "odl"));
162
163         assertNotNull(ds0ChannelNumber);
164         assertNotNull(interfaceId);
165         assertNotNull(schemas);
166         assertNotNull(odl);
167
168         // leaf ds0ChannelNumber
169         assertEquals(QName.create(FOO, "ds0ChannelNumber"), ds0ChannelNumber.getQName());
170
171         // leaf interface-id
172         assertEquals(QName.create(FOO, "interface-id"), interfaceId.getQName());
173
174         // container schemas
175         assertEquals(QName.create(FOO, "schemas"), schemas.getQName());
176
177         // choice odl
178         assertEquals(QName.create(FOO, "odl"), odl.getQName());
179     }
180
181     @Test
182     public void testAugmentedChoice() throws Exception {
183         final SchemaContext context = TestUtils.loadModules(getClass().getResource("/augment-test/augment-in-augment")
184             .toURI());
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);
193
194         // foo.yang
195         // augment "/br:interfaces/br:ifEntry/bz:augment-holder"
196         final ChoiceSchemaNode odl = (ChoiceSchemaNode) augmentedHolder.getDataChildByName(QName.create(FOO, "odl"));
197         assertNotNull(odl);
198         final Collection<? extends CaseSchemaNode> cases = odl.getCases();
199         assertEquals(4, cases.size());
200
201         CaseSchemaNode id = null;
202         CaseSchemaNode node1 = null;
203         CaseSchemaNode node2 = null;
204         CaseSchemaNode node3 = null;
205
206         for (final CaseSchemaNode ccn : cases) {
207             if ("id".equals(ccn.getQName().getLocalName())) {
208                 id = ccn;
209             } else if ("node1".equals(ccn.getQName().getLocalName())) {
210                 node1 = ccn;
211             } else if ("node2".equals(ccn.getQName().getLocalName())) {
212                 node2 = ccn;
213             } else if ("node3".equals(ccn.getQName().getLocalName())) {
214                 node3 = ccn;
215             }
216         }
217
218         assertNotNull(id);
219         assertNotNull(node1);
220         assertNotNull(node2);
221         assertNotNull(node3);
222
223         // case id
224         assertEquals(QName.create(FOO, "id"), id.getQName());
225         final Collection<? extends DataSchemaNode> idChildren = id.getChildNodes();
226         assertEquals(1, idChildren.size());
227
228         // case node1
229         assertEquals(QName.create(FOO, "node1"), node1.getQName());
230         final Collection<? extends DataSchemaNode> node1Children = node1.getChildNodes();
231         assertTrue(node1Children.isEmpty());
232
233         // case node2
234         assertEquals(QName.create(FOO, "node2"), node2.getQName());
235         final Collection<? extends DataSchemaNode> node2Children = node2.getChildNodes();
236         assertTrue(node2Children.isEmpty());
237
238         // case node3
239         assertEquals(QName.create(FOO, "node3"), node3.getQName());
240         final Collection<? extends DataSchemaNode> node3Children = node3.getChildNodes();
241         assertEquals(1, node3Children.size());
242
243         // test cases
244         // case id child
245         final LeafSchemaNode caseIdChild = (LeafSchemaNode) idChildren.iterator().next();
246         assertNotNull(caseIdChild);
247         assertEquals(QName.create(FOO, "id"), caseIdChild.getQName());
248
249         // case node3 child
250         final ContainerSchemaNode caseNode3Child = (ContainerSchemaNode) node3Children.iterator().next();
251         assertNotNull(caseNode3Child);
252         assertEquals(QName.create(FOO, "node3"), caseNode3Child.getQName());
253     }
254
255     @Test
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());
264
265         RpcDefinition submit = null;
266         for (final RpcDefinition rpc : rpcs) {
267             if ("submit".equals(rpc.getQName().getLocalName())) {
268                 submit = rpc;
269                 break;
270             }
271         }
272         assertNotNull(submit);
273
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,
280                 "arguments"));
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());
286
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())) {
292                 attach = child;
293             } else if ("create".equals(child.getQName().getLocalName())) {
294                 create = child;
295             } else if ("destroy".equals(child.getQName().getLocalName())) {
296                 destroy = child;
297             }
298         }
299         assertNotNull(attach);
300         assertNotNull(create);
301         assertNotNull(destroy);
302
303         assertTrue(attach.isAugmenting());
304         assertTrue(create.isAugmenting());
305         assertTrue(destroy.isAugmenting());
306
307         // case attach
308         assertEquals(QName.create(NS_FOO, revision, "attach"), attach.getQName());
309         final Collection<? extends DataSchemaNode> attachChildren = attach.getChildNodes();
310         assertEquals(1, attachChildren.size());
311
312         // case create
313         assertEquals(QName.create(NS_FOO, revision, "create"), create.getQName());
314         final Collection<? extends DataSchemaNode> createChildren = create.getChildNodes();
315         assertEquals(1, createChildren.size());
316
317         // case attach
318         assertEquals(QName.create(NS_FOO, revision, "destroy"), destroy.getQName());
319         final Collection<? extends DataSchemaNode> destroyChildren = destroy.getChildNodes();
320         assertEquals(1, destroyChildren.size());
321     }
322
323     @Test
324     public void testAugmentInUsesResolving() throws Exception {
325         final SchemaContext context = TestUtils.loadModules(getClass().getResource("/augment-test/augment-in-uses")
326             .toURI());
327         assertEquals(1, context.getModules().size());
328
329         final Module test = context.getModules().iterator().next();
330         final DataNodeContainer links = (DataNodeContainer) test.getDataChildByName(QName.create(test.getQNameModule(),
331                 "links"));
332         final DataNodeContainer link = (DataNodeContainer) links.getDataChildByName(QName.create(test.getQNameModule(),
333                 "link"));
334         final DataNodeContainer nodes = (DataNodeContainer) link.getDataChildByName(QName.create(test.getQNameModule(),
335                 "nodes"));
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());
343     }
344 }