Bug 6589 adding support for hwvtep devices ha
[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.DataBroker;
12 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
13 import org.opendaylight.genius.datastoreutils.hwvtep.HwvtepClusteredDataTreeChangeListener;
14 import org.opendaylight.genius.utils.hwvtep.HwvtepUtils;
15 import org.opendaylight.netvirt.elan.l2gw.utils.ElanL2GatewayUtils;
16 import org.opendaylight.netvirt.elan.utils.ElanUtils;
17 import org.opendaylight.netvirt.elanmanager.utils.ElanL2GwCacheUtils;
18 import org.opendaylight.netvirt.neutronvpn.api.l2gw.L2GatewayDevice;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.instances.ElanInstance;
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.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
22 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 /**
27  * A listener for Ucast MAC entries that are added/removed to/from an External
28  * Device (e.g., TOR).
29  *
30  * <p>When a Ucast MAC addr appears in the hwvtep's operational DS, that MAC must
31  * be populated in DMAC tables in all Elan participating DPNs. ELAN is selected
32  * according to field 'tunnel_key' of the Logical Switch to which the new MAC
33  * belongs.
34  */
35 public class HwvtepLocalUcastMacListener extends
36         HwvtepClusteredDataTreeChangeListener<LocalUcastMacs, HwvtepLocalUcastMacListener> implements AutoCloseable {
37
38     private static final Logger LOG = LoggerFactory.getLogger(HwvtepLocalUcastMacListener.class);
39
40     private final DataBroker broker;
41     private final ElanL2GatewayUtils elanL2GatewayUtils;
42
43     public HwvtepLocalUcastMacListener(DataBroker broker, ElanUtils elanUtils) {
44         super(LocalUcastMacs.class, HwvtepLocalUcastMacListener.class);
45
46         this.broker = broker;
47         this.elanL2GatewayUtils = elanUtils.getElanL2GatewayUtils();
48     }
49
50     public void init() {
51         registerListener(LogicalDatastoreType.OPERATIONAL, broker);
52     }
53
54     @Override
55     protected void removed(InstanceIdentifier<LocalUcastMacs> identifier, LocalUcastMacs macRemoved) {
56         String hwvtepNodeId = identifier.firstKeyOf(Node.class).getNodeId().getValue();
57         String macAddress = macRemoved.getMacEntryKey().getValue();
58
59         LOG.trace("LocalUcastMacs {} removed from {}", macAddress, hwvtepNodeId);
60
61         ElanInstance elan = elanL2GatewayUtils.getElanInstanceForUcastLocalMac(macRemoved);
62         if (elan == null) {
63             LOG.warn("Could not find ELAN for mac {} being deleted", macAddress);
64             return;
65         }
66
67         String elanName = elan.getElanInstanceName();
68         L2GatewayDevice elanL2GwDevice = ElanL2GwCacheUtils.getL2GatewayDeviceFromCache(elanName, hwvtepNodeId);
69         if (elanL2GwDevice == null) {
70             LOG.warn("Could not find L2GatewayDevice for ELAN: {}, nodeID:{} from cache", elanName, hwvtepNodeId);
71             return;
72         }
73
74         // Remove MAC from cache
75         elanL2GwDevice.removeUcastLocalMac(macRemoved);
76
77         elanL2GatewayUtils.unInstallL2GwUcastMacFromElan(elan, elanL2GwDevice,
78                 Lists.newArrayList(macRemoved.getMacEntryKey()));
79     }
80
81     @Override
82     protected void updated(InstanceIdentifier<LocalUcastMacs> identifier, LocalUcastMacs original,
83             LocalUcastMacs update) {
84         // TODO (eperefr) what can change here?
85
86     }
87
88     @Override
89     protected void added(InstanceIdentifier<LocalUcastMacs> identifier, LocalUcastMacs macAdded) {
90         String hwvtepNodeId = identifier.firstKeyOf(Node.class).getNodeId().getValue();
91         String macAddress = macAdded.getMacEntryKey().getValue();
92
93         LOG.trace("LocalUcastMacs {} added to {}", macAddress, hwvtepNodeId);
94
95         ElanInstance elan = elanL2GatewayUtils.getElanInstanceForUcastLocalMac(macAdded);
96         if (elan == null) {
97             LOG.warn("Could not find ELAN for mac {} being added", macAddress);
98             return;
99         }
100
101         String elanName = elan.getElanInstanceName();
102         L2GatewayDevice elanL2GwDevice = ElanL2GwCacheUtils.getL2GatewayDeviceFromCache(elanName, hwvtepNodeId);
103         if (elanL2GwDevice == null) {
104             LOG.warn("Could not find L2GatewayDevice for ELAN: {}, nodeID:{} from cache", elanName, hwvtepNodeId);
105             return;
106         }
107
108         // Cache MAC for furthur processing later
109         elanL2GwDevice.addUcastLocalMac(macAdded);
110
111         elanL2GatewayUtils.installL2GwUcastMacInElan(elan, elanL2GwDevice, macAddress, null);
112     }
113
114     @Override
115     protected InstanceIdentifier<LocalUcastMacs> getWildCardPath() {
116         return HwvtepUtils.getWildCardPathForLocalUcastMacs();
117     }
118
119     @Override
120     protected HwvtepLocalUcastMacListener getDataTreeChangeListener() {
121         return this;
122     }
123 }