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.hamcrest.CoreMatchers.containsString;
11 import static org.hamcrest.MatcherAssert.assertThat;
12 import static org.junit.Assert.assertEquals;
13 import static org.junit.Assert.assertNotNull;
14 import static org.junit.Assert.assertNotSame;
15 import static org.junit.Assert.assertThrows;
17 import java.util.List;
18 import org.junit.Test;
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.EffectiveModelContext;
26 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
28 public class GeneratedTypesLeafrefTest {
31 public void testLeafrefResolving() {
32 final EffectiveModelContext context = YangParserTestUtils.parseYangResources(GeneratedTypesLeafrefTest.class,
33 "/leafref-test-models/abstract-topology@2013-02-08.yang", "/ietf-models/ietf-interfaces.yang",
34 "/ietf-models/ietf-inet-types.yang", "/ietf-models/ietf-yang-types.yang");
35 assertEquals(4, context.getModules().size());
37 final List<GeneratedType> genTypes = DefaultBindingGenerator.generateFor(context);
39 assertEquals(54, genTypes.size());
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("org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Uri",
94 condLeafRT.getFullyQualifiedName());
97 final List<GeneratedProperty> gtIfcKeyProps = gtIfcKey.getProperties();
98 assertNotNull(gtIfcKeyProps);
99 GeneratedProperty ifcIdProp = null;
100 for (final GeneratedProperty property : gtIfcKeyProps) {
101 if (property.getName().equals("interfaceId")) {
102 ifcIdProp = property;
105 assertNotNull(ifcIdProp);
106 Type ifcIdPropType = ifcIdProp.getReturnType();
107 assertNotNull(ifcIdPropType);
108 assertEquals("java.lang.String", ifcIdPropType.getFullyQualifiedName());
111 final List<MethodSignature> gtIfcMethods = gtIfc.getMethodDefinitions();
112 assertNotNull(gtIfcMethods);
113 MethodSignature getIfcKey = null;
114 MethodSignature getHigherLayerIf = null;
115 for (final MethodSignature method : gtIfcMethods) {
116 switch (method.getName()) {
117 case BindingMapping.IDENTIFIABLE_KEY_NAME:
120 case "getHigherLayerIf":
121 getHigherLayerIf = method;
126 assertNotNull(getIfcKey);
127 Type getIfcKeyType = getIfcKey.getReturnType();
128 assertNotNull(getIfcKeyType);
129 assertNotSame("java.lang.Void", getIfcKeyType);
130 assertEquals("InterfaceKey", getIfcKeyType.getName());
132 assertNotNull(getHigherLayerIf);
133 Type getHigherLayerIfType = getHigherLayerIf.getReturnType();
134 assertNotNull(getHigherLayerIfType);
135 assertNotSame("java.lang.Void", getHigherLayerIfType);
136 assertEquals("Set", getHigherLayerIfType.getName());
139 final List<MethodSignature> gtNetworkLinkMethods = gtNetworkLink.getMethodDefinitions();
140 assertNotNull(gtNetworkLinkMethods);
141 MethodSignature getIfc = null;
142 for (MethodSignature method : gtNetworkLinkMethods) {
143 if (method.getName().equals("getInterface")) {
147 assertNotNull(getIfc);
148 Type getIfcType = getIfc.getReturnType();
149 assertNotNull(getIfcType);
150 assertNotSame("java.lang.Void", getIfcType);
151 assertEquals("String", getIfcType.getName());
154 final List<MethodSignature> gtSourceMethods = gtSource.getMethodDefinitions();
155 assertNotNull(gtSourceMethods);
156 MethodSignature getIdSource = null;
157 for (MethodSignature method : gtSourceMethods) {
158 if (method.getName().equals("getId")) {
159 getIdSource = method;
162 assertNotNull(getIdSource);
163 Type getIdType = getIdSource.getReturnType();
164 assertNotNull(getIdType);
165 assertNotSame("java.lang.Void", getIdType);
166 assertEquals("Uri", getIdType.getName());
169 final List<MethodSignature> gtDestMethods = gtDest.getMethodDefinitions();
170 assertNotNull(gtDestMethods);
171 MethodSignature getIdDest = null;
172 for (MethodSignature method : gtDestMethods) {
173 if (method.getName().equals("getId")) {
177 assertNotNull(getIdDest);
178 Type getIdDestType = getIdDest.getReturnType();
179 assertNotNull(getIdDestType);
180 assertNotSame("java.lang.Void", getIdDestType);
181 assertEquals("Uri", getIdDestType.getName());
184 final List<MethodSignature> gtTunnelMethods = gtTunnel.getMethodDefinitions();
185 assertNotNull(gtTunnelMethods);
186 MethodSignature getTunnelKey = null;
187 for (MethodSignature method : gtTunnelMethods) {
188 if (BindingMapping.IDENTIFIABLE_KEY_NAME.equals(method.getName())) {
189 getTunnelKey = method;
192 assertNotNull(getTunnelKey);
193 Type getTunnelKeyType = getTunnelKey.getReturnType();
194 assertNotNull(getTunnelKeyType);
195 assertNotSame("java.lang.Void", getTunnelKeyType);
196 assertEquals("TunnelKey", getTunnelKeyType.getName());
199 final List<GeneratedProperty> gtTunnelKeyProps = gtTunnelKey.getProperties();
200 assertNotNull(gtTunnelKeyProps);
201 GeneratedProperty tunnelId = null;
202 for (final GeneratedProperty property : gtTunnelKeyProps) {
203 if (property.getName().equals("tunnelId")) {
207 assertNotNull(tunnelId);
208 Type tunnelIdType = tunnelId.getReturnType();
209 assertNotNull(tunnelIdType);
210 assertNotSame("java.lang.Void", tunnelIdType);
211 assertEquals("Uri", tunnelIdType.getName());
215 public void testLeafrefInvalidPathResolving() {
216 final EffectiveModelContext context = YangParserTestUtils.parseYangResource(
217 "/leafref-test-invalid-model/foo.yang");
218 assertEquals(1, context.getModules().size());
220 IllegalArgumentException ex = assertThrows(IllegalArgumentException.class,
221 () -> DefaultBindingGenerator.generateFor(context));
222 assertThat(ex.getMessage(), containsString("Failed to find leafref target"));