Freeze upstream versions
[netvirt.git] / elanmanager / impl / src / main / java / org / opendaylight / netvirt / elan / l2gw / jobs / DpnDmacJob.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.Locale;
14 import org.opendaylight.infrautils.jobcoordinator.JobCoordinator;
15 import org.opendaylight.netvirt.elan.cache.ElanInstanceCache;
16 import org.opendaylight.netvirt.elan.l2gw.utils.ElanL2GatewayUtils;
17 import org.opendaylight.netvirt.elan.utils.ElanClusterUtils;
18 import org.opendaylight.netvirt.elan.utils.ElanDmacUtils;
19 import org.opendaylight.netvirt.elan.utils.Scheduler;
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.yang.types.rev130715.MacAddress;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.dpn.interfaces.elan.dpn.interfaces.list.DpnInterfaces;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.instances.ElanInstance;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 public class DpnDmacJob extends DataStoreJob {
29     private static final Logger LOG = LoggerFactory.getLogger(DpnDmacJob.class);
30     private String elanName;
31     private DpnInterfaces dpnInterfaces;
32     private ElanL2GatewayUtils elanL2GatewayUtils;
33     private ElanClusterUtils elanClusterUtils;
34     private ElanInstanceCache elanInstanceCache;
35     private ElanDmacUtils elanDmacUtils;
36     private Scheduler scheduler;
37     private JobCoordinator jobCoordinator;
38     private String nodeId;
39     private boolean added;
40
41     public DpnDmacJob(String elanName,
42                       DpnInterfaces dpnInterfaces,
43                       String nodeId,
44                       boolean added,
45                       ElanL2GatewayUtils elanL2GatewayUtils, ElanClusterUtils elanClusterUtils,
46                       ElanInstanceCache elanInstanceCache, ElanDmacUtils elanDmacUtils,
47                       Scheduler scheduler, JobCoordinator jobCoordinator) {
48         super(elanName + ":l2gwdmac:" + dpnInterfaces.getDpId().toString() + ":" + nodeId,
49                 scheduler, jobCoordinator);
50         this.elanName = elanName;
51         this.dpnInterfaces = dpnInterfaces;
52         this.nodeId = nodeId;
53         this.elanL2GatewayUtils = elanL2GatewayUtils;
54         this.elanClusterUtils = elanClusterUtils;
55         this.elanInstanceCache = elanInstanceCache;
56         this.elanDmacUtils = elanDmacUtils;
57         this.scheduler = scheduler;
58         this.jobCoordinator = jobCoordinator;
59         this.added = added;
60     }
61
62     public void submit() {
63         elanClusterUtils.runOnlyInOwnerNode(super.jobKey,"Dpn Dmac Job", this);
64     }
65
66     @Override
67     public List<? extends ListenableFuture<?>> call() throws Exception {
68         ElanInstance elan = elanInstanceCache.get(elanName).orElse(null);
69         if (elan == null) {
70             LOG.error("failed.elan.not.found. {}", jobKey);
71             return null;
72         }
73         L2GatewayDevice device = ElanL2GwCacheUtils.getL2GatewayDeviceFromCache(elanName, nodeId);
74         List<ListenableFuture<Void>> fts = new ArrayList<>();
75         ElanL2GatewayUtils ucastUtils = elanL2GatewayUtils;
76         if (added) {
77             fts = ucastUtils.installDmacFlowsOnDpn(dpnInterfaces.getDpId(), device, elan,
78                     dpnInterfaces.getInterfaces().get(0));
79         } else {
80             List<MacAddress> localMacs = ucastUtils.getL2GwDeviceLocalMacs(elan.getElanInstanceName(), device);
81             if (localMacs != null && !localMacs.isEmpty()) {
82                 for (MacAddress mac : localMacs) {
83                     fts.addAll(elanDmacUtils.deleteDmacFlowsToExternalMac(elan.getElanTag().longValue(),
84                         dpnInterfaces.getDpId(), nodeId, mac.getValue().toLowerCase(Locale.getDefault())));
85                 }
86             }
87         }
88         if (!fts.isEmpty()) {
89             processResult(fts.get(0));
90         }
91         return null;
92     }
93
94     public static void uninstallDmacFromL2gws(String elanName,
95                                               DpnInterfaces dpnInterfaces,
96                                               ElanL2GatewayUtils elanL2GatewayUtils,
97                                               ElanClusterUtils elanClusterUtils,
98                                               ElanInstanceCache elanInstanceCache,
99                                               ElanDmacUtils elanDmacUtils,
100                                               Scheduler scheduler,
101                                               JobCoordinator jobCoordinator) {
102         ElanL2GwCacheUtils.getInvolvedL2GwDevices(elanName).keySet().forEach(nodeId -> {
103             new DpnDmacJob(elanName, dpnInterfaces, nodeId, false, elanL2GatewayUtils, elanClusterUtils,
104                     elanInstanceCache, elanDmacUtils, scheduler, jobCoordinator).submit();
105         });
106     }
107
108     public static void installDmacFromL2gws(String elanName,
109                                             DpnInterfaces dpnInterfaces,
110                                             ElanL2GatewayUtils elanL2GatewayUtils,
111                                             ElanClusterUtils elanClusterUtils,
112                                             ElanInstanceCache elanInstanceCache,
113                                             ElanDmacUtils elanDmacUtils,
114                                             Scheduler scheduler,
115                                             JobCoordinator jobCoordinator) {
116         ElanL2GwCacheUtils.getInvolvedL2GwDevices(elanName).keySet().forEach(nodeId -> {
117             new DpnDmacJob(elanName, dpnInterfaces, nodeId, true, elanL2GatewayUtils, elanClusterUtils,
118                     elanInstanceCache, elanDmacUtils, scheduler, jobCoordinator).submit();
119         });
120     }
121 }