2 * Copyright (c) 2016 Ericsson India Global Services Pvt Ltd. and others. All rights reserved.
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
8 package org.opendaylight.vpnservice.elan.internal;
10 import java.util.List;
11 import java.util.concurrent.Callable;
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;
31 import com.google.common.collect.Lists;
32 import com.google.common.util.concurrent.ListenableFuture;
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;
41 private static final Logger LOG = LoggerFactory.getLogger(ElanDpnInterfaceClusteredListener.class);
43 public ElanDpnInterfaceClusteredListener(final DataBroker db, final ElanInterfaceManager ifManager) {
44 super(DpnInterfaces.class, ElanDpnInterfaceClusteredListener.class);
46 elanInterfaceManager = ifManager;
50 private void registerListener(final DataBroker db) {
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);
60 public InstanceIdentifier<DpnInterfaces> getWildCardPath() {
61 return InstanceIdentifier.builder(ElanDpnInterfaces.class).child(ElanDpnInterfacesList.class)
62 .child(DpnInterfaces.class).build();
66 protected ClusteredDataChangeListener getDataChangeListener() {
67 return ElanDpnInterfaceClusteredListener.this;
71 protected AsyncDataBroker.DataChangeScope getDataChangeScope() {
72 return AsyncDataBroker.DataChangeScope.BASE;
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());
82 ElanClusterUtils.runOnlyInLeaderNode(elanName, "updating mcast mac upon tunnel event",
83 new Callable<List<ListenableFuture<Void>>>() {
85 public List<ListenableFuture<Void>> call() throws Exception {
86 return Lists.newArrayList(
87 ElanL2GatewayMulticastUtils.updateRemoteMcastMacOnElanL2GwDevices(elanName));
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());
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());
104 ElanClusterUtils.runOnlyInLeaderNode(elanName, "handling ElanDpnInterface removed",
105 new Callable<List<ListenableFuture<Void>>>() {
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));
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);
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);
143 private String getElanName(InstanceIdentifier<DpnInterfaces> identifier) {
144 return identifier.firstKeyOf(ElanDpnInterfacesList.class).getElanInstanceName();