2 * Copyright (c) 2016 Ericsson India Global Services Pvt Ltd. and others. All rights reserved.
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
8 package org.opendaylight.genius.itm.impl;
10 import static org.mockito.Mockito.doReturn;
11 import static org.mockito.Mockito.lenient;
12 import static org.mockito.Mockito.verify;
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.Uint16;
72 import org.opendaylight.yangtools.yang.common.Uint64;
74 @RunWith(MockitoJUnitRunner.class)
75 public class ItmInternalTunnelDeleteTest {
77 Uint64 dpId1 = Uint64.ONE;
78 Uint64 dpId2 = Uint64.valueOf(2);
80 Uint16 interval = Uint16.valueOf(1000);
81 String portName1 = "phy0";
82 String portName2 = "phy1" ;
83 String parentInterfaceName = "1:phy0:100" ;
84 String transportZone1 = "TZA" ;
85 String subnetIp = "10.1.1.24";
86 String tepIp1 = "192.168.56.101";
87 String tepIp2 = "192.168.56.102";
88 String gwyIp1 = "0.0.0.0";
89 String gwyIp2 = "0.0.0.1";
90 IpAddress ipAddress1 = null;
91 IpAddress ipAddress2 = null;
92 IpAddress gtwyIp1 = null;
93 IpAddress gtwyIp2 = null;
94 IpPrefix ipPrefixTest = null;
95 DPNTEPsInfo dpntePsInfoVxlan = null;
96 DPNTEPsInfo dpntePsInfoVxlanNew = null;
97 TunnelEndPoints tunnelEndPointsVxlan = null;
98 TunnelEndPoints tunnelEndPointsVxlanNew = null;
99 DpnEndpoints dpnEndpoints = null;
100 List<DPNTEPsInfo> meshDpnListVxlan = new ArrayList<>() ;
101 List<DPNTEPsInfo> cfgdDpnListVxlan = new ArrayList<>() ;
102 List<TunnelEndPoints> tunnelEndPointsListVxlan = new ArrayList<>();
103 List<TunnelEndPoints> tunnelEndPointsListVxlanNew = new ArrayList<>();
104 Class<? extends TunnelTypeBase> tunnelType1 = TunnelTypeVxlan.class;
105 TunnelMonitorParams tunnelMonitorParams = null;
106 TunnelMonitorInterval tunnelMonitorInterval = null;
107 InternalTunnel internalTunnel = new InternalTunnelBuilder()
108 .setDestinationDPN(Uint64.ONE)
109 .setSourceDPN(Uint64.TWO)
110 .setTransportType(tunnelType1)
112 InstanceIdentifier<TunnelMonitorParams> tunnelMonitorParamsInstanceIdentifier =
113 InstanceIdentifier.create(TunnelMonitorParams.class);
114 InstanceIdentifier<TunnelMonitorInterval> tunnelMonitorIntervalIdentifier =
115 InstanceIdentifier.create(TunnelMonitorInterval.class);
116 Class<? extends TunnelMonitoringTypeBase> monitorProtocol = TunnelMonitoringTypeBfd.class;
117 InstanceIdentifier<InternalTunnel> internalTunnelIdentifier = InstanceIdentifier.builder(TunnelList.class)
118 .child(InternalTunnel.class, new InternalTunnelKey(Uint64.ONE, Uint64.TWO, tunnelType1))
121 @Mock DataBroker dataBroker;
122 @Mock ReadTransaction mockReadTx;
123 @Mock ReadWriteTransaction mockReadWriteTx;
124 @Mock IdManagerService idManagerService;
125 @Mock IMdsalApiManager mdsalApiManager;
126 @Mock JobCoordinator jobCoordinator;
127 @Mock IInterfaceManager interfaceManager;
128 @Mock ItmConfig itmConfig;
129 @Mock TunnelMonitoringConfig tunnelMonitoringConfig;
130 @Mock TombstonedNodeManager tombstonedNodeManager;
131 DirectTunnelUtils directTunnelUtils;
132 ItmInternalTunnelDeleteWorker itmInternalTunnelDeleteWorker;
133 UnprocessedNodeConnectorCache unprocessedNodeConnectorCache;
134 UnprocessedNodeConnectorEndPointCache unprocessedNodeConnectorEndPointCache;
135 OfEndPointCache ofEndPointCache;
138 Optional<TunnelMonitorParams> tunnelMonitorParamsOptional;
139 Optional<TunnelMonitorInterval> tunnelMonitorIntervalOptional ;
140 Optional<InternalTunnel> internalTunnelOptional ;
143 public void setUp() {
146 tunnelMonitorParamsOptional = Optional.of(tunnelMonitorParams);
147 tunnelMonitorIntervalOptional = Optional.of(tunnelMonitorInterval);
148 internalTunnelOptional = Optional.of(internalTunnel);
150 doReturn(FluentFutures.immediateFluentFuture(tunnelMonitorParamsOptional)).when(mockReadTx)
151 .read(LogicalDatastoreType.CONFIGURATION,
152 tunnelMonitorParamsInstanceIdentifier);
153 lenient().doReturn(FluentFutures.immediateFluentFuture(tunnelMonitorParamsOptional)).when(mockReadWriteTx)
154 .read(LogicalDatastoreType.CONFIGURATION,
155 tunnelMonitorParamsInstanceIdentifier);
156 lenient().doReturn(FluentFutures.immediateFluentFuture(tunnelMonitorIntervalOptional)).when(mockReadTx)
157 .read(LogicalDatastoreType.CONFIGURATION,
158 tunnelMonitorIntervalIdentifier);
159 lenient().doReturn(FluentFutures.immediateFluentFuture(tunnelMonitorIntervalOptional)).when(mockReadWriteTx)
160 .read(LogicalDatastoreType.CONFIGURATION,
161 tunnelMonitorIntervalIdentifier);
162 doReturn(FluentFutures.immediateFluentFuture(internalTunnelOptional)).when(mockReadTx)
163 .read(LogicalDatastoreType.CONFIGURATION, internalTunnelIdentifier);
164 lenient().doReturn(FluentFutures.immediateFluentFuture(internalTunnelOptional)).when(mockReadWriteTx)
165 .read(LogicalDatastoreType.CONFIGURATION, internalTunnelIdentifier);
167 DPNTEPsInfoCache dpntePsInfoCache =
168 new DPNTEPsInfoCache(dataBroker, new GuavaCacheProvider(new CacheManagersRegistryImpl()),
169 directTunnelUtils, jobCoordinator, unprocessedNodeConnectorEndPointCache);
171 itmInternalTunnelDeleteWorker = new ItmInternalTunnelDeleteWorker(dataBroker, jobCoordinator,
172 new TunnelMonitoringConfig(dataBroker, new GuavaCacheProvider(new CacheManagersRegistryImpl())),
173 interfaceManager, new DpnTepStateCache(dataBroker, jobCoordinator,
174 new GuavaCacheProvider(new CacheManagersRegistryImpl()), directTunnelUtils, dpntePsInfoCache,
175 unprocessedNodeConnectorCache, unprocessedNodeConnectorEndPointCache),
176 new OvsBridgeEntryCache(dataBroker, new GuavaCacheProvider(new CacheManagersRegistryImpl())),
177 new OvsBridgeRefEntryCache(dataBroker, new GuavaCacheProvider(new CacheManagersRegistryImpl())),
178 new TunnelStateCache(dataBroker, new GuavaCacheProvider(new CacheManagersRegistryImpl())),
179 directTunnelUtils, tombstonedNodeManager);
183 public void cleanUp() {
186 private void setupMocks() {
188 ipAddress1 = IpAddressBuilder.getDefaultInstance(tepIp1);
189 ipAddress2 = IpAddressBuilder.getDefaultInstance(tepIp2);
190 ipPrefixTest = IpPrefixBuilder.getDefaultInstance(subnetIp + "/24");
191 gtwyIp1 = IpAddressBuilder.getDefaultInstance(gwyIp1);
192 gtwyIp2 = IpAddressBuilder.getDefaultInstance(gwyIp2);
193 tunnelEndPointsVxlan = new TunnelEndPointsBuilder()
194 .setIpAddress(ipAddress1).setInterfaceName(parentInterfaceName)
195 .setTzMembership(ItmUtils.createTransportZoneMembership(transportZone1))
196 .setTunnelType(tunnelType1)
197 .withKey(new TunnelEndPointsKey(ipAddress1,tunnelType1)).build();
198 tunnelEndPointsVxlanNew = new TunnelEndPointsBuilder()
199 .setIpAddress(ipAddress2).setInterfaceName(parentInterfaceName)
200 .setTzMembership(ItmUtils.createTransportZoneMembership(transportZone1)).setTunnelType(tunnelType1)
202 tunnelEndPointsListVxlan.add(tunnelEndPointsVxlan);
203 tunnelEndPointsListVxlanNew.add(tunnelEndPointsVxlanNew);
204 dpntePsInfoVxlan = new DPNTEPsInfoBuilder().setDPNID(dpId1).withKey(new DPNTEPsInfoKey(dpId1)).setUp(true)
205 .setTunnelEndPoints(tunnelEndPointsListVxlan).build();
206 dpntePsInfoVxlanNew = new DPNTEPsInfoBuilder().setDPNID(dpId2).withKey(new DPNTEPsInfoKey(dpId2)).setUp(true)
207 .setTunnelEndPoints(tunnelEndPointsListVxlanNew).setTunnelEndPoints(tunnelEndPointsListVxlanNew)
209 tunnelMonitorParams = new TunnelMonitorParamsBuilder().setEnabled(true).setMonitorProtocol(monitorProtocol)
211 tunnelMonitorInterval = new TunnelMonitorIntervalBuilder().setInterval(interval).build();
212 cfgdDpnListVxlan.add(dpntePsInfoVxlan);
213 meshDpnListVxlan.add(dpntePsInfoVxlan);
214 meshDpnListVxlan.add(dpntePsInfoVxlanNew);
215 dpnEndpoints = new DpnEndpointsBuilder().setDPNTEPsInfo(cfgdDpnListVxlan).build();
217 doReturn(mockReadTx).when(dataBroker).newReadOnlyTransaction();
218 doReturn(mockReadWriteTx).when(dataBroker).newReadWriteTransaction();
219 lenient().doReturn(FluentFutures.immediateNullFluentFuture()).when(mockReadWriteTx).commit();
220 doReturn(true).when(mockReadWriteTx).cancel();
221 lenient().doReturn(false).when(itmConfig).isUseOfTunnels();
224 // Since all the unit test cases will be written to the new way, this should be taken care then.
226 public void testDeleteTunnels() {
228 InstanceIdentifier<TunnelEndPoints> tunnelEndPointsIdentifier =
229 InstanceIdentifier.builder(DpnEndpoints.class).child(DPNTEPsInfo.class, dpntePsInfoVxlan.key())
230 .child(TunnelEndPoints.class,tunnelEndPointsVxlan.key()).build();
231 InstanceIdentifier<DpnEndpoints> dpnEndpointsIdentifier =
232 InstanceIdentifier.builder(DpnEndpoints.class).build();
233 final InstanceIdentifier<DPNTEPsInfo> dpntePsInfoIdentifier = InstanceIdentifier.builder(DpnEndpoints.class)
234 .child(DPNTEPsInfo.class, dpntePsInfoVxlan.key()).build();
236 Optional<DpnEndpoints> dpnEndpointsOptional = Optional.of(dpnEndpoints);
238 lenient().doReturn(FluentFutures.immediateFluentFuture(dpnEndpointsOptional)).when(mockReadTx).read(
239 LogicalDatastoreType.CONFIGURATION,dpnEndpointsIdentifier);
241 itmInternalTunnelDeleteWorker.deleteTunnels(mdsalApiManager, cfgdDpnListVxlan,meshDpnListVxlan);
242 //FIXME: This verification is broken revisit this.
243 //verify(mockWriteTx).delete(LogicalDatastoreType.CONFIGURATION,tunnelEndPointsIdentifier);
244 verify(mockReadWriteTx).delete(LogicalDatastoreType.CONFIGURATION,dpntePsInfoIdentifier);