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