Sync Integ to Master
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / common / transaction / impl / ESRollbackHandler.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.impl;
22
23 import fj.data.Either;
24 import org.openecomp.sdc.be.dao.api.ResourceUploadStatus;
25 import org.openecomp.sdc.be.dao.impl.ESCatalogDAO;
26 import org.openecomp.sdc.be.resources.data.ESArtifactData;
27 import org.openecomp.sdc.be.resources.exception.ResourceDAOException;
28 import org.openecomp.sdc.be.tosca.CsarUtils;
29 import org.openecomp.sdc.common.transaction.api.RollbackHandler;
30 import org.openecomp.sdc.common.transaction.api.TransactionUtils.DBActionCodeEnum;
31 import org.openecomp.sdc.common.transaction.api.TransactionUtils.DBTypeEnum;
32 import org.openecomp.sdc.common.transaction.api.TransactionUtils.ESActionTypeEnum;
33 import org.openecomp.sdc.common.util.MethodActivationStatusEnum;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 public class ESRollbackHandler extends RollbackHandler {
38
39     private static final Logger log = LoggerFactory.getLogger(CsarUtils.class);
40
41     public ESRollbackHandler(Integer transactionId, String userId, String actionType) {
42         super(transactionId, userId, actionType);
43     }
44
45     public DBTypeEnum getDBType() {
46         return DBTypeEnum.ELASTIC_SEARCH;
47     }
48
49     protected boolean isRollbackForPersistenceData() {
50         return true;
51     }
52
53     public boolean isRollbackResultValid(DBActionCodeEnum rollbackResult) {
54         return rollbackResult == DBActionCodeEnum.SUCCESS;
55     }
56
57     public Either<ESAction, MethodActivationStatusEnum> buildEsRollbackAction(ESCatalogDAO esCatalogDao, ESArtifactData artifactData, ESActionTypeEnum esActiontype) {
58         Either<ESAction, MethodActivationStatusEnum> result;
59
60         try {
61             ESAction esRollbackAction = null;
62             Either<ESArtifactData, ResourceUploadStatus> either = esCatalogDao.getArtifact(artifactData.getId());
63
64             switch (esActiontype) {
65             case ADD_ARTIFACT:
66
67                 if (either.isRight() && either.right().value() == ResourceUploadStatus.NOT_EXIST) {
68                     esRollbackAction = new ESAction(esCatalogDao, artifactData, ESActionTypeEnum.REMOVE_ARTIFACT);
69                 }
70                 break;
71             case REMOVE_ARTIFACT:
72                 if (either.isLeft()) {
73                     esRollbackAction = new ESAction(esCatalogDao, artifactData, ESActionTypeEnum.ADD_ARTIFACT);
74                 }
75                 break;
76             case UPDATE_ARTIFACT:
77
78                 if (either.isLeft()) {
79                     ESArtifactData originalArtifactData = either.left().value();
80                     esRollbackAction = new ESAction(esCatalogDao, originalArtifactData, ESActionTypeEnum.UPDATE_ARTIFACT);
81                 }
82                 break;
83
84             }
85             if (esRollbackAction != null) {
86                 result = Either.left(esRollbackAction);
87             } else {
88                 result = Either.right(MethodActivationStatusEnum.FAILED);
89             }
90         } catch (Exception e) {
91             result = Either.right(MethodActivationStatusEnum.FAILED);
92             log.error("#buildEsRollbackAction - {}, es rollback failed with error: ", result, e);
93         }
94
95         return result;
96     }
97
98 }