OF port deletion complete implementation
[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 package org.opendaylight.genius.itm.impl;
9
10 import static org.mockito.Mockito.doReturn;
11 import static org.mockito.Mockito.lenient;
12 import static org.mockito.Mockito.verify;
13
14 import java.util.ArrayList;
15 import java.util.List;
16 import java.util.Optional;
17 import org.junit.After;
18 import org.junit.Before;
19 import org.junit.Test;
20 import org.junit.runner.RunWith;
21 import org.mockito.Mock;
22 import org.mockito.junit.MockitoJUnitRunner;
23 import org.opendaylight.genius.cloudscaler.api.TombstonedNodeManager;
24 import org.opendaylight.genius.interfacemanager.interfaces.IInterfaceManager;
25 import org.opendaylight.genius.itm.cache.DPNTEPsInfoCache;
26 import org.opendaylight.genius.itm.cache.DpnTepStateCache;
27 import org.opendaylight.genius.itm.cache.OfEndPointCache;
28 import org.opendaylight.genius.itm.cache.OvsBridgeEntryCache;
29 import org.opendaylight.genius.itm.cache.OvsBridgeRefEntryCache;
30 import org.opendaylight.genius.itm.cache.TunnelStateCache;
31 import org.opendaylight.genius.itm.cache.UnprocessedNodeConnectorCache;
32 import org.opendaylight.genius.itm.cache.UnprocessedNodeConnectorEndPointCache;
33 import org.opendaylight.genius.itm.confighelpers.ItmInternalTunnelDeleteWorker;
34 import org.opendaylight.genius.itm.itmdirecttunnels.renderer.ovs.utilities.DirectTunnelUtils;
35 import org.opendaylight.genius.mdsalutil.interfaces.IMdsalApiManager;
36 import org.opendaylight.infrautils.caches.baseimpl.internal.CacheManagersRegistryImpl;
37 import org.opendaylight.infrautils.caches.guava.internal.GuavaCacheProvider;
38 import org.opendaylight.infrautils.jobcoordinator.JobCoordinator;
39 import org.opendaylight.mdsal.binding.api.DataBroker;
40 import org.opendaylight.mdsal.binding.api.ReadTransaction;
41 import org.opendaylight.mdsal.binding.api.ReadWriteTransaction;
42 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
43 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
44 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddressBuilder;
45 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpPrefix;
46 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpPrefixBuilder;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.IdManagerService;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rev160406.TunnelMonitoringTypeBase;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rev160406.TunnelMonitoringTypeBfd;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rev160406.TunnelTypeBase;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rev160406.TunnelTypeVxlan;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.config.rev160406.ItmConfig;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.config.rev160406.TunnelMonitorInterval;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.config.rev160406.TunnelMonitorIntervalBuilder;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.config.rev160406.TunnelMonitorParams;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.config.rev160406.TunnelMonitorParamsBuilder;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.op.rev160406.DpnEndpoints;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.op.rev160406.DpnEndpointsBuilder;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.op.rev160406.TunnelList;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.op.rev160406.dpn.endpoints.DPNTEPsInfo;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.op.rev160406.dpn.endpoints.DPNTEPsInfoBuilder;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.op.rev160406.dpn.endpoints.DPNTEPsInfoKey;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.op.rev160406.dpn.endpoints.dpn.teps.info.TunnelEndPoints;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.op.rev160406.dpn.endpoints.dpn.teps.info.TunnelEndPointsBuilder;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.op.rev160406.dpn.endpoints.dpn.teps.info.TunnelEndPointsKey;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.op.rev160406.tunnel.list.InternalTunnel;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.op.rev160406.tunnel.list.InternalTunnelBuilder;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.op.rev160406.tunnel.list.InternalTunnelKey;
69 import org.opendaylight.yangtools.util.concurrent.FluentFutures;
70 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
71 import org.opendaylight.yangtools.yang.common.Uint64;
72
73 @RunWith(MockitoJUnitRunner.class)
74 public class ItmInternalTunnelDeleteTest {
75
76     Uint64 dpId1 = Uint64.ONE;
77     Uint64 dpId2 = Uint64.valueOf(2);
78     int vlanId = 100 ;
79     int interval = 1000;
80     String portName1 = "phy0";
81     String portName2 = "phy1" ;
82     String parentInterfaceName = "1:phy0:100" ;
83     String transportZone1 = "TZA" ;
84     String subnetIp = "10.1.1.24";
85     String tepIp1 = "192.168.56.101";
86     String tepIp2 = "192.168.56.102";
87     String gwyIp1 = "0.0.0.0";
88     String gwyIp2 = "0.0.0.1";
89     IpAddress ipAddress1 = null;
90     IpAddress ipAddress2 = null;
91     IpAddress gtwyIp1 = null;
92     IpAddress gtwyIp2 = null;
93     IpPrefix ipPrefixTest = null;
94     DPNTEPsInfo dpntePsInfoVxlan = null;
95     DPNTEPsInfo dpntePsInfoVxlanNew = null;
96     TunnelEndPoints tunnelEndPointsVxlan = null;
97     TunnelEndPoints tunnelEndPointsVxlanNew = null;
98     DpnEndpoints dpnEndpoints = null;
99     List<DPNTEPsInfo> meshDpnListVxlan = new ArrayList<>() ;
100     List<DPNTEPsInfo> cfgdDpnListVxlan = new ArrayList<>() ;
101     List<TunnelEndPoints> tunnelEndPointsListVxlan = new ArrayList<>();
102     List<TunnelEndPoints> tunnelEndPointsListVxlanNew = new ArrayList<>();
103     java.lang.Class<? extends TunnelTypeBase> tunnelType1 = TunnelTypeVxlan.class;
104     TunnelMonitorParams tunnelMonitorParams = null;
105     TunnelMonitorInterval tunnelMonitorInterval = null;
106     InternalTunnel internalTunnel = new InternalTunnelBuilder().build();
107     InstanceIdentifier<TunnelMonitorParams> tunnelMonitorParamsInstanceIdentifier =
108             InstanceIdentifier.create(TunnelMonitorParams.class);
109     InstanceIdentifier<TunnelMonitorInterval> tunnelMonitorIntervalIdentifier =
110             InstanceIdentifier.create(TunnelMonitorInterval.class);
111     Class<? extends TunnelMonitoringTypeBase> monitorProtocol = TunnelMonitoringTypeBfd.class;
112     InstanceIdentifier<InternalTunnel> internalTunnelIdentifier = InstanceIdentifier.builder(TunnelList.class)
113             .child(InternalTunnel.class, new InternalTunnelKey(Uint64.ONE, Uint64.valueOf(2), tunnelType1))
114             .build();
115
116     @Mock DataBroker dataBroker;
117     @Mock ReadTransaction mockReadTx;
118     @Mock ReadWriteTransaction mockReadWriteTx;
119     @Mock IdManagerService idManagerService;
120     @Mock IMdsalApiManager mdsalApiManager;
121     @Mock JobCoordinator jobCoordinator;
122     @Mock IInterfaceManager interfaceManager;
123     @Mock ItmConfig itmConfig;
124     @Mock TunnelMonitoringConfig tunnelMonitoringConfig;
125     @Mock TombstonedNodeManager tombstonedNodeManager;
126     DirectTunnelUtils directTunnelUtils;
127     ItmInternalTunnelDeleteWorker itmInternalTunnelDeleteWorker;
128     UnprocessedNodeConnectorCache unprocessedNodeConnectorCache;
129     UnprocessedNodeConnectorEndPointCache unprocessedNodeConnectorEndPointCache;
130     OfEndPointCache ofEndPointCache;
131
132
133     Optional<TunnelMonitorParams> tunnelMonitorParamsOptional;
134     Optional<TunnelMonitorInterval> tunnelMonitorIntervalOptional ;
135     Optional<InternalTunnel> internalTunnelOptional ;
136
137     @Before
138     public void setUp() {
139         setupMocks();
140
141         tunnelMonitorParamsOptional = Optional.of(tunnelMonitorParams);
142         tunnelMonitorIntervalOptional = Optional.of(tunnelMonitorInterval);
143         internalTunnelOptional = Optional.of(internalTunnel);
144
145         doReturn(FluentFutures.immediateFluentFuture(tunnelMonitorParamsOptional)).when(mockReadTx)
146                 .read(LogicalDatastoreType.CONFIGURATION,
147                         tunnelMonitorParamsInstanceIdentifier);
148         lenient().doReturn(FluentFutures.immediateFluentFuture(tunnelMonitorParamsOptional)).when(mockReadWriteTx)
149                 .read(LogicalDatastoreType.CONFIGURATION,
150                         tunnelMonitorParamsInstanceIdentifier);
151         lenient().doReturn(FluentFutures.immediateFluentFuture(tunnelMonitorIntervalOptional)).when(mockReadTx)
152                 .read(LogicalDatastoreType.CONFIGURATION,
153                         tunnelMonitorIntervalIdentifier);
154         lenient().doReturn(FluentFutures.immediateFluentFuture(tunnelMonitorIntervalOptional)).when(mockReadWriteTx)
155                 .read(LogicalDatastoreType.CONFIGURATION,
156                         tunnelMonitorIntervalIdentifier);
157         doReturn(FluentFutures.immediateFluentFuture(internalTunnelOptional)).when(mockReadTx)
158                 .read(LogicalDatastoreType.CONFIGURATION, internalTunnelIdentifier);
159         lenient().doReturn(FluentFutures.immediateFluentFuture(internalTunnelOptional)).when(mockReadWriteTx)
160                 .read(LogicalDatastoreType.CONFIGURATION, internalTunnelIdentifier);
161
162         DPNTEPsInfoCache dpntePsInfoCache =
163                 new DPNTEPsInfoCache(dataBroker, new GuavaCacheProvider(new CacheManagersRegistryImpl()),
164                         directTunnelUtils, jobCoordinator, unprocessedNodeConnectorEndPointCache);
165
166         itmInternalTunnelDeleteWorker = new ItmInternalTunnelDeleteWorker(dataBroker, jobCoordinator,
167             new TunnelMonitoringConfig(dataBroker, new GuavaCacheProvider(new CacheManagersRegistryImpl())),
168             interfaceManager, new DpnTepStateCache(dataBroker, jobCoordinator,
169             new GuavaCacheProvider(new CacheManagersRegistryImpl()), directTunnelUtils, dpntePsInfoCache,
170                 unprocessedNodeConnectorCache, unprocessedNodeConnectorEndPointCache),
171             new OvsBridgeEntryCache(dataBroker, new GuavaCacheProvider(new CacheManagersRegistryImpl())),
172             new OvsBridgeRefEntryCache(dataBroker, new GuavaCacheProvider(new CacheManagersRegistryImpl())),
173             new TunnelStateCache(dataBroker, new GuavaCacheProvider(new CacheManagersRegistryImpl())),
174             directTunnelUtils, tombstonedNodeManager);
175     }
176
177     @After
178     public void cleanUp() {
179     }
180
181     private void setupMocks() {
182
183         ipAddress1 = IpAddressBuilder.getDefaultInstance(tepIp1);
184         ipAddress2 = IpAddressBuilder.getDefaultInstance(tepIp2);
185         ipPrefixTest = IpPrefixBuilder.getDefaultInstance(subnetIp + "/24");
186         gtwyIp1 = IpAddressBuilder.getDefaultInstance(gwyIp1);
187         gtwyIp2 = IpAddressBuilder.getDefaultInstance(gwyIp2);
188         tunnelEndPointsVxlan = new TunnelEndPointsBuilder()
189                 .setIpAddress(ipAddress1).setInterfaceName(parentInterfaceName)
190                 .setTzMembership(ItmUtils.createTransportZoneMembership(transportZone1))
191                 .setTunnelType(tunnelType1)
192                 .withKey(new TunnelEndPointsKey(ipAddress1,tunnelType1)).build();
193         tunnelEndPointsVxlanNew = new TunnelEndPointsBuilder()
194                 .setIpAddress(ipAddress2).setInterfaceName(parentInterfaceName)
195                 .setTzMembership(ItmUtils.createTransportZoneMembership(transportZone1)).setTunnelType(tunnelType1)
196                 .build();
197         tunnelEndPointsListVxlan.add(tunnelEndPointsVxlan);
198         tunnelEndPointsListVxlanNew.add(tunnelEndPointsVxlanNew);
199         dpntePsInfoVxlan = new DPNTEPsInfoBuilder().setDPNID(dpId1).withKey(new DPNTEPsInfoKey(dpId1)).setUp(true)
200                 .setTunnelEndPoints(tunnelEndPointsListVxlan).build();
201         dpntePsInfoVxlanNew = new DPNTEPsInfoBuilder().setDPNID(dpId2).withKey(new DPNTEPsInfoKey(dpId2)).setUp(true)
202                 .setTunnelEndPoints(tunnelEndPointsListVxlanNew).setTunnelEndPoints(tunnelEndPointsListVxlanNew)
203                 .build();
204         tunnelMonitorParams = new TunnelMonitorParamsBuilder().setEnabled(true).setMonitorProtocol(monitorProtocol)
205                 .build();
206         tunnelMonitorInterval = new TunnelMonitorIntervalBuilder().setInterval(interval).build();
207         cfgdDpnListVxlan.add(dpntePsInfoVxlan);
208         meshDpnListVxlan.add(dpntePsInfoVxlan);
209         meshDpnListVxlan.add(dpntePsInfoVxlanNew);
210         dpnEndpoints = new DpnEndpointsBuilder().setDPNTEPsInfo(cfgdDpnListVxlan).build();
211
212         doReturn(mockReadTx).when(dataBroker).newReadOnlyTransaction();
213         doReturn(mockReadWriteTx).when(dataBroker).newReadWriteTransaction();
214         lenient().doReturn(FluentFutures.immediateNullFluentFuture()).when(mockReadWriteTx).commit();
215         doReturn(true).when(mockReadWriteTx).cancel();
216         lenient().doReturn(false).when(itmConfig).isUseOfTunnels();
217     }
218
219     // Since all the unit test cases will be written to the new way, this should be taken care then.
220     @Test
221     public void testDeleteTunnels() {
222
223         InstanceIdentifier<TunnelEndPoints> tunnelEndPointsIdentifier =
224                 InstanceIdentifier.builder(DpnEndpoints.class).child(DPNTEPsInfo.class, dpntePsInfoVxlan.key())
225                         .child(TunnelEndPoints.class,tunnelEndPointsVxlan.key()).build();
226         InstanceIdentifier<DpnEndpoints> dpnEndpointsIdentifier =
227                 InstanceIdentifier.builder(DpnEndpoints.class).build();
228         final InstanceIdentifier<DPNTEPsInfo> dpntePsInfoIdentifier = InstanceIdentifier.builder(DpnEndpoints.class)
229                 .child(DPNTEPsInfo.class, dpntePsInfoVxlan.key()).build();
230
231         Optional<DpnEndpoints> dpnEndpointsOptional = Optional.of(dpnEndpoints);
232
233         lenient().doReturn(FluentFutures.immediateFluentFuture(dpnEndpointsOptional)).when(mockReadTx).read(
234                 LogicalDatastoreType.CONFIGURATION,dpnEndpointsIdentifier);
235
236         itmInternalTunnelDeleteWorker.deleteTunnels(mdsalApiManager, cfgdDpnListVxlan,meshDpnListVxlan);
237         //FIXME: This verification is broken revisit this.
238         //verify(mockWriteTx).delete(LogicalDatastoreType.CONFIGURATION,tunnelEndPointsIdentifier);
239         verify(mockReadWriteTx).delete(LogicalDatastoreType.CONFIGURATION,dpntePsInfoIdentifier);
240     }
241 }