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