Bump versions by 0.1.0 for next dev cycle
[vpnservice.git] / elanmanager / elanmanager-impl / src / main / java / org / opendaylight / vpnservice / elan / internal / ElanDpnInterfaceClusteredListener.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.internal;
9
10 import java.util.List;
11 import java.util.concurrent.Callable;
12
13 import org.opendaylight.controller.md.sal.binding.api.ClusteredDataChangeListener;
14 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
15 import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
16 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker;
17 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
18 import org.opendaylight.elanmanager.utils.ElanL2GwCacheUtils;
19 import org.opendaylight.vpnservice.datastoreutils.AsyncClusteredDataChangeListenerBase;
20 import org.opendaylight.vpnservice.elan.l2gw.utils.ElanL2GatewayMulticastUtils;
21 import org.opendaylight.vpnservice.elan.l2gw.utils.ElanL2GatewayUtils;
22 import org.opendaylight.vpnservice.elan.utils.ElanClusterUtils;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.elan.rev150602.ElanDpnInterfaces;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.elan.rev150602.elan.dpn.interfaces.ElanDpnInterfacesList;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.elan.rev150602.elan.dpn.interfaces.elan.dpn.interfaces.list.DpnInterfaces;
26 import org.opendaylight.yangtools.concepts.ListenerRegistration;
27 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 import com.google.common.collect.Lists;
32 import com.google.common.util.concurrent.ListenableFuture;
33
34 public class ElanDpnInterfaceClusteredListener
35         extends AsyncClusteredDataChangeListenerBase<DpnInterfaces, ElanDpnInterfaceClusteredListener>
36         implements AutoCloseable {
37     private DataBroker broker;
38     private ElanInterfaceManager elanInterfaceManager;
39     private ListenerRegistration<DataChangeListener> listenerRegistration;
40
41     private static final Logger LOG = LoggerFactory.getLogger(ElanDpnInterfaceClusteredListener.class);
42
43     public ElanDpnInterfaceClusteredListener(final DataBroker db, final ElanInterfaceManager ifManager) {
44         super(DpnInterfaces.class, ElanDpnInterfaceClusteredListener.class);
45         broker = db;
46         elanInterfaceManager = ifManager;
47         registerListener(db);
48     }
49
50     private void registerListener(final DataBroker db) {
51         try {
52             listenerRegistration = broker.registerDataChangeListener(LogicalDatastoreType.OPERATIONAL,
53                     getWildCardPath(), ElanDpnInterfaceClusteredListener.this, AsyncDataBroker.DataChangeScope.BASE);
54         } catch (final Exception e) {
55             LOG.error("DpnInterfaces DataChange listener registration fail!", e);
56         }
57     }
58
59     @Override
60     public InstanceIdentifier<DpnInterfaces> getWildCardPath() {
61         return InstanceIdentifier.builder(ElanDpnInterfaces.class).child(ElanDpnInterfacesList.class)
62                 .child(DpnInterfaces.class).build();
63     }
64
65     @Override
66     protected ClusteredDataChangeListener getDataChangeListener() {
67         return ElanDpnInterfaceClusteredListener.this;
68     }
69
70     @Override
71     protected AsyncDataBroker.DataChangeScope getDataChangeScope() {
72         return AsyncDataBroker.DataChangeScope.BASE;
73     }
74
75     void handleUpdate(InstanceIdentifier<DpnInterfaces> id, DpnInterfaces dpnInterfaces) {
76         final String elanName = getElanName(id);
77         if (ElanL2GwCacheUtils.getInvolvedL2GwDevices(elanName).isEmpty()) {
78             LOG.debug("dpnInterface updation, no external l2 devices to update for elan {} with Dp Id:", elanName,
79                     dpnInterfaces.getDpId());
80             return;
81         }
82         ElanClusterUtils.runOnlyInLeaderNode(elanName, "updating mcast mac upon tunnel event",
83                 new Callable<List<ListenableFuture<Void>>>() {
84                     @Override
85                     public List<ListenableFuture<Void>> call() throws Exception {
86                         return Lists.newArrayList(
87                                 ElanL2GatewayMulticastUtils.updateRemoteMcastMacOnElanL2GwDevices(elanName));
88                     }
89                 });
90     }
91
92     @Override
93     protected void remove(InstanceIdentifier<DpnInterfaces> identifier, final DpnInterfaces dpnInterfaces) {
94         // this is the last dpn interface on this elan
95         final String elanName = getElanName(identifier);
96         LOG.debug("Received ElanDpnInterface removed for for elan {} with Dp Id ", elanName,
97                 dpnInterfaces.getDpId());
98
99         if (ElanL2GwCacheUtils.getInvolvedL2GwDevices(elanName).isEmpty()) {
100             LOG.debug("dpnInterface removed, no external l2 devices to update for elan {} with Dp Id:", elanName,
101                     dpnInterfaces.getDpId());
102             return;
103         }
104         ElanClusterUtils.runOnlyInLeaderNode(elanName, "handling ElanDpnInterface removed",
105                 new Callable<List<ListenableFuture<Void>>>() {
106                     @Override
107                     public List<ListenableFuture<Void>> call() throws Exception {
108                         // deleting Elan L2Gw Devices UcastLocalMacs From Dpn
109                         ElanL2GatewayUtils.deleteElanL2GwDevicesUcastLocalMacsFromDpn(elanName,
110                                 dpnInterfaces.getDpId());
111                         // updating remote mcast mac on l2gw devices
112                         return Lists.newArrayList(
113                                 ElanL2GatewayMulticastUtils.updateRemoteMcastMacOnElanL2GwDevices(elanName));
114                     }
115                 });
116     }
117
118     @Override
119     protected void update(InstanceIdentifier<DpnInterfaces> identifier, DpnInterfaces original,
120             final DpnInterfaces dpnInterfaces) {
121         LOG.debug("dpninterfaces update fired new size {}", dpnInterfaces.getInterfaces().size());
122         if (dpnInterfaces.getInterfaces().size() == 0) {
123             LOG.debug("dpninterfaces last dpn interface on this elan {} ", dpnInterfaces.getKey());
124             // this is the last dpn interface on this elan
125             handleUpdate(identifier, dpnInterfaces);
126         }
127     }
128
129     @Override
130     protected void add(InstanceIdentifier<DpnInterfaces> identifier, final DpnInterfaces dpnInterfaces) {
131         if (dpnInterfaces.getInterfaces().size() == 1) {
132             LOG.debug("dpninterfaces first dpn interface on this elan {} {} ", dpnInterfaces.getKey(),
133                     dpnInterfaces.getInterfaces().get(0));
134             // this is the first dpn interface on this elan
135             handleUpdate(identifier, dpnInterfaces);
136         }
137     }
138
139     /**
140      * @param identifier
141      * @return
142      */
143     private String getElanName(InstanceIdentifier<DpnInterfaces> identifier) {
144         return identifier.firstKeyOf(ElanDpnInterfacesList.class).getElanInstanceName();
145     }
146 }