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.generator.api.BindingGenerator;
19 import org.opendaylight.mdsal.binding.model.api.GeneratedProperty;
20 import org.opendaylight.mdsal.binding.model.api.GeneratedTransferObject;
21 import org.opendaylight.mdsal.binding.model.api.GeneratedType;
22 import org.opendaylight.mdsal.binding.model.api.MethodSignature;
23 import org.opendaylight.mdsal.binding.model.api.Type;
24 import org.opendaylight.mdsal.binding.spec.naming.BindingMapping;
25 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
26 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
28 public class GeneratedTypesLeafrefTest {
31 public void testLeafrefResolving() {
32 final SchemaContext context = YangParserTestUtils.parseYangResources(GeneratedTypesLeafrefTest.class,
33 "/leafref-test-models/abstract-topology@2013-02-08.yang", "/ietf/ietf-interfaces.yang",
34 "/ietf/ietf-inet-types.yang", "/ietf/ietf-yang-types.yang");
35 assertNotNull(context);
36 assertEquals(4, context.getModules().size());
38 final BindingGenerator bindingGen = new BindingGeneratorImpl();
39 final List<Type> genTypes = bindingGen.generateTypes(context);
41 assertEquals(54, genTypes.size());
42 assertNotNull(genTypes);
44 GeneratedTransferObject gtIfcKey = null;
45 GeneratedType gtIfc = null;
46 GeneratedType gtNetworkLink = null;
47 GeneratedType gtSource = null;
48 GeneratedType gtDest = null;
49 GeneratedType gtTunnel = null;
50 GeneratedTransferObject gtTunnelKey = null;
51 GeneratedType gtTopology = null;
52 for (final Type type : genTypes) {
53 String name = type.getName();
54 if ("InterfaceKey".equals(name)
55 && "org.opendaylight.yang.gen.v1.urn.model._abstract.topology.rev130208.topology.interfaces".equals(
56 type.getPackageName())) {
57 gtIfcKey = (GeneratedTransferObject) type;
58 } else if ("Interface".equals(name)) {
59 gtIfc = (GeneratedType) type;
60 } else if ("NetworkLink".equals(name)) {
61 gtNetworkLink = (GeneratedType) type;
62 } else if ("SourceNode".equals(name)) {
63 gtSource = (GeneratedType) type;
64 } else if ("DestinationNode".equals(name)) {
65 gtDest = (GeneratedType) type;
66 } else if ("Tunnel".equals(name)) {
67 gtTunnel = (GeneratedType) type;
68 } else if ("TunnelKey".equals(name)) {
69 gtTunnelKey = (GeneratedTransferObject) type;
70 } else if ("Topology".equals(name)) {
71 gtTopology = (GeneratedType) type;
75 assertNotNull(gtIfcKey);
77 assertNotNull(gtNetworkLink);
78 assertNotNull(gtSource);
79 assertNotNull(gtDest);
80 assertNotNull(gtTunnel);
81 assertNotNull(gtTunnelKey);
82 assertNotNull(gtTopology);
85 final List<MethodSignature> gtTopoMethods = gtTopology.getMethodDefinitions();
86 assertNotNull(gtTopoMethods);
87 MethodSignature condLeafref = null;
88 for (final MethodSignature method : gtTopoMethods) {
89 if (method.getName().equals("getCondLeafref")) {
93 assertNotNull(condLeafref);
94 Type condLeafRT = condLeafref.getReturnType();
95 assertNotNull(condLeafRT);
96 assertEquals("java.lang.Object", condLeafRT.getFullyQualifiedName());
99 final List<GeneratedProperty> gtIfcKeyProps = gtIfcKey.getProperties();
100 assertNotNull(gtIfcKeyProps);
101 GeneratedProperty ifcIdProp = null;
102 for (final GeneratedProperty property : gtIfcKeyProps) {
103 if (property.getName().equals("interfaceId")) {
104 ifcIdProp = property;
107 assertNotNull(ifcIdProp);
108 Type ifcIdPropType = ifcIdProp.getReturnType();
109 assertNotNull(ifcIdPropType);
110 assertEquals("java.lang.String", ifcIdPropType.getFullyQualifiedName());
113 final List<MethodSignature> gtIfcMethods = gtIfc.getMethodDefinitions();
114 assertNotNull(gtIfcMethods);
115 MethodSignature getIfcKey = null;
116 MethodSignature getHigherLayerIf = null;
117 for (final MethodSignature method : gtIfcMethods) {
118 switch (method.getName()) {
119 case BindingMapping.IDENTIFIABLE_KEY_NAME:
122 case "getHigherLayerIf":
123 getHigherLayerIf = method;
128 assertNotNull(getIfcKey);
129 Type getIfcKeyType = getIfcKey.getReturnType();
130 assertNotNull(getIfcKeyType);
131 assertNotSame("java.lang.Void", getIfcKeyType);
132 assertEquals("InterfaceKey", getIfcKeyType.getName());
134 assertNotNull(getHigherLayerIf);
135 Type getHigherLayerIfType = getHigherLayerIf.getReturnType();
136 assertNotNull(getHigherLayerIfType);
137 assertNotSame("java.lang.Void", getHigherLayerIfType);
138 assertEquals("List", getHigherLayerIfType.getName());
141 final List<MethodSignature> gtNetworkLinkMethods = gtNetworkLink.getMethodDefinitions();
142 assertNotNull(gtNetworkLinkMethods);
143 MethodSignature getIfc = null;
144 for (MethodSignature method : gtNetworkLinkMethods) {
145 if (method.getName().equals("getInterface")) {
149 assertNotNull(getIfc);
150 Type getIfcType = getIfc.getReturnType();
151 assertNotNull(getIfcType);
152 assertNotSame("java.lang.Void", getIfcType);
153 assertEquals("String", getIfcType.getName());
156 final List<MethodSignature> gtSourceMethods = gtSource.getMethodDefinitions();
157 assertNotNull(gtSourceMethods);
158 MethodSignature getIdSource = null;
159 for (MethodSignature method : gtSourceMethods) {
160 if (method.getName().equals("getId")) {
161 getIdSource = method;
164 assertNotNull(getIdSource);
165 Type getIdType = getIdSource.getReturnType();
166 assertNotNull(getIdType);
167 assertNotSame("java.lang.Void", getIdType);
168 assertEquals("Uri", getIdType.getName());
171 final List<MethodSignature> gtDestMethods = gtDest.getMethodDefinitions();
172 assertNotNull(gtDestMethods);
173 MethodSignature getIdDest = null;
174 for (MethodSignature method : gtDestMethods) {
175 if (method.getName().equals("getId")) {
179 assertNotNull(getIdDest);
180 Type getIdDestType = getIdDest.getReturnType();
181 assertNotNull(getIdDestType);
182 assertNotSame("java.lang.Void", getIdDestType);
183 assertEquals("Uri", getIdDestType.getName());
186 final List<MethodSignature> gtTunnelMethods = gtTunnel.getMethodDefinitions();
187 assertNotNull(gtTunnelMethods);
188 MethodSignature getTunnelKey = null;
189 for (MethodSignature method : gtTunnelMethods) {
190 if (BindingMapping.IDENTIFIABLE_KEY_NAME.equals(method.getName())) {
191 getTunnelKey = method;
194 assertNotNull(getTunnelKey);
195 Type getTunnelKeyType = getTunnelKey.getReturnType();
196 assertNotNull(getTunnelKeyType);
197 assertNotSame("java.lang.Void", getTunnelKeyType);
198 assertEquals("TunnelKey", getTunnelKeyType.getName());
201 final List<GeneratedProperty> gtTunnelKeyProps = gtTunnelKey.getProperties();
202 assertNotNull(gtTunnelKeyProps);
203 GeneratedProperty tunnelId = null;
204 for (final GeneratedProperty property : gtTunnelKeyProps) {
205 if (property.getName().equals("tunnelId")) {
209 assertNotNull(tunnelId);
210 Type tunnelIdType = tunnelId.getReturnType();
211 assertNotNull(tunnelIdType);
212 assertNotSame("java.lang.Void", tunnelIdType);
213 assertEquals("Uri", tunnelIdType.getName());
217 public void testLeafrefInvalidPathResolving() {
218 final SchemaContext context = YangParserTestUtils.parseYangResource("/leafref-test-invalid-model/foo.yang");
219 assertNotNull(context);
220 assertEquals(1, context.getModules().size());
222 final BindingGenerator bindingGen = new BindingGeneratorImpl();
224 bindingGen.generateTypes(context);
225 fail("Expected IllegalArgumentException caused by invalid leafref path");
226 } catch (IllegalArgumentException e) {
227 String expected = "Failed to find leafref target";
228 assertTrue(e.getMessage().contains(expected));