Map system-ordered leaf-lists to Set<T>
[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 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());
36
37         final List<GeneratedType> genTypes = DefaultBindingGenerator.generateFor(context);
38
39         assertEquals(54, genTypes.size());
40
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;
69             }
70         }
71
72         assertNotNull(gtIfcKey);
73         assertNotNull(gtIfc);
74         assertNotNull(gtNetworkLink);
75         assertNotNull(gtSource);
76         assertNotNull(gtDest);
77         assertNotNull(gtTunnel);
78         assertNotNull(gtTunnelKey);
79         assertNotNull(gtTopology);
80
81         // Topology
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")) {
87                 condLeafref = method;
88             }
89         }
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());
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("Set", 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 EffectiveModelContext context =  YangParserTestUtils.parseYangResource(
217             "/leafref-test-invalid-model/foo.yang");
218         assertEquals(1, context.getModules().size());
219
220         IllegalArgumentException ex = assertThrows(IllegalArgumentException.class,
221             () -> DefaultBindingGenerator.generateFor(context));
222         assertThat(ex.getMessage(), containsString("Failed to find leafref target"));
223     }
224 }