Merge "L2 Gw connection support and Elan manager changes"
[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.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;
29
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 /**
34  * A listener for Ucast MAC entries that are added/removed to/from an External Device (e.g., TOR).
35  *
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
38  * MAC belongs.
39  *
40  */
41 public class HwvtepLocalUcastMacListener extends
42         AsyncClusteredDataChangeListenerBase<LocalUcastMacs, HwvtepLocalUcastMacListener> implements AutoCloseable {
43
44     private DataBroker broker;
45     private EntityOwnershipService entityOwnershipService;
46     private BindingNormalizedNodeSerializer bindingNormalizedNodeSerializer;
47     private ListenerRegistration<DataChangeListener> lstnerRegistration;
48
49     private static final Logger logger = LoggerFactory.getLogger(HwvtepLocalUcastMacListener.class);
50
51     public HwvtepLocalUcastMacListener(DataBroker broker, EntityOwnershipService entityOwnershipService,
52             BindingNormalizedNodeSerializer bindingNormalizedNodeSerializer) {
53         super(LocalUcastMacs.class, HwvtepLocalUcastMacListener.class);
54
55         this.broker = broker;
56         this.entityOwnershipService = entityOwnershipService;
57         this.bindingNormalizedNodeSerializer = bindingNormalizedNodeSerializer;
58         registerListener();
59     }
60
61     protected void registerListener() {
62         try {
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);
68         }
69     }
70
71     @Override
72     public void close() throws Exception {
73         if (lstnerRegistration != null) {
74             try {
75                 lstnerRegistration.close();
76             } catch (final Exception e) {
77                 logger.error("Error when cleaning up DataChangeListener.", e);
78             }
79             lstnerRegistration = null;
80         }
81     }
82
83     @Override
84     protected void remove(InstanceIdentifier<LocalUcastMacs> identifier, LocalUcastMacs macRemoved) {
85         String hwvtepNodeId = identifier.firstKeyOf(Node.class).getNodeId().getValue();
86         String macAddress = macRemoved.getMacEntryKey().getValue();
87
88         logger.trace("LocalUcastMacs {} removed from {}", macAddress, hwvtepNodeId);
89
90         ElanInstance elan = ElanL2GatewayUtils.getElanInstanceForUcastLocalMac(macRemoved);
91         if (elan == null) {
92             logger.warn("Could not find ELAN for mac {} being deleted", macAddress);
93             return;
94         }
95
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);
100             return;
101         }
102
103         // Remove MAC from cache
104         elanL2GwDevice.removeUcastLocalMac(macRemoved);
105
106         ElanL2GatewayUtils.unInstallL2GwUcastMacFromElan(entityOwnershipService, bindingNormalizedNodeSerializer, elan,
107                 elanL2GwDevice, macRemoved);    }
108
109     @Override
110     protected void update(InstanceIdentifier<LocalUcastMacs> identifier, LocalUcastMacs original,
111             LocalUcastMacs update) {
112         // TODO (eperefr) what can change here?
113
114     }
115
116     @Override
117     protected void add(InstanceIdentifier<LocalUcastMacs> identifier, LocalUcastMacs macAdded) {
118         String hwvtepNodeId = identifier.firstKeyOf(Node.class).getNodeId().getValue();
119         String macAddress = macAdded.getMacEntryKey().getValue();
120
121         logger.trace("LocalUcastMacs {} added to {}", macAddress, hwvtepNodeId);
122
123         ElanInstance elan = ElanL2GatewayUtils.getElanInstanceForUcastLocalMac(macAdded);
124         if (elan == null) {
125             logger.warn("Could not find ELAN for mac {} being added", macAddress);
126             return;
127         }
128
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);
133             return;
134         }
135
136         // Cache MAC for furthur processing later
137         elanL2GwDevice.addUcastLocalMac(macAdded);
138
139         ElanL2GatewayUtils.installL2GwUcastMacInElan(entityOwnershipService, bindingNormalizedNodeSerializer, elan,
140                 elanL2GwDevice, macAddress);
141     }
142
143     @Override
144     protected InstanceIdentifier<LocalUcastMacs> getWildCardPath() {
145         return InstanceIdentifier.create(LocalUcastMacs.class);
146     }
147
148     @Override
149     protected ClusteredDataChangeListener getDataChangeListener() {
150         return HwvtepLocalUcastMacListener.this;
151     }
152
153     @Override
154     protected DataChangeScope getDataChangeScope() {
155         return DataChangeScope.BASE;
156     }
157 }