diff options
Diffstat (limited to 'catalog-be/src/main/java/org/openecomp/sdc/be/components/lifecycle/CertificationChangeTransition.java')
-rw-r--r-- | catalog-be/src/main/java/org/openecomp/sdc/be/components/lifecycle/CertificationChangeTransition.java | 290 |
1 files changed, 145 insertions, 145 deletions
diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/components/lifecycle/CertificationChangeTransition.java b/catalog-be/src/main/java/org/openecomp/sdc/be/components/lifecycle/CertificationChangeTransition.java index 47e494f66d..c229d87593 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/components/lifecycle/CertificationChangeTransition.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/components/lifecycle/CertificationChangeTransition.java @@ -20,7 +20,7 @@ package org.openecomp.sdc.be.components.lifecycle; -import java.util.Arrays; +import fj.data.Either; import org.openecomp.sdc.be.components.impl.ArtifactsBusinessLogic; import org.openecomp.sdc.be.components.impl.ComponentBusinessLogic; @@ -45,151 +45,151 @@ import org.openecomp.sdc.exception.ResponseFormat; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import fj.data.Either; +import java.util.Arrays; public class CertificationChangeTransition extends LifeCycleTransition { - private static Logger log = LoggerFactory.getLogger(CertificationChangeTransition.class.getName()); - - private LifecycleStateEnum nextState; - private LifeCycleTransitionEnum name; - private AuditingActionEnum auditingAction; - private ArtifactsBusinessLogic artifactsManager; - - public CertificationChangeTransition(LifeCycleTransitionEnum name, ComponentsUtils componentUtils, ToscaElementLifecycleOperation lifecycleOperation, ToscaOperationFacade toscaOperationFacade, TitanDao titanDao) { - super(componentUtils, lifecycleOperation, toscaOperationFacade, titanDao); - - this.name = name; - - // authorized roles - Role[] certificationChangeRoles = { Role.ADMIN, Role.TESTER }; - addAuthorizedRoles(ComponentTypeEnum.RESOURCE, Arrays.asList(certificationChangeRoles)); - addAuthorizedRoles(ComponentTypeEnum.SERVICE, Arrays.asList(certificationChangeRoles)); - // TODO to be later defined for product - - //additional authorized roles for resource type - Role[] resourceRoles = { Role.DESIGNER}; - addResouceAuthorizedRoles(ResourceTypeEnum.VFCMT, Arrays.asList(resourceRoles)); - - switch (this.name) { - case CERTIFY: - this.auditingAction = AuditingActionEnum.CERTIFICATION_SUCCESS_RESOURCE; - this.nextState = LifecycleStateEnum.CERTIFIED; - break; - case FAIL_CERTIFICATION: - this.auditingAction = AuditingActionEnum.FAIL_CERTIFICATION_RESOURCE; - nextState = LifecycleStateEnum.NOT_CERTIFIED_CHECKIN; - break; - case CANCEL_CERTIFICATION: - this.auditingAction = AuditingActionEnum.CANCEL_CERTIFICATION_RESOURCE; - nextState = LifecycleStateEnum.READY_FOR_CERTIFICATION; - break; - default: - break; - } - - } - - @Override - public LifeCycleTransitionEnum getName() { - return name; - } - - @Override - public AuditingActionEnum getAuditingAction() { - return auditingAction; - } - - public ArtifactsBusinessLogic getArtifactsManager() { - return artifactsManager; - } - - public void setArtifactsManager(ArtifactsBusinessLogic artifactsManager) { - this.artifactsManager = artifactsManager; - } - - private ResponseFormat formatCertificationError(Component component, StorageOperationStatus response, ComponentTypeEnum componentType) { - BeEcompErrorManager.getInstance().logBeDaoSystemError("Change LifecycleState - Certify failed on graph"); - log.debug("certification change failed on graph"); - - ActionStatus actionStatus = componentUtils.convertFromStorageResponse(response); - ResponseFormat responseFormat = componentUtils.getResponseFormatByComponent(actionStatus, component, componentType); - return responseFormat; - } - - @Override - public Either<Boolean, ResponseFormat> validateBeforeTransition(Component component, ComponentTypeEnum componentType, User modifier, User owner, LifecycleStateEnum oldState, LifecycleChangeInfoWithAction lifecycleChangeInfo) { - String componentName = component.getComponentMetadataDefinition().getMetadataDataDefinition().getName(); - log.info("validate before certification change. resource name={}, oldState={}, owner userId={}", componentName, oldState, owner.getUserId()); - - // validate user - Either<Boolean, ResponseFormat> userValidationResponse = userRoleValidation(modifier,component, componentType, lifecycleChangeInfo); - if (userValidationResponse.isRight()) { - log.error("userRoleValidation failed"); - return userValidationResponse; - } - - if (!oldState.equals(LifecycleStateEnum.CERTIFICATION_IN_PROGRESS)) { - log.error("oldState={} should be={}",oldState,ActionStatus.COMPONENT_NOT_READY_FOR_CERTIFICATION); - ResponseFormat error = componentUtils.getResponseFormat(ActionStatus.COMPONENT_NOT_READY_FOR_CERTIFICATION, componentName, componentType.name().toLowerCase()); - return Either.right(error); - } - - if (oldState.equals(LifecycleStateEnum.CERTIFICATION_IN_PROGRESS) && !modifier.equals(owner) && !modifier.getRole().equals(Role.ADMIN.name())) { - log.error("oldState={} should not be={}",oldState,ActionStatus.COMPONENT_IN_CERT_IN_PROGRESS_STATE); - log.error("&& modifier({})!={} && modifier.role({})!={}",modifier,owner); - ResponseFormat error = componentUtils.getResponseFormat(ActionStatus.COMPONENT_IN_CERT_IN_PROGRESS_STATE, componentName, componentType.name().toLowerCase(), owner.getFirstName(), owner.getLastName(), owner.getUserId()); - return Either.right(error); - } - - return Either.left(true); - } - - @Override - public Either<? extends Component, ResponseFormat> changeState(ComponentTypeEnum componentType, Component component, ComponentBusinessLogic componentBl, User modifier, User owner, boolean shouldLock, boolean inTransaction) { - - log.info("start performing certification change for resource {}", component.getUniqueId()); - Either<? extends Component, ResponseFormat> result = null; - - try { - Either<ToscaElement, StorageOperationStatus> certificationChangeResult = Either.right(StorageOperationStatus.GENERAL_ERROR); - if (nextState.equals(LifecycleStateEnum.CERTIFIED)) { - certificationChangeResult = lifeCycleOperation.certifyToscaElement(component.getUniqueId(), modifier.getUserId(), owner.getUserId()); - } else { - certificationChangeResult = lifeCycleOperation.cancelOrFailCertification(component.getUniqueId(), modifier.getUserId(), owner.getUserId(), nextState); - } - - if (certificationChangeResult.isRight()) { - ResponseFormat responseFormat = formatCertificationError(component, certificationChangeResult.right().value(), componentType); - result = Either.right(responseFormat); - return result; - } - - if (nextState.equals(LifecycleStateEnum.CERTIFIED)) { - Either<Boolean, StorageOperationStatus> deleteOldComponentVersions = lifeCycleOperation.deleteOldToscaElementVersions(ModelConverter.getVertexType(component), componentType, component.getComponentMetadataDefinition().getMetadataDataDefinition().getName(), - component.getComponentMetadataDefinition().getMetadataDataDefinition().getUUID()); - if (deleteOldComponentVersions.isRight()) { - ResponseFormat responseFormat = formatCertificationError(component, deleteOldComponentVersions.right().value(), componentType); - result = Either.right(responseFormat); - } - } - - result = Either.left(ModelConverter.convertFromToscaElement(certificationChangeResult.left().value())); - return result; - } finally { - if (result == null || result.isRight()) { - BeEcompErrorManager.getInstance().logBeDaoSystemError("Change LifecycleState"); - if (inTransaction == false) { - log.debug("operation failed. do rollback"); - titanDao.rollback(); - } - } else { - if (inTransaction == false) { - log.debug("operation success. do commit"); - titanDao.commit(); - } - } - } - - } + private static final Logger log = LoggerFactory.getLogger(CertificationChangeTransition.class); + + private LifecycleStateEnum nextState; + private LifeCycleTransitionEnum name; + private AuditingActionEnum auditingAction; + private ArtifactsBusinessLogic artifactsManager; + + public CertificationChangeTransition(LifeCycleTransitionEnum name, ComponentsUtils componentUtils, ToscaElementLifecycleOperation lifecycleOperation, ToscaOperationFacade toscaOperationFacade, TitanDao titanDao) { + super(componentUtils, lifecycleOperation, toscaOperationFacade, titanDao); + + this.name = name; + + // authorized roles + Role[] certificationChangeRoles = { Role.ADMIN, Role.TESTER }; + addAuthorizedRoles(ComponentTypeEnum.RESOURCE, Arrays.asList(certificationChangeRoles)); + addAuthorizedRoles(ComponentTypeEnum.SERVICE, Arrays.asList(certificationChangeRoles)); + // TODO to be later defined for product + + //additional authorized roles for resource type + Role[] resourceRoles = { Role.DESIGNER}; + addResouceAuthorizedRoles(ResourceTypeEnum.VFCMT, Arrays.asList(resourceRoles)); + + switch (this.name) { + case CERTIFY: + this.auditingAction = AuditingActionEnum.CERTIFICATION_SUCCESS_RESOURCE; + this.nextState = LifecycleStateEnum.CERTIFIED; + break; + case FAIL_CERTIFICATION: + this.auditingAction = AuditingActionEnum.FAIL_CERTIFICATION_RESOURCE; + nextState = LifecycleStateEnum.NOT_CERTIFIED_CHECKIN; + break; + case CANCEL_CERTIFICATION: + this.auditingAction = AuditingActionEnum.CANCEL_CERTIFICATION_RESOURCE; + nextState = LifecycleStateEnum.READY_FOR_CERTIFICATION; + break; + default: + break; + } + + } + + @Override + public LifeCycleTransitionEnum getName() { + return name; + } + + @Override + public AuditingActionEnum getAuditingAction() { + return auditingAction; + } + + public ArtifactsBusinessLogic getArtifactsManager() { + return artifactsManager; + } + + public void setArtifactsManager(ArtifactsBusinessLogic artifactsManager) { + this.artifactsManager = artifactsManager; + } + + private ResponseFormat formatCertificationError(Component component, StorageOperationStatus response, ComponentTypeEnum componentType) { + BeEcompErrorManager.getInstance().logBeDaoSystemError("Change LifecycleState - Certify failed on graph"); + log.debug("certification change failed on graph"); + + ActionStatus actionStatus = componentUtils.convertFromStorageResponse(response); + ResponseFormat responseFormat = componentUtils.getResponseFormatByComponent(actionStatus, component, componentType); + return responseFormat; + } + + @Override + public Either<Boolean, ResponseFormat> validateBeforeTransition(Component component, ComponentTypeEnum componentType, User modifier, User owner, LifecycleStateEnum oldState, LifecycleChangeInfoWithAction lifecycleChangeInfo) { + String componentName = component.getComponentMetadataDefinition().getMetadataDataDefinition().getName(); + log.info("validate before certification change. resource name={}, oldState={}, owner userId={}", componentName, oldState, owner.getUserId()); + + // validate user + Either<Boolean, ResponseFormat> userValidationResponse = userRoleValidation(modifier,component, componentType, lifecycleChangeInfo); + if (userValidationResponse.isRight()) { + log.error("userRoleValidation failed"); + return userValidationResponse; + } + + if (!oldState.equals(LifecycleStateEnum.CERTIFICATION_IN_PROGRESS)) { + log.error("oldState={} should be={}",oldState,ActionStatus.COMPONENT_NOT_READY_FOR_CERTIFICATION); + ResponseFormat error = componentUtils.getResponseFormat(ActionStatus.COMPONENT_NOT_READY_FOR_CERTIFICATION, componentName, componentType.name().toLowerCase()); + return Either.right(error); + } + + if (oldState.equals(LifecycleStateEnum.CERTIFICATION_IN_PROGRESS) && !modifier.equals(owner) && !modifier.getRole().equals(Role.ADMIN.name())) { + log.error("oldState={} should not be={}",oldState,ActionStatus.COMPONENT_IN_CERT_IN_PROGRESS_STATE); + log.error("&& modifier({})!={} && modifier.role({})!={}",modifier,owner); + ResponseFormat error = componentUtils.getResponseFormat(ActionStatus.COMPONENT_IN_CERT_IN_PROGRESS_STATE, componentName, componentType.name().toLowerCase(), owner.getFirstName(), owner.getLastName(), owner.getUserId()); + return Either.right(error); + } + + return Either.left(true); + } + + @Override + public Either<? extends Component, ResponseFormat> changeState(ComponentTypeEnum componentType, Component component, ComponentBusinessLogic componentBl, User modifier, User owner, boolean shouldLock, boolean inTransaction) { + + log.info("start performing certification change for resource {}", component.getUniqueId()); + Either<? extends Component, ResponseFormat> result = null; + + try { + Either<ToscaElement, StorageOperationStatus> certificationChangeResult = Either.right(StorageOperationStatus.GENERAL_ERROR); + if (nextState.equals(LifecycleStateEnum.CERTIFIED)) { + certificationChangeResult = lifeCycleOperation.certifyToscaElement(component.getUniqueId(), modifier.getUserId(), owner.getUserId()); + } else { + certificationChangeResult = lifeCycleOperation.cancelOrFailCertification(component.getUniqueId(), modifier.getUserId(), owner.getUserId(), nextState); + } + + if (certificationChangeResult.isRight()) { + ResponseFormat responseFormat = formatCertificationError(component, certificationChangeResult.right().value(), componentType); + result = Either.right(responseFormat); + return result; + } + + if (nextState.equals(LifecycleStateEnum.CERTIFIED)) { + Either<Boolean, StorageOperationStatus> deleteOldComponentVersions = lifeCycleOperation.deleteOldToscaElementVersions(ModelConverter.getVertexType(component), componentType, component.getComponentMetadataDefinition().getMetadataDataDefinition().getName(), + component.getComponentMetadataDefinition().getMetadataDataDefinition().getUUID()); + if (deleteOldComponentVersions.isRight()) { + ResponseFormat responseFormat = formatCertificationError(component, deleteOldComponentVersions.right().value(), componentType); + result = Either.right(responseFormat); + } + } + + result = Either.left(ModelConverter.convertFromToscaElement(certificationChangeResult.left().value())); + return result; + } finally { + if (result == null || result.isRight()) { + BeEcompErrorManager.getInstance().logBeDaoSystemError("Change LifecycleState"); + if (inTransaction == false) { + log.debug("operation failed. do rollback"); + titanDao.rollback(); + } + } else { + if (inTransaction == false) { + log.debug("operation success. do commit"); + titanDao.commit(); + } + } + } + + } } |