Network topology and inventory init
[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
13 import org.slf4j.Logger;
14 import org.slf4j.LoggerFactory;
15
16 /**
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.
22  */
23 public class RollbackProcessor {
24
25     private static final Logger LOG = LoggerFactory.getLogger(RollbackProcessor.class);
26
27     private final Deque<RollbackTask> tasks;
28
29     public RollbackProcessor() {
30         this.tasks = new LinkedList<>();
31     }
32
33     /**
34      * Add task to the rollback processor.
35      * @param task the task to add
36      */
37     public void addTask(RollbackTask task) {
38         this.tasks.add(task);
39     }
40
41     /**
42      * Check if any previously added task requires rollback.
43      * Rollback is necessary if just single task requires rollback.
44      * @return
45      *   true if any of added tasks requires rollback. false if none of added tasks requires rollback.
46      */
47     public boolean isRollbackNecessary() {
48         for (RollbackTask task: this.tasks) {
49             if (task.isRollbackNecessary()) {
50                 return true;
51             }
52         }
53         return false;
54     }
55
56     /**
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.
60      * @return
61      *   number of tasks rolled back
62      */
63     @SuppressWarnings("checkstyle:IllegalCatch")
64     public int rollbackAll() {
65         int rollbackCounter = 0;
66         while (this.tasks.size() > 0) {
67             RollbackTask task = this.tasks.pollLast();
68             rollbackCounter++;
69             try {
70                 LOG.info("rolling back: {}", task.getId());
71                 task.call();
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);
75             }
76         }
77         return rollbackCounter;
78     }
79
80     /**
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.
83      * @return
84      *   number of tasks rolled back
85      */
86     public int rollbackAllIfNecessary() {
87         if (!isRollbackNecessary()) {
88             return 0;
89         }
90         return rollbackAll();
91     }
92
93 }