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