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 java.util.Locale;
12 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
13 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
14 import org.opendaylight.genius.datastoreutils.hwvtep.HwvtepClusteredDataTreeChangeListener;
15 import org.opendaylight.genius.utils.batching.ResourceBatchingManager;
16 import org.opendaylight.genius.utils.hwvtep.HwvtepUtils;
17 import org.opendaylight.netvirt.elan.l2gw.utils.ElanL2GatewayUtils;
18 import org.opendaylight.netvirt.elan.utils.ElanUtils;
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.MacAddress;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.instances.ElanInstance;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LocalUcastMacs;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LogicalSwitches;
25 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
26 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
31 * A listener for Ucast MAC entries that are added/removed to/from an External
34 * <p>When a Ucast MAC addr appears in the hwvtep's operational DS, that MAC must
35 * be populated in DMAC tables in all Elan participating DPNs. ELAN is selected
36 * according to field 'tunnel_key' of the Logical Switch to which the new MAC
39 public class HwvtepLocalUcastMacListener extends
40 HwvtepClusteredDataTreeChangeListener<LocalUcastMacs, HwvtepLocalUcastMacListener> {
42 private static final Logger LOG = LoggerFactory.getLogger(HwvtepLocalUcastMacListener.class);
44 private final DataBroker broker;
45 private final ElanL2GatewayUtils elanL2GatewayUtils;
47 public HwvtepLocalUcastMacListener(DataBroker broker, ElanL2GatewayUtils elanL2GatewayUtils) {
48 super(LocalUcastMacs.class, HwvtepLocalUcastMacListener.class);
51 this.elanL2GatewayUtils = elanL2GatewayUtils;
52 ResourceBatchingManager.getInstance().registerDefaultBatchHandlers(this.broker);
56 registerListener(LogicalDatastoreType.OPERATIONAL, broker);
60 protected void removed(InstanceIdentifier<LocalUcastMacs> identifier, LocalUcastMacs macRemoved) {
61 String hwvtepNodeId = identifier.firstKeyOf(Node.class).getNodeId().getValue();
62 String macAddress = macRemoved.getMacEntryKey().getValue().toLowerCase(Locale.getDefault());
64 LOG.trace("LocalUcastMacs {} removed from {}", macAddress, hwvtepNodeId);
66 String elanName = getElanName(macRemoved);
67 ElanInstance elan = ElanUtils.getElanInstanceByName(broker, elanName);
69 L2GatewayDevice elanL2GwDevice = ElanL2GwCacheUtils.getL2GatewayDeviceFromCache(elanName, hwvtepNodeId);
70 if (elanL2GwDevice == null) {
71 LOG.warn("Could not find L2GatewayDevice for ELAN: {}, nodeID:{} from cache", elanName, hwvtepNodeId);
75 // Remove MAC from cache
76 elanL2GwDevice.removeUcastLocalMac(macRemoved);
78 elanL2GatewayUtils.unInstallL2GwUcastMacFromElan(elan, elanL2GwDevice,
79 Collections.singletonList(new MacAddress(macAddress.toLowerCase(Locale.getDefault()))));
82 protected String getElanName(LocalUcastMacs mac) {
83 return mac.getLogicalSwitchRef().getValue()
84 .firstKeyOf(LogicalSwitches.class).getHwvtepNodeName().getValue();
88 protected void updated(InstanceIdentifier<LocalUcastMacs> identifier, LocalUcastMacs original,
89 LocalUcastMacs update) {
90 // TODO (eperefr) what can change here?
95 public void added(InstanceIdentifier<LocalUcastMacs> identifier, LocalUcastMacs macAdded) {
96 String hwvtepNodeId = identifier.firstKeyOf(Node.class).getNodeId().getValue();
97 String macAddress = macAdded.getMacEntryKey().getValue().toLowerCase(Locale.getDefault());
99 LOG.trace("LocalUcastMacs {} added to {}", macAddress, hwvtepNodeId);
101 String elanName = getElanName(macAdded);
102 ElanInstance elan = ElanUtils.getElanInstanceByName(broker, elanName);
104 LOG.warn("Could not find ELAN for mac {} being added", macAddress);
108 L2GatewayDevice elanL2GwDevice = ElanL2GwCacheUtils.getL2GatewayDeviceFromCache(elanName, hwvtepNodeId);
109 if (elanL2GwDevice == null) {
110 LOG.warn("Could not find L2GatewayDevice for ELAN: {}, nodeID:{} from cache", elanName, hwvtepNodeId);
114 // Cache MAC for furthur processing later
115 elanL2GwDevice.addUcastLocalMac(macAdded);
117 elanL2GatewayUtils.installL2GwUcastMacInElan(elan, elanL2GwDevice, macAddress.toLowerCase(Locale.getDefault()),
122 protected InstanceIdentifier<LocalUcastMacs> getWildCardPath() {
123 return HwvtepUtils.getWildCardPathForLocalUcastMacs();
127 protected HwvtepLocalUcastMacListener getDataTreeChangeListener() {