ec18861b0fd6ead8fa962af0cc707b22f6074f63
[mdsal.git] / binding / mdsal-binding-generator-impl / src / test / java / org / opendaylight / mdsal / binding / generator / impl / GeneratedTypesLeafrefTest.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.mdsal.binding.generator.impl;
9
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;
15
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.SchemaContext;
25 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
26
27 public class GeneratedTypesLeafrefTest {
28
29     @Test
30     public void testLeafrefResolving() {
31         final SchemaContext 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         assertNotNull(context);
35         assertEquals(4, context.getModules().size());
36
37         final List<Type> genTypes = DefaultBindingGenerator.generateFor(context);
38
39         assertEquals(54, genTypes.size());
40         assertNotNull(genTypes);
41
42         GeneratedTransferObject gtIfcKey = null;
43         GeneratedType gtIfc = null;
44         GeneratedType gtNetworkLink = null;
45         GeneratedType gtSource = null;
46         GeneratedType gtDest = null;
47         GeneratedType gtTunnel = null;
48         GeneratedTransferObject gtTunnelKey = null;
49         GeneratedType gtTopology = null;
50         for (final Type type : genTypes) {
51             String name = type.getName();
52             if ("InterfaceKey".equals(name)
53                     && "org.opendaylight.yang.gen.v1.urn.model._abstract.topology.rev130208.topology.interfaces".equals(
54                         type.getPackageName())) {
55                 gtIfcKey = (GeneratedTransferObject) type;
56             } else if ("Interface".equals(name)) {
57                 gtIfc = (GeneratedType) type;
58             } else if ("NetworkLink".equals(name)) {
59                 gtNetworkLink = (GeneratedType) type;
60             } else if ("SourceNode".equals(name)) {
61                 gtSource = (GeneratedType) type;
62             } else if ("DestinationNode".equals(name)) {
63                 gtDest = (GeneratedType) type;
64             } else if ("Tunnel".equals(name)) {
65                 gtTunnel = (GeneratedType) type;
66             } else if ("TunnelKey".equals(name)) {
67                 gtTunnelKey = (GeneratedTransferObject) type;
68             } else if ("Topology".equals(name)) {
69                 gtTopology = (GeneratedType) type;
70             }
71         }
72
73         assertNotNull(gtIfcKey);
74         assertNotNull(gtIfc);
75         assertNotNull(gtNetworkLink);
76         assertNotNull(gtSource);
77         assertNotNull(gtDest);
78         assertNotNull(gtTunnel);
79         assertNotNull(gtTunnelKey);
80         assertNotNull(gtTopology);
81
82         // Topology
83         final List<MethodSignature> gtTopoMethods = gtTopology.getMethodDefinitions();
84         assertNotNull(gtTopoMethods);
85         MethodSignature condLeafref = null;
86         for (final MethodSignature method : gtTopoMethods) {
87             if (method.getName().equals("getCondLeafref")) {
88                 condLeafref = method;
89             }
90         }
91         assertNotNull(condLeafref);
92         Type condLeafRT = condLeafref.getReturnType();
93         assertNotNull(condLeafRT);
94         assertEquals("java.lang.Object", condLeafRT.getFullyQualifiedName());
95
96         // InterfaceId
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;
103             }
104         }
105         assertNotNull(ifcIdProp);
106         Type ifcIdPropType = ifcIdProp.getReturnType();
107         assertNotNull(ifcIdPropType);
108         assertEquals("java.lang.String", ifcIdPropType.getFullyQualifiedName());
109
110         // Interface
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:
118                     getIfcKey = method;
119                     break;
120                 case "getHigherLayerIf":
121                     getHigherLayerIf = method;
122                     break;
123                 default:
124             }
125         }
126         assertNotNull(getIfcKey);
127         Type getIfcKeyType = getIfcKey.getReturnType();
128         assertNotNull(getIfcKeyType);
129         assertNotSame("java.lang.Void", getIfcKeyType);
130         assertEquals("InterfaceKey", getIfcKeyType.getName());
131
132         assertNotNull(getHigherLayerIf);
133         Type getHigherLayerIfType = getHigherLayerIf.getReturnType();
134         assertNotNull(getHigherLayerIfType);
135         assertNotSame("java.lang.Void", getHigherLayerIfType);
136         assertEquals("List", getHigherLayerIfType.getName());
137
138         // NetworkLink
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")) {
144                 getIfc = method;
145             }
146         }
147         assertNotNull(getIfc);
148         Type getIfcType = getIfc.getReturnType();
149         assertNotNull(getIfcType);
150         assertNotSame("java.lang.Void", getIfcType);
151         assertEquals("String", getIfcType.getName());
152
153         // SourceNode
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;
160             }
161         }
162         assertNotNull(getIdSource);
163         Type getIdType = getIdSource.getReturnType();
164         assertNotNull(getIdType);
165         assertNotSame("java.lang.Void", getIdType);
166         assertEquals("Uri", getIdType.getName());
167
168         // DestinationNode
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")) {
174                 getIdDest = method;
175             }
176         }
177         assertNotNull(getIdDest);
178         Type getIdDestType = getIdDest.getReturnType();
179         assertNotNull(getIdDestType);
180         assertNotSame("java.lang.Void", getIdDestType);
181         assertEquals("Uri", getIdDestType.getName());
182
183         // Tunnel
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;
190             }
191         }
192         assertNotNull(getTunnelKey);
193         Type getTunnelKeyType = getTunnelKey.getReturnType();
194         assertNotNull(getTunnelKeyType);
195         assertNotSame("java.lang.Void", getTunnelKeyType);
196         assertEquals("TunnelKey", getTunnelKeyType.getName());
197
198         // TunnelKey
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")) {
204                 tunnelId = property;
205             }
206         }
207         assertNotNull(tunnelId);
208         Type tunnelIdType = tunnelId.getReturnType();
209         assertNotNull(tunnelIdType);
210         assertNotSame("java.lang.Void", tunnelIdType);
211         assertEquals("Uri", tunnelIdType.getName());
212     }
213
214     @Test
215     public void testLeafrefInvalidPathResolving() {
216         final SchemaContext context =  YangParserTestUtils.parseYangResource("/leafref-test-invalid-model/foo.yang");
217         assertNotNull(context);
218         assertEquals(1, context.getModules().size());
219
220         try {
221             DefaultBindingGenerator.generateFor(context);
222             fail("Expected IllegalArgumentException caused by invalid leafref path");
223         } catch (IllegalArgumentException e) {
224             String expected = "Failed to find leafref target";
225             assertTrue(e.getMessage().contains(expected));
226         }
227     }
228 }