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