Fix minor bug in FRM proactive flow code path
[controller.git] / opendaylight / sal / yang-prototype / yang / yang-model-parser-impl / src / test / java / org / opendaylight / controller / 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.controller.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.Arrays;
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.controller.yang.common.QName;
25 import org.opendaylight.controller.yang.model.api.AugmentationSchema;
26 import org.opendaylight.controller.yang.model.api.ChoiceCaseNode;
27 import org.opendaylight.controller.yang.model.api.ChoiceNode;
28 import org.opendaylight.controller.yang.model.api.ContainerSchemaNode;
29 import org.opendaylight.controller.yang.model.api.DataSchemaNode;
30 import org.opendaylight.controller.yang.model.api.LeafSchemaNode;
31 import org.opendaylight.controller.yang.model.api.ListSchemaNode;
32 import org.opendaylight.controller.yang.model.api.Module;
33 import org.opendaylight.controller.yang.model.api.SchemaPath;
34 import org.opendaylight.controller.yang.model.api.TypeDefinition;
35 import org.opendaylight.controller.yang.model.util.ExtendedType;
36 import org.opendaylight.controller.yang.model.util.Leafref;
37
38 import com.google.common.collect.Lists;
39
40 public class AugmentTest {
41
42     private final URI types1NS = URI.create("urn:simple.nodes.test");
43     private final URI types2NS = URI.create("urn:simple.types.test");
44     private final URI types3NS = URI.create("urn:custom.nodes.test");
45     private Date types1Rev;
46     private Date types2Rev;
47     private Date types3Rev;
48     private final String t1 = "n";
49     private final String t2 = "t";
50     private final String t3 = "c";
51     private QName q0;
52     private QName q1;
53     private QName q2;
54
55     private final DateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
56     private Set<Module> modules;
57
58     @Before
59     public void init() throws FileNotFoundException, ParseException {
60         types1Rev = simpleDateFormat.parse("2013-02-27");
61         types2Rev = simpleDateFormat.parse("2013-07-03");
62         types3Rev = simpleDateFormat.parse("2013-02-27");
63
64         q0 = new QName(types2NS, types2Rev, t2, "interfaces");
65         q1 = new QName(types2NS, types2Rev, t2, "ifEntry");
66         q2 = new QName(types3NS, types3Rev, t3, "augment-holder");
67
68         modules = TestUtils.loadModules(getClass().getResource("/model").getPath());
69         assertEquals(3, modules.size());
70     }
71
72     @Test
73     public void testAugmentParsing() {
74         SchemaPath expectedPath = null;
75         QName[] qnames = null;
76
77         // testfile1
78         Module module1 = TestUtils.findModule(modules, "nodes");
79         Set<AugmentationSchema> augmentations = module1.getAugmentations();
80         assertEquals(1, augmentations.size());
81         AugmentationSchema augment = augmentations.iterator().next();
82
83         Set<DataSchemaNode> augmentChildren = augment.getChildNodes();
84         assertEquals(5, augmentChildren.size());
85         for(DataSchemaNode dsn : augmentChildren) {
86             assertTrue(dsn.isAugmenting());
87         }
88
89         LeafSchemaNode ds0ChannelNumber = (LeafSchemaNode) augment.getDataChildByName("ds0ChannelNumber");
90         LeafSchemaNode interfaceId = (LeafSchemaNode) augment.getDataChildByName("interface-id");
91         LeafSchemaNode myType = (LeafSchemaNode) augment.getDataChildByName("my-type");
92         ContainerSchemaNode schemas = (ContainerSchemaNode) augment.getDataChildByName("schemas");
93         ChoiceNode odl = (ChoiceNode)augment.getDataChildByName("odl");
94
95         assertNotNull(ds0ChannelNumber);
96         assertNotNull(interfaceId);
97         assertNotNull(myType);
98         assertNotNull(schemas);
99         assertNotNull(odl);
100
101         qnames = new QName[4];
102         qnames[0] = q0;
103         qnames[1] = q1;
104         qnames[2] = q2;
105
106         // leaf ds0ChannelNumber
107         qnames[3] = new QName(types1NS, types1Rev, t1, "ds0ChannelNumber");
108         expectedPath = new SchemaPath(Lists.newArrayList(qnames), true);
109         assertEquals(expectedPath, ds0ChannelNumber.getPath());
110
111         // leaf interface-id
112         qnames[3] = new QName(types1NS, types1Rev, t1, "interface-id");
113         expectedPath = new SchemaPath(Lists.newArrayList(qnames), true);
114         assertEquals(expectedPath, interfaceId.getPath());
115
116         // leaf my-type
117         qnames[3] = new QName(types1NS, types1Rev, t1, "my-type");
118         expectedPath = new SchemaPath(Lists.newArrayList(qnames), true);
119         assertEquals(expectedPath, myType.getPath());
120
121         // container schemas
122         qnames[3] = new QName(types1NS, types1Rev, t1, "schemas");
123         expectedPath = new SchemaPath(Lists.newArrayList(qnames), true);
124         assertEquals(expectedPath, schemas.getPath());
125
126         // choice odl
127         qnames[3] = new QName(types1NS, types1Rev, t1, "odl");
128         expectedPath = new SchemaPath(Lists.newArrayList(qnames), true);
129         assertEquals(expectedPath, odl.getPath());
130
131
132         // testfile3
133         Module module3 = TestUtils.findModule(modules, "custom");
134         augmentations = module3.getAugmentations();
135         assertEquals(3, augmentations.size());
136         AugmentationSchema augment1 = null;
137         AugmentationSchema augment2 = null;
138         AugmentationSchema augment3 = null;
139         for (AugmentationSchema as : augmentations) {
140             if("if:ifType='ds0'".equals(as.getWhenCondition().toString())) {
141                 augment1 = as;
142             } else if("if:ifType='ds2'".equals(as.getWhenCondition().toString())) {
143                 augment2 = as;
144             } else if ("if:leafType='ds1'".equals(as.getWhenCondition().toString())) {
145                 augment3 = as;
146             }
147         }
148         assertNotNull(augment1);
149         assertNotNull(augment2);
150         assertNotNull(augment3);
151
152         assertEquals(1, augment1.getChildNodes().size());
153         ContainerSchemaNode augmentHolder = (ContainerSchemaNode) augment1.getDataChildByName("augment-holder");
154         assertTrue(augmentHolder.isAugmenting());
155
156         assertEquals(1, augment2.getChildNodes().size());
157         ContainerSchemaNode augmentHolder2 = (ContainerSchemaNode) augment2.getDataChildByName("augment-holder2");
158         assertTrue(augmentHolder2.isAugmenting());
159
160         assertEquals(1, augment3.getChildNodes().size());
161         LeafSchemaNode linkleaf = (LeafSchemaNode) augment3.getDataChildByName("linkleaf");
162         assertTrue(linkleaf.isAugmenting());
163     }
164
165     @Test
166     public void testAugmentResolving() throws ParseException {
167         SchemaPath expectedPath = null;
168         QName[] qnames = null;
169
170         Module module2 = TestUtils.findModule(modules, "types");
171         ContainerSchemaNode interfaces = (ContainerSchemaNode) module2.getDataChildByName("interfaces");
172         ListSchemaNode ifEntry = (ListSchemaNode) interfaces.getDataChildByName("ifEntry");
173         ContainerSchemaNode augmentedContainer = (ContainerSchemaNode) ifEntry.getDataChildByName("augment-holder");
174
175         // testfile1.yang
176         // augment "/data:interfaces/data:ifEntry/t3:augment-holder"
177         LeafSchemaNode ds0ChannelNumber = (LeafSchemaNode) augmentedContainer.getDataChildByName("ds0ChannelNumber");
178         LeafSchemaNode interfaceId = (LeafSchemaNode) augmentedContainer.getDataChildByName("interface-id");
179         LeafSchemaNode myType = (LeafSchemaNode) augmentedContainer.getDataChildByName("my-type");
180         ContainerSchemaNode schemas = (ContainerSchemaNode) augmentedContainer.getDataChildByName("schemas");
181         ChoiceNode odl = (ChoiceNode)augmentedContainer.getDataChildByName("odl");
182
183         assertNotNull(ds0ChannelNumber);
184         assertNotNull(interfaceId);
185         assertNotNull(myType);
186         assertNotNull(schemas);
187         assertNotNull(odl);
188
189         qnames = new QName[4];
190         qnames[0] = q0;
191         qnames[1] = q1;
192         qnames[2] = q2;
193
194         // leaf ds0ChannelNumber
195         qnames[3] = new QName(types1NS, types1Rev, t1, "ds0ChannelNumber");
196         expectedPath = new SchemaPath(Lists.newArrayList(qnames), true);
197         assertEquals(expectedPath, ds0ChannelNumber.getPath());
198
199         // leaf interface-id
200         qnames[3] = new QName(types1NS, types1Rev, t1, "interface-id");
201         expectedPath = new SchemaPath(Lists.newArrayList(qnames), true);
202         assertEquals(expectedPath, interfaceId.getPath());
203
204         // leaf my-type
205         qnames[3] = new QName(types1NS, types1Rev, t1, "my-type");
206         expectedPath = new SchemaPath(Lists.newArrayList(qnames), true);
207         assertEquals(expectedPath, myType.getPath());
208
209         // container schemas
210         qnames[3] = new QName(types1NS, types1Rev, t1, "schemas");
211         expectedPath = new SchemaPath(Lists.newArrayList(qnames), true);
212         assertEquals(expectedPath, schemas.getPath());
213
214         // choice odl
215         qnames[3] = new QName(types1NS, types1Rev, t1, "odl");
216         expectedPath = new SchemaPath(Lists.newArrayList(qnames), true);
217         assertEquals(expectedPath, odl.getPath());
218
219         // testfile3.yang
220         // augment "/data:interfaces/data:ifEntry/t3:augment-holder/t1:schemas"
221         LeafSchemaNode linkleaf = (LeafSchemaNode) schemas.getDataChildByName("linkleaf");
222         assertNotNull(linkleaf);
223
224         qnames = new QName[5];
225         qnames[0] = q0;
226         qnames[1] = q1;
227         qnames[2] = q2;
228         qnames[3] = new QName(types1NS, types1Rev, t1, "schemas");
229         qnames[4] = new QName(types3NS, types3Rev, t3, "linkleaf");
230         expectedPath = new SchemaPath(Arrays.asList(qnames), true);
231         assertEquals(expectedPath, linkleaf.getPath());
232     }
233
234     @Test
235     public void testAugmentChoice() throws ParseException {
236         SchemaPath expectedPath = null;
237         QName[] qnames = null;
238
239         Module module2 = TestUtils.findModule(modules, "types");
240         ContainerSchemaNode interfaces = (ContainerSchemaNode) module2.getDataChildByName("interfaces");
241         ListSchemaNode ifEntry = (ListSchemaNode) interfaces.getDataChildByName("ifEntry");
242         ContainerSchemaNode augmentedContainer = (ContainerSchemaNode) ifEntry.getDataChildByName("augment-holder");
243
244         // testfile1.yang
245         // augment "/data:interfaces/data:ifEntry/t3:augment-holder"
246         ChoiceNode odl = (ChoiceNode)augmentedContainer.getDataChildByName("odl");
247         assertNotNull(odl);
248         Set<ChoiceCaseNode> cases = odl.getCases();
249         assertEquals(4, cases.size());
250
251         ChoiceCaseNode id = null;
252         ChoiceCaseNode node1 = null;
253         ChoiceCaseNode node2 = null;
254         ChoiceCaseNode node3 = null;
255
256         for(ChoiceCaseNode ccn : cases) {
257             if("id".equals(ccn.getQName().getLocalName())) {
258                 id = ccn;
259             } else if("node1".equals(ccn.getQName().getLocalName())) {
260                 node1 = ccn;
261             } else if("node2".equals(ccn.getQName().getLocalName())) {
262                 node2 = ccn;
263             } else if("node3".equals(ccn.getQName().getLocalName())) {
264                 node3 = ccn;
265             }
266         }
267
268         assertNotNull(id);
269         assertNotNull(node1);
270         assertNotNull(node2);
271         assertNotNull(node3);
272
273         qnames = new QName[5];
274         qnames[0] = q0;
275         qnames[1] = q1;
276         qnames[2] = q2;
277         qnames[3] = new QName(types1NS, types1Rev, t1, "odl");
278
279         // case id
280         qnames[4] = new QName(types1NS, types1Rev, t1, "id");
281         expectedPath = new SchemaPath(Lists.newArrayList(qnames), true);
282         assertEquals(expectedPath, id.getPath());
283         Set<DataSchemaNode> idChildren = id.getChildNodes();
284         assertEquals(1, idChildren.size());
285
286         // case node1
287         qnames[4] = new QName(types1NS, types1Rev, t1, "node1");
288         expectedPath = new SchemaPath(Lists.newArrayList(qnames), true);
289         assertEquals(expectedPath, node1.getPath());
290         Set<DataSchemaNode> node1Children = node1.getChildNodes();
291         assertTrue(node1Children.isEmpty());
292
293         // case node2
294         qnames[4] = new QName(types1NS, types1Rev, t1, "node2");
295         expectedPath = new SchemaPath(Lists.newArrayList(qnames), true);
296         assertEquals(expectedPath, node2.getPath());
297         Set<DataSchemaNode> node2Children = node2.getChildNodes();
298         assertTrue(node2Children.isEmpty());
299
300         // case node3
301         qnames[4] = new QName(types1NS, types1Rev, t1, "node3");
302         expectedPath = new SchemaPath(Lists.newArrayList(qnames), true);
303         assertEquals(expectedPath, node3.getPath());
304         Set<DataSchemaNode> node3Children = node3.getChildNodes();
305         assertEquals(1, node3Children.size());
306
307         // test cases
308         qnames = new QName[6];
309         qnames[0] = q0;
310         qnames[1] = q1;
311         qnames[2] = q2;
312         qnames[3] = new QName(types1NS, types1Rev, t1, "odl");
313
314         // case id child
315         qnames[4] = new QName(types1NS, types1Rev, t1, "id");
316         qnames[5] = new QName(types1NS, types1Rev, t1, "id");
317         LeafSchemaNode caseIdChild = (LeafSchemaNode)idChildren.iterator().next();
318         assertNotNull(caseIdChild);
319         expectedPath = new SchemaPath(Lists.newArrayList(qnames), true);
320         assertEquals(expectedPath, caseIdChild.getPath());
321
322         // case node3 child
323         qnames[4] = new QName(types1NS, types1Rev, t1, "node3");
324         qnames[5] = new QName(types1NS, types1Rev, t1, "node3");
325         ContainerSchemaNode caseNode3Child = (ContainerSchemaNode)node3Children.iterator().next();
326         assertNotNull(caseNode3Child);
327         expectedPath = new SchemaPath(Lists.newArrayList(qnames), true);
328         assertEquals(expectedPath, caseNode3Child.getPath());
329     }
330
331     @Test
332     public void testAugmentNodesTypeSchemaPath() throws Exception {
333         Module testModule = TestUtils.findModule(modules, "nodes");
334         Set<AugmentationSchema> augments = testModule.getAugmentations();
335         assertEquals(1, augments.size());
336         AugmentationSchema augment = augments.iterator().next();
337
338         LeafSchemaNode ifcId = (LeafSchemaNode) augment.getDataChildByName("interface-id");
339         Leafref ifcIdType = (Leafref) ifcId.getType();
340         SchemaPath ifcIdTypeSchemaPath = ifcIdType.getPath();
341         List<QName> ifcIdTypePath = ifcIdTypeSchemaPath.getPath();
342
343         Date expectedDate = simpleDateFormat.parse("2013-02-27");
344
345         QName q3 = new QName(types1NS, expectedDate, "data", "interface-id");
346         assertEquals(q0, ifcIdTypePath.get(0));
347         assertEquals(q1, ifcIdTypePath.get(1));
348         assertEquals(q2, ifcIdTypePath.get(2));
349         assertEquals(q3, ifcIdTypePath.get(3));
350
351         LeafSchemaNode myType = (LeafSchemaNode) augment.getDataChildByName("my-type");
352         ExtendedType leafType = (ExtendedType) myType.getType();
353
354         testModule = TestUtils.findModule(modules, "types");
355         TypeDefinition<?> typedef = TestUtils.findTypedef(testModule.getTypeDefinitions(), "int32-ext2");
356
357         assertEquals(typedef, leafType);
358     }
359
360 }