Do not generate union builders
[mdsal.git] / binding / mdsal-binding-generator / 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.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;
16
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;
27
28 public class GeneratedTypesLeafrefTest {
29
30     @Test
31     public void testLeafrefResolving() {
32         final var 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());
36
37         final var genTypes = DefaultBindingGenerator.generateFor(context);
38         assertEquals(53, genTypes.size());
39
40         GeneratedTransferObject gtIfcKey = null;
41         GeneratedType gtIfc = null;
42         GeneratedType gtNetworkLink = null;
43         GeneratedType gtSource = null;
44         GeneratedType gtDest = null;
45         GeneratedType gtTunnel = null;
46         GeneratedTransferObject gtTunnelKey = null;
47         GeneratedType gtTopology = null;
48         for (final Type type : genTypes) {
49             String name = type.getName();
50             if ("InterfaceKey".equals(name)
51                     && "org.opendaylight.yang.gen.v1.urn.model._abstract.topology.rev130208.topology.interfaces".equals(
52                         type.getPackageName())) {
53                 gtIfcKey = (GeneratedTransferObject) type;
54             } else if ("Interface".equals(name)) {
55                 gtIfc = (GeneratedType) type;
56             } else if ("NetworkLink".equals(name)) {
57                 gtNetworkLink = (GeneratedType) type;
58             } else if ("SourceNode".equals(name)) {
59                 gtSource = (GeneratedType) type;
60             } else if ("DestinationNode".equals(name)) {
61                 gtDest = (GeneratedType) type;
62             } else if ("Tunnel".equals(name)) {
63                 gtTunnel = (GeneratedType) type;
64             } else if ("TunnelKey".equals(name)) {
65                 gtTunnelKey = (GeneratedTransferObject) type;
66             } else if ("Topology".equals(name)) {
67                 gtTopology = (GeneratedType) type;
68             }
69         }
70
71         assertNotNull(gtIfcKey);
72         assertNotNull(gtIfc);
73         assertNotNull(gtNetworkLink);
74         assertNotNull(gtSource);
75         assertNotNull(gtDest);
76         assertNotNull(gtTunnel);
77         assertNotNull(gtTunnelKey);
78         assertNotNull(gtTopology);
79
80         // Topology
81         final List<MethodSignature> gtTopoMethods = gtTopology.getMethodDefinitions();
82         assertNotNull(gtTopoMethods);
83         MethodSignature condLeafref = null;
84         for (final MethodSignature method : gtTopoMethods) {
85             if (method.getName().equals("getCondLeafref")) {
86                 condLeafref = method;
87             }
88         }
89         assertNotNull(condLeafref);
90         Type condLeafRT = condLeafref.getReturnType();
91         assertNotNull(condLeafRT);
92         assertEquals("org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Uri",
93             condLeafRT.getFullyQualifiedName());
94
95         // InterfaceId
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;
102             }
103         }
104         assertNotNull(ifcIdProp);
105         Type ifcIdPropType = ifcIdProp.getReturnType();
106         assertNotNull(ifcIdPropType);
107         assertEquals("java.lang.String", ifcIdPropType.getFullyQualifiedName());
108
109         // Interface
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:
117                     getIfcKey = method;
118                     break;
119                 case "getHigherLayerIf":
120                     getHigherLayerIf = method;
121                     break;
122                 default:
123             }
124         }
125         assertNotNull(getIfcKey);
126         Type getIfcKeyType = getIfcKey.getReturnType();
127         assertNotNull(getIfcKeyType);
128         assertNotSame("java.lang.Void", getIfcKeyType);
129         assertEquals("InterfaceKey", getIfcKeyType.getName());
130
131         assertNotNull(getHigherLayerIf);
132         Type getHigherLayerIfType = getHigherLayerIf.getReturnType();
133         assertNotNull(getHigherLayerIfType);
134         assertNotSame("java.lang.Void", getHigherLayerIfType);
135         assertEquals("Set", getHigherLayerIfType.getName());
136
137         // NetworkLink
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")) {
143                 getIfc = method;
144             }
145         }
146         assertNotNull(getIfc);
147         Type getIfcType = getIfc.getReturnType();
148         assertNotNull(getIfcType);
149         assertNotSame("java.lang.Void", getIfcType);
150         assertEquals("String", getIfcType.getName());
151
152         // SourceNode
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;
159             }
160         }
161         assertNotNull(getIdSource);
162         Type getIdType = getIdSource.getReturnType();
163         assertNotNull(getIdType);
164         assertNotSame("java.lang.Void", getIdType);
165         assertEquals("Uri", getIdType.getName());
166
167         // DestinationNode
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")) {
173                 getIdDest = method;
174             }
175         }
176         assertNotNull(getIdDest);
177         Type getIdDestType = getIdDest.getReturnType();
178         assertNotNull(getIdDestType);
179         assertNotSame("java.lang.Void", getIdDestType);
180         assertEquals("Uri", getIdDestType.getName());
181
182         // Tunnel
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;
189             }
190         }
191         assertNotNull(getTunnelKey);
192         Type getTunnelKeyType = getTunnelKey.getReturnType();
193         assertNotNull(getTunnelKeyType);
194         assertNotSame("java.lang.Void", getTunnelKeyType);
195         assertEquals("TunnelKey", getTunnelKeyType.getName());
196
197         // TunnelKey
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")) {
203                 tunnelId = property;
204             }
205         }
206         assertNotNull(tunnelId);
207         Type tunnelIdType = tunnelId.getReturnType();
208         assertNotNull(tunnelIdType);
209         assertNotSame("java.lang.Void", tunnelIdType);
210         assertEquals("Uri", tunnelIdType.getName());
211     }
212
213     @Test
214     public void testLeafrefInvalidPathResolving() {
215         final EffectiveModelContext context =  YangParserTestUtils.parseYangResource(
216             "/leafref-test-invalid-model/foo.yang");
217         assertEquals(1, context.getModules().size());
218
219         IllegalArgumentException ex = assertThrows(IllegalArgumentException.class,
220             () -> DefaultBindingGenerator.generateFor(context));
221         assertThat(ex.getMessage(), containsString("Failed to find leafref target"));
222     }
223 }