Fix for ELAN JUNIT TCs failure
[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
86 // .attributes.LocalUcastMacs;
87 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LogicalSwitches;
88 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
89 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
90 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
91 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
92 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
93 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
94 import org.slf4j.Logger;
95 import org.slf4j.LoggerFactory;
96
97
98 /**
99  * End-to-end test of IElanService.
100  *
101  * @author Michael Vorburger
102  * @author Riyazahmed Talikoti
103  */
104 public class ElanServiceTest extends  ElanServiceTestBase {
105
106     private static final Logger LOG = LoggerFactory.getLogger(ElanServiceTest.class);
107
108     // TODO as-is, this test is flaky; as uncommenting will show
109     // Uncomment this to keep running this test indefinitely
110     // This is very useful to detect concurrency issues (such as https://bugs.opendaylight.org/show_bug.cgi?id=7538)
111     // public static @ClassRule RunUntilFailureClassRule classRepeater = new RunUntilFailureClassRule();
112     // public @Rule RunUntilFailureRule repeater = new RunUntilFailureRule(classRepeater);
113
114     public @Rule LogRule logRule = new LogRule();
115     public @Rule MethodRule guice = new GuiceRule(ElanServiceTestModule.class, JobCoordinatorTestModule.class,
116             CacheModule.class);
117     // TODO re-enable after we can await completion of listeners and DJC:
118     // Otherwise this too frequently causes spurious test failures, e.g. due to error
119     // logs Caused by: java.lang.RuntimeException: java.util.concurrent.ExecutionException: Operation was interrupted
120     // public @Rule LogCaptureRule logCaptureRule = new LogCaptureRule();
121     private @Inject IElanService elanService;
122     private @Inject IdManagerService idManager;
123     private @Inject EvpnElanInstanceListener evpnElanInstanceListener;
124     private @Inject ElanMacEntryListener elanMacEntryListener;
125     private @Inject MacVrfEntryListener macVrfEntryListener;
126     private @Inject EvpnUtils evpnUtils;
127     private @Inject IBgpManager bgpManager;
128     private @Inject IVpnManager vpnManager;
129     private @Inject EvpnTestHelper evpnTestHelper;
130     private @Inject OdlInterfaceRpcService odlInterfaceRpcService;
131     private @Inject ElanL2GatewayUtils elanL2GatewayUtils;
132     private @Inject ElanInterfaceManager elanInterfaceManager;
133     private @Inject HwvtepPhysicalSwitchListener hwvtepPhysicalSwitchListener;
134     private @Inject L2GatewayConnectionListener l2GatewayConnectionListener;
135     private @Inject LocalUcastMacListener localUcastMacListener;
136     private @Inject ElanDpnInterfaceClusteredListener elanDpnInterfaceClusteredListener;
137     private @Inject EntityOwnershipService mockedEntityOwnershipService;
138     private @Inject L2GatewayCache l2GatewayCache;
139     private @Inject ElanUtils elanUtils;
140     private @Inject ElanInstanceDpnsCache elanInstanceDpnsCache;
141     private @Inject ElanExtnTepConfigListener elanExtnTepConfigListener;
142     private @Inject ElanExtnTepListener elanExtnTepListener;
143     private @Inject CacheProvider cacheProvider;
144     private @Inject L2GatewayInstanceRecoveryHandler l2GatewayInstanceRecoveryHandler;
145     private @Inject ServiceRecoveryRegistry serviceRecoveryRegistry;
146
147     private L2GatewayListener l2gwListener;
148     private final MetricProvider metricProvider = new TestMetricProviderImpl();
149
150     private Verifications verifications;
151     private L2gwBuilders l2gwBuilders;
152
153     private SingleTransactionDataBroker singleTxdataBroker;
154
155     private InstanceIdDataObjectCache<LogicalSwitches> logicalSwitchCache;
156
157     @Before public void before() throws Exception {
158         singleTxdataBroker = new SingleTransactionDataBroker(dataBroker);
159         logicalSwitchCache = new InstanceIdDataObjectCache<LogicalSwitches>(
160                 LogicalSwitches.class, dataBroker, CONFIGURATION,
161                 InstanceIdentifier.builder(NetworkTopology.class)
162                         .child(Topology.class, new TopologyKey(new TopologyId(new Uri("hwvtep:1"))))
163                         .child(org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang
164                                 .network.topology.rev131021.network.topology.topology.Node.class)
165                         .augmentation(HwvtepGlobalAugmentation.class)
166                         .child(LogicalSwitches.class).build(), cacheProvider) {
167             @Override
168             protected void added(InstanceIdentifier<LogicalSwitches> path, LogicalSwitches dataObject) {
169                 new Thread(() -> {
170                     try {
171                         singleTxdataBroker.syncWrite(OPERATIONAL, path, dataObject);
172                     } catch (TransactionCommitFailedException e) {
173                         LOG.error("Failed to write to oper ds");
174                     }
175                 }).start();
176             }
177         };
178         verifications = new Verifications(singleTxdataBroker, odlInterfaceRpcService, EXTN_INTFS, getAwaiter());
179         l2gwBuilders = new L2gwBuilders(singleTxdataBroker);
180         JobCoordinator jobCoordinator = new JobCoordinatorImpl(metricProvider);
181
182         l2gwListener = new L2GatewayListener(dataBroker, mockedEntityOwnershipService,
183                 Mockito.mock(ItmRpcService.class), Mockito.mock(IL2gwService.class), jobCoordinator, l2GatewayCache,
184                 l2GatewayInstanceRecoveryHandler,serviceRecoveryRegistry);
185         l2gwListener.init();
186         setupItm();
187         l2gwBuilders.buildTorNode(TOR2_NODE_ID, PS2, TOR2_TEPIP);
188         l2gwBuilders.buildTorNode(TOR1_NODE_ID, PS1, TOR1_TEPIP);
189     }
190
191     @After public void after() throws Exception {
192         for (ResourceBatchingManager.ShardResource i : ResourceBatchingManager.ShardResource.values()) {
193             ResourceBatchingManager.getInstance().deregisterBatchableResource(i.name());
194         }
195
196         ElanL2GwCacheUtils.removeL2GatewayDeviceFromAllElanCache(TOR1_NODE_ID);
197         ElanL2GwCacheUtils.removeL2GatewayDeviceFromAllElanCache(TOR2_NODE_ID);
198
199         ElanL2GwCacheUtils.removeL2GatewayDeviceFromCache(ExpectedObjects.ELAN1, TOR1_NODE_ID);
200         ElanL2GwCacheUtils.removeL2GatewayDeviceFromCache(ExpectedObjects.ELAN1, TOR2_NODE_ID);
201     }
202
203     @Test public void elanServiceTestModule() {
204         // Intentionally empty; the goal is just to first test the ElanServiceTestModule
205     }
206
207     void createL2gwAndConnection(InstanceIdentifier<Node> nodePath,
208                                  String l2gwName,
209                                  String deviceName,
210                                  List<String> ports,
211                                  String connectionName)
212             throws InterruptedException, TransactionCommitFailedException {
213
214         //Create l2gw
215         singleTxdataBroker.syncWrite(LogicalDatastoreType.CONFIGURATION,
216                 l2gwBuilders.buildL2gwIid(l2gwName), l2gwBuilders.buildL2gw(l2gwName, deviceName, ports));
217         awaitForData(LogicalDatastoreType.CONFIGURATION, l2gwBuilders.buildL2gwIid(l2gwName));
218
219         //Create l2gwconn
220         SingleTransactionDataBroker.syncWrite(dataBroker, LogicalDatastoreType.CONFIGURATION,
221                 l2gwBuilders.buildConnectionIid(connectionName), l2gwBuilders.buildConnection(connectionName,
222                         l2gwName, ExpectedObjects.ELAN1, 100));
223         awaitForData(LogicalDatastoreType.CONFIGURATION, l2gwBuilders.buildConnectionIid(connectionName));
224     }
225
226     @Test
227     public void checkSMAC() throws Exception {
228         // Create Elan instance
229         createElanInstance(ExpectedObjects.ELAN1, ExpectedObjects.ELAN1_SEGMENT_ID);
230         awaitForElanTag(ExpectedObjects.ELAN1);
231
232         // Add Elan interface
233         InterfaceInfo interfaceInfo = ELAN_INTERFACES.get(ELAN1 + ":" + DPN1MAC1).getLeft();
234         addElanInterface(ExpectedObjects.ELAN1, interfaceInfo, DPN1IP1);
235
236         // Read Elan instance
237         InstanceIdentifier<ElanInstance> elanInstanceIid = InstanceIdentifier.builder(ElanInstances.class)
238                 .child(ElanInstance.class, new ElanInstanceKey(ExpectedObjects.ELAN1)).build();
239         ElanInstance actualElanInstances = singleTxdataBroker.syncRead(CONFIGURATION, elanInstanceIid);
240
241         // Read and Compare SMAC flow
242         String flowId = new StringBuilder()
243                 .append(actualElanInstances.getElanTag())
244                 .append(interfaceInfo.getMacAddress())
245                 .toString();
246         InstanceIdentifier<Flow> flowInstanceIidSrc = getFlowIid(NwConstants.ELAN_SMAC_TABLE,
247                 new FlowId(flowId), DPN1_ID);
248         awaitForData(LogicalDatastoreType.CONFIGURATION, flowInstanceIidSrc);
249
250         Flow flowSrc = singleTxdataBroker.syncRead(CONFIGURATION, flowInstanceIidSrc);
251         flowSrc = getFlowWithoutCookie(flowSrc);
252
253         Flow expected = ExpectedObjects.checkSmac(flowId, interfaceInfo, actualElanInstances);
254         AssertDataObjects.assertEqualBeans(expected, flowSrc);
255     }
256
257     @Test public void checkDmacSameDPN() throws Exception {
258         // Create Elan instance
259         createElanInstance(ExpectedObjects.ELAN1, ExpectedObjects.ELAN1_SEGMENT_ID);
260         awaitForElanTag(ExpectedObjects.ELAN1);
261
262         // Add Elan interface in DPN1
263         InterfaceInfo interfaceInfo = ELAN_INTERFACES.get(ELAN1 + ":" + DPN1MAC1).getLeft();
264         addElanInterface(ExpectedObjects.ELAN1, interfaceInfo, DPN1IP1);
265
266         // Read Elan instance
267         InstanceIdentifier<ElanInstance> elanInstanceIid = InstanceIdentifier.builder(ElanInstances.class)
268                 .child(ElanInstance.class, new ElanInstanceKey(ExpectedObjects.ELAN1)).build();
269         ElanInstance actualElanInstances = singleTxdataBroker.syncRead(CONFIGURATION, elanInstanceIid);
270
271         // Read DMAC Flow in DPN1
272         String flowId = new StringBuilder()
273                 .append(actualElanInstances.getElanTag())
274                 .append(interfaceInfo.getMacAddress())
275                 .toString();
276         InstanceIdentifier<Flow> flowInstanceIidDst = getFlowIid(NwConstants.ELAN_DMAC_TABLE,
277                 new FlowId(flowId), DPN1_ID);
278         awaitForData(LogicalDatastoreType.CONFIGURATION, flowInstanceIidDst);
279
280         Flow flowDst = singleTxdataBroker.syncRead(CONFIGURATION, flowInstanceIidDst);
281         flowDst = getFlowWithoutCookie(flowDst);
282
283         Flow expected = ExpectedObjects.checkDmacOfSameDpn(flowId, interfaceInfo, actualElanInstances);
284         AssertDataObjects.assertEqualBeans(getSortedActions(expected), getSortedActions(flowDst));
285     }
286
287     @Test public void checkDmacOfOtherDPN() throws Exception {
288         // Create Elan instance
289         createElanInstance(ExpectedObjects.ELAN1, ExpectedObjects.ELAN1_SEGMENT_ID);
290         awaitForElanTag(ExpectedObjects.ELAN1);
291
292         InterfaceInfo interfaceInfo = ELAN_INTERFACES.get(ELAN1 + ":" + DPN1MAC1).getLeft();
293         addElanInterface(ExpectedObjects.ELAN1, interfaceInfo, DPN1IP1);
294
295         // Read Elan instance
296         InstanceIdentifier<ElanInstance> elanInstanceIid = InstanceIdentifier.builder(ElanInstances.class)
297                 .child(ElanInstance.class, new ElanInstanceKey(ExpectedObjects.ELAN1)).build();
298         ElanInstance actualElanInstances = singleTxdataBroker.syncRead(CONFIGURATION, elanInstanceIid);
299
300         interfaceInfo = ELAN_INTERFACES.get(ELAN1 + ":" + DPN2MAC1).getLeft();
301         addElanInterface(ExpectedObjects.ELAN1, interfaceInfo, DPN2IP1);
302
303         // Read and Compare DMAC flow in DPN1 for MAC1 of DPN2
304         String flowId = ElanUtils.getKnownDynamicmacFlowRef(actualElanInstances.getElanTag(),
305                 interfaceInfo.getMacAddress());
306
307         InstanceIdentifier<Flow> flowInstanceIidDst = getFlowIid(NwConstants.ELAN_DMAC_TABLE,
308                 new FlowId(flowId), DPN1_ID);
309         awaitForData(LogicalDatastoreType.CONFIGURATION, flowInstanceIidDst);
310
311         Flow flowDst = singleTxdataBroker.syncRead(CONFIGURATION, flowInstanceIidDst);
312         flowDst = getFlowWithoutCookie(flowDst);
313
314         TunnelInterfaceDetails tepDetails = EXTN_INTFS.get(DPN1_ID_STR + ":" + DPN2_ID_STR);
315         Flow expected = ExpectedObjects.checkDmacOfOtherDPN(flowId, interfaceInfo, tepDetails,
316                 actualElanInstances);
317         AssertDataObjects.assertEqualBeans(getSortedActions(expected), getSortedActions(flowDst));
318     }
319
320     @Test public void checkEvpnAdvRT2() throws Exception {
321         createElanInstanceAndInterfaceAndAttachEvpn();
322
323
324         AssertDataObjects.assertEqualBeans(
325                 ExpectedObjects.checkEvpnAdvertiseRoute(ELAN1_SEGMENT_ID, DPN1MAC1, DPN1_TEPIP, DPN1IP1, RD),
326                 readBgpNetworkFromDS(DPN1IP1));
327     }
328
329     @Test public void checkEvpnAdvRT2NewInterface() throws Exception {
330         createElanInstanceAndInterfaceAndAttachEvpn();
331
332         // Add Elan interface
333         addElanInterface(ExpectedObjects.ELAN1, ELAN_INTERFACES.get(ELAN1 + ":" + DPN1MAC2).getLeft(), DPN1IP2);
334
335         AssertDataObjects.assertEqualBeans(
336                 ExpectedObjects.checkEvpnAdvertiseRoute(ELAN1_SEGMENT_ID, DPN1MAC2, DPN1_TEPIP, DPN1IP2, RD),
337                 readBgpNetworkFromDS(DPN1IP2));
338     }
339
340     @Test public void checkEvpnWithdrawRT2DelIntf() throws Exception {
341         createElanInstanceAndInterfaceAndAttachEvpn();
342
343         InstanceIdentifier<Networks> iid = evpnTestHelper.buildBgpNetworkIid(DPN1IP1);
344         awaitForData(LogicalDatastoreType.CONFIGURATION, iid);
345
346         evpnTestHelper.deleteRdtoNetworks();
347
348         deleteElanInterface(ELAN_INTERFACES.get(ELAN1 + ":" + DPN1MAC1).getLeft());
349         awaitForDataDelete(LogicalDatastoreType.CONFIGURATION, iid);
350     }
351
352 /*    @Test public void checkEvpnWithdrawRouteDetachEvpn() throws Exception {
353         createElanInstanceAndInterfaceAndAttachEvpn();
354         addElanInterface(ExpectedObjects.ELAN1, ELAN_INTERFACES.get(ELAN1 + ":" + DPN1MAC2).getLeft(), DPN1IP2);
355
356         awaitForData(LogicalDatastoreType.CONFIGURATION, evpnTestHelper.buildBgpNetworkIid(DPN1IP1));
357         awaitForData(LogicalDatastoreType.CONFIGURATION, evpnTestHelper.buildBgpNetworkIid(DPN1IP2));
358
359         evpnTestHelper.detachEvpnToNetwork(ExpectedObjects.ELAN1);
360
361         awaitForDataDelete(LogicalDatastoreType.CONFIGURATION, evpnTestHelper.buildBgpNetworkIid(DPN1IP1));
362         awaitForDataDelete(LogicalDatastoreType.CONFIGURATION, evpnTestHelper.buildBgpNetworkIid(DPN1IP2));
363     }*/
364
365     @Test public void checkEvpnInstalDmacFlow() throws Exception {
366         createElanInstanceAndInterfaceAndAttachEvpn();
367         addElanInterface(ExpectedObjects.ELAN1, ELAN_INTERFACES.get(ELAN1 + ":" + DPN1MAC2).getLeft(), DPN1IP2);
368
369         // Verify advertise RT2 route success for both MAC's
370         awaitForData(LogicalDatastoreType.CONFIGURATION, evpnTestHelper.buildBgpNetworkIid(DPN1IP1));
371         awaitForData(LogicalDatastoreType.CONFIGURATION, evpnTestHelper.buildBgpNetworkIid(DPN1IP2));
372
373         // RT2 received from Peer
374         evpnTestHelper.handleEvpnRt2Recvd(EVPNRECVMAC1, EVPNRECVIP1);
375         evpnTestHelper.handleEvpnRt2Recvd(EVPNRECVMAC2, EVPNRECVIP2);
376
377         // verify successful installation of DMAC flow for recvd rt2
378         awaitForData(LogicalDatastoreType.CONFIGURATION, evpnTestHelper.buildMacVrfEntryIid(EVPNRECVMAC1));
379         awaitForData(LogicalDatastoreType.CONFIGURATION, evpnTestHelper.buildMacVrfEntryIid(EVPNRECVMAC2));
380     }
381
382     @Test
383     @Ignore
384     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.create(Bgp.class)
429                 .child(NetworksContainer.class)
430                 .child(Networks.class, new NetworksKey(prefix, RD));
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 }