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