Bump upstreams for Silicon
[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     Class<? extends TunnelTypeBase> tunnelType1 = TunnelTypeVxlan.class;
104     TunnelMonitorParams tunnelMonitorParams = null;
105     TunnelMonitorInterval tunnelMonitorInterval = null;
106     InternalTunnel internalTunnel = new InternalTunnelBuilder()
107             .setDestinationDPN(Uint64.ONE)
108             .setSourceDPN(Uint64.TWO)
109             .setTransportType(tunnelType1)
110             .build();
111     InstanceIdentifier<TunnelMonitorParams> tunnelMonitorParamsInstanceIdentifier =
112             InstanceIdentifier.create(TunnelMonitorParams.class);
113     InstanceIdentifier<TunnelMonitorInterval> tunnelMonitorIntervalIdentifier =
114             InstanceIdentifier.create(TunnelMonitorInterval.class);
115     Class<? extends TunnelMonitoringTypeBase> monitorProtocol = TunnelMonitoringTypeBfd.class;
116     InstanceIdentifier<InternalTunnel> internalTunnelIdentifier = InstanceIdentifier.builder(TunnelList.class)
117             .child(InternalTunnel.class, new InternalTunnelKey(Uint64.ONE, Uint64.TWO, tunnelType1))
118             .build();
119
120     @Mock DataBroker dataBroker;
121     @Mock ReadTransaction mockReadTx;
122     @Mock ReadWriteTransaction mockReadWriteTx;
123     @Mock IdManagerService idManagerService;
124     @Mock IMdsalApiManager mdsalApiManager;
125     @Mock JobCoordinator jobCoordinator;
126     @Mock IInterfaceManager interfaceManager;
127     @Mock ItmConfig itmConfig;
128     @Mock TunnelMonitoringConfig tunnelMonitoringConfig;
129     @Mock TombstonedNodeManager tombstonedNodeManager;
130     DirectTunnelUtils directTunnelUtils;
131     ItmInternalTunnelDeleteWorker itmInternalTunnelDeleteWorker;
132     UnprocessedNodeConnectorCache unprocessedNodeConnectorCache;
133     UnprocessedNodeConnectorEndPointCache unprocessedNodeConnectorEndPointCache;
134     OfEndPointCache ofEndPointCache;
135
136
137     Optional<TunnelMonitorParams> tunnelMonitorParamsOptional;
138     Optional<TunnelMonitorInterval> tunnelMonitorIntervalOptional ;
139     Optional<InternalTunnel> internalTunnelOptional ;
140
141     @Before
142     public void setUp() {
143         setupMocks();
144
145         tunnelMonitorParamsOptional = Optional.of(tunnelMonitorParams);
146         tunnelMonitorIntervalOptional = Optional.of(tunnelMonitorInterval);
147         internalTunnelOptional = Optional.of(internalTunnel);
148
149         doReturn(FluentFutures.immediateFluentFuture(tunnelMonitorParamsOptional)).when(mockReadTx)
150                 .read(LogicalDatastoreType.CONFIGURATION,
151                         tunnelMonitorParamsInstanceIdentifier);
152         lenient().doReturn(FluentFutures.immediateFluentFuture(tunnelMonitorParamsOptional)).when(mockReadWriteTx)
153                 .read(LogicalDatastoreType.CONFIGURATION,
154                         tunnelMonitorParamsInstanceIdentifier);
155         lenient().doReturn(FluentFutures.immediateFluentFuture(tunnelMonitorIntervalOptional)).when(mockReadTx)
156                 .read(LogicalDatastoreType.CONFIGURATION,
157                         tunnelMonitorIntervalIdentifier);
158         lenient().doReturn(FluentFutures.immediateFluentFuture(tunnelMonitorIntervalOptional)).when(mockReadWriteTx)
159                 .read(LogicalDatastoreType.CONFIGURATION,
160                         tunnelMonitorIntervalIdentifier);
161         doReturn(FluentFutures.immediateFluentFuture(internalTunnelOptional)).when(mockReadTx)
162                 .read(LogicalDatastoreType.CONFIGURATION, internalTunnelIdentifier);
163         lenient().doReturn(FluentFutures.immediateFluentFuture(internalTunnelOptional)).when(mockReadWriteTx)
164                 .read(LogicalDatastoreType.CONFIGURATION, internalTunnelIdentifier);
165
166         DPNTEPsInfoCache dpntePsInfoCache =
167                 new DPNTEPsInfoCache(dataBroker, new GuavaCacheProvider(new CacheManagersRegistryImpl()),
168                         directTunnelUtils, jobCoordinator, unprocessedNodeConnectorEndPointCache);
169
170         itmInternalTunnelDeleteWorker = new ItmInternalTunnelDeleteWorker(dataBroker, jobCoordinator,
171             new TunnelMonitoringConfig(dataBroker, new GuavaCacheProvider(new CacheManagersRegistryImpl())),
172             interfaceManager, new DpnTepStateCache(dataBroker, jobCoordinator,
173             new GuavaCacheProvider(new CacheManagersRegistryImpl()), directTunnelUtils, dpntePsInfoCache,
174                 unprocessedNodeConnectorCache, unprocessedNodeConnectorEndPointCache),
175             new OvsBridgeEntryCache(dataBroker, new GuavaCacheProvider(new CacheManagersRegistryImpl())),
176             new OvsBridgeRefEntryCache(dataBroker, new GuavaCacheProvider(new CacheManagersRegistryImpl())),
177             new TunnelStateCache(dataBroker, new GuavaCacheProvider(new CacheManagersRegistryImpl())),
178             directTunnelUtils, tombstonedNodeManager);
179     }
180
181     @After
182     public void cleanUp() {
183     }
184
185     private void setupMocks() {
186
187         ipAddress1 = IpAddressBuilder.getDefaultInstance(tepIp1);
188         ipAddress2 = IpAddressBuilder.getDefaultInstance(tepIp2);
189         ipPrefixTest = IpPrefixBuilder.getDefaultInstance(subnetIp + "/24");
190         gtwyIp1 = IpAddressBuilder.getDefaultInstance(gwyIp1);
191         gtwyIp2 = IpAddressBuilder.getDefaultInstance(gwyIp2);
192         tunnelEndPointsVxlan = new TunnelEndPointsBuilder()
193                 .setIpAddress(ipAddress1).setInterfaceName(parentInterfaceName)
194                 .setTzMembership(ItmUtils.createTransportZoneMembership(transportZone1))
195                 .setTunnelType(tunnelType1)
196                 .withKey(new TunnelEndPointsKey(ipAddress1,tunnelType1)).build();
197         tunnelEndPointsVxlanNew = new TunnelEndPointsBuilder()
198                 .setIpAddress(ipAddress2).setInterfaceName(parentInterfaceName)
199                 .setTzMembership(ItmUtils.createTransportZoneMembership(transportZone1)).setTunnelType(tunnelType1)
200                 .build();
201         tunnelEndPointsListVxlan.add(tunnelEndPointsVxlan);
202         tunnelEndPointsListVxlanNew.add(tunnelEndPointsVxlanNew);
203         dpntePsInfoVxlan = new DPNTEPsInfoBuilder().setDPNID(dpId1).withKey(new DPNTEPsInfoKey(dpId1)).setUp(true)
204                 .setTunnelEndPoints(tunnelEndPointsListVxlan).build();
205         dpntePsInfoVxlanNew = new DPNTEPsInfoBuilder().setDPNID(dpId2).withKey(new DPNTEPsInfoKey(dpId2)).setUp(true)
206                 .setTunnelEndPoints(tunnelEndPointsListVxlanNew).setTunnelEndPoints(tunnelEndPointsListVxlanNew)
207                 .build();
208         tunnelMonitorParams = new TunnelMonitorParamsBuilder().setEnabled(true).setMonitorProtocol(monitorProtocol)
209                 .build();
210         tunnelMonitorInterval = new TunnelMonitorIntervalBuilder().setInterval(interval).build();
211         cfgdDpnListVxlan.add(dpntePsInfoVxlan);
212         meshDpnListVxlan.add(dpntePsInfoVxlan);
213         meshDpnListVxlan.add(dpntePsInfoVxlanNew);
214         dpnEndpoints = new DpnEndpointsBuilder().setDPNTEPsInfo(cfgdDpnListVxlan).build();
215
216         doReturn(mockReadTx).when(dataBroker).newReadOnlyTransaction();
217         doReturn(mockReadWriteTx).when(dataBroker).newReadWriteTransaction();
218         lenient().doReturn(FluentFutures.immediateNullFluentFuture()).when(mockReadWriteTx).commit();
219         doReturn(true).when(mockReadWriteTx).cancel();
220         lenient().doReturn(false).when(itmConfig).isUseOfTunnels();
221     }
222
223     // Since all the unit test cases will be written to the new way, this should be taken care then.
224     @Test
225     public void testDeleteTunnels() {
226
227         InstanceIdentifier<TunnelEndPoints> tunnelEndPointsIdentifier =
228                 InstanceIdentifier.builder(DpnEndpoints.class).child(DPNTEPsInfo.class, dpntePsInfoVxlan.key())
229                         .child(TunnelEndPoints.class,tunnelEndPointsVxlan.key()).build();
230         InstanceIdentifier<DpnEndpoints> dpnEndpointsIdentifier =
231                 InstanceIdentifier.builder(DpnEndpoints.class).build();
232         final InstanceIdentifier<DPNTEPsInfo> dpntePsInfoIdentifier = InstanceIdentifier.builder(DpnEndpoints.class)
233                 .child(DPNTEPsInfo.class, dpntePsInfoVxlan.key()).build();
234
235         Optional<DpnEndpoints> dpnEndpointsOptional = Optional.of(dpnEndpoints);
236
237         lenient().doReturn(FluentFutures.immediateFluentFuture(dpnEndpointsOptional)).when(mockReadTx).read(
238                 LogicalDatastoreType.CONFIGURATION,dpnEndpointsIdentifier);
239
240         itmInternalTunnelDeleteWorker.deleteTunnels(mdsalApiManager, cfgdDpnListVxlan,meshDpnListVxlan);
241         //FIXME: This verification is broken revisit this.
242         //verify(mockWriteTx).delete(LogicalDatastoreType.CONFIGURATION,tunnelEndPointsIdentifier);
243         verify(mockReadWriteTx).delete(LogicalDatastoreType.CONFIGURATION,dpntePsInfoIdentifier);
244     }
245 }