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