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.mngr;
24 import fj.data.Either;
25 import fj.data.HashMap;
28 import org.openecomp.sdc.common.transaction.api.RollbackHandler;
29 import org.openecomp.sdc.common.transaction.api.TransactionUtils.DBActionCodeEnum;
30 import org.openecomp.sdc.common.transaction.api.TransactionUtils.DBTypeEnum;
31 import org.openecomp.sdc.common.util.MethodActivationStatusEnum;
33 public class RollbackManager {
34 private final HashMap<DBTypeEnum, RollbackHandler> rollbackHandlersMap;
35 private final Integer transactionId;
36 private final String userId;
37 private final String actionType;
39 RollbackManager(Integer transactionId, String userId, String actionType, Iterable<RollbackHandler> rollbackHandlers) {
40 this.transactionId = transactionId;
42 this.actionType = actionType;
43 this.rollbackHandlersMap = HashMap.from(List.iterableList(rollbackHandlers).map(i -> P.p(i.getDBType(), i)));
46 public DBActionCodeEnum transactionRollback() {
47 List<DBActionCodeEnum> results = rollbackHandlersMap.values().map(RollbackHandler::doRollback);
48 boolean failure = results.exists(r -> r == DBActionCodeEnum.FAIL_GENERAL);
49 return failure ? DBActionCodeEnum.FAIL_GENERAL : DBActionCodeEnum.SUCCESS;
52 protected Either<RollbackHandler, MethodActivationStatusEnum> addRollbackHandler(RollbackHandler rollbackHandler) {
53 Either<RollbackHandler, MethodActivationStatusEnum> result;
54 if (rollbackHandlersMap.contains(rollbackHandler.getDBType())) {
55 result = Either.right(MethodActivationStatusEnum.NOT_ALLOWED);
57 rollbackHandlersMap.set(rollbackHandler.getDBType(), rollbackHandler);
58 result = Either.left(rollbackHandler);
64 protected Either<RollbackHandler, MethodActivationStatusEnum> createRollbackHandler(final DBTypeEnum dbType) {
66 RollbackHandler rollbackHandler = new RollbackHandler(transactionId, userId, actionType) {
69 public DBTypeEnum getDBType() {
74 protected boolean isRollbackForPersistenceData() {
78 return addRollbackHandler(rollbackHandler);
81 protected Either<RollbackHandler, MethodActivationStatusEnum> getRollbackHandler(DBTypeEnum dbType) {
82 // need to swap here because the uses of Either in SDC appears to be opposite of convention
83 // by convention left is failure; in SDC right is failure
84 return rollbackHandlersMap.get(dbType).toEither(MethodActivationStatusEnum.NOT_FOUND).swap();