Abort power setup if setting gainloss fails
[transportpce.git] / renderer / src / main / java / org / opendaylight / transportpce / renderer / provisiondevice / tasks / RollbackProcessor.java
1 /*
2  * Copyright © 2017 AT&T 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.transportpce.renderer.provisiondevice.tasks;
9
10 import java.util.Deque;
11 import java.util.LinkedList;
12 import org.slf4j.Logger;
13 import org.slf4j.LoggerFactory;
14
15 /**
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.
21  */
22 public class RollbackProcessor {
23
24     private static final Logger LOG = LoggerFactory.getLogger(RollbackProcessor.class);
25
26     private final Deque<RollbackTask> tasks;
27
28     public RollbackProcessor() {
29         this.tasks = new LinkedList<>();
30     }
31
32     /**
33      * Add task to the rollback processor.
34      * @param task the task to add
35      */
36     public void addTask(RollbackTask task) {
37         this.tasks.add(task);
38     }
39
40     /**
41      * Check if any previously added task requires rollback.
42      * Rollback is necessary if just single task requires rollback.
43      * @return
44      *     true if any of added tasks requires rollback. false if none of added tasks requires rollback.
45      */
46     public boolean isRollbackNecessary() {
47         for (RollbackTask task: this.tasks) {
48             if (task.isRollbackNecessary()) {
49                 return true;
50             }
51         }
52         return false;
53     }
54
55     /**
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.
59      * @return
60      *     number of tasks rolled back
61      */
62     @SuppressWarnings("checkstyle:IllegalCatch")
63     public int rollbackAll() {
64         int rollbackCounter = 0;
65         while (this.tasks.size() > 0) {
66             RollbackTask task = this.tasks.pollLast();
67             rollbackCounter++;
68             try {
69                 LOG.info("rolling back: {}", task.getId());
70                 task.call();
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);
74             }
75         }
76         return rollbackCounter;
77     }
78
79     /**
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.
82      * @return
83      *     number of tasks rolled back
84      */
85     public int rollbackAllIfNecessary() {
86         if (!isRollbackNecessary()) {
87             return 0;
88         }
89         return rollbackAll();
90     }
91
92 }