Sync Integ to Master
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / components / lifecycle / UndoCheckoutTransition.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.be.components.lifecycle;
22
23 import fj.data.Either;
24
25 import org.openecomp.sdc.be.components.impl.ArtifactsBusinessLogic;
26 import org.openecomp.sdc.be.components.impl.ComponentBusinessLogic;
27 import org.openecomp.sdc.be.config.BeEcompErrorManager;
28 import org.openecomp.sdc.be.dao.api.ActionStatus;
29 import org.openecomp.sdc.be.dao.jsongraph.TitanDao;
30 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
31 import org.openecomp.sdc.be.impl.ComponentsUtils;
32 import org.openecomp.sdc.be.model.Component;
33 import org.openecomp.sdc.be.model.LifeCycleTransitionEnum;
34 import org.openecomp.sdc.be.model.LifecycleStateEnum;
35 import org.openecomp.sdc.be.model.User;
36 import org.openecomp.sdc.be.model.jsontitan.datamodel.ToscaElement;
37 import org.openecomp.sdc.be.model.jsontitan.operations.ToscaElementLifecycleOperation;
38 import org.openecomp.sdc.be.model.jsontitan.operations.ToscaOperationFacade;
39 import org.openecomp.sdc.be.model.jsontitan.utils.ModelConverter;
40 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
41 import org.openecomp.sdc.be.resources.data.auditing.AuditingActionEnum;
42 import org.openecomp.sdc.be.user.Role;
43 import org.openecomp.sdc.exception.ResponseFormat;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
46
47 import java.util.Arrays;
48
49 public class UndoCheckoutTransition extends LifeCycleTransition {
50     private static final Logger log = LoggerFactory.getLogger(CheckoutTransition.class);
51     private ArtifactsBusinessLogic artifactsManager;
52
53     public UndoCheckoutTransition(ComponentsUtils componentUtils, ToscaElementLifecycleOperation lifecycleOperation, ToscaOperationFacade toscaOperationFacade, TitanDao titanDao) {
54         super(componentUtils, lifecycleOperation, toscaOperationFacade,  titanDao);
55
56         // authorized roles
57         Role[] resourceServiceCheckoutRoles = { Role.ADMIN, Role.DESIGNER };
58         Role[] productCheckoutRoles = { Role.ADMIN, Role.PRODUCT_MANAGER };
59         addAuthorizedRoles(ComponentTypeEnum.RESOURCE, Arrays.asList(resourceServiceCheckoutRoles));
60         addAuthorizedRoles(ComponentTypeEnum.SERVICE, Arrays.asList(resourceServiceCheckoutRoles));
61         addAuthorizedRoles(ComponentTypeEnum.PRODUCT, Arrays.asList(productCheckoutRoles));
62
63     }
64
65     @Override
66     public LifeCycleTransitionEnum getName() {
67         return LifeCycleTransitionEnum.UNDO_CHECKOUT;
68     }
69
70     @Override
71     public AuditingActionEnum getAuditingAction() {
72         return AuditingActionEnum.UNDO_CHECKOUT_RESOURCE;
73     }
74
75     public ArtifactsBusinessLogic getArtifactsBusinessLogic() {
76         return artifactsManager;
77     }
78
79     public void setArtifactsBusinessLogic(ArtifactsBusinessLogic artifactsBusinessLogic) {
80         this.artifactsManager = artifactsBusinessLogic;
81     }
82
83     @Override
84     public Either<Boolean, ResponseFormat> validateBeforeTransition(Component component, ComponentTypeEnum componentType, User modifier, User owner, LifecycleStateEnum oldState, LifecycleChangeInfoWithAction lifecycleChangeInfo) {
85         String componentName = component.getComponentMetadataDefinition().getMetadataDataDefinition().getName();
86         log.debug("validate before undo checkout. resource name={}, oldState={}, owner userId={}", componentName, oldState, owner.getUserId());
87
88         // validate user
89         Either<Boolean, ResponseFormat> userValidationResponse = userRoleValidation(modifier,component, componentType, lifecycleChangeInfo);
90         if (userValidationResponse.isRight()) {
91             return userValidationResponse;
92         }
93
94         // check resource is not locked by another user
95         if (!oldState.equals(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT)) {
96             ResponseFormat error = componentUtils.getResponseFormat(ActionStatus.COMPONENT_ALREADY_CHECKED_IN, componentName, componentType.name().toLowerCase(), owner.getFirstName(), owner.getLastName(), owner.getUserId());
97             return Either.right(error);
98         }
99
100         if (!modifier.equals(owner) && !modifier.getRole().equals(Role.ADMIN.name())) {
101             ResponseFormat error = componentUtils.getResponseFormat(ActionStatus.COMPONENT_CHECKOUT_BY_ANOTHER_USER, componentName, componentType.name().toLowerCase(), owner.getFirstName(), owner.getLastName(), owner.getUserId());
102             return Either.right(error);
103         }
104
105         return Either.left(true);
106     }
107
108     @Override
109     public Either<? extends Component, ResponseFormat> changeState(ComponentTypeEnum componentType, Component component, ComponentBusinessLogic componentBl, User modifier, User owner, boolean shouldLock, boolean inTransaction) {
110
111         Either<? extends Component, ResponseFormat> result = null;
112         log.debug("start performing undo-checkout for resource {}", component.getUniqueId());
113
114         try {
115             Either<ToscaElement, StorageOperationStatus> undoCheckoutResourceResult = lifeCycleOperation.undoCheckout(component.getUniqueId());
116
117             if (undoCheckoutResourceResult.isRight()) {
118                 log.debug("checkout failed on graph");
119                 StorageOperationStatus response = undoCheckoutResourceResult.right().value();
120                 ActionStatus actionStatus = componentUtils.convertFromStorageResponse(response);
121                 ResponseFormat responseFormat = componentUtils.getResponseFormatByComponent(actionStatus, component, componentType);
122                 result =  Either.right(responseFormat);
123             }
124             else {
125                 result =  Either.left(ModelConverter.convertFromToscaElement(undoCheckoutResourceResult.left().value()));
126             }
127         } finally {
128             if (result == null || result.isRight()) {
129                 BeEcompErrorManager.getInstance().logBeDaoSystemError("Change LifecycleState - Undo Checkout failed on graph");
130                 log.debug("operation failed. do rollback");
131                 titanDao.rollback();
132             } else {
133                 log.debug("operation success. do commit");
134                 titanDao.commit();
135             }
136         }
137         return result;
138     }
139
140 }