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.List;
26 import org.openecomp.sdc.be.components.impl.ArtifactsBusinessLogic;
27 import org.openecomp.sdc.be.components.impl.ComponentBusinessLogic;
28 import org.openecomp.sdc.be.config.BeEcompErrorManager;
29 import org.openecomp.sdc.be.dao.api.ActionStatus;
30 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
31 import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
32 import org.openecomp.sdc.be.impl.ComponentsUtils;
33 import org.openecomp.sdc.be.model.ArtifactDefinition;
34 import org.openecomp.sdc.be.model.Component;
35 import org.openecomp.sdc.be.model.LifeCycleTransitionEnum;
36 import org.openecomp.sdc.be.model.LifecycleStateEnum;
37 import org.openecomp.sdc.be.model.Resource;
38 import org.openecomp.sdc.be.model.User;
39 import org.openecomp.sdc.be.model.operations.api.ILifecycleOperation;
40 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
41 import org.openecomp.sdc.be.model.operations.impl.ResourceOperation;
42 import org.openecomp.sdc.be.resources.data.auditing.AuditingActionEnum;
43 import org.openecomp.sdc.be.user.Role;
44 import org.openecomp.sdc.common.config.EcompErrorName;
45 import org.openecomp.sdc.exception.ResponseFormat;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
49 import fj.data.Either;
51 public class CertificationChangeTransition extends LifeCycleTransition {
53 private static Logger log = LoggerFactory.getLogger(CertificationChangeTransition.class.getName());
55 private LifecycleStateEnum nextState;
56 private LifeCycleTransitionEnum name;
57 private AuditingActionEnum auditingAction;
58 private ArtifactsBusinessLogic artifactsManager;
60 public CertificationChangeTransition(LifeCycleTransitionEnum name, ComponentsUtils componentUtils, ILifecycleOperation lifecycleOperation) {
61 super(componentUtils, lifecycleOperation);
66 Role[] certificationChangeRoles = { Role.ADMIN, Role.TESTER };
67 addAuthorizedRoles(ComponentTypeEnum.RESOURCE, Arrays.asList(certificationChangeRoles));
68 addAuthorizedRoles(ComponentTypeEnum.SERVICE, Arrays.asList(certificationChangeRoles));
69 // TODO to be later defined for product
73 this.auditingAction = AuditingActionEnum.CERTIFICATION_SUCCESS_RESOURCE;
74 this.nextState = LifecycleStateEnum.CERTIFIED;
76 case FAIL_CERTIFICATION:
77 this.auditingAction = AuditingActionEnum.FAIL_CERTIFICATION_RESOURCE;
78 nextState = LifecycleStateEnum.NOT_CERTIFIED_CHECKIN;
80 case CANCEL_CERTIFICATION:
81 this.auditingAction = AuditingActionEnum.CANCEL_CERTIFICATION_RESOURCE;
82 nextState = LifecycleStateEnum.READY_FOR_CERTIFICATION;
91 public LifeCycleTransitionEnum getName() {
96 public AuditingActionEnum getAuditingAction() {
97 return auditingAction;
100 public ArtifactsBusinessLogic getArtifactsManager() {
101 return artifactsManager;
104 public void setArtifactsManager(ArtifactsBusinessLogic artifactsManager) {
105 this.artifactsManager = artifactsManager;
108 private ResponseFormat formatCertificationError(Component component, StorageOperationStatus response, ComponentTypeEnum componentType) {
109 BeEcompErrorManager.getInstance().processEcompError(EcompErrorName.BeDaoSystemError, "Change LifecycleState - Certify failed on graph");
110 BeEcompErrorManager.getInstance().logBeDaoSystemError("Change LifecycleState - Certify failed on graph");
111 log.debug("certification change failed on graph");
113 ActionStatus actionStatus = componentUtils.convertFromStorageResponse(response);
114 ResponseFormat responseFormat = componentUtils.getResponseFormatByComponent(actionStatus, component, componentType);
115 return responseFormat;
119 public Either<Boolean, ResponseFormat> validateBeforeTransition(Component component, ComponentTypeEnum componentType, User modifier, User owner, LifecycleStateEnum oldState, LifecycleChangeInfoWithAction lifecycleChangeInfo) {
120 String componentName = component.getComponentMetadataDefinition().getMetadataDataDefinition().getName();
121 log.debug("validate before certification change. resource name={}, oldState={}, owner userId={}", componentName, oldState, owner.getUserId());
124 Either<Boolean, ResponseFormat> userValidationResponse = userRoleValidation(modifier, componentType, lifecycleChangeInfo);
125 if (userValidationResponse.isRight()) {
126 return userValidationResponse;
129 if (!oldState.equals(LifecycleStateEnum.CERTIFICATION_IN_PROGRESS)) {
130 ResponseFormat error = componentUtils.getResponseFormat(ActionStatus.COMPONENT_NOT_READY_FOR_CERTIFICATION, componentName, componentType.name().toLowerCase());
131 return Either.right(error);
134 if (oldState.equals(LifecycleStateEnum.CERTIFICATION_IN_PROGRESS) && !modifier.equals(owner) && !modifier.getRole().equals(Role.ADMIN.name())) {
135 ResponseFormat error = componentUtils.getResponseFormat(ActionStatus.COMPONENT_IN_CERT_IN_PROGRESS_STATE, componentName, componentType.name().toLowerCase(), owner.getFirstName(), owner.getLastName(), owner.getUserId());
136 return Either.right(error);
139 return Either.left(true);
143 public Either<? extends Component, ResponseFormat> changeState(ComponentTypeEnum componentType, Component component, ComponentBusinessLogic componentBl, User modifier, User owner, boolean shouldLock, boolean inTransaction) {
145 log.info("start performing certification change for resource {}", component.getUniqueId());
146 Either<? extends Component, ResponseFormat> result = null;
147 NodeTypeEnum nodeType = componentType.getNodeType();
150 Either<? extends Component, StorageOperationStatus> certificationChangeResult = Either.right(StorageOperationStatus.GENERAL_ERROR);
151 if (nextState.equals(LifecycleStateEnum.CERTIFIED)) {
152 certificationChangeResult = lifeCycleOperation.certifyComponent(nodeType, component, modifier, owner, true);
154 certificationChangeResult = lifeCycleOperation.cancelOrFailCertification(nodeType, component, modifier, owner, nextState, true);
157 if (certificationChangeResult.isRight()) {
158 ResponseFormat responseFormat = formatCertificationError(component, certificationChangeResult.right().value(), componentType);
159 result = Either.right(responseFormat);
163 if (nextState.equals(LifecycleStateEnum.CERTIFIED)) {
164 Either<Boolean, StorageOperationStatus> deleteOldComponentVersions = lifeCycleOperation.deleteOldComponentVersions(nodeType, component.getComponentMetadataDefinition().getMetadataDataDefinition().getName(),
165 component.getComponentMetadataDefinition().getMetadataDataDefinition().getUUID(), true);
166 if (deleteOldComponentVersions.isRight()) {
167 ResponseFormat responseFormat = formatCertificationError(component, deleteOldComponentVersions.right().value(), componentType);
168 result = Either.right(responseFormat);
173 result = Either.left(certificationChangeResult.left().value());
176 if (result == null || result.isRight()) {
177 BeEcompErrorManager.getInstance().processEcompError(EcompErrorName.BeDaoSystemError, "Change LifecycleState");
178 BeEcompErrorManager.getInstance().logBeDaoSystemError("Change LifecycleState");
179 if (inTransaction == false) {
180 log.debug("operation failed. do rollback");
181 lifeCycleOperation.getResourceOperation().getTitanGenericDao().rollback();
184 if (inTransaction == false) {
185 log.debug("operation success. do commit");
186 lifeCycleOperation.getResourceOperation().getTitanGenericDao().commit();
193 public StorageOperationStatus deleteOldVersion(List<ArtifactDefinition> artifactsToDelete, Resource resourceToDelete) {
194 ResourceOperation resourceOperation = lifeCycleOperation.getResourceOperation();
196 Either<List<ArtifactDefinition>, StorageOperationStatus> artifactsRes = resourceOperation.getComponentArtifactsForDelete(resourceToDelete.getUniqueId(), NodeTypeEnum.Resource, true);
197 if (artifactsRes.isRight()) {
198 return artifactsRes.right().value();
200 Either<Resource, StorageOperationStatus> deleteResourceRes = resourceOperation.deleteResource(resourceToDelete.getUniqueId(), true);
201 if (deleteResourceRes.isRight()) {
202 return deleteResourceRes.right().value();
204 artifactsToDelete.addAll(artifactsRes.left().value());
206 return StorageOperationStatus.OK;