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.controller.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.Arrays;
18 import java.util.Date;
19 import java.util.List;
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;
38 import com.google.common.collect.Lists;
40 public class AugmentTest {
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";
55 private final DateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
56 private Set<Module> modules;
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");
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");
68 modules = TestUtils.loadModules(getClass().getResource("/model").getPath());
69 assertEquals(3, modules.size());
73 public void testAugmentParsing() {
74 SchemaPath expectedPath = null;
75 QName[] qnames = null;
78 Module module1 = TestUtils.findModule(modules, "nodes");
79 Set<AugmentationSchema> augmentations = module1.getAugmentations();
80 assertEquals(1, augmentations.size());
81 AugmentationSchema augment = augmentations.iterator().next();
83 Set<DataSchemaNode> augmentChildren = augment.getChildNodes();
84 assertEquals(5, augmentChildren.size());
85 for(DataSchemaNode dsn : augmentChildren) {
86 assertTrue(dsn.isAugmenting());
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");
95 assertNotNull(ds0ChannelNumber);
96 assertNotNull(interfaceId);
97 assertNotNull(myType);
98 assertNotNull(schemas);
101 qnames = new QName[4];
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());
112 qnames[3] = new QName(types1NS, types1Rev, t1, "interface-id");
113 expectedPath = new SchemaPath(Lists.newArrayList(qnames), true);
114 assertEquals(expectedPath, interfaceId.getPath());
117 qnames[3] = new QName(types1NS, types1Rev, t1, "my-type");
118 expectedPath = new SchemaPath(Lists.newArrayList(qnames), true);
119 assertEquals(expectedPath, myType.getPath());
122 qnames[3] = new QName(types1NS, types1Rev, t1, "schemas");
123 expectedPath = new SchemaPath(Lists.newArrayList(qnames), true);
124 assertEquals(expectedPath, schemas.getPath());
127 qnames[3] = new QName(types1NS, types1Rev, t1, "odl");
128 expectedPath = new SchemaPath(Lists.newArrayList(qnames), true);
129 assertEquals(expectedPath, odl.getPath());
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())) {
142 } else if("if:ifType='ds2'".equals(as.getWhenCondition().toString())) {
144 } else if ("if:leafType='ds1'".equals(as.getWhenCondition().toString())) {
148 assertNotNull(augment1);
149 assertNotNull(augment2);
150 assertNotNull(augment3);
152 assertEquals(1, augment1.getChildNodes().size());
153 ContainerSchemaNode augmentHolder = (ContainerSchemaNode) augment1.getDataChildByName("augment-holder");
154 assertTrue(augmentHolder.isAugmenting());
156 assertEquals(1, augment2.getChildNodes().size());
157 ContainerSchemaNode augmentHolder2 = (ContainerSchemaNode) augment2.getDataChildByName("augment-holder2");
158 assertTrue(augmentHolder2.isAugmenting());
160 assertEquals(1, augment3.getChildNodes().size());
161 LeafSchemaNode linkleaf = (LeafSchemaNode) augment3.getDataChildByName("linkleaf");
162 assertTrue(linkleaf.isAugmenting());
166 public void testAugmentResolving() throws ParseException {
167 SchemaPath expectedPath = null;
168 QName[] qnames = null;
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");
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");
183 assertNotNull(ds0ChannelNumber);
184 assertNotNull(interfaceId);
185 assertNotNull(myType);
186 assertNotNull(schemas);
189 qnames = new QName[4];
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());
200 qnames[3] = new QName(types1NS, types1Rev, t1, "interface-id");
201 expectedPath = new SchemaPath(Lists.newArrayList(qnames), true);
202 assertEquals(expectedPath, interfaceId.getPath());
205 qnames[3] = new QName(types1NS, types1Rev, t1, "my-type");
206 expectedPath = new SchemaPath(Lists.newArrayList(qnames), true);
207 assertEquals(expectedPath, myType.getPath());
210 qnames[3] = new QName(types1NS, types1Rev, t1, "schemas");
211 expectedPath = new SchemaPath(Lists.newArrayList(qnames), true);
212 assertEquals(expectedPath, schemas.getPath());
215 qnames[3] = new QName(types1NS, types1Rev, t1, "odl");
216 expectedPath = new SchemaPath(Lists.newArrayList(qnames), true);
217 assertEquals(expectedPath, odl.getPath());
220 // augment "/data:interfaces/data:ifEntry/t3:augment-holder/t1:schemas"
221 LeafSchemaNode linkleaf = (LeafSchemaNode) schemas.getDataChildByName("linkleaf");
222 assertNotNull(linkleaf);
224 qnames = new QName[5];
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());
235 public void testAugmentChoice() throws ParseException {
236 SchemaPath expectedPath = null;
237 QName[] qnames = null;
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");
245 // augment "/data:interfaces/data:ifEntry/t3:augment-holder"
246 ChoiceNode odl = (ChoiceNode)augmentedContainer.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 qnames = new QName[5];
277 qnames[3] = new QName(types1NS, types1Rev, t1, "odl");
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());
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());
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());
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());
308 qnames = new QName[6];
312 qnames[3] = new QName(types1NS, types1Rev, t1, "odl");
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());
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());
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();
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();
343 Date expectedDate = simpleDateFormat.parse("2013-02-27");
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));
351 LeafSchemaNode myType = (LeafSchemaNode) augment.getDataChildByName("my-type");
352 ExtendedType leafType = (ExtendedType) myType.getType();
354 testModule = TestUtils.findModule(modules, "types");
355 TypeDefinition<?> typedef = TestUtils.findTypedef(testModule.getTypeDefinitions(), "int32-ext2");
357 assertEquals(typedef, leafType);