4e1ca5e9e19b61ba42ff36670c35a6087de3802a
[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.Collection;
13 import java.util.List;
14 import java.util.Locale;
15 import java.util.concurrent.Callable;
16 import org.opendaylight.netvirt.elan.l2gw.utils.ElanL2GatewayUtils;
17 import org.opendaylight.netvirt.elan.l2gw.utils.ElanRefUtil;
18 import org.opendaylight.netvirt.elan.utils.ElanDmacUtils;
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.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 implements Callable<List<? extends ListenableFuture<?>>> {
29     private static final Logger LOG = LoggerFactory.getLogger(DpnDmacJob.class);
30
31     private String elanName;
32     private DpnInterfaces dpnInterfaces;
33     private ElanL2GatewayUtils elanL2GatewayUtils;
34     private ElanDmacUtils elanDmacUtils;
35     private final ElanRefUtil elanRefUtil;
36     private String nodeId;
37     private boolean added;
38     protected String jobKey;
39
40     public DpnDmacJob(String elanName,
41                       DpnInterfaces dpnInterfaces,
42                       String nodeId,
43                       boolean added,
44                       ElanL2GatewayUtils elanL2GatewayUtils, ElanRefUtil elanRefUtil,
45                       ElanDmacUtils elanDmacUtils) {
46         this.jobKey = ElanUtils.getBcGroupUpdateKey(elanName);
47         this.elanName = elanName;
48         this.dpnInterfaces = dpnInterfaces;
49         this.nodeId = nodeId;
50         this.elanL2GatewayUtils = elanL2GatewayUtils;
51         this.elanRefUtil = elanRefUtil;
52         this.elanDmacUtils = elanDmacUtils;
53         this.added = added;
54     }
55
56     public void submit() {
57         elanRefUtil.getElanClusterUtils().runOnlyInOwnerNode(this.jobKey,"Dpn Dmac Job", this);
58     }
59
60     @Override
61     public List<ListenableFuture<Void>> call() throws Exception {
62         ElanInstance elan = elanRefUtil.getElanInstanceCache().get(elanName).orElse(null);
63         if (elan == null) {
64             LOG.error("failed.elan.not.found.{}", jobKey);
65             return null;
66         }
67         List<ListenableFuture<Void>> result = new ArrayList<>();
68         L2GatewayDevice device = ElanL2GwCacheUtils.getL2GatewayDeviceFromCache(elanName, nodeId);
69         if (added) {
70             result.addAll(elanL2GatewayUtils.installDmacFlowsOnDpn(dpnInterfaces.getDpId(), device, elan,
71                     dpnInterfaces.getInterfaces().get(0)));
72         } else {
73             Collection<MacAddress> localMacs = elanL2GatewayUtils.getL2GwDeviceLocalMacs(
74                     elan.getElanInstanceName(), device);
75             if (localMacs != null && !localMacs.isEmpty()) {
76                 for (MacAddress mac : localMacs) {
77                     result.addAll(elanDmacUtils.deleteDmacFlowsToExternalMac(elan.getElanTag().toJava(),
78                             dpnInterfaces.getDpId(), nodeId, mac.getValue().toLowerCase(Locale.getDefault())));
79                 }
80             }
81         }
82         return result;
83     }
84
85     public static void uninstallDmacFromL2gws(String elanName,
86                                               DpnInterfaces dpnInterfaces,
87                                               ElanL2GatewayUtils elanL2GatewayUtils,
88                                               ElanRefUtil elanRefUtil,
89                                               ElanDmacUtils elanDmacUtils) {
90         ElanL2GwCacheUtils.getInvolvedL2GwDevices(elanName).forEach(l2GatewayDevice -> {
91             new DpnDmacJob(elanName, dpnInterfaces, l2GatewayDevice.getHwvtepNodeId(), false, elanL2GatewayUtils,
92                     elanRefUtil, elanDmacUtils).submit();
93         });
94     }
95
96     public static void installDmacFromL2gws(String elanName,
97                                             DpnInterfaces dpnInterfaces,
98                                             ElanL2GatewayUtils elanL2GatewayUtils,
99                                             ElanRefUtil elanRefUtil,
100                                             ElanDmacUtils elanDmacUtils) {
101         ElanL2GwCacheUtils.getInvolvedL2GwDevices(elanName).forEach(l2GatewayDevice -> {
102             new DpnDmacJob(elanName, dpnInterfaces, l2GatewayDevice.getHwvtepNodeId(), true, elanL2GatewayUtils,
103                     elanRefUtil, elanDmacUtils).submit();
104         });
105     }
106 }