Sync Integ to Master
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / components / lifecycle / CheckinTransition.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.ComponentBusinessLogic;
25 import org.openecomp.sdc.be.config.BeEcompErrorManager;
26 import org.openecomp.sdc.be.dao.api.ActionStatus;
27 import org.openecomp.sdc.be.dao.jsongraph.TitanDao;
28 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
29 import org.openecomp.sdc.be.impl.ComponentsUtils;
30 import org.openecomp.sdc.be.model.Component;
31 import org.openecomp.sdc.be.model.LifeCycleTransitionEnum;
32 import org.openecomp.sdc.be.model.LifecycleStateEnum;
33 import org.openecomp.sdc.be.model.User;
34 import org.openecomp.sdc.be.model.jsontitan.datamodel.ToscaElement;
35 import org.openecomp.sdc.be.model.jsontitan.operations.ToscaElementLifecycleOperation;
36 import org.openecomp.sdc.be.model.jsontitan.operations.ToscaOperationFacade;
37 import org.openecomp.sdc.be.model.jsontitan.utils.ModelConverter;
38 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
39 import org.openecomp.sdc.be.resources.data.auditing.AuditingActionEnum;
40 import org.openecomp.sdc.be.user.Role;
41 import org.openecomp.sdc.exception.ResponseFormat;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
44
45 import java.util.Arrays;
46
47 public class CheckinTransition extends LifeCycleTransition {
48
49     private static final Logger log = LoggerFactory.getLogger(CheckinTransition.class);
50
51     public CheckinTransition(ComponentsUtils componentUtils, ToscaElementLifecycleOperation lifecycleOperation, ToscaOperationFacade toscaOperationFacade, TitanDao titanDao) {
52         super(componentUtils, lifecycleOperation, toscaOperationFacade, titanDao);
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.CHECKIN;
66     }
67
68     @Override
69     public AuditingActionEnum getAuditingAction() {
70         return AuditingActionEnum.CHECKIN_RESOURCE;
71     }
72
73     @Override
74     public Either<? extends Component, ResponseFormat> changeState(ComponentTypeEnum componentType, Component component, ComponentBusinessLogic componentBl, User modifier, User owner, boolean shouldLock, boolean inTransaction) {
75         log.debug("start performing checkin for {} {}", componentType, component.getUniqueId());
76
77         Either<? extends Component, ResponseFormat> result = null;
78         try{
79             Either<ToscaElement, StorageOperationStatus> checkinResourceResult = lifeCycleOperation.
80                     checkinToscaELement(component.getLifecycleState(), component.getUniqueId(), modifier.getUserId(), owner.getUserId());
81
82             if (checkinResourceResult.isRight()) {
83                 log.debug("checkout failed on graph");
84                 StorageOperationStatus response = checkinResourceResult.right().value();
85                 ActionStatus actionStatus = componentUtils.convertFromStorageResponse(response);
86
87                 if (response.equals(StorageOperationStatus.ENTITY_ALREADY_EXISTS)) {
88                     actionStatus = ActionStatus.COMPONENT_VERSION_ALREADY_EXIST;
89                 }
90                 ResponseFormat responseFormat = componentUtils.getResponseFormatByComponent(actionStatus, component, componentType);
91                 result =  Either.right(responseFormat);
92             }
93             else {
94                 result =  Either.left(ModelConverter.convertFromToscaElement(checkinResourceResult.left().value()));
95             }
96         } finally {
97             if (result == null || result.isRight()) {
98                 BeEcompErrorManager.getInstance().logBeDaoSystemError("Change LifecycleState");
99                 if (inTransaction == false) {
100                     log.debug("operation failed. do rollback");
101                     titanDao.rollback();
102                 }
103             } else {
104                 if (inTransaction == false) {
105                     log.debug("operation success. do commit");
106                     titanDao.commit();
107                 }
108             }
109         }
110         return result;
111     }
112
113     @Override
114     public Either<Boolean, ResponseFormat> validateBeforeTransition(Component component, ComponentTypeEnum componentType, User modifier, User owner, LifecycleStateEnum oldState, LifecycleChangeInfoWithAction lifecycleChangeInfo) {
115         String componentName = component.getComponentMetadataDefinition().getMetadataDataDefinition().getName();
116         log.debug("validate before checkin. component name={}, oldState={}, owner userId={}", componentName, oldState, owner.getUserId());
117
118         // validate user
119         Either<Boolean, ResponseFormat> userValidationResponse = userRoleValidation(modifier,component, componentType, lifecycleChangeInfo);
120         if (userValidationResponse.isRight()) {
121             return userValidationResponse;
122         }
123
124         if (!oldState.equals(LifecycleStateEnum.READY_FOR_CERTIFICATION) && !oldState.equals(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT)) {
125             ActionStatus action = ActionStatus.COMPONENT_ALREADY_CHECKED_IN;
126             if (oldState.equals(LifecycleStateEnum.CERTIFICATION_IN_PROGRESS)){
127                 action = ActionStatus.COMPONENT_SENT_FOR_CERTIFICATION;
128             } else if (oldState.equals(LifecycleStateEnum.CERTIFIED)){
129                 action = ActionStatus.COMPONENT_ALREADY_CERTIFIED;
130             }
131             ResponseFormat error = componentUtils.getResponseFormat(action, componentName, componentType.name().toLowerCase(), owner.getFirstName(), owner.getLastName(), owner.getUserId());
132             return Either.right(error);
133         }
134
135         if (oldState.equals(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT) && !modifier.equals(owner) && !modifier.getRole().equals(Role.ADMIN.name())) {
136             ResponseFormat error = componentUtils.getResponseFormat(ActionStatus.COMPONENT_CHECKOUT_BY_ANOTHER_USER, componentName, componentType.name().toLowerCase(), owner.getFirstName(), owner.getLastName(), owner.getUserId());
137             return Either.right(error);
138         }
139
140         if (oldState.equals(LifecycleStateEnum.READY_FOR_CERTIFICATION) && !modifier.equals(owner) && !modifier.getRole().equals(Role.ADMIN.name())) {
141             ResponseFormat error = componentUtils.getResponseFormat(ActionStatus.COMPONENT_SENT_FOR_CERTIFICATION, componentName, componentType.name().toLowerCase(), owner.getFirstName(), owner.getLastName(), owner.getUserId());
142             return Either.right(error);
143         }
144
145         return Either.left(true);
146     }
147 }