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.Futures;
18 import java.math.BigInteger;
19 import java.util.ArrayList;
20 import java.util.List;
21 import org.junit.After;
22 import org.junit.Before;
23 import org.junit.Test;
24 import org.junit.runner.RunWith;
25 import org.mockito.Mock;
26 import org.mockito.runners.MockitoJUnitRunner;
27 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
28 import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
29 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
30 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
31 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker;
32 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
33 import org.opendaylight.genius.itm.confighelpers.ItmInternalTunnelDeleteWorker;
34 import org.opendaylight.genius.itm.globals.ITMConstants;
35 import org.opendaylight.genius.mdsalutil.interfaces.IMdsalApiManager;
36 import org.opendaylight.genius.utils.cache.DataStoreCache;
37 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
38 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddressBuilder;
39 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpPrefix;
40 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpPrefixBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.IdManagerService;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rev160406.TunnelMonitoringTypeBase;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rev160406.TunnelMonitoringTypeBfd;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rev160406.TunnelTypeBase;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rev160406.TunnelTypeVxlan;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.config.rev160406.TunnelMonitorInterval;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.config.rev160406.TunnelMonitorIntervalBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.config.rev160406.TunnelMonitorParams;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.config.rev160406.TunnelMonitorParamsBuilder;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.op.rev160406.DpnEndpoints;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.op.rev160406.DpnEndpointsBuilder;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.op.rev160406.dpn.endpoints.DPNTEPsInfo;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.op.rev160406.dpn.endpoints.DPNTEPsInfoBuilder;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.op.rev160406.dpn.endpoints.DPNTEPsInfoKey;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.op.rev160406.dpn.endpoints.dpn.teps.info.TunnelEndPoints;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.op.rev160406.dpn.endpoints.dpn.teps.info.TunnelEndPointsBuilder;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.op.rev160406.dpn.endpoints.dpn.teps.info.TunnelEndPointsKey;
58 import org.opendaylight.yangtools.concepts.ListenerRegistration;
59 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
61 @RunWith(MockitoJUnitRunner.class)
62 public class ItmInternalTunnelDeleteTest {
64 BigInteger dpId1 = BigInteger.valueOf(1);
65 BigInteger dpId2 = BigInteger.valueOf(2);
68 String portName1 = "phy0";
69 String portName2 = "phy1" ;
70 String parentInterfaceName = "1:phy0:100" ;
71 String transportZone1 = "TZA" ;
72 String subnetIp = "10.1.1.24";
73 String tepIp1 = "192.168.56.101";
74 String tepIp2 = "192.168.56.102";
75 String gwyIp1 = "0.0.0.0";
76 String gwyIp2 = "0.0.0.1";
77 IpAddress ipAddress1 = null;
78 IpAddress ipAddress2 = null;
79 IpAddress gtwyIp1 = null;
80 IpAddress gtwyIp2 = null;
81 IpPrefix ipPrefixTest = null;
82 DPNTEPsInfo dpntePsInfoVxlan = null;
83 DPNTEPsInfo dpntePsInfoVxlanNew = null;
84 TunnelEndPoints tunnelEndPointsVxlan = null;
85 TunnelEndPoints tunnelEndPointsVxlanNew = null;
86 DpnEndpoints dpnEndpoints = null;
87 List<DPNTEPsInfo> meshDpnListVxlan = new ArrayList<>() ;
88 List<DPNTEPsInfo> cfgdDpnListVxlan = new ArrayList<>() ;
89 List<TunnelEndPoints> tunnelEndPointsListVxlan = new ArrayList<>();
90 List<TunnelEndPoints> tunnelEndPointsListVxlanNew = new ArrayList<>();
91 java.lang.Class<? extends TunnelTypeBase> tunnelType1 = TunnelTypeVxlan.class;
92 TunnelMonitorParams TunnelMonitorParams = null;
93 TunnelMonitorInterval tunnelMonitorInterval = null;
94 InstanceIdentifier<TunnelMonitorParams> TunnelMonitorParamsIdentifier = InstanceIdentifier.create(TunnelMonitorParams.class);
95 InstanceIdentifier<TunnelMonitorInterval> tunnelMonitorIntervalIdentifier = InstanceIdentifier.create(TunnelMonitorInterval.class);
96 Class<? extends TunnelMonitoringTypeBase> monitorProtocol = TunnelMonitoringTypeBfd.class;
98 @Mock DataBroker dataBroker;
99 @Mock ListenerRegistration<DataChangeListener> dataChangeListenerRegistration;
100 @Mock ReadOnlyTransaction mockReadTx;
101 @Mock WriteTransaction mockWriteTx;
102 @Mock IdManagerService idManagerService;
103 @Mock IMdsalApiManager iMdsalApiManager;
105 ItmInternalTunnelDeleteWorker itmInternalTunnelDeleteWorker = new ItmInternalTunnelDeleteWorker();
107 Optional<TunnelMonitorParams> TunnelMonitorParamsOptional ;
108 Optional<TunnelMonitorInterval> tunnelMonitorIntervalOptional ;
111 public void setUp() throws Exception {
112 when(dataBroker.registerDataChangeListener(
113 any(LogicalDatastoreType.class),
114 any(InstanceIdentifier.class),
115 any(DataChangeListener.class),
116 any(AsyncDataBroker.DataChangeScope.class)))
117 .thenReturn(dataChangeListenerRegistration);
119 DataStoreCache.create(ITMConstants.ITM_MONIRORING_PARAMS_CACHE_NAME);
121 TunnelMonitorParamsOptional = Optional.of(TunnelMonitorParams);
122 tunnelMonitorIntervalOptional = Optional.of(tunnelMonitorInterval);
124 doReturn(Futures.immediateCheckedFuture(TunnelMonitorParamsOptional)).when(mockReadTx).read(LogicalDatastoreType.CONFIGURATION,
125 TunnelMonitorParamsIdentifier);
126 doReturn(Futures.immediateCheckedFuture(tunnelMonitorIntervalOptional)).when(mockReadTx).read(LogicalDatastoreType.CONFIGURATION,
127 tunnelMonitorIntervalIdentifier);
132 public void cleanUp() {
135 private void setupMocks(){
137 ipAddress1 = IpAddressBuilder.getDefaultInstance(tepIp1);
138 ipAddress2 = IpAddressBuilder.getDefaultInstance(tepIp2);
139 ipPrefixTest = IpPrefixBuilder.getDefaultInstance(subnetIp + "/24");
140 gtwyIp1 = IpAddressBuilder.getDefaultInstance(gwyIp1);
141 gtwyIp2 = IpAddressBuilder.getDefaultInstance(gwyIp2);
142 tunnelEndPointsVxlan = new TunnelEndPointsBuilder().setVLANID(vlanId).setPortname(portName1).setIpAddress
143 (ipAddress1).setGwIpAddress(gtwyIp1).setInterfaceName(parentInterfaceName).setTzMembership
144 (ItmUtils.createTransportZoneMembership(transportZone1)).setTunnelType(tunnelType1).setSubnetMask
145 (ipPrefixTest).setKey(new TunnelEndPointsKey
146 (ipAddress1,portName1,tunnelType1,vlanId)).build();
147 tunnelEndPointsVxlanNew = new TunnelEndPointsBuilder().setVLANID(vlanId).setPortname(portName2).setIpAddress
148 (ipAddress2).setGwIpAddress(gtwyIp2).setInterfaceName(parentInterfaceName).setTzMembership
149 (ItmUtils.createTransportZoneMembership(transportZone1)).setTunnelType(tunnelType1).setSubnetMask
150 (ipPrefixTest).build();
151 tunnelEndPointsListVxlan.add(tunnelEndPointsVxlan);
152 tunnelEndPointsListVxlanNew.add(tunnelEndPointsVxlanNew);
153 dpntePsInfoVxlan = new DPNTEPsInfoBuilder().setDPNID(dpId1).setKey(new DPNTEPsInfoKey(dpId1)).setUp(true)
154 .setTunnelEndPoints(tunnelEndPointsListVxlan).build();
155 dpntePsInfoVxlanNew = new DPNTEPsInfoBuilder().setDPNID(dpId2).setKey(new DPNTEPsInfoKey(dpId1)).setUp(true)
156 .setTunnelEndPoints(tunnelEndPointsListVxlanNew).setTunnelEndPoints(tunnelEndPointsListVxlan).build();
157 TunnelMonitorParams = new TunnelMonitorParamsBuilder().setEnabled(true).setMonitorProtocol(monitorProtocol).build();
158 tunnelMonitorInterval = new TunnelMonitorIntervalBuilder().setInterval(interval).build();
159 cfgdDpnListVxlan.add(dpntePsInfoVxlan);
160 meshDpnListVxlan.add(dpntePsInfoVxlanNew);
161 dpnEndpoints = new DpnEndpointsBuilder().setDPNTEPsInfo(cfgdDpnListVxlan).build();
163 doReturn(mockReadTx).when(dataBroker).newReadOnlyTransaction();
164 doReturn(mockWriteTx).when(dataBroker).newWriteOnlyTransaction();
165 doReturn(Futures.immediateCheckedFuture(null)).when(mockWriteTx).submit();
169 public void testDeleteTunnels(){
171 InstanceIdentifier<TunnelEndPoints> tunnelEndPointsIdentifier =
172 InstanceIdentifier.builder(DpnEndpoints.class).child(DPNTEPsInfo.class, dpntePsInfoVxlan.getKey())
173 .child(TunnelEndPoints.class,tunnelEndPointsVxlan.getKey() ).build();
174 InstanceIdentifier<DpnEndpoints> dpnEndpointsIdentifier =
175 InstanceIdentifier.builder(DpnEndpoints.class).build();
176 InstanceIdentifier<DPNTEPsInfo> dpntePsInfoIdentifier =
177 InstanceIdentifier.builder(DpnEndpoints.class).child(DPNTEPsInfo.class, dpntePsInfoVxlan.getKey()).build();
179 Optional<DpnEndpoints> dpnEndpointsOptional = Optional.of(dpnEndpoints);
181 doReturn(Futures.immediateCheckedFuture(dpnEndpointsOptional)).when(mockReadTx).read(LogicalDatastoreType
182 .CONFIGURATION,dpnEndpointsIdentifier);
184 itmInternalTunnelDeleteWorker.deleteTunnels(dataBroker,idManagerService,iMdsalApiManager,cfgdDpnListVxlan,
187 verify(mockWriteTx).delete(LogicalDatastoreType.CONFIGURATION,tunnelEndPointsIdentifier);
188 verify(mockWriteTx).delete(LogicalDatastoreType.CONFIGURATION,dpntePsInfoIdentifier);