Adding ITM unit testcases and syncing mdsalutil changes from vpnservice
[genius.git] / itm / itm-impl / src / test / java / org / opendaylight / genius / itm / impl / ItmExternalTunnelDeleteTest.java
1 /*
2  * Copyright (c) 2016 Ericsson India Global Services Pvt Ltd. 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
9 package org.opendaylight.genius.itm.impl;
10
11 import com.google.common.base.Optional;
12 import com.google.common.util.concurrent.Futures;
13 import org.junit.After;
14 import org.junit.Before;
15 import org.junit.Test;
16 import org.junit.runner.RunWith;
17 import org.mockito.Mock;
18 import org.mockito.runners.MockitoJUnitRunner;
19 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
20 import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
21 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
22 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
23 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker;
24 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
25 import org.opendaylight.genius.itm.confighelpers.HwVtep;
26 import org.opendaylight.genius.itm.confighelpers.ItmExternalTunnelDeleteWorker;
27 import org.opendaylight.genius.itm.globals.ITMConstants;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddressBuilder;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpPrefix;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpPrefixBuilder;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.*;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rev160406.TunnelTypeBase;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rev160406.TunnelTypeVxlan;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.op.rev160406.ExternalTunnelList;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.op.rev160406.dpn.endpoints.DPNTEPsInfo;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.op.rev160406.dpn.endpoints.DPNTEPsInfoBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.op.rev160406.dpn.endpoints.DPNTEPsInfoKey;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.op.rev160406.dpn.endpoints.dpn.teps.info
41         .TunnelEndPoints;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.op.rev160406.dpn.endpoints.dpn.teps.info
43         .TunnelEndPointsBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.op.rev160406.external.tunnel.list.ExternalTunnel;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.op.rev160406.external.tunnel.list
46         .ExternalTunnelBuilder;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.op.rev160406.external.tunnel.list.ExternalTunnelKey;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rev160406.TransportZones;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rev160406.transport.zones.TransportZone;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rev160406.transport.zones.TransportZoneBuilder;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rev160406.transport.zones.TransportZoneKey;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rev160406.transport.zones.transport.zone.Subnets;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rev160406.transport.zones.transport.zone
54         .SubnetsBuilder;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rev160406.transport.zones.transport.zone.SubnetsKey;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rev160406.transport.zones.transport.zone.subnets.*;
57 import org.opendaylight.yangtools.concepts.ListenerRegistration;
58 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
59 import org.opendaylight.yangtools.yang.common.RpcResult;
60 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
61
62 import java.math.BigInteger;
63 import java.util.ArrayList;
64 import java.util.List;
65 import java.util.concurrent.Future;
66
67 import static org.mockito.Matchers.any;
68 import static org.mockito.Mockito.*;
69
70 @RunWith(MockitoJUnitRunner.class)
71 public class ItmExternalTunnelDeleteTest {
72
73     int vlanId = 100 ;
74     String portName1 = "phy0";
75     String parentInterfaceName = "1:phy0:100" ;
76     String transportZone1 = "TZA" ;
77     String subnetIp = "10.1.1.24";
78     String tepIp1 = "192.168.56.30";
79     String tepIp2 = "192.168.56.40";
80     String tepIp3 = "192.168.56.101";
81     String gwyIp1 = "0.0.0.0";
82     String gwyIp2 = "0.0.0.1";
83     String trunkInterfaceName = null;
84     IpAddress ipAddress1 = null;
85     IpAddress ipAddress2 = null;
86     IpAddress ipAddress3 = null;
87     IpAddress gtwyIp1 = null;
88     IpAddress gtwyIp2 = null;
89     IpPrefix ipPrefixTest = null;
90     BigInteger dpId2 = BigInteger.valueOf(1);
91     DPNTEPsInfo dpntePsInfoVxlan = null;
92     TunnelEndPoints tunnelEndPointsVxlan = null;
93     HwVtep hwVtep1  = null;
94     Subnets subnets = null;
95     DeviceVteps deviceVteps1 = null;
96     DeviceVteps deviceVteps2 = null;
97     Vteps vteps = null;
98     TransportZone transportZone = null;
99     AllocateIdInput getIdInput1 = null;
100     AllocateIdInput getIdInput2 = null;
101     AllocateIdInput getIdInput3 = null;
102     AllocateIdInput getIdInput4 = null;
103     AllocateIdInput getIdInput5 = null;
104     AllocateIdInput getIdInput6 = null;
105     AllocateIdInput getIdInput7 = null;
106     AllocateIdInput getIdInput8 = null;
107     ExternalTunnel externalTunnel = null;
108     List<TunnelEndPoints> tunnelEndPointsListVxlan = new ArrayList<>();
109     List<DPNTEPsInfo> dpnTepsList = new ArrayList<DPNTEPsInfo>() ;
110     List<HwVtep> cfgdHwVtepsList = new ArrayList<>();
111     List<Subnets> subnetsList = new ArrayList<>();
112     List<DeviceVteps> deviceVtepsList = new ArrayList<>();
113     List<Vteps> vtepsList = new ArrayList<>();
114     java.lang.Class<? extends TunnelTypeBase> tunnelType1 = TunnelTypeVxlan.class;
115
116     AllocateIdOutput expectedId1 = new AllocateIdOutputBuilder().setIdValue(Long.valueOf("100")).build();
117     Future<RpcResult<AllocateIdOutput>> idOutputOptional1 ;
118     AllocateIdOutput expectedId2 = new AllocateIdOutputBuilder().setIdValue(Long.valueOf("101")).build();
119     Future<RpcResult<AllocateIdOutput>> idOutputOptional2 ;
120     AllocateIdOutput expectedId3 = new AllocateIdOutputBuilder().setIdValue(Long.valueOf("102")).build();
121     Future<RpcResult<AllocateIdOutput>> idOutputOptional3 ;
122     AllocateIdOutput expectedId4 = new AllocateIdOutputBuilder().setIdValue(Long.valueOf("103")).build();
123     Future<RpcResult<AllocateIdOutput>> idOutputOptional4 ;
124     AllocateIdOutput expectedId5 = new AllocateIdOutputBuilder().setIdValue(Long.valueOf("104")).build();
125     Future<RpcResult<AllocateIdOutput>> idOutputOptional5 ;
126     AllocateIdOutput expectedId6 = new AllocateIdOutputBuilder().setIdValue(Long.valueOf("105")).build();
127     Future<RpcResult<AllocateIdOutput>> idOutputOptional6 ;
128     AllocateIdOutput expectedId7 = new AllocateIdOutputBuilder().setIdValue(Long.valueOf("105")).build();
129     Future<RpcResult<AllocateIdOutput>> idOutputOptional7 ;
130     AllocateIdOutput expectedId8 = new AllocateIdOutputBuilder().setIdValue(Long.valueOf("106")).build();
131     Future<RpcResult<AllocateIdOutput>> idOutputOptional8 ;
132     InstanceIdentifier<Interface> trunkIdentifier ;
133     InstanceIdentifier<ExternalTunnel> path ;
134
135     @Mock DataBroker dataBroker;
136     @Mock ListenerRegistration<DataChangeListener> dataChangeListenerRegistration;
137     @Mock ReadOnlyTransaction mockReadTx;
138     @Mock WriteTransaction mockWriteTx;
139     @Mock IdManagerService idManagerService;
140
141     ItmExternalTunnelDeleteWorker itmExternalTunnelDeleteWorker = new ItmExternalTunnelDeleteWorker();
142
143     @Before
144     public void setUp() throws Exception {
145         when(dataBroker.registerDataChangeListener(
146                 any(LogicalDatastoreType.class),
147                 any(InstanceIdentifier.class),
148                 any(DataChangeListener.class),
149                 any(AsyncDataBroker.DataChangeScope.class)))
150                 .thenReturn(dataChangeListenerRegistration);
151         setupMocks();
152
153         when(idManagerService.releaseId(any(ReleaseIdInput.class))).thenReturn(Futures.immediateFuture(RpcResultBuilder
154                 .<Void>success().build()));
155     }
156
157     @After
158     public void cleanUp() {
159     }
160
161     private void setupMocks(){
162
163         ipAddress1 = IpAddressBuilder.getDefaultInstance(tepIp1);
164         ipAddress2 = IpAddressBuilder.getDefaultInstance(tepIp2);
165         ipAddress3 = IpAddressBuilder.getDefaultInstance(tepIp3);
166         ipPrefixTest = IpPrefixBuilder.getDefaultInstance(subnetIp + "/24");
167         gtwyIp1 = IpAddressBuilder.getDefaultInstance(gwyIp1);
168         gtwyIp2 = IpAddressBuilder.getDefaultInstance(gwyIp2);
169         deviceVteps1 = new DeviceVtepsBuilder().setIpAddress(ipAddress1).setKey(new DeviceVtepsKey(ipAddress1, "hwvtep:1"))
170                 .setNodeId("hwvtep://192.168.101.30:6640/physicalswitch/s3").setTopologyId("hwvtep:1").build();
171         deviceVteps2 = new DeviceVtepsBuilder().setIpAddress(ipAddress2).setKey(new DeviceVtepsKey(ipAddress2, "hwvtep:1"))
172                 .setNodeId("hwvtep://192.168.101.30:6640/physicalswitch/s3").setTopologyId("hwvtep:1").build();
173         deviceVtepsList.add(deviceVteps1);
174         deviceVtepsList.add(deviceVteps2);
175         hwVtep1 = new HwVtep();
176         hwVtep1.setTransportZone(transportZone1);
177         hwVtep1.setGatewayIP(gtwyIp1);
178         hwVtep1.setHwIp(ipAddress2);
179         hwVtep1.setTunnel_type(tunnelType1);
180         hwVtep1.setVlanID(vlanId);
181         hwVtep1.setTopo_id("hwvtep:1");
182         hwVtep1.setNode_id("hwvtep://192.168.101.30:6640/physicalswitch/s3");
183         hwVtep1.setIpPrefix(ipPrefixTest);
184         vteps = new VtepsBuilder().setDpnId(dpId2).setIpAddress(ipAddress1).setPortname(portName1).setKey(new
185                 VtepsKey(dpId2,portName1)).build();
186         vtepsList.add(vteps);
187         idOutputOptional1 = RpcResultBuilder.success(expectedId1).buildFuture();
188         getIdInput1 = new AllocateIdInputBuilder()
189                 .setPoolName(ITMConstants.ITM_IDPOOL_NAME)
190                 .setIdKey("1:phy0:100:192.168.56.101:192.168.56.30:VXLAN").build();
191         doReturn(idOutputOptional1).when(idManagerService).allocateId(getIdInput1);
192         idOutputOptional2 = RpcResultBuilder.success(expectedId2).buildFuture();
193         getIdInput2 = new AllocateIdInputBuilder()
194                 .setPoolName(ITMConstants.ITM_IDPOOL_NAME)
195                 .setIdKey("hwvtep:1:hwvtep:1:192.168.56.30:192.168.56.101:VXLAN").build();
196         doReturn(idOutputOptional2).when(idManagerService).allocateId(getIdInput2);
197         idOutputOptional3 = RpcResultBuilder.success(expectedId3).buildFuture();
198         getIdInput3 = new AllocateIdInputBuilder()
199                 .setPoolName(ITMConstants.ITM_IDPOOL_NAME)
200                 .setIdKey("1:phy0:100:192.168.56.101:192.168.56.40:VXLAN").build();
201         doReturn(idOutputOptional3).when(idManagerService).allocateId(getIdInput3);
202         idOutputOptional4 = RpcResultBuilder.success(expectedId4).buildFuture();
203         getIdInput4 = new AllocateIdInputBuilder()
204                 .setPoolName(ITMConstants.ITM_IDPOOL_NAME)
205                 .setIdKey("hwvtep:1:hwvtep:1:192.168.56.40:192.168.56.101:VXLAN").build();
206         doReturn(idOutputOptional4).when(idManagerService).allocateId(getIdInput4);
207         idOutputOptional5 = RpcResultBuilder.success(expectedId5).buildFuture();
208         getIdInput5 = new AllocateIdInputBuilder()
209                 .setPoolName(ITMConstants.ITM_IDPOOL_NAME)
210                 .setIdKey("hwvtep:1:hwvtep://192.168.101.30:6640/physicalswitch/s3:192.168.56.40:192.168.56.101:VXLAN").build();
211         doReturn(idOutputOptional5).when(idManagerService).allocateId(getIdInput5);
212         idOutputOptional6 = RpcResultBuilder.success(expectedId6).buildFuture();
213         getIdInput6 = new AllocateIdInputBuilder()
214                 .setPoolName(ITMConstants.ITM_IDPOOL_NAME)
215                 .setIdKey("hwvtep:1:hwvtep://192.168.101.30:6640/physicalswitch/s3:192.168.56.40:192.168.56.30:VXLAN")
216                 .build();
217         doReturn(idOutputOptional6).when(idManagerService).allocateId(getIdInput6);
218         idOutputOptional7 = RpcResultBuilder.success(expectedId7).buildFuture();
219         getIdInput7 = new AllocateIdInputBuilder()
220                 .setPoolName(ITMConstants.ITM_IDPOOL_NAME)
221                 .setIdKey("hwvtep:1:hwvtep:1:192.168.56.30:192.168.56.40:VXLAN").build();
222         doReturn(idOutputOptional7).when(idManagerService).allocateId(getIdInput7);
223         idOutputOptional8 = RpcResultBuilder.success(expectedId8).buildFuture();
224         getIdInput8 = new AllocateIdInputBuilder()
225                 .setPoolName(ITMConstants.ITM_IDPOOL_NAME)
226                 .setIdKey("1:phy0:100:192.168.56.30:192.168.56.40:VXLAN").build();
227         doReturn(idOutputOptional8).when(idManagerService).allocateId(getIdInput8);
228         tunnelEndPointsVxlan = new TunnelEndPointsBuilder().setVLANID(vlanId).setPortname(portName1).setIpAddress
229                 (ipAddress3).setGwIpAddress(gtwyIp1).setInterfaceName(parentInterfaceName).setTransportZone
230                 (transportZone1).setTunnelType(tunnelType1).setSubnetMask(ipPrefixTest).build();
231         tunnelEndPointsListVxlan.add(tunnelEndPointsVxlan);
232         dpntePsInfoVxlan = new DPNTEPsInfoBuilder().setDPNID(dpId2).setUp(true).setKey(new DPNTEPsInfoKey(dpId2))
233                 .setTunnelEndPoints(tunnelEndPointsListVxlan).build();
234         dpnTepsList.add(dpntePsInfoVxlan);
235         cfgdHwVtepsList.add(hwVtep1);
236         subnets = new SubnetsBuilder().setGatewayIp(gtwyIp1).setVlanId(vlanId).setKey(new SubnetsKey(ipPrefixTest))
237                 .setDeviceVteps(deviceVtepsList).setVteps(vtepsList).build();
238         subnetsList.add(subnets);
239         transportZone = new TransportZoneBuilder().setTunnelType(tunnelType1).setZoneName(transportZone1).setKey(new
240                 TransportZoneKey(transportZone1)).setSubnets(subnetsList).build();
241         externalTunnel = new ExternalTunnelBuilder().setTunnelInterfaceName(parentInterfaceName).setTransportType
242                 (tunnelType1).setDestinationDevice("hwvtep:1").setSourceDevice(dpId2.toString()).setKey(new
243                 ExternalTunnelKey(dpId2.toString() , hwVtep1.getNode_id() , tunnelType1)).build();
244         trunkInterfaceName = ItmUtils.getTrunkInterfaceName( idManagerService, parentInterfaceName,
245                 tunnelEndPointsVxlan.getIpAddress().getIpv4Address().getValue(), ipAddress1.getIpv4Address().getValue(),
246                 tunnelType1.getName());
247         trunkIdentifier = ItmUtils.buildId(trunkInterfaceName);
248         path = InstanceIdentifier.create(ExternalTunnelList.class)
249                 .child(ExternalTunnel.class, ItmUtils.getExternalTunnelKey(ipAddress1.toString(), dpId2.toString(),
250                         tunnelType1));
251
252         doReturn(mockReadTx).when(dataBroker).newReadOnlyTransaction();
253         doReturn(mockWriteTx).when(dataBroker).newWriteOnlyTransaction();
254         doReturn(Futures.immediateCheckedFuture(null)).when(mockWriteTx).submit();
255     }
256
257     @Test
258     public void testDeleteTunnels(){
259
260         itmExternalTunnelDeleteWorker.deleteTunnels(dataBroker,idManagerService,dpnTepsList,ipAddress1,tunnelType1);
261
262         verify(mockWriteTx).delete(LogicalDatastoreType.CONFIGURATION,trunkIdentifier);
263         verify(mockWriteTx).delete(LogicalDatastoreType.CONFIGURATION,path);
264
265     }
266
267     @Test
268     public void testDeleteHwVtepsTunnels(){
269
270         InstanceIdentifier<TransportZone> transportZoneIdentifier = InstanceIdentifier.builder(TransportZones.class).child
271                 (TransportZone.class, new TransportZoneKey(transportZone1)).build();
272         InstanceIdentifier<ExternalTunnel> externalTunnelIdentifier1 = InstanceIdentifier.create(ExternalTunnelList
273                 .class).child(ExternalTunnel.class, ItmUtils.getExternalTunnelKey(hwVtep1.getTopo_id(), dpId2.toString(),
274                         tunnelType1));
275         InstanceIdentifier<ExternalTunnel> externalTunnelIdentifier2 = InstanceIdentifier.create(ExternalTunnelList
276                 .class).child(ExternalTunnel.class, ItmUtils.getExternalTunnelKey(dpId2.toString(), hwVtep1.getTopo_id(),
277                 tunnelType1));
278         InstanceIdentifier<ExternalTunnel> externalTunnelIdentifier3 = InstanceIdentifier.create(ExternalTunnelList
279                 .class).child(ExternalTunnel.class, ItmUtils.getExternalTunnelKey(hwVtep1.getNode_id(), dpId2.toString(),
280                 tunnelType1));
281         InstanceIdentifier<ExternalTunnel> externalTunnelIdentifier4 = InstanceIdentifier.create(ExternalTunnelList
282                 .class).child(ExternalTunnel.class, ItmUtils.getExternalTunnelKey(dpId2.toString(), hwVtep1.getNode_id(),
283                 tunnelType1));
284         InstanceIdentifier<ExternalTunnel> externalTunnelIdentifier5 = InstanceIdentifier.create(ExternalTunnelList
285                 .class).child(ExternalTunnel.class, ItmUtils.getExternalTunnelKey(hwVtep1.getTopo_id(), hwVtep1.getNode_id(),
286                 tunnelType1));
287         InstanceIdentifier<ExternalTunnel> externalTunnelIdentifier6 = InstanceIdentifier.create(ExternalTunnelList
288                 .class).child(ExternalTunnel.class, ItmUtils.getExternalTunnelKey( hwVtep1.getNode_id(), hwVtep1.getTopo_id(),
289                 tunnelType1));
290
291         Optional<TransportZone> optionalTransportZone = Optional.of(transportZone);
292         Optional<ExternalTunnel> exTunnels = Optional.of(externalTunnel);
293
294         doReturn(Futures.immediateCheckedFuture(optionalTransportZone)).when(mockReadTx).read(LogicalDatastoreType
295                 .CONFIGURATION,transportZoneIdentifier);
296         doReturn(Futures.immediateCheckedFuture(exTunnels)).when(mockReadTx).read(LogicalDatastoreType.CONFIGURATION,
297                 externalTunnelIdentifier1);
298         doReturn(Futures.immediateCheckedFuture(exTunnels)).when(mockReadTx).read(LogicalDatastoreType.CONFIGURATION,
299                 externalTunnelIdentifier2);
300         doReturn(Futures.immediateCheckedFuture(exTunnels)).when(mockReadTx).read(LogicalDatastoreType.CONFIGURATION,
301                 externalTunnelIdentifier3);
302         doReturn(Futures.immediateCheckedFuture(exTunnels)).when(mockReadTx).read(LogicalDatastoreType.CONFIGURATION,
303                 externalTunnelIdentifier4);
304         doReturn(Futures.immediateCheckedFuture(exTunnels)).when(mockReadTx).read(LogicalDatastoreType.CONFIGURATION,
305                 externalTunnelIdentifier5);
306         doReturn(Futures.immediateCheckedFuture(exTunnels)).when(mockReadTx).read(LogicalDatastoreType.CONFIGURATION,
307                 externalTunnelIdentifier6);
308
309         itmExternalTunnelDeleteWorker.deleteHwVtepsTunnels(dataBroker,idManagerService,dpnTepsList,cfgdHwVtepsList);
310
311         verify(mockWriteTx).delete(LogicalDatastoreType.CONFIGURATION,trunkIdentifier);
312         verify(mockWriteTx, times(2)).delete(LogicalDatastoreType.CONFIGURATION,externalTunnelIdentifier1);
313         verify(mockWriteTx, times(2)).delete(LogicalDatastoreType.CONFIGURATION,externalTunnelIdentifier2);
314         verify(mockWriteTx, times(2)).delete(LogicalDatastoreType.CONFIGURATION,externalTunnelIdentifier3);
315         verify(mockWriteTx, times(2)).delete(LogicalDatastoreType.CONFIGURATION,externalTunnelIdentifier4);
316         verify(mockWriteTx).delete(LogicalDatastoreType.CONFIGURATION,externalTunnelIdentifier5);
317         verify(mockWriteTx).delete(LogicalDatastoreType.CONFIGURATION,externalTunnelIdentifier6);
318
319     }
320
321 }