package org.opendaylight.netvirt.elan.l2gw.utils;
+import static java.util.Collections.emptyList;
import static org.opendaylight.netvirt.elan.utils.ElanUtils.isVxlanNetworkOrVxlanSegment;
-import com.google.common.base.Optional;
import com.google.common.collect.Lists;
+import com.google.common.util.concurrent.FluentFuture;
+import com.google.common.util.concurrent.FutureCallback;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.MoreExecutors;
-import com.google.common.util.concurrent.SettableFuture;
+import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.util.ArrayList;
-import java.util.Collections;
import java.util.List;
+import java.util.Objects;
+import java.util.Optional;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArrayList;
-import javax.annotation.Nonnull;
+import java.util.concurrent.ExecutionException;
import javax.annotation.PreDestroy;
import javax.inject.Inject;
import javax.inject.Singleton;
-import org.opendaylight.controller.md.sal.binding.api.DataBroker;
-import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
-import org.opendaylight.genius.mdsalutil.MDSALUtil;
+import org.eclipse.jdt.annotation.NonNull;
+import org.eclipse.jdt.annotation.Nullable;
+import org.opendaylight.genius.datastoreutils.SingleTransactionDataBroker;
+import org.opendaylight.genius.utils.hwvtep.HwvtepNodeHACache;
import org.opendaylight.genius.utils.hwvtep.HwvtepSouthboundUtils;
-import org.opendaylight.genius.utils.hwvtep.HwvtepUtils;
import org.opendaylight.infrautils.jobcoordinator.JobCoordinator;
+import org.opendaylight.mdsal.binding.api.DataBroker;
+import org.opendaylight.mdsal.binding.util.ManagedNewTransactionRunner;
+import org.opendaylight.mdsal.binding.util.RetryingManagedNewTransactionRunner;
+import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
import org.opendaylight.netvirt.elan.cache.ElanInstanceCache;
import org.opendaylight.netvirt.elan.l2gw.ha.listeners.HAOpClusteredListener;
import org.opendaylight.netvirt.elan.l2gw.jobs.AssociateHwvtepToElanJob;
import org.opendaylight.netvirt.elan.l2gw.jobs.DisAssociateHwvtepFromElanJob;
-import org.opendaylight.netvirt.elan.l2gw.listeners.HwvtepLogicalSwitchListener;
import org.opendaylight.netvirt.elan.l2gw.listeners.LocalUcastMacListener;
+import org.opendaylight.netvirt.elan.l2gw.recovery.impl.L2GatewayServiceRecoveryHandler;
import org.opendaylight.netvirt.elan.utils.ElanClusterUtils;
import org.opendaylight.netvirt.elanmanager.utils.ElanL2GwCacheUtils;
import org.opendaylight.netvirt.neutronvpn.api.l2gw.L2GatewayCache;
import org.opendaylight.netvirt.neutronvpn.api.l2gw.L2GatewayDevice;
+import org.opendaylight.serviceutils.srm.ServiceRecoveryRegistry;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.instances.ElanInstance;
import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateway.attributes.Devices;
private final L2GatewayCache l2GatewayCache;
private final ElanInstanceCache elanInstanceCache;
private final List<AutoCloseable> closeables = new CopyOnWriteArrayList<>();
+ private final HwvtepNodeHACache hwvtepNodeHACache;
private final HAOpClusteredListener haOpClusteredListener;
+ private final ElanRefUtil elanRefUtil;
+ private final L2GatewayServiceRecoveryHandler l2GatewayServiceRecoveryHandler;
+ private final ServiceRecoveryRegistry serviceRecoveryRegistry;
+ private final ManagedNewTransactionRunner txRunner;
@Inject
- public L2GatewayConnectionUtils(DataBroker dataBroker,
- ElanClusterUtils elanClusterUtils, ElanL2GatewayUtils elanL2GatewayUtils,
- JobCoordinator jobCoordinator, ElanL2GatewayMulticastUtils elanL2GatewayMulticastUtils,
- L2GatewayCache l2GatewayCache, HAOpClusteredListener haOpClusteredListener,
- ElanInstanceCache elanInstanceCache) {
+ public L2GatewayConnectionUtils(DataBroker dataBroker, ElanClusterUtils elanClusterUtils,
+ ElanL2GatewayUtils elanL2GatewayUtils, JobCoordinator jobCoordinator,
+ ElanL2GatewayMulticastUtils elanL2GatewayMulticastUtils,
+ L2GatewayCache l2GatewayCache, HAOpClusteredListener haOpClusteredListener,
+ ElanInstanceCache elanInstanceCache, HwvtepNodeHACache hwvtepNodeHACache,
+ ElanRefUtil elanRefUtil,
+ L2GatewayServiceRecoveryHandler l2GatewayServiceRecoveryHandler,
+ ServiceRecoveryRegistry serviceRecoveryRegistry) {
this.broker = dataBroker;
this.elanL2GatewayUtils = elanL2GatewayUtils;
this.elanClusterUtils = elanClusterUtils;
this.l2GatewayCache = l2GatewayCache;
this.haOpClusteredListener = haOpClusteredListener;
this.elanInstanceCache = elanInstanceCache;
+ this.hwvtepNodeHACache = hwvtepNodeHACache;
+ this.elanRefUtil = elanRefUtil;
+ this.l2GatewayServiceRecoveryHandler = l2GatewayServiceRecoveryHandler;
+ this.serviceRecoveryRegistry = serviceRecoveryRegistry;
+ this.txRunner = new RetryingManagedNewTransactionRunner(dataBroker);
}
@Override
}
public static boolean isGatewayAssociatedToL2Device(L2GatewayDevice l2GwDevice) {
- return l2GwDevice.getL2GatewayIds().size() > 0;
+ return !l2GwDevice.getL2GatewayIds().isEmpty();
}
+ @Nullable
public static L2gateway getNeutronL2gateway(DataBroker broker, Uuid l2GatewayId) {
LOG.debug("getNeutronL2gateway for {}", l2GatewayId.getValue());
InstanceIdentifier<L2gateway> inst = InstanceIdentifier.create(Neutron.class).child(L2gateways.class)
.child(L2gateway.class, new L2gatewayKey(l2GatewayId));
- return MDSALUtil.read(broker, LogicalDatastoreType.CONFIGURATION, inst).orNull();
+ try {
+ return SingleTransactionDataBroker.syncReadOptional(broker, LogicalDatastoreType.CONFIGURATION, inst)
+ .orElse(null);
+ } catch (ExecutionException | InterruptedException e) {
+ LOG.error("getNeutronL2gateway: Exception while reading L2gateway DS for the ID {}", l2GatewayId, e);
+ }
+ return null;
}
- @Nonnull
+ @NonNull
public static List<L2gateway> getL2gatewayList(DataBroker broker) {
InstanceIdentifier<L2gateways> inst = InstanceIdentifier.create(Neutron.class).child(L2gateways.class);
- return MDSALUtil.read(broker, LogicalDatastoreType.CONFIGURATION, inst).toJavaUtil().map(
- L2gateways::getL2gateway).orElse(Collections.emptyList());
+ try {
+ return SingleTransactionDataBroker.syncReadOptional(broker, LogicalDatastoreType.CONFIGURATION, inst).map(
+ L2gateways::getL2gateway).orElse(emptyList());
+ } catch (ExecutionException | InterruptedException e) {
+ LOG.error("getNeutronL2gateway: Exception while reading L2gateway DS", e);
+ }
+ return null;
}
- @Nonnull
+ @NonNull
public static List<L2gatewayConnection> getAllL2gatewayConnections(DataBroker broker) {
InstanceIdentifier<L2gatewayConnections> inst = InstanceIdentifier.create(Neutron.class)
.child(L2gatewayConnections.class);
- return MDSALUtil.read(broker, LogicalDatastoreType.CONFIGURATION, inst).toJavaUtil().map(
- L2gatewayConnections::getL2gatewayConnection).orElse(Collections.emptyList());
+ try {
+ return SingleTransactionDataBroker.syncReadOptional(broker, LogicalDatastoreType.CONFIGURATION, inst).map(
+ L2gatewayConnections::getL2gatewayConnection).orElse(emptyList());
+ } catch (ExecutionException | InterruptedException e) {
+ LOG.error("getNeutronL2gateway: Exception while reading L2gateway DS", e);
+ }
+ return null;
}
/**
* the l2 gateway ids
* @return the associated l2 gw connections
*/
- @Nonnull
+ @NonNull
public static List<L2gatewayConnection> getAssociatedL2GwConnections(DataBroker broker, Set<Uuid> l2GatewayIds) {
List<L2gatewayConnection> allL2GwConns = getAllL2gatewayConnections(broker);
List<L2gatewayConnection> l2GwConnections = new ArrayList<>();
for (Uuid l2GatewayId : l2GatewayIds) {
for (L2gatewayConnection l2GwConn : allL2GwConns) {
- if (l2GwConn.getL2gatewayId().equals(l2GatewayId)) {
+ if (Objects.equals(l2GwConn.getL2gatewayId(), l2GatewayId)) {
l2GwConnections.add(l2GwConn);
}
}
* the elan Name
* @return the associated l2 gw connection with elan
*/
- @Nonnull
+ @NonNull
public static List<L2gatewayConnection> getL2GwConnectionsByElanName(DataBroker broker, String elanName) {
List<L2gatewayConnection> allL2GwConns = getAllL2gatewayConnections(broker);
List<L2gatewayConnection> elanL2GateWayConnections = new ArrayList<>();
}
public void addL2GatewayConnection(final L2gatewayConnection input,
- final String l2GwDeviceName ,
- L2gateway l2Gateway) {
- LOG.info("Adding L2gateway Connection with ID: {}", input.getKey().getUuid());
+ @Nullable final String l2GwDeviceName ,
+ @Nullable L2gateway l2Gateway) {
+ LOG.info("Adding L2gateway Connection with ID: {}", input.key().getUuid());
Uuid networkUuid = input.getNetworkId();
// comes we need to wait for elaninstance to resolve. Hence updating the map with the runnable .
// When elanInstance add comes , it look in to the map and run the associated runnable associated with it.
ElanInstance elanInstance = elanInstanceCache.get(networkUuid.getValue(),
- () -> addL2GatewayConnection(input, l2GwDeviceName)).orNull();
+ () -> addL2GatewayConnection(input, l2GwDeviceName)).orElse(null);
if (elanInstance == null) {
return;
}
}
public void deleteL2GatewayConnection(L2gatewayConnection input) {
- LOG.info("Deleting L2gateway Connection with ID: {}", input.getKey().getUuid());
+ LOG.info("Deleting L2gateway Connection with ID: {}", input.key().getUuid());
Uuid networkUuid = input.getNetworkId();
String elanName = networkUuid.getValue();
List<L2GatewayDevice> l2Devices = ElanL2GwCacheUtils.getAllElanDevicesFromCache();
List<Devices> l2gwDevicesToBeDeleted = new ArrayList<>();
for (L2GatewayDevice elanL2gwDevice : l2Devices) {
- if (elanL2gwDevice.getL2GatewayIds().contains(input.getKey().getUuid())) {
- l2gwDevicesToBeDeleted.addAll(elanL2gwDevice.getDevicesForL2gwConnectionId(input.getKey().getUuid()));
+ if (elanL2gwDevice.getL2GatewayIds().contains(input.key().getUuid())) {
+ l2gwDevicesToBeDeleted.addAll(elanL2gwDevice.getDevicesForL2gwConnectionId(input.key().getUuid()));
}
}
if (l2gwDevicesToBeDeleted.isEmpty()) {
if (l2Gateway == null) {
LOG.error("Failed to find the l2gateway for the connection {}", input.getUuid());
return;
- } else {
+ } else if (l2Gateway.getDevices() != null) {
l2gwDevicesToBeDeleted.addAll(l2Gateway.getDevices());
}
}
ElanL2GwCacheUtils.removeL2GatewayDeviceFromCache(elanName, hwvtepNodeId);
isLastL2GwConnDeleted = true;
} else {
- Uuid l2GwConnId = input.getKey().getUuid();
+ Uuid l2GwConnId = input.key().getUuid();
LOG.debug("Elan L2Gw Conn cache with id {} is being referred by other L2Gw Conns; so only "
+ "L2 Gw Conn {} reference is removed", hwvtepNodeId, l2GwConnId);
if (elanL2GwDevice != null) {
DisAssociateHwvtepFromElanJob disAssociateHwvtepToElanJob =
new DisAssociateHwvtepFromElanJob(elanL2GatewayUtils, elanL2GatewayMulticastUtils,
- elanL2GwDevice, elanName, () -> elanInstanceCache.get(elanName).orNull(),
- l2Device, defaultVlan, hwvtepNodeId, isLastL2GwConnDeleted);
+ elanL2GwDevice, elanName,
+ l2Device, defaultVlan, hwvtepNodeId, isLastL2GwConnDeleted);
elanClusterUtils.runOnlyInOwnerNode(disAssociateHwvtepToElanJob.getJobKey(), "remove l2gw connection job",
disAssociateHwvtepToElanJob);
}
}
private void associateHwvtepsToElan(ElanInstance elanInstance,
- L2gateway l2Gateway, L2gatewayConnection input, String l2GwDeviceName) {
+ L2gateway l2Gateway, L2gatewayConnection input, @Nullable String l2GwDeviceName) {
String elanName = elanInstance.getElanInstanceName();
Integer defaultVlan = input.getSegmentId();
- Uuid l2GwConnId = input.getKey().getUuid();
+ Uuid l2GwConnId = input.key().getUuid();
List<Devices> l2Devices = l2Gateway.getDevices();
LOG.trace("Associating ELAN {} with L2Gw Conn Id {} having below L2Gw devices {}", elanName, l2GwConnId,
l2Devices);
+ if (l2Devices == null) {
+ return;
+ }
+
for (Devices l2Device : l2Devices) {
String l2DeviceName = l2Device.getDeviceName();
// L2gateway can have more than one L2 Gw devices. Configure Logical Switch, VLAN mappings,...
// Add L2 Gateway device to 'ElanL2GwDevice' cache
boolean createLogicalSwitch;
- LogicalSwitches logicalSwitch = HwvtepUtils.getLogicalSwitch(broker, LogicalDatastoreType.CONFIGURATION,
- hwvtepNodeId, elanName);
- if (logicalSwitch == null) {
- HwvtepLogicalSwitchListener hwVTEPLogicalSwitchListener = new HwvtepLogicalSwitchListener(
- elanInstanceCache, elanL2GatewayUtils, elanClusterUtils, elanL2GatewayMulticastUtils,
- this, l2GatewayDevice, elanName, l2Device, defaultVlan, l2GwConnId);
- hwVTEPLogicalSwitchListener.registerListener(LogicalDatastoreType.OPERATIONAL, broker);
- closeables.add(hwVTEPLogicalSwitchListener);
- createLogicalSwitch = true;
- } else {
- addL2DeviceToElanL2GwCache(elanName, l2GatewayDevice, l2GwConnId, l2Device);
- createLogicalSwitch = false;
- }
+ addL2DeviceToElanL2GwCache(elanName, l2GatewayDevice, l2GwConnId, l2Device);
+
AssociateHwvtepToElanJob associateHwvtepToElanJob = new AssociateHwvtepToElanJob(broker,
elanL2GatewayUtils, elanL2GatewayMulticastUtils, elanInstanceCache, l2GatewayDevice,
- elanInstance, l2Device, defaultVlan, createLogicalSwitch);
+ elanInstance, l2Device, defaultVlan, elanRefUtil);
elanClusterUtils.runOnlyInOwnerNode(associateHwvtepToElanJob.getJobKey(),
"create logical switch in hwvtep topo", associateHwvtepToElanJob);
return l2GwDevice != null && l2GwDevice.getHwvtepNodeId() != null;
}
- protected static boolean isLastL2GwConnBeingDeleted(@Nonnull L2GatewayDevice l2GwDevice) {
+ protected static boolean isLastL2GwConnBeingDeleted(@NonNull L2GatewayDevice l2GwDevice) {
return l2GwDevice.getL2GatewayIds().size() == 1;
}
final InstanceIdentifier<Node> nodeIid = HwvtepSouthboundUtils.createInstanceIdentifier(
new NodeId(l2GatewayDevice.getHwvtepNodeId()));
jobCoordinator.enqueueJob(elanName + ":" + l2GatewayDevice.getDeviceName(), () -> {
- final SettableFuture settableFuture = SettableFuture.create();
- Futures.addCallback(broker.newReadOnlyTransaction().read(LogicalDatastoreType.OPERATIONAL,
- nodeIid), new SettableFutureCallback<Optional<Node>>(settableFuture) {
- @Override
- public void onSuccess(@Nonnull Optional<Node> resultNode) {
+ FluentFuture<Optional<Node>> fluentFuture = broker.newReadOnlyTransaction().read(
+ LogicalDatastoreType.OPERATIONAL, nodeIid);
+ Futures.addCallback(fluentFuture, new FutureCallback<Optional<Node>>() {
+ @Override
+ public void onSuccess(Optional<Node> nodeOptional) {
+ if (nodeOptional.isPresent()) {
+ Node node = nodeOptional.get();
+ if (node.augmentation(HwvtepGlobalAugmentation.class) != null) {
+ List<LocalUcastMacs> localUcastMacs =
+ node.augmentation(HwvtepGlobalAugmentation.class).getLocalUcastMacs();
+ if (localUcastMacs == null) {
+ return;
+ }
LocalUcastMacListener localUcastMacListener =
new LocalUcastMacListener(broker, haOpClusteredListener,
- elanL2GatewayUtils, jobCoordinator, elanInstanceCache);
- settableFuture.set(resultNode);
- Optional<Node> nodeOptional = resultNode;
- if (nodeOptional.isPresent()) {
- Node node = nodeOptional.get();
- if (node.getAugmentation(HwvtepGlobalAugmentation.class) != null) {
- List<LocalUcastMacs> localUcastMacs =
- node.getAugmentation(HwvtepGlobalAugmentation.class).getLocalUcastMacs();
- if (localUcastMacs == null) {
- return;
- }
- localUcastMacs.stream()
- .filter((mac) -> macBelongsToLogicalSwitch(mac, elanName))
- .forEach((mac) -> {
- InstanceIdentifier<LocalUcastMacs> macIid = getMacIid(nodeIid, mac);
- localUcastMacListener.added(macIid, mac);
- });
- }
- }
+ elanL2GatewayUtils, jobCoordinator, elanInstanceCache, hwvtepNodeHACache,
+ l2GatewayServiceRecoveryHandler, serviceRecoveryRegistry);
+ localUcastMacs.stream()
+ .filter((mac) -> macBelongsToLogicalSwitch(mac, elanName))
+ .forEach((mac) -> {
+ InstanceIdentifier<LocalUcastMacs> macIid = getMacIid(nodeIid, mac);
+ localUcastMacListener.added(macIid, mac);
+ });
}
- }, MoreExecutors.directExecutor());
- return Lists.newArrayList(settableFuture);
+ }
+ }
+
+ @Override
+ public void onFailure(Throwable throwable) {
+ }
+ }, MoreExecutors.directExecutor());
+ return Lists.newArrayList(fluentFuture);
} , 5);
}
List<L2gatewayConnection> l2GwConnections = new ArrayList<>();
List<L2gatewayConnection> allL2GwConns = getAllL2gatewayConnections(broker);
for (L2gatewayConnection l2GwConn : allL2GwConns) {
- if (l2GwConn.getL2gatewayId().equals(l2GatewayId)) {
+ if (Objects.equals(l2GwConn.getL2gatewayId(), l2GatewayId)) {
l2GwConnections.add(l2GwConn);
}
}
return l2GwConnections;
}
+ @SuppressFBWarnings(value = "UPM_UNCALLED_PRIVATE_METHOD",
+ justification = "https://github.com/spotbugs/spotbugs/issues/811")
private static boolean macBelongsToLogicalSwitch(LocalUcastMacs mac, String logicalSwitchName) {
InstanceIdentifier<LogicalSwitches> iid = (InstanceIdentifier<LogicalSwitches>)
mac.getLogicalSwitchRef().getValue();
}
static InstanceIdentifier<LocalUcastMacs> getMacIid(InstanceIdentifier<Node> nodeIid, LocalUcastMacs mac) {
- return nodeIid.augmentation(HwvtepGlobalAugmentation.class).child(LocalUcastMacs.class, mac.getKey());
+ return nodeIid.augmentation(HwvtepGlobalAugmentation.class).child(LocalUcastMacs.class, mac.key());
}
}