re base code
[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.tosca.CsarUtils;
28 import org.openecomp.sdc.common.log.wrappers.Logger;
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
35 public class ESRollbackHandler extends RollbackHandler {
36
37     private static final Logger log = Logger.getLogger(CsarUtils.class.getName());
38
39     public ESRollbackHandler(Integer transactionId, String userId, String actionType) {
40         super(transactionId, userId, actionType);
41     }
42
43     public DBTypeEnum getDBType() {
44         return DBTypeEnum.ELASTIC_SEARCH;
45     }
46
47     protected boolean isRollbackForPersistenceData() {
48         return true;
49     }
50
51     public boolean isRollbackResultValid(DBActionCodeEnum rollbackResult) {
52         return rollbackResult == DBActionCodeEnum.SUCCESS;
53     }
54
55     public Either<ESAction, MethodActivationStatusEnum> buildEsRollbackAction(ESCatalogDAO esCatalogDao, ESArtifactData artifactData, ESActionTypeEnum esActiontype) {
56         Either<ESAction, MethodActivationStatusEnum> result;
57
58         try {
59             ESAction esRollbackAction = null;
60             Either<ESArtifactData, ResourceUploadStatus> either = esCatalogDao.getArtifact(artifactData.getId());
61
62             switch (esActiontype) {
63             case ADD_ARTIFACT:
64
65                 if (either.isRight() && either.right().value() == ResourceUploadStatus.NOT_EXIST) {
66                     esRollbackAction = new ESAction(esCatalogDao, artifactData, ESActionTypeEnum.REMOVE_ARTIFACT);
67                 }
68                 break;
69             case REMOVE_ARTIFACT:
70                 if (either.isLeft()) {
71                     esRollbackAction = new ESAction(esCatalogDao, artifactData, ESActionTypeEnum.ADD_ARTIFACT);
72                 }
73                 break;
74             case UPDATE_ARTIFACT:
75
76                 if (either.isLeft()) {
77                     ESArtifactData originalArtifactData = either.left().value();
78                     esRollbackAction = new ESAction(esCatalogDao, originalArtifactData, ESActionTypeEnum.UPDATE_ARTIFACT);
79                 }
80                 break;
81
82             }
83             if (esRollbackAction != null) {
84                 result = Either.left(esRollbackAction);
85             } else {
86                 result = Either.right(MethodActivationStatusEnum.FAILED);
87             }
88         } catch (Exception e) {
89             result = Either.right(MethodActivationStatusEnum.FAILED);
90             log.error("#buildEsRollbackAction - {}, es rollback failed with error: ", result, e);
91         }
92
93         return result;
94     }
95
96 }