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