2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.be.components.lifecycle;
23 import java.util.Arrays;
24 import java.util.Collection;
25 import java.util.List;
27 import java.util.stream.Collectors;
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;
60 import fj.data.Either;
62 public class CheckoutTransition extends LifeCycleTransition {
64 private static Logger log = LoggerFactory.getLogger(CheckoutTransition.class.getName());
66 public CheckoutTransition(ComponentsUtils componentUtils, ToscaElementLifecycleOperation lifecycleOperation, ToscaOperationFacade toscaOperationFacade, TitanDao titanDao) {
67 super(componentUtils, lifecycleOperation, toscaOperationFacade, titanDao);
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));
79 public LifeCycleTransitionEnum getName() {
80 return LifeCycleTransitionEnum.CHECKOUT;
84 public AuditingActionEnum getAuditingAction() {
85 return AuditingActionEnum.CHECKOUT_RESOURCE;
89 public Either<? extends Component, ResponseFormat> changeState(ComponentTypeEnum componentType, Component component, ComponentBusinessLogic componentBl, User modifier, User owner, boolean shouldLock, boolean inTransaction) {
91 log.debug("start performing {} for resource {}", getName().name(), component.getUniqueId());
93 Either<? extends Component, ResponseFormat> result = null;
96 Either<ToscaElement, StorageOperationStatus> checkoutResourceResult = lifeCycleOperation.checkoutToscaElement(component.getUniqueId(), modifier.getUserId(), owner.getUserId());
98 if (checkoutResourceResult.isRight()) {
99 log.debug("checkout failed on graph");
100 StorageOperationStatus response = checkoutResourceResult.right().value();
101 ActionStatus actionStatus = componentUtils.convertFromStorageResponse(response);
103 if (response.equals(StorageOperationStatus.ENTITY_ALREADY_EXISTS)) {
104 actionStatus = ActionStatus.COMPONENT_VERSION_ALREADY_EXIST;
106 ResponseFormat responseFormat = componentUtils.getResponseFormatByComponent(actionStatus, component, componentType);
107 result = Either.right(responseFormat);
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);
127 if (result == null || result.isRight()) {
128 BeEcompErrorManager.getInstance().logBeDaoSystemError("Change LifecycleState");
129 if (inTransaction == false) {
130 log.debug("operation failed. do rollback");
134 if (inTransaction == false) {
135 log.debug("operation success. do commit");
143 private StorageOperationStatus upgradeToLatestGenericData(Component clonedComponent) {
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);
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());
162 Either<Boolean, ResponseFormat> userValidationResponse = userRoleValidation(modifier, component, componentType, lifecycleChangeInfo);
163 if (userValidationResponse.isRight()) {
164 return userValidationResponse;
167 // Disabled as of 1604 patch after discussing with Ella/Eli/Michael
170 * if (componentType == ComponentTypeEnum.PRODUCT){ Either<Boolean, ResponseFormat> productContactsEither = productContactsValidation((Product)component, modifier); if (productContactsEither.isRight()){ return productContactsEither; } }
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);
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);
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);
190 return Either.left(true);