2a3e9c35e5d3d32d4dd926358ce150a89f57d801
[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 org.slf4j.Logger;
11 import org.slf4j.LoggerFactory;
12
13 import java.util.concurrent.ConcurrentHashMap;
14 import java.util.concurrent.Future;
15
16 /**
17  * This class is a task cache manager that provides a cache to store
18  * the task that is queued for the reconciliation. Whenever new task
19  * is submitted to the reconciliation manager, it will be cached in
20  * the cache. If the reconciliation is successful or it's done with
21  * all the attempt of reconciliation,
22  *
23  * Caching of the task is required, because reconciliation task might
24  * require longer duration to reconcile and there is a possibility that
25  * meanwhile user can change the configuration in config data store while
26  * task is queued for the reconciliation. In that scenario, reconciliation
27  * manager should not attempt any further reconciliation attempt for that
28  * task. ReconciliationManager will call cancelTask() to cancel the task.
29  *
30  * Created by Anil Vishnoi (avishnoi@Brocade.com) on 3/15/16.
31  */
32 class ReconciliationTaskManager {
33     private static final Logger LOG = LoggerFactory.getLogger(ReconciliationTaskManager.class);
34
35     private final ConcurrentHashMap<ReconciliationTask,Future<?>> reconciliationTaskCache
36             = new ConcurrentHashMap();
37
38     public boolean isTaskQueued(ReconciliationTask task) {
39         return reconciliationTaskCache.containsKey(task);
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     public void cacheTask(ReconciliationTask task, Future<?> taskFuture) {
53         reconciliationTaskCache.put(task,taskFuture);
54     }
55 }