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