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