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.DateFormat;
15 import java.text.ParseException;
16 import java.text.SimpleDateFormat;
17 import java.util.ArrayList;
18 import java.util.Arrays;
19 import java.util.Collections;
20 import java.util.Date;
21 import java.util.List;
24 import org.junit.BeforeClass;
25 import org.junit.Test;
26 import org.opendaylight.yangtools.yang.common.QName;
27 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
28 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
29 import org.opendaylight.yangtools.yang.model.api.ChoiceNode;
30 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
31 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
32 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
33 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
34 import org.opendaylight.yangtools.yang.model.api.Module;
35 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
36 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
37 import org.opendaylight.yangtools.yang.model.util.BaseTypes;
39 public class AugmentTest {
40 private static final DateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
42 private static final URI fooNS = URI.create("urn:opendaylight.foo");
43 private static final URI barNS = URI.create("urn:opendaylight.bar");
44 private static final URI bazNS = URI.create("urn:opendaylight.baz");
45 private static Date fooRev;
46 private static Date barRev;
47 private static Date bazRev;
48 private static final String foo = "foo";
49 private static final String bar = "bar";
50 private static final String baz = "baz";
51 private static QName q0;
52 private static QName q1;
53 private static QName q2;
55 private Set<Module> modules;
58 public static void init() throws FileNotFoundException, ParseException {
59 fooRev = simpleDateFormat.parse("2013-10-13");
60 barRev = simpleDateFormat.parse("2013-10-14");
61 bazRev = simpleDateFormat.parse("2013-10-15");
63 q0 = new QName(barNS, barRev, bar, "interfaces");
64 q1 = new QName(barNS, barRev, bar, "ifEntry");
65 q2 = new QName(bazNS, bazRev, baz, "augment-holder");
69 public void testAugmentParsing() throws Exception {
70 modules = TestUtils.loadModules(getClass().getResource("/augment-test/augment-in-augment").getPath());
71 SchemaPath expectedSchemaPath;
72 List<QName> qnames = new ArrayList<>();
78 Module module1 = TestUtils.findModule(modules, "foo");
79 Set<AugmentationSchema> augmentations = module1.getAugmentations();
80 assertEquals(1, augmentations.size());
81 AugmentationSchema augment = augmentations.iterator().next();
82 assertNotNull(augment);
84 expectedSchemaPath = new SchemaPath(qnames, true);
85 assertEquals(expectedSchemaPath, augment.getTargetPath());
87 Set<DataSchemaNode> augmentChildren = augment.getChildNodes();
88 assertEquals(4, augmentChildren.size());
89 for (DataSchemaNode dsn : augmentChildren) {
90 TestUtils.checkIsAugmenting(dsn, false);
93 LeafSchemaNode ds0ChannelNumber = (LeafSchemaNode) augment.getDataChildByName("ds0ChannelNumber");
94 LeafSchemaNode interfaceId = (LeafSchemaNode) augment.getDataChildByName("interface-id");
95 ContainerSchemaNode schemas = (ContainerSchemaNode) augment.getDataChildByName("schemas");
96 ChoiceNode odl = (ChoiceNode) augment.getDataChildByName("odl");
98 assertNotNull(ds0ChannelNumber);
99 assertNotNull(interfaceId);
100 assertNotNull(schemas);
103 // leaf ds0ChannelNumber
104 QName qname = new QName(fooNS, fooRev, foo, "ds0ChannelNumber");
106 assertEquals(qname, ds0ChannelNumber.getQName());
107 expectedSchemaPath = new SchemaPath(qnames, true);
108 assertEquals(expectedSchemaPath, ds0ChannelNumber.getPath());
109 assertFalse(ds0ChannelNumber.isAugmenting());
110 // type of leaf ds0ChannelNumber
111 QName typeQName = BaseTypes.constructQName("string");
112 List<QName> typePath = Collections.singletonList(typeQName);
113 expectedSchemaPath = new SchemaPath(typePath, true);
114 assertEquals(expectedSchemaPath, ds0ChannelNumber.getType().getPath());
117 qname = new QName(fooNS, fooRev, foo, "interface-id");
118 assertEquals(qname, interfaceId.getQName());
119 qnames.set(3, qname);
120 expectedSchemaPath = new SchemaPath(qnames, true);
121 assertEquals(expectedSchemaPath, interfaceId.getPath());
122 assertFalse(interfaceId.isAugmenting());
125 qname = new QName(fooNS, fooRev, foo, "schemas");
126 assertEquals(qname, schemas.getQName());
127 qnames.set(3, qname);
128 expectedSchemaPath = new SchemaPath(qnames, true);
129 assertEquals(expectedSchemaPath, schemas.getPath());
130 assertFalse(schemas.isAugmenting());
133 qname = new QName(fooNS, fooRev, foo, "odl");
134 assertEquals(qname, odl.getQName());
135 qnames.set(3, qname);
136 expectedSchemaPath = new SchemaPath(qnames, true);
137 assertEquals(expectedSchemaPath, odl.getPath());
138 assertFalse(odl.isAugmenting());
141 Module module3 = TestUtils.findModule(modules, "baz");
142 augmentations = module3.getAugmentations();
143 assertEquals(3, augmentations.size());
144 AugmentationSchema augment1 = null;
145 AugmentationSchema augment2 = null;
146 AugmentationSchema augment3 = null;
147 for (AugmentationSchema as : augmentations) {
148 if (as.getWhenCondition() == null) {
150 } else if ("if:ifType='ds0'".equals(as.getWhenCondition().toString())) {
152 } else if ("if:ifType='ds2'".equals(as.getWhenCondition().toString())) {
156 assertNotNull(augment1);
157 assertNotNull(augment2);
158 assertNotNull(augment3);
160 assertEquals(1, augment1.getChildNodes().size());
161 ContainerSchemaNode augmentHolder = (ContainerSchemaNode) augment1.getDataChildByName("augment-holder");
162 assertNotNull(augmentHolder);
164 assertEquals(1, augment2.getChildNodes().size());
165 ContainerSchemaNode augmentHolder2 = (ContainerSchemaNode) augment2.getDataChildByName("augment-holder2");
166 assertNotNull(augmentHolder2);
168 assertEquals(1, augment3.getChildNodes().size());
169 LeafSchemaNode pause = (LeafSchemaNode) augment3.getDataChildByName("pause");
170 assertNotNull(pause);
174 public void testAugmentResolving() throws Exception {
175 modules = TestUtils.loadModules(getClass().getResource("/augment-test/augment-in-augment").getPath());
176 Module module2 = TestUtils.findModule(modules, "bar");
177 ContainerSchemaNode interfaces = (ContainerSchemaNode) module2.getDataChildByName("interfaces");
178 ListSchemaNode ifEntry = (ListSchemaNode) interfaces.getDataChildByName("ifEntry");
180 SchemaPath expectedPath;
181 List<QName> qnames = new ArrayList<>();
187 // augment "/br:interfaces/br:ifEntry" {
188 ContainerSchemaNode augmentHolder = (ContainerSchemaNode) ifEntry.getDataChildByName("augment-holder");
189 TestUtils.checkIsAugmenting(augmentHolder, true);
190 assertEquals(q2, augmentHolder.getQName());
191 expectedPath = new SchemaPath(qnames, true);
192 assertEquals(expectedPath, augmentHolder.getPath());
195 // augment "/br:interfaces/br:ifEntry/bz:augment-holder"
196 LeafSchemaNode ds0ChannelNumber = (LeafSchemaNode) augmentHolder.getDataChildByName("ds0ChannelNumber");
197 LeafSchemaNode interfaceId = (LeafSchemaNode) augmentHolder.getDataChildByName("interface-id");
198 ContainerSchemaNode schemas = (ContainerSchemaNode) augmentHolder.getDataChildByName("schemas");
199 ChoiceNode odl = (ChoiceNode) augmentHolder.getDataChildByName("odl");
201 assertNotNull(ds0ChannelNumber);
202 assertNotNull(interfaceId);
203 assertNotNull(schemas);
206 // leaf ds0ChannelNumber
207 QName qname = new QName(fooNS, fooRev, foo, "ds0ChannelNumber");
208 assertEquals(qname, ds0ChannelNumber.getQName());
210 expectedPath = new SchemaPath(qnames, true);
211 assertEquals(expectedPath, ds0ChannelNumber.getPath());
214 qname = new QName(fooNS, fooRev, foo, "interface-id");
215 assertEquals(qname, interfaceId.getQName());
216 qnames.set(3, qname);
217 expectedPath = new SchemaPath(qnames, true);
218 assertEquals(expectedPath, interfaceId.getPath());
221 qname = new QName(fooNS, fooRev, foo, "schemas");
222 assertEquals(qname, schemas.getQName());
223 qnames.set(3, qname);
224 expectedPath = new SchemaPath(qnames, true);
225 assertEquals(expectedPath, schemas.getPath());
228 qname = new QName(fooNS, fooRev, foo, "odl");
229 assertEquals(qname, odl.getQName());
230 qnames.set(3, qname);
231 expectedPath = new SchemaPath(qnames, true);
232 assertEquals(expectedPath, odl.getPath());
236 public void testAugmentedChoice() throws Exception {
237 modules = TestUtils.loadModules(getClass().getResource("/augment-test/augment-in-augment").getPath());
238 Module module2 = TestUtils.findModule(modules, "bar");
239 ContainerSchemaNode interfaces = (ContainerSchemaNode) module2.getDataChildByName("interfaces");
240 ListSchemaNode ifEntry = (ListSchemaNode) interfaces.getDataChildByName("ifEntry");
241 ContainerSchemaNode augmentedHolder = (ContainerSchemaNode) ifEntry.getDataChildByName("augment-holder");
242 TestUtils.checkIsAugmenting(augmentedHolder, true);
245 // augment "/br:interfaces/br:ifEntry/bz:augment-holder"
246 ChoiceNode odl = (ChoiceNode) augmentedHolder.getDataChildByName("odl");
248 Set<ChoiceCaseNode> cases = odl.getCases();
249 assertEquals(4, cases.size());
251 ChoiceCaseNode id = null;
252 ChoiceCaseNode node1 = null;
253 ChoiceCaseNode node2 = null;
254 ChoiceCaseNode node3 = null;
256 for (ChoiceCaseNode ccn : cases) {
257 if ("id".equals(ccn.getQName().getLocalName())) {
259 } else if ("node1".equals(ccn.getQName().getLocalName())) {
261 } else if ("node2".equals(ccn.getQName().getLocalName())) {
263 } else if ("node3".equals(ccn.getQName().getLocalName())) {
269 assertNotNull(node1);
270 assertNotNull(node2);
271 assertNotNull(node3);
273 SchemaPath expectedPath;
274 List<QName> qnames = new ArrayList<>();
278 qnames.add(new QName(fooNS, fooRev, foo, "odl"));
281 QName qname = new QName(fooNS, fooRev, foo, "id");
282 assertEquals(qname, id.getQName());
284 expectedPath = new SchemaPath(qnames, true);
285 assertEquals(expectedPath, id.getPath());
286 Set<DataSchemaNode> idChildren = id.getChildNodes();
287 assertEquals(1, idChildren.size());
290 qname = new QName(fooNS, fooRev, foo, "node1");
291 assertEquals(qname, node1.getQName());
292 qnames.set(4, qname);
293 expectedPath = new SchemaPath(qnames, true);
294 assertEquals(expectedPath, node1.getPath());
295 Set<DataSchemaNode> node1Children = node1.getChildNodes();
296 assertTrue(node1Children.isEmpty());
299 qname = new QName(fooNS, fooRev, foo, "node2");
300 assertEquals(qname, node2.getQName());
301 qnames.set(4, qname);
302 expectedPath = new SchemaPath(qnames, true);
303 assertEquals(expectedPath, node2.getPath());
304 Set<DataSchemaNode> node2Children = node2.getChildNodes();
305 assertTrue(node2Children.isEmpty());
308 qname = new QName(fooNS, fooRev, foo, "node3");
309 assertEquals(qname, node3.getQName());
310 qnames.set(4, qname);
311 expectedPath = new SchemaPath(qnames, true);
312 assertEquals(expectedPath, node3.getPath());
313 Set<DataSchemaNode> node3Children = node3.getChildNodes();
314 assertEquals(1, node3Children.size());
321 qnames.add(new QName(fooNS, fooRev, foo, "odl"));
324 qnames.add(new QName(fooNS, fooRev, foo, "id"));
325 qnames.add(new QName(fooNS, fooRev, foo, "id"));
326 LeafSchemaNode caseIdChild = (LeafSchemaNode) idChildren.iterator().next();
327 assertNotNull(caseIdChild);
328 expectedPath = new SchemaPath(qnames, true);
329 assertEquals(expectedPath, caseIdChild.getPath());
332 qnames.set(4, new QName(fooNS, fooRev, foo, "node3"));
333 qnames.set(5, new QName(fooNS, fooRev, foo, "node3"));
334 ContainerSchemaNode caseNode3Child = (ContainerSchemaNode) node3Children.iterator().next();
335 assertNotNull(caseNode3Child);
336 expectedPath = new SchemaPath(qnames, true);
337 assertEquals(expectedPath, caseNode3Child.getPath());
341 public void testAugmentRpc() throws Exception {
342 modules = TestUtils.loadModules(getClass().getResource("/augment-test/rpc").getPath());
343 final URI NS_BAR = URI.create("urn:opendaylight:bar");
344 final URI NS_FOO = URI.create("urn:opendaylight:foo");
345 final Date revision = simpleDateFormat.parse("2013-10-11");
347 Module bar = TestUtils.findModule(modules, "bar");
348 Set<RpcDefinition> rpcs = bar.getRpcs();
349 assertEquals(2, rpcs.size());
351 RpcDefinition submit = null;
352 for (RpcDefinition rpc : rpcs) {
353 if ("submit".equals(rpc.getQName().getLocalName())) {
358 assertNotNull(submit);
360 QName submitQName = new QName(NS_BAR, revision, "b", "submit");
361 assertEquals(submitQName, submit.getQName());
362 ContainerSchemaNode input = submit.getInput();
363 QName inputQName = new QName(NS_BAR, revision, "b", "input");
364 assertEquals(inputQName, input.getQName());
365 ChoiceNode arguments = (ChoiceNode) input.getDataChildByName("arguments");
366 QName argumentsQName = new QName(NS_BAR, revision, "b", "arguments");
367 assertEquals(argumentsQName, arguments.getQName());
368 assertFalse(arguments.isAugmenting());
369 Set<ChoiceCaseNode> cases = arguments.getCases();
370 assertEquals(3, cases.size());
372 ChoiceCaseNode attach = null;
373 ChoiceCaseNode create = null;
374 ChoiceCaseNode destroy = null;
375 for (ChoiceCaseNode child : cases) {
376 if ("attach".equals(child.getQName().getLocalName())) {
378 } else if ("create".equals(child.getQName().getLocalName())) {
380 } else if ("destroy".equals(child.getQName().getLocalName())) {
384 assertNotNull(attach);
385 assertNotNull(create);
386 assertNotNull(destroy);
388 assertTrue(attach.isAugmenting());
389 assertTrue(create.isAugmenting());
390 assertTrue(destroy.isAugmenting());
392 SchemaPath expectedPath;
393 QName[] qnames = new QName[4];
394 qnames[0] = submitQName;
395 qnames[1] = inputQName;
396 qnames[2] = argumentsQName;
399 qnames[3] = new QName(NS_FOO, revision, "f", "attach");
400 assertEquals(qnames[3], attach.getQName());
401 expectedPath = new SchemaPath(Arrays.asList(qnames), true);
402 assertEquals(expectedPath, attach.getPath());
403 Set<DataSchemaNode> attachChildren = attach.getChildNodes();
404 assertEquals(1, attachChildren.size());
407 qnames[3] = new QName(NS_FOO, revision, "f", "create");
408 assertEquals(qnames[3], create.getQName());
409 expectedPath = new SchemaPath(Arrays.asList(qnames), true);
410 assertEquals(expectedPath, create.getPath());
411 Set<DataSchemaNode> createChildren = create.getChildNodes();
412 assertEquals(1, createChildren.size());
415 qnames[3] = new QName(NS_FOO, revision, "f", "destroy");
416 assertEquals(qnames[3], destroy.getQName());
417 expectedPath = new SchemaPath(Arrays.asList(qnames), true);
418 assertEquals(expectedPath, destroy.getPath());
419 Set<DataSchemaNode> destroyChildren = destroy.getChildNodes();
420 assertEquals(1, destroyChildren.size());