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 static org.opendaylight.genius.infra.Datastore.CONFIGURATION;
11 import static org.opendaylight.genius.infra.Datastore.OPERATIONAL;
13 import com.google.common.collect.Sets;
14 import java.util.Collections;
15 import java.util.HashMap;
16 import java.util.HashSet;
19 import java.util.function.Predicate;
20 import javax.annotation.PostConstruct;
21 import javax.inject.Inject;
22 import javax.inject.Singleton;
23 import org.eclipse.jdt.annotation.Nullable;
24 import org.opendaylight.controller.md.sal.binding.api.ClusteredDataTreeChangeListener;
25 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
26 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
27 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
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.HwvtepNodeHACache;
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.IetfYangUtil;
42 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.MacAddress;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.instances.ElanInstance;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalAugmentation;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LocalUcastMacs;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LogicalSwitches;
47 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
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 final ManagedNewTransactionRunner txRunner;
63 private final ElanL2GatewayUtils elanL2GatewayUtils;
64 private final HAOpClusteredListener haOpClusteredListener;
65 private final JobCoordinator jobCoordinator;
66 private final ElanInstanceCache elanInstanceCache;
67 private final HwvtepNodeHACache hwvtepNodeHACache;
70 public LocalUcastMacListener(final DataBroker dataBroker,
71 final HAOpClusteredListener haOpClusteredListener,
72 final ElanL2GatewayUtils elanL2GatewayUtils,
73 final JobCoordinator jobCoordinator,
74 final ElanInstanceCache elanInstanceCache,
75 final HwvtepNodeHACache hwvtepNodeHACache) {
76 super(dataBroker, false);
77 this.txRunner = new ManagedNewTransactionRunnerImpl(dataBroker);
78 this.elanL2GatewayUtils = elanL2GatewayUtils;
79 this.haOpClusteredListener = haOpClusteredListener;
80 this.jobCoordinator = jobCoordinator;
81 this.elanInstanceCache = elanInstanceCache;
82 this.hwvtepNodeHACache = hwvtepNodeHACache;
87 public void init() throws Exception {
88 ResourceBatchingManager.getInstance().registerDefaultBatchHandlers(this.dataBroker);
93 protected boolean proceed(final InstanceIdentifier<Node> parent) {
94 return isNotHAChild(parent);
97 protected String getElanName(final LocalUcastMacs mac) {
98 return mac.getLogicalSwitchRef().getValue().firstKeyOf(LogicalSwitches.class).getHwvtepNodeName().getValue();
102 protected String getGroup(final LocalUcastMacs localUcastMacs) {
103 return getElanName(localUcastMacs);
107 protected void onUpdate(final Map<String, Map<InstanceIdentifier, LocalUcastMacs>> updatedMacsGrouped,
108 final Map<String, Map<InstanceIdentifier, LocalUcastMacs>> deletedMacsGrouped) {
109 updatedMacsGrouped.forEach((key, value) -> value.forEach(this::added));
110 deletedMacsGrouped.forEach((key, value) -> value.forEach(this::removed));
113 public void removed(final InstanceIdentifier<LocalUcastMacs> identifier, final LocalUcastMacs macRemoved) {
114 String hwvtepNodeId = identifier.firstKeyOf(Node.class).getNodeId().getValue();
115 MacAddress macAddress = IetfYangUtil.INSTANCE.canonizeMacAddress(macRemoved.getMacEntryKey());
117 LOG.trace("LocalUcastMacs {} removed from {}", macAddress.getValue(), hwvtepNodeId);
119 ResourceBatchingManager.getInstance().delete(ResourceBatchingManager.ShardResource.CONFIG_TOPOLOGY,
122 String elanName = getElanName(macRemoved);
124 jobCoordinator.enqueueJob(elanName + HwvtepHAUtil.L2GW_JOB_KEY ,
126 L2GatewayDevice elanL2GwDevice = ElanL2GwCacheUtils.getL2GatewayDeviceFromCache(elanName,
128 if (elanL2GwDevice == null) {
129 LOG.warn("Could not find L2GatewayDevice for ELAN: {}, nodeID:{} from cache",
130 elanName, hwvtepNodeId);
134 elanL2GwDevice.removeUcastLocalMac(macRemoved);
135 ElanInstance elanInstance = elanInstanceCache.get(elanName).orNull();
136 elanL2GatewayUtils.unInstallL2GwUcastMacFromL2gwDevices(elanName, elanL2GwDevice,
137 Collections.singletonList(macAddress));
138 elanL2GatewayUtils.unInstallL2GwUcastMacFromElanDpns(elanInstance, elanL2GwDevice,
139 Collections.singletonList(macAddress));
144 public void added(final InstanceIdentifier<LocalUcastMacs> identifier, final LocalUcastMacs macAdded) {
145 ResourceBatchingManager.getInstance().put(ResourceBatchingManager.ShardResource.CONFIG_TOPOLOGY,
146 identifier, macAdded);
148 String hwvtepNodeId = identifier.firstKeyOf(Node.class).getNodeId().getValue();
149 String macAddress = IetfYangUtil.INSTANCE.canonizeMacAddress(macAdded.getMacEntryKey()).getValue();
150 String elanName = getElanName(macAdded);
152 LOG.trace("LocalUcastMacs {} added to {}", macAddress, hwvtepNodeId);
154 ElanInstance elan = elanInstanceCache.get(elanName).orNull();
156 LOG.warn("Could not find ELAN for mac {} being added", macAddress);
159 jobCoordinator.enqueueJob(elanName + HwvtepHAUtil.L2GW_JOB_KEY,
161 L2GatewayDevice elanL2GwDevice =
162 ElanL2GwCacheUtils.getL2GatewayDeviceFromCache(elanName, hwvtepNodeId);
163 if (elanL2GwDevice == null) {
164 LOG.warn("Could not find L2GatewayDevice for ELAN: {}, nodeID:{} from cache",
165 elanName, hwvtepNodeId);
169 elanL2GwDevice.addUcastLocalMac(macAdded);
170 elanL2GatewayUtils.installL2GwUcastMacInElan(elan, elanL2GwDevice, macAddress, macAdded, null);
176 protected Map<InstanceIdentifier<LocalUcastMacs>, DataObjectModification<LocalUcastMacs>> getChildMod(
177 final InstanceIdentifier<Node> parentIid,
178 final DataObjectModification<Node> mod) {
180 Map<InstanceIdentifier<LocalUcastMacs>, DataObjectModification<LocalUcastMacs>> result = new HashMap<>();
181 DataObjectModification<HwvtepGlobalAugmentation> aug = mod.getModifiedAugmentation(
182 HwvtepGlobalAugmentation.class);
183 if (aug != null && getModificationType(aug) != null) {
184 aug.getModifiedChildren().stream()
185 .filter(childMod -> getModificationType(childMod) != null)
186 .filter(childMod -> childMod.getDataType() == LocalUcastMacs.class)
187 .forEach(childMod -> {
188 LocalUcastMacs afterMac = (LocalUcastMacs) childMod.getDataAfter();
189 LocalUcastMacs mac = afterMac != null ? afterMac : (LocalUcastMacs)childMod.getDataBefore();
190 InstanceIdentifier<LocalUcastMacs> iid = parentIid
191 .augmentation(HwvtepGlobalAugmentation.class)
192 .child(LocalUcastMacs.class, mac.key());
193 result.put(iid, (DataObjectModification<LocalUcastMacs>) childMod);
200 protected void onParentAdded(final DataTreeModification<Node> modification) {
201 InstanceIdentifier<Node> nodeIid = modification.getRootPath().getRootIdentifier();
202 if (IS_PS_NODE_IID.test(nodeIid)) {
205 // TODO skitt we're only using read transactions here
206 ListenableFutures.addErrorLogging(txRunner.callWithNewReadWriteTransactionAndSubmit(OPERATIONAL,
207 tx -> haOpClusteredListener.onGlobalNodeAdd(nodeIid, modification.getRootNode().getDataAfter(), tx)), LOG,
208 "Error processing added parent");
209 if (!isHAChild(nodeIid)) {
210 ListenableFutures.addErrorLogging(txRunner.callWithNewReadWriteTransactionAndSubmit(CONFIGURATION, tx -> {
211 LOG.trace("On parent add {}", nodeIid);
212 Node operNode = modification.getRootNode().getDataAfter();
213 Set<LocalUcastMacs> configMacs = getMacs(tx.read(nodeIid).get().orNull());
214 Set<LocalUcastMacs> operMacs = getMacs(operNode);
215 Set<LocalUcastMacs> staleMacs = Sets.difference(configMacs, operMacs);
216 staleMacs.forEach(staleMac -> removed(getMacIid(nodeIid, staleMac), staleMac));
217 }), LOG, "Error processing added parent");
221 InstanceIdentifier<LocalUcastMacs> getMacIid(InstanceIdentifier<Node> nodeIid, LocalUcastMacs mac) {
222 return nodeIid.augmentation(HwvtepGlobalAugmentation.class)
223 .child(LocalUcastMacs.class, mac.key());
226 private static Set<LocalUcastMacs> getMacs(@Nullable Node node) {
228 HwvtepGlobalAugmentation augmentation = node.augmentation(HwvtepGlobalAugmentation.class);
229 if (augmentation != null && augmentation.getLocalUcastMacs() != null) {
230 return new HashSet<>(augmentation.getLocalUcastMacs());
233 return Collections.emptySet();
237 protected void onParentRemoved(InstanceIdentifier<Node> parent) {
238 if (IS_PS_NODE_IID.test(parent)) {
241 LOG.trace("on parent removed {}", parent);
245 protected InstanceIdentifier<Node> getParentWildCardPath() {
246 return HwvtepSouthboundUtils.createHwvtepTopologyInstanceIdentifier()
250 private boolean isNotHAChild(InstanceIdentifier<Node> nodeId) {
251 return !hwvtepNodeHACache.isHAEnabledDevice(nodeId)
252 && !nodeId.firstKeyOf(Node.class).getNodeId().getValue().contains(HwvtepHAUtil.PHYSICALSWITCH);
255 private boolean isHAChild(InstanceIdentifier<Node> nodeId) {
256 return hwvtepNodeHACache.isHAEnabledDevice(nodeId);