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