2 * Copyright (c) 2013 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.parser.impl;
10 import static org.junit.Assert.*;
12 import java.io.FileNotFoundException;
14 import java.text.ParseException;
15 import java.util.ArrayList;
16 import java.util.Date;
17 import java.util.List;
20 import org.junit.Before;
21 import org.junit.Test;
22 import org.opendaylight.yangtools.yang.common.QName;
23 import org.opendaylight.yangtools.yang.model.api.ChoiceNode;
24 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
25 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
26 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
27 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
28 import org.opendaylight.yangtools.yang.model.api.Module;
29 import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
30 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
31 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
32 import org.opendaylight.yangtools.yang.model.util.BaseTypes;
33 import org.opendaylight.yangtools.yang.model.util.BooleanType;
34 import org.opendaylight.yangtools.yang.model.util.Uint32;
35 import org.opendaylight.yangtools.yang.model.util.Uint8;
36 import org.opendaylight.yangtools.yang.model.util.UnionType;
38 import com.google.common.collect.Lists;
40 public class UsesAugmentTest {
41 private final URI ns = URI.create("urn:opendaylight:params:xml:ns:yang:uses-grouping");
43 private final String prefix = "ug";
45 private Set<Module> modules;
48 public void init() throws FileNotFoundException, ParseException {
49 rev = TestUtils.simpleDateFormat.parse("2013-07-30");
53 * Structure of testing model:
56 * |-- leaf version (U)
59 * |-- |-- container rp
60 * |-- |-- |-- leaf priority (U)
61 * |-- |-- |-- container box (U)
62 * |-- |-- |-- |-- container order (A)
63 * |-- |-- |-- |-- |-- leaf delete (U)
64 * |-- |-- |-- |-- |-- |-- leaf setup (U)
65 * |-- |-- |-- leaf processing-rule (U)
66 * |-- |-- |-- leaf ignore (U)
67 * |-- |-- path-key-expansion
68 * |-- |-- |-- container path-key
69 * |-- |-- |-- |-- list path-keys (U)
70 * |-- |-- |-- |-- |-- leaf version (U)
71 * |-- |-- |-- |-- |-- leaf type (U)
72 * |-- |-- |-- |-- |-- leaf processing-rule (U)
73 * |-- |-- |-- |-- |-- leaf ignore (U)
74 * |-- |-- container segment-computation
75 * |-- |-- |-- container p2p
76 * |-- |-- |-- |-- container endpoints
77 * |-- |-- |-- |-- |-- leaf processing-rule (U)
78 * |-- |-- |-- |-- |-- leaf ignore (U)
79 * |-- |-- |-- |-- |-- container box (U)
80 * |-- |-- |-- |-- |-- choice address-family (U)
81 * |-- |-- |-- |-- |-- |-- case ipv4
82 * |-- |-- |-- |-- |-- |-- |-- leaf source-ipv4-address
83 * |-- |-- |-- |-- |-- |-- case ipv6
84 * |-- |-- |-- |-- |-- |-- |-- leaf source-ipv6-address
85 * |-- |-- |-- |-- container reported-route
86 * |-- |-- |-- |-- |-- container bandwidth
87 * |-- |-- |-- |-- |-- list subobjects(U)
88 * |-- |-- |-- |-- |-- leaf processing-rule (U)
89 * |-- |-- |-- |-- |-- leaf ignore (U)
90 * |-- |-- |-- |-- container bandwidth (U)
91 * |-- |-- |-- |-- |-- container bandwidth (U)
92 * |-- |-- |-- |-- |-- leaf processing-rule (U)
93 * |-- |-- |-- |-- |-- leaf ignore (U)
96 * |-- |-- |-- leaf metric-type (U)
97 * |-- |-- |-- container box (U)
98 * |-- |-- |-- leaf processing-rule (U)
99 * |-- |-- |-- leaf ignore (U)
100 * |-- |-- leaf link-diverse (U)
101 * |-- |-- leaf processing-rule (U)
102 * |-- |-- leaf ignore (U)
104 * U = added by uses A = added by augment
107 public void testAugmentInUses() throws Exception {
108 modules = TestUtils.loadModules(getClass().getResource("/grouping-test").getPath());
109 Module testModule = TestUtils.findModule(modules, "uses-grouping");
111 // * notification pcreq
112 Set<NotificationDefinition> notifications = testModule.getNotifications();
113 assertEquals(1, notifications.size());
114 NotificationDefinition pcreq = notifications.iterator().next();
115 assertNotNull(pcreq);
116 assertEquals(createPath("pcreq"), pcreq.getPath());
117 Set<DataSchemaNode> childNodes = pcreq.getChildNodes();
118 assertEquals(4, childNodes.size());
119 // * |-- leaf version (U)
120 LeafSchemaNode version = (LeafSchemaNode) pcreq.getDataChildByName("version");
121 assertNotNull(version);
122 assertEquals(createPath("pcreq", "version"), version.getPath());
123 assertEquals(createPath("pcreq", "version", "protocol-version"), version.getType().getPath());
124 assertEquals(Uint8.getInstance(), version.getType().getBaseType());
125 assertTrue(version.isAddedByUses());
126 // * |-- leaf type (U)
127 LeafSchemaNode type = (LeafSchemaNode) pcreq.getDataChildByName("type");
129 assertTrue(type.isAddedByUses());
130 assertEquals(createPath("pcreq", "type"), type.getPath());
131 assertEquals(createPath("pcreq", "type", "int-ext"), type.getType().getPath());
132 UnionType union = (UnionType)type.getType().getBaseType();
133 assertEquals(BaseTypes.schemaPath(BaseTypes.constructQName("union")), union.getPath());
134 assertEquals(2, union.getTypes().size());
135 // * |-- list requests
136 ListSchemaNode requests = (ListSchemaNode) pcreq.getDataChildByName("requests");
137 assertNotNull(requests);
138 assertEquals(createPath("pcreq", "requests"), requests.getPath());
139 assertFalse(requests.isAddedByUses());
140 childNodes = requests.getChildNodes();
141 assertEquals(3, childNodes.size());
142 // * |-- |-- container rp
143 ContainerSchemaNode rp = (ContainerSchemaNode) requests.getDataChildByName("rp");
145 assertEquals(createPath("pcreq", "requests", "rp"), rp.getPath());
146 assertFalse(rp.isAddedByUses());
147 childNodes = rp.getChildNodes();
148 assertEquals(4, childNodes.size());
149 // * |-- |-- |-- leaf priority (U)
150 LeafSchemaNode priority = (LeafSchemaNode) rp.getDataChildByName("priority");
151 assertNotNull(priority);
152 assertEquals(createPath("pcreq", "requests", "rp", "priority"), priority.getPath());
153 assertEquals(createPath("rp-object", "priority", "uint8"), priority.getType().getPath());
154 assertEquals(Uint8.getInstance(), priority.getType().getBaseType());
155 assertTrue(priority.isAddedByUses());
156 // * |-- |-- |-- container box (U)
157 ContainerSchemaNode box = (ContainerSchemaNode) rp.getDataChildByName("box");
159 assertEquals(createPath("pcreq", "requests", "rp", "box"), box.getPath());
160 assertTrue(box.isAddedByUses());
161 // * |-- |-- |-- |-- container order (A)
162 ContainerSchemaNode order = (ContainerSchemaNode) box.getDataChildByName("order");
163 assertNotNull(order);
164 assertEquals(createPath("pcreq", "requests", "rp", "box", "order"), order.getPath());
165 assertFalse(order.isAddedByUses());
166 assertTrue(order.isAugmenting());
167 assertEquals(2, order.getChildNodes().size());
168 // * |-- |-- |-- |-- |-- leaf delete (U)
169 LeafSchemaNode delete = (LeafSchemaNode) order.getDataChildByName("delete");
170 assertNotNull(delete);
171 assertEquals(createPath("pcreq", "requests", "rp", "box", "order", "delete"), delete.getPath());
172 assertEquals(Uint32.getInstance(), delete.getType());
173 assertTrue(delete.isAddedByUses());
174 // * |-- |-- |-- |-- |-- leaf ignore (U)
175 LeafSchemaNode setup = (LeafSchemaNode) order.getDataChildByName("setup");
176 assertNotNull(setup);
177 assertEquals(createPath("pcreq", "requests", "rp", "box", "order", "setup"), setup.getPath());
178 assertEquals(Uint32.getInstance(), setup.getType());
179 assertTrue(setup.isAddedByUses());
180 // * |-- |-- |-- leaf processing-rule (U)
181 LeafSchemaNode processingRule = (LeafSchemaNode) rp.getDataChildByName("processing-rule");
182 assertNotNull(processingRule);
183 assertEquals(createPath("pcreq", "requests", "rp", "processing-rule"), processingRule.getPath());
184 assertEquals(BooleanType.getInstance(), processingRule.getType());
185 assertTrue(processingRule.isAddedByUses());
186 // * |-- |-- |-- leaf ignore (U)
187 LeafSchemaNode ignore = (LeafSchemaNode) rp.getDataChildByName("ignore");
188 assertNotNull(ignore);
189 assertEquals(createPath("pcreq", "requests", "rp", "ignore"), ignore.getPath());
190 assertEquals(BooleanType.getInstance(), ignore.getType());
191 assertTrue(ignore.isAddedByUses());
192 // * |-- |-- path-key-expansion
193 ContainerSchemaNode pke = (ContainerSchemaNode) requests.getDataChildByName("path-key-expansion");
195 assertEquals(createPath("pcreq", "requests", "path-key-expansion"), pke.getPath());
196 assertFalse(pke.isAddedByUses());
197 // * |-- |-- |-- path-key
198 ContainerSchemaNode pathKey = (ContainerSchemaNode) pke.getDataChildByName("path-key");
199 assertNotNull(pathKey);
200 assertEquals(createPath("pcreq", "requests", "path-key-expansion", "path-key"), pathKey.getPath());
201 assertFalse(pathKey.isAddedByUses());
202 assertEquals(3, pathKey.getChildNodes().size());
203 // * |-- |-- |-- |-- list path-keys (U)
204 ListSchemaNode pathKeys = (ListSchemaNode) pathKey.getDataChildByName("path-keys");
205 assertNotNull(pathKeys);
206 assertEquals(createPath("pcreq", "requests", "path-key-expansion", "path-key", "path-keys"), pathKeys.getPath());
207 assertTrue(pathKeys.isAddedByUses());
208 childNodes = pathKeys.getChildNodes();
209 assertEquals(2, childNodes.size());
210 // * |-- |-- |-- |-- |-- leaf version (U)
211 version = (LeafSchemaNode) pathKeys.getDataChildByName("version");
212 assertNotNull(version);
213 assertEquals(createPath("pcreq", "requests", "path-key-expansion", "path-key", "path-keys", "version"),
216 createPath("pcreq", "requests", "path-key-expansion", "path-key", "path-keys", "version",
217 "protocol-version"), version.getType().getPath());
218 assertEquals(Uint8.getInstance(), version.getType().getBaseType());
219 assertTrue(version.isAddedByUses());
220 assertFalse(version.isAugmenting());
221 // * |-- |-- |-- |-- |-- leaf type (U)
222 type = (LeafSchemaNode) pathKeys.getDataChildByName("type");
224 assertEquals(createPath("pcreq", "requests", "path-key-expansion", "path-key", "path-keys", "type"),
227 createPath("pcreq", "requests", "path-key-expansion", "path-key", "path-keys", "type",
228 "int-ext"), type.getType().getPath());
229 assertTrue(type.isAddedByUses());
230 assertFalse(type.isAugmenting());
231 // * |-- |-- |-- |-- |-- leaf processing-rule (U)
232 processingRule = (LeafSchemaNode) pathKey.getDataChildByName("processing-rule");
233 assertNotNull(processingRule);
234 assertEquals(createPath("pcreq", "requests", "path-key-expansion", "path-key", "processing-rule"),
235 processingRule.getPath());
236 assertEquals(BooleanType.getInstance(), processingRule.getType());
237 assertTrue(processingRule.isAddedByUses());
238 // * |-- |-- |-- |-- |-- leaf ignore (U)
239 ignore = (LeafSchemaNode) pathKey.getDataChildByName("ignore");
240 assertNotNull(ignore);
241 assertEquals(createPath("pcreq", "requests", "path-key-expansion", "path-key", "ignore"), ignore.getPath());
242 assertEquals(BooleanType.getInstance(), ignore.getType());
243 assertTrue(ignore.isAddedByUses());
244 // * |-- |-- container segment-computation
245 ContainerSchemaNode sc = (ContainerSchemaNode) requests.getDataChildByName("segment-computation");
247 assertEquals(createPath("pcreq", "requests", "segment-computation"), sc.getPath());
248 assertFalse(sc.isAddedByUses());
249 // * |-- |-- |-- container p2p
250 ContainerSchemaNode p2p = (ContainerSchemaNode) sc.getDataChildByName("p2p");
252 assertEquals(createPath("pcreq", "requests", "segment-computation", "p2p"), p2p.getPath());
253 assertFalse(p2p.isAddedByUses());
254 // * |-- |-- |-- |-- container endpoints
255 ContainerSchemaNode endpoints = (ContainerSchemaNode) p2p.getDataChildByName("endpoints");
256 assertNotNull(endpoints);
257 assertEquals(createPath("pcreq", "requests", "segment-computation", "p2p", "endpoints"), endpoints.getPath());
258 assertFalse(endpoints.isAddedByUses());
259 // * |-- |-- |-- |-- |-- leaf processing-rule (U)
260 processingRule = (LeafSchemaNode) endpoints.getDataChildByName("processing-rule");
261 assertNotNull(processingRule);
262 assertEquals(createPath("pcreq", "requests", "segment-computation", "p2p", "endpoints", "processing-rule"),
263 processingRule.getPath());
264 assertEquals(BooleanType.getInstance(), processingRule.getType());
265 assertTrue(processingRule.isAddedByUses());
266 // * |-- |-- |-- |-- |-- leaf ignore (U)
267 ignore = (LeafSchemaNode) endpoints.getDataChildByName("ignore");
268 assertNotNull(ignore);
269 assertEquals(createPath("pcreq", "requests", "segment-computation", "p2p", "endpoints", "ignore"),
271 assertEquals(BooleanType.getInstance(), ignore.getType());
272 assertTrue(ignore.isAddedByUses());
273 // * |-- |-- |-- |-- |-- container box
274 box = (ContainerSchemaNode) endpoints.getDataChildByName("box");
276 assertEquals(createPath("pcreq", "requests", "segment-computation", "p2p", "endpoints", "box"), box.getPath());
277 assertTrue(box.isAddedByUses());
278 // * |-- |-- |-- |-- |-- choice address-family (U)
279 ChoiceNode af = (ChoiceNode) endpoints.getDataChildByName("address-family");
281 assertEquals(createPath("pcreq", "requests", "segment-computation", "p2p", "endpoints", "address-family"),
283 assertTrue(af.isAddedByUses());
284 // * |-- |-- |-- |-- container reported-route
285 ContainerSchemaNode reportedRoute = (ContainerSchemaNode) p2p.getDataChildByName("reported-route");
286 assertNotNull(reportedRoute);
287 assertEquals(createPath("pcreq", "requests", "segment-computation", "p2p", "reported-route"),
288 reportedRoute.getPath());
289 assertFalse(reportedRoute.isAddedByUses());
290 // * |-- |-- |-- |-- |-- container bandwidth
291 ContainerSchemaNode bandwidth = (ContainerSchemaNode) reportedRoute.getDataChildByName("bandwidth");
292 assertNotNull(bandwidth);
293 assertEquals(createPath("pcreq", "requests", "segment-computation", "p2p", "reported-route", "bandwidth"),
294 bandwidth.getPath());
295 assertFalse(bandwidth.isAddedByUses());
296 // * |-- |-- |-- |-- |-- list subobjects
297 ListSchemaNode subobjects = (ListSchemaNode) reportedRoute.getDataChildByName("subobjects");
298 assertNotNull(subobjects);
299 assertEquals(createPath("pcreq", "requests", "segment-computation", "p2p", "reported-route", "subobjects"),
300 subobjects.getPath());
301 assertTrue(subobjects.isAddedByUses());
302 // * |-- |-- |-- |-- |-- leaf processing-rule (U)
303 processingRule = (LeafSchemaNode) reportedRoute.getDataChildByName("processing-rule");
304 assertNotNull(processingRule);
306 createPath("pcreq", "requests", "segment-computation", "p2p", "reported-route", "processing-rule"),
307 processingRule.getPath());
308 assertEquals(BooleanType.getInstance(), processingRule.getType());
309 assertTrue(processingRule.isAddedByUses());
310 // * |-- |-- |-- |-- |-- leaf ignore (U)
311 ignore = (LeafSchemaNode) reportedRoute.getDataChildByName("ignore");
312 assertNotNull(ignore);
313 assertEquals(createPath("pcreq", "requests", "segment-computation", "p2p", "reported-route", "ignore"),
315 assertEquals(BooleanType.getInstance(), ignore.getType());
316 assertTrue(ignore.isAddedByUses());
317 // * |-- |-- |-- |-- container bandwidth (U)
318 bandwidth = (ContainerSchemaNode) p2p.getDataChildByName("bandwidth");
319 assertNotNull(bandwidth);
320 assertEquals(createPath("pcreq", "requests", "segment-computation", "p2p", "bandwidth"), bandwidth.getPath());
321 assertTrue(bandwidth.isAddedByUses());
322 // * |-- |-- |-- |-- |-- container bandwidth (U)
323 ContainerSchemaNode bandwidthInner = (ContainerSchemaNode) bandwidth.getDataChildByName("bandwidth");
324 assertNotNull(bandwidthInner);
325 assertEquals(createPath("pcreq", "requests", "segment-computation", "p2p", "bandwidth", "bandwidth"),
326 bandwidthInner.getPath());
327 assertTrue(bandwidthInner.isAddedByUses());
328 // * |-- |-- |-- |-- |-- leaf processing-rule (U)
329 processingRule = (LeafSchemaNode) bandwidth.getDataChildByName("processing-rule");
330 assertNotNull(processingRule);
331 assertEquals(createPath("pcreq", "requests", "segment-computation", "p2p", "bandwidth", "processing-rule"),
332 processingRule.getPath());
333 assertEquals(BooleanType.getInstance(), processingRule.getType());
334 assertTrue(processingRule.isAddedByUses());
335 // * |-- |-- |-- |-- |-- leaf ignore (U)
336 ignore = (LeafSchemaNode) bandwidth.getDataChildByName("ignore");
337 assertNotNull(ignore);
338 assertEquals(createPath("pcreq", "requests", "segment-computation", "p2p", "bandwidth", "ignore"),
340 assertEquals(BooleanType.getInstance(), ignore.getType());
341 assertTrue(ignore.isAddedByUses());
343 ListSchemaNode svec = (ListSchemaNode) pcreq.getDataChildByName("svec");
345 assertEquals(createPath("pcreq", "svec"), svec.getPath());
346 assertFalse(svec.isAddedByUses());
347 // * |-- |-- list metric
348 ListSchemaNode metric = (ListSchemaNode) svec.getDataChildByName("metric");
349 assertNotNull(metric);
350 assertEquals(createPath("pcreq", "svec", "metric"), metric.getPath());
351 assertFalse(metric.isAddedByUses());
352 // * |-- |-- |-- leaf metric-type (U)
353 LeafSchemaNode metricType = (LeafSchemaNode) metric.getDataChildByName("metric-type");
354 assertNotNull(metricType);
355 assertEquals(createPath("pcreq", "svec", "metric", "metric-type"), metricType.getPath());
356 assertEquals(Uint8.getInstance(), metricType.getType());
357 assertTrue(metricType.isAddedByUses());
358 // * |-- |-- |-- box (U)
359 box = (ContainerSchemaNode) metric.getDataChildByName("box");
361 assertEquals(createPath("pcreq", "svec", "metric", "box"), box.getPath());
362 assertTrue(box.isAddedByUses());
363 // * |-- |-- |-- leaf processing-rule (U)
364 processingRule = (LeafSchemaNode) metric.getDataChildByName("processing-rule");
365 assertNotNull(processingRule);
366 assertEquals(createPath("pcreq", "svec", "metric", "processing-rule"), processingRule.getPath());
367 assertEquals(BooleanType.getInstance(), processingRule.getType());
368 assertTrue(processingRule.isAddedByUses());
369 // * |-- |-- |-- leaf ignore (U)
370 ignore = (LeafSchemaNode) metric.getDataChildByName("ignore");
371 assertNotNull(ignore);
372 assertEquals(createPath("pcreq", "svec", "metric", "ignore"), ignore.getPath());
373 assertEquals(BooleanType.getInstance(), ignore.getType());
374 assertTrue(ignore.isAddedByUses());
375 // * |-- |-- leaf link-diverse (U)
376 LeafSchemaNode linkDiverse = (LeafSchemaNode) svec.getDataChildByName("link-diverse");
377 assertNotNull(linkDiverse);
378 assertEquals(createPath("pcreq", "svec", "link-diverse"), linkDiverse.getPath());
379 assertEquals(BooleanType.getInstance(), linkDiverse.getType());
380 assertTrue(linkDiverse.isAddedByUses());
381 // * |-- |-- leaf processing-rule (U)
382 processingRule = (LeafSchemaNode) svec.getDataChildByName("processing-rule");
383 assertNotNull(processingRule);
384 assertEquals(createPath("pcreq", "svec", "processing-rule"), processingRule.getPath());
385 assertEquals(BooleanType.getInstance(), processingRule.getType());
386 assertTrue(processingRule.isAddedByUses());
387 // * |-- |-- leaf ignore (U)
388 ignore = (LeafSchemaNode) svec.getDataChildByName("ignore");
389 assertNotNull(ignore);
390 assertEquals(createPath("pcreq", "svec", "ignore"), ignore.getPath());
391 assertEquals(BooleanType.getInstance(), ignore.getType());
392 assertTrue(ignore.isAddedByUses());
395 private SchemaPath createPath(String... names) {
396 List<QName> path = new ArrayList<>();
397 for (String name : names) {
398 path.add(new QName(ns, rev, prefix, name));
400 return new SchemaPath(path, true);
404 public void testTypedefs() throws FileNotFoundException {
405 modules = TestUtils.loadModules(getClass().getResource("/grouping-test").getPath());
406 Module testModule = TestUtils.findModule(modules, "uses-grouping");
407 Set<TypeDefinition<?>> types = testModule.getTypeDefinitions();
409 TypeDefinition<?> intExt = null;
410 for(TypeDefinition<?> td : types) {
411 if("int-ext".equals(td.getQName().getLocalName())) {
415 assertNotNull(intExt);
416 assertEquals(createPath("int-ext"), intExt.getPath());
418 UnionType union = (UnionType)intExt.getBaseType();
420 TypeDefinition<?> uint8 = null;
421 TypeDefinition<?> pv = null;
422 for(TypeDefinition<?> td : union.getTypes()) {
423 if("uint8".equals(td.getQName().getLocalName())) {
425 } else if("protocol-version".equals(td.getQName().getLocalName())) {
429 assertNotNull(uint8);
432 SchemaPath expectedPath = null;
433 QName q1 = BaseTypes.constructQName("union");
435 expectedPath = new SchemaPath(Lists.newArrayList(q1), true);
436 assertEquals(expectedPath, union.getPath());