f96445c41f5ecf353460608d989ea7f4f027cc72
[netvirt.git] / elanmanager / impl / src / main / java / org / opendaylight / netvirt / elan / l2gw / jobs / McastUpdateJob.java
1 /*
2  * Copyright (c) 2019 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.jobs;
9
10 import com.google.common.util.concurrent.ListenableFuture;
11 import java.util.ArrayList;
12 import java.util.List;
13 import java.util.concurrent.Callable;
14 import org.opendaylight.netvirt.elan.l2gw.utils.ElanL2GatewayMulticastUtils;
15 import org.opendaylight.netvirt.elan.utils.ElanClusterUtils;
16 import org.opendaylight.netvirt.elan.utils.ElanItmUtils;
17 import org.opendaylight.netvirt.elan.utils.ElanUtils;
18 import org.opendaylight.netvirt.elanmanager.utils.ElanL2GwCacheUtils;
19 import org.opendaylight.netvirt.neutronvpn.api.l2gw.L2GatewayDevice;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
21 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
22 import org.opendaylight.yangtools.yang.common.Uint64;
23
24 public class McastUpdateJob implements Callable<List<? extends ListenableFuture<?>>> {
25     private String elanName;
26     private String nodeId;
27     private ElanL2GatewayMulticastUtils mcastUtils;
28     private ElanClusterUtils elanClusterUtils;
29     boolean add;
30     protected String jobKey;
31     private IpAddress removedDstTep;
32     private boolean dpnOrConnectionRemoved;
33
34     public McastUpdateJob(String elanName,
35                           String nodeId,
36                           boolean add,
37                           ElanL2GatewayMulticastUtils mcastUtils,
38                           ElanClusterUtils elanClusterUtils) {
39         this.jobKey = ElanUtils.getBcGroupUpdateKey(elanName);
40         this.elanName = elanName;
41         this.nodeId = nodeId;
42         this.mcastUtils = mcastUtils;
43         this.add = add;
44         this.elanClusterUtils = elanClusterUtils;
45     }
46
47     public void submit() {
48         elanClusterUtils.runOnlyInOwnerNode(this.jobKey, "Mcast Update job",this);
49     }
50
51     @Override
52     public List<ListenableFuture<?>> call() throws Exception {
53         L2GatewayDevice device = ElanL2GwCacheUtils.getL2GatewayDeviceFromCache(elanName, nodeId);
54         ListenableFuture<?> ft = null;
55         //TODO: make prepareRemoteMcastMacUpdateOnDevice return a ListenableFuture<Void>
56         if (add) {
57             ft = mcastUtils.prepareRemoteMcastMacUpdateOnDevice(elanName, device, !dpnOrConnectionRemoved ,
58                     removedDstTep);
59         } else {
60             ft =  mcastUtils.deleteRemoteMcastMac(new NodeId(nodeId), elanName);
61         }
62         List<ListenableFuture<?>> fts = new ArrayList<>();
63         fts.add(ft);
64         return fts;
65     }
66
67     public static void updateAllMcasts(String elanName,
68                                        ElanL2GatewayMulticastUtils mcastUtils,
69                                        ElanClusterUtils elanClusterUtils) {
70         ElanL2GwCacheUtils.getInvolvedL2GwDevices(elanName).forEach(device -> {
71             new McastUpdateJob(elanName, device.getHwvtepNodeId(), true, mcastUtils,
72                     elanClusterUtils).submit();
73         });
74     }
75
76     public static void removeMcastForNode(String elanName, String nodeId,
77                                           ElanL2GatewayMulticastUtils mcastUtils,
78                                           ElanClusterUtils elanClusterUtils) {
79         new McastUpdateJob(elanName, nodeId, false, mcastUtils,
80                 elanClusterUtils).submit();
81     }
82
83     public static void updateMcastForNode(String elanName, String nodeId,
84                                           ElanL2GatewayMulticastUtils mcastUtils,
85                                           ElanClusterUtils elanClusterUtils) {
86         new McastUpdateJob(elanName, nodeId, true, mcastUtils,
87                 elanClusterUtils).submit();
88     }
89
90     private McastUpdateJob setRemovedDstTep(IpAddress removedDstTep) {
91         this.removedDstTep = removedDstTep;
92         return this;
93     }
94
95     private McastUpdateJob setDpnOrconnectionRemoved() {
96         this.dpnOrConnectionRemoved = true;
97         return this;
98     }
99
100     public static void updateAllMcastsForConnectionAdd(String elanName,
101                                                        ElanL2GatewayMulticastUtils mcastUtils,
102                                                        ElanClusterUtils elanClusterUtils) {
103         ElanL2GwCacheUtils.getInvolvedL2GwDevices(elanName).forEach(device -> {
104             new McastUpdateJob(elanName, device.getHwvtepNodeId(), true , mcastUtils, elanClusterUtils).submit();
105         });
106     }
107
108     public static void updateAllMcastsForConnectionDelete(String elanName,
109                                                           ElanL2GatewayMulticastUtils mcastUtils,
110                                                           ElanClusterUtils elanClusterUtils,
111                                                           L2GatewayDevice deletedDevice) {
112         ElanL2GwCacheUtils.getInvolvedL2GwDevices(elanName).forEach(device -> {
113             IpAddress deletedTep = deletedDevice.getTunnelIp();
114             new McastUpdateJob(elanName, device.getHwvtepNodeId(), true , mcastUtils, elanClusterUtils)
115                     .setDpnOrconnectionRemoved()
116                     .setRemovedDstTep(deletedTep)
117                     .submit();
118         });
119     }
120
121     public static void updateAllMcastsForDpnAdd(String elanName,
122                                                 ElanL2GatewayMulticastUtils mcastUtils,
123                                                 ElanClusterUtils elanClusterUtils) {
124         ElanL2GwCacheUtils.getInvolvedL2GwDevices(elanName).forEach(device -> {
125             new McastUpdateJob(elanName, device.getHwvtepNodeId(), true , mcastUtils, elanClusterUtils).submit();
126         });
127     }
128
129     public static void updateAllMcastsForDpnDelete(String elanName,
130                                                    ElanL2GatewayMulticastUtils mcastUtils,
131                                                    ElanClusterUtils elanClusterUtils,
132                                                    Uint64 srcDpnId,
133                                                    ElanItmUtils elanItmUtils) {
134         ElanL2GwCacheUtils.getInvolvedL2GwDevices(elanName).forEach(device -> {
135             IpAddress deletedTep = elanItmUtils.getSourceDpnTepIp(srcDpnId, new NodeId(device.getHwvtepNodeId()));
136             new McastUpdateJob(elanName, device.getHwvtepNodeId(), true , mcastUtils, elanClusterUtils)
137                     .setDpnOrconnectionRemoved()
138                     .setRemovedDstTep(deletedTep)
139                     .submit();
140         });
141     }
142
143 }