re base code
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / common / transaction / mngr / CommitManager.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.be.config.BeEcompErrorManager;
24 import org.openecomp.sdc.common.log.wrappers.Logger;
25 import org.openecomp.sdc.common.transaction.api.ICommitHandler;
26 import org.openecomp.sdc.common.transaction.api.TransactionUtils;
27 import org.openecomp.sdc.common.transaction.api.TransactionUtils.DBActionCodeEnum;
28 import org.openecomp.sdc.common.transaction.api.TransactionUtils.LogMessages;
29
30 import java.util.List;
31
32 public class CommitManager {
33
34     // TODO test using slf4j-test and make this final
35     private static Logger log = Logger.getLogger(CommitManager.class);
36     private List<ICommitHandler> commitHandlers;
37     private Integer transactionId;
38     private String userId, actionType;
39
40     CommitManager(Integer transactionId, String userId, String actionType, List<ICommitHandler> commitHandlers) {
41         this.commitHandlers = commitHandlers;
42         this.transactionId = transactionId;
43         this.userId = userId;
44         this.actionType = actionType;
45
46     }
47
48     public DBActionCodeEnum transactionCommit() {
49         log.debug(LogMessages.COMMIT_ACTION_ALL_DB, transactionId, userId, actionType);
50         DBActionCodeEnum commitResult = DBActionCodeEnum.SUCCESS;
51         ICommitHandler lastHandler = null;
52         try {
53             for (ICommitHandler handler : commitHandlers) {
54                 lastHandler = handler;
55                 log.debug(LogMessages.COMMIT_ACTION_SPECIFIC_DB, transactionId, handler.getDBType().name(), userId, actionType);
56                 DBActionCodeEnum commitCode = handler.doCommit();
57                 if (commitCode == DBActionCodeEnum.FAIL_GENERAL) {
58                     BeEcompErrorManager.getInstance().logBeSystemError("transactionCommit on DB " + handler.getDBType().name());
59                     log.debug("Commit failed for SdncTransactionID:{} on DB:{}", transactionId, handler.getDBType().name());
60                     commitResult = DBActionCodeEnum.FAIL_GENERAL;
61                     break;
62                 }
63                 log.debug("Commit succeeded for SdncTransactionID:{} on DB:{}", transactionId, handler.getDBType().name());
64             }
65         } catch (Exception e) {
66             BeEcompErrorManager.getInstance().logBeSystemError("transactionCommit - on DB " + getDBName(lastHandler));
67             log.debug("Commit failed for SdncTransactionID:{} on DB:{}, Exception message:{}", transactionId, getDBName(lastHandler), e.getMessage(), e);
68             log.info(TransactionUtils.TRANSACTION_MARKER, "Commit failed for SdncTransactionID:{} on DB:{}", transactionId, getDBName(lastHandler));
69             commitResult = DBActionCodeEnum.FAIL_GENERAL;
70         }
71         return commitResult;
72     }
73
74     private String getDBName(ICommitHandler lastHandler) {
75         String dbName = "Unknown";
76         if (lastHandler != null) {
77             dbName = lastHandler.getDBType().name();
78         }
79         return dbName;
80     }
81
82     // TODO test using slf4j-test and remove this
83     static void setLog(Logger log) {
84         CommitManager.log = log;
85     }
86 }