2 * Copyright © 2017 Ericsson India Global Services Pvt Ltd. and others. All rights reserved.
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
8 package org.opendaylight.netvirt.elan.l2gw.listeners;
10 import com.google.common.collect.Sets;
11 import java.util.Collection;
12 import java.util.Collections;
13 import java.util.HashMap;
14 import java.util.HashSet;
15 import java.util.Locale;
18 import java.util.function.Predicate;
19 import javax.annotation.Nullable;
20 import javax.annotation.PostConstruct;
21 import javax.inject.Inject;
22 import javax.inject.Singleton;
23 import org.opendaylight.controller.md.sal.binding.api.ClusteredDataTreeChangeListener;
24 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
25 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
26 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
27 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
28 import org.opendaylight.genius.infra.ManagedNewTransactionRunner;
29 import org.opendaylight.genius.infra.ManagedNewTransactionRunnerImpl;
30 import org.opendaylight.genius.utils.batching.ResourceBatchingManager;
31 import org.opendaylight.genius.utils.hwvtep.HwvtepHACache;
32 import org.opendaylight.genius.utils.hwvtep.HwvtepSouthboundUtils;
33 import org.opendaylight.infrautils.jobcoordinator.JobCoordinator;
34 import org.opendaylight.infrautils.utils.concurrent.ListenableFutures;
35 import org.opendaylight.netvirt.elan.cache.ElanInstanceCache;
36 import org.opendaylight.netvirt.elan.l2gw.ha.HwvtepHAUtil;
37 import org.opendaylight.netvirt.elan.l2gw.ha.listeners.HAOpClusteredListener;
38 import org.opendaylight.netvirt.elan.l2gw.utils.ElanL2GatewayUtils;
39 import org.opendaylight.netvirt.elanmanager.utils.ElanL2GwCacheUtils;
40 import org.opendaylight.netvirt.neutronvpn.api.l2gw.L2GatewayDevice;
41 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.MacAddress;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.instances.ElanInstance;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalAugmentation;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LocalUcastMacs;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LogicalSwitches;
46 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
47 import org.opendaylight.yangtools.yang.binding.DataObject;
48 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
49 import org.slf4j.Logger;
50 import org.slf4j.LoggerFactory;
53 public class LocalUcastMacListener extends ChildListener<Node, LocalUcastMacs, String>
54 implements ClusteredDataTreeChangeListener<Node> {
56 private static final Logger LOG = LoggerFactory.getLogger(LocalUcastMacListener.class);
57 public static final String NODE_CHECK = "physical";
59 private static final Predicate<InstanceIdentifier<Node>> IS_PS_NODE_IID =
60 (iid) -> iid.firstKeyOf(Node.class).getNodeId().getValue().contains(NODE_CHECK);
62 private static final Predicate<InstanceIdentifier<Node>> IS_NOT_HA_CHILD =
63 (iid) -> !HwvtepHACache.getInstance().isHAEnabledDevice(iid)
64 && !iid.firstKeyOf(Node.class).getNodeId().getValue().contains(HwvtepHAUtil.PHYSICALSWITCH);
66 private static final Predicate<InstanceIdentifier<Node>> IS_HA_CHILD =
67 (iid) -> HwvtepHACache.getInstance().isHAEnabledDevice(iid);
69 private final ManagedNewTransactionRunner txRunner;
70 private final ElanL2GatewayUtils elanL2GatewayUtils;
71 private final HAOpClusteredListener haOpClusteredListener;
72 private final JobCoordinator jobCoordinator;
73 private final ElanInstanceCache elanInstanceCache;
76 public LocalUcastMacListener(final DataBroker dataBroker,
77 final HAOpClusteredListener haOpClusteredListener,
78 final ElanL2GatewayUtils elanL2GatewayUtils,
79 final JobCoordinator jobCoordinator,
80 final ElanInstanceCache elanInstanceCache) {
81 super(dataBroker, false);
82 this.txRunner = new ManagedNewTransactionRunnerImpl(dataBroker);
83 this.elanL2GatewayUtils = elanL2GatewayUtils;
84 this.haOpClusteredListener = haOpClusteredListener;
85 this.jobCoordinator = jobCoordinator;
86 this.elanInstanceCache = elanInstanceCache;
91 public void init() throws Exception {
92 ResourceBatchingManager.getInstance().registerDefaultBatchHandlers(this.dataBroker);
97 protected boolean proceed(final InstanceIdentifier<Node> parent) {
98 return IS_NOT_HA_CHILD.test(parent);
101 protected String getElanName(final LocalUcastMacs mac) {
102 return mac.getLogicalSwitchRef().getValue().firstKeyOf(LogicalSwitches.class).getHwvtepNodeName().getValue();
106 protected String getGroup(final LocalUcastMacs localUcastMacs) {
107 return getElanName(localUcastMacs);
111 protected void onUpdate(final Map<String, Map<InstanceIdentifier, LocalUcastMacs>> updatedMacsGrouped,
112 final Map<String, Map<InstanceIdentifier, LocalUcastMacs>> deletedMacsGrouped) {
113 updatedMacsGrouped.forEach((key, value) -> value.forEach(this::added));
114 deletedMacsGrouped.forEach((key, value) -> value.forEach(this::removed));
117 public void removed(final InstanceIdentifier<LocalUcastMacs> identifier, final LocalUcastMacs macRemoved) {
118 String hwvtepNodeId = identifier.firstKeyOf(Node.class).getNodeId().getValue();
119 String macAddress = macRemoved.getMacEntryKey().getValue().toLowerCase(Locale.getDefault());
121 LOG.trace("LocalUcastMacs {} removed from {}", macAddress, hwvtepNodeId);
123 ResourceBatchingManager.getInstance().delete(ResourceBatchingManager.ShardResource.CONFIG_TOPOLOGY,
126 String elanName = getElanName(macRemoved);
128 jobCoordinator.enqueueJob(elanName + HwvtepHAUtil.L2GW_JOB_KEY ,
130 L2GatewayDevice elanL2GwDevice = ElanL2GwCacheUtils.getL2GatewayDeviceFromCache(elanName,
132 if (elanL2GwDevice == null) {
133 LOG.warn("Could not find L2GatewayDevice for ELAN: {}, nodeID:{} from cache",
134 elanName, hwvtepNodeId);
138 elanL2GwDevice.removeUcastLocalMac(macRemoved);
139 ElanInstance elanInstance = elanInstanceCache.get(elanName).orNull();
140 elanL2GatewayUtils.unInstallL2GwUcastMacFromL2gwDevices(elanName, elanL2GwDevice,
141 Collections.singletonList(new MacAddress(macAddress.toLowerCase(Locale.getDefault()))));
142 elanL2GatewayUtils.unInstallL2GwUcastMacFromElanDpns(elanInstance, elanL2GwDevice,
143 Collections.singletonList(new MacAddress(macAddress.toLowerCase(Locale.getDefault()))));
148 public void added(final InstanceIdentifier<LocalUcastMacs> identifier, final LocalUcastMacs macAdded) {
149 ResourceBatchingManager.getInstance().put(ResourceBatchingManager.ShardResource.CONFIG_TOPOLOGY,
150 identifier, macAdded);
152 String hwvtepNodeId = identifier.firstKeyOf(Node.class).getNodeId().getValue();
153 String macAddress = macAdded.getMacEntryKey().getValue().toLowerCase(Locale.getDefault());
154 String elanName = getElanName(macAdded);
156 LOG.trace("LocalUcastMacs {} added to {}", macAddress, hwvtepNodeId);
158 ElanInstance elan = elanInstanceCache.get(elanName).orNull();
160 LOG.warn("Could not find ELAN for mac {} being added", macAddress);
163 jobCoordinator.enqueueJob(elanName + HwvtepHAUtil.L2GW_JOB_KEY,
165 L2GatewayDevice elanL2GwDevice =
166 ElanL2GwCacheUtils.getL2GatewayDeviceFromCache(elanName, hwvtepNodeId);
167 if (elanL2GwDevice == null) {
168 LOG.warn("Could not find L2GatewayDevice for ELAN: {}, nodeID:{} from cache",
169 elanName, hwvtepNodeId);
173 elanL2GwDevice.addUcastLocalMac(macAdded);
174 elanL2GatewayUtils.installL2GwUcastMacInElan(elan, elanL2GwDevice,
175 macAddress.toLowerCase(), macAdded, null);
181 protected Map<InstanceIdentifier<LocalUcastMacs>, DataObjectModification<LocalUcastMacs>> getChildMod(
182 final InstanceIdentifier<Node> parentIid,
183 final DataObjectModification<Node> mod) {
185 Map<InstanceIdentifier<LocalUcastMacs>, DataObjectModification<LocalUcastMacs>> result = new HashMap<>();
186 DataObjectModification<HwvtepGlobalAugmentation> aug = mod.getModifiedAugmentation(
187 HwvtepGlobalAugmentation.class);
188 if (aug != null && getModificationType(aug) != null) {
189 Collection<DataObjectModification<? extends DataObject>> children = aug.getModifiedChildren();
191 .filter(childMod -> getModificationType(childMod) != null)
192 .filter(childMod -> childMod.getDataType() == LocalUcastMacs.class)
193 .forEach(childMod -> {
194 LocalUcastMacs afterMac = (LocalUcastMacs) childMod.getDataAfter();
195 LocalUcastMacs mac = afterMac != null ? afterMac : (LocalUcastMacs)childMod.getDataBefore();
196 InstanceIdentifier<LocalUcastMacs> iid = parentIid
197 .augmentation(HwvtepGlobalAugmentation.class)
198 .child(LocalUcastMacs.class, mac.getKey());
199 result.put(iid, (DataObjectModification<LocalUcastMacs>) childMod);
206 protected void onParentAdded(final DataTreeModification<Node> modification) {
207 InstanceIdentifier<Node> nodeIid = modification.getRootPath().getRootIdentifier();
208 if (IS_PS_NODE_IID.test(nodeIid)) {
211 ListenableFutures.addErrorLogging(txRunner.callWithNewReadWriteTransactionAndSubmit(tx -> {
212 haOpClusteredListener.onGlobalNodeAdd(nodeIid, modification.getRootNode().getDataAfter(), tx);
213 if (!IS_HA_CHILD.test(nodeIid)) {
214 LOG.trace("On parent add {}", nodeIid);
215 Node operNode = modification.getRootNode().getDataAfter();
216 Set<LocalUcastMacs> configMacs =
217 getMacs(tx.read(LogicalDatastoreType.CONFIGURATION, nodeIid).checkedGet().orNull());
218 Set<LocalUcastMacs> operMacs = getMacs(operNode);
219 Set<LocalUcastMacs> staleMacs = Sets.difference(configMacs, operMacs);
220 staleMacs.forEach(staleMac -> removed(getMacIid(nodeIid, staleMac), staleMac));
222 }), LOG, "Error processing added parent");
225 InstanceIdentifier<LocalUcastMacs> getMacIid(InstanceIdentifier<Node> nodeIid, LocalUcastMacs mac) {
226 return nodeIid.augmentation(HwvtepGlobalAugmentation.class)
227 .child(LocalUcastMacs.class, mac.getKey());
230 private Set<LocalUcastMacs> getMacs(@Nullable Node node) {
232 HwvtepGlobalAugmentation augmentation = node.getAugmentation(HwvtepGlobalAugmentation.class);
233 if (augmentation != null && augmentation.getLocalUcastMacs() != null) {
234 return new HashSet<>(augmentation.getLocalUcastMacs());
237 return Collections.emptySet();
241 protected void onParentRemoved(InstanceIdentifier<Node> parent) {
242 if (IS_PS_NODE_IID.test(parent)) {
245 LOG.trace("on parent removed {}", parent);
249 protected InstanceIdentifier<Node> getParentWildCardPath() {
250 return HwvtepSouthboundUtils.createHwvtepTopologyInstanceIdentifier()