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.assertEquals;
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertTrue;
14 import java.io.FileNotFoundException;
16 import java.text.DateFormat;
17 import java.text.ParseException;
18 import java.text.SimpleDateFormat;
19 import java.util.ArrayList;
20 import java.util.Arrays;
21 import java.util.Collection;
22 import java.util.Collections;
23 import java.util.Date;
24 import java.util.List;
26 import org.junit.BeforeClass;
27 import org.junit.Test;
28 import org.opendaylight.yangtools.yang.common.QName;
29 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
30 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
31 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
32 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
33 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
34 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
35 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
36 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
37 import org.opendaylight.yangtools.yang.model.api.Module;
38 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
39 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
40 import org.opendaylight.yangtools.yang.model.util.BaseTypes;
42 public class AugmentTest {
43 private static final URI fooNS = URI.create("urn:opendaylight.foo");
44 private static final URI barNS = URI.create("urn:opendaylight.bar");
45 private static final URI bazNS = URI.create("urn:opendaylight.baz");
46 private static Date fooRev;
47 private static Date barRev;
48 private static Date bazRev;
49 private static QName q0;
50 private static QName q1;
51 private static QName q2;
53 private Set<Module> modules;
56 public static void init() throws FileNotFoundException, ParseException {
57 DateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
58 fooRev = simpleDateFormat.parse("2013-10-13");
59 barRev = simpleDateFormat.parse("2013-10-14");
60 bazRev = simpleDateFormat.parse("2013-10-15");
62 q0 = QName.create(barNS, barRev, "interfaces");
63 q1 = QName.create(barNS, barRev, "ifEntry");
64 q2 = QName.create(bazNS, bazRev, "augment-holder");
68 public void testAugmentParsing() throws Exception {
69 modules = TestUtils.loadModules(getClass().getResource("/augment-test/augment-in-augment").toURI());
70 SchemaPath expectedSchemaPath;
71 List<QName> qnames = new ArrayList<>();
77 Module module1 = TestUtils.findModule(modules, "foo");
78 Set<AugmentationSchema> augmentations = module1.getAugmentations();
79 assertEquals(1, augmentations.size());
80 AugmentationSchema augment = augmentations.iterator().next();
81 assertNotNull(augment);
83 expectedSchemaPath = SchemaPath.create(qnames, true);
84 assertEquals(expectedSchemaPath, augment.getTargetPath());
86 Collection<DataSchemaNode> augmentChildren = augment.getChildNodes();
87 assertEquals(4, augmentChildren.size());
88 for (DataSchemaNode dsn : augmentChildren) {
89 TestUtils.checkIsAugmenting(dsn, false);
92 LeafSchemaNode ds0ChannelNumber = (LeafSchemaNode) augment.getDataChildByName("ds0ChannelNumber");
93 LeafSchemaNode interfaceId = (LeafSchemaNode) augment.getDataChildByName("interface-id");
94 ContainerSchemaNode schemas = (ContainerSchemaNode) augment.getDataChildByName("schemas");
95 ChoiceSchemaNode odl = (ChoiceSchemaNode) augment.getDataChildByName("odl");
97 assertNotNull(ds0ChannelNumber);
98 assertNotNull(interfaceId);
99 assertNotNull(schemas);
102 // leaf ds0ChannelNumber
103 QName qname = QName.create(fooNS, fooRev, "ds0ChannelNumber");
105 assertEquals(qname, ds0ChannelNumber.getQName());
106 expectedSchemaPath = SchemaPath.create(qnames, true);
107 assertEquals(expectedSchemaPath, ds0ChannelNumber.getPath());
108 assertFalse(ds0ChannelNumber.isAugmenting());
109 // type of leaf ds0ChannelNumber
110 QName typeQName = BaseTypes.constructQName("string");
111 List<QName> typePath = Collections.singletonList(typeQName);
112 expectedSchemaPath = SchemaPath.create(typePath, true);
113 assertEquals(expectedSchemaPath, ds0ChannelNumber.getType().getPath());
116 qname = QName.create(fooNS, fooRev, "interface-id");
117 assertEquals(qname, interfaceId.getQName());
118 qnames.set(3, qname);
119 expectedSchemaPath = SchemaPath.create(qnames, true);
120 assertEquals(expectedSchemaPath, interfaceId.getPath());
121 assertFalse(interfaceId.isAugmenting());
124 qname = QName.create(fooNS, fooRev, "schemas");
125 assertEquals(qname, schemas.getQName());
126 qnames.set(3, qname);
127 expectedSchemaPath = SchemaPath.create(qnames, true);
128 assertEquals(expectedSchemaPath, schemas.getPath());
129 assertFalse(schemas.isAugmenting());
132 qname = QName.create(fooNS, fooRev, "odl");
133 assertEquals(qname, odl.getQName());
134 qnames.set(3, qname);
135 expectedSchemaPath = SchemaPath.create(qnames, true);
136 assertEquals(expectedSchemaPath, odl.getPath());
137 assertFalse(odl.isAugmenting());
140 Module module3 = TestUtils.findModule(modules, "baz");
141 augmentations = module3.getAugmentations();
142 assertEquals(3, augmentations.size());
143 AugmentationSchema augment1 = null;
144 AugmentationSchema augment2 = null;
145 AugmentationSchema augment3 = null;
146 for (AugmentationSchema as : augmentations) {
147 if (as.getWhenCondition() == null) {
149 } else if ("if:ifType='ds0'".equals(as.getWhenCondition().toString())) {
151 } else if ("if:ifType='ds2'".equals(as.getWhenCondition().toString())) {
155 assertNotNull(augment1);
156 assertNotNull(augment2);
157 assertNotNull(augment3);
159 assertEquals(1, augment1.getChildNodes().size());
160 ContainerSchemaNode augmentHolder = (ContainerSchemaNode) augment1.getDataChildByName("augment-holder");
161 assertNotNull(augmentHolder);
163 assertEquals(1, augment2.getChildNodes().size());
164 ContainerSchemaNode augmentHolder2 = (ContainerSchemaNode) augment2.getDataChildByName("augment-holder2");
165 assertNotNull(augmentHolder2);
167 assertEquals(1, augment3.getChildNodes().size());
168 LeafSchemaNode pause = (LeafSchemaNode) augment3.getDataChildByName("pause");
169 assertNotNull(pause);
173 public void testAugmentResolving() throws Exception {
174 modules = TestUtils.loadModules(getClass().getResource("/augment-test/augment-in-augment").toURI());
175 Module module2 = TestUtils.findModule(modules, "bar");
176 ContainerSchemaNode interfaces = (ContainerSchemaNode) module2.getDataChildByName("interfaces");
177 ListSchemaNode ifEntry = (ListSchemaNode) interfaces.getDataChildByName("ifEntry");
179 SchemaPath expectedPath;
180 List<QName> qnames = new ArrayList<>();
186 // augment "/br:interfaces/br:ifEntry" {
187 ContainerSchemaNode augmentHolder = (ContainerSchemaNode) ifEntry.getDataChildByName("augment-holder");
188 TestUtils.checkIsAugmenting(augmentHolder, true);
189 assertEquals(q2, augmentHolder.getQName());
190 expectedPath = SchemaPath.create(qnames, true);
191 assertEquals(expectedPath, augmentHolder.getPath());
194 // augment "/br:interfaces/br:ifEntry/bz:augment-holder"
195 LeafSchemaNode ds0ChannelNumber = (LeafSchemaNode) augmentHolder.getDataChildByName("ds0ChannelNumber");
196 LeafSchemaNode interfaceId = (LeafSchemaNode) augmentHolder.getDataChildByName("interface-id");
197 ContainerSchemaNode schemas = (ContainerSchemaNode) augmentHolder.getDataChildByName("schemas");
198 ChoiceSchemaNode odl = (ChoiceSchemaNode) augmentHolder.getDataChildByName("odl");
200 assertNotNull(ds0ChannelNumber);
201 assertNotNull(interfaceId);
202 assertNotNull(schemas);
205 // leaf ds0ChannelNumber
206 QName qname = QName.create(fooNS, fooRev, "ds0ChannelNumber");
207 assertEquals(qname, ds0ChannelNumber.getQName());
209 expectedPath = SchemaPath.create(qnames, true);
210 assertEquals(expectedPath, ds0ChannelNumber.getPath());
213 qname = QName.create(fooNS, fooRev, "interface-id");
214 assertEquals(qname, interfaceId.getQName());
215 qnames.set(3, qname);
216 expectedPath = SchemaPath.create(qnames, true);
217 assertEquals(expectedPath, interfaceId.getPath());
220 qname = QName.create(fooNS, fooRev, "schemas");
221 assertEquals(qname, schemas.getQName());
222 qnames.set(3, qname);
223 expectedPath = SchemaPath.create(qnames, true);
224 assertEquals(expectedPath, schemas.getPath());
227 qname = QName.create(fooNS, fooRev, "odl");
228 assertEquals(qname, odl.getQName());
229 qnames.set(3, qname);
230 expectedPath = SchemaPath.create(qnames, true);
231 assertEquals(expectedPath, odl.getPath());
235 public void testAugmentedChoice() throws Exception {
236 modules = TestUtils.loadModules(getClass().getResource("/augment-test/augment-in-augment").toURI());
237 Module module2 = TestUtils.findModule(modules, "bar");
238 ContainerSchemaNode interfaces = (ContainerSchemaNode) module2.getDataChildByName("interfaces");
239 ListSchemaNode ifEntry = (ListSchemaNode) interfaces.getDataChildByName("ifEntry");
240 ContainerSchemaNode augmentedHolder = (ContainerSchemaNode) ifEntry.getDataChildByName("augment-holder");
241 TestUtils.checkIsAugmenting(augmentedHolder, true);
244 // augment "/br:interfaces/br:ifEntry/bz:augment-holder"
245 ChoiceSchemaNode odl = (ChoiceSchemaNode) augmentedHolder.getDataChildByName("odl");
247 Set<ChoiceCaseNode> cases = odl.getCases();
248 assertEquals(4, cases.size());
250 ChoiceCaseNode id = null;
251 ChoiceCaseNode node1 = null;
252 ChoiceCaseNode node2 = null;
253 ChoiceCaseNode node3 = null;
255 for (ChoiceCaseNode ccn : cases) {
256 if ("id".equals(ccn.getQName().getLocalName())) {
258 } else if ("node1".equals(ccn.getQName().getLocalName())) {
260 } else if ("node2".equals(ccn.getQName().getLocalName())) {
262 } else if ("node3".equals(ccn.getQName().getLocalName())) {
268 assertNotNull(node1);
269 assertNotNull(node2);
270 assertNotNull(node3);
272 SchemaPath expectedPath;
273 List<QName> qnames = new ArrayList<>();
277 qnames.add(QName.create(fooNS, fooRev, "odl"));
280 QName qname = QName.create(fooNS, fooRev, "id");
281 assertEquals(qname, id.getQName());
283 expectedPath = SchemaPath.create(qnames, true);
284 assertEquals(expectedPath, id.getPath());
285 Collection<DataSchemaNode> idChildren = id.getChildNodes();
286 assertEquals(1, idChildren.size());
289 qname = QName.create(fooNS, fooRev, "node1");
290 assertEquals(qname, node1.getQName());
291 qnames.set(4, qname);
292 expectedPath = SchemaPath.create(qnames, true);
293 assertEquals(expectedPath, node1.getPath());
294 Collection<DataSchemaNode> node1Children = node1.getChildNodes();
295 assertTrue(node1Children.isEmpty());
298 qname = QName.create(fooNS, fooRev, "node2");
299 assertEquals(qname, node2.getQName());
300 qnames.set(4, qname);
301 expectedPath = SchemaPath.create(qnames, true);
302 assertEquals(expectedPath, node2.getPath());
303 Collection<DataSchemaNode> node2Children = node2.getChildNodes();
304 assertTrue(node2Children.isEmpty());
307 qname = QName.create(fooNS, fooRev, "node3");
308 assertEquals(qname, node3.getQName());
309 qnames.set(4, qname);
310 expectedPath = SchemaPath.create(qnames, true);
311 assertEquals(expectedPath, node3.getPath());
312 Collection<DataSchemaNode> node3Children = node3.getChildNodes();
313 assertEquals(1, node3Children.size());
320 qnames.add(QName.create(fooNS, fooRev, "odl"));
323 qnames.add(QName.create(fooNS, fooRev, "id"));
324 qnames.add(QName.create(fooNS, fooRev, "id"));
325 LeafSchemaNode caseIdChild = (LeafSchemaNode) idChildren.iterator().next();
326 assertNotNull(caseIdChild);
327 expectedPath = SchemaPath.create(qnames, true);
328 assertEquals(expectedPath, caseIdChild.getPath());
331 qnames.set(4, QName.create(fooNS, fooRev, "node3"));
332 qnames.set(5, QName.create(fooNS, fooRev, "node3"));
333 ContainerSchemaNode caseNode3Child = (ContainerSchemaNode) node3Children.iterator().next();
334 assertNotNull(caseNode3Child);
335 expectedPath = SchemaPath.create(qnames, true);
336 assertEquals(expectedPath, caseNode3Child.getPath());
340 public void testAugmentRpc() throws Exception {
341 modules = TestUtils.loadModules(getClass().getResource("/augment-test/rpc").toURI());
342 final URI NS_BAR = URI.create("urn:opendaylight:bar");
343 final URI NS_FOO = URI.create("urn:opendaylight:foo");
344 final Date revision = new SimpleDateFormat("yyyy-MM-dd").parse("2013-10-11");
346 Module bar = TestUtils.findModule(modules, "bar");
347 Set<RpcDefinition> rpcs = bar.getRpcs();
348 assertEquals(2, rpcs.size());
350 RpcDefinition submit = null;
351 for (RpcDefinition rpc : rpcs) {
352 if ("submit".equals(rpc.getQName().getLocalName())) {
357 assertNotNull(submit);
359 QName submitQName = QName.create(NS_BAR, revision, "submit");
360 assertEquals(submitQName, submit.getQName());
361 ContainerSchemaNode input = submit.getInput();
362 QName inputQName = QName.create(NS_BAR, revision, "input");
363 assertEquals(inputQName, input.getQName());
364 ChoiceSchemaNode arguments = (ChoiceSchemaNode) input.getDataChildByName("arguments");
365 QName argumentsQName = QName.create(NS_BAR, revision, "arguments");
366 assertEquals(argumentsQName, arguments.getQName());
367 assertFalse(arguments.isAugmenting());
368 Set<ChoiceCaseNode> cases = arguments.getCases();
369 assertEquals(3, cases.size());
371 ChoiceCaseNode attach = null;
372 ChoiceCaseNode create = null;
373 ChoiceCaseNode destroy = null;
374 for (ChoiceCaseNode child : cases) {
375 if ("attach".equals(child.getQName().getLocalName())) {
377 } else if ("create".equals(child.getQName().getLocalName())) {
379 } else if ("destroy".equals(child.getQName().getLocalName())) {
383 assertNotNull(attach);
384 assertNotNull(create);
385 assertNotNull(destroy);
387 assertTrue(attach.isAugmenting());
388 assertTrue(create.isAugmenting());
389 assertTrue(destroy.isAugmenting());
391 SchemaPath expectedPath;
392 QName[] qnames = new QName[4];
393 qnames[0] = submitQName;
394 qnames[1] = inputQName;
395 qnames[2] = argumentsQName;
398 qnames[3] = QName.create(NS_FOO, revision, "attach");
399 assertEquals(qnames[3], attach.getQName());
400 expectedPath = SchemaPath.create(Arrays.asList(qnames), true);
401 assertEquals(expectedPath, attach.getPath());
402 Collection<DataSchemaNode> attachChildren = attach.getChildNodes();
403 assertEquals(1, attachChildren.size());
406 qnames[3] = QName.create(NS_FOO, revision, "create");
407 assertEquals(qnames[3], create.getQName());
408 expectedPath = SchemaPath.create(Arrays.asList(qnames), true);
409 assertEquals(expectedPath, create.getPath());
410 Collection<DataSchemaNode> createChildren = create.getChildNodes();
411 assertEquals(1, createChildren.size());
414 qnames[3] = QName.create(NS_FOO, revision, "destroy");
415 assertEquals(qnames[3], destroy.getQName());
416 expectedPath = SchemaPath.create(Arrays.asList(qnames), true);
417 assertEquals(expectedPath, destroy.getPath());
418 Collection<DataSchemaNode> destroyChildren = destroy.getChildNodes();
419 assertEquals(1, destroyChildren.size());
423 public void testAugmentInUsesResolving() throws Exception {
424 modules = TestUtils.loadModules(getClass().getResource("/augment-test/augment-in-uses").toURI());
425 assertEquals(1, modules.size());
427 Module test = modules.iterator().next();
428 DataNodeContainer links = (DataNodeContainer) test.getDataChildByName("links");
429 DataNodeContainer link = (DataNodeContainer) links.getDataChildByName("link");
430 DataNodeContainer nodes = (DataNodeContainer) link.getDataChildByName("nodes");
431 ContainerSchemaNode node = (ContainerSchemaNode) nodes.getDataChildByName("node");
432 Set<AugmentationSchema> augments = node.getAvailableAugmentations();
433 assertEquals(1, augments.size());
434 assertEquals(1, node.getChildNodes().size());
435 LeafSchemaNode id = (LeafSchemaNode) node.getDataChildByName("id");
436 assertTrue(id.isAugmenting());
440 public void caseShortHandAugmentingTest() throws Exception {
441 modules = TestUtils.loadModules(getClass().getResource("/choice-case-type-test-models").toURI());
443 Module findModule = TestUtils.findModule(modules, "choice-monitoring");
445 ContainerSchemaNode netconf = (ContainerSchemaNode) findModule.getDataChildByName("netconf-state");
446 ContainerSchemaNode datastores = (ContainerSchemaNode) netconf.getDataChildByName("datastores");
447 ListSchemaNode datastore = (ListSchemaNode) datastores.getDataChildByName("datastore");
448 ContainerSchemaNode locks = (ContainerSchemaNode) datastore.getDataChildByName("locks");
449 ChoiceSchemaNode lockType = (ChoiceSchemaNode)locks.getDataChildByName("lock-type");
451 ChoiceCaseNode leafAugCase = lockType.getCaseNodeByName("leaf-aug-case");
452 assertTrue(leafAugCase.isAugmenting());
453 DataSchemaNode leafAug = leafAugCase.getDataChildByName("leaf-aug-case");
454 assertFalse(leafAug.isAugmenting());