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