Migrate idmanager to use mdsal-binding-util
[genius.git] / idmanager / idmanager-impl / src / main / java / org / opendaylight / genius / idmanager / jobs / CleanUpJob.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.common.api.LogicalDatastoreType.CONFIGURATION;
11
12 import com.google.common.util.concurrent.ListenableFuture;
13 import java.util.Collections;
14 import java.util.List;
15 import java.util.Optional;
16 import java.util.concurrent.Callable;
17 import java.util.concurrent.ExecutionException;
18 import org.opendaylight.genius.datastoreutils.SingleTransactionDataBroker;
19 import org.opendaylight.genius.idmanager.IdLocalPool;
20 import org.opendaylight.genius.idmanager.IdManagerException;
21 import org.opendaylight.genius.idmanager.IdUtils;
22 import org.opendaylight.genius.idmanager.ReleasedIdHolder;
23 import org.opendaylight.infrautils.jobcoordinator.JobCoordinator;
24 import org.opendaylight.mdsal.binding.api.DataBroker;
25 import org.opendaylight.mdsal.binding.util.ManagedNewTransactionRunner;
26 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
27 import org.opendaylight.mdsal.common.api.TransactionCommitFailedException;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.id.pools.id.pool.ReleasedIdsHolder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.id.pools.id.pool.ReleasedIdsHolderBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.lockmanager.rev160413.LockManagerService;
31 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 public class CleanUpJob implements Callable<List<? extends ListenableFuture<?>>> {
36     private static final Logger LOG = LoggerFactory.getLogger(CleanUpJob.class);
37
38     private final IdLocalPool idLocalPool;
39     private final ManagedNewTransactionRunner txRunner;
40     private final DataBroker broker;
41     private final String parentPoolName;
42     private final int blockSize;
43     private final LockManagerService lockManager;
44     private final IdUtils idUtils;
45     private final JobCoordinator jobCoordinator;
46
47     public CleanUpJob(IdLocalPool idLocalPool, ManagedNewTransactionRunner txRunner, DataBroker broker,
48             String parentPoolName, int blockSize,
49             LockManagerService lockManager, IdUtils idUtils, JobCoordinator jobCoordinator) {
50         this.idLocalPool = idLocalPool;
51         this.txRunner = txRunner;
52         this.broker = broker;
53         this.parentPoolName = parentPoolName;
54         this.blockSize = blockSize;
55         this.lockManager = lockManager;
56         this.idUtils = idUtils;
57         this.jobCoordinator = jobCoordinator;
58     }
59
60     @Override
61     public List<ListenableFuture<Void>> call() throws Exception {
62         cleanupExcessIds();
63         return Collections.emptyList();
64     }
65
66     private void cleanupExcessIds()
67             throws IdManagerException, TransactionCommitFailedException, ExecutionException , InterruptedException {
68         // We can update the availableCount here... and update it in DS using IdHolderSyncJob
69         long totalAvailableIdCount = idLocalPool.getAvailableIds().getAvailableIdCount()
70                 + idLocalPool.getReleasedIds().getAvailableIdCount();
71         if (totalAvailableIdCount > blockSize * 2) {
72             if (LOG.isDebugEnabled()) {
73                 LOG.debug("Condition for cleanUp Satisfied for localPool {} - totalAvailableIdCount {}",
74                         idLocalPool, totalAvailableIdCount);
75             }
76             String parentPoolNameIntern = parentPoolName.intern();
77             InstanceIdentifier<ReleasedIdsHolder> releasedIdInstanceIdentifier
78                     = idUtils.getReleasedIdsHolderInstance(parentPoolNameIntern);
79             // We need lock manager because maybe one cluster tries to read the
80             // available ids from the global pool while the other is writing. We
81             // cannot rely on DSJC because that is not cluster-aware
82             try {
83                 idUtils.lock(lockManager, parentPoolNameIntern);
84                 Optional<ReleasedIdsHolder> releasedIdsHolder = SingleTransactionDataBroker.syncReadOptional(broker,
85                         CONFIGURATION, releasedIdInstanceIdentifier);
86                 if (!releasedIdsHolder.isPresent()) {
87                     LOG.error("ReleasedIds not present in parent pool. Unable to cleanup excess ids");
88                     return;
89                 }
90                 if (LOG.isDebugEnabled()) {
91                     LOG.debug("Releasing excesss Ids from local pool");
92                 }
93                 ReleasedIdHolder releasedIds = (ReleasedIdHolder) idLocalPool.getReleasedIds();
94                 ReleasedIdsHolderBuilder releasedIdsParent = new ReleasedIdsHolderBuilder(releasedIdsHolder.get());
95                 idUtils.freeExcessAvailableIds(releasedIds, releasedIdsParent, totalAvailableIdCount - blockSize * 2);
96                 IdHolderSyncJob job = new IdHolderSyncJob(idLocalPool.getPoolName(), releasedIds, txRunner, idUtils);
97                 jobCoordinator.enqueueJob(idLocalPool.getPoolName(), job, IdUtils.RETRY_COUNT);
98                 SingleTransactionDataBroker.syncWrite(broker, LogicalDatastoreType.CONFIGURATION,
99                         releasedIdInstanceIdentifier, releasedIdsParent.build());
100             } finally {
101                 idUtils.unlock(lockManager, parentPoolNameIntern);
102             }
103         }
104     }
105 }