InfraUtil JobCordinator Changes.
[genius.git] / idmanager / idmanager-impl / src / main / java / org / opendaylight / genius / idmanager / jobs / UpdateIdEntryJob.java
1 /*
2  * Copyright (c) 2016 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
9 package org.opendaylight.genius.idmanager.jobs;
10
11 import static org.opendaylight.genius.infra.Datastore.CONFIGURATION;
12
13 import com.google.common.util.concurrent.FluentFuture;
14 import com.google.common.util.concurrent.FutureCallback;
15 import com.google.common.util.concurrent.ListenableFuture;
16 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
17 import java.util.ArrayList;
18 import java.util.Collections;
19 import java.util.List;
20 import java.util.concurrent.Callable;
21 import java.util.concurrent.CountDownLatch;
22 import java.util.concurrent.ExecutorService;
23 import org.eclipse.jdt.annotation.Nullable;
24 import org.opendaylight.genius.idmanager.IdUtils;
25 import org.opendaylight.genius.infra.ManagedNewTransactionRunner;
26 import org.opendaylight.infrautils.utils.concurrent.Executors;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.id.pools.id.pool.IdEntries;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.lockmanager.rev160413.LockManagerService;
29 import org.opendaylight.yangtools.yang.common.Uint32;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 public class UpdateIdEntryJob implements Callable<List<? extends ListenableFuture<?>>> {
34
35     private static final Logger LOG = LoggerFactory.getLogger(UpdateIdEntryJob.class);
36
37     // static to have total threads globally, not per UpdateIdEntryJob (of which there are many)
38     private static final ExecutorService EXECUTOR_SERVICE = Executors.newFixedThreadPool(5, "UpdateIdEntryJob", LOG);
39
40     private final String parentPoolName;
41     private final String localPoolName;
42     private final String idKey;
43     private final List<Uint32> newIdValues = new ArrayList<>();
44     private final ManagedNewTransactionRunner txRunner;
45     private final IdUtils idUtils;
46     private final LockManagerService lockManager;
47
48     public UpdateIdEntryJob(String parentPoolName, String localPoolName, String idKey,
49             List<Uint32> newIdValues, ManagedNewTransactionRunner txRunner, IdUtils idUtils,
50             LockManagerService lockManager) {
51         this.parentPoolName = parentPoolName;
52         this.localPoolName = localPoolName;
53         this.idKey = idKey;
54         this.txRunner = txRunner;
55         this.idUtils = idUtils;
56         this.lockManager = lockManager;
57         if (newIdValues != null) {
58             this.newIdValues.addAll(newIdValues);
59         }
60     }
61
62     @Override
63     public List<ListenableFuture<Void>> call() {
64         FluentFuture<Void> future = txRunner.callWithNewWriteOnlyTransactionAndSubmit(CONFIGURATION, tx -> {
65             idUtils.updateChildPool(tx, parentPoolName, localPoolName);
66             if (!newIdValues.isEmpty()) {
67                 IdEntries newIdEntry = idUtils.createIdEntries(idKey, newIdValues);
68                 tx.merge(idUtils.getIdEntriesInstanceIdentifier(parentPoolName, idKey), newIdEntry);
69             } else {
70                 tx.delete(idUtils.getIdEntriesInstanceIdentifier(parentPoolName, idKey));
71             }
72         });
73         future.addCallback(new FutureCallback<Void>() {
74             @Override
75             public void onSuccess(@Nullable Void result) {
76                 cleanUp();
77             }
78
79             @Override
80             public void onFailure(Throwable throwable) {
81                 cleanUp();
82             }
83         }, EXECUTOR_SERVICE);
84         return Collections.singletonList(future);
85     }
86
87     @SuppressFBWarnings(value = "UPM_UNCALLED_PRIVATE_METHOD",
88             justification = "https://github.com/spotbugs/spotbugs/issues/811")
89     private void cleanUp() {
90         String uniqueIdKey = idUtils.getUniqueKey(parentPoolName, idKey);
91         CountDownLatch latch = idUtils.getReleaseIdLatch(uniqueIdKey);
92         if (latch != null) {
93             latch.countDown();
94         }
95         // Once the id is written to DS, removing the id value from map.
96         idUtils.removeAllocatedIds(uniqueIdKey);
97         idUtils.unlock(lockManager, uniqueIdKey);
98     }
99 }