re base code
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / common / transaction / mngr / RollbackManager.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.mngr;
22
23 import fj.P;
24 import fj.data.Either;
25 import fj.data.HashMap;
26 import fj.data.List;
27 import org.openecomp.sdc.common.transaction.api.RollbackHandler;
28 import org.openecomp.sdc.common.transaction.api.TransactionUtils.DBActionCodeEnum;
29 import org.openecomp.sdc.common.transaction.api.TransactionUtils.DBTypeEnum;
30 import org.openecomp.sdc.common.util.MethodActivationStatusEnum;
31
32 public class RollbackManager {
33     private final HashMap<DBTypeEnum, RollbackHandler> rollbackHandlersMap;
34     private final Integer transactionId;
35     private final String userId;
36     private final String actionType;
37
38     RollbackManager(Integer transactionId, String userId, String actionType, Iterable<RollbackHandler> rollbackHandlers) {
39         this.transactionId = transactionId;
40         this.userId = userId;
41         this.actionType = actionType;
42         this.rollbackHandlersMap = HashMap.from(List.iterableList(rollbackHandlers).map(i -> P.p(i.getDBType(), i)));
43     }
44
45     public DBActionCodeEnum transactionRollback() {
46         List<DBActionCodeEnum> results = rollbackHandlersMap.values().map(RollbackHandler::doRollback);
47         boolean failure = results.exists(r -> r == DBActionCodeEnum.FAIL_GENERAL);
48         return failure ? DBActionCodeEnum.FAIL_GENERAL : DBActionCodeEnum.SUCCESS;
49     }
50
51     protected Either<RollbackHandler, MethodActivationStatusEnum> addRollbackHandler(RollbackHandler rollbackHandler) {
52         Either<RollbackHandler, MethodActivationStatusEnum> result;
53         if (rollbackHandlersMap.contains(rollbackHandler.getDBType())) {
54             result = Either.right(MethodActivationStatusEnum.NOT_ALLOWED);
55         } else {
56             rollbackHandlersMap.set(rollbackHandler.getDBType(), rollbackHandler);
57             result = Either.left(rollbackHandler);
58         }
59         return result;
60
61     }
62
63     protected Either<RollbackHandler, MethodActivationStatusEnum> createRollbackHandler(final DBTypeEnum dbType) {
64
65         RollbackHandler rollbackHandler = new RollbackHandler(transactionId, userId, actionType) {
66
67             @Override
68             public DBTypeEnum getDBType() {
69                 return dbType;
70             }
71
72             @Override
73             protected boolean isRollbackForPersistenceData() {
74                 return true;
75             }
76         };
77         return addRollbackHandler(rollbackHandler);
78     }
79
80     protected Either<RollbackHandler, MethodActivationStatusEnum> getRollbackHandler(DBTypeEnum dbType) {
81         // need to swap here because the uses of Either in SDC appears to be opposite of convention
82         // by convention left is failure; in SDC right is failure
83         return rollbackHandlersMap.get(dbType).toEither(MethodActivationStatusEnum.NOT_FOUND).swap();
84     }
85 }