2 * Copyright © 2017 AT&T and others. All rights reserved.
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
8 package org.opendaylight.transportpce.renderer.provisiondevice.tasks;
10 import java.util.Deque;
11 import java.util.LinkedList;
13 import org.slf4j.Logger;
14 import org.slf4j.LoggerFactory;
17 * This class collects tasks for later rollback.
18 * This implementation is not thread safe, it must be called from single orchestration thread.
19 * Rollback order is: last added task is rolled back first.
20 * After rollback, each task is removed from rollback processor.
21 * All rollback tasks are executed in single thread.
23 public class RollbackProcessor {
25 private static final Logger LOG = LoggerFactory.getLogger(RollbackProcessor.class);
27 private final Deque<RollbackTask> tasks;
29 public RollbackProcessor() {
30 this.tasks = new LinkedList<>();
34 * Add task to the rollback processor.
35 * @param task the task to add
37 public void addTask(RollbackTask task) {
42 * Check if any previously added task requires rollback.
43 * Rollback is necessary if just single task requires rollback.
45 * true if any of added tasks requires rollback. false if none of added tasks requires rollback.
47 public boolean isRollbackNecessary() {
48 for (RollbackTask task: this.tasks) {
49 if (task.isRollbackNecessary()) {
57 * Rollback all tasks previously added to this processor.
58 * It does not matter if any of the tasks requires rollback.
59 * All previously added tasks will be rolled back and removed from this processor.
61 * number of tasks rolled back
63 @SuppressWarnings("checkstyle:IllegalCatch")
64 public int rollbackAll() {
65 int rollbackCounter = 0;
66 while (this.tasks.size() > 0) {
67 RollbackTask task = this.tasks.pollLast();
70 LOG.info("rolling back: {}", task.getId());
72 //this method prototype only uses the generic Exception but no specific and useable subclass
73 } catch (Exception e) {
74 LOG.error("ERROR: Rollback task {} has failed", task.getId(), e);
77 return rollbackCounter;
81 * Rollback all tasks in case any task has failed.
82 * If rollback is necessary, all previously added tasks will be rolled back and removed from this processor.
84 * number of tasks rolled back
86 public int rollbackAllIfNecessary() {
87 if (!isRollbackNecessary()) {