NETVIRT-1630 migrate to md-sal APIs
[netvirt.git] / elanmanager / impl / src / main / java / org / opendaylight / netvirt / elan / l2gw / jobs / BcGroupUpdateJob.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 static org.opendaylight.genius.infra.Datastore.CONFIGURATION;
11
12 import com.google.common.collect.Lists;
13 import com.google.common.util.concurrent.ListenableFuture;
14 import java.util.List;
15 import java.util.Optional;
16 import java.util.concurrent.Callable;
17 import org.opendaylight.genius.infra.ManagedNewTransactionRunner;
18 import org.opendaylight.genius.infra.ManagedNewTransactionRunnerImpl;
19 import org.opendaylight.mdsal.binding.api.DataBroker;
20 import org.opendaylight.netvirt.elan.l2gw.utils.ElanL2GatewayMulticastUtils;
21 import org.opendaylight.netvirt.elan.l2gw.utils.ElanRefUtil;
22 import org.opendaylight.netvirt.elan.utils.ElanUtils;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.instances.ElanInstance;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 public class BcGroupUpdateJob implements Callable<List<? extends ListenableFuture<?>>> {
28
29     private static final Logger LOG = LoggerFactory.getLogger("HwvtepEventLogger");
30
31     private final String elanName;
32     private final ElanL2GatewayMulticastUtils elanL2GatewayMulticastUtils;
33     private final ElanRefUtil elanRefUtil;
34     private final ManagedNewTransactionRunner txRunner;
35     protected String jobKey;
36     private final boolean createCase;
37
38     public BcGroupUpdateJob(String elanName,
39                             ElanRefUtil elanRefUtil,
40                             ElanL2GatewayMulticastUtils elanL2GatewayMulticastUtils,
41                             DataBroker dataBroker, boolean createCase) {
42         this.jobKey = ElanUtils.getBcGroupUpdateKey(elanName);
43         this.txRunner = new ManagedNewTransactionRunnerImpl(dataBroker);
44         this.elanName = elanName;
45         this.elanRefUtil = elanRefUtil;
46         this.elanL2GatewayMulticastUtils = elanL2GatewayMulticastUtils;
47         this.createCase = createCase;
48     }
49
50     public void submit() {
51         elanRefUtil.getElanClusterUtils().runOnlyInOwnerNode(this.jobKey, "BC Group Update Job", this);
52     }
53
54     @Override
55     public List<ListenableFuture<Void>> call() throws Exception {
56         Optional<ElanInstance> elanInstanceOptional =  elanRefUtil.getElanInstanceCache().get(elanName);
57         if (elanInstanceOptional.isPresent()) {
58             return Lists.newArrayList(txRunner.callWithNewWriteOnlyTransactionAndSubmit(CONFIGURATION,
59                 confTx -> elanL2GatewayMulticastUtils.updateRemoteBroadcastGroupForAllElanDpns(
60                         elanInstanceOptional.get(), createCase, confTx)));
61         }
62         return null;
63     }
64
65     public static void updateAllBcGroups(String elanName,
66                                          ElanRefUtil elanRefUtil,
67                                          ElanL2GatewayMulticastUtils elanL2GatewayMulticastUtils,
68                                          DataBroker dataBroker, boolean createCase) {
69         new BcGroupUpdateJob(elanName, elanRefUtil, elanL2GatewayMulticastUtils, dataBroker, createCase).submit();
70     }
71 }