Sync Integ to Master
[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 org.openecomp.sdc.common.transaction.api.RollbackHandler;
24 import org.openecomp.sdc.common.transaction.api.TransactionUtils.DBActionCodeEnum;
25 import org.openecomp.sdc.common.transaction.api.TransactionUtils.DBTypeEnum;
26 import org.openecomp.sdc.common.util.MethodActivationStatusEnum;
27
28 import fj.P;
29 import fj.data.Either;
30 import fj.data.HashMap;
31 import fj.data.List;
32
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;
38
39     RollbackManager(Integer transactionId, String userId, String actionType, Iterable<RollbackHandler> rollbackHandlers) {
40         this.transactionId = transactionId;
41         this.userId = userId;
42         this.actionType = actionType;
43         this.rollbackHandlersMap = HashMap.from(List.iterableList(rollbackHandlers).map(i -> P.p(i.getDBType(), i)));
44     }
45
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;
50     }
51
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);
56         } else {
57             rollbackHandlersMap.set(rollbackHandler.getDBType(), rollbackHandler);
58             result = Either.left(rollbackHandler);
59         }
60         return result;
61
62     }
63
64     protected Either<RollbackHandler, MethodActivationStatusEnum> createRollbackHandler(final DBTypeEnum dbType) {
65
66         RollbackHandler rollbackHandler = new RollbackHandler(transactionId, userId, actionType) {
67
68             @Override
69             public DBTypeEnum getDBType() {
70                 return dbType;
71             }
72
73             @Override
74             protected boolean isRollbackForPersistenceData() {
75                 return true;
76             }
77         };
78         return addRollbackHandler(rollbackHandler);
79     }
80
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();
85     }
86 }