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.vpnservice.elan.l2gw.listeners;
10 import org.opendaylight.controller.md.sal.binding.api.ClusteredDataChangeListener;
11 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
12 import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
13 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
14 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
15 import org.opendaylight.elanmanager.utils.ElanL2GwCacheUtils;
16 import org.opendaylight.vpnservice.datastoreutils.AsyncClusteredDataChangeListenerBase;
17 import org.opendaylight.vpnservice.elan.l2gw.utils.ElanL2GatewayUtils;
18 import org.opendaylight.vpnservice.neutronvpn.api.l2gw.L2GatewayDevice;
19 import org.opendaylight.vpnservice.utils.hwvtep.HwvtepUtils;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LocalUcastMacs;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.elan.rev150602.elan.instances.ElanInstance;
22 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
23 import org.opendaylight.yangtools.concepts.ListenerRegistration;
24 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
28 import com.google.common.collect.Lists;
31 * A listener for Ucast MAC entries that are added/removed to/from an External Device (e.g., TOR).
33 * When a Ucast MAC addr appears in the hwvtep's operational DS, that MAC must be populated in DMAC tables in all
34 * Elan participating DPNs. ELAN is selected according to field 'tunnel_key' of the Logical Switch to which the new
38 public class HwvtepLocalUcastMacListener extends
39 AsyncClusteredDataChangeListenerBase<LocalUcastMacs, HwvtepLocalUcastMacListener> implements AutoCloseable {
41 private DataBroker broker;
42 private ListenerRegistration<DataChangeListener> lstnerRegistration;
44 private static final Logger logger = LoggerFactory.getLogger(HwvtepLocalUcastMacListener.class);
46 public HwvtepLocalUcastMacListener(DataBroker broker) {
47 super(LocalUcastMacs.class, HwvtepLocalUcastMacListener.class);
53 protected void registerListener() {
55 lstnerRegistration = this.broker.registerDataChangeListener(LogicalDatastoreType.OPERATIONAL,
56 HwvtepUtils.getWildCardPathForLocalUcastMacs(), this, DataChangeScope.SUBTREE);
57 } catch (final Exception e) {
58 logger.error("Hwvtep LocalUcasMacs DataChange listener registration failed !", e);
59 throw new IllegalStateException("Hwvtep LocalUcasMacs DataChange listener registration failed .", e);
64 public void close() throws Exception {
65 if (lstnerRegistration != null) {
67 lstnerRegistration.close();
68 } catch (final Exception e) {
69 logger.error("Error when cleaning up DataChangeListener.", e);
71 lstnerRegistration = null;
76 protected void remove(InstanceIdentifier<LocalUcastMacs> identifier, LocalUcastMacs macRemoved) {
77 String hwvtepNodeId = identifier.firstKeyOf(Node.class).getNodeId().getValue();
78 String macAddress = macRemoved.getMacEntryKey().getValue();
80 logger.trace("LocalUcastMacs {} removed from {}", macAddress, hwvtepNodeId);
82 ElanInstance elan = ElanL2GatewayUtils.getElanInstanceForUcastLocalMac(macRemoved);
84 logger.warn("Could not find ELAN for mac {} being deleted", macAddress);
88 String elanName = elan.getElanInstanceName();
89 L2GatewayDevice elanL2GwDevice = ElanL2GwCacheUtils.getL2GatewayDeviceFromCache(elanName, hwvtepNodeId);
90 if (elanL2GwDevice == null) {
91 logger.warn("Could not find L2GatewayDevice for ELAN: {}, nodeID:{} from cache", elanName, hwvtepNodeId);
95 // Remove MAC from cache
96 elanL2GwDevice.removeUcastLocalMac(macRemoved);
98 ElanL2GatewayUtils.unInstallL2GwUcastMacFromElan(elan, elanL2GwDevice,
99 Lists.newArrayList(macRemoved.getMacEntryKey()));
103 protected void update(InstanceIdentifier<LocalUcastMacs> identifier, LocalUcastMacs original,
104 LocalUcastMacs update) {
105 // TODO (eperefr) what can change here?
110 protected void add(InstanceIdentifier<LocalUcastMacs> identifier, LocalUcastMacs macAdded) {
111 String hwvtepNodeId = identifier.firstKeyOf(Node.class).getNodeId().getValue();
112 String macAddress = macAdded.getMacEntryKey().getValue();
114 logger.trace("LocalUcastMacs {} added to {}", macAddress, hwvtepNodeId);
116 ElanInstance elan = ElanL2GatewayUtils.getElanInstanceForUcastLocalMac(macAdded);
118 logger.warn("Could not find ELAN for mac {} being added", macAddress);
122 String elanName = elan.getElanInstanceName();
123 L2GatewayDevice elanL2GwDevice = ElanL2GwCacheUtils.getL2GatewayDeviceFromCache(elanName, hwvtepNodeId);
124 if (elanL2GwDevice == null) {
125 logger.warn("Could not find L2GatewayDevice for ELAN: {}, nodeID:{} from cache", elanName, hwvtepNodeId);
129 // Cache MAC for furthur processing later
130 elanL2GwDevice.addUcastLocalMac(macAdded);
132 ElanL2GatewayUtils.installL2GwUcastMacInElan(elan, elanL2GwDevice, macAddress);
136 protected InstanceIdentifier<LocalUcastMacs> getWildCardPath() {
137 return InstanceIdentifier.create(LocalUcastMacs.class);
141 protected ClusteredDataChangeListener getDataChangeListener() {
142 return HwvtepLocalUcastMacListener.this;
146 protected DataChangeScope getDataChangeScope() {
147 return DataChangeScope.BASE;