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.mdsal.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.generator.impl.BindingGeneratorImpl;
24 import org.opendaylight.mdsal.binding.model.api.GeneratedProperty;
25 import org.opendaylight.mdsal.binding.model.api.GeneratedTransferObject;
26 import org.opendaylight.mdsal.binding.model.api.GeneratedType;
27 import org.opendaylight.mdsal.binding.model.api.MethodSignature;
28 import org.opendaylight.mdsal.binding.model.api.Type;
29 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
30 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
31 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
32 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
34 public class GeneratedTypesLeafrefTest {
37 public void testLeafrefResolving() throws URISyntaxException, IOException, SourceException, ReactorException {
38 File abstractTopology = new File(getClass().getResource(
39 "/leafref-test-models/abstract-topology@2013-02-08.yang").toURI());
40 File ietfInterfaces = new File(getClass().getResource("/ietf/ietf-interfaces.yang").toURI());
41 File ietfInetTypes = new File(getClass().getResource("/ietf/ietf-inet-types.yang").toURI());
42 File ietfYangTypes = new File(getClass().getResource("/ietf/ietf-yang-types.yang").toURI());
44 final SchemaContext context = YangParserTestUtils.parseYangSources(abstractTopology, ietfInterfaces,
45 ietfInetTypes, ietfYangTypes);
46 assertNotNull(context);
47 assertEquals(4, context.getModules().size());
49 final BindingGenerator bindingGen = new BindingGeneratorImpl(true);
50 final List<Type> genTypes = bindingGen.generateTypes(context);
52 assertEquals(54, genTypes.size());
53 assertNotNull(genTypes);
55 GeneratedTransferObject gtIfcKey = null;
56 GeneratedType gtIfc = null;
57 GeneratedType gtNetworkLink = null;
58 GeneratedType gtSource = null;
59 GeneratedType gtDest = null;
60 GeneratedType gtTunnel = null;
61 GeneratedTransferObject gtTunnelKey = null;
62 GeneratedType gtTopology = null;
63 for (final Type type : genTypes) {
64 String name = type.getName();
65 if ("InterfaceKey".equals(name)) {
66 gtIfcKey = (GeneratedTransferObject) type;
67 } else if ("Interface".equals(name)) {
68 gtIfc = (GeneratedType) type;
69 } else if ("NetworkLink".equals(name)) {
70 gtNetworkLink = (GeneratedType) type;
71 } else if ("SourceNode".equals(name)) {
72 gtSource = (GeneratedType) type;
73 } else if ("DestinationNode".equals(name)) {
74 gtDest = (GeneratedType) type;
75 } else if ("Tunnel".equals(name)) {
76 gtTunnel = (GeneratedType) type;
77 } else if ("TunnelKey".equals(name)) {
78 gtTunnelKey = (GeneratedTransferObject) type;
79 } else if ("Topology".equals(name)) {
80 gtTopology = (GeneratedType) type;
84 assertNotNull(gtIfcKey);
86 assertNotNull(gtNetworkLink);
87 assertNotNull(gtSource);
88 assertNotNull(gtDest);
89 assertNotNull(gtTunnel);
90 assertNotNull(gtTunnelKey);
91 assertNotNull(gtTopology);
94 final List<MethodSignature> gtTopoMethods = gtTopology.getMethodDefinitions();
95 assertNotNull(gtTopoMethods);
96 MethodSignature condLeafref = null;
97 for (final MethodSignature method : gtTopoMethods) {
98 if (method.getName().equals("getCondLeafref")) {
102 assertNotNull(condLeafref);
103 Type condLeafRT = condLeafref.getReturnType();
104 assertNotNull(condLeafRT);
105 assertEquals("java.lang.Object", condLeafRT.getFullyQualifiedName());
108 final List<GeneratedProperty> gtIfcKeyProps = gtIfcKey.getProperties();
109 assertNotNull(gtIfcKeyProps);
110 GeneratedProperty ifcIdProp = null;
111 for (final GeneratedProperty property : gtIfcKeyProps) {
112 if (property.getName().equals("interfaceId")) {
113 ifcIdProp = property;
116 assertNotNull(ifcIdProp);
117 Type ifcIdPropType = ifcIdProp.getReturnType();
118 assertNotNull(ifcIdPropType);
119 assertEquals("java.lang.String", ifcIdPropType.getFullyQualifiedName());
122 final List<MethodSignature> gtIfcMethods = gtIfc.getMethodDefinitions();
123 assertNotNull(gtIfcMethods);
124 MethodSignature getIfcKey = null;
125 MethodSignature getHigherLayerIf = null;
126 for (final MethodSignature method : gtIfcMethods) {
127 if (method.getName().equals("getKey")) {
129 } else if (method.getName().equals("getHigherLayerIf")) {
130 getHigherLayerIf = method;
133 assertNotNull(getIfcKey);
134 Type getIfcKeyType = getIfcKey.getReturnType();
135 assertNotNull(getIfcKeyType);
136 assertNotSame("java.lang.Void", getIfcKeyType);
137 assertEquals("InterfaceKey", getIfcKeyType.getName());
139 assertNotNull(getHigherLayerIf);
140 Type getHigherLayerIfType = getHigherLayerIf.getReturnType();
141 assertNotNull(getHigherLayerIfType);
142 assertNotSame("java.lang.Void", getHigherLayerIfType);
143 assertEquals("List", getHigherLayerIfType.getName());
146 final List<MethodSignature> gtNetworkLinkMethods = gtNetworkLink.getMethodDefinitions();
147 assertNotNull(gtNetworkLinkMethods);
148 MethodSignature getIfc = null;
149 for (MethodSignature method : gtNetworkLinkMethods) {
150 if (method.getName().equals("getInterface")) {
154 assertNotNull(getIfc);
155 Type getIfcType = getIfc.getReturnType();
156 assertNotNull(getIfcType);
157 assertNotSame("java.lang.Void", getIfcType);
158 assertEquals("String", getIfcType.getName());
161 final List<MethodSignature> gtSourceMethods = gtSource.getMethodDefinitions();
162 assertNotNull(gtSourceMethods);
163 MethodSignature getIdSource = null;
164 for (MethodSignature method : gtSourceMethods) {
165 if (method.getName().equals("getId")) {
166 getIdSource = method;
169 assertNotNull(getIdSource);
170 Type getIdType = getIdSource.getReturnType();
171 assertNotNull(getIdType);
172 assertNotSame("java.lang.Void", getIdType);
173 assertEquals("Uri", getIdType.getName());
176 final List<MethodSignature> gtDestMethods = gtDest.getMethodDefinitions();
177 assertNotNull(gtDestMethods);
178 MethodSignature getIdDest = null;
179 for (MethodSignature method : gtDestMethods) {
180 if (method.getName().equals("getId")) {
184 assertNotNull(getIdDest);
185 Type getIdDestType = getIdDest.getReturnType();
186 assertNotNull(getIdDestType);
187 assertNotSame("java.lang.Void", getIdDestType);
188 assertEquals("Uri", getIdDestType.getName());
191 final List<MethodSignature> gtTunnelMethods = gtTunnel.getMethodDefinitions();
192 assertNotNull(gtTunnelMethods);
193 MethodSignature getTunnelKey = null;
194 for (MethodSignature method : gtTunnelMethods) {
195 if (method.getName().equals("getKey")) {
196 getTunnelKey = method;
199 assertNotNull(getTunnelKey);
200 Type getTunnelKeyType = getTunnelKey.getReturnType();
201 assertNotNull(getTunnelKeyType);
202 assertNotSame("java.lang.Void", getTunnelKeyType);
203 assertEquals("TunnelKey", getTunnelKeyType.getName());
206 final List<GeneratedProperty> gtTunnelKeyProps = gtTunnelKey.getProperties();
207 assertNotNull(gtTunnelKeyProps);
208 GeneratedProperty tunnelId = null;
209 for (final GeneratedProperty property : gtTunnelKeyProps) {
210 if (property.getName().equals("tunnelId")) {
214 assertNotNull(tunnelId);
215 Type tunnelIdType = tunnelId.getReturnType();
216 assertNotNull(tunnelIdType);
217 assertNotSame("java.lang.Void", tunnelIdType);
218 assertEquals("Uri", tunnelIdType.getName());
222 public void testLeafrefInvalidPathResolving() throws URISyntaxException, IOException, SourceException, ReactorException {
223 final URI resource = getClass().getResource("/leafref-test-invalid-model/foo.yang").toURI();
224 assertNotNull(resource);
226 final SchemaContext context = YangParserTestUtils.parseYangSources(new File(resource));
227 assertNotNull(context);
228 assertEquals(1, context.getModules().size());
230 final BindingGenerator bindingGen = new BindingGeneratorImpl(true);
232 bindingGen.generateTypes(context);
233 fail("Expected IllegalArgumentException caused by invalid leafref path");
234 } catch (IllegalArgumentException e) {
235 String expected = "Failed to find leafref target";
236 assertTrue(e.getMessage().contains(expected));