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