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.base.Optional;
11 import com.google.common.collect.Sets;
12 import java.util.Collection;
13 import java.util.Collections;
14 import java.util.HashMap;
15 import java.util.HashSet;
16 import java.util.Locale;
19 import java.util.function.Predicate;
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.binding.api.ReadWriteTransaction;
28 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
29 import org.opendaylight.genius.mdsalutil.MDSALUtil;
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.netvirt.elan.l2gw.ha.HwvtepHAUtil;
35 import org.opendaylight.netvirt.elan.l2gw.ha.listeners.HAOpClusteredListener;
36 import org.opendaylight.netvirt.elan.l2gw.utils.ElanL2GatewayUtils;
37 import org.opendaylight.netvirt.elan.utils.ElanClusterUtils;
38 import org.opendaylight.netvirt.elan.utils.ElanUtils;
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;
51 public class LocalUcastMacListener extends ChildListener<Node, LocalUcastMacs, String>
52 implements ClusteredDataTreeChangeListener<Node> {
54 public static final String NODE_CHECK = "physical";
56 private static final Predicate<InstanceIdentifier<Node>> IS_PS_NODE_IID = (iid) -> {
57 return iid.firstKeyOf(Node.class).getNodeId().getValue().contains(NODE_CHECK);
60 private static final Predicate<InstanceIdentifier<Node>> IS_NOT_HA_CHILD = (iid) -> {
61 return !HwvtepHACache.getInstance().isHAEnabledDevice(iid)
62 && !iid.firstKeyOf(Node.class).getNodeId().getValue().contains(HwvtepHAUtil.PHYSICALSWITCH);
65 private static final Predicate<InstanceIdentifier<Node>> IS_HA_CHILD = (iid) -> {
66 return HwvtepHACache.getInstance().isHAEnabledDevice(iid);
69 private final ElanL2GatewayUtils elanL2GatewayUtils;
70 private final HAOpClusteredListener haOpClusteredListener;
71 private final JobCoordinator jobCoordinator;
72 private final ElanClusterUtils elanClusterUtils;
75 public LocalUcastMacListener(final DataBroker dataBroker,
76 final HAOpClusteredListener haOpClusteredListener,
77 final ElanL2GatewayUtils elanL2GatewayUtils,
78 final ElanClusterUtils elanClusterUtils,
79 final JobCoordinator jobCoordinator) {
80 super(dataBroker, false);
81 this.elanL2GatewayUtils = elanL2GatewayUtils;
82 this.haOpClusteredListener = haOpClusteredListener;
83 this.elanClusterUtils = elanClusterUtils;
84 this.jobCoordinator = jobCoordinator;
89 public void init() throws Exception {
90 ResourceBatchingManager.getInstance().registerDefaultBatchHandlers(this.dataBroker);
95 protected boolean proceed(final InstanceIdentifier<Node> parent) {
96 return IS_NOT_HA_CHILD.test(parent);
99 protected String getElanName(final LocalUcastMacs mac) {
100 return ((InstanceIdentifier<LogicalSwitches>) mac.getLogicalSwitchRef().getValue())
101 .firstKeyOf(LogicalSwitches.class).getHwvtepNodeName().getValue();
105 protected String getGroup(final InstanceIdentifier<LocalUcastMacs> childIid,
106 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.entrySet().forEach((entry) -> {
114 entry.getValue().entrySet().forEach((entry2) -> {
115 added(entry2.getKey(), entry2.getValue());
118 deletedMacsGrouped.entrySet().forEach((entry) -> {
119 entry.getValue().entrySet().forEach((entry2) -> {
120 removed(entry2.getKey(), entry2.getValue());
125 public void removed(final InstanceIdentifier<LocalUcastMacs> identifier, final LocalUcastMacs macRemoved) {
126 String hwvtepNodeId = identifier.firstKeyOf(Node.class).getNodeId().getValue();
127 String macAddress = macRemoved.getMacEntryKey().getValue().toLowerCase(Locale.getDefault());
129 LOG.trace("LocalUcastMacs {} removed from {}", macAddress, hwvtepNodeId);
131 ResourceBatchingManager.getInstance().delete(ResourceBatchingManager.ShardResource.CONFIG_TOPOLOGY,
134 String elanName = getElanName(macRemoved);
136 jobCoordinator.enqueueJob(elanName + HwvtepHAUtil.L2GW_JOB_KEY ,
138 L2GatewayDevice elanL2GwDevice = ElanL2GwCacheUtils.getL2GatewayDeviceFromCache(elanName,
140 if (elanL2GwDevice == null) {
141 LOG.warn("Could not find L2GatewayDevice for ELAN: {}, nodeID:{} from cache",
142 elanName, hwvtepNodeId);
146 elanL2GwDevice.removeUcastLocalMac(macRemoved);
147 ElanInstance elanInstance = ElanUtils.getElanInstanceByName(dataBroker, elanName);
149 elanClusterUtils.runOnlyInOwnerNode(elanName + ":" + macAddress,
150 "Delete l2gw local ucast mac", () -> {
151 return elanL2GatewayUtils.unInstallL2GwUcastMacFromElan(elanInstance, elanL2GwDevice,
152 Collections.singletonList(new MacAddress(macAddress.toLowerCase()))); });
157 public void added(final InstanceIdentifier<LocalUcastMacs> identifier, final LocalUcastMacs macAdded) {
158 ResourceBatchingManager.getInstance().put(ResourceBatchingManager.ShardResource.CONFIG_TOPOLOGY,
159 identifier, macAdded);
161 String hwvtepNodeId = identifier.firstKeyOf(Node.class).getNodeId().getValue();
162 String macAddress = macAdded.getMacEntryKey().getValue().toLowerCase(Locale.getDefault());
163 String elanName = getElanName(macAdded);
165 LOG.trace("LocalUcastMacs {} added to {}", macAddress, hwvtepNodeId);
167 ElanInstance elan = ElanUtils.getElanInstanceByName(dataBroker, elanName);
169 LOG.warn("Could not find ELAN for mac {} being added", macAddress);
172 jobCoordinator.enqueueJob(elanName + HwvtepHAUtil.L2GW_JOB_KEY,
174 L2GatewayDevice elanL2GwDevice =
175 ElanL2GwCacheUtils.getL2GatewayDeviceFromCache(elanName, hwvtepNodeId);
176 if (elanL2GwDevice == null) {
177 LOG.warn("Could not find L2GatewayDevice for ELAN: {}, nodeID:{} from cache",
178 elanName, hwvtepNodeId);
182 elanL2GwDevice.addUcastLocalMac(macAdded);
183 elanL2GatewayUtils.installL2GwUcastMacInElan(elan, elanL2GwDevice,
184 macAddress.toLowerCase(), macAdded, null);
190 protected Map<InstanceIdentifier<LocalUcastMacs>, DataObjectModification<LocalUcastMacs>> getChildMod(
191 final InstanceIdentifier<Node> parentIid,
192 final DataObjectModification<Node> mod) {
194 Map<InstanceIdentifier<LocalUcastMacs>, DataObjectModification<LocalUcastMacs>> result = new HashMap<>();
195 DataObjectModification<HwvtepGlobalAugmentation> aug = mod.getModifiedAugmentation(
196 HwvtepGlobalAugmentation.class);
197 if (aug != null && getModificationType(aug) != null) {
198 Collection<DataObjectModification<? extends DataObject>> children = aug.getModifiedChildren();
200 .filter(childMod -> getModificationType(childMod) != null)
201 .filter(childMod -> childMod.getDataType() == LocalUcastMacs.class)
202 .forEach(childMod -> {
203 LocalUcastMacs afterMac = (LocalUcastMacs) childMod.getDataAfter();
204 LocalUcastMacs mac = afterMac != null ? afterMac : (LocalUcastMacs)childMod.getDataBefore();
205 InstanceIdentifier<LocalUcastMacs> iid = parentIid
206 .augmentation(HwvtepGlobalAugmentation.class)
207 .child(LocalUcastMacs.class, mac.getKey());
208 result.put(iid, (DataObjectModification<LocalUcastMacs>) childMod);
215 protected void onParentAdded(final DataTreeModification<Node> modification) {
216 InstanceIdentifier<Node> nodeIid = modification.getRootPath().getRootIdentifier();
217 if (IS_PS_NODE_IID.test(nodeIid)) {
220 ReadWriteTransaction tx = dataBroker.newReadWriteTransaction();
221 haOpClusteredListener.onGlobalNodeAdd(nodeIid, modification.getRootNode().getDataAfter(), tx);
223 if (IS_HA_CHILD.test(nodeIid)) {
227 LOG.trace("On parent add {}", nodeIid);
228 Node operNode = modification.getRootNode().getDataAfter();
229 Optional<Node> configNode = MDSALUtil.read(dataBroker, LogicalDatastoreType.CONFIGURATION, nodeIid);
230 Set<LocalUcastMacs> configMacs = getMacs(configNode);
231 Set<LocalUcastMacs> operMacs = getMacs(Optional.of(operNode));
232 Set<LocalUcastMacs> staleMacs = Sets.difference(configMacs, operMacs);
233 staleMacs.forEach(staleMac -> removed(getMacIid(nodeIid, staleMac), staleMac));
236 InstanceIdentifier<LocalUcastMacs> getMacIid(InstanceIdentifier<Node> nodeIid, LocalUcastMacs mac) {
237 return nodeIid.augmentation(HwvtepGlobalAugmentation.class)
238 .child(LocalUcastMacs.class, mac.getKey());
241 Set<LocalUcastMacs> getMacs(Optional<Node> node) {
242 if (node.isPresent()) {
243 HwvtepGlobalAugmentation augmentation = node.get().getAugmentation(HwvtepGlobalAugmentation.class);
244 if (augmentation != null && augmentation.getLocalUcastMacs() != null) {
245 return new HashSet<>(augmentation.getLocalUcastMacs());
248 return Collections.emptySet();
252 protected void onParentRemoved(InstanceIdentifier<Node> parent) {
253 if (IS_PS_NODE_IID.test(parent)) {
256 LOG.trace("on parent removed {}", parent);
260 protected InstanceIdentifier<Node> getParentWildCardPath() {
261 return HwvtepSouthboundUtils.createHwvtepTopologyInstanceIdentifier()