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 com.google.common.collect.Lists;
11 import org.opendaylight.controller.md.sal.binding.api.ClusteredDataChangeListener;
12 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
13 import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
14 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
15 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
16 import org.opendaylight.genius.datastoreutils.AsyncClusteredDataChangeListenerBase;
17 import org.opendaylight.genius.utils.hwvtep.HwvtepUtils;
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.opendaylight.netvirt.elan.rev150602.elan.instances.ElanInstance;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LocalUcastMacs;
23 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
24 import org.opendaylight.yangtools.concepts.ListenerRegistration;
25 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
30 * A listener for Ucast MAC entries that are added/removed to/from an External
33 * When a Ucast MAC addr appears in the hwvtep's operational DS, that MAC must
34 * be populated in DMAC tables in all Elan participating DPNs. ELAN is selected
35 * according to field 'tunnel_key' of the Logical Switch to which the new MAC
39 public class HwvtepLocalUcastMacListener extends
40 AsyncClusteredDataChangeListenerBase<LocalUcastMacs, HwvtepLocalUcastMacListener> implements AutoCloseable {
42 private DataBroker broker;
43 private ListenerRegistration<DataChangeListener> lstnerRegistration;
45 private static final Logger logger = LoggerFactory.getLogger(HwvtepLocalUcastMacListener.class);
47 public HwvtepLocalUcastMacListener(DataBroker broker) {
48 super(LocalUcastMacs.class, HwvtepLocalUcastMacListener.class);
54 protected void registerListener() {
56 lstnerRegistration = this.broker.registerDataChangeListener(LogicalDatastoreType.OPERATIONAL,
57 HwvtepUtils.getWildCardPathForLocalUcastMacs(), this, DataChangeScope.SUBTREE);
58 } catch (final Exception e) {
59 logger.error("Hwvtep LocalUcasMacs DataChange listener registration failed !", e);
60 throw new IllegalStateException("Hwvtep LocalUcasMacs DataChange listener registration failed .", e);
65 public void close() throws Exception {
66 if (lstnerRegistration != null) {
68 lstnerRegistration.close();
69 } catch (final Exception e) {
70 logger.error("Error when cleaning up DataChangeListener.", e);
72 lstnerRegistration = null;
77 protected void remove(InstanceIdentifier<LocalUcastMacs> identifier, LocalUcastMacs macRemoved) {
78 String hwvtepNodeId = identifier.firstKeyOf(Node.class).getNodeId().getValue();
79 String macAddress = macRemoved.getMacEntryKey().getValue();
81 logger.trace("LocalUcastMacs {} removed from {}", macAddress, hwvtepNodeId);
83 ElanInstance elan = ElanL2GatewayUtils.getElanInstanceForUcastLocalMac(macRemoved);
85 logger.warn("Could not find ELAN for mac {} being deleted", macAddress);
89 String elanName = elan.getElanInstanceName();
90 L2GatewayDevice elanL2GwDevice = ElanL2GwCacheUtils.getL2GatewayDeviceFromCache(elanName, hwvtepNodeId);
91 if (elanL2GwDevice == null) {
92 logger.warn("Could not find L2GatewayDevice for ELAN: {}, nodeID:{} from cache", elanName, hwvtepNodeId);
96 // Remove MAC from cache
97 elanL2GwDevice.removeUcastLocalMac(macRemoved);
99 ElanL2GatewayUtils.unInstallL2GwUcastMacFromElan(elan, elanL2GwDevice,
100 Lists.newArrayList(macRemoved.getMacEntryKey()));
104 protected void update(InstanceIdentifier<LocalUcastMacs> identifier, LocalUcastMacs original,
105 LocalUcastMacs update) {
106 // TODO (eperefr) what can change here?
111 protected void add(InstanceIdentifier<LocalUcastMacs> identifier, LocalUcastMacs macAdded) {
112 String hwvtepNodeId = identifier.firstKeyOf(Node.class).getNodeId().getValue();
113 String macAddress = macAdded.getMacEntryKey().getValue();
115 logger.trace("LocalUcastMacs {} added to {}", macAddress, hwvtepNodeId);
117 ElanInstance elan = ElanL2GatewayUtils.getElanInstanceForUcastLocalMac(macAdded);
119 logger.warn("Could not find ELAN for mac {} being added", macAddress);
123 String elanName = elan.getElanInstanceName();
124 L2GatewayDevice elanL2GwDevice = ElanL2GwCacheUtils.getL2GatewayDeviceFromCache(elanName, hwvtepNodeId);
125 if (elanL2GwDevice == null) {
126 logger.warn("Could not find L2GatewayDevice for ELAN: {}, nodeID:{} from cache", elanName, hwvtepNodeId);
130 // Cache MAC for furthur processing later
131 elanL2GwDevice.addUcastLocalMac(macAdded);
133 ElanL2GatewayUtils.installL2GwUcastMacInElan(elan, elanL2GwDevice, macAddress, null);
137 protected InstanceIdentifier<LocalUcastMacs> getWildCardPath() {
138 return InstanceIdentifier.create(LocalUcastMacs.class);
142 protected ClusteredDataChangeListener getDataChangeListener() {
143 return HwvtepLocalUcastMacListener.this;
147 protected DataChangeScope getDataChangeScope() {
148 return DataChangeScope.BASE;