2 * Copyright (c) 2016 Ericsson India Global Services Pvt Ltd. and others. All rights reserved.
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
8 package org.opendaylight.genius.idmanager.jobs;
10 import static org.opendaylight.mdsal.binding.util.Datastore.CONFIGURATION;
12 import com.google.common.util.concurrent.FluentFuture;
13 import com.google.common.util.concurrent.FutureCallback;
14 import com.google.common.util.concurrent.ListenableFuture;
15 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
16 import java.util.ArrayList;
17 import java.util.Collections;
18 import java.util.List;
19 import java.util.concurrent.Callable;
20 import java.util.concurrent.CountDownLatch;
21 import java.util.concurrent.ExecutorService;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.opendaylight.genius.idmanager.IdUtils;
24 import org.opendaylight.infrautils.utils.concurrent.Executors;
25 import org.opendaylight.mdsal.binding.util.ManagedNewTransactionRunner;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.id.pools.id.pool.IdEntries;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.lockmanager.rev160413.LockManagerService;
28 import org.opendaylight.yangtools.yang.common.Uint32;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
32 public class UpdateIdEntryJob implements Callable<List<? extends ListenableFuture<?>>> {
33 private static final Logger LOG = LoggerFactory.getLogger(UpdateIdEntryJob.class);
35 // static to have total threads globally, not per UpdateIdEntryJob (of which there are many)
36 private static final ExecutorService EXECUTOR_SERVICE = Executors.newFixedThreadPool(5, "UpdateIdEntryJob", LOG);
38 private final String parentPoolName;
39 private final String localPoolName;
40 private final String idKey;
41 private final List<Uint32> newIdValues = new ArrayList<>();
42 private final ManagedNewTransactionRunner txRunner;
43 private final IdUtils idUtils;
44 private final LockManagerService lockManager;
46 public UpdateIdEntryJob(String parentPoolName, String localPoolName, String idKey,
47 List<Uint32> newIdValues, ManagedNewTransactionRunner txRunner, IdUtils idUtils,
48 LockManagerService lockManager) {
49 this.parentPoolName = parentPoolName;
50 this.localPoolName = localPoolName;
52 this.txRunner = txRunner;
53 this.idUtils = idUtils;
54 this.lockManager = lockManager;
55 if (newIdValues != null) {
56 this.newIdValues.addAll(newIdValues);
61 public List<? extends ListenableFuture<?>> call() {
62 FluentFuture<?> future = txRunner.callWithNewWriteOnlyTransactionAndSubmit(CONFIGURATION, tx -> {
63 idUtils.updateChildPool(tx, parentPoolName, localPoolName);
64 if (!newIdValues.isEmpty()) {
65 IdEntries newIdEntry = idUtils.createIdEntries(idKey, newIdValues);
66 tx.merge(idUtils.getIdEntriesInstanceIdentifier(parentPoolName, idKey), newIdEntry);
68 tx.delete(idUtils.getIdEntriesInstanceIdentifier(parentPoolName, idKey));
71 future.addCallback(new FutureCallback<Object>() {
73 public void onSuccess(@Nullable Object result) {
78 public void onFailure(Throwable throwable) {
82 return Collections.singletonList(future);
85 @SuppressFBWarnings(value = "UPM_UNCALLED_PRIVATE_METHOD",
86 justification = "https://github.com/spotbugs/spotbugs/issues/811")
87 private void cleanUp() {
88 String uniqueIdKey = idUtils.getUniqueKey(parentPoolName, idKey);
89 CountDownLatch latch = idUtils.getReleaseIdLatch(uniqueIdKey);
93 // Once the id is written to DS, removing the id value from map.
94 idUtils.removeAllocatedIds(uniqueIdKey);
95 idUtils.unlock(lockManager, uniqueIdKey);