re base code
[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.datatypes.enums.ResourceTypeEnum;
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.jsontitan.datamodel.ToscaElement;
36 import org.openecomp.sdc.be.model.jsontitan.datamodel.ToscaElementTypeEnum;
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.tosca.ToscaUtils;
43 import org.openecomp.sdc.be.user.Role;
44 import org.openecomp.sdc.common.log.wrappers.Logger;
45 import org.openecomp.sdc.exception.ResponseFormat;
46
47 import java.util.Arrays;
48
49 public class CheckinTransition extends LifeCycleTransition {
50
51     private static final Logger log = Logger.getLogger(CheckinTransition.class);
52
53     public CheckinTransition(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.CHECKIN;
68     }
69
70     @Override
71     public AuditingActionEnum getAuditingAction() {
72         return AuditingActionEnum.CHECKIN_RESOURCE;
73     }
74
75     @Override
76     public Either<? extends Component, ResponseFormat> changeState(ComponentTypeEnum componentType, Component component, ComponentBusinessLogic componentBl, User modifier, User owner, boolean shouldLock, boolean inTransaction) {
77         log.debug("start performing checkin for {} {}", componentType, component.getUniqueId());
78
79         Either<? extends Component, ResponseFormat> result = null;
80         try{
81             Either<ToscaElement, StorageOperationStatus> checkinResourceResult = lifeCycleOperation.
82                     checkinToscaELement(component.getLifecycleState(), component.getUniqueId(), modifier.getUserId(), owner.getUserId());
83
84             if (checkinResourceResult.isRight()) {
85                 log.debug("checkout failed on graph");
86                 StorageOperationStatus response = checkinResourceResult.right().value();
87                 ActionStatus actionStatus = componentUtils.convertFromStorageResponse(response);
88
89                 if (response.equals(StorageOperationStatus.ENTITY_ALREADY_EXISTS)) {
90                     actionStatus = ActionStatus.COMPONENT_VERSION_ALREADY_EXIST;
91                 }
92                 ResponseFormat responseFormat = componentUtils.getResponseFormatByComponent(actionStatus, component, componentType);
93                 result =  Either.right(responseFormat);
94             }
95             else {
96                 updateCalculatedCapabilitiesRequirements(checkinResourceResult.left().value());
97                 result =  Either.left(ModelConverter.convertFromToscaElement(checkinResourceResult.left().value()));
98             }
99         } finally {
100             if (result == null || result.isRight()) {
101                 BeEcompErrorManager.getInstance().logBeDaoSystemError("Change LifecycleState");
102                 if (!inTransaction) {
103                     log.debug("operation failed. do rollback");
104                     titanDao.rollback();
105                 }
106             } else {
107                 if (!inTransaction) {
108                     log.debug("operation success. do commit");
109                     titanDao.commit();
110                 }
111             }
112         }
113         return result;
114     }
115
116     @Override
117     public Either<Boolean, ResponseFormat> validateBeforeTransition(Component component, ComponentTypeEnum componentType, User modifier, User owner, LifecycleStateEnum oldState, LifecycleChangeInfoWithAction lifecycleChangeInfo) {
118         String componentName = component.getComponentMetadataDefinition().getMetadataDataDefinition().getName();
119         log.debug("validate before checkin. component name={}, oldState={}, owner userId={}", componentName, oldState, owner.getUserId());
120
121         // validate user
122         Either<Boolean, ResponseFormat> userValidationResponse = userRoleValidation(modifier,component, componentType, lifecycleChangeInfo);
123         if (userValidationResponse.isRight()) {
124             return userValidationResponse;
125         }
126
127         if (!oldState.equals(LifecycleStateEnum.READY_FOR_CERTIFICATION) && !oldState.equals(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT)) {
128             ActionStatus action = ActionStatus.COMPONENT_ALREADY_CHECKED_IN;
129             if (oldState.equals(LifecycleStateEnum.CERTIFICATION_IN_PROGRESS)){
130                 action = ActionStatus.COMPONENT_SENT_FOR_CERTIFICATION;
131             } else if (oldState.equals(LifecycleStateEnum.CERTIFIED)){
132                 action = ActionStatus.COMPONENT_ALREADY_CERTIFIED;
133             }
134             ResponseFormat error = componentUtils.getResponseFormat(action, componentName, componentType.name().toLowerCase(), owner.getFirstName(), owner.getLastName(), owner.getUserId());
135             return Either.right(error);
136         }
137
138         if (oldState.equals(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT) && !modifier.getUserId().equals(owner.getUserId()) && !modifier.getRole().equals(Role.ADMIN.name())) {
139             ResponseFormat error = componentUtils.getResponseFormat(ActionStatus.COMPONENT_CHECKOUT_BY_ANOTHER_USER, componentName, componentType.name().toLowerCase(), owner.getFirstName(), owner.getLastName(), owner.getUserId());
140             return Either.right(error);
141         }
142
143         if (oldState.equals(LifecycleStateEnum.READY_FOR_CERTIFICATION) && !modifier.equals(owner) && !modifier.getRole().equals(Role.ADMIN.name())) {
144             ResponseFormat error = componentUtils.getResponseFormat(ActionStatus.COMPONENT_SENT_FOR_CERTIFICATION, componentName, componentType.name().toLowerCase(), owner.getFirstName(), owner.getLastName(), owner.getUserId());
145             return Either.right(error);
146         }
147
148         return Either.left(true);
149     }
150
151     private void updateCalculatedCapabilitiesRequirements(ToscaElement toscaElement) {
152         if(toscaElement.getToscaType() == ToscaElementTypeEnum.TOPOLOGY_TEMPLATE && toscaElement.getResourceType() != ResourceTypeEnum.CVFC){
153             toscaOperationFacade.updateNamesOfCalculatedCapabilitiesRequirements(toscaElement.getUniqueId());
154         }
155     }
156 }