ovsdb enable checkstyle on error
[ovsdb.git] / southbound / southbound-impl / src / main / java / org / opendaylight / ovsdb / southbound / reconciliation / ReconciliationTaskManager.java
1 /*
2  * Copyright (c) 2016 Brocade Communications Systems, Inc. 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.ovsdb.southbound.reconciliation;
9
10 import java.util.concurrent.ConcurrentHashMap;
11 import java.util.concurrent.Future;
12 import org.slf4j.Logger;
13 import org.slf4j.LoggerFactory;
14
15 /**
16  * This class is a task cache manager that provides a cache to store
17  * the task that is queued for the reconciliation. Whenever new task
18  * is submitted to the reconciliation manager, it will be cached in
19  * the cache. If the reconciliation is successful or it's done with
20  * all the attempt of reconciliation,
21  * <p>
22  * Caching of the task is required, because reconciliation task might
23  * require longer duration to reconcile and there is a possibility that
24  * meanwhile user can change the configuration in config data store while
25  * task is queued for the reconciliation. In that scenario, reconciliation
26  * manager should not attempt any further reconciliation attempt for that
27  * task. ReconciliationManager will call cancelTask() to cancel the task.
28  * </p>
29  * Created by Anil Vishnoi (avishnoi@Brocade.com) on 3/15/16.
30  */
31 class ReconciliationTaskManager {
32     private static final Logger LOG = LoggerFactory.getLogger(ReconciliationTaskManager.class);
33
34     private final ConcurrentHashMap<ReconciliationTask,Future<?>> reconciliationTaskCache
35             = new ConcurrentHashMap();
36
37     public boolean isTaskQueued(ReconciliationTask task) {
38         return reconciliationTaskCache.containsKey(task);
39     }
40
41     public boolean cancelTask(ReconciliationTask task) {
42         if (reconciliationTaskCache.containsKey(task)) {
43             Future<?> taskFuture = reconciliationTaskCache.remove(task);
44             if (!taskFuture.isDone() && !taskFuture.isCancelled()) {
45                 LOG.info("Reconciliation task is cancelled : {}", task);
46                 return taskFuture.cancel(true);
47             }
48         }
49         return false;
50
51     }
52
53     public void cacheTask(ReconciliationTask task, Future<?> taskFuture) {
54         reconciliationTaskCache.put(task,taskFuture);
55     }
56 }