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 org.openecomp.sdc.be.components.impl.ArtifactsBusinessLogic;
25 import org.openecomp.sdc.be.components.impl.ComponentBusinessLogic;
26 import org.openecomp.sdc.be.config.BeEcompErrorManager;
27 import org.openecomp.sdc.be.dao.api.ActionStatus;
28 import org.openecomp.sdc.be.dao.jsongraph.TitanDao;
29 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
30 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
31 import org.openecomp.sdc.be.impl.ComponentsUtils;
32 import org.openecomp.sdc.be.model.Component;
33 import org.openecomp.sdc.be.model.LifeCycleTransitionEnum;
34 import org.openecomp.sdc.be.model.LifecycleStateEnum;
35 import org.openecomp.sdc.be.model.User;
36 import org.openecomp.sdc.be.model.jsontitan.datamodel.ToscaElement;
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.user.Role;
43 import org.openecomp.sdc.exception.ResponseFormat;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
47 import fj.data.Either;
49 public class CertificationChangeTransition extends LifeCycleTransition {
51 private static Logger log = LoggerFactory.getLogger(CertificationChangeTransition.class.getName());
53 private LifecycleStateEnum nextState;
54 private LifeCycleTransitionEnum name;
55 private AuditingActionEnum auditingAction;
56 private ArtifactsBusinessLogic artifactsManager;
58 public CertificationChangeTransition(LifeCycleTransitionEnum name, ComponentsUtils componentUtils, ToscaElementLifecycleOperation lifecycleOperation, ToscaOperationFacade toscaOperationFacade, TitanDao titanDao) {
59 super(componentUtils, lifecycleOperation, toscaOperationFacade, titanDao);
64 Role[] certificationChangeRoles = { Role.ADMIN, Role.TESTER };
65 addAuthorizedRoles(ComponentTypeEnum.RESOURCE, Arrays.asList(certificationChangeRoles));
66 addAuthorizedRoles(ComponentTypeEnum.SERVICE, Arrays.asList(certificationChangeRoles));
67 // TODO to be later defined for product
69 //additional authorized roles for resource type
70 Role[] resourceRoles = { Role.DESIGNER};
71 addResouceAuthorizedRoles(ResourceTypeEnum.VFCMT, Arrays.asList(resourceRoles));
75 this.auditingAction = AuditingActionEnum.CERTIFICATION_SUCCESS_RESOURCE;
76 this.nextState = LifecycleStateEnum.CERTIFIED;
78 case FAIL_CERTIFICATION:
79 this.auditingAction = AuditingActionEnum.FAIL_CERTIFICATION_RESOURCE;
80 nextState = LifecycleStateEnum.NOT_CERTIFIED_CHECKIN;
82 case CANCEL_CERTIFICATION:
83 this.auditingAction = AuditingActionEnum.CANCEL_CERTIFICATION_RESOURCE;
84 nextState = LifecycleStateEnum.READY_FOR_CERTIFICATION;
93 public LifeCycleTransitionEnum getName() {
98 public AuditingActionEnum getAuditingAction() {
99 return auditingAction;
102 public ArtifactsBusinessLogic getArtifactsManager() {
103 return artifactsManager;
106 public void setArtifactsManager(ArtifactsBusinessLogic artifactsManager) {
107 this.artifactsManager = artifactsManager;
110 private ResponseFormat formatCertificationError(Component component, StorageOperationStatus response, ComponentTypeEnum componentType) {
111 BeEcompErrorManager.getInstance().logBeDaoSystemError("Change LifecycleState - Certify failed on graph");
112 log.debug("certification change failed on graph");
114 ActionStatus actionStatus = componentUtils.convertFromStorageResponse(response);
115 ResponseFormat responseFormat = componentUtils.getResponseFormatByComponent(actionStatus, component, componentType);
116 return responseFormat;
120 public Either<Boolean, ResponseFormat> validateBeforeTransition(Component component, ComponentTypeEnum componentType, User modifier, User owner, LifecycleStateEnum oldState, LifecycleChangeInfoWithAction lifecycleChangeInfo) {
121 String componentName = component.getComponentMetadataDefinition().getMetadataDataDefinition().getName();
122 log.info("validate before certification change. resource name={}, oldState={}, owner userId={}", componentName, oldState, owner.getUserId());
125 Either<Boolean, ResponseFormat> userValidationResponse = userRoleValidation(modifier,component, componentType, lifecycleChangeInfo);
126 if (userValidationResponse.isRight()) {
127 log.error("userRoleValidation failed");
128 return userValidationResponse;
131 if (!oldState.equals(LifecycleStateEnum.CERTIFICATION_IN_PROGRESS)) {
132 log.error("oldState={} should be={}",oldState,ActionStatus.COMPONENT_NOT_READY_FOR_CERTIFICATION);
133 ResponseFormat error = componentUtils.getResponseFormat(ActionStatus.COMPONENT_NOT_READY_FOR_CERTIFICATION, componentName, componentType.name().toLowerCase());
134 return Either.right(error);
137 if (oldState.equals(LifecycleStateEnum.CERTIFICATION_IN_PROGRESS) && !modifier.equals(owner) && !modifier.getRole().equals(Role.ADMIN.name())) {
138 log.error("oldState={} should not be={}",oldState,ActionStatus.COMPONENT_IN_CERT_IN_PROGRESS_STATE);
139 log.error("&& modifier({})!={} && modifier.role({})!={}",modifier,owner);
140 ResponseFormat error = componentUtils.getResponseFormat(ActionStatus.COMPONENT_IN_CERT_IN_PROGRESS_STATE, componentName, componentType.name().toLowerCase(), owner.getFirstName(), owner.getLastName(), owner.getUserId());
141 return Either.right(error);
144 return Either.left(true);
148 public Either<? extends Component, ResponseFormat> changeState(ComponentTypeEnum componentType, Component component, ComponentBusinessLogic componentBl, User modifier, User owner, boolean shouldLock, boolean inTransaction) {
150 log.info("start performing certification change for resource {}", component.getUniqueId());
151 Either<? extends Component, ResponseFormat> result = null;
154 Either<ToscaElement, StorageOperationStatus> certificationChangeResult = Either.right(StorageOperationStatus.GENERAL_ERROR);
155 if (nextState.equals(LifecycleStateEnum.CERTIFIED)) {
156 certificationChangeResult = lifeCycleOperation.certifyToscaElement(component.getUniqueId(), modifier.getUserId(), owner.getUserId());
158 certificationChangeResult = lifeCycleOperation.cancelOrFailCertification(component.getUniqueId(), modifier.getUserId(), owner.getUserId(), nextState);
161 if (certificationChangeResult.isRight()) {
162 ResponseFormat responseFormat = formatCertificationError(component, certificationChangeResult.right().value(), componentType);
163 result = Either.right(responseFormat);
167 if (nextState.equals(LifecycleStateEnum.CERTIFIED)) {
168 Either<Boolean, StorageOperationStatus> deleteOldComponentVersions = lifeCycleOperation.deleteOldToscaElementVersions(ModelConverter.getVertexType(component), componentType, component.getComponentMetadataDefinition().getMetadataDataDefinition().getName(),
169 component.getComponentMetadataDefinition().getMetadataDataDefinition().getUUID());
170 if (deleteOldComponentVersions.isRight()) {
171 ResponseFormat responseFormat = formatCertificationError(component, deleteOldComponentVersions.right().value(), componentType);
172 result = Either.right(responseFormat);
176 result = Either.left(ModelConverter.convertFromToscaElement(certificationChangeResult.left().value()));
179 if (result == null || result.isRight()) {
180 BeEcompErrorManager.getInstance().logBeDaoSystemError("Change LifecycleState");
181 if (inTransaction == false) {
182 log.debug("operation failed. do rollback");
186 if (inTransaction == false) {
187 log.debug("operation success. do commit");