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.DataBroker;
11 import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
12 import org.opendaylight.controller.md.sal.binding.api.ClusteredDataChangeListener;
13 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
14 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
15 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipService;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LocalUcastMacs;
17 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
18 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.elan.rev150602.elan.instances.ElanInstance;
20 import org.opendaylight.yangtools.binding.data.codec.api.BindingNormalizedNodeSerializer;
21 import org.opendaylight.yangtools.concepts.ListenerRegistration;
22 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
23 import org.opendaylight.vpnservice.elan.l2gw.utils.ElanL2GatewayUtils;
24 import org.opendaylight.vpnservice.datastoreutils.AsyncClusteredDataChangeListenerBase;
25 import org.opendaylight.vpnservice.mdsalutil.MDSALUtil;
26 import org.opendaylight.vpnservice.utils.hwvtep.HwvtepUtils;
27 import org.opendaylight.vpnservice.neutronvpn.api.l2gw.L2GatewayDevice;
28 import org.opendaylight.elanmanager.utils.ElanL2GwCacheUtils;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
34 * A listener for Ucast MAC entries that are added/removed to/from an External Device (e.g., TOR).
36 * When a Ucast MAC addr appears in the hwvtep's operational DS, that MAC must be populated in DMAC tables in all
37 * Elan participating DPNs. ELAN is selected according to field 'tunnel_key' of the Logical Switch to which the new
41 public class HwvtepLocalUcastMacListener extends
42 AsyncClusteredDataChangeListenerBase<LocalUcastMacs, HwvtepLocalUcastMacListener> implements AutoCloseable {
44 private DataBroker broker;
45 private EntityOwnershipService entityOwnershipService;
46 private BindingNormalizedNodeSerializer bindingNormalizedNodeSerializer;
47 private ListenerRegistration<DataChangeListener> lstnerRegistration;
49 private static final Logger logger = LoggerFactory.getLogger(HwvtepLocalUcastMacListener.class);
51 public HwvtepLocalUcastMacListener(DataBroker broker, EntityOwnershipService entityOwnershipService,
52 BindingNormalizedNodeSerializer bindingNormalizedNodeSerializer) {
53 super(LocalUcastMacs.class, HwvtepLocalUcastMacListener.class);
56 this.entityOwnershipService = entityOwnershipService;
57 this.bindingNormalizedNodeSerializer = bindingNormalizedNodeSerializer;
61 protected void registerListener() {
63 lstnerRegistration = this.broker.registerDataChangeListener(LogicalDatastoreType.OPERATIONAL,
64 HwvtepUtils.getWildCardPathForLocalUcastMacs(), this, DataChangeScope.SUBTREE);
65 } catch (final Exception e) {
66 logger.error("Hwvtep LocalUcasMacs DataChange listener registration failed !", e);
67 throw new IllegalStateException("Hwvtep LocalUcasMacs DataChange listener registration failed .", e);
72 public void close() throws Exception {
73 if (lstnerRegistration != null) {
75 lstnerRegistration.close();
76 } catch (final Exception e) {
77 logger.error("Error when cleaning up DataChangeListener.", e);
79 lstnerRegistration = null;
84 protected void remove(InstanceIdentifier<LocalUcastMacs> identifier, LocalUcastMacs macRemoved) {
85 String hwvtepNodeId = identifier.firstKeyOf(Node.class).getNodeId().getValue();
86 String macAddress = macRemoved.getMacEntryKey().getValue();
88 logger.trace("LocalUcastMacs {} removed from {}", macAddress, hwvtepNodeId);
90 ElanInstance elan = ElanL2GatewayUtils.getElanInstanceForUcastLocalMac(macRemoved);
92 logger.warn("Could not find ELAN for mac {} being deleted", macAddress);
96 String elanName = elan.getElanInstanceName();
97 L2GatewayDevice elanL2GwDevice = ElanL2GwCacheUtils.getL2GatewayDeviceFromCache(elanName, hwvtepNodeId);
98 if (elanL2GwDevice == null) {
99 logger.warn("Could not find L2GatewayDevice for ELAN: {}, nodeID:{} from cache", elanName, hwvtepNodeId);
103 // Remove MAC from cache
104 elanL2GwDevice.removeUcastLocalMac(macRemoved);
106 ElanL2GatewayUtils.unInstallL2GwUcastMacFromElan(entityOwnershipService, bindingNormalizedNodeSerializer, elan,
107 elanL2GwDevice, macRemoved); }
110 protected void update(InstanceIdentifier<LocalUcastMacs> identifier, LocalUcastMacs original,
111 LocalUcastMacs update) {
112 // TODO (eperefr) what can change here?
117 protected void add(InstanceIdentifier<LocalUcastMacs> identifier, LocalUcastMacs macAdded) {
118 String hwvtepNodeId = identifier.firstKeyOf(Node.class).getNodeId().getValue();
119 String macAddress = macAdded.getMacEntryKey().getValue();
121 logger.trace("LocalUcastMacs {} added to {}", macAddress, hwvtepNodeId);
123 ElanInstance elan = ElanL2GatewayUtils.getElanInstanceForUcastLocalMac(macAdded);
125 logger.warn("Could not find ELAN for mac {} being added", macAddress);
129 String elanName = elan.getElanInstanceName();
130 L2GatewayDevice elanL2GwDevice = ElanL2GwCacheUtils.getL2GatewayDeviceFromCache(elanName, hwvtepNodeId);
131 if (elanL2GwDevice == null) {
132 logger.warn("Could not find L2GatewayDevice for ELAN: {}, nodeID:{} from cache", elanName, hwvtepNodeId);
136 // Cache MAC for furthur processing later
137 elanL2GwDevice.addUcastLocalMac(macAdded);
139 ElanL2GatewayUtils.installL2GwUcastMacInElan(entityOwnershipService, bindingNormalizedNodeSerializer, elan,
140 elanL2GwDevice, macAddress);
144 protected InstanceIdentifier<LocalUcastMacs> getWildCardPath() {
145 return InstanceIdentifier.create(LocalUcastMacs.class);
149 protected ClusteredDataChangeListener getDataChangeListener() {
150 return HwvtepLocalUcastMacListener.this;
154 protected DataChangeScope getDataChangeScope() {
155 return DataChangeScope.BASE;