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