2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.common.transaction.api;
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;
29 import java.util.Stack;
31 public abstract class RollbackHandler implements IDBType {
33 // TODO test using slf4j-test and make this final
34 private static Logger log = LoggerFactory.getLogger(RollbackHandler.class);
35 private Stack<IDBAction> dbActionRollbacks;
37 private Integer transactionId;
38 private String userId, actionType;
40 protected RollbackHandler(Integer transactionId, String userId, String actionType) {
41 if (isRollbackForPersistenceData()) {
42 dbActionRollbacks = new Stack<>();
44 this.transactionId = transactionId;
46 this.actionType = actionType;
49 public MethodActivationStatusEnum addRollbackAction(IDBAction rollbackAction) {
50 MethodActivationStatusEnum result = MethodActivationStatusEnum.SUCCESS;
51 if (isRollbackForPersistenceData()) {
52 dbActionRollbacks.push(rollbackAction);
54 result = MethodActivationStatusEnum.NOT_ALLOWED;
59 public DBActionCodeEnum doRollback() {
60 DBActionCodeEnum result;
63 if (isRollbackForPersistenceData()) {
64 result = doPersistenceDataRollback();
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();
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);
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);
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;
100 * Override for specific logic
104 public <T> boolean isRollbackResultValid(T rollbackResult) {
109 * Override for specific logic
111 public DBActionCodeEnum doNonPersistenceDataRollback() {
112 return DBActionCodeEnum.SUCCESS;
115 protected abstract boolean isRollbackForPersistenceData();
118 * Only Used for Unit Tests !
120 // TODO test using slf4j-test and remove this
121 public static void setLog(Logger log) {
122 RollbackHandler.log = log;