ca39cecf664f19ba4aee206d48365c6d8e76c11e
[genius.git] / itm / itm-impl / src / test / java / org / opendaylight / genius / itm / impl / ItmInternalTunnelDeleteTest.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 static org.mockito.Matchers.any;
12 import static org.mockito.Mockito.doReturn;
13 import static org.mockito.Mockito.verify;
14 import static org.mockito.Mockito.when;
15
16 import com.google.common.base.Optional;
17 import com.google.common.util.concurrent.Futures;
18 import java.math.BigInteger;
19 import java.util.ArrayList;
20 import java.util.List;
21 import org.junit.After;
22 import org.junit.Before;
23 import org.junit.Test;
24 import org.junit.runner.RunWith;
25 import org.mockito.Mock;
26 import org.mockito.runners.MockitoJUnitRunner;
27 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
28 import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
29 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
30 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
31 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker;
32 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
33 import org.opendaylight.genius.itm.confighelpers.ItmInternalTunnelDeleteWorker;
34 import org.opendaylight.genius.itm.globals.ITMConstants;
35 import org.opendaylight.genius.mdsalutil.interfaces.IMdsalApiManager;
36 import org.opendaylight.genius.utils.cache.DataStoreCache;
37 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
38 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddressBuilder;
39 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpPrefix;
40 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpPrefixBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.IdManagerService;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rev160406.TunnelMonitoringTypeBase;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rev160406.TunnelMonitoringTypeBfd;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rev160406.TunnelTypeBase;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rev160406.TunnelTypeVxlan;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.config.rev160406.TunnelMonitorInterval;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.config.rev160406.TunnelMonitorIntervalBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.config.rev160406.TunnelMonitorParams;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.config.rev160406.TunnelMonitorParamsBuilder;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.op.rev160406.DpnEndpoints;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.op.rev160406.DpnEndpointsBuilder;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.op.rev160406.TunnelList;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.op.rev160406.dpn.endpoints.DPNTEPsInfo;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.op.rev160406.dpn.endpoints.DPNTEPsInfoBuilder;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.op.rev160406.dpn.endpoints.DPNTEPsInfoKey;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.op.rev160406.dpn.endpoints.dpn.teps.info.TunnelEndPoints;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.op.rev160406.dpn.endpoints.dpn.teps.info.TunnelEndPointsBuilder;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.op.rev160406.dpn.endpoints.dpn.teps.info.TunnelEndPointsKey;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.op.rev160406.tunnel.list.InternalTunnel;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.op.rev160406.tunnel.list.InternalTunnelBuilder;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.op.rev160406.tunnel.list.InternalTunnelKey;
62 import org.opendaylight.yangtools.concepts.ListenerRegistration;
63 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
64
65 @RunWith(MockitoJUnitRunner.class)
66 public class ItmInternalTunnelDeleteTest {
67
68     BigInteger dpId1 = BigInteger.valueOf(1);
69     BigInteger dpId2 = BigInteger.valueOf(2);
70     int vlanId = 100 ;
71     int interval = 1000;
72     String portName1 = "phy0";
73     String portName2 = "phy1" ;
74     String parentInterfaceName = "1:phy0:100" ;
75     String transportZone1 = "TZA" ;
76     String subnetIp = "10.1.1.24";
77     String tepIp1 = "192.168.56.101";
78     String tepIp2 = "192.168.56.102";
79     String gwyIp1 = "0.0.0.0";
80     String gwyIp2 = "0.0.0.1";
81     IpAddress ipAddress1 = null;
82     IpAddress ipAddress2 = null;
83     IpAddress gtwyIp1 = null;
84     IpAddress gtwyIp2 = null;
85     IpPrefix ipPrefixTest = null;
86     DPNTEPsInfo dpntePsInfoVxlan = null;
87     DPNTEPsInfo dpntePsInfoVxlanNew = null;
88     TunnelEndPoints tunnelEndPointsVxlan = null;
89     TunnelEndPoints tunnelEndPointsVxlanNew = null;
90     DpnEndpoints dpnEndpoints = null;
91     List<DPNTEPsInfo> meshDpnListVxlan = new ArrayList<>() ;
92     List<DPNTEPsInfo> cfgdDpnListVxlan = new ArrayList<>() ;
93     List<TunnelEndPoints> tunnelEndPointsListVxlan = new ArrayList<>();
94     List<TunnelEndPoints> tunnelEndPointsListVxlanNew = new ArrayList<>();
95     java.lang.Class<? extends TunnelTypeBase> tunnelType1 = TunnelTypeVxlan.class;
96     TunnelMonitorParams tunnelMonitorParams = null;
97     TunnelMonitorInterval tunnelMonitorInterval = null;
98     InternalTunnel internalTunnel = new InternalTunnelBuilder().build();
99     InstanceIdentifier<TunnelMonitorParams> tunnelMonitorParamsInstanceIdentifier =
100             InstanceIdentifier.create(TunnelMonitorParams.class);
101     InstanceIdentifier<TunnelMonitorInterval> tunnelMonitorIntervalIdentifier =
102             InstanceIdentifier.create(TunnelMonitorInterval.class);
103     Class<? extends TunnelMonitoringTypeBase> monitorProtocol = TunnelMonitoringTypeBfd.class;
104     InstanceIdentifier<InternalTunnel> internalTunnelIdentifier = InstanceIdentifier.builder(TunnelList.class)
105             .child(InternalTunnel.class, new InternalTunnelKey(new BigInteger("1"),
106                     new BigInteger("2"), tunnelType1)).build();
107
108     @Mock DataBroker dataBroker;
109     @Mock ListenerRegistration<DataChangeListener> dataChangeListenerRegistration;
110     @Mock ReadOnlyTransaction mockReadTx;
111     @Mock WriteTransaction mockWriteTx;
112     @Mock IdManagerService idManagerService;
113     @Mock IMdsalApiManager mdsalApiManager;
114
115     ItmInternalTunnelDeleteWorker itmInternalTunnelDeleteWorker = new ItmInternalTunnelDeleteWorker();
116
117     Optional<TunnelMonitorParams> tunnelMonitorParamsOptional;
118     Optional<TunnelMonitorInterval> tunnelMonitorIntervalOptional ;
119     Optional<InternalTunnel> internalTunnelOptional ;
120
121     @Before
122     public void setUp() {
123         when(dataBroker.registerDataChangeListener(
124                 any(LogicalDatastoreType.class),
125                 any(InstanceIdentifier.class),
126                 any(DataChangeListener.class),
127                 any(AsyncDataBroker.DataChangeScope.class)))
128                 .thenReturn(dataChangeListenerRegistration);
129         setupMocks();
130         DataStoreCache.create(ITMConstants.ITM_MONIRORING_PARAMS_CACHE_NAME);
131
132         tunnelMonitorParamsOptional = Optional.of(tunnelMonitorParams);
133         tunnelMonitorIntervalOptional = Optional.of(tunnelMonitorInterval);
134         internalTunnelOptional = Optional.of(internalTunnel);
135
136         doReturn(Futures.immediateCheckedFuture(tunnelMonitorParamsOptional)).when(mockReadTx)
137                 .read(LogicalDatastoreType.CONFIGURATION,
138                         tunnelMonitorParamsInstanceIdentifier);
139         doReturn(Futures.immediateCheckedFuture(tunnelMonitorIntervalOptional)).when(mockReadTx)
140                 .read(LogicalDatastoreType.CONFIGURATION,
141                 tunnelMonitorIntervalIdentifier);
142         doReturn(Futures.immediateCheckedFuture(internalTunnelOptional)).when(mockReadTx)
143                 .read(LogicalDatastoreType.CONFIGURATION,internalTunnelIdentifier);
144
145     }
146
147     @After
148     public void cleanUp() {
149     }
150
151     private void setupMocks() {
152
153         ipAddress1 = IpAddressBuilder.getDefaultInstance(tepIp1);
154         ipAddress2 = IpAddressBuilder.getDefaultInstance(tepIp2);
155         ipPrefixTest = IpPrefixBuilder.getDefaultInstance(subnetIp + "/24");
156         gtwyIp1 = IpAddressBuilder.getDefaultInstance(gwyIp1);
157         gtwyIp2 = IpAddressBuilder.getDefaultInstance(gwyIp2);
158         tunnelEndPointsVxlan = new TunnelEndPointsBuilder().setVLANID(vlanId).setPortname(portName1)
159                 .setIpAddress(ipAddress1).setGwIpAddress(gtwyIp1).setInterfaceName(parentInterfaceName)
160                 .setTzMembership(ItmUtils.createTransportZoneMembership(transportZone1))
161                 .setTunnelType(tunnelType1).setSubnetMask(ipPrefixTest)
162                 .setKey(new TunnelEndPointsKey(ipAddress1,portName1,tunnelType1,vlanId)).build();
163         tunnelEndPointsVxlanNew = new TunnelEndPointsBuilder().setVLANID(vlanId).setPortname(portName2)
164                 .setIpAddress(ipAddress2).setGwIpAddress(gtwyIp2).setInterfaceName(parentInterfaceName)
165                 .setTzMembership(ItmUtils.createTransportZoneMembership(transportZone1)).setTunnelType(tunnelType1)
166                 .setSubnetMask(ipPrefixTest).build();
167         tunnelEndPointsListVxlan.add(tunnelEndPointsVxlan);
168         tunnelEndPointsListVxlanNew.add(tunnelEndPointsVxlanNew);
169         dpntePsInfoVxlan = new DPNTEPsInfoBuilder().setDPNID(dpId1).setKey(new DPNTEPsInfoKey(dpId1)).setUp(true)
170                 .setTunnelEndPoints(tunnelEndPointsListVxlan).build();
171         dpntePsInfoVxlanNew = new DPNTEPsInfoBuilder().setDPNID(dpId2).setKey(new DPNTEPsInfoKey(dpId2)).setUp(true)
172                 .setTunnelEndPoints(tunnelEndPointsListVxlanNew).setTunnelEndPoints(tunnelEndPointsListVxlanNew)
173                 .build();
174         tunnelMonitorParams = new TunnelMonitorParamsBuilder().setEnabled(true).setMonitorProtocol(monitorProtocol)
175                 .build();
176         tunnelMonitorInterval = new TunnelMonitorIntervalBuilder().setInterval(interval).build();
177         cfgdDpnListVxlan.add(dpntePsInfoVxlan);
178         meshDpnListVxlan.add(dpntePsInfoVxlan);
179         meshDpnListVxlan.add(dpntePsInfoVxlanNew);
180         dpnEndpoints = new DpnEndpointsBuilder().setDPNTEPsInfo(cfgdDpnListVxlan).build();
181
182         doReturn(mockReadTx).when(dataBroker).newReadOnlyTransaction();
183         doReturn(mockWriteTx).when(dataBroker).newWriteOnlyTransaction();
184         doReturn(Futures.immediateCheckedFuture(null)).when(mockWriteTx).submit();
185     }
186
187     // Since all the unit test cases will be written to the new way, this should be taken care then.
188     @Test
189     public void testDeleteTunnels() {
190
191         InstanceIdentifier<TunnelEndPoints> tunnelEndPointsIdentifier =
192                 InstanceIdentifier.builder(DpnEndpoints.class).child(DPNTEPsInfo.class, dpntePsInfoVxlan.getKey())
193                         .child(TunnelEndPoints.class,tunnelEndPointsVxlan.getKey()).build();
194         InstanceIdentifier<DpnEndpoints> dpnEndpointsIdentifier =
195                 InstanceIdentifier.builder(DpnEndpoints.class).build();
196         final InstanceIdentifier<DPNTEPsInfo> dpntePsInfoIdentifier = InstanceIdentifier.builder(DpnEndpoints.class)
197                 .child(DPNTEPsInfo.class, dpntePsInfoVxlan.getKey()).build();
198
199         Optional<DpnEndpoints> dpnEndpointsOptional = Optional.of(dpnEndpoints);
200
201         doReturn(Futures.immediateCheckedFuture(dpnEndpointsOptional)).when(mockReadTx).read(LogicalDatastoreType
202                 .CONFIGURATION,dpnEndpointsIdentifier);
203
204         itmInternalTunnelDeleteWorker.deleteTunnels(dataBroker,idManagerService, mdsalApiManager,cfgdDpnListVxlan,
205                 meshDpnListVxlan);
206
207         verify(mockWriteTx).delete(LogicalDatastoreType.CONFIGURATION,tunnelEndPointsIdentifier);
208         verify(mockWriteTx).delete(LogicalDatastoreType.CONFIGURATION,dpntePsInfoIdentifier);
209     }
210 }