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