Add blueprint wiring for elanmanager
[netvirt.git] / vpnservice / elanmanager / elanmanager-impl / src / main / java / org / opendaylight / netvirt / 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.netvirt.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.common.api.clustering.EntityOwnershipService;
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.netvirt.elan.l2gw.utils.ElanL2GatewayMulticastUtils;
19 import org.opendaylight.netvirt.elan.utils.ElanClusterUtils;
20 import org.opendaylight.netvirt.elan.l2gw.utils.ElanL2GatewayUtils;
21 import org.opendaylight.netvirt.elanmanager.utils.ElanL2GwCacheUtils;
22 import org.opendaylight.genius.datastoreutils.AsyncClusteredDataChangeListenerBase;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.ElanDpnInterfaces;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.dpn.interfaces.ElanDpnInterfacesList;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.dpn.interfaces.elan.dpn.interfaces.list.DpnInterfaces;
26
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
38     private static final Logger LOG = LoggerFactory.getLogger(ElanDpnInterfaceClusteredListener.class);
39
40     private final DataBroker broker;
41     private final EntityOwnershipService entityOwnershipService;
42     private final ElanL2GatewayUtils elanL2GatewayUtils;
43     private final ElanL2GatewayMulticastUtils elanL2GatewayMulticastUtils;
44
45     public ElanDpnInterfaceClusteredListener(DataBroker broker, EntityOwnershipService entityOwnershipService,
46                                              ElanL2GatewayUtils elanL2GatewayUtils,
47                                              ElanL2GatewayMulticastUtils elanL2GatewayMulticastUtils) {
48         super(DpnInterfaces.class, ElanDpnInterfaceClusteredListener.class);
49         this.broker = broker;
50         this.entityOwnershipService = entityOwnershipService;
51         this.elanL2GatewayUtils = elanL2GatewayUtils;
52         this.elanL2GatewayMulticastUtils = elanL2GatewayMulticastUtils;
53     }
54
55     public void init() {
56         registerListener(LogicalDatastoreType.OPERATIONAL, this.broker);
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(entityOwnershipService, 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(entityOwnershipService, 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
147 }