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.jupiter.api.Assertions.assertEquals;
11 import static org.junit.jupiter.api.Assertions.assertFalse;
12 import static org.junit.jupiter.api.Assertions.assertInstanceOf;
13 import static org.junit.jupiter.api.Assertions.assertNotNull;
14 import static org.junit.jupiter.api.Assertions.assertTrue;
16 import java.util.Collection;
17 import org.junit.jupiter.api.BeforeAll;
18 import org.junit.jupiter.api.Test;
19 import org.opendaylight.yangtools.yang.common.QName;
20 import org.opendaylight.yangtools.yang.common.QNameModule;
21 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
22 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
23 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
24 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
25 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
26 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
27 import org.opendaylight.yangtools.yang.model.api.Module;
28 import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
29 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
30 import org.opendaylight.yangtools.yang.model.api.type.Uint8TypeDefinition;
31 import org.opendaylight.yangtools.yang.model.api.type.UnionTypeDefinition;
32 import org.opendaylight.yangtools.yang.model.ri.type.BaseTypes;
34 class UsesAugmentTest extends AbstractYangTest {
35 private static final QNameModule UG =
36 QNameModule.of("urn:opendaylight:params:xml:ns:yang:uses-grouping", "2013-07-30");
37 private static final QNameModule GD =
38 QNameModule.of("urn:opendaylight:params:xml:ns:yang:grouping-definitions", "2013-09-04");
40 private static EffectiveModelContext CONTEXT;
43 static void beforeClass() throws Exception {
44 CONTEXT = assertEffectiveModelDir("/grouping-test");
48 * Structure of testing model:
51 * |-- leaf version (U)
54 * |-- |-- container rp
55 * |-- |-- |-- leaf priority (U)
56 * |-- |-- |-- container box (U)
57 * |-- |-- |-- |-- container order (A)
58 * |-- |-- |-- |-- |-- leaf delete (U)
59 * |-- |-- |-- |-- |-- |-- leaf setup (U)
60 * |-- |-- |-- leaf processing-rule (U)
61 * |-- |-- |-- leaf ignore (U)
62 * |-- |-- path-key-expansion
63 * |-- |-- |-- container path-key
64 * |-- |-- |-- |-- list path-keys (U)
65 * |-- |-- |-- |-- |-- leaf version (U)
66 * |-- |-- |-- |-- |-- leaf type (U)
67 * |-- |-- |-- |-- |-- leaf processing-rule (U)
68 * |-- |-- |-- |-- |-- leaf ignore (U)
69 * |-- |-- container segment-computation
70 * |-- |-- |-- container p2p
71 * |-- |-- |-- |-- container endpoints
72 * |-- |-- |-- |-- |-- leaf processing-rule (U)
73 * |-- |-- |-- |-- |-- leaf ignore (U)
74 * |-- |-- |-- |-- |-- container box (U)
75 * |-- |-- |-- |-- |-- choice address-family (U)
76 * |-- |-- |-- |-- |-- |-- case ipv4
77 * |-- |-- |-- |-- |-- |-- |-- leaf source-ipv4-address
78 * |-- |-- |-- |-- |-- |-- case ipv6
79 * |-- |-- |-- |-- |-- |-- |-- leaf source-ipv6-address
80 * |-- |-- |-- |-- container reported-route
81 * |-- |-- |-- |-- |-- container bandwidth
82 * |-- |-- |-- |-- |-- list subobjects(U)
83 * |-- |-- |-- |-- |-- leaf processing-rule (U)
84 * |-- |-- |-- |-- |-- leaf ignore (U)
85 * |-- |-- |-- |-- container bandwidth (U)
86 * |-- |-- |-- |-- |-- container bandwidth (U)
87 * |-- |-- |-- |-- |-- leaf processing-rule (U)
88 * |-- |-- |-- |-- |-- leaf ignore (U)
91 * |-- |-- |-- leaf metric-type (U)
92 * |-- |-- |-- container box (U)
93 * |-- |-- |-- leaf processing-rule (U)
94 * |-- |-- |-- leaf ignore (U)
95 * |-- |-- leaf link-diverse (U)
96 * |-- |-- leaf processing-rule (U)
97 * |-- |-- leaf ignore (U)
99 * U = added by uses A = added by augment
101 * @throws Exception if exception occurs
104 void testAugmentInUses() throws Exception {
105 final Module testModule = CONTEXT.findModules("uses-grouping").iterator().next();
107 // * notification pcreq
108 final Collection<? extends NotificationDefinition> notifications = testModule.getNotifications();
109 assertEquals(1, notifications.size());
110 final NotificationDefinition pcreq = notifications.iterator().next();
111 assertNotNull(pcreq);
112 assertEquals(QName.create(UG, "pcreq"), pcreq.getQName());
113 Collection<? extends DataSchemaNode> childNodes = pcreq.getChildNodes();
114 assertEquals(4, childNodes.size());
115 // * |-- leaf version
116 LeafSchemaNode version = (LeafSchemaNode) pcreq.getDataChildByName(QName.create(testModule.getQNameModule(),
118 assertNotNull(version);
119 assertEquals(QName.create(UG, "version"), version.getQName());
120 assertEquals(QName.create(UG, "version"), version.getType().getQName());
121 assertEquals(BaseTypes.uint8Type(), version.getType().getBaseType().getBaseType());
122 assertTrue(version.isAddedByUses());
124 LeafSchemaNode type = (LeafSchemaNode) pcreq.getDataChildByName(QName.create(testModule.getQNameModule(),
127 assertTrue(type.isAddedByUses());
128 assertEquals(QName.create(UG, "type"), type.getQName());
129 assertEquals(QName.create(GD, "int-ext"), type.getType().getQName());
130 final UnionTypeDefinition union = (UnionTypeDefinition) type.getType().getBaseType();
131 assertEquals(QName.create(GD, "union"), union.getQName());
132 assertEquals(2, union.getTypes().size());
133 // * |-- list requests
134 final ListSchemaNode requests = (ListSchemaNode) pcreq.getDataChildByName(QName.create(
135 testModule.getQNameModule(), "requests"));
136 assertNotNull(requests);
137 assertEquals(QName.create(UG, "requests"), requests.getQName());
138 assertFalse(requests.isAddedByUses());
139 childNodes = requests.getChildNodes();
140 assertEquals(3, childNodes.size());
141 // * |-- |-- container rp
142 final ContainerSchemaNode rp = (ContainerSchemaNode) requests.getDataChildByName(QName.create(
143 testModule.getQNameModule(), "rp"));
145 assertEquals(QName.create(UG, "rp"), rp.getQName());
146 assertFalse(rp.isAddedByUses());
147 childNodes = rp.getChildNodes();
148 assertEquals(4, childNodes.size());
149 // * |-- |-- |-- leaf processing-rule
150 LeafSchemaNode processingRule = (LeafSchemaNode) rp.getDataChildByName(QName.create(
151 testModule.getQNameModule(), "processing-rule"));
152 assertNotNull(processingRule);
153 assertEquals(QName.create(UG, "processing-rule"), processingRule.getQName());
154 assertEquals(BaseTypes.booleanType(), processingRule.getType());
155 assertTrue(processingRule.isAddedByUses());
156 // * |-- |-- |-- leaf ignore
157 LeafSchemaNode ignore = (LeafSchemaNode) rp.getDataChildByName(QName.create(testModule.getQNameModule(),
159 assertNotNull(ignore);
160 assertEquals(QName.create(UG, "ignore"), ignore.getQName());
161 assertEquals(BaseTypes.booleanType(), ignore.getType());
162 assertTrue(ignore.isAddedByUses());
163 // * |-- |-- |-- leaf priority
164 final LeafSchemaNode priority = (LeafSchemaNode) rp.getDataChildByName(QName.create(
165 testModule.getQNameModule(), "priority"));
166 assertNotNull(priority);
167 assertEquals(QName.create(UG, "priority"), priority.getQName());
169 // assertEquals(QName.create(UG, "uint8"), priority.getType().getQName());
170 assertEquals(BaseTypes.uint8Type(), priority.getType().getBaseType());
171 assertTrue(priority.isAddedByUses());
172 // * |-- |-- |-- container box
173 ContainerSchemaNode box = (ContainerSchemaNode) rp.getDataChildByName(QName.create(testModule.getQNameModule(),
176 assertEquals(QName.create(UG, "box"), box.getQName());
177 assertTrue(box.isAddedByUses());
178 // * |-- |-- |-- |-- container order
179 final ContainerSchemaNode order = (ContainerSchemaNode) box.getDataChildByName(QName.create(
180 testModule.getQNameModule(), "order"));
181 assertNotNull(order);
182 assertEquals(QName.create(UG, "order"), order.getQName());
183 assertTrue(order.isAddedByUses());
184 assertTrue(order.isAugmenting());
185 assertEquals(2, order.getChildNodes().size());
186 // * |-- |-- |-- |-- |-- leaf delete
187 final LeafSchemaNode delete = (LeafSchemaNode) order.getDataChildByName(QName.create(
188 testModule.getQNameModule(), "delete"));
189 assertNotNull(delete);
190 assertEquals(QName.create(UG, "delete"), delete.getQName());
191 assertEquals(BaseTypes.uint32Type(), delete.getType());
192 assertTrue(delete.isAddedByUses());
193 // * |-- |-- |-- |-- |-- leaf setup
194 final LeafSchemaNode setup = (LeafSchemaNode) order.getDataChildByName(QName.create(
195 testModule.getQNameModule(), "setup"));
196 assertNotNull(setup);
197 assertEquals(QName.create(UG, "setup"), setup.getQName());
198 assertEquals(BaseTypes.uint32Type(), setup.getType());
199 assertTrue(setup.isAddedByUses());
200 // * |-- |-- path-key-expansion
201 final ContainerSchemaNode pke = (ContainerSchemaNode) requests.getDataChildByName(QName.create(
202 testModule.getQNameModule(), "path-key-expansion"));
204 assertEquals(QName.create(UG, "path-key-expansion"), pke.getQName());
205 assertFalse(pke.isAddedByUses());
206 // * |-- |-- |-- path-key
207 final ContainerSchemaNode pathKey = (ContainerSchemaNode) pke.getDataChildByName(QName.create(
208 testModule.getQNameModule(), "path-key"));
209 assertNotNull(pathKey);
210 assertEquals(QName.create(UG, "path-key"), pathKey.getQName());
211 assertFalse(pathKey.isAddedByUses());
212 assertEquals(3, pathKey.getChildNodes().size());
213 // * |-- |-- |-- |-- leaf processing-rule
214 processingRule = (LeafSchemaNode) pathKey.getDataChildByName(QName.create(testModule.getQNameModule(),
216 assertNotNull(processingRule);
217 assertEquals(QName.create(UG, "processing-rule"), processingRule.getQName());
218 assertEquals(BaseTypes.booleanType(), processingRule.getType());
219 assertTrue(processingRule.isAddedByUses());
220 // * |-- |-- |-- |-- leaf ignore
221 ignore = (LeafSchemaNode) pathKey.getDataChildByName(QName.create(testModule.getQNameModule(), "ignore"));
222 assertNotNull(ignore);
223 assertEquals(QName.create(UG, "ignore"), ignore.getQName());
224 assertEquals(BaseTypes.booleanType(), ignore.getType());
225 assertTrue(ignore.isAddedByUses());
226 // * |-- |-- |-- |-- list path-keys
227 final ListSchemaNode pathKeys = (ListSchemaNode) pathKey.getDataChildByName(QName.create(
228 testModule.getQNameModule(), "path-keys"));
229 assertNotNull(pathKeys);
230 assertEquals(QName.create(UG, "path-keys"), pathKeys.getQName());
231 assertTrue(pathKeys.isAddedByUses());
232 childNodes = pathKeys.getChildNodes();
233 assertEquals(2, childNodes.size());
234 // * |-- |-- |-- |-- |-- leaf version
235 version = (LeafSchemaNode) pathKeys.getDataChildByName(QName.create(testModule.getQNameModule(), "version"));
236 assertNotNull(version);
237 assertEquals(QName.create(UG, "version"), version.getQName());
238 assertInstanceOf(Uint8TypeDefinition.class, version.getType());
239 assertEquals(BaseTypes.uint8Type(), version.getType().getBaseType().getBaseType());
240 assertTrue(version.isAddedByUses());
241 assertTrue(version.isAugmenting());
242 // * |-- |-- |-- |-- |-- leaf type
243 type = (LeafSchemaNode) pathKeys.getDataChildByName(QName.create(testModule.getQNameModule(), "type"));
245 assertEquals(QName.create(UG, "type"), type.getQName());
246 assertInstanceOf(UnionTypeDefinition.class, type.getType());
247 assertTrue(type.isAddedByUses());
248 assertTrue(type.isAugmenting());
249 // * |-- |-- container segment-computation
250 final ContainerSchemaNode sc = (ContainerSchemaNode) requests.getDataChildByName(QName.create(
251 testModule.getQNameModule(), "segment-computation"));
253 assertEquals(QName.create(UG, "segment-computation"), sc.getQName());
254 assertFalse(sc.isAddedByUses());
255 // * |-- |-- |-- container p2p
256 final ContainerSchemaNode p2p = (ContainerSchemaNode) sc.getDataChildByName(QName.create(
257 testModule.getQNameModule(), "p2p"));
259 assertEquals(QName.create(UG, "p2p"), p2p.getQName());
260 assertFalse(p2p.isAddedByUses());
261 // * |-- |-- |-- |-- container endpoints
262 final ContainerSchemaNode endpoints = (ContainerSchemaNode) p2p.getDataChildByName(QName.create(
263 testModule.getQNameModule(), "endpoints"));
264 assertNotNull(endpoints);
265 assertEquals(QName.create(UG, "endpoints"), endpoints.getQName());
266 assertFalse(endpoints.isAddedByUses());
267 // * |-- |-- |-- |-- |-- leaf processing-rule
268 processingRule = (LeafSchemaNode) endpoints.getDataChildByName(QName.create(testModule.getQNameModule(),
270 assertNotNull(processingRule);
271 assertEquals(QName.create(UG, "processing-rule"), processingRule.getQName());
272 assertEquals(BaseTypes.booleanType(), processingRule.getType());
273 assertTrue(processingRule.isAddedByUses());
274 // * |-- |-- |-- |-- |-- leaf ignore
275 ignore = (LeafSchemaNode) endpoints.getDataChildByName(QName.create(testModule.getQNameModule(), "ignore"));
276 assertNotNull(ignore);
277 assertEquals(QName.create(UG, "ignore"), ignore.getQName());
278 assertEquals(BaseTypes.booleanType(), ignore.getType());
279 assertTrue(ignore.isAddedByUses());
280 // * |-- |-- |-- |-- |-- container box
281 box = (ContainerSchemaNode) endpoints.getDataChildByName(QName.create(testModule.getQNameModule(), "box"));
283 assertEquals(QName.create(UG, "box"), box.getQName());
284 assertTrue(box.isAddedByUses());
285 // * |-- |-- |-- |-- |-- choice address-family
286 final ChoiceSchemaNode af = (ChoiceSchemaNode) endpoints.getDataChildByName(QName.create(
287 testModule.getQNameModule(), "address-family"));
289 assertEquals(QName.create(UG, "address-family"), af.getQName());
290 assertTrue(af.isAddedByUses());
291 // * |-- |-- |-- |-- container reported-route
292 final ContainerSchemaNode reportedRoute = (ContainerSchemaNode) p2p.getDataChildByName(QName.create(
293 testModule.getQNameModule(), "reported-route"));
294 assertNotNull(reportedRoute);
295 assertEquals(QName.create(UG, "reported-route"), reportedRoute.getQName());
296 assertFalse(reportedRoute.isAddedByUses());
297 // * |-- |-- |-- |-- |-- leaf processing-rule
298 processingRule = (LeafSchemaNode) reportedRoute.getDataChildByName(QName.create(testModule.getQNameModule(),
300 assertNotNull(processingRule);
301 assertEquals(QName.create(UG, "processing-rule"), processingRule.getQName());
302 assertEquals(BaseTypes.booleanType(), processingRule.getType());
303 assertTrue(processingRule.isAddedByUses());
304 // * |-- |-- |-- |-- |-- leaf ignore
305 ignore = (LeafSchemaNode) reportedRoute.getDataChildByName(QName.create(testModule.getQNameModule(), "ignore"));
306 assertNotNull(ignore);
307 assertEquals(QName.create(UG, "ignore"), ignore.getQName());
308 assertEquals(BaseTypes.booleanType(), ignore.getType());
309 assertTrue(ignore.isAddedByUses());
310 // * |-- |-- |-- |-- |-- list subobjects
311 final ListSchemaNode subobjects = (ListSchemaNode) reportedRoute.getDataChildByName(QName.create(
312 testModule.getQNameModule(), "subobjects"));
313 assertNotNull(subobjects);
314 assertEquals(QName.create(UG, "subobjects"), subobjects.getQName());
315 assertTrue(subobjects.isAddedByUses());
316 // * |-- |-- |-- |-- |-- container bandwidth
317 ContainerSchemaNode bandwidth = (ContainerSchemaNode) reportedRoute.getDataChildByName(QName.create(
318 testModule.getQNameModule(), "bandwidth"));
319 assertNotNull(bandwidth);
320 assertEquals(QName.create(UG, "bandwidth"), bandwidth.getQName());
321 assertFalse(bandwidth.isAddedByUses());
322 // * |-- |-- |-- |-- container bandwidth
323 bandwidth = (ContainerSchemaNode) p2p
324 .getDataChildByName(QName.create(testModule.getQNameModule(), "bandwidth"));
325 assertNotNull(bandwidth);
326 assertEquals(QName.create(UG, "bandwidth"), bandwidth.getQName());
327 assertTrue(bandwidth.isAddedByUses());
328 // * |-- |-- |-- |-- |-- leaf processing-rule
329 processingRule = (LeafSchemaNode) bandwidth.getDataChildByName(QName.create(testModule.getQNameModule(),
331 assertNotNull(processingRule);
332 assertEquals(QName.create(UG, "processing-rule"), processingRule.getQName());
333 assertEquals(BaseTypes.booleanType(), processingRule.getType());
334 assertTrue(processingRule.isAddedByUses());
335 // * |-- |-- |-- |-- |-- leaf ignore
336 ignore = (LeafSchemaNode) bandwidth.getDataChildByName(QName.create(testModule.getQNameModule(), "ignore"));
337 assertNotNull(ignore);
338 assertEquals(QName.create(UG, "ignore"), ignore.getQName());
339 assertEquals(BaseTypes.booleanType(), ignore.getType());
340 assertTrue(ignore.isAddedByUses());
341 // * |-- |-- |-- |-- |-- container bandwidth
342 final ContainerSchemaNode bandwidthInner = (ContainerSchemaNode) bandwidth.getDataChildByName(QName.create(
343 testModule.getQNameModule(), "bandwidth"));
344 assertNotNull(bandwidthInner);
345 assertEquals(QName.create(UG, "bandwidth"), bandwidth.getQName());
346 assertTrue(bandwidthInner.isAddedByUses());
348 final ListSchemaNode svec = (ListSchemaNode) pcreq.getDataChildByName(QName.create(testModule.getQNameModule(),
351 assertEquals(QName.create(UG, "svec"), svec.getQName());
352 assertFalse(svec.isAddedByUses());
353 // * |-- |-- leaf link-diverse
354 final LeafSchemaNode linkDiverse = (LeafSchemaNode) svec.getDataChildByName(QName.create(
355 testModule.getQNameModule(), "link-diverse"));
356 assertNotNull(linkDiverse);
357 assertEquals(QName.create(UG, "link-diverse"), linkDiverse.getQName());
358 assertEquals(BaseTypes.booleanType(), linkDiverse.getType().getBaseType());
359 assertTrue(linkDiverse.isAddedByUses());
360 // * |-- |-- leaf processing-rule
361 processingRule = (LeafSchemaNode) svec.getDataChildByName(QName.create(testModule.getQNameModule(),
363 assertNotNull(processingRule);
364 assertEquals(QName.create(UG, "processing-rule"), processingRule.getQName());
365 assertEquals(BaseTypes.booleanType(), processingRule.getType());
366 assertTrue(processingRule.isAddedByUses());
367 // * |-- |-- leaf ignore
368 ignore = (LeafSchemaNode) svec.getDataChildByName(QName.create(testModule.getQNameModule(), "ignore"));
369 assertNotNull(ignore);
370 assertEquals(QName.create(UG, "ignore"), ignore.getQName());
371 assertEquals(BaseTypes.booleanType(), ignore.getType());
372 assertTrue(ignore.isAddedByUses());
373 // * |-- |-- list metric
374 final ListSchemaNode metric = (ListSchemaNode) svec.getDataChildByName(QName.create(
375 testModule.getQNameModule(), "metric"));
376 assertNotNull(metric);
377 assertEquals(QName.create(UG, "metric"), metric.getQName());
378 assertFalse(metric.isAddedByUses());
379 // * |-- |-- |-- leaf metric-type
380 final LeafSchemaNode metricType = (LeafSchemaNode) metric.getDataChildByName(QName.create(
381 testModule.getQNameModule(), "metric-type"));
382 assertNotNull(metricType);
383 assertEquals(QName.create(UG, "metric-type"), metricType.getQName());
384 assertEquals(BaseTypes.uint8Type(), metricType.getType());
385 assertTrue(metricType.isAddedByUses());
387 box = (ContainerSchemaNode) metric.getDataChildByName(QName.create(testModule.getQNameModule(), "box"));
389 assertEquals(QName.create(UG, "box"), box.getQName());
390 assertTrue(box.isAddedByUses());
391 // * |-- |-- |-- leaf processing-rule
392 processingRule = (LeafSchemaNode) metric.getDataChildByName(QName.create(testModule.getQNameModule(),
394 assertNotNull(processingRule);
395 assertEquals(QName.create(UG, "processing-rule"), processingRule.getQName());
396 assertEquals(BaseTypes.booleanType(), processingRule.getType());
397 assertTrue(processingRule.isAddedByUses());
398 // * |-- |-- |-- leaf ignore
399 ignore = (LeafSchemaNode) metric.getDataChildByName(QName.create(testModule.getQNameModule(), "ignore"));
400 assertNotNull(ignore);
401 assertEquals(QName.create(UG, "ignore"), ignore.getQName());
402 assertEquals(BaseTypes.booleanType(), ignore.getType());
403 assertTrue(ignore.isAddedByUses());
407 void testTypedefs() throws Exception {
408 final Module testModule = CONTEXT.findModules("grouping-definitions").iterator().next();
409 final Collection<? extends TypeDefinition<?>> types = testModule.getTypeDefinitions();
411 TypeDefinition<?> intExt = null;
412 for (final TypeDefinition<?> td : types) {
413 if ("int-ext".equals(td.getQName().getLocalName())) {
417 assertNotNull(intExt);
419 assertEquals(QName.create(GD, "int-ext"), intExt.getQName());
421 final UnionTypeDefinition union = (UnionTypeDefinition) intExt.getBaseType();
423 TypeDefinition<?> uint8 = null;
424 TypeDefinition<?> pv = null;
425 for (final TypeDefinition<?> td : union.getTypes()) {
426 if ("uint8".equals(td.getQName().getLocalName())) {
428 } else if ("protocol-version".equals(td.getQName().getLocalName())) {
432 assertNotNull(uint8);
434 assertEquals(QName.create(GD, "union"), union.getQName());