Merge "Fixed resolving of schema path and qname for nodes added by augmentation."
[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.SchemaPath;
34 import org.opendaylight.yangtools.yang.model.util.BaseTypes;
35
36 import com.google.common.collect.Lists;
37
38 public class AugmentTest {
39
40     private final URI types1NS = URI.create("urn:simple.nodes.test");
41     private final URI types2NS = URI.create("urn:simple.types.test");
42     private final URI types3NS = URI.create("urn:custom.nodes.test");
43     private Date types1Rev;
44     private Date types2Rev;
45     private Date types3Rev;
46     private final String t1 = "n";
47     private final String t2 = "t";
48     private final String t3 = "c";
49     private QName q0;
50     private QName q1;
51     private QName q2;
52
53     private final DateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
54     private Set<Module> modules;
55
56     @Before
57     public void init() throws FileNotFoundException, ParseException {
58         types1Rev = simpleDateFormat.parse("2013-02-27");
59         types2Rev = simpleDateFormat.parse("2013-07-03");
60         types3Rev = simpleDateFormat.parse("2013-02-27");
61
62         q0 = new QName(types2NS, types2Rev, t2, "interfaces");
63         q1 = new QName(types2NS, types2Rev, t2, "ifEntry");
64         q2 = new QName(types3NS, types3Rev, t3, "augment-holder");
65
66         modules = TestUtils.loadModules(getClass().getResource("/model").getPath());
67         assertEquals(3, modules.size());
68     }
69
70     @Test
71     public void testAugmentParsing() {
72         SchemaPath expectedPath = null;
73         QName[] qnames = null;
74
75         // testfile1
76         Module module1 = TestUtils.findModule(modules, "nodes");
77         Set<AugmentationSchema> augmentations = module1.getAugmentations();
78         assertEquals(1, augmentations.size());
79         AugmentationSchema augment = augmentations.iterator().next();
80
81         Set<DataSchemaNode> augmentChildren = augment.getChildNodes();
82         assertEquals(5, augmentChildren.size());
83         for(DataSchemaNode dsn : augmentChildren) {
84             assertTrue(dsn.isAugmenting());
85         }
86
87         LeafSchemaNode ds0ChannelNumber = (LeafSchemaNode) augment.getDataChildByName("ds0ChannelNumber");
88         LeafSchemaNode interfaceId = (LeafSchemaNode) augment.getDataChildByName("interface-id");
89         LeafSchemaNode myType = (LeafSchemaNode) augment.getDataChildByName("my-type");
90         ContainerSchemaNode schemas = (ContainerSchemaNode) augment.getDataChildByName("schemas");
91         ChoiceNode odl = (ChoiceNode)augment.getDataChildByName("odl");
92
93         assertNotNull(ds0ChannelNumber);
94         assertNotNull(interfaceId);
95         assertNotNull(myType);
96         assertNotNull(schemas);
97         assertNotNull(odl);
98
99         qnames = new QName[4];
100         qnames[0] = q0;
101         qnames[1] = q1;
102         qnames[2] = q2;
103
104         // leaf ds0ChannelNumber
105         qnames[3] = new QName(types1NS, types1Rev, t1, "ds0ChannelNumber");
106         expectedPath = new SchemaPath(Lists.newArrayList(qnames), true);
107         assertEquals(expectedPath, ds0ChannelNumber.getPath());
108         // type of leaf ds0ChannelNumber
109         QName typeQName = BaseTypes.constructQName("string");
110         List<QName> typePath = Collections.singletonList(typeQName);
111         expectedPath = new SchemaPath(typePath, true);
112         assertEquals(expectedPath, ds0ChannelNumber.getType().getPath());
113
114         // leaf interface-id
115         qnames[3] = new QName(types1NS, types1Rev, t1, "interface-id");
116         expectedPath = new SchemaPath(Lists.newArrayList(qnames), true);
117         assertEquals(expectedPath, interfaceId.getPath());
118
119         // leaf my-type
120         qnames[3] = new QName(types1NS, types1Rev, t1, "my-type");
121         expectedPath = new SchemaPath(Lists.newArrayList(qnames), true);
122         assertEquals(expectedPath, myType.getPath());
123
124         // container schemas
125         qnames[3] = new QName(types1NS, types1Rev, t1, "schemas");
126         expectedPath = new SchemaPath(Lists.newArrayList(qnames), true);
127         assertEquals(expectedPath, schemas.getPath());
128
129         // choice odl
130         qnames[3] = new QName(types1NS, types1Rev, t1, "odl");
131         expectedPath = new SchemaPath(Lists.newArrayList(qnames), true);
132         assertEquals(expectedPath, odl.getPath());
133
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         assertTrue(augmentHolder.isAugmenting());
154
155         assertEquals(1, augment2.getChildNodes().size());
156         ContainerSchemaNode augmentHolder2 = (ContainerSchemaNode) augment2.getDataChildByName("augment-holder2");
157         assertTrue(augmentHolder2.isAugmenting());
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
170         // testfile1.yang
171         // augment "/data:interfaces/data:ifEntry/t3:augment-holder"
172         LeafSchemaNode ds0ChannelNumber = (LeafSchemaNode) augmentedContainer.getDataChildByName("ds0ChannelNumber");
173         LeafSchemaNode interfaceId = (LeafSchemaNode) augmentedContainer.getDataChildByName("interface-id");
174         LeafSchemaNode myType = (LeafSchemaNode) augmentedContainer.getDataChildByName("my-type");
175         ContainerSchemaNode schemas = (ContainerSchemaNode) augmentedContainer.getDataChildByName("schemas");
176         ChoiceNode odl = (ChoiceNode)augmentedContainer.getDataChildByName("odl");
177
178         assertNotNull(ds0ChannelNumber);
179         assertNotNull(interfaceId);
180         assertNotNull(myType);
181         assertNotNull(schemas);
182         assertNotNull(odl);
183
184         qnames = new QName[4];
185         qnames[0] = q0;
186         qnames[1] = q1;
187         qnames[2] = q2;
188
189         // leaf ds0ChannelNumber
190         qnames[3] = new QName(types1NS, types1Rev, t1, "ds0ChannelNumber");
191         expectedPath = new SchemaPath(Lists.newArrayList(qnames), true);
192         assertEquals(expectedPath, ds0ChannelNumber.getPath());
193
194         // leaf interface-id
195         qnames[3] = new QName(types1NS, types1Rev, t1, "interface-id");
196         expectedPath = new SchemaPath(Lists.newArrayList(qnames), true);
197         assertEquals(expectedPath, interfaceId.getPath());
198
199         // leaf my-type
200         qnames[3] = new QName(types1NS, types1Rev, t1, "my-type");
201         expectedPath = new SchemaPath(Lists.newArrayList(qnames), true);
202         assertEquals(expectedPath, myType.getPath());
203
204         // container schemas
205         qnames[3] = new QName(types1NS, types1Rev, t1, "schemas");
206         expectedPath = new SchemaPath(Lists.newArrayList(qnames), true);
207         assertEquals(expectedPath, schemas.getPath());
208
209         // choice odl
210         qnames[3] = new QName(types1NS, types1Rev, t1, "odl");
211         expectedPath = new SchemaPath(Lists.newArrayList(qnames), true);
212         assertEquals(expectedPath, odl.getPath());
213     }
214
215     @Test
216     public void testAugmentChoice() throws ParseException {
217         SchemaPath expectedPath = null;
218         QName[] qnames = null;
219
220         Module module2 = TestUtils.findModule(modules, "types");
221         ContainerSchemaNode interfaces = (ContainerSchemaNode) module2.getDataChildByName("interfaces");
222         ListSchemaNode ifEntry = (ListSchemaNode) interfaces.getDataChildByName("ifEntry");
223         ContainerSchemaNode augmentedContainer = (ContainerSchemaNode) ifEntry.getDataChildByName("augment-holder");
224
225         // testfile1.yang
226         // augment "/data:interfaces/data:ifEntry/t3:augment-holder"
227         ChoiceNode odl = (ChoiceNode)augmentedContainer.getDataChildByName("odl");
228         assertNotNull(odl);
229         Set<ChoiceCaseNode> cases = odl.getCases();
230         assertEquals(4, cases.size());
231
232         ChoiceCaseNode id = null;
233         ChoiceCaseNode node1 = null;
234         ChoiceCaseNode node2 = null;
235         ChoiceCaseNode node3 = null;
236
237         for(ChoiceCaseNode ccn : cases) {
238             if("id".equals(ccn.getQName().getLocalName())) {
239                 id = ccn;
240             } else if("node1".equals(ccn.getQName().getLocalName())) {
241                 node1 = ccn;
242             } else if("node2".equals(ccn.getQName().getLocalName())) {
243                 node2 = ccn;
244             } else if("node3".equals(ccn.getQName().getLocalName())) {
245                 node3 = ccn;
246             }
247         }
248
249         assertNotNull(id);
250         assertNotNull(node1);
251         assertNotNull(node2);
252         assertNotNull(node3);
253
254         qnames = new QName[5];
255         qnames[0] = q0;
256         qnames[1] = q1;
257         qnames[2] = q2;
258         qnames[3] = new QName(types1NS, types1Rev, t1, "odl");
259
260         // case id
261         qnames[4] = new QName(types1NS, types1Rev, t1, "id");
262         expectedPath = new SchemaPath(Lists.newArrayList(qnames), true);
263         assertEquals(expectedPath, id.getPath());
264         Set<DataSchemaNode> idChildren = id.getChildNodes();
265         assertEquals(1, idChildren.size());
266
267         // case node1
268         qnames[4] = new QName(types1NS, types1Rev, t1, "node1");
269         expectedPath = new SchemaPath(Lists.newArrayList(qnames), true);
270         assertEquals(expectedPath, node1.getPath());
271         Set<DataSchemaNode> node1Children = node1.getChildNodes();
272         assertTrue(node1Children.isEmpty());
273
274         // case node2
275         qnames[4] = new QName(types1NS, types1Rev, t1, "node2");
276         expectedPath = new SchemaPath(Lists.newArrayList(qnames), true);
277         assertEquals(expectedPath, node2.getPath());
278         Set<DataSchemaNode> node2Children = node2.getChildNodes();
279         assertTrue(node2Children.isEmpty());
280
281         // case node3
282         qnames[4] = new QName(types1NS, types1Rev, t1, "node3");
283         expectedPath = new SchemaPath(Lists.newArrayList(qnames), true);
284         assertEquals(expectedPath, node3.getPath());
285         Set<DataSchemaNode> node3Children = node3.getChildNodes();
286         assertEquals(1, node3Children.size());
287
288         // test cases
289         qnames = new QName[6];
290         qnames[0] = q0;
291         qnames[1] = q1;
292         qnames[2] = q2;
293         qnames[3] = new QName(types1NS, types1Rev, t1, "odl");
294
295         // case id child
296         qnames[4] = new QName(types1NS, types1Rev, t1, "id");
297         qnames[5] = new QName(types1NS, types1Rev, t1, "id");
298         LeafSchemaNode caseIdChild = (LeafSchemaNode)idChildren.iterator().next();
299         assertNotNull(caseIdChild);
300         expectedPath = new SchemaPath(Lists.newArrayList(qnames), true);
301         assertEquals(expectedPath, caseIdChild.getPath());
302
303         // case node3 child
304         qnames[4] = new QName(types1NS, types1Rev, t1, "node3");
305         qnames[5] = new QName(types1NS, types1Rev, t1, "node3");
306         ContainerSchemaNode caseNode3Child = (ContainerSchemaNode)node3Children.iterator().next();
307         assertNotNull(caseNode3Child);
308         expectedPath = new SchemaPath(Lists.newArrayList(qnames), true);
309         assertEquals(expectedPath, caseNode3Child.getPath());
310     }
311
312 }