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;
16 import java.util.List;
17 import org.junit.Test;
18 import org.opendaylight.mdsal.binding.model.api.GeneratedProperty;
19 import org.opendaylight.mdsal.binding.model.api.GeneratedTransferObject;
20 import org.opendaylight.mdsal.binding.model.api.GeneratedType;
21 import org.opendaylight.mdsal.binding.model.api.MethodSignature;
22 import org.opendaylight.mdsal.binding.model.api.Type;
23 import org.opendaylight.mdsal.binding.spec.naming.BindingMapping;
24 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
25 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
27 public class GeneratedTypesLeafrefTest {
30 public void testLeafrefResolving() {
31 final EffectiveModelContext context = YangParserTestUtils.parseYangResources(GeneratedTypesLeafrefTest.class,
32 "/leafref-test-models/abstract-topology@2013-02-08.yang", "/ietf/ietf-interfaces.yang",
33 "/ietf/ietf-inet-types.yang", "/ietf/ietf-yang-types.yang");
34 assertEquals(4, context.getModules().size());
36 final List<Type> genTypes = DefaultBindingGenerator.generateFor(context);
38 assertEquals(54, genTypes.size());
39 assertNotNull(genTypes);
41 GeneratedTransferObject gtIfcKey = null;
42 GeneratedType gtIfc = null;
43 GeneratedType gtNetworkLink = null;
44 GeneratedType gtSource = null;
45 GeneratedType gtDest = null;
46 GeneratedType gtTunnel = null;
47 GeneratedTransferObject gtTunnelKey = null;
48 GeneratedType gtTopology = null;
49 for (final Type type : genTypes) {
50 String name = type.getName();
51 if ("InterfaceKey".equals(name)
52 && "org.opendaylight.yang.gen.v1.urn.model._abstract.topology.rev130208.topology.interfaces".equals(
53 type.getPackageName())) {
54 gtIfcKey = (GeneratedTransferObject) type;
55 } else if ("Interface".equals(name)) {
56 gtIfc = (GeneratedType) type;
57 } else if ("NetworkLink".equals(name)) {
58 gtNetworkLink = (GeneratedType) type;
59 } else if ("SourceNode".equals(name)) {
60 gtSource = (GeneratedType) type;
61 } else if ("DestinationNode".equals(name)) {
62 gtDest = (GeneratedType) type;
63 } else if ("Tunnel".equals(name)) {
64 gtTunnel = (GeneratedType) type;
65 } else if ("TunnelKey".equals(name)) {
66 gtTunnelKey = (GeneratedTransferObject) type;
67 } else if ("Topology".equals(name)) {
68 gtTopology = (GeneratedType) type;
72 assertNotNull(gtIfcKey);
74 assertNotNull(gtNetworkLink);
75 assertNotNull(gtSource);
76 assertNotNull(gtDest);
77 assertNotNull(gtTunnel);
78 assertNotNull(gtTunnelKey);
79 assertNotNull(gtTopology);
82 final List<MethodSignature> gtTopoMethods = gtTopology.getMethodDefinitions();
83 assertNotNull(gtTopoMethods);
84 MethodSignature condLeafref = null;
85 for (final MethodSignature method : gtTopoMethods) {
86 if (method.getName().equals("getCondLeafref")) {
90 assertNotNull(condLeafref);
91 Type condLeafRT = condLeafref.getReturnType();
92 assertNotNull(condLeafRT);
93 assertEquals("java.lang.Object", condLeafRT.getFullyQualifiedName());
96 final List<GeneratedProperty> gtIfcKeyProps = gtIfcKey.getProperties();
97 assertNotNull(gtIfcKeyProps);
98 GeneratedProperty ifcIdProp = null;
99 for (final GeneratedProperty property : gtIfcKeyProps) {
100 if (property.getName().equals("interfaceId")) {
101 ifcIdProp = property;
104 assertNotNull(ifcIdProp);
105 Type ifcIdPropType = ifcIdProp.getReturnType();
106 assertNotNull(ifcIdPropType);
107 assertEquals("java.lang.String", ifcIdPropType.getFullyQualifiedName());
110 final List<MethodSignature> gtIfcMethods = gtIfc.getMethodDefinitions();
111 assertNotNull(gtIfcMethods);
112 MethodSignature getIfcKey = null;
113 MethodSignature getHigherLayerIf = null;
114 for (final MethodSignature method : gtIfcMethods) {
115 switch (method.getName()) {
116 case BindingMapping.IDENTIFIABLE_KEY_NAME:
119 case "getHigherLayerIf":
120 getHigherLayerIf = method;
125 assertNotNull(getIfcKey);
126 Type getIfcKeyType = getIfcKey.getReturnType();
127 assertNotNull(getIfcKeyType);
128 assertNotSame("java.lang.Void", getIfcKeyType);
129 assertEquals("InterfaceKey", getIfcKeyType.getName());
131 assertNotNull(getHigherLayerIf);
132 Type getHigherLayerIfType = getHigherLayerIf.getReturnType();
133 assertNotNull(getHigherLayerIfType);
134 assertNotSame("java.lang.Void", getHigherLayerIfType);
135 assertEquals("List", getHigherLayerIfType.getName());
138 final List<MethodSignature> gtNetworkLinkMethods = gtNetworkLink.getMethodDefinitions();
139 assertNotNull(gtNetworkLinkMethods);
140 MethodSignature getIfc = null;
141 for (MethodSignature method : gtNetworkLinkMethods) {
142 if (method.getName().equals("getInterface")) {
146 assertNotNull(getIfc);
147 Type getIfcType = getIfc.getReturnType();
148 assertNotNull(getIfcType);
149 assertNotSame("java.lang.Void", getIfcType);
150 assertEquals("String", getIfcType.getName());
153 final List<MethodSignature> gtSourceMethods = gtSource.getMethodDefinitions();
154 assertNotNull(gtSourceMethods);
155 MethodSignature getIdSource = null;
156 for (MethodSignature method : gtSourceMethods) {
157 if (method.getName().equals("getId")) {
158 getIdSource = method;
161 assertNotNull(getIdSource);
162 Type getIdType = getIdSource.getReturnType();
163 assertNotNull(getIdType);
164 assertNotSame("java.lang.Void", getIdType);
165 assertEquals("Uri", getIdType.getName());
168 final List<MethodSignature> gtDestMethods = gtDest.getMethodDefinitions();
169 assertNotNull(gtDestMethods);
170 MethodSignature getIdDest = null;
171 for (MethodSignature method : gtDestMethods) {
172 if (method.getName().equals("getId")) {
176 assertNotNull(getIdDest);
177 Type getIdDestType = getIdDest.getReturnType();
178 assertNotNull(getIdDestType);
179 assertNotSame("java.lang.Void", getIdDestType);
180 assertEquals("Uri", getIdDestType.getName());
183 final List<MethodSignature> gtTunnelMethods = gtTunnel.getMethodDefinitions();
184 assertNotNull(gtTunnelMethods);
185 MethodSignature getTunnelKey = null;
186 for (MethodSignature method : gtTunnelMethods) {
187 if (BindingMapping.IDENTIFIABLE_KEY_NAME.equals(method.getName())) {
188 getTunnelKey = method;
191 assertNotNull(getTunnelKey);
192 Type getTunnelKeyType = getTunnelKey.getReturnType();
193 assertNotNull(getTunnelKeyType);
194 assertNotSame("java.lang.Void", getTunnelKeyType);
195 assertEquals("TunnelKey", getTunnelKeyType.getName());
198 final List<GeneratedProperty> gtTunnelKeyProps = gtTunnelKey.getProperties();
199 assertNotNull(gtTunnelKeyProps);
200 GeneratedProperty tunnelId = null;
201 for (final GeneratedProperty property : gtTunnelKeyProps) {
202 if (property.getName().equals("tunnelId")) {
206 assertNotNull(tunnelId);
207 Type tunnelIdType = tunnelId.getReturnType();
208 assertNotNull(tunnelIdType);
209 assertNotSame("java.lang.Void", tunnelIdType);
210 assertEquals("Uri", tunnelIdType.getName());
214 public void testLeafrefInvalidPathResolving() {
215 final EffectiveModelContext context = YangParserTestUtils.parseYangResource(
216 "/leafref-test-invalid-model/foo.yang");
217 assertEquals(1, context.getModules().size());
220 DefaultBindingGenerator.generateFor(context);
221 fail("Expected IllegalArgumentException caused by invalid leafref path");
222 } catch (IllegalArgumentException e) {
223 String expected = "Failed to find leafref target";
224 assertTrue(e.getMessage().contains(expected));