97454af9d20e4b46a21dc52c841c0d7b4d8e7f23
[yangtools.git] / yang / yang-parser-impl / src / test / java / org / opendaylight / yangtools / yang / parser / impl / AugmentTest.java
1 /*
2  * Copyright (c) 2013 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.parser.impl;
9
10 import static org.junit.Assert.*;
11
12 import java.io.FileNotFoundException;
13 import java.net.URI;
14 import java.text.DateFormat;
15 import java.text.ParseException;
16 import java.text.SimpleDateFormat;
17 import java.util.Collections;
18 import java.util.Date;
19 import java.util.List;
20 import java.util.Set;
21
22 import org.junit.Before;
23 import org.junit.Test;
24 import org.opendaylight.yangtools.yang.common.QName;
25 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
26 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
27 import org.opendaylight.yangtools.yang.model.api.ChoiceNode;
28 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
29 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
30 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
31 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
32 import org.opendaylight.yangtools.yang.model.api.Module;
33 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
34 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
35 import org.opendaylight.yangtools.yang.model.util.BaseTypes;
36
37 import com.google.common.collect.Lists;
38
39 public class AugmentTest {
40
41     private final URI types1NS = URI.create("urn:simple.nodes.test");
42     private final URI types2NS = URI.create("urn:simple.types.test");
43     private final URI types3NS = URI.create("urn:custom.nodes.test");
44     private Date types1Rev;
45     private Date types2Rev;
46     private Date types3Rev;
47     private final String t1 = "n";
48     private final String t2 = "t";
49     private final String t3 = "c";
50     private QName q0;
51     private QName q1;
52     private QName q2;
53
54     private final DateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
55     private Set<Module> modules;
56
57     @Before
58     public void init() throws FileNotFoundException, ParseException {
59         types1Rev = simpleDateFormat.parse("2013-02-27");
60         types2Rev = simpleDateFormat.parse("2013-07-03");
61         types3Rev = simpleDateFormat.parse("2013-02-27");
62
63         q0 = new QName(types2NS, types2Rev, t2, "interfaces");
64         q1 = new QName(types2NS, types2Rev, t2, "ifEntry");
65         q2 = new QName(types3NS, types3Rev, t3, "augment-holder");
66
67         modules = TestUtils.loadModules(getClass().getResource("/model").getPath());
68         assertEquals(3, modules.size());
69     }
70
71     @Test
72     public void testAugmentParsing() {
73         SchemaPath expectedPath = null;
74         QName[] qnames = null;
75
76         // testfile1
77         Module module1 = TestUtils.findModule(modules, "nodes");
78         Set<AugmentationSchema> augmentations = module1.getAugmentations();
79         assertEquals(1, augmentations.size());
80         AugmentationSchema augment = augmentations.iterator().next();
81
82         Set<DataSchemaNode> augmentChildren = augment.getChildNodes();
83         assertEquals(5, augmentChildren.size());
84         for (DataSchemaNode dsn : augmentChildren) {
85             TestUtils.checkIsAugmenting(dsn, false);
86         }
87
88         LeafSchemaNode ds0ChannelNumber = (LeafSchemaNode) augment.getDataChildByName("ds0ChannelNumber");
89         LeafSchemaNode interfaceId = (LeafSchemaNode) augment.getDataChildByName("interface-id");
90         LeafSchemaNode myType = (LeafSchemaNode) augment.getDataChildByName("my-type");
91         ContainerSchemaNode schemas = (ContainerSchemaNode) augment.getDataChildByName("schemas");
92         ChoiceNode odl = (ChoiceNode) augment.getDataChildByName("odl");
93
94         assertNotNull(ds0ChannelNumber);
95         assertNotNull(interfaceId);
96         assertNotNull(myType);
97         assertNotNull(schemas);
98         assertNotNull(odl);
99
100         qnames = new QName[4];
101         qnames[0] = q0;
102         qnames[1] = q1;
103         qnames[2] = q2;
104
105         // leaf ds0ChannelNumber
106         qnames[3] = new QName(types1NS, types1Rev, t1, "ds0ChannelNumber");
107         expectedPath = new SchemaPath(Lists.newArrayList(qnames), true);
108         assertEquals(expectedPath, ds0ChannelNumber.getPath());
109         // type of leaf ds0ChannelNumber
110         QName typeQName = BaseTypes.constructQName("string");
111         List<QName> typePath = Collections.singletonList(typeQName);
112         expectedPath = new SchemaPath(typePath, true);
113         assertEquals(expectedPath, ds0ChannelNumber.getType().getPath());
114
115         // leaf interface-id
116         qnames[3] = new QName(types1NS, types1Rev, t1, "interface-id");
117         expectedPath = new SchemaPath(Lists.newArrayList(qnames), true);
118         assertEquals(expectedPath, interfaceId.getPath());
119
120         // leaf my-type
121         qnames[3] = new QName(types1NS, types1Rev, t1, "my-type");
122         expectedPath = new SchemaPath(Lists.newArrayList(qnames), true);
123         assertEquals(expectedPath, myType.getPath());
124
125         // container schemas
126         qnames[3] = new QName(types1NS, types1Rev, t1, "schemas");
127         expectedPath = new SchemaPath(Lists.newArrayList(qnames), true);
128         assertEquals(expectedPath, schemas.getPath());
129
130         // choice odl
131         qnames[3] = new QName(types1NS, types1Rev, t1, "odl");
132         expectedPath = new SchemaPath(Lists.newArrayList(qnames), true);
133         assertEquals(expectedPath, odl.getPath());
134
135         // testfile3
136         Module module3 = TestUtils.findModule(modules, "custom");
137         augmentations = module3.getAugmentations();
138         assertEquals(2, augmentations.size());
139         AugmentationSchema augment1 = null;
140         AugmentationSchema augment2 = null;
141         for (AugmentationSchema as : augmentations) {
142             if ("if:ifType='ds0'".equals(as.getWhenCondition().toString())) {
143                 augment1 = as;
144             } else if ("if:ifType='ds2'".equals(as.getWhenCondition().toString())) {
145                 augment2 = as;
146             }
147         }
148         assertNotNull(augment1);
149         assertNotNull(augment2);
150
151         assertEquals(1, augment1.getChildNodes().size());
152         ContainerSchemaNode augmentHolder = (ContainerSchemaNode) augment1.getDataChildByName("augment-holder");
153         assertNotNull(augmentHolder);
154
155         assertEquals(1, augment2.getChildNodes().size());
156         ContainerSchemaNode augmentHolder2 = (ContainerSchemaNode) augment2.getDataChildByName("augment-holder2");
157         assertNotNull(augmentHolder2);
158     }
159
160     @Test
161     public void testAugmentResolving() throws ParseException {
162         SchemaPath expectedPath = null;
163         QName[] qnames = null;
164
165         Module module2 = TestUtils.findModule(modules, "types");
166         ContainerSchemaNode interfaces = (ContainerSchemaNode) module2.getDataChildByName("interfaces");
167         ListSchemaNode ifEntry = (ListSchemaNode) interfaces.getDataChildByName("ifEntry");
168         ContainerSchemaNode augmentedContainer = (ContainerSchemaNode) ifEntry.getDataChildByName("augment-holder");
169         TestUtils.checkIsAugmenting(augmentedContainer, true);
170
171         // testfile1.yang
172         // augment "/data:interfaces/data:ifEntry/t3:augment-holder"
173         LeafSchemaNode ds0ChannelNumber = (LeafSchemaNode) augmentedContainer.getDataChildByName("ds0ChannelNumber");
174         LeafSchemaNode interfaceId = (LeafSchemaNode) augmentedContainer.getDataChildByName("interface-id");
175         LeafSchemaNode myType = (LeafSchemaNode) augmentedContainer.getDataChildByName("my-type");
176         ContainerSchemaNode schemas = (ContainerSchemaNode) augmentedContainer.getDataChildByName("schemas");
177         ChoiceNode odl = (ChoiceNode) augmentedContainer.getDataChildByName("odl");
178
179         assertNotNull(ds0ChannelNumber);
180         assertNotNull(interfaceId);
181         assertNotNull(myType);
182         assertNotNull(schemas);
183         assertNotNull(odl);
184
185         qnames = new QName[4];
186         qnames[0] = q0;
187         qnames[1] = q1;
188         qnames[2] = q2;
189
190         // leaf ds0ChannelNumber
191         qnames[3] = new QName(types1NS, types1Rev, t1, "ds0ChannelNumber");
192         expectedPath = new SchemaPath(Lists.newArrayList(qnames), true);
193         assertEquals(expectedPath, ds0ChannelNumber.getPath());
194
195         // leaf interface-id
196         qnames[3] = new QName(types1NS, types1Rev, t1, "interface-id");
197         expectedPath = new SchemaPath(Lists.newArrayList(qnames), true);
198         assertEquals(expectedPath, interfaceId.getPath());
199
200         // leaf my-type
201         qnames[3] = new QName(types1NS, types1Rev, t1, "my-type");
202         expectedPath = new SchemaPath(Lists.newArrayList(qnames), true);
203         assertEquals(expectedPath, myType.getPath());
204
205         // container schemas
206         qnames[3] = new QName(types1NS, types1Rev, t1, "schemas");
207         expectedPath = new SchemaPath(Lists.newArrayList(qnames), true);
208         assertEquals(expectedPath, schemas.getPath());
209
210         // choice odl
211         qnames[3] = new QName(types1NS, types1Rev, t1, "odl");
212         expectedPath = new SchemaPath(Lists.newArrayList(qnames), true);
213         assertEquals(expectedPath, odl.getPath());
214     }
215
216     @Test
217     public void testAugmentChoice() throws ParseException {
218         SchemaPath expectedPath = null;
219         QName[] qnames = null;
220
221         Module module2 = TestUtils.findModule(modules, "types");
222         ContainerSchemaNode interfaces = (ContainerSchemaNode) module2.getDataChildByName("interfaces");
223         ListSchemaNode ifEntry = (ListSchemaNode) interfaces.getDataChildByName("ifEntry");
224         ContainerSchemaNode augmentedContainer = (ContainerSchemaNode) ifEntry.getDataChildByName("augment-holder");
225         TestUtils.checkIsAugmenting(augmentedContainer, true);
226
227         // testfile1.yang
228         // augment "/data:interfaces/data:ifEntry/t3:augment-holder"
229         ChoiceNode odl = (ChoiceNode) augmentedContainer.getDataChildByName("odl");
230         assertNotNull(odl);
231         Set<ChoiceCaseNode> cases = odl.getCases();
232         assertEquals(4, cases.size());
233
234         ChoiceCaseNode id = null;
235         ChoiceCaseNode node1 = null;
236         ChoiceCaseNode node2 = null;
237         ChoiceCaseNode node3 = null;
238
239         for (ChoiceCaseNode ccn : cases) {
240             if ("id".equals(ccn.getQName().getLocalName())) {
241                 id = ccn;
242             } else if ("node1".equals(ccn.getQName().getLocalName())) {
243                 node1 = ccn;
244             } else if ("node2".equals(ccn.getQName().getLocalName())) {
245                 node2 = ccn;
246             } else if ("node3".equals(ccn.getQName().getLocalName())) {
247                 node3 = ccn;
248             }
249         }
250
251         assertNotNull(id);
252         assertNotNull(node1);
253         assertNotNull(node2);
254         assertNotNull(node3);
255
256         qnames = new QName[5];
257         qnames[0] = q0;
258         qnames[1] = q1;
259         qnames[2] = q2;
260         qnames[3] = new QName(types1NS, types1Rev, t1, "odl");
261
262         // case id
263         qnames[4] = new QName(types1NS, types1Rev, t1, "id");
264         expectedPath = new SchemaPath(Lists.newArrayList(qnames), true);
265         assertEquals(expectedPath, id.getPath());
266         Set<DataSchemaNode> idChildren = id.getChildNodes();
267         assertEquals(1, idChildren.size());
268
269         // case node1
270         qnames[4] = new QName(types1NS, types1Rev, t1, "node1");
271         expectedPath = new SchemaPath(Lists.newArrayList(qnames), true);
272         assertEquals(expectedPath, node1.getPath());
273         Set<DataSchemaNode> node1Children = node1.getChildNodes();
274         assertTrue(node1Children.isEmpty());
275
276         // case node2
277         qnames[4] = new QName(types1NS, types1Rev, t1, "node2");
278         expectedPath = new SchemaPath(Lists.newArrayList(qnames), true);
279         assertEquals(expectedPath, node2.getPath());
280         Set<DataSchemaNode> node2Children = node2.getChildNodes();
281         assertTrue(node2Children.isEmpty());
282
283         // case node3
284         qnames[4] = new QName(types1NS, types1Rev, t1, "node3");
285         expectedPath = new SchemaPath(Lists.newArrayList(qnames), true);
286         assertEquals(expectedPath, node3.getPath());
287         Set<DataSchemaNode> node3Children = node3.getChildNodes();
288         assertEquals(1, node3Children.size());
289
290         // test cases
291         qnames = new QName[6];
292         qnames[0] = q0;
293         qnames[1] = q1;
294         qnames[2] = q2;
295         qnames[3] = new QName(types1NS, types1Rev, t1, "odl");
296
297         // case id child
298         qnames[4] = new QName(types1NS, types1Rev, t1, "id");
299         qnames[5] = new QName(types1NS, types1Rev, t1, "id");
300         LeafSchemaNode caseIdChild = (LeafSchemaNode) idChildren.iterator().next();
301         assertNotNull(caseIdChild);
302         expectedPath = new SchemaPath(Lists.newArrayList(qnames), true);
303         assertEquals(expectedPath, caseIdChild.getPath());
304
305         // case node3 child
306         qnames[4] = new QName(types1NS, types1Rev, t1, "node3");
307         qnames[5] = new QName(types1NS, types1Rev, t1, "node3");
308         ContainerSchemaNode caseNode3Child = (ContainerSchemaNode) node3Children.iterator().next();
309         assertNotNull(caseNode3Child);
310         expectedPath = new SchemaPath(Lists.newArrayList(qnames), true);
311         assertEquals(expectedPath, caseNode3Child.getPath());
312     }
313
314     @Test
315     public void testAugmentRpc() throws Exception {
316         modules = TestUtils.loadModules(getClass().getResource("/augment-test/rpc").getPath());
317         final URI NS_BAR = URI.create("urn:opendaylight:bar");
318         final URI NS_FOO = URI.create("urn:opendaylight:foo");
319         final Date revision = simpleDateFormat.parse("2013-10-11");
320
321         Module bar = TestUtils.findModule(modules, "bar");
322         Set<RpcDefinition> rpcs = bar.getRpcs();
323         assertEquals(2, rpcs.size());
324
325         RpcDefinition submit = null;
326         for (RpcDefinition rpc : rpcs) {
327             if ("submit".equals(rpc.getQName().getLocalName())) {
328                 submit = rpc;
329                 break;
330             }
331         }
332         assertNotNull(submit);
333
334         QName submitQName = new QName(NS_BAR, revision, "b", "submit");
335         assertEquals(submitQName, submit.getQName());
336         ContainerSchemaNode input = submit.getInput();
337         QName inputQName = new QName(NS_BAR, revision, "b", "input");
338         assertEquals(inputQName, input.getQName());
339         ChoiceNode arguments = (ChoiceNode) input.getDataChildByName("arguments");
340         QName argumentsQName = new QName(NS_BAR, revision, "b", "arguments");
341         assertEquals(argumentsQName, arguments.getQName());
342         assertFalse(arguments.isAugmenting());
343         Set<ChoiceCaseNode> cases = arguments.getCases();
344         assertEquals(3, cases.size());
345
346         ChoiceCaseNode attach = null;
347         ChoiceCaseNode create = null;
348         ChoiceCaseNode destroy = null;
349         for (ChoiceCaseNode child : cases) {
350             if ("attach".equals(child.getQName().getLocalName())) {
351                 attach = child;
352             } else if ("create".equals(child.getQName().getLocalName())) {
353                 create = child;
354             } else if ("destroy".equals(child.getQName().getLocalName())) {
355                 destroy = child;
356             }
357         }
358         assertNotNull(attach);
359         assertNotNull(create);
360         assertNotNull(destroy);
361
362         assertTrue(attach.isAugmenting());
363         assertTrue(create.isAugmenting());
364         assertTrue(destroy.isAugmenting());
365
366         SchemaPath expectedPath = null;
367         QName[] qnames = new QName[4];
368         qnames[0] = submitQName;
369         qnames[1] = inputQName;
370         qnames[2] = argumentsQName;
371
372         // case attach
373         qnames[3] = new QName(NS_FOO, revision, "f", "attach");
374         assertEquals(qnames[3], attach.getQName());
375         expectedPath = new SchemaPath(Lists.newArrayList(qnames), true);
376         assertEquals(expectedPath, attach.getPath());
377         Set<DataSchemaNode> attachChildren = attach.getChildNodes();
378         assertEquals(1, attachChildren.size());
379
380         // case create
381         qnames[3] = new QName(NS_FOO, revision, "f", "create");
382         assertEquals(qnames[3], create.getQName());
383         expectedPath = new SchemaPath(Lists.newArrayList(qnames), true);
384         assertEquals(expectedPath, create.getPath());
385         Set<DataSchemaNode> createChildren = create.getChildNodes();
386         assertEquals(1, createChildren.size());
387
388         // case attach
389         qnames[3] = new QName(NS_FOO, revision, "f", "destroy");
390         assertEquals(qnames[3], destroy.getQName());
391         expectedPath = new SchemaPath(Lists.newArrayList(qnames), true);
392         assertEquals(expectedPath, destroy.getPath());
393         Set<DataSchemaNode> destroyChildren = destroy.getChildNodes();
394         assertEquals(1, destroyChildren.size());
395
396     }
397
398 }