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.yangtools.sal.binding.generator.api.BindingGenerator;
23 import org.opendaylight.yangtools.sal.binding.model.api.GeneratedProperty;
24 import org.opendaylight.yangtools.sal.binding.model.api.GeneratedTransferObject;
25 import org.opendaylight.yangtools.sal.binding.model.api.GeneratedType;
26 import org.opendaylight.yangtools.sal.binding.model.api.MethodSignature;
27 import org.opendaylight.yangtools.sal.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;
32 public class GeneratedTypesLeafrefTest {
35 public void testLeafrefResolving() throws URISyntaxException, IOException, SourceException, ReactorException {
36 File abstractTopology = new File(getClass().getResource(
37 "/leafref-test-models/abstract-topology@2013-02-08.yang").toURI());
38 File ietfInterfaces = new File(getClass().getResource("/ietf/ietf-interfaces.yang").toURI());
39 File ietfInetTypes = new File(getClass().getResource("/ietf/ietf-inet-types.yang").toURI());
40 File ietfYangTypes = new File(getClass().getResource("/ietf/ietf-yang-types.yang").toURI());
42 final SchemaContext context = RetestUtils.parseYangSources(abstractTopology, ietfInterfaces,
43 ietfInetTypes, ietfYangTypes);
44 assertNotNull(context);
45 assertEquals(4, context.getModules().size());
47 final BindingGenerator bindingGen = new BindingGeneratorImpl(true);
48 final List<Type> genTypes = bindingGen.generateTypes(context);
50 assertEquals(54, genTypes.size());
51 assertNotNull(genTypes);
53 GeneratedTransferObject gtIfcKey = null;
54 GeneratedType gtIfc = null;
55 GeneratedType gtNetworkLink = null;
56 GeneratedType gtSource = null;
57 GeneratedType gtDest = null;
58 GeneratedType gtTunnel = null;
59 GeneratedTransferObject gtTunnelKey = null;
60 GeneratedType gtTopology = null;
61 for (final Type type : genTypes) {
62 String name = type.getName();
63 if ("InterfaceKey".equals(name)) {
64 gtIfcKey = (GeneratedTransferObject) type;
65 } else if ("Interface".equals(name)) {
66 gtIfc = (GeneratedType) type;
67 } else if ("NetworkLink".equals(name)) {
68 gtNetworkLink = (GeneratedType) type;
69 } else if ("SourceNode".equals(name)) {
70 gtSource = (GeneratedType) type;
71 } else if ("DestinationNode".equals(name)) {
72 gtDest = (GeneratedType) type;
73 } else if ("Tunnel".equals(name)) {
74 gtTunnel = (GeneratedType) type;
75 } else if ("TunnelKey".equals(name)) {
76 gtTunnelKey = (GeneratedTransferObject) type;
77 } else if ("Topology".equals(name)) {
78 gtTopology = (GeneratedType) type;
82 assertNotNull(gtIfcKey);
84 assertNotNull(gtNetworkLink);
85 assertNotNull(gtSource);
86 assertNotNull(gtDest);
87 assertNotNull(gtTunnel);
88 assertNotNull(gtTunnelKey);
89 assertNotNull(gtTopology);
92 final List<MethodSignature> gtTopoMethods = gtTopology.getMethodDefinitions();
93 assertNotNull(gtTopoMethods);
94 MethodSignature condLeafref = null;
95 for (final MethodSignature method : gtTopoMethods) {
96 if (method.getName().equals("getCondLeafref")) {
100 assertNotNull(condLeafref);
101 Type condLeafRT = condLeafref.getReturnType();
102 assertNotNull(condLeafRT);
103 assertEquals("java.lang.Object", condLeafRT.getFullyQualifiedName());
106 final List<GeneratedProperty> gtIfcKeyProps = gtIfcKey.getProperties();
107 assertNotNull(gtIfcKeyProps);
108 GeneratedProperty ifcIdProp = null;
109 for (final GeneratedProperty property : gtIfcKeyProps) {
110 if (property.getName().equals("interfaceId")) {
111 ifcIdProp = property;
114 assertNotNull(ifcIdProp);
115 Type ifcIdPropType = ifcIdProp.getReturnType();
116 assertNotNull(ifcIdPropType);
117 assertEquals("java.lang.String", ifcIdPropType.getFullyQualifiedName());
120 final List<MethodSignature> gtIfcMethods = gtIfc.getMethodDefinitions();
121 assertNotNull(gtIfcMethods);
122 MethodSignature getIfcKey = null;
123 MethodSignature getHigherLayerIf = null;
124 for (final MethodSignature method : gtIfcMethods) {
125 if (method.getName().equals("getKey")) {
127 } else if (method.getName().equals("getHigherLayerIf")) {
128 getHigherLayerIf = method;
131 assertNotNull(getIfcKey);
132 Type getIfcKeyType = getIfcKey.getReturnType();
133 assertNotNull(getIfcKeyType);
134 assertNotSame("java.lang.Void", getIfcKeyType);
135 assertEquals("InterfaceKey", getIfcKeyType.getName());
137 assertNotNull(getHigherLayerIf);
138 Type getHigherLayerIfType = getHigherLayerIf.getReturnType();
139 assertNotNull(getHigherLayerIfType);
140 assertNotSame("java.lang.Void", getHigherLayerIfType);
141 assertEquals("List", getHigherLayerIfType.getName());
144 final List<MethodSignature> gtNetworkLinkMethods = gtNetworkLink.getMethodDefinitions();
145 assertNotNull(gtNetworkLinkMethods);
146 MethodSignature getIfc = null;
147 for (MethodSignature method : gtNetworkLinkMethods) {
148 if (method.getName().equals("getInterface")) {
152 assertNotNull(getIfc);
153 Type getIfcType = getIfc.getReturnType();
154 assertNotNull(getIfcType);
155 assertNotSame("java.lang.Void", getIfcType);
156 assertEquals("String", getIfcType.getName());
159 final List<MethodSignature> gtSourceMethods = gtSource.getMethodDefinitions();
160 assertNotNull(gtSourceMethods);
161 MethodSignature getIdSource = null;
162 for (MethodSignature method : gtSourceMethods) {
163 if (method.getName().equals("getId")) {
164 getIdSource = method;
167 assertNotNull(getIdSource);
168 Type getIdType = getIdSource.getReturnType();
169 assertNotNull(getIdType);
170 assertNotSame("java.lang.Void", getIdType);
171 assertEquals("Uri", getIdType.getName());
174 final List<MethodSignature> gtDestMethods = gtDest.getMethodDefinitions();
175 assertNotNull(gtDestMethods);
176 MethodSignature getIdDest = null;
177 for (MethodSignature method : gtDestMethods) {
178 if (method.getName().equals("getId")) {
182 assertNotNull(getIdDest);
183 Type getIdDestType = getIdDest.getReturnType();
184 assertNotNull(getIdDestType);
185 assertNotSame("java.lang.Void", getIdDestType);
186 assertEquals("Uri", getIdDestType.getName());
189 final List<MethodSignature> gtTunnelMethods = gtTunnel.getMethodDefinitions();
190 assertNotNull(gtTunnelMethods);
191 MethodSignature getTunnelKey = null;
192 for (MethodSignature method : gtTunnelMethods) {
193 if (method.getName().equals("getKey")) {
194 getTunnelKey = method;
197 assertNotNull(getTunnelKey);
198 Type getTunnelKeyType = getTunnelKey.getReturnType();
199 assertNotNull(getTunnelKeyType);
200 assertNotSame("java.lang.Void", getTunnelKeyType);
201 assertEquals("TunnelKey", getTunnelKeyType.getName());
204 final List<GeneratedProperty> gtTunnelKeyProps = gtTunnelKey.getProperties();
205 assertNotNull(gtTunnelKeyProps);
206 GeneratedProperty tunnelId = null;
207 for (final GeneratedProperty property : gtTunnelKeyProps) {
208 if (property.getName().equals("tunnelId")) {
212 assertNotNull(tunnelId);
213 Type tunnelIdType = tunnelId.getReturnType();
214 assertNotNull(tunnelIdType);
215 assertNotSame("java.lang.Void", tunnelIdType);
216 assertEquals("Uri", tunnelIdType.getName());
220 public void testLeafrefInvalidPathResolving() throws URISyntaxException, IOException, SourceException, ReactorException {
221 final URI resource = getClass().getResource("/leafref-test-invalid-model/foo.yang").toURI();
222 assertNotNull(resource);
224 final SchemaContext context = RetestUtils.parseYangSources(new File(resource));
225 assertNotNull(context);
226 assertEquals(1, context.getModules().size());
228 final BindingGenerator bindingGen = new BindingGeneratorImpl(true);
230 bindingGen.generateTypes(context);
231 fail("Expected IllegalArgumentException caused by invalid leafref path");
232 } catch (IllegalArgumentException e) {
233 String expected = "Failed to find leafref target";
234 assertTrue(e.getMessage().contains(expected));