[SDC-29] rebase continue work to align source
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / components / lifecycle / CheckoutTransition.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 java.util.Arrays;
24 import java.util.Collection;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.stream.Collectors;
28
29 import org.openecomp.sdc.be.components.impl.ComponentBusinessLogic;
30 import org.openecomp.sdc.be.config.BeEcompErrorManager;
31 import org.openecomp.sdc.be.dao.api.ActionStatus;
32 import org.openecomp.sdc.be.dao.jsongraph.TitanDao;
33 import org.openecomp.sdc.be.dao.jsongraph.types.EdgeLabelEnum;
34 import org.openecomp.sdc.be.dao.jsongraph.types.VertexTypeEnum;
35 import org.openecomp.sdc.be.datatypes.elements.ComponentInstanceDataDefinition;
36 import org.openecomp.sdc.be.datatypes.elements.MapPropertiesDataDefinition;
37 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
38 import org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields;
39 import org.openecomp.sdc.be.datatypes.enums.OriginTypeEnum;
40 import org.openecomp.sdc.be.impl.ComponentsUtils;
41 import org.openecomp.sdc.be.model.Component;
42 import org.openecomp.sdc.be.model.InputDefinition;
43 import org.openecomp.sdc.be.model.LifeCycleTransitionEnum;
44 import org.openecomp.sdc.be.model.LifecycleStateEnum;
45 import org.openecomp.sdc.be.model.User;
46 import org.openecomp.sdc.be.model.jsontitan.datamodel.TopologyTemplate;
47 import org.openecomp.sdc.be.model.jsontitan.datamodel.ToscaElement;
48 import org.openecomp.sdc.be.model.jsontitan.datamodel.ToscaElementTypeEnum;
49 import org.openecomp.sdc.be.model.jsontitan.operations.ToscaElementLifecycleOperation;
50 import org.openecomp.sdc.be.model.jsontitan.operations.ToscaElementOperation;
51 import org.openecomp.sdc.be.model.jsontitan.operations.ToscaOperationFacade;
52 import org.openecomp.sdc.be.model.jsontitan.utils.ModelConverter;
53 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
54 import org.openecomp.sdc.be.resources.data.auditing.AuditingActionEnum;
55 import org.openecomp.sdc.be.user.Role;
56 import org.openecomp.sdc.exception.ResponseFormat;
57 import org.slf4j.Logger;
58 import org.slf4j.LoggerFactory;
59
60 import fj.data.Either;
61
62 public class CheckoutTransition extends LifeCycleTransition {
63
64         private static Logger log = LoggerFactory.getLogger(CheckoutTransition.class.getName());
65
66         public CheckoutTransition(ComponentsUtils componentUtils, ToscaElementLifecycleOperation lifecycleOperation, ToscaOperationFacade toscaOperationFacade, TitanDao titanDao) {
67                 super(componentUtils, lifecycleOperation, toscaOperationFacade, titanDao);
68
69                 // authorized roles
70                 Role[] resourceServiceCheckoutRoles = { Role.ADMIN, Role.DESIGNER };
71                 Role[] productCheckoutRoles = { Role.ADMIN, Role.PRODUCT_MANAGER };
72                 addAuthorizedRoles(ComponentTypeEnum.RESOURCE, Arrays.asList(resourceServiceCheckoutRoles));
73                 addAuthorizedRoles(ComponentTypeEnum.SERVICE, Arrays.asList(resourceServiceCheckoutRoles));
74                 addAuthorizedRoles(ComponentTypeEnum.PRODUCT, Arrays.asList(productCheckoutRoles));
75
76         }
77
78         @Override
79         public LifeCycleTransitionEnum getName() {
80                 return LifeCycleTransitionEnum.CHECKOUT;
81         }
82
83         @Override
84         public AuditingActionEnum getAuditingAction() {
85                 return AuditingActionEnum.CHECKOUT_RESOURCE;
86         }
87
88         @Override
89         public Either<? extends Component, ResponseFormat> changeState(ComponentTypeEnum componentType, Component component, ComponentBusinessLogic componentBl, User modifier, User owner, boolean shouldLock, boolean inTransaction) {
90
91                 log.debug("start performing {} for resource {}", getName().name(), component.getUniqueId());
92
93                 Either<? extends Component, ResponseFormat> result = null;
94                 try {
95
96                         Either<ToscaElement, StorageOperationStatus> checkoutResourceResult = lifeCycleOperation.checkoutToscaElement(component.getUniqueId(), modifier.getUserId(), owner.getUserId());
97
98                         if (checkoutResourceResult.isRight()) {
99                                 log.debug("checkout failed on graph");
100                                 StorageOperationStatus response = checkoutResourceResult.right().value();
101                                 ActionStatus actionStatus = componentUtils.convertFromStorageResponse(response);
102
103                                 if (response.equals(StorageOperationStatus.ENTITY_ALREADY_EXISTS)) {
104                                         actionStatus = ActionStatus.COMPONENT_VERSION_ALREADY_EXIST;
105                                 }
106                                 ResponseFormat responseFormat = componentUtils.getResponseFormatByComponent(actionStatus, component, componentType);
107                                 result = Either.right(responseFormat);
108                         } else {
109                         
110                                 Component clonedComponent = ModelConverter.convertFromToscaElement(checkoutResourceResult.left().value());
111                                 result = Either.left(clonedComponent); 
112                                 Either<Boolean, ResponseFormat> upgradeToLatestGeneric = componentBl.shouldUpgradeToLatestGeneric(clonedComponent);
113                                 if (upgradeToLatestGeneric.isRight())
114                                         result = Either.right(upgradeToLatestGeneric.right().value());
115                                 else if (upgradeToLatestGeneric.left().value()) {
116                                         StorageOperationStatus response = upgradeToLatestGenericData(clonedComponent);
117                                         if (StorageOperationStatus.OK != response) {
118                                                 ActionStatus actionStatus = componentUtils.convertFromStorageResponse(response);
119                                                 ResponseFormat responseFormat = componentUtils.getResponseFormatByComponent(actionStatus, component, componentType);
120                                                 result = Either.right(responseFormat);
121                                         }
122                                 }
123
124                         }
125
126                 } finally {
127                         if (result == null || result.isRight()) {
128                                 BeEcompErrorManager.getInstance().logBeDaoSystemError("Change LifecycleState");
129                                 if (inTransaction == false) {
130                                         log.debug("operation failed. do rollback");
131                                         titanDao.rollback();
132                                 }
133                         } else {
134                                 if (inTransaction == false) {
135                                         log.debug("operation success. do commit");
136                                         titanDao.commit();
137                                 }
138                         }
139                 }
140                 return result;
141         }
142
143         private StorageOperationStatus upgradeToLatestGenericData(Component clonedComponent) {
144                 
145                 StorageOperationStatus updateStatus = StorageOperationStatus.OK;
146                 Either<Component, StorageOperationStatus> updateEither = toscaOperationFacade.updateToscaElement(clonedComponent);
147                 if (updateEither.isRight())
148                         updateStatus = updateEither.right().value();  
149                 else if (clonedComponent.shouldGenerateInputs()) {
150                         List<InputDefinition> newInputs = clonedComponent.getInputs();
151                         updateStatus = lifeCycleOperation.updateToscaDataOfToscaElement(clonedComponent.getUniqueId(), EdgeLabelEnum.INPUTS, VertexTypeEnum.INPUTS, newInputs, JsonPresentationFields.NAME);
152                 }
153                 return updateStatus;
154         }
155
156         @Override
157         public Either<Boolean, ResponseFormat> validateBeforeTransition(Component component, ComponentTypeEnum componentType, User modifier, User owner, LifecycleStateEnum oldState, LifecycleChangeInfoWithAction lifecycleChangeInfo) {
158                 String componentName = component.getComponentMetadataDefinition().getMetadataDataDefinition().getName();
159                 log.debug("validate before checkout. resource name={}, oldState={}, owner userId={}", componentName, oldState, owner.getUserId());
160
161                 // validate user
162                 Either<Boolean, ResponseFormat> userValidationResponse = userRoleValidation(modifier, component, componentType, lifecycleChangeInfo);
163                 if (userValidationResponse.isRight()) {
164                         return userValidationResponse;
165                 }
166
167                 // Disabled as of 1604 patch after discussing with Ella/Eli/Michael
168
169                 /*
170                  * if (componentType == ComponentTypeEnum.PRODUCT){ Either<Boolean, ResponseFormat> productContactsEither = productContactsValidation((Product)component, modifier); if (productContactsEither.isRight()){ return productContactsEither; } }
171                  */
172
173                 // check resource is not locked by another user
174                 if (oldState.equals(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT)) {
175                         ResponseFormat error = componentUtils.getResponseFormat(ActionStatus.COMPONENT_IN_CHECKOUT_STATE, componentName, componentType.name().toLowerCase(), owner.getFirstName(), owner.getLastName(), owner.getUserId());
176                         return Either.right(error);
177                 }
178
179                 if (oldState.equals(LifecycleStateEnum.CERTIFICATION_IN_PROGRESS)) {
180                         ResponseFormat error = componentUtils.getResponseFormat(ActionStatus.COMPONENT_IN_CERT_IN_PROGRESS_STATE, componentName, componentType.name().toLowerCase(), owner.getFirstName(), owner.getLastName(), owner.getUserId());
181                         return Either.right(error);
182                 }
183
184                 if (oldState.equals(LifecycleStateEnum.READY_FOR_CERTIFICATION)) {
185                         if (!modifier.getRole().equals(Role.DESIGNER.name()) && !modifier.getRole().equals(Role.ADMIN.name())) {
186                                 ResponseFormat error = componentUtils.getResponseFormat(ActionStatus.COMPONENT_SENT_FOR_CERTIFICATION, componentName, componentType.name().toLowerCase(), owner.getFirstName(), owner.getLastName(), owner.getUserId());
187                                 return Either.right(error);
188                         }
189                 }
190                 return Either.left(true);
191         }
192
193 }