*/
package org.opendaylight.netvirt.elan.l2gw.listeners;
-import com.google.common.base.Optional;
import com.google.common.collect.Sets;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import java.util.Set;
import java.util.function.Predicate;
+import javax.annotation.Nullable;
import javax.annotation.PostConstruct;
import javax.inject.Inject;
import javax.inject.Singleton;
import org.opendaylight.controller.md.sal.binding.api.DataBroker;
import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
-import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
-import org.opendaylight.genius.mdsalutil.MDSALUtil;
+import org.opendaylight.genius.infra.ManagedNewTransactionRunner;
+import org.opendaylight.genius.infra.ManagedNewTransactionRunnerImpl;
import org.opendaylight.genius.utils.batching.ResourceBatchingManager;
import org.opendaylight.genius.utils.hwvtep.HwvtepHACache;
import org.opendaylight.genius.utils.hwvtep.HwvtepSouthboundUtils;
import org.opendaylight.infrautils.jobcoordinator.JobCoordinator;
+import org.opendaylight.infrautils.utils.concurrent.ListenableFutures;
+import org.opendaylight.netvirt.elan.cache.ElanInstanceCache;
import org.opendaylight.netvirt.elan.l2gw.ha.HwvtepHAUtil;
import org.opendaylight.netvirt.elan.l2gw.ha.listeners.HAOpClusteredListener;
import org.opendaylight.netvirt.elan.l2gw.utils.ElanL2GatewayUtils;
-import org.opendaylight.netvirt.elan.utils.ElanClusterUtils;
-import org.opendaylight.netvirt.elan.utils.ElanUtils;
import org.opendaylight.netvirt.elanmanager.utils.ElanL2GwCacheUtils;
import org.opendaylight.netvirt.neutronvpn.api.l2gw.L2GatewayDevice;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.MacAddress;
public static final String NODE_CHECK = "physical";
- private static final Predicate<InstanceIdentifier<Node>> IS_PS_NODE_IID = (iid) -> {
- return iid.firstKeyOf(Node.class).getNodeId().getValue().contains(NODE_CHECK);
- };
+ private static final Predicate<InstanceIdentifier<Node>> IS_PS_NODE_IID =
+ (iid) -> iid.firstKeyOf(Node.class).getNodeId().getValue().contains(NODE_CHECK);
- private static final Predicate<InstanceIdentifier<Node>> IS_NOT_HA_CHILD = (iid) -> {
- return !HwvtepHACache.getInstance().isHAEnabledDevice(iid)
+ private static final Predicate<InstanceIdentifier<Node>> IS_NOT_HA_CHILD =
+ (iid) -> !HwvtepHACache.getInstance().isHAEnabledDevice(iid)
&& !iid.firstKeyOf(Node.class).getNodeId().getValue().contains(HwvtepHAUtil.PHYSICALSWITCH);
- };
- private static final Predicate<InstanceIdentifier<Node>> IS_HA_CHILD = (iid) -> {
- return HwvtepHACache.getInstance().isHAEnabledDevice(iid);
- };
+ private static final Predicate<InstanceIdentifier<Node>> IS_HA_CHILD =
+ (iid) -> HwvtepHACache.getInstance().isHAEnabledDevice(iid);
+ private final ManagedNewTransactionRunner txRunner;
private final ElanL2GatewayUtils elanL2GatewayUtils;
private final HAOpClusteredListener haOpClusteredListener;
private final JobCoordinator jobCoordinator;
- private final ElanClusterUtils elanClusterUtils;
+ private final ElanInstanceCache elanInstanceCache;
@Inject
public LocalUcastMacListener(final DataBroker dataBroker,
final HAOpClusteredListener haOpClusteredListener,
final ElanL2GatewayUtils elanL2GatewayUtils,
- final ElanClusterUtils elanClusterUtils,
- final JobCoordinator jobCoordinator) {
+ final JobCoordinator jobCoordinator,
+ final ElanInstanceCache elanInstanceCache) {
super(dataBroker, false);
+ this.txRunner = new ManagedNewTransactionRunnerImpl(dataBroker);
this.elanL2GatewayUtils = elanL2GatewayUtils;
this.haOpClusteredListener = haOpClusteredListener;
- this.elanClusterUtils = elanClusterUtils;
this.jobCoordinator = jobCoordinator;
+ this.elanInstanceCache = elanInstanceCache;
}
@Override
}
protected String getElanName(final LocalUcastMacs mac) {
- return ((InstanceIdentifier<LogicalSwitches>) mac.getLogicalSwitchRef().getValue())
- .firstKeyOf(LogicalSwitches.class).getHwvtepNodeName().getValue();
+ return mac.getLogicalSwitchRef().getValue().firstKeyOf(LogicalSwitches.class).getHwvtepNodeName().getValue();
}
@Override
- protected String getGroup(final InstanceIdentifier<LocalUcastMacs> childIid,
- final LocalUcastMacs localUcastMacs) {
+ protected String getGroup(final LocalUcastMacs localUcastMacs) {
return getElanName(localUcastMacs);
}
@Override
protected void onUpdate(final Map<String, Map<InstanceIdentifier, LocalUcastMacs>> updatedMacsGrouped,
final Map<String, Map<InstanceIdentifier, LocalUcastMacs>> deletedMacsGrouped) {
- updatedMacsGrouped.entrySet().forEach((entry) -> {
- entry.getValue().entrySet().forEach((entry2) -> {
- added(entry2.getKey(), entry2.getValue());
- });
- });
- deletedMacsGrouped.entrySet().forEach((entry) -> {
- entry.getValue().entrySet().forEach((entry2) -> {
- removed(entry2.getKey(), entry2.getValue());
- });
- });
+ updatedMacsGrouped.forEach((key, value) -> value.forEach(this::added));
+ deletedMacsGrouped.forEach((key, value) -> value.forEach(this::removed));
}
public void removed(final InstanceIdentifier<LocalUcastMacs> identifier, final LocalUcastMacs macRemoved) {
}
elanL2GwDevice.removeUcastLocalMac(macRemoved);
- ElanInstance elanInstance = ElanUtils.getElanInstanceByName(dataBroker, elanName);
-
- elanClusterUtils.runOnlyInOwnerNode(elanName + ":" + macAddress,
- "Delete l2gw local ucast mac", () -> {
- return elanL2GatewayUtils.unInstallL2GwUcastMacFromElan(elanInstance, elanL2GwDevice,
- Collections.singletonList(new MacAddress(macAddress.toLowerCase()))); });
+ ElanInstance elanInstance = elanInstanceCache.get(elanName).orNull();
+ elanL2GatewayUtils.unInstallL2GwUcastMacFromL2gwDevices(elanName, elanL2GwDevice,
+ Collections.singletonList(new MacAddress(macAddress.toLowerCase(Locale.getDefault()))));
+ elanL2GatewayUtils.unInstallL2GwUcastMacFromElanDpns(elanInstance, elanL2GwDevice,
+ Collections.singletonList(new MacAddress(macAddress.toLowerCase(Locale.getDefault()))));
return null;
});
}
LOG.trace("LocalUcastMacs {} added to {}", macAddress, hwvtepNodeId);
- ElanInstance elan = ElanUtils.getElanInstanceByName(dataBroker, elanName);
+ ElanInstance elan = elanInstanceCache.get(elanName).orNull();
if (elan == null) {
LOG.warn("Could not find ELAN for mac {} being added", macAddress);
return;
if (IS_PS_NODE_IID.test(nodeIid)) {
return;
}
- ReadWriteTransaction tx = dataBroker.newReadWriteTransaction();
- haOpClusteredListener.onGlobalNodeAdd(nodeIid, modification.getRootNode().getDataAfter(), tx);
- tx.submit();
- if (IS_HA_CHILD.test(nodeIid)) {
- return;
- }
-
- LOG.trace("On parent add {}", nodeIid);
- Node operNode = modification.getRootNode().getDataAfter();
- Optional<Node> configNode = MDSALUtil.read(dataBroker, LogicalDatastoreType.CONFIGURATION, nodeIid);
- Set<LocalUcastMacs> configMacs = getMacs(configNode);
- Set<LocalUcastMacs> operMacs = getMacs(Optional.of(operNode));
- Set<LocalUcastMacs> staleMacs = Sets.difference(configMacs, operMacs);
- staleMacs.forEach(staleMac -> removed(getMacIid(nodeIid, staleMac), staleMac));
+ ListenableFutures.addErrorLogging(txRunner.callWithNewReadWriteTransactionAndSubmit(tx -> {
+ haOpClusteredListener.onGlobalNodeAdd(nodeIid, modification.getRootNode().getDataAfter(), tx);
+ if (!IS_HA_CHILD.test(nodeIid)) {
+ LOG.trace("On parent add {}", nodeIid);
+ Node operNode = modification.getRootNode().getDataAfter();
+ Set<LocalUcastMacs> configMacs =
+ getMacs(tx.read(LogicalDatastoreType.CONFIGURATION, nodeIid).checkedGet().orNull());
+ Set<LocalUcastMacs> operMacs = getMacs(operNode);
+ Set<LocalUcastMacs> staleMacs = Sets.difference(configMacs, operMacs);
+ staleMacs.forEach(staleMac -> removed(getMacIid(nodeIid, staleMac), staleMac));
+ }
+ }), LOG, "Error processing added parent");
}
InstanceIdentifier<LocalUcastMacs> getMacIid(InstanceIdentifier<Node> nodeIid, LocalUcastMacs mac) {
.child(LocalUcastMacs.class, mac.getKey());
}
- Set<LocalUcastMacs> getMacs(Optional<Node> node) {
- if (node.isPresent()) {
- HwvtepGlobalAugmentation augmentation = node.get().getAugmentation(HwvtepGlobalAugmentation.class);
+ private Set<LocalUcastMacs> getMacs(@Nullable Node node) {
+ if (node != null) {
+ HwvtepGlobalAugmentation augmentation = node.getAugmentation(HwvtepGlobalAugmentation.class);
if (augmentation != null && augmentation.getLocalUcastMacs() != null) {
return new HashSet<>(augmentation.getLocalUcastMacs());
}