6e3a42cdc743093b7b53bb2d74716a450265ae8b
[netvirt.git] / elanmanager / impl / src / main / java / org / opendaylight / netvirt / elan / l2gw / utils / L2GatewayConnectionUtils.java
1 /*
2  * Copyright (c) 2016 Ericsson India Global Services Pvt Ltd. 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
9 package org.opendaylight.netvirt.elan.l2gw.utils;
10
11 import static java.util.Collections.emptyList;
12 import static org.opendaylight.netvirt.elan.utils.ElanUtils.isVxlanNetworkOrVxlanSegment;
13 import static org.opendaylight.netvirt.elan.utils.ElanUtils.requireNonNullElse;
14
15 import com.google.common.base.Optional;
16 import com.google.common.collect.Lists;
17 import com.google.common.util.concurrent.Futures;
18 import com.google.common.util.concurrent.MoreExecutors;
19 import com.google.common.util.concurrent.SettableFuture;
20 import java.util.ArrayList;
21 import java.util.List;
22 import java.util.Objects;
23 import java.util.Set;
24 import java.util.concurrent.CopyOnWriteArrayList;
25 import javax.annotation.Nonnull;
26 import javax.annotation.Nullable;
27 import javax.annotation.PreDestroy;
28 import javax.inject.Inject;
29 import javax.inject.Singleton;
30 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
31 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
32 import org.opendaylight.genius.mdsalutil.MDSALUtil;
33 import org.opendaylight.genius.utils.hwvtep.HwvtepNodeHACache;
34 import org.opendaylight.genius.utils.hwvtep.HwvtepSouthboundUtils;
35 import org.opendaylight.genius.utils.hwvtep.HwvtepUtils;
36 import org.opendaylight.infrautils.jobcoordinator.JobCoordinator;
37 import org.opendaylight.netvirt.elan.cache.ElanInstanceCache;
38 import org.opendaylight.netvirt.elan.l2gw.ha.listeners.HAOpClusteredListener;
39 import org.opendaylight.netvirt.elan.l2gw.jobs.AssociateHwvtepToElanJob;
40 import org.opendaylight.netvirt.elan.l2gw.jobs.DisAssociateHwvtepFromElanJob;
41 import org.opendaylight.netvirt.elan.l2gw.listeners.HwvtepLogicalSwitchListener;
42 import org.opendaylight.netvirt.elan.l2gw.listeners.LocalUcastMacListener;
43 import org.opendaylight.netvirt.elan.utils.ElanClusterUtils;
44 import org.opendaylight.netvirt.elanmanager.utils.ElanL2GwCacheUtils;
45 import org.opendaylight.netvirt.neutronvpn.api.l2gw.L2GatewayCache;
46 import org.opendaylight.netvirt.neutronvpn.api.l2gw.L2GatewayDevice;
47 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.instances.ElanInstance;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateway.attributes.Devices;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateway.connections.attributes.L2gatewayConnections;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateway.connections.attributes.l2gatewayconnections.L2gatewayConnection;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateways.attributes.L2gateways;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateways.attributes.l2gateways.L2gateway;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateways.attributes.l2gateways.L2gatewayKey;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.rev150712.Neutron;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalAugmentation;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LocalUcastMacs;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LogicalSwitches;
59 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
60 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
61 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
62 import org.slf4j.Logger;
63 import org.slf4j.LoggerFactory;
64
65 @Singleton
66 public class L2GatewayConnectionUtils implements AutoCloseable {
67     private static final Logger LOG = LoggerFactory.getLogger(L2GatewayConnectionUtils.class);
68
69     private final DataBroker broker;
70     private final ElanL2GatewayUtils elanL2GatewayUtils;
71     private final ElanClusterUtils elanClusterUtils;
72     private final ElanL2GatewayMulticastUtils elanL2GatewayMulticastUtils;
73     private final JobCoordinator jobCoordinator;
74     private final L2GatewayCache l2GatewayCache;
75     private final ElanInstanceCache elanInstanceCache;
76     private final List<AutoCloseable> closeables = new CopyOnWriteArrayList<>();
77     private final HwvtepNodeHACache hwvtepNodeHACache;
78     private final HAOpClusteredListener haOpClusteredListener;
79
80     @Inject
81     public L2GatewayConnectionUtils(DataBroker dataBroker,
82             ElanClusterUtils elanClusterUtils, ElanL2GatewayUtils elanL2GatewayUtils,
83             JobCoordinator jobCoordinator, ElanL2GatewayMulticastUtils elanL2GatewayMulticastUtils,
84             L2GatewayCache l2GatewayCache, HAOpClusteredListener haOpClusteredListener,
85             ElanInstanceCache elanInstanceCache, HwvtepNodeHACache hwvtepNodeHACache) {
86         this.broker = dataBroker;
87         this.elanL2GatewayUtils = elanL2GatewayUtils;
88         this.elanClusterUtils = elanClusterUtils;
89         this.elanL2GatewayMulticastUtils = elanL2GatewayMulticastUtils;
90         this.jobCoordinator = jobCoordinator;
91         this.l2GatewayCache = l2GatewayCache;
92         this.haOpClusteredListener = haOpClusteredListener;
93         this.elanInstanceCache = elanInstanceCache;
94         this.hwvtepNodeHACache = hwvtepNodeHACache;
95     }
96
97     @Override
98     @PreDestroy
99     @SuppressWarnings("checkstyle:IllegalCatch")
100     public void close() {
101         closeables.forEach(c -> {
102             try {
103                 c.close();
104             } catch (Exception e) {
105                 LOG.warn("Error closing {}", c, e);
106             }
107         });
108     }
109
110     public static boolean isGatewayAssociatedToL2Device(L2GatewayDevice l2GwDevice) {
111         return !l2GwDevice.getL2GatewayIds().isEmpty();
112     }
113
114     @Nullable
115     public static L2gateway getNeutronL2gateway(DataBroker broker, Uuid l2GatewayId) {
116         LOG.debug("getNeutronL2gateway for {}", l2GatewayId.getValue());
117         InstanceIdentifier<L2gateway> inst = InstanceIdentifier.create(Neutron.class).child(L2gateways.class)
118                 .child(L2gateway.class, new L2gatewayKey(l2GatewayId));
119         return MDSALUtil.read(broker, LogicalDatastoreType.CONFIGURATION, inst).orNull();
120     }
121
122     @Nonnull
123     public static List<L2gateway> getL2gatewayList(DataBroker broker) {
124         InstanceIdentifier<L2gateways> inst = InstanceIdentifier.create(Neutron.class).child(L2gateways.class);
125         return MDSALUtil.read(broker, LogicalDatastoreType.CONFIGURATION, inst).toJavaUtil().map(
126                 L2gateways::getL2gateway).orElse(emptyList());
127     }
128
129     @Nonnull
130     public static List<L2gatewayConnection> getAllL2gatewayConnections(DataBroker broker) {
131         InstanceIdentifier<L2gatewayConnections> inst = InstanceIdentifier.create(Neutron.class)
132                 .child(L2gatewayConnections.class);
133         return MDSALUtil.read(broker, LogicalDatastoreType.CONFIGURATION, inst).toJavaUtil().map(
134                 L2gatewayConnections::getL2gatewayConnection).orElse(emptyList());
135     }
136
137     /**
138      * Gets the associated l2 gw connections.
139      *
140      * @param broker
141      *            the broker
142      * @param l2GatewayIds
143      *            the l2 gateway ids
144      * @return the associated l2 gw connections
145      */
146     @Nonnull
147     public static List<L2gatewayConnection> getAssociatedL2GwConnections(DataBroker broker, Set<Uuid> l2GatewayIds) {
148         List<L2gatewayConnection> allL2GwConns = getAllL2gatewayConnections(broker);
149         List<L2gatewayConnection> l2GwConnections = new ArrayList<>();
150         for (Uuid l2GatewayId : l2GatewayIds) {
151             for (L2gatewayConnection l2GwConn : allL2GwConns) {
152                 if (Objects.equals(l2GwConn.getL2gatewayId(), l2GatewayId)) {
153                     l2GwConnections.add(l2GwConn);
154                 }
155             }
156         }
157         return l2GwConnections;
158     }
159
160     /**
161      * Gets the associated l2 gw connections.
162      *
163      * @param broker
164      *            the broker
165      * @param elanName
166      *            the elan Name
167      * @return the associated l2 gw connection with elan
168      */
169     @Nonnull
170     public static List<L2gatewayConnection> getL2GwConnectionsByElanName(DataBroker broker, String elanName) {
171         List<L2gatewayConnection> allL2GwConns = getAllL2gatewayConnections(broker);
172         List<L2gatewayConnection> elanL2GateWayConnections = new ArrayList<>();
173         for (L2gatewayConnection l2GwConn : allL2GwConns) {
174             if (l2GwConn.getNetworkId().getValue().equalsIgnoreCase(elanName)) {
175                 elanL2GateWayConnections.add(l2GwConn);
176             }
177         }
178         return elanL2GateWayConnections;
179     }
180
181     public void addL2GatewayConnection(L2gatewayConnection input) {
182         addL2GatewayConnection(input, null/*deviceName*/, null);
183     }
184
185     public void addL2GatewayConnection(final L2gatewayConnection input,
186                                        final String l2GwDeviceName) {
187         addL2GatewayConnection(input, l2GwDeviceName, null);
188     }
189
190     public void addL2GatewayConnection(final L2gatewayConnection input,
191                                        @Nullable final String l2GwDeviceName ,
192                                        @Nullable L2gateway l2Gateway) {
193         LOG.info("Adding L2gateway Connection with ID: {}", input.key().getUuid());
194
195         Uuid networkUuid = input.getNetworkId();
196
197         // Taking cluster reboot scenario , if Elan instance is not available when l2GatewayConnection add events
198         // comes we need to wait for elaninstance to resolve. Hence updating the map with the runnable .
199         // When elanInstance add comes , it look in to the map and run the associated runnable associated with it.
200         ElanInstance elanInstance = elanInstanceCache.get(networkUuid.getValue(),
201             () -> addL2GatewayConnection(input, l2GwDeviceName)).orNull();
202         if (elanInstance == null) {
203             return;
204         }
205
206         if (!isVxlanNetworkOrVxlanSegment(elanInstance)) {
207             LOG.error("Neutron network with id {} is not VxlanNetwork", networkUuid.getValue());
208         } else {
209             Uuid l2GatewayId = input.getL2gatewayId();
210             if (l2Gateway == null) {
211                 l2Gateway = getNeutronL2gateway(broker, l2GatewayId);
212             }
213             if (l2Gateway == null) {
214                 LOG.error("L2Gateway with id {} is not present", l2GatewayId.getValue());
215             } else {
216                 associateHwvtepsToElan(elanInstance, l2Gateway, input, l2GwDeviceName);
217             }
218         }
219     }
220
221     public void deleteL2GatewayConnection(L2gatewayConnection input) {
222         LOG.info("Deleting L2gateway Connection with ID: {}", input.key().getUuid());
223
224         Uuid networkUuid = input.getNetworkId();
225         String elanName = networkUuid.getValue();
226         disAssociateHwvtepsFromElan(elanName, input);
227     }
228
229     private void disAssociateHwvtepsFromElan(String elanName, L2gatewayConnection input) {
230         Integer defaultVlan = input.getSegmentId();
231         List<L2GatewayDevice> l2Devices =
232             requireNonNullElse(ElanL2GwCacheUtils.getAllElanDevicesFromCache(), emptyList());
233         List<Devices> l2gwDevicesToBeDeleted = new ArrayList<>();
234         for (L2GatewayDevice elanL2gwDevice : l2Devices) {
235             if (elanL2gwDevice.getL2GatewayIds().contains(input.key().getUuid())) {
236                 l2gwDevicesToBeDeleted.addAll(elanL2gwDevice.getDevicesForL2gwConnectionId(input.key().getUuid()));
237             }
238         }
239         if (l2gwDevicesToBeDeleted.isEmpty()) {
240             //delete logical switch
241             Uuid l2GatewayId = input.getL2gatewayId();
242             L2gateway l2Gateway = L2GatewayConnectionUtils.getNeutronL2gateway(broker, l2GatewayId);
243             if (l2Gateway == null) {
244                 LOG.error("Failed to find the l2gateway for the connection {}", input.getUuid());
245                 return;
246             } else if (l2Gateway.getDevices() != null) {
247                 l2gwDevicesToBeDeleted.addAll(l2Gateway.getDevices());
248             }
249         }
250         for (Devices l2Device : l2gwDevicesToBeDeleted) {
251             String l2DeviceName = l2Device.getDeviceName();
252             L2GatewayDevice l2GatewayDevice = l2GatewayCache.get(l2DeviceName);
253             String hwvtepNodeId = l2GatewayDevice.getHwvtepNodeId();
254             boolean isLastL2GwConnDeleted = false;
255             L2GatewayDevice elanL2GwDevice = ElanL2GwCacheUtils.getL2GatewayDeviceFromCache(elanName, hwvtepNodeId);
256             if (elanL2GwDevice != null && isLastL2GwConnBeingDeleted(elanL2GwDevice)) {
257                 // Delete L2 Gateway device from 'ElanL2GwDevice' cache
258                 LOG.debug("Elan L2Gw Conn cache removed for id {}", hwvtepNodeId);
259                 ElanL2GwCacheUtils.removeL2GatewayDeviceFromCache(elanName, hwvtepNodeId);
260                 isLastL2GwConnDeleted = true;
261             } else {
262                 Uuid l2GwConnId = input.key().getUuid();
263                 LOG.debug("Elan L2Gw Conn cache with id {} is being referred by other L2Gw Conns; so only "
264                         + "L2 Gw Conn {} reference is removed", hwvtepNodeId, l2GwConnId);
265                 if (elanL2GwDevice != null) {
266                     elanL2GwDevice.removeL2GatewayId(l2GwConnId);
267                 } else {
268                     isLastL2GwConnDeleted = true;
269                 }
270             }
271
272             DisAssociateHwvtepFromElanJob disAssociateHwvtepToElanJob =
273                     new DisAssociateHwvtepFromElanJob(elanL2GatewayUtils, elanL2GatewayMulticastUtils,
274                             elanL2GwDevice, elanName, () -> elanInstanceCache.get(elanName).orNull(),
275                             l2Device, defaultVlan, hwvtepNodeId, isLastL2GwConnDeleted);
276             elanClusterUtils.runOnlyInOwnerNode(disAssociateHwvtepToElanJob.getJobKey(), "remove l2gw connection job",
277                     disAssociateHwvtepToElanJob);
278         }
279     }
280
281     private void associateHwvtepsToElan(ElanInstance elanInstance,
282             L2gateway l2Gateway, L2gatewayConnection input, @Nullable String l2GwDeviceName) {
283         String elanName = elanInstance.getElanInstanceName();
284         Integer defaultVlan = input.getSegmentId();
285         Uuid l2GwConnId = input.key().getUuid();
286         List<Devices> l2Devices = requireNonNullElse(l2Gateway.getDevices(), emptyList());
287
288         LOG.trace("Associating ELAN {} with L2Gw Conn Id {} having below L2Gw devices {}", elanName, l2GwConnId,
289                 l2Devices);
290
291         for (Devices l2Device : l2Devices) {
292             String l2DeviceName = l2Device.getDeviceName();
293             // L2gateway can have more than one L2 Gw devices. Configure Logical Switch, VLAN mappings,...
294             // only on the switch which has come up just now and exclude all other devices from
295             // preprovisioning/re-provisioning
296             if (l2GwDeviceName != null && !l2GwDeviceName.equals(l2DeviceName)) {
297                 LOG.debug("Associating Hwvtep to ELAN is not been processed for {}; as only {} got connected now!",
298                         l2DeviceName, l2GwDeviceName);
299                 continue;
300             }
301             L2GatewayDevice l2GatewayDevice = l2GatewayCache.get(l2DeviceName);
302             if (isL2GwDeviceConnected(l2GatewayDevice)) {
303                 NodeId hwvtepNodeId = new NodeId(l2GatewayDevice.getHwvtepNodeId());
304
305                 // Delete pending delete logical switch task if scheduled
306                 elanL2GatewayUtils.cancelDeleteLogicalSwitch(hwvtepNodeId,
307                         ElanL2GatewayUtils.getLogicalSwitchFromElan(elanName));
308
309                 // Add L2 Gateway device to 'ElanL2GwDevice' cache
310                 boolean createLogicalSwitch;
311                 LogicalSwitches logicalSwitch = HwvtepUtils.getLogicalSwitch(broker, LogicalDatastoreType.CONFIGURATION,
312                         hwvtepNodeId, elanName);
313                 if (logicalSwitch == null) {
314                     HwvtepLogicalSwitchListener hwVTEPLogicalSwitchListener = new HwvtepLogicalSwitchListener(
315                             elanInstanceCache, elanL2GatewayUtils, elanClusterUtils, elanL2GatewayMulticastUtils,
316                             this, l2GatewayDevice, elanName, l2Device, defaultVlan, l2GwConnId, hwvtepNodeHACache);
317                     hwVTEPLogicalSwitchListener.registerListener(LogicalDatastoreType.OPERATIONAL, broker);
318                     closeables.add(hwVTEPLogicalSwitchListener);
319                     createLogicalSwitch = true;
320                 } else {
321                     addL2DeviceToElanL2GwCache(elanName, l2GatewayDevice, l2GwConnId, l2Device);
322                     createLogicalSwitch = false;
323                 }
324                 AssociateHwvtepToElanJob associateHwvtepToElanJob = new AssociateHwvtepToElanJob(broker,
325                         elanL2GatewayUtils, elanL2GatewayMulticastUtils, elanInstanceCache, l2GatewayDevice,
326                         elanInstance, l2Device, defaultVlan, createLogicalSwitch);
327
328                 elanClusterUtils.runOnlyInOwnerNode(associateHwvtepToElanJob.getJobKey(),
329                         "create logical switch in hwvtep topo", associateHwvtepToElanJob);
330
331             } else {
332                 LOG.info("L2GwConn create is not handled for device with id {} as it's not connected", l2DeviceName);
333             }
334         }
335     }
336
337     public L2GatewayDevice addL2DeviceToElanL2GwCache(String elanName, L2GatewayDevice l2GatewayDevice, Uuid l2GwConnId,
338             Devices l2Device) {
339         String l2gwDeviceNodeId = l2GatewayDevice.getHwvtepNodeId();
340         L2GatewayDevice elanL2GwDevice = ElanL2GwCacheUtils.getL2GatewayDeviceFromCache(elanName, l2gwDeviceNodeId);
341         if (elanL2GwDevice == null) {
342             elanL2GwDevice = new L2GatewayDevice(l2GatewayDevice.getDeviceName());
343             elanL2GwDevice.setHwvtepNodeId(l2gwDeviceNodeId);
344             elanL2GwDevice.setTunnelIps(l2GatewayDevice.getTunnelIps());
345             ElanL2GwCacheUtils.addL2GatewayDeviceToCache(elanName, elanL2GwDevice);
346             LOG.debug("Elan L2GwConn cache created for hwvtep id {}", l2gwDeviceNodeId);
347         } else {
348             LOG.debug("Elan L2GwConn cache already exists for hwvtep id {}; updating L2GwConn id {} to it",
349                     l2gwDeviceNodeId, l2GwConnId);
350         }
351         elanL2GwDevice.addL2GatewayId(l2GwConnId);
352         elanL2GwDevice.addL2gwConnectionIdToDevice(l2GwConnId, l2Device);
353
354         //incase of cluster reboot scenario southbound device would have added more macs
355         //while odl is down, pull them now
356         readAndCopyLocalUcastMacsToCache(elanName, l2GatewayDevice);
357
358         LOG.trace("Elan L2GwConn cache updated with below details: {}", elanL2GwDevice);
359         return elanL2GwDevice;
360     }
361
362     private static boolean isL2GwDeviceConnected(L2GatewayDevice l2GwDevice) {
363         return l2GwDevice != null && l2GwDevice.getHwvtepNodeId() != null;
364     }
365
366     protected static boolean isLastL2GwConnBeingDeleted(@Nonnull L2GatewayDevice l2GwDevice) {
367         return l2GwDevice.getL2GatewayIds().size() == 1;
368     }
369
370     private void readAndCopyLocalUcastMacsToCache(final String elanName, final L2GatewayDevice l2GatewayDevice) {
371         final InstanceIdentifier<Node> nodeIid = HwvtepSouthboundUtils.createInstanceIdentifier(
372                 new NodeId(l2GatewayDevice.getHwvtepNodeId()));
373         jobCoordinator.enqueueJob(elanName + ":" + l2GatewayDevice.getDeviceName(), () -> {
374             final SettableFuture settableFuture = SettableFuture.create();
375             Futures.addCallback(broker.newReadOnlyTransaction().read(LogicalDatastoreType.OPERATIONAL,
376                     nodeIid), new SettableFutureCallback<Optional<Node>>(settableFuture) {
377                         @Override
378                         public void onSuccess(@Nonnull Optional<Node> resultNode) {
379                             LocalUcastMacListener localUcastMacListener =
380                                     new LocalUcastMacListener(broker, haOpClusteredListener,
381                                             elanL2GatewayUtils, jobCoordinator, elanInstanceCache, hwvtepNodeHACache);
382                             settableFuture.set(resultNode);
383                             Optional<Node> nodeOptional = resultNode;
384                             if (nodeOptional.isPresent()) {
385                                 Node node = nodeOptional.get();
386                                 if (node.augmentation(HwvtepGlobalAugmentation.class) != null) {
387                                     List<LocalUcastMacs> localUcastMacs =
388                                             node.augmentation(HwvtepGlobalAugmentation.class).getLocalUcastMacs();
389                                     if (localUcastMacs == null) {
390                                         return;
391                                     }
392                                     localUcastMacs.stream()
393                                             .filter((mac) -> macBelongsToLogicalSwitch(mac, elanName))
394                                             .forEach((mac) -> {
395                                                 InstanceIdentifier<LocalUcastMacs> macIid = getMacIid(nodeIid, mac);
396                                                 localUcastMacListener.added(macIid, mac);
397                                             });
398                                 }
399                             }
400                         }
401                     }, MoreExecutors.directExecutor());
402             return Lists.newArrayList(settableFuture);
403         } , 5);
404     }
405
406     /**
407      * Gets the associated l2 gw connections.
408      *
409      * @param l2GatewayId the l2 gateway id
410      *
411      * @return the associated l2 gw connections
412      */
413     public List<L2gatewayConnection> getL2GwConnectionsByL2GatewayId(Uuid l2GatewayId) {
414         List<L2gatewayConnection> l2GwConnections = new ArrayList<>();
415         List<L2gatewayConnection> allL2GwConns = getAllL2gatewayConnections(broker);
416         for (L2gatewayConnection l2GwConn : allL2GwConns) {
417             if (Objects.equals(l2GwConn.getL2gatewayId(), l2GatewayId)) {
418                 l2GwConnections.add(l2GwConn);
419             }
420         }
421         return l2GwConnections;
422     }
423
424     private static boolean macBelongsToLogicalSwitch(LocalUcastMacs mac, String logicalSwitchName) {
425         InstanceIdentifier<LogicalSwitches> iid = (InstanceIdentifier<LogicalSwitches>)
426                 mac.getLogicalSwitchRef().getValue();
427         return iid.firstKeyOf(LogicalSwitches.class).getHwvtepNodeName().getValue().equals(logicalSwitchName);
428     }
429
430     static InstanceIdentifier<LocalUcastMacs> getMacIid(InstanceIdentifier<Node> nodeIid, LocalUcastMacs mac) {
431         return nodeIid.augmentation(HwvtepGlobalAugmentation.class).child(LocalUcastMacs.class, mac.key());
432     }
433 }