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.yangtools.sal.binding.generator.impl;
10 import static org.junit.Assert.*;
14 import java.net.URISyntaxException;
15 import java.util.ArrayList;
16 import java.util.List;
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;
31 public class GeneratedTypesLeafrefTest {
33 private SchemaContext resolveSchemaContextFromFiles(final URI... yangFiles) {
34 final YangModelParser parser = new YangParserImpl();
36 final List<File> inputFiles = new ArrayList<File>();
37 for (int i = 0; i < yangFiles.length; ++i) {
38 inputFiles.add(new File(yangFiles[i]));
41 final Set<Module> modules = parser.parseYangModels(inputFiles);
42 return parser.resolveSchemaContext(modules);
46 public void testLeafrefResolving() throws URISyntaxException {
47 final URI topologyPath = getClass().getResource("/leafref-test-models/abstract-topology@2013-02-08.yang")
49 final URI interfacesPath = getClass().getResource("/leafref-test-models/ietf-interfaces@2012-11-15.yang")
51 final URI inetTypesPath = getClass().getResource("/leafref-test-models/ietf-inet-types@2010-09-24.yang")
53 final URI yangTypesPath = getClass().getResource("/leafref-test-models/ietf-yang-types@2010-09-24.yang")
56 assertNotNull(topologyPath);
57 assertNotNull(interfacesPath);
58 assertNotNull(inetTypesPath);
59 assertNotNull(yangTypesPath);
61 final SchemaContext context = resolveSchemaContextFromFiles(topologyPath, interfacesPath, inetTypesPath,
63 assertNotNull(context);
64 assertEquals(4, context.getModules().size());
66 final BindingGenerator bindingGen = new BindingGeneratorImpl();
67 final List<Type> genTypes = bindingGen.generateTypes(context);
69 assertEquals(54, genTypes.size());
70 assertNotNull(genTypes);
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;
101 assertNotNull(gtIfcKey);
102 assertNotNull(gtIfc);
103 assertNotNull(gtNetworkLink);
104 assertNotNull(gtSource);
105 assertNotNull(gtDest);
106 assertNotNull(gtTunnel);
107 assertNotNull(gtTunnelKey);
108 assertNotNull(gtTopology);
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;
119 assertNotNull(condLeafref);
120 Type condLeafRT = condLeafref.getReturnType();
121 assertNotNull(condLeafRT);
122 assertEquals("java.lang.Object", condLeafRT.getFullyQualifiedName());
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;
133 assertNotNull(ifcIdProp);
134 Type ifcIdPropType = ifcIdProp.getReturnType();
135 assertNotNull(ifcIdPropType);
136 assertEquals("java.lang.String", ifcIdPropType.getFullyQualifiedName());
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")) {
146 } else if (method.getName().equals("getHigherLayerIf")) {
147 getHigherLayerIf = method;
150 assertNotNull(getIfcKey);
151 Type getIfcKeyType = getIfcKey.getReturnType();
152 assertNotNull(getIfcKeyType);
153 assertNotSame("java.lang.Void", getIfcKeyType);
154 assertEquals("InterfaceKey", getIfcKeyType.getName());
156 assertNotNull(getHigherLayerIf);
157 Type getHigherLayerIfType = getHigherLayerIf.getReturnType();
158 assertNotNull(getHigherLayerIfType);
159 assertNotSame("java.lang.Void", getHigherLayerIfType);
160 assertEquals("List", getHigherLayerIfType.getName());
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")) {
171 assertNotNull(getIfc);
172 Type getIfcType = getIfc.getReturnType();
173 assertNotNull(getIfcType);
174 assertNotSame("java.lang.Void", getIfcType);
175 assertEquals("String", getIfcType.getName());
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;
186 assertNotNull(getIdSource);
187 Type getIdType = getIdSource.getReturnType();
188 assertNotNull(getIdType);
189 assertNotSame("java.lang.Void", getIdType);
190 assertEquals("Uri", getIdType.getName());
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")) {
201 assertNotNull(getIdDest);
202 Type getIdDestType = getIdDest.getReturnType();
203 assertNotNull(getIdDestType);
204 assertNotSame("java.lang.Void", getIdDestType);
205 assertEquals("Uri", getIdDestType.getName());
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;
216 assertNotNull(getTunnelKey);
217 Type getTunnelKeyType = getTunnelKey.getReturnType();
218 assertNotNull(getTunnelKeyType);
219 assertNotSame("java.lang.Void", getTunnelKeyType);
220 assertEquals("TunnelKey", getTunnelKeyType.getName());
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")) {
231 assertNotNull(tunnelId);
232 Type tunnelIdType = tunnelId.getReturnType();
233 assertNotNull(tunnelIdType);
234 assertNotSame("java.lang.Void", tunnelIdType);
235 assertEquals("Uri", tunnelIdType.getName());
239 public void testLeafrefInvalidPathResolving() throws URISyntaxException {
240 final URI resource = getClass().getResource("/leafref-test-invalid-model/foo.yang").toURI();
241 assertNotNull(resource);
243 final SchemaContext context = resolveSchemaContextFromFiles(resource);
244 assertNotNull(context);
245 assertEquals(1, context.getModules().size());
247 final BindingGenerator bindingGen = new BindingGeneratorImpl();
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());