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;
25 import org.openecomp.sdc.be.components.impl.ArtifactsBusinessLogic;
26 import org.openecomp.sdc.be.components.impl.ComponentBusinessLogic;
27 import org.openecomp.sdc.be.config.BeEcompErrorManager;
28 import org.openecomp.sdc.be.dao.api.ActionStatus;
29 import org.openecomp.sdc.be.dao.jsongraph.TitanDao;
30 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
31 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
32 import org.openecomp.sdc.be.impl.ComponentsUtils;
33 import org.openecomp.sdc.be.model.Component;
34 import org.openecomp.sdc.be.model.LifeCycleTransitionEnum;
35 import org.openecomp.sdc.be.model.LifecycleStateEnum;
36 import org.openecomp.sdc.be.model.User;
37 import org.openecomp.sdc.be.model.jsontitan.datamodel.ToscaElement;
38 import org.openecomp.sdc.be.model.jsontitan.operations.ToscaElementLifecycleOperation;
39 import org.openecomp.sdc.be.model.jsontitan.operations.ToscaOperationFacade;
40 import org.openecomp.sdc.be.model.jsontitan.utils.ModelConverter;
41 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
42 import org.openecomp.sdc.be.resources.data.auditing.AuditingActionEnum;
43 import org.openecomp.sdc.be.user.Role;
44 import org.openecomp.sdc.exception.ResponseFormat;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
48 import fj.data.Either;
50 public class CertificationChangeTransition extends LifeCycleTransition {
52 private static Logger log = LoggerFactory.getLogger(CertificationChangeTransition.class.getName());
54 private LifecycleStateEnum nextState;
55 private LifeCycleTransitionEnum name;
56 private AuditingActionEnum auditingAction;
57 private ArtifactsBusinessLogic artifactsManager;
59 public CertificationChangeTransition(LifeCycleTransitionEnum name, ComponentsUtils componentUtils, ToscaElementLifecycleOperation lifecycleOperation, ToscaOperationFacade toscaOperationFacade, TitanDao titanDao) {
60 super(componentUtils, lifecycleOperation, toscaOperationFacade, titanDao);
65 Role[] certificationChangeRoles = { Role.ADMIN, Role.TESTER };
66 addAuthorizedRoles(ComponentTypeEnum.RESOURCE, Arrays.asList(certificationChangeRoles));
67 addAuthorizedRoles(ComponentTypeEnum.SERVICE, Arrays.asList(certificationChangeRoles));
68 // TODO to be later defined for product
70 //additional authorized roles for resource type
71 Role[] resourceRoles = { Role.DESIGNER};
72 addResouceAuthorizedRoles(ResourceTypeEnum.VFCMT, Arrays.asList(resourceRoles));
76 this.auditingAction = AuditingActionEnum.CERTIFICATION_SUCCESS_RESOURCE;
77 this.nextState = LifecycleStateEnum.CERTIFIED;
79 case FAIL_CERTIFICATION:
80 this.auditingAction = AuditingActionEnum.FAIL_CERTIFICATION_RESOURCE;
81 nextState = LifecycleStateEnum.NOT_CERTIFIED_CHECKIN;
83 case CANCEL_CERTIFICATION:
84 this.auditingAction = AuditingActionEnum.CANCEL_CERTIFICATION_RESOURCE;
85 nextState = LifecycleStateEnum.READY_FOR_CERTIFICATION;
94 public LifeCycleTransitionEnum getName() {
99 public AuditingActionEnum getAuditingAction() {
100 return auditingAction;
103 public ArtifactsBusinessLogic getArtifactsManager() {
104 return artifactsManager;
107 public void setArtifactsManager(ArtifactsBusinessLogic artifactsManager) {
108 this.artifactsManager = artifactsManager;
111 private ResponseFormat formatCertificationError(Component component, StorageOperationStatus response, ComponentTypeEnum componentType) {
112 BeEcompErrorManager.getInstance().logBeDaoSystemError("Change LifecycleState - Certify failed on graph");
113 log.debug("certification change failed on graph");
115 ActionStatus actionStatus = componentUtils.convertFromStorageResponse(response);
116 ResponseFormat responseFormat = componentUtils.getResponseFormatByComponent(actionStatus, component, componentType);
117 return responseFormat;
121 public Either<Boolean, ResponseFormat> validateBeforeTransition(Component component, ComponentTypeEnum componentType, User modifier, User owner, LifecycleStateEnum oldState, LifecycleChangeInfoWithAction lifecycleChangeInfo) {
122 String componentName = component.getComponentMetadataDefinition().getMetadataDataDefinition().getName();
123 log.info("validate before certification change. resource name={}, oldState={}, owner userId={}", componentName, oldState, owner.getUserId());
126 Either<Boolean, ResponseFormat> userValidationResponse = userRoleValidation(modifier,component, componentType, lifecycleChangeInfo);
127 if (userValidationResponse.isRight()) {
128 log.error("userRoleValidation failed");
129 return userValidationResponse;
132 if (!oldState.equals(LifecycleStateEnum.CERTIFICATION_IN_PROGRESS)) {
133 log.error("oldState={} should be={}",oldState,ActionStatus.COMPONENT_NOT_READY_FOR_CERTIFICATION);
134 ResponseFormat error = componentUtils.getResponseFormat(ActionStatus.COMPONENT_NOT_READY_FOR_CERTIFICATION, componentName, componentType.name().toLowerCase());
135 return Either.right(error);
138 if (oldState.equals(LifecycleStateEnum.CERTIFICATION_IN_PROGRESS) && !modifier.equals(owner) && !modifier.getRole().equals(Role.ADMIN.name())) {
139 log.error("oldState={} should not be={}",oldState,ActionStatus.COMPONENT_IN_CERT_IN_PROGRESS_STATE);
140 log.error("&& modifier({})!={} && modifier.role({})!={}",modifier,owner);
141 ResponseFormat error = componentUtils.getResponseFormat(ActionStatus.COMPONENT_IN_CERT_IN_PROGRESS_STATE, componentName, componentType.name().toLowerCase(), owner.getFirstName(), owner.getLastName(), owner.getUserId());
142 return Either.right(error);
145 return Either.left(true);
149 public Either<? extends Component, ResponseFormat> changeState(ComponentTypeEnum componentType, Component component, ComponentBusinessLogic componentBl, User modifier, User owner, boolean shouldLock, boolean inTransaction) {
151 log.info("start performing certification change for resource {}", component.getUniqueId());
152 Either<? extends Component, ResponseFormat> result = null;
155 Either<ToscaElement, StorageOperationStatus> certificationChangeResult = Either.right(StorageOperationStatus.GENERAL_ERROR);
156 if (nextState.equals(LifecycleStateEnum.CERTIFIED)) {
157 certificationChangeResult = lifeCycleOperation.certifyToscaElement(component.getUniqueId(), modifier.getUserId(), owner.getUserId());
159 certificationChangeResult = lifeCycleOperation.cancelOrFailCertification(component.getUniqueId(), modifier.getUserId(), owner.getUserId(), nextState);
162 if (certificationChangeResult.isRight()) {
163 ResponseFormat responseFormat = formatCertificationError(component, certificationChangeResult.right().value(), componentType);
164 result = Either.right(responseFormat);
168 if (nextState.equals(LifecycleStateEnum.CERTIFIED)) {
169 Either<Boolean, StorageOperationStatus> deleteOldComponentVersions = lifeCycleOperation.deleteOldToscaElementVersions(ModelConverter.getVertexType(component), componentType, component.getComponentMetadataDefinition().getMetadataDataDefinition().getName(),
170 component.getComponentMetadataDefinition().getMetadataDataDefinition().getUUID());
171 if (deleteOldComponentVersions.isRight()) {
172 ResponseFormat responseFormat = formatCertificationError(component, deleteOldComponentVersions.right().value(), componentType);
173 result = Either.right(responseFormat);
177 result = Either.left(ModelConverter.convertFromToscaElement(certificationChangeResult.left().value()));
180 if (result == null || result.isRight()) {
181 BeEcompErrorManager.getInstance().logBeDaoSystemError("Change LifecycleState");
182 if (inTransaction == false) {
183 log.debug("operation failed. do rollback");
187 if (inTransaction == false) {
188 log.debug("operation success. do commit");