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 com.google.common.base.Optional;
12 import com.google.common.util.concurrent.Futures;
13 import org.junit.After;
14 import org.junit.Before;
15 import org.junit.Test;
16 import org.junit.runner.RunWith;
17 import org.mockito.Mock;
18 import org.mockito.runners.MockitoJUnitRunner;
19 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
20 import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
21 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
22 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
23 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker;
24 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
25 import org.opendaylight.genius.itm.confighelpers.ItmInternalTunnelDeleteWorker;
26 import org.opendaylight.genius.itm.globals.ITMConstants;
27 import org.opendaylight.genius.mdsalutil.interfaces.IMdsalApiManager;
28 import org.opendaylight.genius.utils.cache.DataStoreCache;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddressBuilder;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpPrefix;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpPrefixBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.IdManagerService;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rev160406.TunnelMonitoringTypeBase;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rev160406.TunnelMonitoringTypeBfd;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rev160406.TunnelTypeBase;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rev160406.TunnelTypeVxlan;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.config.rev160406.TunnelMonitorInterval;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.config.rev160406.TunnelMonitorIntervalBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.config.rev160406.TunnelMonitorParams;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.config.rev160406.TunnelMonitorParamsBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.op.rev160406.DpnEndpoints;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.op.rev160406.DpnEndpointsBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.op.rev160406.dpn.endpoints.DPNTEPsInfo;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.op.rev160406.dpn.endpoints.DPNTEPsInfoBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.op.rev160406.dpn.endpoints.DPNTEPsInfoKey;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.op.rev160406.dpn.endpoints.dpn.teps.info
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.op.rev160406.dpn.endpoints.dpn.teps.info
50 .TunnelEndPointsBuilder;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.op.rev160406.dpn.endpoints.dpn.teps.info
53 import org.opendaylight.yangtools.concepts.ListenerRegistration;
54 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
56 import java.math.BigInteger;
57 import java.util.ArrayList;
58 import java.util.List;
60 import static org.mockito.Matchers.any;
61 import static org.mockito.Mockito.doReturn;
62 import static org.mockito.Mockito.verify;
63 import static org.mockito.Mockito.when;
65 @RunWith(MockitoJUnitRunner.class)
66 public class ItmInternalTunnelDeleteTest {
68 BigInteger dpId1 = BigInteger.valueOf(1);
69 BigInteger dpId2 = BigInteger.valueOf(2);
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 InstanceIdentifier<TunnelMonitorParams> TunnelMonitorParamsIdentifier = InstanceIdentifier.create(TunnelMonitorParams.class);
99 InstanceIdentifier<TunnelMonitorInterval> tunnelMonitorIntervalIdentifier = InstanceIdentifier.create(TunnelMonitorInterval.class);
100 Class<? extends TunnelMonitoringTypeBase> monitorProtocol = TunnelMonitoringTypeBfd.class;
102 @Mock DataBroker dataBroker;
103 @Mock ListenerRegistration<DataChangeListener> dataChangeListenerRegistration;
104 @Mock ReadOnlyTransaction mockReadTx;
105 @Mock WriteTransaction mockWriteTx;
106 @Mock IdManagerService idManagerService;
107 @Mock IMdsalApiManager iMdsalApiManager;
109 ItmInternalTunnelDeleteWorker itmInternalTunnelDeleteWorker = new ItmInternalTunnelDeleteWorker();
111 Optional<TunnelMonitorParams> TunnelMonitorParamsOptional ;
112 Optional<TunnelMonitorInterval> tunnelMonitorIntervalOptional ;
115 public void setUp() throws Exception {
116 when(dataBroker.registerDataChangeListener(
117 any(LogicalDatastoreType.class),
118 any(InstanceIdentifier.class),
119 any(DataChangeListener.class),
120 any(AsyncDataBroker.DataChangeScope.class)))
121 .thenReturn(dataChangeListenerRegistration);
123 DataStoreCache.create(ITMConstants.ITM_MONIRORING_PARAMS_CACHE_NAME);
125 TunnelMonitorParamsOptional = Optional.of(TunnelMonitorParams);
126 tunnelMonitorIntervalOptional = Optional.of(tunnelMonitorInterval);
128 doReturn(Futures.immediateCheckedFuture(TunnelMonitorParamsOptional)).when(mockReadTx).read(LogicalDatastoreType.CONFIGURATION,
129 TunnelMonitorParamsIdentifier);
130 doReturn(Futures.immediateCheckedFuture(tunnelMonitorIntervalOptional)).when(mockReadTx).read(LogicalDatastoreType.CONFIGURATION,
131 tunnelMonitorIntervalIdentifier);
136 public void cleanUp() {
139 private void setupMocks(){
141 ipAddress1 = IpAddressBuilder.getDefaultInstance(tepIp1);
142 ipAddress2 = IpAddressBuilder.getDefaultInstance(tepIp2);
143 ipPrefixTest = IpPrefixBuilder.getDefaultInstance(subnetIp + "/24");
144 gtwyIp1 = IpAddressBuilder.getDefaultInstance(gwyIp1);
145 gtwyIp2 = IpAddressBuilder.getDefaultInstance(gwyIp2);
146 tunnelEndPointsVxlan = new TunnelEndPointsBuilder().setVLANID(vlanId).setPortname(portName1).setIpAddress
147 (ipAddress1).setGwIpAddress(gtwyIp1).setInterfaceName(parentInterfaceName).setTzMembership
148 (ItmUtils.createTransportZoneMembership(transportZone1)).setTunnelType(tunnelType1).setSubnetMask
149 (ipPrefixTest).setKey(new TunnelEndPointsKey
150 (ipAddress1,portName1,tunnelType1,vlanId)).build();
151 tunnelEndPointsVxlanNew = new TunnelEndPointsBuilder().setVLANID(vlanId).setPortname(portName2).setIpAddress
152 (ipAddress2).setGwIpAddress(gtwyIp2).setInterfaceName(parentInterfaceName).setTzMembership
153 (ItmUtils.createTransportZoneMembership(transportZone1)).setTunnelType(tunnelType1).setSubnetMask
154 (ipPrefixTest).build();
155 tunnelEndPointsListVxlan.add(tunnelEndPointsVxlan);
156 tunnelEndPointsListVxlanNew.add(tunnelEndPointsVxlanNew);
157 dpntePsInfoVxlan = new DPNTEPsInfoBuilder().setDPNID(dpId1).setKey(new DPNTEPsInfoKey(dpId1)).setUp(true)
158 .setTunnelEndPoints(tunnelEndPointsListVxlan).build();
159 dpntePsInfoVxlanNew = new DPNTEPsInfoBuilder().setDPNID(dpId2).setKey(new DPNTEPsInfoKey(dpId1)).setUp(true)
160 .setTunnelEndPoints(tunnelEndPointsListVxlanNew).setTunnelEndPoints(tunnelEndPointsListVxlan).build();
161 TunnelMonitorParams = new TunnelMonitorParamsBuilder().setEnabled(true).setMonitorProtocol(monitorProtocol).build();
162 tunnelMonitorInterval = new TunnelMonitorIntervalBuilder().setInterval(interval).build();
163 cfgdDpnListVxlan.add(dpntePsInfoVxlan);
164 meshDpnListVxlan.add(dpntePsInfoVxlanNew);
165 dpnEndpoints = new DpnEndpointsBuilder().setDPNTEPsInfo(cfgdDpnListVxlan).build();
167 doReturn(mockReadTx).when(dataBroker).newReadOnlyTransaction();
168 doReturn(mockWriteTx).when(dataBroker).newWriteOnlyTransaction();
169 doReturn(Futures.immediateCheckedFuture(null)).when(mockWriteTx).submit();
173 public void testDeleteTunnels(){
175 InstanceIdentifier<TunnelEndPoints> tunnelEndPointsIdentifier =
176 InstanceIdentifier.builder(DpnEndpoints.class).child(DPNTEPsInfo.class, dpntePsInfoVxlan.getKey())
177 .child(TunnelEndPoints.class,tunnelEndPointsVxlan.getKey() ).build();
178 InstanceIdentifier<DpnEndpoints> dpnEndpointsIdentifier =
179 InstanceIdentifier.builder(DpnEndpoints.class).build();
180 InstanceIdentifier<DPNTEPsInfo> dpntePsInfoIdentifier =
181 InstanceIdentifier.builder(DpnEndpoints.class).child(DPNTEPsInfo.class, dpntePsInfoVxlan.getKey()).build();
183 Optional<DpnEndpoints> dpnEndpointsOptional = Optional.of(dpnEndpoints);
185 doReturn(Futures.immediateCheckedFuture(dpnEndpointsOptional)).when(mockReadTx).read(LogicalDatastoreType
186 .CONFIGURATION,dpnEndpointsIdentifier);
188 itmInternalTunnelDeleteWorker.deleteTunnels(dataBroker,idManagerService,iMdsalApiManager,cfgdDpnListVxlan,
191 verify(mockWriteTx).delete(LogicalDatastoreType.CONFIGURATION,tunnelEndPointsIdentifier);
192 verify(mockWriteTx).delete(LogicalDatastoreType.CONFIGURATION,dpntePsInfoIdentifier);