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;
12 import org.slf4j.Logger;
13 import org.slf4j.LoggerFactory;
16 * This class collects tasks for later rollback.
17 * This implementation is not thread safe, it must be called from single orchestration thread.
18 * Rollback order is: last added task is rolled back first.
19 * After rollback, each task is removed from rollback processor.
20 * All rollback tasks are executed in single thread.
22 public class RollbackProcessor {
24 private static final Logger LOG = LoggerFactory.getLogger(RollbackProcessor.class);
26 private final Deque<RollbackTask> tasks;
28 public RollbackProcessor() {
29 this.tasks = new LinkedList<>();
33 * Add task to the rollback processor.
34 * @param task the task to add
36 public void addTask(RollbackTask task) {
41 * Check if any previously added task requires rollback.
42 * Rollback is necessary if just single task requires rollback.
44 * true if any of added tasks requires rollback. false if none of added tasks requires rollback.
46 public boolean isRollbackNecessary() {
47 for (RollbackTask task: this.tasks) {
48 if (task.isRollbackNecessary()) {
56 * Rollback all tasks previously added to this processor.
57 * It does not matter if any of the tasks requires rollback.
58 * All previously added tasks will be rolled back and removed from this processor.
60 * number of tasks rolled back
62 @SuppressWarnings("checkstyle:IllegalCatch")
63 public int rollbackAll() {
64 int rollbackCounter = 0;
65 while (this.tasks.size() > 0) {
66 RollbackTask task = this.tasks.pollLast();
69 LOG.info("rolling back: {}", task.getId());
71 //this method prototype only uses the generic Exception but no specific and useable subclass
72 } catch (Exception e) {
73 LOG.error("ERROR: Rollback task {} has failed", task.getId(), e);
76 return rollbackCounter;
80 * Rollback all tasks in case any task has failed.
81 * If rollback is necessary, all previously added tasks will be rolled back and removed from this processor.
83 * number of tasks rolled back
85 public int rollbackAllIfNecessary() {
86 if (!isRollbackNecessary()) {