Bump odlparent->6.0.0,mdsal->5.0.3
[netvirt.git] / elanmanager / impl / src / test / java / org / opendaylight / netvirt / elanmanager / tests / ElanServiceTest.java
1 /*
2  * Copyright (C) 2016, 2017 Red Hat Inc., 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.netvirt.elanmanager.tests;
9
10 import static java.util.Arrays.asList;
11 import static org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType.CONFIGURATION;
12 import static org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType.OPERATIONAL;
13
14 import com.google.common.base.Optional;
15 import java.util.List;
16 import javax.inject.Inject;
17 import org.junit.After;
18 import org.junit.Before;
19 import org.junit.Ignore;
20 import org.junit.Rule;
21 import org.junit.Test;
22 import org.junit.rules.MethodRule;
23 import org.mockito.Mockito;
24 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
25 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
26 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
27 import org.opendaylight.genius.datastoreutils.SingleTransactionDataBroker;
28 import org.opendaylight.genius.datastoreutils.testutils.JobCoordinatorTestModule;
29 import org.opendaylight.genius.interfacemanager.globals.InterfaceInfo;
30 import org.opendaylight.genius.mdsalutil.MDSALUtil;
31 import org.opendaylight.genius.mdsalutil.NwConstants;
32 import org.opendaylight.genius.mdsalutil.cache.InstanceIdDataObjectCache;
33 import org.opendaylight.genius.testutils.interfacemanager.TunnelInterfaceDetails;
34 import org.opendaylight.genius.utils.batching.ResourceBatchingManager;
35 import org.opendaylight.infrautils.caches.CacheProvider;
36 import org.opendaylight.infrautils.caches.testutils.CacheModule;
37 import org.opendaylight.infrautils.inject.guice.testutils.GuiceRule;
38 import org.opendaylight.infrautils.jobcoordinator.JobCoordinator;
39 import org.opendaylight.infrautils.jobcoordinator.internal.JobCoordinatorImpl;
40 import org.opendaylight.infrautils.metrics.MetricProvider;
41 import org.opendaylight.infrautils.metrics.testimpl.TestMetricProviderImpl;
42 import org.opendaylight.infrautils.testutils.LogRule;
43 import org.opendaylight.mdsal.binding.testutils.AssertDataObjects;
44 import org.opendaylight.mdsal.eos.binding.api.EntityOwnershipService;
45 import org.opendaylight.netvirt.bgpmanager.api.IBgpManager;
46 import org.opendaylight.netvirt.elan.cache.ElanInstanceDpnsCache;
47 import org.opendaylight.netvirt.elan.evpn.listeners.ElanMacEntryListener;
48 import org.opendaylight.netvirt.elan.evpn.listeners.EvpnElanInstanceListener;
49 import org.opendaylight.netvirt.elan.evpn.listeners.MacVrfEntryListener;
50 import org.opendaylight.netvirt.elan.evpn.utils.EvpnUtils;
51 import org.opendaylight.netvirt.elan.internal.ElanDpnInterfaceClusteredListener;
52 import org.opendaylight.netvirt.elan.internal.ElanExtnTepConfigListener;
53 import org.opendaylight.netvirt.elan.internal.ElanExtnTepListener;
54 import org.opendaylight.netvirt.elan.internal.ElanInterfaceManager;
55 import org.opendaylight.netvirt.elan.l2gw.listeners.HwvtepPhysicalSwitchListener;
56 import org.opendaylight.netvirt.elan.l2gw.listeners.L2GatewayConnectionListener;
57 import org.opendaylight.netvirt.elan.l2gw.listeners.LocalUcastMacListener;
58 import org.opendaylight.netvirt.elan.l2gw.nodehandlertest.DataProvider;
59 import org.opendaylight.netvirt.elan.l2gw.nodehandlertest.PhysicalSwitchHelper;
60 import org.opendaylight.netvirt.elan.l2gw.utils.ElanL2GatewayUtils;
61 import org.opendaylight.netvirt.elan.utils.ElanUtils;
62 import org.opendaylight.netvirt.elanmanager.api.IElanService;
63 import org.opendaylight.netvirt.elanmanager.api.IL2gwService;
64 import org.opendaylight.netvirt.elanmanager.tests.utils.EvpnTestHelper;
65 import org.opendaylight.netvirt.elanmanager.utils.ElanL2GwCacheUtils;
66 import org.opendaylight.netvirt.neutronvpn.api.l2gw.L2GatewayCache;
67 import org.opendaylight.netvirt.neutronvpn.l2gw.L2GatewayListener;
68 import org.opendaylight.netvirt.vpnmanager.api.IVpnManager;
69 import org.opendaylight.yang.gen.v1.urn.ericsson.params.xml.ns.yang.ebgp.rev150901.Bgp;
70 import org.opendaylight.yang.gen.v1.urn.ericsson.params.xml.ns.yang.ebgp.rev150901.bgp.Networks;
71 import org.opendaylight.yang.gen.v1.urn.ericsson.params.xml.ns.yang.ebgp.rev150901.bgp.NetworksKey;
72 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Uri;
73 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
74 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
75 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.IdManagerService;
76 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rpcs.rev160406.OdlInterfaceRpcService;
77 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rpcs.rev160406.ItmRpcService;
78 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.ElanInstances;
79 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.instances.ElanInstance;
80 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.instances.ElanInstanceKey;
81 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalAugmentation;
82 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LocalUcastMacs;
83 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LogicalSwitches;
84 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
85 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
86 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
87 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
88 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
89 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
90 import org.slf4j.Logger;
91 import org.slf4j.LoggerFactory;
92
93
94 /**
95  * End-to-end test of IElanService.
96  *
97  * @author Michael Vorburger
98  * @author Riyazahmed Talikoti
99  */
100 public class ElanServiceTest extends  ElanServiceTestBase {
101
102     private static final Logger LOG = LoggerFactory.getLogger(ElanServiceTest.class);
103
104     // TODO as-is, this test is flaky; as uncommenting will show
105     // Uncomment this to keep running this test indefinitely
106     // This is very useful to detect concurrency issues (such as https://bugs.opendaylight.org/show_bug.cgi?id=7538)
107     // public static @ClassRule RunUntilFailureClassRule classRepeater = new RunUntilFailureClassRule();
108     // public @Rule RunUntilFailureRule repeater = new RunUntilFailureRule(classRepeater);
109
110     public @Rule LogRule logRule = new LogRule();
111     public @Rule MethodRule guice = new GuiceRule(ElanServiceTestModule.class, JobCoordinatorTestModule.class,
112             CacheModule.class);
113     // TODO re-enable after we can await completion of listeners and DJC:
114     // Otherwise this too frequently causes spurious test failures, e.g. due to error
115     // logs Caused by: java.lang.RuntimeException: java.util.concurrent.ExecutionException: Operation was interrupted
116     // public @Rule LogCaptureRule logCaptureRule = new LogCaptureRule();
117     private @Inject IElanService elanService;
118     private @Inject IdManagerService idManager;
119     private @Inject EvpnElanInstanceListener evpnElanInstanceListener;
120     private @Inject ElanMacEntryListener elanMacEntryListener;
121     private @Inject MacVrfEntryListener macVrfEntryListener;
122     private @Inject EvpnUtils evpnUtils;
123     private @Inject IBgpManager bgpManager;
124     private @Inject IVpnManager vpnManager;
125     private @Inject EvpnTestHelper evpnTestHelper;
126     private @Inject OdlInterfaceRpcService odlInterfaceRpcService;
127     private @Inject ElanL2GatewayUtils elanL2GatewayUtils;
128     private @Inject ElanInterfaceManager elanInterfaceManager;
129     private @Inject HwvtepPhysicalSwitchListener hwvtepPhysicalSwitchListener;
130     private @Inject L2GatewayConnectionListener l2GatewayConnectionListener;
131     private @Inject LocalUcastMacListener localUcastMacListener;
132     private @Inject ElanDpnInterfaceClusteredListener elanDpnInterfaceClusteredListener;
133     private @Inject EntityOwnershipService mockedEntityOwnershipService;
134     private @Inject L2GatewayCache l2GatewayCache;
135     private @Inject ElanUtils elanUtils;
136     private @Inject ElanInstanceDpnsCache elanInstanceDpnsCache;
137     private @Inject ElanExtnTepConfigListener elanExtnTepConfigListener;
138     private @Inject ElanExtnTepListener elanExtnTepListener;
139     private @Inject CacheProvider cacheProvider;
140
141     private L2GatewayListener l2gwListener;
142     private final MetricProvider metricProvider = new TestMetricProviderImpl();
143
144     private Verifications verifications;
145     private L2gwBuilders l2gwBuilders;
146
147     private SingleTransactionDataBroker singleTxdataBroker;
148
149     private InstanceIdDataObjectCache<LogicalSwitches> logicalSwitchCache;
150
151     @Before public void before() throws Exception {
152         singleTxdataBroker = new SingleTransactionDataBroker(dataBroker);
153         logicalSwitchCache = new InstanceIdDataObjectCache<LogicalSwitches>(
154                 LogicalSwitches.class, dataBroker, CONFIGURATION,
155                 InstanceIdentifier.builder(NetworkTopology.class)
156                         .child(Topology.class, new TopologyKey(new TopologyId(new Uri("hwvtep:1"))))
157                         .child(org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang
158                                 .network.topology.rev131021.network.topology.topology.Node.class)
159                         .augmentation(HwvtepGlobalAugmentation.class)
160                         .child(LogicalSwitches.class).build(), cacheProvider) {
161             @Override
162             protected void added(InstanceIdentifier<LogicalSwitches> path, LogicalSwitches dataObject) {
163                 new Thread(() -> {
164                     try {
165                         singleTxdataBroker.syncWrite(OPERATIONAL, path, dataObject);
166                     } catch (TransactionCommitFailedException e) {
167                         LOG.error("Failed to write to oper ds");
168                     }
169                 }).start();
170             }
171         };
172         verifications = new Verifications(singleTxdataBroker, odlInterfaceRpcService, EXTN_INTFS, getAwaiter());
173         l2gwBuilders = new L2gwBuilders(singleTxdataBroker);
174         JobCoordinator jobCoordinator = new JobCoordinatorImpl(metricProvider);
175
176         l2gwListener = new L2GatewayListener(dataBroker, mockedEntityOwnershipService,
177                 Mockito.mock(ItmRpcService.class), Mockito.mock(IL2gwService.class), jobCoordinator, l2GatewayCache);
178         l2gwListener.init();
179         setupItm();
180         l2gwBuilders.buildTorNode(TOR2_NODE_ID, PS2, TOR2_TEPIP);
181         l2gwBuilders.buildTorNode(TOR1_NODE_ID, PS1, TOR1_TEPIP);
182     }
183
184     @After public void after() throws Exception {
185         for (ResourceBatchingManager.ShardResource i : ResourceBatchingManager.ShardResource.values()) {
186             ResourceBatchingManager.getInstance().deregisterBatchableResource(i.name());
187         }
188
189         ElanL2GwCacheUtils.removeL2GatewayDeviceFromAllElanCache(TOR1_NODE_ID);
190         ElanL2GwCacheUtils.removeL2GatewayDeviceFromAllElanCache(TOR2_NODE_ID);
191
192         ElanL2GwCacheUtils.removeL2GatewayDeviceFromCache(ExpectedObjects.ELAN1, TOR1_NODE_ID);
193         ElanL2GwCacheUtils.removeL2GatewayDeviceFromCache(ExpectedObjects.ELAN1, TOR2_NODE_ID);
194     }
195
196     @Test public void elanServiceTestModule() {
197         // Intentionally empty; the goal is just to first test the ElanServiceTestModule
198     }
199
200     void createL2gwAndConnection(InstanceIdentifier<Node> nodePath,
201                                  String l2gwName,
202                                  String deviceName,
203                                  List<String> ports,
204                                  String connectionName)
205             throws InterruptedException, TransactionCommitFailedException {
206
207         //Create l2gw
208         singleTxdataBroker.syncWrite(LogicalDatastoreType.CONFIGURATION,
209                 l2gwBuilders.buildL2gwIid(l2gwName), l2gwBuilders.buildL2gw(l2gwName, deviceName, ports));
210         awaitForData(LogicalDatastoreType.CONFIGURATION, l2gwBuilders.buildL2gwIid(l2gwName));
211
212         //Create l2gwconn
213         SingleTransactionDataBroker.syncWrite(dataBroker, LogicalDatastoreType.CONFIGURATION,
214                 l2gwBuilders.buildConnectionIid(connectionName), l2gwBuilders.buildConnection(connectionName,
215                         l2gwName, ExpectedObjects.ELAN1, 100));
216         awaitForData(LogicalDatastoreType.CONFIGURATION, l2gwBuilders.buildConnectionIid(connectionName));
217     }
218
219     @Test
220     public void checkSMAC() throws Exception {
221         // Create Elan instance
222         createElanInstance(ExpectedObjects.ELAN1, ExpectedObjects.ELAN1_SEGMENT_ID);
223         awaitForElanTag(ExpectedObjects.ELAN1);
224
225         // Add Elan interface
226         InterfaceInfo interfaceInfo = ELAN_INTERFACES.get(ELAN1 + ":" + DPN1MAC1).getLeft();
227         addElanInterface(ExpectedObjects.ELAN1, interfaceInfo, DPN1IP1);
228
229         // Read Elan instance
230         InstanceIdentifier<ElanInstance> elanInstanceIid = InstanceIdentifier.builder(ElanInstances.class)
231                 .child(ElanInstance.class, new ElanInstanceKey(ExpectedObjects.ELAN1)).build();
232         ElanInstance actualElanInstances = singleTxdataBroker.syncRead(CONFIGURATION, elanInstanceIid);
233
234         // Read and Compare SMAC flow
235         String flowId = new StringBuilder()
236                 .append(NwConstants.ELAN_SMAC_TABLE)
237                 .append(actualElanInstances.getElanTag())
238                 .append(DPN1_ID)
239                 .append(interfaceInfo.getInterfaceTag())
240                 .append(interfaceInfo.getMacAddress())
241                 .toString();
242         InstanceIdentifier<Flow> flowInstanceIidSrc = getFlowIid(NwConstants.ELAN_SMAC_TABLE,
243                 new FlowId(flowId), DPN1_ID);
244         awaitForData(LogicalDatastoreType.CONFIGURATION, flowInstanceIidSrc);
245
246         Flow flowSrc = singleTxdataBroker.syncRead(CONFIGURATION, flowInstanceIidSrc);
247         flowSrc = getFlowWithoutCookie(flowSrc);
248
249         Flow expected = ExpectedObjects.checkSmac(flowId, interfaceInfo, actualElanInstances);
250         AssertDataObjects.assertEqualBeans(expected, flowSrc);
251     }
252
253     @Test public void checkDmacSameDPN() throws Exception {
254         // Create Elan instance
255         createElanInstance(ExpectedObjects.ELAN1, ExpectedObjects.ELAN1_SEGMENT_ID);
256         awaitForElanTag(ExpectedObjects.ELAN1);
257
258         // Add Elan interface in DPN1
259         InterfaceInfo interfaceInfo = ELAN_INTERFACES.get(ELAN1 + ":" + DPN1MAC1).getLeft();
260         addElanInterface(ExpectedObjects.ELAN1, interfaceInfo, DPN1IP1);
261
262         // Read Elan instance
263         InstanceIdentifier<ElanInstance> elanInstanceIid = InstanceIdentifier.builder(ElanInstances.class)
264                 .child(ElanInstance.class, new ElanInstanceKey(ExpectedObjects.ELAN1)).build();
265         ElanInstance actualElanInstances = singleTxdataBroker.syncRead(CONFIGURATION, elanInstanceIid);
266
267         // Read DMAC Flow in DPN1
268         String flowId = new StringBuilder()
269                 .append(NwConstants.ELAN_DMAC_TABLE)
270                 .append(actualElanInstances.getElanTag())
271                 .append(DPN1_ID)
272                 .append(interfaceInfo.getInterfaceTag())
273                 .append(interfaceInfo.getMacAddress())
274                 .toString();
275         InstanceIdentifier<Flow> flowInstanceIidDst = getFlowIid(NwConstants.ELAN_DMAC_TABLE,
276                 new FlowId(flowId), DPN1_ID);
277         awaitForData(LogicalDatastoreType.CONFIGURATION, flowInstanceIidDst);
278
279         Flow flowDst = singleTxdataBroker.syncRead(CONFIGURATION, flowInstanceIidDst);
280         flowDst = getFlowWithoutCookie(flowDst);
281
282         Flow expected = ExpectedObjects.checkDmacOfSameDpn(flowId, interfaceInfo, actualElanInstances);
283         AssertDataObjects.assertEqualBeans(getSortedActions(expected), getSortedActions(flowDst));
284     }
285
286     @Test public void checkDmacOfOtherDPN() throws Exception {
287         // Create Elan instance
288         createElanInstance(ExpectedObjects.ELAN1, ExpectedObjects.ELAN1_SEGMENT_ID);
289         awaitForElanTag(ExpectedObjects.ELAN1);
290
291         InterfaceInfo interfaceInfo = ELAN_INTERFACES.get(ELAN1 + ":" + DPN1MAC1).getLeft();
292         addElanInterface(ExpectedObjects.ELAN1, interfaceInfo, DPN1IP1);
293
294         // Read Elan instance
295         InstanceIdentifier<ElanInstance> elanInstanceIid = InstanceIdentifier.builder(ElanInstances.class)
296                 .child(ElanInstance.class, new ElanInstanceKey(ExpectedObjects.ELAN1)).build();
297         ElanInstance actualElanInstances = singleTxdataBroker.syncRead(CONFIGURATION, elanInstanceIid);
298
299         interfaceInfo = ELAN_INTERFACES.get(ELAN1 + ":" + DPN2MAC1).getLeft();
300         addElanInterface(ExpectedObjects.ELAN1, interfaceInfo, DPN2IP1);
301
302         // Read and Compare DMAC flow in DPN1 for MAC1 of DPN2
303         String flowId = ElanUtils.getKnownDynamicmacFlowRef((short)51,
304                 DPN1_ID,
305                 DPN2_ID,
306                 interfaceInfo.getMacAddress().toString(),
307                 actualElanInstances.getElanTag());
308
309         InstanceIdentifier<Flow> flowInstanceIidDst = getFlowIid(NwConstants.ELAN_DMAC_TABLE,
310                 new FlowId(flowId), DPN1_ID);
311         awaitForData(LogicalDatastoreType.CONFIGURATION, flowInstanceIidDst);
312
313         Flow flowDst = singleTxdataBroker.syncRead(CONFIGURATION, flowInstanceIidDst);
314         flowDst = getFlowWithoutCookie(flowDst);
315
316         TunnelInterfaceDetails tepDetails = EXTN_INTFS.get(DPN1_ID_STR + ":" + DPN2_ID_STR);
317         Flow expected = ExpectedObjects.checkDmacOfOtherDPN(flowId, interfaceInfo, tepDetails,
318                 actualElanInstances);
319         AssertDataObjects.assertEqualBeans(getSortedActions(expected), getSortedActions(flowDst));
320     }
321
322     @Test public void checkEvpnAdvRT2() throws Exception {
323         createElanInstanceAndInterfaceAndAttachEvpn();
324
325
326         AssertDataObjects.assertEqualBeans(
327                 ExpectedObjects.checkEvpnAdvertiseRoute(ELAN1_SEGMENT_ID, DPN1MAC1, DPN1_TEPIP, DPN1IP1, RD),
328                 readBgpNetworkFromDS(DPN1IP1));
329     }
330
331     @Test public void checkEvpnAdvRT2NewInterface() throws Exception {
332         createElanInstanceAndInterfaceAndAttachEvpn();
333
334         // Add Elan interface
335         addElanInterface(ExpectedObjects.ELAN1, ELAN_INTERFACES.get(ELAN1 + ":" + DPN1MAC2).getLeft(), DPN1IP2);
336
337         AssertDataObjects.assertEqualBeans(
338                 ExpectedObjects.checkEvpnAdvertiseRoute(ELAN1_SEGMENT_ID, DPN1MAC2, DPN1_TEPIP, DPN1IP2, RD),
339                 readBgpNetworkFromDS(DPN1IP2));
340     }
341
342     @Test public void checkEvpnWithdrawRT2DelIntf() throws Exception {
343         createElanInstanceAndInterfaceAndAttachEvpn();
344
345         InstanceIdentifier<Networks> iid = evpnTestHelper.buildBgpNetworkIid(DPN1IP1);
346         awaitForData(LogicalDatastoreType.CONFIGURATION, iid);
347
348         evpnTestHelper.deleteRdtoNetworks();
349
350         deleteElanInterface(ELAN_INTERFACES.get(ELAN1 + ":" + DPN1MAC1).getLeft());
351         awaitForDataDelete(LogicalDatastoreType.CONFIGURATION, iid);
352     }
353
354     @Test public void checkEvpnWithdrawRouteDetachEvpn() throws Exception {
355         createElanInstanceAndInterfaceAndAttachEvpn();
356         addElanInterface(ExpectedObjects.ELAN1, ELAN_INTERFACES.get(ELAN1 + ":" + DPN1MAC2).getLeft(), DPN1IP2);
357
358         awaitForData(LogicalDatastoreType.CONFIGURATION, evpnTestHelper.buildBgpNetworkIid(DPN1IP1));
359         awaitForData(LogicalDatastoreType.CONFIGURATION, evpnTestHelper.buildBgpNetworkIid(DPN1IP2));
360
361         evpnTestHelper.detachEvpnToNetwork(ExpectedObjects.ELAN1);
362
363         awaitForDataDelete(LogicalDatastoreType.CONFIGURATION, evpnTestHelper.buildBgpNetworkIid(DPN1IP1));
364         awaitForDataDelete(LogicalDatastoreType.CONFIGURATION, evpnTestHelper.buildBgpNetworkIid(DPN1IP2));
365     }
366
367     @Test public void checkEvpnInstalDmacFlow() throws Exception {
368         createElanInstanceAndInterfaceAndAttachEvpn();
369         addElanInterface(ExpectedObjects.ELAN1, ELAN_INTERFACES.get(ELAN1 + ":" + DPN1MAC2).getLeft(), DPN1IP2);
370
371         // Verify advertise RT2 route success for both MAC's
372         awaitForData(LogicalDatastoreType.CONFIGURATION, evpnTestHelper.buildBgpNetworkIid(DPN1IP1));
373         awaitForData(LogicalDatastoreType.CONFIGURATION, evpnTestHelper.buildBgpNetworkIid(DPN1IP2));
374
375         // RT2 received from Peer
376         evpnTestHelper.handleEvpnRt2Recvd(EVPNRECVMAC1, EVPNRECVIP1);
377         evpnTestHelper.handleEvpnRt2Recvd(EVPNRECVMAC2, EVPNRECVIP2);
378
379         // verify successful installation of DMAC flow for recvd rt2
380         awaitForData(LogicalDatastoreType.CONFIGURATION, evpnTestHelper.buildMacVrfEntryIid(EVPNRECVMAC1));
381         awaitForData(LogicalDatastoreType.CONFIGURATION, evpnTestHelper.buildMacVrfEntryIid(EVPNRECVMAC2));
382     }
383
384     @Test public void checkEvpnUnInstalDmacFlow() throws Exception {
385         createElanInstanceAndInterfaceAndAttachEvpn();
386         addElanInterface(ExpectedObjects.ELAN1, ELAN_INTERFACES.get(ELAN1 + ":" + DPN1MAC2).getLeft(), DPN1IP2);
387
388         // Verify advertise RT2 route success for both MAC's
389         awaitForData(LogicalDatastoreType.CONFIGURATION, evpnTestHelper.buildBgpNetworkIid(DPN1IP1));
390         awaitForData(LogicalDatastoreType.CONFIGURATION, evpnTestHelper.buildBgpNetworkIid(DPN1IP2));
391
392         // RT2 received from Peer
393         evpnTestHelper.handleEvpnRt2Recvd(EVPNRECVMAC1, EVPNRECVIP1);
394         evpnTestHelper.handleEvpnRt2Recvd(EVPNRECVMAC2, EVPNRECVIP2);
395
396         // verify successful installation of DMAC flow for recvd rt2
397         awaitForData(LogicalDatastoreType.CONFIGURATION, evpnTestHelper.buildMacVrfEntryIid(EVPNRECVMAC1));
398         awaitForData(LogicalDatastoreType.CONFIGURATION, evpnTestHelper.buildMacVrfEntryIid(EVPNRECVMAC2));
399
400         // withdraw RT2 received from Peer
401         evpnTestHelper.deleteMacVrfEntryToDS(RD, EVPNRECVMAC1);
402         evpnTestHelper.deleteMacVrfEntryToDS(RD, EVPNRECVMAC2);
403
404         // verify successful un-installation of DMAC flow for recvd rt2
405         awaitForDataDelete(LogicalDatastoreType.CONFIGURATION, evpnTestHelper.buildMacVrfEntryIid(EVPNRECVMAC1));
406         awaitForDataDelete(LogicalDatastoreType.CONFIGURATION, evpnTestHelper.buildMacVrfEntryIid(EVPNRECVMAC2));
407     }
408
409     public void createElanInstanceAndInterfaceAndAttachEvpn() throws ReadFailedException,
410             TransactionCommitFailedException {
411         // Create Elan instance
412         createElanInstance(ExpectedObjects.ELAN1, ExpectedObjects.ELAN1_SEGMENT_ID);
413         awaitForElanTag(ExpectedObjects.ELAN1);
414
415         // Read Elan Instance
416         InstanceIdentifier<ElanInstance> elanInstanceIid = InstanceIdentifier.builder(ElanInstances.class)
417                 .child(ElanInstance.class, new ElanInstanceKey(ExpectedObjects.ELAN1)).build();
418         ElanInstance elanInstance = singleTxdataBroker.syncRead(CONFIGURATION, elanInstanceIid);
419
420         // Add Elan interface
421         addElanInterface(ExpectedObjects.ELAN1, ELAN_INTERFACES.get(ELAN1 + ":" + DPN1MAC1).getLeft(), DPN1IP1);
422
423         // Attach EVPN to networks
424         evpnTestHelper.attachEvpnToNetwork(elanInstance);
425     }
426
427     public Networks readBgpNetworkFromDS(String prefix) throws ReadFailedException {
428         InstanceIdentifier<Networks> iid = InstanceIdentifier.builder(Bgp.class)
429                 .child(Networks.class, new NetworksKey(prefix, RD))
430                 .build();
431         awaitForData(LogicalDatastoreType.CONFIGURATION, iid);
432
433         return singleTxdataBroker.syncRead(CONFIGURATION, iid);
434     }
435
436     private void awaitForElanTag(String elanName) {
437         InstanceIdentifier<ElanInstance> elanInstanceIid = InstanceIdentifier.builder(ElanInstances.class)
438                 .child(ElanInstance.class, new ElanInstanceKey(elanName)).build();
439         getAwaiter().until(() -> {
440             Optional<ElanInstance> elanInstance = MDSALUtil.read(dataBroker, CONFIGURATION, elanInstanceIid);
441             return elanInstance.isPresent() && elanInstance.get().getElanTag() != null;
442         });
443     }
444
445     public void verifyL2gw1Connection() throws Exception {
446
447         //Create ELAN
448         createElanInstance(ExpectedObjects.ELAN1, ExpectedObjects.ELAN1_SEGMENT_ID);
449         awaitForElanTag(ExpectedObjects.ELAN1);
450
451         //Add Elan MAC1, MAC2 in DPN1
452         InterfaceInfo interfaceInfo = ELAN_INTERFACES.get(ELAN1 + ":" + DPN1MAC1).getLeft();
453         addElanInterface(ExpectedObjects.ELAN1, interfaceInfo, DPN1IP1);
454         interfaceInfo = ELAN_INTERFACES.get(ELAN1 + ":" + DPN1MAC2).getLeft();
455         addElanInterface(ExpectedObjects.ELAN1, interfaceInfo, DPN1IP2);
456
457         //Add Elan MAC1, MAC2 in DPN2
458         interfaceInfo = ELAN_INTERFACES.get(ELAN1 + ":" + DPN2MAC1).getLeft();
459         addElanInterface(ExpectedObjects.ELAN1, interfaceInfo, DPN2IP1);
460         verifications.verifyLocalBcGroup(DPN2_ID, 1);
461
462         interfaceInfo = ELAN_INTERFACES.get(ELAN1 + ":" + DPN2MAC2).getLeft();
463         addElanInterface(ExpectedObjects.ELAN1, interfaceInfo, DPN2IP2);
464         verifications.verifyLocalBcGroup(DPN2_ID, 2);
465
466         createL2gwAndConnection(TOR1_NODE_IID, L2GW1, PS1, DataProvider.getPortNameListD1(), L2GW_CONN1);
467
468         verifications.verifyThatMcastMacTepsCreated(TOR1_NODE_IID, asList(DPN1_TEPIP, DPN2_TEPIP));
469         verifications.verifyThatUcastCreated(TOR1_NODE_IID, asList(DPN1MAC1, DPN1MAC2, DPN2MAC1, DPN2MAC2));
470         verifications.verifyThatDpnGroupUpdated(DPN1_ID, asList(DPN2_ID), asList(TOR1_TEPIP));
471         verifications.verifyThatDpnGroupUpdated(DPN2_ID, asList(DPN1_ID), asList(TOR1_TEPIP));
472     }
473
474     @Test
475     public void verifyL2gwPreProvisioning() throws Exception {
476
477         createElanInstance(ExpectedObjects.ELAN1, ExpectedObjects.ELAN1_SEGMENT_ID);
478         awaitForElanTag(ExpectedObjects.ELAN1);
479
480         addElanInterface(ExpectedObjects.ELAN1, ELAN_INTERFACES.get(ELAN1 + ":" + DPN1MAC1).getLeft(), DPN1IP1);
481
482         singleTxdataBroker.syncDelete(OPERATIONAL, TOR1_NODE_IID);
483         singleTxdataBroker.syncDelete(OPERATIONAL,
484                 PhysicalSwitchHelper.getPhysicalSwitchInstanceIdentifier(TOR1_NODE_IID, PS1));
485
486         createL2gwAndConnection(TOR1_NODE_IID, L2GW1, PS1, DataProvider.getPortNameListD1(), L2GW_CONN1);
487
488         l2gwBuilders.buildTorNode(TOR1_NODE_ID, PS1, TOR1_TEPIP);
489
490         verifications.verifyThatMcastMacTepsCreated(TOR1_NODE_IID, asList(DPN1_TEPIP));
491         verifications.verifyThatUcastCreated(TOR1_NODE_IID, asList(DPN1MAC1));
492     }
493
494     public void verifyL2gwMac1InDpns() throws Exception {
495         verifyL2gw1Connection();
496         l2gwBuilders.createLocalUcastMac(TOR1_NODE_IID, TOR1_MAC1, TOR1_IP1, TOR1_TEPIP);
497         verifications.verifyThatDmacFlowOfTORCreated(asList(DPN1_ID, DPN2_ID), TOR1_NODE_IID, asList(TOR1_MAC1));
498     }
499
500     public void verifyL2gw2Connection() throws Exception {
501         verifyL2gwMac1InDpns();
502         // TOR Node 2 creation
503         createL2gwAndConnection(TOR2_NODE_IID, L2GW2, PS2, DataProvider.getPortNameListTor2(), L2GW_CONN2);
504         //check for remote mcast mac in tor2 against TEPs of dpn1, dpn2 and dpn3, tor1)
505         verifications.verifyThatMcastMacTepsCreated(TOR2_NODE_IID, asList(DPN1_TEPIP, DPN2_TEPIP, TOR1_TEPIP));
506         verifications.verifyThatMcastMacTepsCreated(TOR1_NODE_IID, asList(DPN1_TEPIP, DPN2_TEPIP, TOR2_TEPIP));
507         verifications.verifyThatUcastCreated(TOR2_NODE_IID, asList(DPN1MAC1, DPN2MAC1, DPN2MAC1, DPN2MAC2, TOR1_MAC1));
508     }
509
510     @Test
511     public void verifyL2gwMac2InTors() throws Exception {
512         verifyL2gw2Connection();
513         l2gwBuilders.createLocalUcastMac(TOR1_NODE_IID, TOR1_MAC2, TOR1_IP2, TOR1_TEPIP);
514         verifications.verifyThatUcastCreated(TOR2_NODE_IID, asList(TOR1_MAC2));
515     }
516
517     @Test
518     public void verifyL2gwMacDeleteInTors() throws Exception {
519         verifyL2gwMac2InTors();
520         LocalUcastMacs localUcastMacs1 = l2gwBuilders.createLocalUcastMac(
521                 TOR1_NODE_IID, TOR1_MAC1, TOR1_IP1, TOR1_TEPIP);
522         singleTxdataBroker.syncDelete(LogicalDatastoreType.OPERATIONAL,
523                 l2gwBuilders.buildMacIid(TOR1_NODE_IID, localUcastMacs1));
524         verifications.verifyThatDmacFlowOfTORDeleted(asList(DPN1_ID, DPN2_ID), TOR1_NODE_IID, asList(TOR1_MAC1));
525         verifications.verifyThatUcastDeleted(TOR2_NODE_IID, asList(TOR1_MAC1));
526     }
527
528     @Test
529     public void verifyAddDpnAfterL2gwConnection() throws Exception {
530         verifyL2gwMac2InTors();
531         //Add Elan MAC1, MAC2 in DPN3
532         InterfaceInfo interfaceInfo = ELAN_INTERFACES.get(ELAN1 + ":" + DPN3MAC1).getLeft();
533         addElanInterface(ExpectedObjects.ELAN1, interfaceInfo, DPN3IP1);
534
535         //bc group of this dpn created
536         verifications.verifyThatDpnGroupUpdated(DPN3_ID, asList(DPN1_ID, DPN2_ID), asList(TOR1_NODE_ID, TOR2_NODE_ID));
537         //other tors macs be installed in this dpn
538         verifications.verifyThatDmacFlowOfTORCreated(asList(DPN3_ID), TOR1_NODE_IID, asList(TOR1_MAC1));
539         verifications.verifyThatDmacOfOtherDpnCreated(DPN3_ID, DPN1_ID, asList(DPN1MAC1, DPN1MAC2));
540         verifications.verifyThatDmacOfOtherDpnCreated(DPN3_ID, DPN2_ID, asList(DPN2MAC1, DPN2MAC2));
541
542         //bc group of the other dpns be updated
543         verifications.verifyThatDpnGroupUpdated(DPN1_ID, asList(DPN2_ID, DPN3_ID), asList(TOR1_NODE_ID, TOR2_NODE_ID));
544         verifications.verifyThatDpnGroupUpdated(DPN2_ID, asList(DPN1_ID, DPN3_ID), asList(TOR1_NODE_ID, TOR2_NODE_ID));
545
546         //mcast of tor should be updated
547         verifications.verifyThatMcastMacTepsCreated(TOR2_NODE_IID,
548                 asList(DPN1_TEPIP, DPN2_TEPIP, DPN3_TEPIP, TOR1_TEPIP));
549         verifications.verifyThatMcastMacTepsCreated(TOR1_NODE_IID,
550                 asList(DPN1_TEPIP, DPN2_TEPIP, DPN3_TEPIP, TOR2_TEPIP));
551
552         //this dpn mac should get installed in other dpns and tors
553         verifications.verifyThatUcastCreated(TOR1_NODE_IID, asList(DPN3MAC1));
554         verifications.verifyThatUcastCreated(TOR2_NODE_IID, asList(DPN3MAC1));
555         verifications.verifyThatDmacOfOtherDpnCreated(DPN1_ID, DPN3_ID, asList(DPN3MAC1));
556         verifications.verifyThatDmacOfOtherDpnCreated(DPN2_ID, DPN3_ID, asList(DPN3MAC1));
557     }
558
559     @Test
560     @Ignore("Ignoring for Neon MRI")
561     public void verifyDeleteDpnAfterL2gwConnection() throws Exception {
562         verifyAddDpnAfterL2gwConnection();
563         InterfaceInfo interfaceInfo = ELAN_INTERFACES.get(ELAN1 + ":" + DPN3MAC1).getLeft();
564         deleteElanInterface(interfaceInfo);
565
566         //clean up of group of this dpn3
567         verifications.verifyThatDpnGroupDeleted(DPN3_ID);
568         //clean up dmacs of this dpn3
569         verifications.verifyThatDmacFlowOfTORDeleted(asList(DPN3_ID), TOR1_NODE_IID, asList(TOR1_MAC1));
570         verifications.verifyThatDmacOfOtherDPNDeleted(DPN3_ID, DPN1_ID, asList(DPN1MAC1, DPN1MAC2));
571         verifications.verifyThatDmacOfOtherDPNDeleted(DPN3_ID, DPN2_ID, asList(DPN2MAC1, DPN2MAC2));
572
573         //clean up of dmacs in the other dpns
574         verifications.verifyThatDmacOfOtherDPNDeleted(DPN1_ID, DPN3_ID, asList(DPN3MAC1));
575         verifications.verifyThatDmacOfOtherDPNDeleted(DPN2_ID, DPN3_ID, asList(DPN3MAC1));
576
577         //cleanup of bc group of other dpns
578         verifications.verifyThatDpnGroupUpdated(DPN1_ID, asList(DPN2_ID), asList(TOR1_NODE_ID, TOR2_NODE_ID));
579         verifications.verifyThatDpnGroupUpdated(DPN2_ID, asList(DPN1_ID), asList(TOR1_NODE_ID, TOR2_NODE_ID));
580
581         //dpn tep should be removed from tors
582         verifications.verifyThatMcastMacTepsDeleted(TOR2_NODE_IID, asList(DPN3_TEPIP));
583         verifications.verifyThatMcastMacTepsDeleted(TOR2_NODE_IID, asList(DPN3_TEPIP));
584
585         //dpn mac should be removed from tors
586         verifications.verifyThatUcastDeleted(TOR1_NODE_IID, asList(DPN3MAC1));
587         verifications.verifyThatUcastDeleted(TOR2_NODE_IID, asList(DPN3MAC1));
588     }
589
590     @Test
591     public void verifyDeleteL2gw1Connection() throws Exception {
592         verifyL2gw2Connection();
593         //delete tor1 l2gw connection
594         l2gwBuilders.deletel2GWConnection(L2GW_CONN1);
595
596         //deleted tors mcast & cast be cleared
597         verifications.verifyThatMcastMacTepsDeleted(TOR1_NODE_IID, asList(DPN1_TEPIP, DPN2_TEPIP, TOR2_TEPIP));
598         verifications.verifyThatUcastDeleted(TOR1_NODE_IID, asList(DPN1MAC1, DPN1MAC2, DPN2MAC1, DPN2MAC2));
599
600         //mcast of other tor be updated
601         verifications.verifyThatMcastMacTepsDeleted(TOR2_NODE_IID, asList(TOR1_TEPIP));
602
603         //ucast of deleted to be deleted in other tor
604         verifications.verifyThatUcastDeleted(TOR2_NODE_IID, asList(TOR1_MAC1));
605
606         //group of dpns be udpated
607         verifications.verifyThatDpnGroupUpdated(DPN1_ID, asList(DPN2_ID), asList(TOR2_NODE_ID));
608         verifications.verifyThatDpnGroupUpdated(DPN2_ID, asList(DPN1_ID), asList(TOR2_NODE_ID));
609
610         //ucast of deleted tor be deleted in other dpns
611         verifications.verifyThatDmacFlowOfTORDeleted(asList(DPN1_ID, DPN2_ID), TOR1_NODE_IID, asList(TOR1_MAC1));
612     }
613 }