9b9c75d92c06a147a89043cf50cdf921341aac04
[mdsal.git] / binding / mdsal-binding-generator-impl / src / test / java / org / opendaylight / mdsal / binding / generator / impl / GeneratedTypesLeafrefTest.java
1 /*
2  * Copyright (c) 2016 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.mdsal.binding.generator.impl;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertNotSame;
13 import static org.junit.Assert.assertTrue;
14 import static org.junit.Assert.fail;
15
16 import java.io.File;
17 import java.net.URI;
18 import java.util.List;
19 import org.junit.Test;
20 import org.opendaylight.mdsal.binding.generator.api.BindingGenerator;
21 import org.opendaylight.mdsal.binding.model.api.GeneratedProperty;
22 import org.opendaylight.mdsal.binding.model.api.GeneratedTransferObject;
23 import org.opendaylight.mdsal.binding.model.api.GeneratedType;
24 import org.opendaylight.mdsal.binding.model.api.MethodSignature;
25 import org.opendaylight.mdsal.binding.model.api.Type;
26 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
27 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
28
29 public class GeneratedTypesLeafrefTest {
30
31     @Test
32     public void testLeafrefResolving() throws Exception {
33         final File abstractTopology = new File(getClass().getResource(
34                 "/leafref-test-models/abstract-topology@2013-02-08.yang").toURI());
35         final File ietfInterfaces = new File(getClass().getResource("/ietf/ietf-interfaces.yang").toURI());
36         final File ietfInetTypes = new File(getClass().getResource("/ietf/ietf-inet-types.yang").toURI());
37         final File ietfYangTypes = new File(getClass().getResource("/ietf/ietf-yang-types.yang").toURI());
38
39         final SchemaContext context =  YangParserTestUtils.parseYangFiles(abstractTopology, ietfInterfaces,
40                 ietfInetTypes, ietfYangTypes);
41         assertNotNull(context);
42         assertEquals(4, context.getModules().size());
43
44         final BindingGenerator bindingGen = new BindingGeneratorImpl(true);
45         final List<Type> genTypes = bindingGen.generateTypes(context);
46
47         assertEquals(54, genTypes.size());
48         assertNotNull(genTypes);
49
50         GeneratedTransferObject gtIfcKey = null;
51         GeneratedType gtIfc = null;
52         GeneratedType gtNetworkLink = null;
53         GeneratedType gtSource = null;
54         GeneratedType gtDest = null;
55         GeneratedType gtTunnel = null;
56         GeneratedTransferObject gtTunnelKey = null;
57         GeneratedType gtTopology = null;
58         for (final Type type : genTypes) {
59             final String name = type.getName();
60             if ("InterfaceKey".equals(name)) {
61                 gtIfcKey = (GeneratedTransferObject) type;
62             } else if ("Interface".equals(name)) {
63                 gtIfc = (GeneratedType) type;
64             } else if ("NetworkLink".equals(name)) {
65                 gtNetworkLink = (GeneratedType) type;
66             } else if ("SourceNode".equals(name)) {
67                 gtSource = (GeneratedType) type;
68             } else if ("DestinationNode".equals(name)) {
69                 gtDest = (GeneratedType) type;
70             } else if ("Tunnel".equals(name)) {
71                 gtTunnel = (GeneratedType) type;
72             } else if ("TunnelKey".equals(name)) {
73                 gtTunnelKey = (GeneratedTransferObject) type;
74             } else if ("Topology".equals(name)) {
75                 gtTopology = (GeneratedType) type;
76             }
77         }
78
79         assertNotNull(gtIfcKey);
80         assertNotNull(gtIfc);
81         assertNotNull(gtNetworkLink);
82         assertNotNull(gtSource);
83         assertNotNull(gtDest);
84         assertNotNull(gtTunnel);
85         assertNotNull(gtTunnelKey);
86         assertNotNull(gtTopology);
87
88         // Topology
89         final List<MethodSignature> gtTopoMethods = gtTopology.getMethodDefinitions();
90         assertNotNull(gtTopoMethods);
91         MethodSignature condLeafref = null;
92         for (final MethodSignature method : gtTopoMethods) {
93             if (method.getName().equals("getCondLeafref")) {
94                 condLeafref = method;
95             }
96         }
97         assertNotNull(condLeafref);
98         final Type condLeafRT = condLeafref.getReturnType();
99         assertNotNull(condLeafRT);
100         assertEquals("java.lang.Object", condLeafRT.getFullyQualifiedName());
101
102         // InterfaceId
103         final List<GeneratedProperty> gtIfcKeyProps = gtIfcKey.getProperties();
104         assertNotNull(gtIfcKeyProps);
105         GeneratedProperty ifcIdProp = null;
106         for (final GeneratedProperty property : gtIfcKeyProps) {
107             if (property.getName().equals("interfaceId")) {
108                 ifcIdProp = property;
109             }
110         }
111         assertNotNull(ifcIdProp);
112         final Type ifcIdPropType = ifcIdProp.getReturnType();
113         assertNotNull(ifcIdPropType);
114         assertEquals("java.lang.String", ifcIdPropType.getFullyQualifiedName());
115
116         // Interface
117         final List<MethodSignature> gtIfcMethods = gtIfc.getMethodDefinitions();
118         assertNotNull(gtIfcMethods);
119         MethodSignature getIfcKey = null;
120         MethodSignature getHigherLayerIf = null;
121         for (final MethodSignature method : gtIfcMethods) {
122             if (method.getName().equals("getKey")) {
123                 getIfcKey = method;
124             } else if (method.getName().equals("getHigherLayerIf")) {
125                 getHigherLayerIf = method;
126             }
127         }
128         assertNotNull(getIfcKey);
129         final Type getIfcKeyType = getIfcKey.getReturnType();
130         assertNotNull(getIfcKeyType);
131         assertNotSame("java.lang.Void", getIfcKeyType);
132         assertEquals("InterfaceKey", getIfcKeyType.getName());
133
134         assertNotNull(getHigherLayerIf);
135         final Type getHigherLayerIfType = getHigherLayerIf.getReturnType();
136         assertNotNull(getHigherLayerIfType);
137         assertNotSame("java.lang.Void", getHigherLayerIfType);
138         assertEquals("List", getHigherLayerIfType.getName());
139
140         // NetworkLink
141         final List<MethodSignature> gtNetworkLinkMethods = gtNetworkLink.getMethodDefinitions();
142         assertNotNull(gtNetworkLinkMethods);
143         MethodSignature getIfc = null;
144         for (final MethodSignature method : gtNetworkLinkMethods) {
145             if (method.getName().equals("getInterface")) {
146                 getIfc = method;
147             }
148         }
149         assertNotNull(getIfc);
150         final Type getIfcType = getIfc.getReturnType();
151         assertNotNull(getIfcType);
152         assertNotSame("java.lang.Void", getIfcType);
153         assertEquals("String", getIfcType.getName());
154
155         // SourceNode
156         final List<MethodSignature> gtSourceMethods = gtSource.getMethodDefinitions();
157         assertNotNull(gtSourceMethods);
158         MethodSignature getIdSource = null;
159         for (final MethodSignature method : gtSourceMethods) {
160             if (method.getName().equals("getId")) {
161                 getIdSource = method;
162             }
163         }
164         assertNotNull(getIdSource);
165         final Type getIdType = getIdSource.getReturnType();
166         assertNotNull(getIdType);
167         assertNotSame("java.lang.Void", getIdType);
168         assertEquals("Uri", getIdType.getName());
169
170         // DestinationNode
171         final List<MethodSignature> gtDestMethods = gtDest.getMethodDefinitions();
172         assertNotNull(gtDestMethods);
173         MethodSignature getIdDest = null;
174         for (final MethodSignature method : gtDestMethods) {
175             if (method.getName().equals("getId")) {
176                 getIdDest = method;
177             }
178         }
179         assertNotNull(getIdDest);
180         final Type getIdDestType = getIdDest.getReturnType();
181         assertNotNull(getIdDestType);
182         assertNotSame("java.lang.Void", getIdDestType);
183         assertEquals("Uri", getIdDestType.getName());
184
185         // Tunnel
186         final List<MethodSignature> gtTunnelMethods = gtTunnel.getMethodDefinitions();
187         assertNotNull(gtTunnelMethods);
188         MethodSignature getTunnelKey = null;
189         for (final MethodSignature method : gtTunnelMethods) {
190             if (method.getName().equals("getKey")) {
191                 getTunnelKey = method;
192             }
193         }
194         assertNotNull(getTunnelKey);
195         final Type getTunnelKeyType = getTunnelKey.getReturnType();
196         assertNotNull(getTunnelKeyType);
197         assertNotSame("java.lang.Void", getTunnelKeyType);
198         assertEquals("TunnelKey", getTunnelKeyType.getName());
199
200         // TunnelKey
201         final List<GeneratedProperty> gtTunnelKeyProps = gtTunnelKey.getProperties();
202         assertNotNull(gtTunnelKeyProps);
203         GeneratedProperty tunnelId = null;
204         for (final GeneratedProperty property : gtTunnelKeyProps) {
205             if (property.getName().equals("tunnelId")) {
206                 tunnelId = property;
207             }
208         }
209         assertNotNull(tunnelId);
210         final Type tunnelIdType = tunnelId.getReturnType();
211         assertNotNull(tunnelIdType);
212         assertNotSame("java.lang.Void", tunnelIdType);
213         assertEquals("Uri", tunnelIdType.getName());
214     }
215
216     @Test
217     public void testLeafrefInvalidPathResolving() throws Exception {
218         final URI resource = getClass().getResource("/leafref-test-invalid-model/foo.yang").toURI();
219         assertNotNull(resource);
220
221         final SchemaContext context = YangParserTestUtils.parseYangFiles(new File(resource));
222         assertNotNull(context);
223         assertEquals(1, context.getModules().size());
224
225         final BindingGenerator bindingGen = new BindingGeneratorImpl(true);
226         try {
227             bindingGen.generateTypes(context);
228             fail("Expected IllegalArgumentException caused by invalid leafref path");
229         } catch (final IllegalArgumentException e) {
230             final String expected = "Failed to find leafref target";
231             assertTrue(e.getMessage().contains(expected));
232         }
233     }
234 }