Migrate idmanager to use mdsal-binding-util
[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 package org.opendaylight.genius.idmanager.jobs;
9
10 import static org.opendaylight.mdsal.binding.util.Datastore.CONFIGURATION;
11
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;
31
32 public class UpdateIdEntryJob implements Callable<List<? extends ListenableFuture<?>>> {
33     private static final Logger LOG = LoggerFactory.getLogger(UpdateIdEntryJob.class);
34
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);
37
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;
45
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;
51         this.idKey = idKey;
52         this.txRunner = txRunner;
53         this.idUtils = idUtils;
54         this.lockManager = lockManager;
55         if (newIdValues != null) {
56             this.newIdValues.addAll(newIdValues);
57         }
58     }
59
60     @Override
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);
67             } else {
68                 tx.delete(idUtils.getIdEntriesInstanceIdentifier(parentPoolName, idKey));
69             }
70         });
71         future.addCallback(new FutureCallback<Object>() {
72             @Override
73             public void onSuccess(@Nullable Object result) {
74                 cleanUp();
75             }
76
77             @Override
78             public void onFailure(Throwable throwable) {
79                 cleanUp();
80             }
81         }, EXECUTOR_SERVICE);
82         return Collections.singletonList(future);
83     }
84
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);
90         if (latch != null) {
91             latch.countDown();
92         }
93         // Once the id is written to DS, removing the id value from map.
94         idUtils.removeAllocatedIds(uniqueIdKey);
95         idUtils.unlock(lockManager, uniqueIdKey);
96     }
97 }