Sync Integ to Master
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / common / transaction / api / RollbackHandler.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.sdc.common.transaction.api;
22
23 import org.openecomp.sdc.common.transaction.api.TransactionUtils.DBActionCodeEnum;
24 import org.openecomp.sdc.common.transaction.api.TransactionUtils.LogMessages;
25 import org.openecomp.sdc.common.util.MethodActivationStatusEnum;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 import java.util.Stack;
30
31 public abstract class RollbackHandler implements IDBType {
32
33     // TODO test using slf4j-test and make this final
34     private static Logger log = LoggerFactory.getLogger(RollbackHandler.class);
35     private Stack<IDBAction> dbActionRollbacks;
36
37     private Integer transactionId;
38     private String userId, actionType;
39
40     protected RollbackHandler(Integer transactionId, String userId, String actionType) {
41         if (isRollbackForPersistenceData()) {
42             dbActionRollbacks = new Stack<>();
43         }
44         this.transactionId = transactionId;
45         this.userId = userId;
46         this.actionType = actionType;
47     }
48
49     public MethodActivationStatusEnum addRollbackAction(IDBAction rollbackAction) {
50         MethodActivationStatusEnum result = MethodActivationStatusEnum.SUCCESS;
51         if (isRollbackForPersistenceData()) {
52             dbActionRollbacks.push(rollbackAction);
53         } else {
54             result = MethodActivationStatusEnum.NOT_ALLOWED;
55         }
56         return result;
57     }
58
59     public DBActionCodeEnum doRollback() {
60         DBActionCodeEnum result;
61
62         try {
63             if (isRollbackForPersistenceData()) {
64                 result = doPersistenceDataRollback();
65             } else {
66                 log.debug(LogMessages.ROLLBACK_NON_PERSISTENT_ACTION, getDBType().name(), transactionId, userId, actionType);
67                 log.debug(TransactionUtils.TRANSACTION_MARKER, LogMessages.ROLLBACK_NON_PERSISTENT_ACTION, getDBType().name(), transactionId, userId, actionType);
68                 result = doNonPersistenceDataRollback();
69             }
70             if (result != DBActionCodeEnum.SUCCESS) {
71                 log.error(LogMessages.ROLLBACK_FAILED_ON_DB, transactionId, getDBType().name(), userId, actionType);
72                 log.error(TransactionUtils.TRANSACTION_MARKER, LogMessages.ROLLBACK_FAILED_ON_DB, transactionId, getDBType().name(), userId, actionType);
73             }
74
75         } catch (Exception e) {
76             result = DBActionCodeEnum.FAIL_GENERAL;
77             log.error(LogMessages.ROLLBACK_FAILED_ON_DB_WITH_EXCEPTION, transactionId, getDBType().name(), e.getMessage(), userId, actionType);
78             log.debug(LogMessages.ROLLBACK_FAILED_ON_DB_WITH_EXCEPTION, transactionId, getDBType().name(), e.getMessage(), userId, actionType, e);
79             log.error(TransactionUtils.TRANSACTION_MARKER, LogMessages.ROLLBACK_FAILED_ON_DB_WITH_EXCEPTION, transactionId, getDBType().name(), e.getMessage(), userId, actionType);
80         }
81
82         return result;
83     }
84
85     private <T> DBActionCodeEnum doPersistenceDataRollback() {
86         DBActionCodeEnum result = DBActionCodeEnum.SUCCESS;
87         while (!dbActionRollbacks.empty()) {
88             log.debug(LogMessages.ROLLBACK_PERSISTENT_ACTION, getDBType().name(), transactionId, userId, actionType);
89             log.debug(TransactionUtils.TRANSACTION_MARKER, LogMessages.ROLLBACK_PERSISTENT_ACTION, getDBType().name(), transactionId, userId, actionType);
90             IDBAction rollbackAction = dbActionRollbacks.pop();
91             T rollbackResult = rollbackAction.doAction();
92             if (!isRollbackResultValid(rollbackResult)) {
93                 result = DBActionCodeEnum.FAIL_GENERAL;
94             }
95         }
96         return result;
97     }
98
99     /**
100      * Override for specific logic
101      *
102      * @param <T>
103      */
104     public <T> boolean isRollbackResultValid(T rollbackResult) {
105         return true;
106     }
107
108     /**
109      * Override for specific logic
110      */
111     public DBActionCodeEnum doNonPersistenceDataRollback() {
112         return DBActionCodeEnum.SUCCESS;
113     }
114
115     protected abstract boolean isRollbackForPersistenceData();
116
117     /**
118      * Only Used for Unit Tests !
119      */
120     // TODO test using slf4j-test and remove this
121     public static void setLog(Logger log) {
122         RollbackHandler.log = log;
123     }
124 }