2 * Copyright (c) 2016 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 java.util.Collections;
11 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
12 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
13 import org.opendaylight.genius.datastoreutils.hwvtep.HwvtepClusteredDataTreeChangeListener;
14 import org.opendaylight.genius.utils.batching.ResourceBatchingManager;
15 import org.opendaylight.genius.utils.hwvtep.HwvtepNodeHACache;
16 import org.opendaylight.genius.utils.hwvtep.HwvtepUtils;
17 import org.opendaylight.netvirt.elan.cache.ElanInstanceCache;
18 import org.opendaylight.netvirt.elan.l2gw.utils.ElanL2GatewayUtils;
19 import org.opendaylight.netvirt.elanmanager.utils.ElanL2GwCacheUtils;
20 import org.opendaylight.netvirt.neutronvpn.api.l2gw.L2GatewayDevice;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.IetfYangUtil;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.MacAddress;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.instances.ElanInstance;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LocalUcastMacs;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LogicalSwitches;
26 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
27 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
32 * A listener for Ucast MAC entries that are added/removed to/from an External
35 * <p>When a Ucast MAC addr appears in the hwvtep's operational DS, that MAC must
36 * be populated in DMAC tables in all Elan participating DPNs. ELAN is selected
37 * according to field 'tunnel_key' of the Logical Switch to which the new MAC
40 public class HwvtepLocalUcastMacListener extends
41 HwvtepClusteredDataTreeChangeListener<LocalUcastMacs, HwvtepLocalUcastMacListener> {
43 private static final Logger LOG = LoggerFactory.getLogger(HwvtepLocalUcastMacListener.class);
45 private final DataBroker broker;
46 private final ElanL2GatewayUtils elanL2GatewayUtils;
47 private final ElanInstanceCache elanInstanceCache;
49 public HwvtepLocalUcastMacListener(DataBroker broker, ElanL2GatewayUtils elanL2GatewayUtils,
50 ElanInstanceCache elanInstanceCache, HwvtepNodeHACache hwvtepNodeHACache) {
51 super(LocalUcastMacs.class, HwvtepLocalUcastMacListener.class, hwvtepNodeHACache);
54 this.elanL2GatewayUtils = elanL2GatewayUtils;
55 this.elanInstanceCache = elanInstanceCache;
56 ResourceBatchingManager.getInstance().registerDefaultBatchHandlers(this.broker);
60 registerListener(LogicalDatastoreType.OPERATIONAL, broker);
64 protected void removed(InstanceIdentifier<LocalUcastMacs> identifier, LocalUcastMacs macRemoved) {
65 String hwvtepNodeId = identifier.firstKeyOf(Node.class).getNodeId().getValue();
66 MacAddress macAddress = IetfYangUtil.INSTANCE.canonizeMacAddress(macRemoved.getMacEntryKey());
68 LOG.trace("LocalUcastMacs {} removed from {}", macAddress.getValue(), hwvtepNodeId);
70 String elanName = getElanName(macRemoved);
72 L2GatewayDevice elanL2GwDevice = ElanL2GwCacheUtils.getL2GatewayDeviceFromCache(elanName, hwvtepNodeId);
73 if (elanL2GwDevice == null) {
74 LOG.warn("Could not find L2GatewayDevice for ELAN: {}, nodeID:{} from cache", elanName, hwvtepNodeId);
78 // Remove MAC from cache
79 elanL2GwDevice.removeUcastLocalMac(macRemoved);
80 elanL2GatewayUtils.unInstallL2GwUcastMacFromL2gwDevices(elanName, elanL2GwDevice,
81 Collections.singletonList(macAddress));
82 elanL2GatewayUtils.unInstallL2GwUcastMacFromElanDpns(elanInstanceCache.get(elanName).orNull(),
83 elanL2GwDevice, Collections.singletonList(macAddress));
86 protected String getElanName(LocalUcastMacs mac) {
87 return mac.getLogicalSwitchRef().getValue()
88 .firstKeyOf(LogicalSwitches.class).getHwvtepNodeName().getValue();
92 protected void updated(InstanceIdentifier<LocalUcastMacs> identifier, LocalUcastMacs original,
93 LocalUcastMacs update) {
94 // TODO (eperefr) what can change here?
99 public void added(InstanceIdentifier<LocalUcastMacs> identifier, LocalUcastMacs macAdded) {
100 String hwvtepNodeId = identifier.firstKeyOf(Node.class).getNodeId().getValue();
101 String macAddress = IetfYangUtil.INSTANCE.canonizeMacAddress(macAdded.getMacEntryKey()).getValue();
103 LOG.trace("LocalUcastMacs {} added to {}", macAddress, hwvtepNodeId);
105 String elanName = getElanName(macAdded);
106 ElanInstance elan = elanInstanceCache.get(elanName).orNull();
108 LOG.warn("Could not find ELAN for mac {} being added", macAddress);
112 L2GatewayDevice elanL2GwDevice = ElanL2GwCacheUtils.getL2GatewayDeviceFromCache(elanName, hwvtepNodeId);
113 if (elanL2GwDevice == null) {
114 LOG.warn("Could not find L2GatewayDevice for ELAN: {}, nodeID:{} from cache", elanName, hwvtepNodeId);
118 // Cache MAC for furthur processing later
119 elanL2GwDevice.addUcastLocalMac(macAdded);
121 elanL2GatewayUtils.installL2GwUcastMacInElan(elan, elanL2GwDevice, macAddress, macAdded, null);
125 protected InstanceIdentifier<LocalUcastMacs> getWildCardPath() {
126 return HwvtepUtils.getWildCardPathForLocalUcastMacs();
130 protected HwvtepLocalUcastMacListener getDataTreeChangeListener() {