Merge "Bug 6278: Switch to use odlparent's karaf-parent"
[netvirt.git] / vpnservice / elanmanager / elanmanager-impl / src / main / java / org / opendaylight / netvirt / elan / l2gw / listeners / HwvtepLocalUcastMacListener.java
1 /*
2  * Copyright (c) 2016 Ericsson India Global Services Pvt Ltd. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.netvirt.elan.l2gw.listeners;
9
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;
28
29 /**
30  * A listener for Ucast MAC entries that are added/removed to/from an External
31  * Device (e.g., TOR).
32  *
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
36  * belongs.
37  *
38  */
39 public class HwvtepLocalUcastMacListener extends
40         AsyncClusteredDataChangeListenerBase<LocalUcastMacs, HwvtepLocalUcastMacListener> implements AutoCloseable {
41
42     private DataBroker broker;
43     private ListenerRegistration<DataChangeListener> lstnerRegistration;
44
45     private static final Logger logger = LoggerFactory.getLogger(HwvtepLocalUcastMacListener.class);
46
47     public HwvtepLocalUcastMacListener(DataBroker broker) {
48         super(LocalUcastMacs.class, HwvtepLocalUcastMacListener.class);
49
50         this.broker = broker;
51         registerListener();
52     }
53
54     protected void registerListener() {
55         try {
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);
61         }
62     }
63
64     @Override
65     public void close() throws Exception {
66         if (lstnerRegistration != null) {
67             try {
68                 lstnerRegistration.close();
69             } catch (final Exception e) {
70                 logger.error("Error when cleaning up DataChangeListener.", e);
71             }
72             lstnerRegistration = null;
73         }
74     }
75
76     @Override
77     protected void remove(InstanceIdentifier<LocalUcastMacs> identifier, LocalUcastMacs macRemoved) {
78         String hwvtepNodeId = identifier.firstKeyOf(Node.class).getNodeId().getValue();
79         String macAddress = macRemoved.getMacEntryKey().getValue();
80
81         logger.trace("LocalUcastMacs {} removed from {}", macAddress, hwvtepNodeId);
82
83         ElanInstance elan = ElanL2GatewayUtils.getElanInstanceForUcastLocalMac(macRemoved);
84         if (elan == null) {
85             logger.warn("Could not find ELAN for mac {} being deleted", macAddress);
86             return;
87         }
88
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);
93             return;
94         }
95
96         // Remove MAC from cache
97         elanL2GwDevice.removeUcastLocalMac(macRemoved);
98
99         ElanL2GatewayUtils.unInstallL2GwUcastMacFromElan(elan, elanL2GwDevice,
100                 Lists.newArrayList(macRemoved.getMacEntryKey()));
101     }
102
103     @Override
104     protected void update(InstanceIdentifier<LocalUcastMacs> identifier, LocalUcastMacs original,
105             LocalUcastMacs update) {
106         // TODO (eperefr) what can change here?
107
108     }
109
110     @Override
111     protected void add(InstanceIdentifier<LocalUcastMacs> identifier, LocalUcastMacs macAdded) {
112         String hwvtepNodeId = identifier.firstKeyOf(Node.class).getNodeId().getValue();
113         String macAddress = macAdded.getMacEntryKey().getValue();
114
115         logger.trace("LocalUcastMacs {} added to {}", macAddress, hwvtepNodeId);
116
117         ElanInstance elan = ElanL2GatewayUtils.getElanInstanceForUcastLocalMac(macAdded);
118         if (elan == null) {
119             logger.warn("Could not find ELAN for mac {} being added", macAddress);
120             return;
121         }
122
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);
127             return;
128         }
129
130         // Cache MAC for furthur processing later
131         elanL2GwDevice.addUcastLocalMac(macAdded);
132
133         ElanL2GatewayUtils.installL2GwUcastMacInElan(elan, elanL2GwDevice, macAddress, null);
134     }
135
136     @Override
137     protected InstanceIdentifier<LocalUcastMacs> getWildCardPath() {
138         return InstanceIdentifier.create(LocalUcastMacs.class);
139     }
140
141     @Override
142     protected ClusteredDataChangeListener getDataChangeListener() {
143         return HwvtepLocalUcastMacListener.this;
144     }
145
146     @Override
147     protected DataChangeScope getDataChangeScope() {
148         return DataChangeScope.BASE;
149     }
150 }