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.cache.ElanInstanceCache;
35 import org.opendaylight.netvirt.elan.l2gw.ha.HwvtepHAUtil;
36 import org.opendaylight.netvirt.elan.l2gw.ha.listeners.HAOpClusteredListener;
37 import org.opendaylight.netvirt.elan.l2gw.utils.ElanL2GatewayUtils;
38 import org.opendaylight.netvirt.elanmanager.utils.ElanL2GwCacheUtils;
39 import org.opendaylight.netvirt.neutronvpn.api.l2gw.L2GatewayDevice;
40 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.MacAddress;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.instances.ElanInstance;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalAugmentation;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LocalUcastMacs;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LogicalSwitches;
45 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
46 import org.opendaylight.yangtools.yang.binding.DataObject;
47 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
50 public class LocalUcastMacListener extends ChildListener<Node, LocalUcastMacs, String>
51 implements ClusteredDataTreeChangeListener<Node> {
53 public static final String NODE_CHECK = "physical";
55 private static final Predicate<InstanceIdentifier<Node>> IS_PS_NODE_IID = (iid) -> {
56 return iid.firstKeyOf(Node.class).getNodeId().getValue().contains(NODE_CHECK);
59 private static final Predicate<InstanceIdentifier<Node>> IS_NOT_HA_CHILD = (iid) -> {
60 return !HwvtepHACache.getInstance().isHAEnabledDevice(iid)
61 && !iid.firstKeyOf(Node.class).getNodeId().getValue().contains(HwvtepHAUtil.PHYSICALSWITCH);
64 private static final Predicate<InstanceIdentifier<Node>> IS_HA_CHILD = (iid) -> {
65 return HwvtepHACache.getInstance().isHAEnabledDevice(iid);
68 private final ElanL2GatewayUtils elanL2GatewayUtils;
69 private final HAOpClusteredListener haOpClusteredListener;
70 private final JobCoordinator jobCoordinator;
71 private final ElanInstanceCache elanInstanceCache;
74 public LocalUcastMacListener(final DataBroker dataBroker,
75 final HAOpClusteredListener haOpClusteredListener,
76 final ElanL2GatewayUtils elanL2GatewayUtils,
77 final JobCoordinator jobCoordinator,
78 final ElanInstanceCache elanInstanceCache) {
79 super(dataBroker, false);
80 this.elanL2GatewayUtils = elanL2GatewayUtils;
81 this.haOpClusteredListener = haOpClusteredListener;
82 this.jobCoordinator = jobCoordinator;
83 this.elanInstanceCache = elanInstanceCache;
88 public void init() throws Exception {
89 ResourceBatchingManager.getInstance().registerDefaultBatchHandlers(this.dataBroker);
94 protected boolean proceed(final InstanceIdentifier<Node> parent) {
95 return IS_NOT_HA_CHILD.test(parent);
98 protected String getElanName(final LocalUcastMacs mac) {
99 return ((InstanceIdentifier<LogicalSwitches>) mac.getLogicalSwitchRef().getValue())
100 .firstKeyOf(LogicalSwitches.class).getHwvtepNodeName().getValue();
104 protected String getGroup(final InstanceIdentifier<LocalUcastMacs> childIid,
105 final LocalUcastMacs localUcastMacs) {
106 return getElanName(localUcastMacs);
110 protected void onUpdate(final Map<String, Map<InstanceIdentifier, LocalUcastMacs>> updatedMacsGrouped,
111 final Map<String, Map<InstanceIdentifier, LocalUcastMacs>> deletedMacsGrouped) {
112 updatedMacsGrouped.entrySet().forEach((entry) -> {
113 entry.getValue().entrySet().forEach((entry2) -> {
114 added(entry2.getKey(), entry2.getValue());
117 deletedMacsGrouped.entrySet().forEach((entry) -> {
118 entry.getValue().entrySet().forEach((entry2) -> {
119 removed(entry2.getKey(), entry2.getValue());
124 public void removed(final InstanceIdentifier<LocalUcastMacs> identifier, final LocalUcastMacs macRemoved) {
125 String hwvtepNodeId = identifier.firstKeyOf(Node.class).getNodeId().getValue();
126 String macAddress = macRemoved.getMacEntryKey().getValue().toLowerCase(Locale.getDefault());
128 LOG.trace("LocalUcastMacs {} removed from {}", macAddress, hwvtepNodeId);
130 ResourceBatchingManager.getInstance().delete(ResourceBatchingManager.ShardResource.CONFIG_TOPOLOGY,
133 String elanName = getElanName(macRemoved);
135 jobCoordinator.enqueueJob(elanName + HwvtepHAUtil.L2GW_JOB_KEY ,
137 L2GatewayDevice elanL2GwDevice = ElanL2GwCacheUtils.getL2GatewayDeviceFromCache(elanName,
139 if (elanL2GwDevice == null) {
140 LOG.warn("Could not find L2GatewayDevice for ELAN: {}, nodeID:{} from cache",
141 elanName, hwvtepNodeId);
145 elanL2GwDevice.removeUcastLocalMac(macRemoved);
146 ElanInstance elanInstance = elanInstanceCache.get(elanName).orNull();
147 elanL2GatewayUtils.unInstallL2GwUcastMacFromL2gwDevices(elanName, elanL2GwDevice,
148 Collections.singletonList(new MacAddress(macAddress.toLowerCase(Locale.getDefault()))));
149 elanL2GatewayUtils.unInstallL2GwUcastMacFromElanDpns(elanInstance, elanL2GwDevice,
150 Collections.singletonList(new MacAddress(macAddress.toLowerCase(Locale.getDefault()))));
155 public void added(final InstanceIdentifier<LocalUcastMacs> identifier, final LocalUcastMacs macAdded) {
156 ResourceBatchingManager.getInstance().put(ResourceBatchingManager.ShardResource.CONFIG_TOPOLOGY,
157 identifier, macAdded);
159 String hwvtepNodeId = identifier.firstKeyOf(Node.class).getNodeId().getValue();
160 String macAddress = macAdded.getMacEntryKey().getValue().toLowerCase(Locale.getDefault());
161 String elanName = getElanName(macAdded);
163 LOG.trace("LocalUcastMacs {} added to {}", macAddress, hwvtepNodeId);
165 ElanInstance elan = elanInstanceCache.get(elanName).orNull();
167 LOG.warn("Could not find ELAN for mac {} being added", macAddress);
170 jobCoordinator.enqueueJob(elanName + HwvtepHAUtil.L2GW_JOB_KEY,
172 L2GatewayDevice elanL2GwDevice =
173 ElanL2GwCacheUtils.getL2GatewayDeviceFromCache(elanName, hwvtepNodeId);
174 if (elanL2GwDevice == null) {
175 LOG.warn("Could not find L2GatewayDevice for ELAN: {}, nodeID:{} from cache",
176 elanName, hwvtepNodeId);
180 elanL2GwDevice.addUcastLocalMac(macAdded);
181 elanL2GatewayUtils.installL2GwUcastMacInElan(elan, elanL2GwDevice,
182 macAddress.toLowerCase(), macAdded, null);
188 protected Map<InstanceIdentifier<LocalUcastMacs>, DataObjectModification<LocalUcastMacs>> getChildMod(
189 final InstanceIdentifier<Node> parentIid,
190 final DataObjectModification<Node> mod) {
192 Map<InstanceIdentifier<LocalUcastMacs>, DataObjectModification<LocalUcastMacs>> result = new HashMap<>();
193 DataObjectModification<HwvtepGlobalAugmentation> aug = mod.getModifiedAugmentation(
194 HwvtepGlobalAugmentation.class);
195 if (aug != null && getModificationType(aug) != null) {
196 Collection<DataObjectModification<? extends DataObject>> children = aug.getModifiedChildren();
198 .filter(childMod -> getModificationType(childMod) != null)
199 .filter(childMod -> childMod.getDataType() == LocalUcastMacs.class)
200 .forEach(childMod -> {
201 LocalUcastMacs afterMac = (LocalUcastMacs) childMod.getDataAfter();
202 LocalUcastMacs mac = afterMac != null ? afterMac : (LocalUcastMacs)childMod.getDataBefore();
203 InstanceIdentifier<LocalUcastMacs> iid = parentIid
204 .augmentation(HwvtepGlobalAugmentation.class)
205 .child(LocalUcastMacs.class, mac.getKey());
206 result.put(iid, (DataObjectModification<LocalUcastMacs>) childMod);
213 protected void onParentAdded(final DataTreeModification<Node> modification) {
214 InstanceIdentifier<Node> nodeIid = modification.getRootPath().getRootIdentifier();
215 if (IS_PS_NODE_IID.test(nodeIid)) {
218 ReadWriteTransaction tx = dataBroker.newReadWriteTransaction();
219 haOpClusteredListener.onGlobalNodeAdd(nodeIid, modification.getRootNode().getDataAfter(), tx);
221 if (IS_HA_CHILD.test(nodeIid)) {
225 LOG.trace("On parent add {}", nodeIid);
226 Node operNode = modification.getRootNode().getDataAfter();
227 Optional<Node> configNode = MDSALUtil.read(dataBroker, LogicalDatastoreType.CONFIGURATION, nodeIid);
228 Set<LocalUcastMacs> configMacs = getMacs(configNode);
229 Set<LocalUcastMacs> operMacs = getMacs(Optional.of(operNode));
230 Set<LocalUcastMacs> staleMacs = Sets.difference(configMacs, operMacs);
231 staleMacs.forEach(staleMac -> removed(getMacIid(nodeIid, staleMac), staleMac));
234 InstanceIdentifier<LocalUcastMacs> getMacIid(InstanceIdentifier<Node> nodeIid, LocalUcastMacs mac) {
235 return nodeIid.augmentation(HwvtepGlobalAugmentation.class)
236 .child(LocalUcastMacs.class, mac.getKey());
239 Set<LocalUcastMacs> getMacs(Optional<Node> node) {
240 if (node.isPresent()) {
241 HwvtepGlobalAugmentation augmentation = node.get().getAugmentation(HwvtepGlobalAugmentation.class);
242 if (augmentation != null && augmentation.getLocalUcastMacs() != null) {
243 return new HashSet<>(augmentation.getLocalUcastMacs());
246 return Collections.emptySet();
250 protected void onParentRemoved(InstanceIdentifier<Node> parent) {
251 if (IS_PS_NODE_IID.test(parent)) {
254 LOG.trace("on parent removed {}", parent);
258 protected InstanceIdentifier<Node> getParentWildCardPath() {
259 return HwvtepSouthboundUtils.createHwvtepTopologyInstanceIdentifier()