2 * Copyright (c) 2016 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.assertTrue;
14 import static org.junit.Assert.fail;
17 import java.io.IOException;
19 import java.net.URISyntaxException;
20 import java.util.List;
21 import org.junit.Test;
22 import org.opendaylight.mdsal.binding.generator.api.BindingGenerator;
23 import org.opendaylight.mdsal.binding.model.api.GeneratedProperty;
24 import org.opendaylight.mdsal.binding.model.api.GeneratedTransferObject;
25 import org.opendaylight.mdsal.binding.model.api.GeneratedType;
26 import org.opendaylight.mdsal.binding.model.api.MethodSignature;
27 import org.opendaylight.mdsal.binding.model.api.Type;
28 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
29 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
30 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
31 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
33 public class GeneratedTypesLeafrefTest {
36 public void testLeafrefResolving() throws URISyntaxException, IOException, SourceException, ReactorException {
37 File abstractTopology = new File(getClass().getResource(
38 "/leafref-test-models/abstract-topology@2013-02-08.yang").toURI());
39 File ietfInterfaces = new File(getClass().getResource("/ietf/ietf-interfaces.yang").toURI());
40 File ietfInetTypes = new File(getClass().getResource("/ietf/ietf-inet-types.yang").toURI());
41 File ietfYangTypes = new File(getClass().getResource("/ietf/ietf-yang-types.yang").toURI());
43 final SchemaContext context = YangParserTestUtils.parseYangSources(abstractTopology, ietfInterfaces,
44 ietfInetTypes, ietfYangTypes);
45 assertNotNull(context);
46 assertEquals(4, context.getModules().size());
48 final BindingGenerator bindingGen = new BindingGeneratorImpl(true);
49 final List<Type> genTypes = bindingGen.generateTypes(context);
51 assertEquals(54, genTypes.size());
52 assertNotNull(genTypes);
54 GeneratedTransferObject gtIfcKey = null;
55 GeneratedType gtIfc = null;
56 GeneratedType gtNetworkLink = null;
57 GeneratedType gtSource = null;
58 GeneratedType gtDest = null;
59 GeneratedType gtTunnel = null;
60 GeneratedTransferObject gtTunnelKey = null;
61 GeneratedType gtTopology = null;
62 for (final Type type : genTypes) {
63 String name = type.getName();
64 if ("InterfaceKey".equals(name)) {
65 gtIfcKey = (GeneratedTransferObject) type;
66 } else if ("Interface".equals(name)) {
67 gtIfc = (GeneratedType) type;
68 } else if ("NetworkLink".equals(name)) {
69 gtNetworkLink = (GeneratedType) type;
70 } else if ("SourceNode".equals(name)) {
71 gtSource = (GeneratedType) type;
72 } else if ("DestinationNode".equals(name)) {
73 gtDest = (GeneratedType) type;
74 } else if ("Tunnel".equals(name)) {
75 gtTunnel = (GeneratedType) type;
76 } else if ("TunnelKey".equals(name)) {
77 gtTunnelKey = (GeneratedTransferObject) type;
78 } else if ("Topology".equals(name)) {
79 gtTopology = (GeneratedType) type;
83 assertNotNull(gtIfcKey);
85 assertNotNull(gtNetworkLink);
86 assertNotNull(gtSource);
87 assertNotNull(gtDest);
88 assertNotNull(gtTunnel);
89 assertNotNull(gtTunnelKey);
90 assertNotNull(gtTopology);
93 final List<MethodSignature> gtTopoMethods = gtTopology.getMethodDefinitions();
94 assertNotNull(gtTopoMethods);
95 MethodSignature condLeafref = null;
96 for (final MethodSignature method : gtTopoMethods) {
97 if (method.getName().equals("getCondLeafref")) {
101 assertNotNull(condLeafref);
102 Type condLeafRT = condLeafref.getReturnType();
103 assertNotNull(condLeafRT);
104 assertEquals("java.lang.Object", condLeafRT.getFullyQualifiedName());
107 final List<GeneratedProperty> gtIfcKeyProps = gtIfcKey.getProperties();
108 assertNotNull(gtIfcKeyProps);
109 GeneratedProperty ifcIdProp = null;
110 for (final GeneratedProperty property : gtIfcKeyProps) {
111 if (property.getName().equals("interfaceId")) {
112 ifcIdProp = property;
115 assertNotNull(ifcIdProp);
116 Type ifcIdPropType = ifcIdProp.getReturnType();
117 assertNotNull(ifcIdPropType);
118 assertEquals("java.lang.String", ifcIdPropType.getFullyQualifiedName());
121 final List<MethodSignature> gtIfcMethods = gtIfc.getMethodDefinitions();
122 assertNotNull(gtIfcMethods);
123 MethodSignature getIfcKey = null;
124 MethodSignature getHigherLayerIf = null;
125 for (final MethodSignature method : gtIfcMethods) {
126 if (method.getName().equals("getKey")) {
128 } else if (method.getName().equals("getHigherLayerIf")) {
129 getHigherLayerIf = method;
132 assertNotNull(getIfcKey);
133 Type getIfcKeyType = getIfcKey.getReturnType();
134 assertNotNull(getIfcKeyType);
135 assertNotSame("java.lang.Void", getIfcKeyType);
136 assertEquals("InterfaceKey", getIfcKeyType.getName());
138 assertNotNull(getHigherLayerIf);
139 Type getHigherLayerIfType = getHigherLayerIf.getReturnType();
140 assertNotNull(getHigherLayerIfType);
141 assertNotSame("java.lang.Void", getHigherLayerIfType);
142 assertEquals("List", getHigherLayerIfType.getName());
145 final List<MethodSignature> gtNetworkLinkMethods = gtNetworkLink.getMethodDefinitions();
146 assertNotNull(gtNetworkLinkMethods);
147 MethodSignature getIfc = null;
148 for (MethodSignature method : gtNetworkLinkMethods) {
149 if (method.getName().equals("getInterface")) {
153 assertNotNull(getIfc);
154 Type getIfcType = getIfc.getReturnType();
155 assertNotNull(getIfcType);
156 assertNotSame("java.lang.Void", getIfcType);
157 assertEquals("String", getIfcType.getName());
160 final List<MethodSignature> gtSourceMethods = gtSource.getMethodDefinitions();
161 assertNotNull(gtSourceMethods);
162 MethodSignature getIdSource = null;
163 for (MethodSignature method : gtSourceMethods) {
164 if (method.getName().equals("getId")) {
165 getIdSource = method;
168 assertNotNull(getIdSource);
169 Type getIdType = getIdSource.getReturnType();
170 assertNotNull(getIdType);
171 assertNotSame("java.lang.Void", getIdType);
172 assertEquals("Uri", getIdType.getName());
175 final List<MethodSignature> gtDestMethods = gtDest.getMethodDefinitions();
176 assertNotNull(gtDestMethods);
177 MethodSignature getIdDest = null;
178 for (MethodSignature method : gtDestMethods) {
179 if (method.getName().equals("getId")) {
183 assertNotNull(getIdDest);
184 Type getIdDestType = getIdDest.getReturnType();
185 assertNotNull(getIdDestType);
186 assertNotSame("java.lang.Void", getIdDestType);
187 assertEquals("Uri", getIdDestType.getName());
190 final List<MethodSignature> gtTunnelMethods = gtTunnel.getMethodDefinitions();
191 assertNotNull(gtTunnelMethods);
192 MethodSignature getTunnelKey = null;
193 for (MethodSignature method : gtTunnelMethods) {
194 if (method.getName().equals("getKey")) {
195 getTunnelKey = method;
198 assertNotNull(getTunnelKey);
199 Type getTunnelKeyType = getTunnelKey.getReturnType();
200 assertNotNull(getTunnelKeyType);
201 assertNotSame("java.lang.Void", getTunnelKeyType);
202 assertEquals("TunnelKey", getTunnelKeyType.getName());
205 final List<GeneratedProperty> gtTunnelKeyProps = gtTunnelKey.getProperties();
206 assertNotNull(gtTunnelKeyProps);
207 GeneratedProperty tunnelId = null;
208 for (final GeneratedProperty property : gtTunnelKeyProps) {
209 if (property.getName().equals("tunnelId")) {
213 assertNotNull(tunnelId);
214 Type tunnelIdType = tunnelId.getReturnType();
215 assertNotNull(tunnelIdType);
216 assertNotSame("java.lang.Void", tunnelIdType);
217 assertEquals("Uri", tunnelIdType.getName());
221 public void testLeafrefInvalidPathResolving() throws URISyntaxException, IOException, SourceException, ReactorException {
222 final URI resource = getClass().getResource("/leafref-test-invalid-model/foo.yang").toURI();
223 assertNotNull(resource);
225 final SchemaContext context = YangParserTestUtils.parseYangSources(new File(resource));
226 assertNotNull(context);
227 assertEquals(1, context.getModules().size());
229 final BindingGenerator bindingGen = new BindingGeneratorImpl(true);
231 bindingGen.generateTypes(context);
232 fail("Expected IllegalArgumentException caused by invalid leafref path");
233 } catch (IllegalArgumentException e) {
234 String expected = "Failed to find leafref target";
235 assertTrue(e.getMessage().contains(expected));