X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=catalog-be%2Fsrc%2Fmain%2Fjava%2Forg%2Fopenecomp%2Fsdc%2Fbe%2Fcomponents%2Fimpl%2FServiceBusinessLogic.java;h=9874020b49451dd7c6d1d0dfb34d9e33f8509390;hb=216ea4998de9ae4ee603d7f8f5f6725b218ca710;hp=48cbb35fddb6780e0c6aab223c5a135e0609a5cd;hpb=2924a3dbb7e0f50b239287fb11f6ec8c2235522a;p=sdc.git diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ServiceBusinessLogic.java b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ServiceBusinessLogic.java index 48cbb35fdd..9874020b49 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ServiceBusinessLogic.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ServiceBusinessLogic.java @@ -16,71 +16,138 @@ * See the License for the specific language governing permissions and * limitations under the License. * ============LICENSE_END========================================================= + * Modifications copyright (c) 2019 Nokia + * ================================================================================ */ package org.openecomp.sdc.be.components.impl; +import static org.apache.commons.collections.CollectionUtils.isNotEmpty; +import static org.openecomp.sdc.be.components.utils.ConsumptionUtils.handleConsumptionInputMappedToCapabilityProperty; +import static org.openecomp.sdc.be.components.utils.ConsumptionUtils.isAssignedValueFromValidType; +import static org.openecomp.sdc.be.components.utils.InterfaceOperationUtils.getOperationOutputName; +import static org.openecomp.sdc.be.components.utils.InterfaceOperationUtils.isOperationInputMappedToOtherOperationOutput; +import static org.openecomp.sdc.be.resources.data.auditing.AuditingActionEnum.UPDATE_SERVICE_METADATA; +import static org.openecomp.sdc.be.tosca.InterfacesOperationsConverter.SELF; +import static org.openecomp.sdc.be.types.ServiceConsumptionSource.SERVICE_INPUT; +import static org.openecomp.sdc.be.types.ServiceConsumptionSource.STATIC; + +import com.google.common.annotations.VisibleForTesting; +import com.google.common.base.Strings; +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import fj.data.Either; import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.Collections; +import java.util.Comparator; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Map.Entry; +import java.util.Objects; import java.util.Optional; import java.util.Set; import java.util.concurrent.Callable; import java.util.function.Function; import java.util.stream.Collectors; - import javax.servlet.ServletContext; import javax.servlet.http.HttpServletRequest; - +import lombok.AccessLevel; +import lombok.AllArgsConstructor; +import lombok.Getter; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.collections.MapUtils; +import org.apache.commons.collections4.ListUtils; +import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.tuple.ImmutablePair; +import org.openecomp.sdc.be.catalog.enums.ChangeTypeEnum; import org.openecomp.sdc.be.components.distribution.engine.IDistributionEngine; import org.openecomp.sdc.be.components.distribution.engine.INotificationData; import org.openecomp.sdc.be.components.distribution.engine.VfModuleArtifactPayload; import org.openecomp.sdc.be.components.health.HealthCheckBusinessLogic; -import org.openecomp.sdc.be.components.lifecycle.LifecycleChangeInfoWithAction; +import org.openecomp.sdc.be.components.impl.exceptions.ByActionStatusComponentException; +import org.openecomp.sdc.be.components.impl.exceptions.ByResponseFormatComponentException; +import org.openecomp.sdc.be.components.impl.exceptions.ComponentException; +import org.openecomp.sdc.be.components.kafka.KafkaHandler; import org.openecomp.sdc.be.components.path.ForwardingPathValidator; +import org.openecomp.sdc.be.components.utils.InterfaceOperationUtils; +import org.openecomp.sdc.be.components.utils.PropertiesUtils; import org.openecomp.sdc.be.components.validation.ServiceDistributionValidation; +import org.openecomp.sdc.be.components.validation.component.ComponentContactIdValidator; +import org.openecomp.sdc.be.components.validation.component.ComponentDescriptionValidator; +import org.openecomp.sdc.be.components.validation.component.ComponentIconValidator; +import org.openecomp.sdc.be.components.validation.component.ComponentNameValidator; +import org.openecomp.sdc.be.components.validation.component.ComponentProjectCodeValidator; +import org.openecomp.sdc.be.components.validation.component.ComponentTagsValidator; +import org.openecomp.sdc.be.components.validation.component.ComponentValidator; +import org.openecomp.sdc.be.components.validation.service.ServiceCategoryValidator; +import org.openecomp.sdc.be.components.validation.service.ServiceFunctionValidator; +import org.openecomp.sdc.be.components.validation.service.ServiceInstantiationTypeValidator; +import org.openecomp.sdc.be.components.validation.service.ServiceRoleValidator; +import org.openecomp.sdc.be.components.validation.service.ServiceTypeValidator; +import org.openecomp.sdc.be.components.validation.service.ServiceValidator; import org.openecomp.sdc.be.config.BeEcompErrorManager; import org.openecomp.sdc.be.config.ConfigurationManager; import org.openecomp.sdc.be.dao.api.ActionStatus; import org.openecomp.sdc.be.dao.cassandra.AuditCassandraDao; import org.openecomp.sdc.be.dao.jsongraph.types.JsonParseFlagEnum; import org.openecomp.sdc.be.datamodel.ServiceRelations; +import org.openecomp.sdc.be.datamodel.utils.PropertyValueConstraintValidationUtil; import org.openecomp.sdc.be.datamodel.utils.UiComponentDataConverter; import org.openecomp.sdc.be.datatypes.elements.ForwardingPathDataDefinition; +import org.openecomp.sdc.be.datatypes.elements.InterfaceDataDefinition; +import org.openecomp.sdc.be.datatypes.elements.ListDataDefinition; +import org.openecomp.sdc.be.datatypes.elements.OperationInputDefinition; +import org.openecomp.sdc.be.datatypes.elements.OperationOutputDefinition; +import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition; +import org.openecomp.sdc.be.datatypes.elements.ToscaFunction; +import org.openecomp.sdc.be.datatypes.elements.ToscaFunctionType; +import org.openecomp.sdc.be.datatypes.elements.ToscaGetFunctionDataDefinition; +import org.openecomp.sdc.be.datatypes.enums.ComponentFieldsEnum; import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum; +import org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields; +import org.openecomp.sdc.be.datatypes.enums.ModelTypeEnum; import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum; import org.openecomp.sdc.be.datatypes.enums.OriginTypeEnum; import org.openecomp.sdc.be.externalapi.servlet.representation.ServiceDistributionReqInfo; import org.openecomp.sdc.be.impl.ForwardingPathUtils; import org.openecomp.sdc.be.impl.WebAppContextWrapper; import org.openecomp.sdc.be.model.ArtifactDefinition; +import org.openecomp.sdc.be.model.CapabilityDefinition; import org.openecomp.sdc.be.model.Component; import org.openecomp.sdc.be.model.ComponentInstance; +import org.openecomp.sdc.be.model.ComponentInstanceInterface; +import org.openecomp.sdc.be.model.ComponentInstanceProperty; import org.openecomp.sdc.be.model.ComponentParametersView; import org.openecomp.sdc.be.model.DistributionStatusEnum; -import org.openecomp.sdc.be.model.DistributionTransitionEnum; import org.openecomp.sdc.be.model.GroupInstance; import org.openecomp.sdc.be.model.GroupInstanceProperty; +import org.openecomp.sdc.be.model.InputDefinition; +import org.openecomp.sdc.be.model.InterfaceDefinition; import org.openecomp.sdc.be.model.LifecycleStateEnum; +import org.openecomp.sdc.be.model.Model; +import org.openecomp.sdc.be.model.Operation; +import org.openecomp.sdc.be.model.PropertyDefinition; import org.openecomp.sdc.be.model.Resource; import org.openecomp.sdc.be.model.Service; import org.openecomp.sdc.be.model.User; import org.openecomp.sdc.be.model.category.CategoryDefinition; -import org.openecomp.sdc.be.model.jsontitan.operations.ForwardingPathOperation; -import org.openecomp.sdc.be.model.jsontitan.operations.ToscaOperationFacade; -import org.openecomp.sdc.be.model.operations.api.ICacheMangerOperation; +import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ArtifactsOperations; +import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ForwardingPathOperation; +import org.openecomp.sdc.be.model.jsonjanusgraph.operations.InterfaceOperation; import org.openecomp.sdc.be.model.operations.api.IElementOperation; +import org.openecomp.sdc.be.model.operations.api.IGroupInstanceOperation; +import org.openecomp.sdc.be.model.operations.api.IGroupOperation; +import org.openecomp.sdc.be.model.operations.api.IGroupTypeOperation; import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus; +import org.openecomp.sdc.be.model.operations.impl.InterfaceLifecycleOperation; +import org.openecomp.sdc.be.model.operations.impl.ModelOperation; import org.openecomp.sdc.be.model.operations.impl.UniqueIdBuilder; import org.openecomp.sdc.be.model.operations.utils.ComponentValidationUtils; +import org.openecomp.sdc.be.plugins.ServiceCreationPlugin; import org.openecomp.sdc.be.resources.data.ComponentInstanceData; import org.openecomp.sdc.be.resources.data.ComponentMetadataData; import org.openecomp.sdc.be.resources.data.auditing.AuditingActionEnum; @@ -88,7 +155,10 @@ import org.openecomp.sdc.be.resources.data.auditing.AuditingGenericEvent; import org.openecomp.sdc.be.resources.data.auditing.DistributionDeployEvent; import org.openecomp.sdc.be.resources.data.auditing.DistributionNotificationEvent; import org.openecomp.sdc.be.resources.data.auditing.ResourceAdminEvent; -import org.openecomp.sdc.be.resources.data.auditing.model.ResourceAuditData; +import org.openecomp.sdc.be.resources.data.auditing.model.ResourceCommonInfo; +import org.openecomp.sdc.be.resources.data.auditing.model.ResourceVersionInfo; +import org.openecomp.sdc.be.types.ServiceConsumptionData; +import org.openecomp.sdc.be.types.ServiceConsumptionSource; import org.openecomp.sdc.be.ui.model.UiComponentDataTransfer; import org.openecomp.sdc.be.user.Role; import org.openecomp.sdc.common.api.ArtifactGroupTypeEnum; @@ -96,138 +166,91 @@ import org.openecomp.sdc.common.api.ArtifactTypeEnum; import org.openecomp.sdc.common.api.Constants; import org.openecomp.sdc.common.datastructure.Wrapper; import org.openecomp.sdc.common.kpi.api.ASDCKpiApi; +import org.openecomp.sdc.common.log.wrappers.Logger; import org.openecomp.sdc.common.util.GeneralUtility; import org.openecomp.sdc.common.util.ThreadLocalsHolder; import org.openecomp.sdc.common.util.ValidationUtils; import org.openecomp.sdc.exception.ResponseFormat; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import org.openecomp.sdc.tosca.datatypes.ToscaFunctions; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; import org.springframework.web.context.WebApplicationContext; -import com.google.common.base.Strings; -import com.google.gson.Gson; -import com.google.gson.GsonBuilder; - -import fj.data.Either; - @org.springframework.stereotype.Component("serviceBusinessLogic") public class ServiceBusinessLogic extends ComponentBusinessLogic { + private static final String IS_VALID = "isValid"; + private static final String THE_SERVICE_WITH_SYSTEM_NAME_LOCKED = "The service with system name {} locked. "; + private static final String FAILED_TO_LOCK_SERVICE_RESPONSE_IS = "Failed to lock service {}. Response is {}. "; + private static final String AUDIT_BEFORE_SENDING_RESPONSE = "audit before sending response"; + private static final Logger log = Logger.getLogger(ServiceBusinessLogic.class); + private static final String INITIAL_VERSION = "0.1"; private static final String STATUS_SUCCESS_200 = "200"; - private static final String STATUS_DEPLOYED = "DEPLOYED"; - - @Autowired - private IElementOperation elementDao; - - @Autowired - private IDistributionEngine distributionEngine; - - @Autowired - private AuditCassandraDao auditCassandraDao; - - @Autowired - private ComponentInstanceBusinessLogic componentInstanceBusinessLogic; - - @Autowired - private GroupBusinessLogic groupBusinessLogic; - - @Autowired - private ICacheMangerOperation cacheManagerOperation; - - @Autowired - private ServiceDistributionValidation serviceDistributionValidation; - - private static final Logger log = LoggerFactory.getLogger(ServiceBusinessLogic.class); - private static final String INITIAL_VERSION = "0.1"; - @Autowired + private static final String PLACE_HOLDER_RESOURCE_TYPES = "validForResourceTypes"; + private final IDistributionEngine distributionEngine; + private final ComponentInstanceBusinessLogic componentInstanceBusinessLogic; + private final ServiceDistributionValidation serviceDistributionValidation; + private final ForwardingPathValidator forwardingPathValidator; + private final UiComponentDataConverter uiComponentDataConverter; + private final ModelOperation modelOperation; + private final ServiceRoleValidator serviceRoleValidator; + private final ServiceInstantiationTypeValidator serviceInstantiationTypeValidator; + private final ServiceCategoryValidator serviceCategoryValidator; + private final ServiceValidator serviceValidator; + private final GroupBusinessLogic groupBusinessLogic; + private final KafkaHandler kafkaHandler; private ForwardingPathOperation forwardingPathOperation; + private AuditCassandraDao auditCassandraDao; + private ServiceTypeValidator serviceTypeValidator; + private List serviceCreationPluginList; + private ServiceFunctionValidator serviceFunctionValidator; + + public ServiceBusinessLogic(IElementOperation elementDao, IGroupOperation groupOperation, IGroupInstanceOperation groupInstanceOperation, + IGroupTypeOperation groupTypeOperation, GroupBusinessLogic groupBusinessLogic, InterfaceOperation interfaceOperation, + InterfaceLifecycleOperation interfaceLifecycleTypeOperation, ArtifactsBusinessLogic artifactsBusinessLogic, + IDistributionEngine distributionEngine, ComponentInstanceBusinessLogic componentInstanceBusinessLogic, + ServiceDistributionValidation serviceDistributionValidation, ForwardingPathValidator forwardingPathValidator, + UiComponentDataConverter uiComponentDataConverter, ArtifactsOperations artifactToscaOperation, + ComponentContactIdValidator componentContactIdValidator, ComponentNameValidator componentNameValidator, + ComponentTagsValidator componentTagsValidator, ComponentValidator componentValidator, + ComponentIconValidator componentIconValidator, ComponentProjectCodeValidator componentProjectCodeValidator, + ComponentDescriptionValidator componentDescriptionValidator, ModelOperation modelOperation, + final ServiceRoleValidator serviceRoleValidator, + final ServiceInstantiationTypeValidator serviceInstantiationTypeValidator, + final ServiceCategoryValidator serviceCategoryValidator, final ServiceValidator serviceValidator, + KafkaHandler kafkaHandler) { + super(elementDao, groupOperation, groupInstanceOperation, groupTypeOperation, groupBusinessLogic, interfaceOperation, + interfaceLifecycleTypeOperation, artifactsBusinessLogic, artifactToscaOperation, componentContactIdValidator, componentNameValidator, + componentTagsValidator, componentValidator, componentIconValidator, componentProjectCodeValidator, componentDescriptionValidator); + this.distributionEngine = distributionEngine; + this.componentInstanceBusinessLogic = componentInstanceBusinessLogic; + this.serviceDistributionValidation = serviceDistributionValidation; + this.forwardingPathValidator = forwardingPathValidator; + this.uiComponentDataConverter = uiComponentDataConverter; + this.modelOperation = modelOperation; + this.serviceRoleValidator = serviceRoleValidator; + this.serviceInstantiationTypeValidator = serviceInstantiationTypeValidator; + this.serviceCategoryValidator = serviceCategoryValidator; + this.serviceValidator = serviceValidator; + this.groupBusinessLogic = groupBusinessLogic; + this.kafkaHandler = kafkaHandler; + } @Autowired - private ForwardingPathValidator forwardingPathValidator; - - public ServiceBusinessLogic() { - log.debug("ServiceBusinessLogic started"); + public void setServiceTypeValidator(ServiceTypeValidator serviceTypeValidator) { + this.serviceTypeValidator = serviceTypeValidator; } - public Either changeServiceDistributionState(String serviceId, String state, LifecycleChangeInfoWithAction commentObj, User user) { - - Either resp = validateUserExists(user.getUserId(), "change Service Distribution State", false); - if (resp.isRight()) { - return Either.right(resp.right().value()); - } - - log.debug("check request state"); - Either validateEnum = validateTransitionEnum(state, user); - if (validateEnum.isRight()) { - return Either.right(validateEnum.right().value()); - } - DistributionTransitionEnum distributionTransition = validateEnum.left().value(); - AuditingActionEnum auditAction = distributionTransition == DistributionTransitionEnum.APPROVE ? AuditingActionEnum.DISTRIBUTION_STATE_CHANGE_APPROV : AuditingActionEnum.DISTRIBUTION_STATE_CHANGE_REJECT; - Either commentResponse = validateComment(commentObj, user, auditAction); - if (commentResponse.isRight()) { - return Either.right(commentResponse.right().value()); - } - String comment = commentResponse.left().value(); - - Either validateService = validateServiceDistributionChange(user, serviceId, auditAction, comment); - if (validateService.isRight()) { - return Either.right(validateService.right().value()); - } - Service service = validateService.left().value(); - Either validateUser = validateUserDistributionChange(user, service, auditAction, comment); - if (validateUser.isRight()) { - return Either.right(validateUser.right().value()); - } - user = validateUser.left().value(); - - // lock resource - - Either lockResult = lockComponent(serviceId, service, "ChangeServiceDistributionState"); - if (lockResult.isRight()) { - ResponseFormat responseFormat = lockResult.right().value(); - createAudit(user, auditAction, comment, service, responseFormat); - return Either.right(responseFormat); - } - - try { - - DistributionStatusEnum newState; - if (distributionTransition == DistributionTransitionEnum.APPROVE) { - newState = DistributionStatusEnum.DISTRIBUTION_APPROVED; - } else { - newState = DistributionStatusEnum.DISTRIBUTION_REJECTED; - } - Either result = toscaOperationFacade.updateDistributionStatus(service, user, newState); - if (result.isRight()) { - titanDao.rollback(); - BeEcompErrorManager.getInstance().logBeSystemError("ChangeServiceDistributionState"); - log.debug("service {} is change destribuation status failed", service.getUniqueId()); - ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR, service.getVersion(), service.getName()); - createAudit(user, auditAction, comment, service, responseFormat); - return Either.right(componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR)); - } - titanDao.commit(); - Service updatedService = result.left().value(); - ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.OK); - log.debug("audit before sending response"); - componentsUtils.auditComponent(responseFormat, user, updatedService, auditAction, ComponentTypeEnum.SERVICE, ResourceAuditData.newBuilder().build(), comment); - return Either.left(result.left().value()); - } finally { - graphLockOperation.unlockComponent(serviceId, NodeTypeEnum.Service); - } - + @Autowired + public void setServiceFunctionValidator(ServiceFunctionValidator serviceFunctionValidator) { + this.serviceFunctionValidator = serviceFunctionValidator; } public Either>, ResponseFormat> getComponentAuditRecords(String componentVersion, String componentUUID, String userId) { - Either resp = validateUserExists(userId, "get Component Audit Records", false); - if (resp.isRight()) { - return Either.right(resp.right().value()); - } + validateUserExists(userId); Either>, ActionStatus> result; try { - // Certified Version if (componentVersion.endsWith(".0")) { Either, ActionStatus> eitherAuditingForCertified = auditCassandraDao.getByServiceInstanceId(componentUUID); @@ -245,47 +268,393 @@ public class ServiceBusinessLogic extends ComponentBusinessLogic { log.debug("get Audit Records failed with exception {}", e); result = Either.right(ActionStatus.GENERAL_ERROR); } - if (result.isRight()) { return Either.right(componentsUtils.getResponseFormat(result.right().value())); } else { return Either.left(result.left().value()); } + } + public Either, ResponseFormat> addServiceConsumptionData(String serviceId, String serviceInstanceId, String operationId, + List serviceConsumptionDataList, String userId) { + List operationList = new ArrayList<>(); + Either serviceEither = toscaOperationFacade.getToscaElement(serviceId); + if (serviceEither.isRight()) { + return Either.right(componentsUtils.getResponseFormat(serviceEither.right().value())); + } + Service service = serviceEither.left().value(); + StorageOperationStatus storageOperationStatus = graphLockOperation.lockComponent(service.getUniqueId(), NodeTypeEnum.Service); + if (storageOperationStatus != StorageOperationStatus.OK) { + return Either.right(componentsUtils.getResponseFormat(storageOperationStatus)); + } + try { + for (ServiceConsumptionData serviceConsumptionData : serviceConsumptionDataList) { + Either operationEither = addPropertyServiceConsumption(serviceId, serviceInstanceId, operationId, userId, + serviceConsumptionData); + if (operationEither.isRight()) { + return Either.right(operationEither.right().value()); + } + operationList.add(operationEither.left().value()); + } + janusGraphDao.commit(); + return Either.left(operationList); + } catch (Exception e) { + janusGraphDao.rollback(); + return Either.right(componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR)); + } finally { + graphLockOperation.unlockComponent(service.getUniqueId(), NodeTypeEnum.Service); + } + } + + public Either addPropertyServiceConsumption(String serviceId, String serviceInstanceId, String operationId, + String userId, ServiceConsumptionData serviceConsumptionData) { + validateUserExists(userId); + Either serviceEither = toscaOperationFacade.getToscaElement(serviceId); + if (serviceEither.isRight()) { + return Either.right(componentsUtils.getResponseFormat(serviceEither.right().value())); + } + Service parentService = serviceEither.left().value(); + List componentInstances = parentService.getComponentInstances(); + if (CollectionUtils.isEmpty(componentInstances)) { + return Either.right(componentsUtils.getResponseFormat(ActionStatus.INTERFACE_OPERATION_NOT_FOUND, serviceInstanceId)); + } + Optional serviceInstanceCandidate = componentInstances.stream() + .filter(instance -> instance.getUniqueId().equals(serviceInstanceId)).findAny(); + if (!serviceInstanceCandidate.isPresent()) { + return Either.right(componentsUtils.getResponseFormat(ActionStatus.INTERFACE_OPERATION_NOT_FOUND, serviceInstanceId)); + } + Map> componentInstancesInterfaces = parentService.getComponentInstancesInterfaces(); + if (MapUtils.isEmpty(componentInstancesInterfaces)) { + return Either.right(componentsUtils.getResponseFormat(ActionStatus.INTERFACE_OPERATION_NOT_FOUND, serviceInstanceId)); + } + List interfaces = new ArrayList<>(); + for (ComponentInstanceInterface componentInstanceInterface : componentInstancesInterfaces.get(serviceInstanceId)) { + interfaces.add(componentInstanceInterface); + } + ComponentInstance serviceInstance = serviceInstanceCandidate.get(); + Optional interfaceCandidate = InterfaceOperationUtils.getInterfaceDefinitionFromOperationId(interfaces, operationId); + if (!interfaceCandidate.isPresent()) { + return Either.right(componentsUtils.getResponseFormat(ActionStatus.INTERFACE_OPERATION_NOT_FOUND, serviceInstanceId)); + } + InterfaceDefinition interfaceDefinition = interfaceCandidate.get(); + Map operations = interfaceDefinition.getOperationsMap(); + if (MapUtils.isEmpty(operations)) { + return Either.right(componentsUtils.getResponseFormat(ActionStatus.INTERFACE_OPERATION_NOT_FOUND, serviceInstanceId)); + } + Operation operation = operations.get(operationId); + Either operationEither = Either.left(operation); + ListDataDefinition inputs = operation.getInputs(); + Optional inputCandidate = getOperationInputByInputId(serviceConsumptionData, inputs); + if (!inputCandidate.isPresent()) { + return Either.right(new ResponseFormat(HttpStatus.NOT_FOUND.value())); + } + OperationInputDefinition operationInputDefinition = inputCandidate.get(); + // add data to operation + if (Objects.nonNull(serviceConsumptionData.getValue())) { + operationEither = handleConsumptionValue(parentService, serviceInstanceId, serviceConsumptionData, operation, operationInputDefinition); + } + if (operationEither.isRight()) { + return Either.right(operationEither.right().value()); + } + Operation updatedOperation = operationEither.left().value(); + operations.remove(operationId); + operations.put(operationId, updatedOperation); + interfaceDefinition.setOperationsMap(operations); + parentService.getComponentInstances().remove(serviceInstance); + if (CollectionUtils.isEmpty(parentService.getComponentInstances())) { + parentService.setComponentInstances(new ArrayList<>()); + } + Map instanceInterfaces = + MapUtils.isEmpty(serviceInstance.getInterfaces()) ? new HashMap<>() : serviceInstance.getInterfaces(); + instanceInterfaces.remove(interfaceDefinition.getUniqueId()); + instanceInterfaces.put(interfaceDefinition.getUniqueId(), interfaceDefinition); + serviceInstance.setInterfaces(instanceInterfaces); + removeComponentInstanceInterfaceByInterfaceId(interfaceDefinition.getUniqueId(), componentInstancesInterfaces.get(serviceInstanceId)); + componentInstancesInterfaces.get(serviceInstanceId) + .add(new ComponentInstanceInterface(interfaceDefinition.getUniqueId(), interfaceDefinition)); + parentService.getComponentInstances().add(serviceInstance); + StorageOperationStatus status = toscaOperationFacade.updateComponentInstanceInterfaces(parentService, serviceInstanceId); + if (status != StorageOperationStatus.OK) { + return Either.right(componentsUtils.getResponseFormat(ActionStatus.INTERFACE_OPERATION_NOT_FOUND, serviceInstanceId)); + } + return Either.left(operation); + } + + private void removeComponentInstanceInterfaceByInterfaceId(String interfaceIdToRemove, List instanceInterfaces) { + if (CollectionUtils.isEmpty(instanceInterfaces)) { + return; + } + Optional interfaceToRemove = instanceInterfaces.stream() + .filter(instInterface -> instInterface.getUniqueId().equals(interfaceIdToRemove)).findAny(); + if (interfaceToRemove.isPresent()) { + instanceInterfaces.remove(interfaceToRemove.get()); + } + } + + private Either handleConsumptionValue(Service containerService, String serviceInstanceId, + ServiceConsumptionData serviceConsumptionData, Operation operation, + OperationInputDefinition operationInputDefinition) { + String source = serviceConsumptionData.getSource(); + String consumptionValue = serviceConsumptionData.getValue(); + String type = serviceConsumptionData.getType(); + String operationIdentifier = + consumptionValue.contains(".") ? consumptionValue.substring(0, consumptionValue.lastIndexOf('.')) : consumptionValue; + ServiceConsumptionSource sourceValue = ServiceConsumptionSource.getSourceValue(source); + if (STATIC.equals(sourceValue)) { + // Validate constraint on input value + Either constraintValidationResult = validateOperationInputConstraint(operationInputDefinition, consumptionValue, + type, containerService.getModel()); + if (constraintValidationResult.isRight()) { + return Either.right(constraintValidationResult.right().value()); + } + return handleConsumptionStaticValue(consumptionValue, type, operation, operationInputDefinition, containerService.getModel()); + } + if (Objects.isNull(sourceValue)) { + List propertyDefinitions; + Map> capabilities = null; + String componentName; + List outputs = null; + if (source.equals(containerService.getUniqueId())) { + Either serviceToTakePropEither = toscaOperationFacade.getToscaElement(source); + if (serviceToTakePropEither.isRight()) { + return Either.right(componentsUtils.getResponseFormat(serviceToTakePropEither.right().value())); + } + Service service = serviceToTakePropEither.left().value(); + operationInputDefinition.setSource(service.getUniqueId()); + sourceValue = SERVICE_INPUT; + propertyDefinitions = service.getProperties(); + componentName = service.getName(); + outputs = InterfaceOperationUtils.getOtherOperationOutputsOfComponent(operationIdentifier, service.getInterfaces()) + .getListToscaDataDefinition(); + } else { + Optional getComponentInstance = containerService.getComponentInstanceById(source); + if (!getComponentInstance.isPresent()) { + return Either.right(componentsUtils.getResponseFormat(ActionStatus.COMPONENT_INSTANCE_NOT_FOUND_ON_CONTAINER, source)); + } + ComponentInstance componentInstance = getComponentInstance.get(); + operationInputDefinition.setSource(componentInstance.getUniqueId()); + propertyDefinitions = componentInstance.getProperties(); + capabilities = componentInstance.getCapabilities(); + componentName = source.equals(serviceInstanceId) ? SELF : componentInstance.getName(); + if (MapUtils.isNotEmpty(componentInstance.getInterfaces())) { + Map componentInstanceInterfaces = componentInstance.getInterfaces().entrySet().stream() + .collect(Collectors.toMap((Map.Entry::getKey), (interfaceEntry -> (InterfaceDataDefinition) interfaceEntry.getValue()))); + outputs = InterfaceOperationUtils.getOtherOperationOutputsOfComponent(operationIdentifier, componentInstanceInterfaces) + .getListToscaDataDefinition(); + } + } + if (sourceValue == ServiceConsumptionSource.SERVICE_INPUT) { + //The operation input in service consumption has been mapped to an input in the parent service + return handleConsumptionInputValue(consumptionValue, containerService, operation, operationInputDefinition); + } + return handleConsumptionPropertyValue(operation, operationInputDefinition, serviceConsumptionData, propertyDefinitions, capabilities, + outputs, componentName); + } + operationInputDefinition.setToscaPresentationValue(JsonPresentationFields.SOURCE, source); + operationInputDefinition.setSource(source); + return Either.left(operation); + } + + private Optional getOperationInputByInputId(ServiceConsumptionData serviceConsumptionData, + ListDataDefinition inputs) { + if (CollectionUtils.isEmpty(inputs.getListToscaDataDefinition())) { + return Optional.empty(); + } + return inputs.getListToscaDataDefinition().stream() + .filter(operationInput -> operationInput.getInputId().equals(serviceConsumptionData.getInputId())).findAny(); + } + + private Either handleConsumptionPropertyValue(Operation operation, OperationInputDefinition operationInputDefinition, + ServiceConsumptionData serviceConsumptionData, + List properties, + Map> capabilities, + List outputs, String componentName) { + if (CollectionUtils.isEmpty(properties) && CollectionUtils.isEmpty(outputs)) { + return Either.left(operation); + } + String consumptionValue = serviceConsumptionData.getValue(); + if (CollectionUtils.isNotEmpty(outputs) && isOperationInputMappedToOtherOperationOutput(getOperationOutputName(consumptionValue), outputs)) { + return handleConsumptionInputMappedToOperationOutput(operation, operationInputDefinition, outputs, consumptionValue, componentName); + } + if (CollectionUtils.isNotEmpty(properties) && PropertiesUtils.isNodeProperty(consumptionValue, properties)) { + return handleConsumptionInputMappedToProperty(operation, operationInputDefinition, serviceConsumptionData, properties, componentName); + } + if (MapUtils.isNotEmpty(capabilities)) { + return handleConsumptionInputMappedToCapabilityProperty(operation, operationInputDefinition, serviceConsumptionData, capabilities, + componentName); + } + return Either.left(operation); + } + + private Either handleConsumptionInputMappedToProperty(Operation operation, + OperationInputDefinition operationInputDefinition, + ServiceConsumptionData serviceConsumptionData, + List properties, String componentName) { + Optional servicePropertyCandidate = properties.stream() + .filter(property -> property.getName().equals(serviceConsumptionData.getValue())).findAny(); + if (servicePropertyCandidate.isPresent()) { + boolean isInputTypeSimilarToOperation = isAssignedValueFromValidType(operationInputDefinition.getType(), servicePropertyCandidate.get()); + if (!isInputTypeSimilarToOperation) { + return Either.right(componentsUtils.getResponseFormat(ActionStatus.INVALID_CONSUMPTION_TYPE, operationInputDefinition.getType())); + } + addPropertyToInputValue(componentName, operation, operationInputDefinition, servicePropertyCandidate.get()); + } + return Either.left(operation); + } + + private Either handleConsumptionInputMappedToOperationOutput(Operation operation, + OperationInputDefinition operationInputDefinition, + List outputs, + String consumptionValue, String componentName) { + String outputName = getOperationOutputName(consumptionValue); + Optional servicePropertyOutputCandidate = outputs.stream().filter(output -> output.getName().equals(outputName)) + .findAny(); + if (servicePropertyOutputCandidate.isPresent()) { + boolean isInputTypeSimilarToOperation = isAssignedValueFromValidType(operationInputDefinition.getType(), + servicePropertyOutputCandidate.get()); + if (!isInputTypeSimilarToOperation) { + return Either.right(componentsUtils.getResponseFormat(ActionStatus.INVALID_CONSUMPTION_TYPE, operationInputDefinition.getType())); + } + addOutputToInputValue(componentName, consumptionValue, operation, operationInputDefinition); + } + return Either.left(operation); + } + + private void addPropertyToInputValue(String componentName, Operation operation, OperationInputDefinition operationInputDefinition, + PropertyDefinition serviceProperty) { + Map> getProperty = new HashMap<>(); + List getPropertyValues = new ArrayList<>(); + getPropertyValues.add(componentName); + getPropertyValues.add(serviceProperty.getName()); + getProperty.put(ToscaFunctions.GET_PROPERTY.getFunctionName(), getPropertyValues); + operationInputDefinition.setSourceProperty(serviceProperty.getUniqueId()); + operation.getInputs().delete(operationInputDefinition); + operationInputDefinition.setToscaPresentationValue(JsonPresentationFields.GET_PROPERTY, getPropertyValues); + operationInputDefinition.setValue((new Gson()).toJson(getProperty)); + operation.getInputs().add(operationInputDefinition); + } + + private void addOutputToInputValue(String componentName, String consumptionValue, Operation operation, + OperationInputDefinition operationInputDefinition) { + Map> getOperationOutput = InterfaceOperationUtils.createMappedOutputDefaultValue(componentName, consumptionValue); + operation.getInputs().delete(operationInputDefinition); + operationInputDefinition.setToscaPresentationValue(JsonPresentationFields.GET_OPERATION_OUTPUT, getOperationOutput); + operationInputDefinition.setValue((new Gson()).toJson(getOperationOutput)); + operation.getInputs().add(operationInputDefinition); + } + + public Either handleConsumptionStaticValue(String value, String type, Operation operation, + OperationInputDefinition operationInputDefinition, String model) { + boolean isInputTypeSimilarToOperation = isAssignedValueFromValidType(type, value); + if (!isInputTypeSimilarToOperation) { + return Either.right(componentsUtils.getResponseFormat(ActionStatus.INVALID_CONSUMPTION_TYPE, type)); + } + //Validate Constraint and Value + Either constraintValidationResponse = validateOperationInputConstraint(operationInputDefinition, value, type, model); + if (constraintValidationResponse.isRight()) { + return Either.right(constraintValidationResponse.right().value()); + } + addStaticValueToInputOperation(value, operation, operationInputDefinition); + return Either.left(operation); + } + + private Either validateOperationInputConstraint(OperationInputDefinition operationInputDefinition, String value, + String type, String model) { + ComponentInstanceProperty propertyDefinition = new ComponentInstanceProperty(); + propertyDefinition.setType(operationInputDefinition.getParentPropertyType()); + InputDefinition inputDefinition = new InputDefinition(); + inputDefinition.setDefaultValue(value); + inputDefinition.setInputPath(operationInputDefinition.getSubPropertyInputPath()); + inputDefinition.setType(type); + if (Objects.nonNull(operationInputDefinition.getParentPropertyType())) { + inputDefinition.setProperties(Collections.singletonList(propertyDefinition)); + } + return new PropertyValueConstraintValidationUtil().validatePropertyConstraints(Collections.singletonList(inputDefinition), + applicationDataTypeCache, model); + } + + private void addStaticValueToInputOperation(String value, Operation operation, OperationInputDefinition operationInputDefinition) { + operation.getInputs().delete(operationInputDefinition); + operationInputDefinition.setSource(STATIC.getSource()); + operationInputDefinition.setSourceProperty(null); + operationInputDefinition.setValue(value); + operation.getInputs().add(operationInputDefinition); + } + + private Either handleConsumptionInputValue(String inputId, Service service, Operation operation, + OperationInputDefinition operationInputDefinition) { + List serviceInputs = service.getInputs(); + Optional inputForValue = serviceInputs.stream().filter(input -> input.getUniqueId().contains(inputId)).findAny(); + if (inputForValue.isPresent()) { + boolean isInputTypeSimilarToOperation = isAssignedValueFromValidType(operationInputDefinition.getType(), inputForValue.get()); + if (!isInputTypeSimilarToOperation) { + return Either.right(componentsUtils.getResponseFormat(ActionStatus.INVALID_CONSUMPTION_TYPE, operationInputDefinition.getType())); + } + addGetInputValueToOperationInput(operation, operationInputDefinition, inputForValue.get()); + } + return Either.left(operation); + } + + private void addGetInputValueToOperationInput(Operation operation, OperationInputDefinition operationInputDefinition, + InputDefinition inputForValue) { + operation.getInputs().delete(operationInputDefinition); + Map getInputMap = new HashMap<>(); + getInputMap.put(ToscaFunctions.GET_INPUT.getFunctionName(), inputForValue.getName()); + operationInputDefinition.setSourceProperty(inputForValue.getUniqueId()); + operationInputDefinition.setToscaPresentationValue(JsonPresentationFields.GET_INPUT, getInputMap); + operationInputDefinition.setValue(new Gson().toJson(getInputMap)); + operation.getInputs().add(operationInputDefinition); } private Either>, ActionStatus> getAuditRecordsForUncertifiedComponent(String componentUUID, String componentVersion) { // First Query - Either, ActionStatus> eitherprevVerAudit = auditCassandraDao.getAuditByServiceIdAndPrevVersion(componentUUID, componentVersion); - + Either, ActionStatus> eitherprevVerAudit = auditCassandraDao + .getAuditByServiceIdAndPrevVersion(componentUUID, componentVersion); if (eitherprevVerAudit.isRight()) { return Either.right(eitherprevVerAudit.right().value()); } - // Second Query - Either, ActionStatus> eitherCurrVerAudit = auditCassandraDao.getAuditByServiceIdAndCurrVersion(componentUUID, componentVersion); + Either, ActionStatus> eitherCurrVerAudit = auditCassandraDao + .getAuditByServiceIdAndCurrVersion(componentUUID, componentVersion); if (eitherCurrVerAudit.isRight()) { return Either.right(eitherCurrVerAudit.right().value()); } - + Either, ActionStatus> eitherArchiveRestoreList = getArchiveRestoreEventList(componentUUID); + if (eitherArchiveRestoreList.isRight()) { + return Either.right(eitherArchiveRestoreList.right().value()); + } List> prevVerAuditList = getAuditingFieldsList(eitherprevVerAudit.left().value()); List> currVerAuditList = getAuditingFieldsList(eitherCurrVerAudit.left().value()); - - List> duplicateElements = new ArrayList>(); + List> duplicateElements = new ArrayList<>(); duplicateElements.addAll(prevVerAuditList); duplicateElements.retainAll(currVerAuditList); - - List> joinedNonDuplicatedList = new ArrayList>(); + List> joinedNonDuplicatedList = new ArrayList<>(); joinedNonDuplicatedList.addAll(prevVerAuditList); joinedNonDuplicatedList.removeAll(duplicateElements); joinedNonDuplicatedList.addAll(currVerAuditList); - + joinedNonDuplicatedList.addAll(getAuditingFieldsList(eitherArchiveRestoreList.left().value())); return Either.left(joinedNonDuplicatedList); } - private List> getAuditingFieldsList(List prevVerAuditList) { + private Either, ActionStatus> getArchiveRestoreEventList(String componentUUID) { + // Archive Query + Either, ActionStatus> eitherArchiveAudit = auditCassandraDao.getArchiveAuditByServiceInstanceId(componentUUID); + if (eitherArchiveAudit.isRight()) { + return Either.right(eitherArchiveAudit.right().value()); + } + // Restore Query + Either, ActionStatus> eitherRestoreAudit = auditCassandraDao.getRestoreAuditByServiceInstanceId(componentUUID); + if (eitherRestoreAudit.isRight()) { + return Either.right(eitherRestoreAudit.right().value()); + } + List archiveAudit = new ArrayList<>(); + archiveAudit.addAll(eitherArchiveAudit.left().value()); + archiveAudit.addAll(eitherRestoreAudit.left().value()); + return Either.left(archiveAudit); + } - List> prevVerAudit = new ArrayList>(); + private List> getAuditingFieldsList(List prevVerAuditList) { + List> prevVerAudit = new ArrayList<>(); for (AuditingGenericEvent auditEvent : prevVerAuditList) { auditEvent.fillFields(); prevVerAudit.add(auditEvent.getFields()); @@ -296,109 +665,109 @@ public class ServiceBusinessLogic extends ComponentBusinessLogic { /** * createService * - * @param service - * - Service - * @param user - * - modifier data (userId) + * @param service - Service + * @param user - modifier data (userId) * @return Either */ public Either createService(Service service, User user) { - // get user details - Either eitherCreator = validateUser(user, "Create Service", service, AuditingActionEnum.CREATE_RESOURCE, false); - if (eitherCreator.isRight()) { - return Either.right(eitherCreator.right().value()); - } - user = eitherCreator.left().value(); - + user = validateUser(user, "Create Service", service, AuditingActionEnum.CREATE_RESOURCE, false); + log.debug("User returned from validation: {}", user); // validate user role - Either validateRes = validateUserRole(user, service, new ArrayList(), AuditingActionEnum.CREATE_RESOURCE, null); - if (validateRes.isRight()) { - return Either.right(validateRes.right().value()); - } + validateUserRole(user, service, new ArrayList<>(), AuditingActionEnum.CREATE_RESOURCE, null); service.setCreatorUserId(user.getUserId()); - // warn on overridden fields - checkFieldsForOverideAttampt(service); + checkFieldsForOverideAttempt(service); // enrich object log.debug("enrich service with version and state"); service.setState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT); service.setVersion(INITIAL_VERSION); service.setConformanceLevel(ConfigurationManager.getConfigurationManager().getConfiguration().getToscaConformanceLevel()); service.setDistributionStatus(DistributionStatusEnum.DISTRIBUTION_NOT_APPROVED); - + service.setComponentType(ComponentTypeEnum.SERVICE); Either createServiceResponse = validateServiceBeforeCreate(service, user, AuditingActionEnum.CREATE_RESOURCE); if (createServiceResponse.isRight()) { return createServiceResponse; } - return createServiceByDao(service, AuditingActionEnum.CREATE_RESOURCE, user); + return createServiceByDao(service, user).left().bind(c -> updateCatalog(c, ChangeTypeEnum.LIFECYCLE).left().map(Service.class::cast)); } - private void checkFieldsForOverideAttampt(Service service) { + private void checkFieldsForOverideAttempt(Service service) { checkComponentFieldsForOverrideAttempt(service); if (service.getDistributionStatus() != null) { log.info("Distribution Status cannot be defined by user. This field will be overridden by the application"); } } - private Either createServiceByDao(Service service, AuditingActionEnum actionEnum, User user) { + private Either createServiceByDao(final Service service, final User user) { log.debug("send service {} to dao for create", service.getComponentMetadataDefinition().getMetadataDataDefinition().getName()); - Either lockResult = lockComponentByName(service.getSystemName(), service, "Create Service"); if (lockResult.isRight()) { ResponseFormat responseFormat = lockResult.right().value(); - componentsUtils.auditComponentAdmin(responseFormat, user, service, actionEnum, ComponentTypeEnum.SERVICE); + componentsUtils.auditComponentAdmin(responseFormat, user, service, AuditingActionEnum.CREATE_RESOURCE, ComponentTypeEnum.SERVICE); return Either.right(responseFormat); } - log.debug("System name locked is {}, status = {}", service.getSystemName(), lockResult); - try { - createMandatoryArtifactsData(service, user); createServiceApiArtifactsData(service, user); setToscaArtifactsPlaceHolders(service, user); - Either genericServiceEither = fetchAndSetDerivedFromGenericType(service); - if (genericServiceEither.isRight()) - return Either.right(genericServiceEither.right().value()); - - generateAndAddInputsFromGenericTypeProperties(service, genericServiceEither.left().value()); + if (service.isSubstituteCandidate() || genericTypeBusinessLogic.hasMandatorySubstitutionType(service)) { + final Resource genericType = fetchAndSetDerivedFromGenericType(service); + generatePropertiesFromGenericType(service, genericType); + if (Constants.DEFAULT_MODEL_NAME.equals(service.getModel()) || service.getModel() == null) { + generateAndAddInputsFromGenericTypeProperties(service, genericType); + } + } + beforeCreate(service); Either dataModelResponse = toscaOperationFacade.createToscaComponent(service); - - // service created successfully!!! if (dataModelResponse.isLeft()) { - log.debug("Service created successfully!!!"); + log.debug("Service '{}' created successfully", service.getName()); ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.CREATED); - componentsUtils.auditComponentAdmin(responseFormat, user, service, actionEnum, ComponentTypeEnum.SERVICE); + componentsUtils.auditComponentAdmin(responseFormat, user, service, AuditingActionEnum.CREATE_RESOURCE, ComponentTypeEnum.SERVICE); ASDCKpiApi.countCreatedServicesKPI(); return Either.left(dataModelResponse.left().value()); } - - ResponseFormat responseFormat = componentsUtils.getResponseFormatByComponent(componentsUtils.convertFromStorageResponse(dataModelResponse.right().value()), service, ComponentTypeEnum.SERVICE); - log.debug("audit before sending response"); - componentsUtils.auditComponentAdmin(responseFormat, user, service, actionEnum, ComponentTypeEnum.SERVICE); + janusGraphDao.rollback(); + ResponseFormat responseFormat = componentsUtils + .getResponseFormatByComponent(componentsUtils.convertFromStorageResponse(dataModelResponse.right().value()), service, + ComponentTypeEnum.SERVICE); + log.debug(AUDIT_BEFORE_SENDING_RESPONSE); + componentsUtils.auditComponentAdmin(responseFormat, user, service, AuditingActionEnum.CREATE_RESOURCE, ComponentTypeEnum.SERVICE); return Either.right(responseFormat); - } finally { graphLockOperation.unlockComponentByName(service.getSystemName(), service.getUniqueId(), NodeTypeEnum.Service); } } + private void beforeCreate(final Service service) { + if (CollectionUtils.isEmpty(serviceCreationPluginList)) { + return; + } + serviceCreationPluginList.stream().sorted(Comparator.comparingInt(ServiceCreationPlugin::getOrder)).forEach(serviceCreationPlugin -> { + try { + serviceCreationPlugin.beforeCreate(service); + } catch (final Exception e) { + log.error("An error has occurred while running the serviceCreationPlugin '{}'", serviceCreationPlugin.getClass(), e); + } + }); + } + @SuppressWarnings("unchecked") private void createServiceApiArtifactsData(Service service, User user) { // create mandatory artifacts // TODO it must be removed after that artifact uniqueId creation will be + // moved to ArtifactOperation String serviceUniqueId = service.getUniqueId(); Map artifactMap = service.getServiceApiArtifacts(); - if (artifactMap == null) - artifactMap = new HashMap(); - + if (artifactMap == null) { + artifactMap = new HashMap<>(); + } Map serviceApiArtifacts = ConfigurationManager.getConfigurationManager().getConfiguration().getServiceApiArtifacts(); List exludeServiceCategory = ConfigurationManager.getConfigurationManager().getConfiguration().getExcludeServiceCategory(); - List categories = service.getCategories(); boolean isCreateArtifact = true; if (categories != null && exludeServiceCategory != null && !exludeServiceCategory.isEmpty()) { @@ -408,120 +777,53 @@ public class ServiceBusinessLogic extends ComponentBusinessLogic { break; } } - } - if (serviceApiArtifacts != null && isCreateArtifact) { Set keys = serviceApiArtifacts.keySet(); for (String serviceApiArtifactName : keys) { Map artifactInfoMap = (Map) serviceApiArtifacts.get(serviceApiArtifactName); - ArtifactDefinition artifactDefinition = createArtifactDefinition(serviceUniqueId, serviceApiArtifactName, artifactInfoMap, user, true); + ArtifactDefinition artifactDefinition = createArtifactDefinition(serviceUniqueId, serviceApiArtifactName, artifactInfoMap, user, + true); artifactDefinition.setArtifactGroupType(ArtifactGroupTypeEnum.SERVICE_API); artifactMap.put(artifactDefinition.getArtifactLabel(), artifactDefinition); } - service.setServiceApiArtifacts(artifactMap); } } - private Either validateServiceBeforeCreate(Service service, User user, AuditingActionEnum actionEnum) { - - Either validationResponse = validateServiceFieldsBeforeCreate(user, service, actionEnum); - if (validationResponse.isRight()) { - return Either.right(validationResponse.right().value()); + @VisibleForTesting + protected Either validateServiceBeforeCreate(Service service, User user, AuditingActionEnum actionEnum) { + try { + serviceValidator.validate(user, service, actionEnum); + } catch (ComponentException exp) { + ResponseFormat responseFormat = componentsUtils.getResponseFormat(exp); + componentsUtils.auditComponentAdmin(responseFormat, user, service, AuditingActionEnum.CREATE_SERVICE, ComponentTypeEnum.SERVICE); + throw exp; + } + if (!AuditingActionEnum.UPDATE_SERVICE_TOSCA_TEMPLATE.equals(actionEnum) && + !AuditingActionEnum.UPDATE_SERVICE_TOSCA_MODEL.equals(actionEnum)) { + service.setCreatorFullName(user.getFirstName() + " " + user.getLastName()); + service.setContactId(service.getContactId().toLowerCase()); + // Generate invariant UUID - must be here and not in operation since it should stay constant during clone + service.setInvariantUUID(UniqueIdBuilder.buildInvariantUUID()); } - service.setCreatorFullName(user.getFirstName() + " " + user.getLastName()); - service.setContactId(service.getContactId().toLowerCase()); - - // Generate invariant UUID - must be here and not in operation since it - // should stay constant during clone - String invariantUUID = UniqueIdBuilder.buildInvariantUUID(); - service.setInvariantUUID(invariantUUID); - return Either.left(service); } - private Either validateServiceFieldsBeforeCreate(User user, Service service, AuditingActionEnum actionEnum) { - Either componentsFieldsValidation = validateComponentFieldsBeforeCreate(user, service, actionEnum); - if (componentsFieldsValidation.isRight()) { - return componentsFieldsValidation; - } - - log.debug("validate service name uniqueness"); - Either serviceNameUniquenessValidation = validateComponentNameUnique(user, service, actionEnum); - if (serviceNameUniquenessValidation.isRight()) { - return serviceNameUniquenessValidation; - } - - log.debug("validate category"); - Either categoryValidation = validateServiceCategory(user, service, actionEnum); - if (categoryValidation.isRight()) { - return categoryValidation; - } - - // validate project name (ProjectCode) - mandatory in service - log.debug("validate projectName"); - Either projectCodeValidation = validateProjectCode(user, service, actionEnum); - if (projectCodeValidation.isRight()) { - return projectCodeValidation; - } - - log.debug("validate service type"); - Either serviceTypeValidation = validateServiceTypeAndCleanup(user, service, actionEnum); - if (serviceTypeValidation.isRight()) { - return serviceTypeValidation; - } - - log.debug("validate service role"); - Either serviceRoleValidation = validateServiceRoleAndCleanup(user, service, actionEnum); - if (serviceRoleValidation.isRight()) { - return serviceRoleValidation; - } - - return Either.left(true); - - } - - private Either validateServiceCategory(User user, Service service, AuditingActionEnum actionEnum) { - log.debug("validate Service category"); - - if (service.getCategories() == null || service.getCategories().size() == 0) { - ResponseFormat errorResponse = componentsUtils.getResponseFormat(ActionStatus.COMPONENT_MISSING_CATEGORY, ComponentTypeEnum.SERVICE.getValue()); - componentsUtils.auditComponentAdmin(errorResponse, user, service, actionEnum, ComponentTypeEnum.SERVICE); - return Either.right(errorResponse); - } - - Either validatCategory = validateServiceCategory(service.getCategories()); - if (validatCategory.isRight()) { - ResponseFormat responseFormat = validatCategory.right().value(); - componentsUtils.auditComponentAdmin(responseFormat, user, service, actionEnum, ComponentTypeEnum.SERVICE); - return Either.right(responseFormat); - } - - return Either.left(true); - } - public Either, ResponseFormat> validateServiceNameExists(String serviceName, String userId) { - - Either resp = validateUserExists(userId, "validate Service Name Exists", false); - if (resp.isRight()) { - return Either.right(resp.right().value()); - } - - Either dataModelResponse = toscaOperationFacade.validateComponentNameUniqueness(serviceName, null, ComponentTypeEnum.SERVICE); - + validateUserExists(userId); + Either dataModelResponse = toscaOperationFacade + .validateComponentNameUniqueness(serviceName, null, ComponentTypeEnum.SERVICE); // DE242223 - titanDao.commit(); - + janusGraphDao.commit(); if (dataModelResponse.isLeft()) { Map result = new HashMap<>(); - result.put("isValid", dataModelResponse.left().value()); + result.put(IS_VALID, dataModelResponse.left().value()); log.debug("validation was successfully performed."); return Either.left(result); } - - ResponseFormat responseFormat = componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(dataModelResponse.right().value())); - + ResponseFormat responseFormat = componentsUtils + .getResponseFormat(componentsUtils.convertFromStorageResponse(dataModelResponse.right().value())); return Either.right(responseFormat); } @@ -529,6 +831,7 @@ public class ServiceBusinessLogic extends ComponentBusinessLogic { this.elementDao = elementDao; } + @Autowired public void setCassandraAuditingDao(AuditCassandraDao auditingDao) { this.auditCassandraDao = auditingDao; } @@ -542,121 +845,124 @@ public class ServiceBusinessLogic extends ComponentBusinessLogic { } public Either updateServiceMetadata(String serviceId, Service serviceUpdate, User user) { - Either eitherCreator = validateUser(user, "updateServiceMetadata", serviceUpdate, null, false); - if (eitherCreator.isRight()) { - return Either.right(eitherCreator.right().value()); - } - user = eitherCreator.left().value(); - + user = validateUser(user, "updateServiceMetadata", serviceUpdate, null, false); // validate user role - Either validateRes = validateUserRole(user, serviceUpdate, new ArrayList<>(), null, null); - if (validateRes.isRight()) { - return Either.right(validateRes.right().value()); - } - + validateUserRole(user, serviceUpdate, new ArrayList<>(), null, null); Either storageStatus = toscaOperationFacade.getToscaElement(serviceId); if (storageStatus.isRight()) { - return Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(storageStatus.right().value(), ComponentTypeEnum.SERVICE), "")); + return Either.right(componentsUtils + .getResponseFormat(componentsUtils.convertFromStorageResponse(storageStatus.right().value(), ComponentTypeEnum.SERVICE), "")); } - Service currentService = storageStatus.left().value(); - if (!ComponentValidationUtils.canWorkOnComponent(currentService, user.getUserId())) { log.info("Restricted operation for user: {}, on service: {}", user.getUserId(), currentService.getCreatorUserId()); return Either.right(componentsUtils.getResponseFormat(ActionStatus.RESTRICTED_OPERATION)); } - - Either validationRsponse = validateAndUpdateServiceMetadata(user, currentService, serviceUpdate); - if (validationRsponse.isRight()) { + List subNodePropsToBeRemoved = getSubstitutionNodePropertiesToBeRemoved(currentService, serviceUpdate); + List subNodePropsToBeAdded = getSubstitutionNodePropertiesToBeAdded(currentService, serviceUpdate); + boolean subNodeChanged = isSubstitutionNodeChanged(currentService, serviceUpdate); + Either validationResponse = + validateAndUpdateServiceMetadata(user, currentService, serviceUpdate, subNodeChanged, ListUtils.emptyIfNull(subNodePropsToBeRemoved)); + if (validationResponse.isRight()) { log.info("service update metadata: validations field."); - return validationRsponse; + return validationResponse; } - Service serviceToUpdate = validationRsponse.left().value(); + Service serviceToUpdate = validationResponse.left().value(); // lock resource - - Either lockResult = lockComponent(serviceId, currentService, "Update Service Metadata"); - if (lockResult.isRight()) { - return Either.right(lockResult.right().value()); - } + lockComponent(serviceId, currentService, "Update Service Metadata"); try { - Either updateResponse = toscaOperationFacade.updateToscaElement(serviceToUpdate); - if (updateResponse.isRight()) { - titanDao.rollback(); + if (subNodeChanged) { + if (!subNodePropsToBeRemoved.isEmpty()) { + removePropertiesFromService(currentService, subNodePropsToBeRemoved); + removeInputsFromService(currentService, subNodePropsToBeRemoved); + } + if (!subNodePropsToBeAdded.isEmpty()) { + addPropertiesToService(currentService, subNodePropsToBeAdded); + if (Constants.DEFAULT_MODEL_NAME.equals(currentService.getModel()) || currentService.getModel() == null) { + addInputsToService(currentService, subNodePropsToBeAdded); + } + } + } + return toscaOperationFacade.updateToscaElement(serviceToUpdate).right().map(rf -> { + janusGraphDao.rollback(); BeEcompErrorManager.getInstance().logBeSystemError("Update Service Metadata"); log.debug("failed to update sevice {}", serviceToUpdate.getUniqueId()); - return Either.right(componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR)); - } - titanDao.commit(); - return Either.left(updateResponse.left().value()); + return (componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR)); + }).left().bind(this::updateCatalogAndCommit); + } catch (ComponentException e) { + janusGraphDao.rollback(); + BeEcompErrorManager.getInstance().logBeSystemError("Update Service Metadata"); + log.debug("failed to update sevice {}", serviceToUpdate.getUniqueId()); + return Either.right(componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR)); } finally { graphLockOperation.unlockComponent(serviceId, NodeTypeEnum.Service); } } - public Either, ResponseFormat> deleteForwardingPaths(String serviceId, Set pathIdsToDelete, User user, boolean lock) { - Service serviceToDelete = initServiceToDeletePaths(serviceId, pathIdsToDelete); - Either eitherCreator = validateUser(user, "deleteForwardingPaths", serviceToDelete, null, false); - if (eitherCreator.isRight()) { - return Either.right(eitherCreator.right().value()); - } - user = eitherCreator.left().value(); + private Either updateCatalogAndCommit(Service service) { + Either res = updateCatalog(service, ChangeTypeEnum.LIFECYCLE).left().map(Service.class::cast); + janusGraphDao.commit(); + return res; + } + public Set deleteForwardingPaths(String serviceId, Set pathIdsToDelete, User user, boolean lock) { + Service serviceToDelete = initServiceToDeletePaths(serviceId, pathIdsToDelete); + user = validateUser(user, "deleteForwardingPaths", serviceToDelete, null, false); // validate user role - Either validateRes = validateUserRole(user, serviceToDelete, new ArrayList<>(), null, null); - if (validateRes.isRight()) { - return Either.right(validateRes.right().value()); - } + validateUserRole(user, serviceToDelete, new ArrayList<>(), null, null); Either storageStatus = toscaOperationFacade.getToscaElement(serviceId); if (storageStatus.isRight()) { - return Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(storageStatus.right().value(), ComponentTypeEnum.SERVICE), "")); + throw new ByActionStatusComponentException( + componentsUtils.convertFromStorageResponse(storageStatus.right().value(), ComponentTypeEnum.SERVICE), ""); } Service service = storageStatus.left().value(); Either, StorageOperationStatus> result = null; if (lock) { - Either lockResult = lockComponent(service.getUniqueId(), service, "Delete Forwarding Path on Service"); - if (lockResult.isRight()) { - titanDao.rollback(); - return Either.right(componentsUtils.getResponseFormat(componentsUtils - .convertFromStorageResponse(storageStatus.right().value(), ComponentTypeEnum.SERVICE), "")); + try { + lockComponent(service.getUniqueId(), service, "Delete Forwarding Path on Service"); + } catch (ComponentException e) { + janusGraphDao.rollback(); + throw new ByActionStatusComponentException( + componentsUtils.convertFromStorageResponse(storageStatus.right().value(), ComponentTypeEnum.SERVICE), ""); } } - try{ - result = forwardingPathOperation.deleteForwardingPath(service ,pathIdsToDelete); + try { + result = forwardingPathOperation.deleteForwardingPath(service, pathIdsToDelete); if (result.isRight()) { - log.debug("Failed to lock service {}. Response is {}. ", service.getName(), result.right().value()); - titanDao.rollback(); - return Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(storageStatus.right().value(), ComponentTypeEnum.SERVICE))); + log.debug(FAILED_TO_LOCK_SERVICE_RESPONSE_IS, service.getName(), result.right().value()); + janusGraphDao.rollback(); + throw new ByActionStatusComponentException( + componentsUtils.convertFromStorageResponse(storageStatus.right().value(), ComponentTypeEnum.SERVICE)); } - titanDao.commit(); - log.debug("The service with system name {} locked. ", service.getSystemName()); - - } catch (Exception e){ + janusGraphDao.commit(); + log.debug(THE_SERVICE_WITH_SYSTEM_NAME_LOCKED, service.getSystemName()); + } catch (ComponentException e) { log.error("Exception occurred during delete forwarding path : {}", e.getMessage(), e); - titanDao.rollback(); - return Either.right(componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR)); + janusGraphDao.rollback(); + throw new ByActionStatusComponentException(ActionStatus.GENERAL_ERROR); } finally { - graphLockOperation.unlockComponent(service.getUniqueId(), NodeTypeEnum.Service); + graphLockOperation.unlockComponent(service.getUniqueId(), NodeTypeEnum.Service); } - return Either.left(result.left().value()); + return result.left().value(); } private Service initServiceToDeletePaths(String serviceId, Collection pathIdsToDelete) { Service serviceToDelete = new Service(); serviceToDelete.setUniqueId(serviceId); serviceToDelete.setForwardingPaths(new HashMap<>()); - pathIdsToDelete.forEach(pathIdToDelete -> serviceToDelete.getForwardingPaths().put(pathIdToDelete, new ForwardingPathDataDefinition())); + pathIdsToDelete.forEach(pathIdToDelete -> serviceToDelete.getForwardingPaths().put(pathIdToDelete, new ForwardingPathDataDefinition())); return serviceToDelete; } - public Either updateForwardingPath(String serviceId, Service serviceUpdate, User user, boolean lock) { - return createOrUpdateForwardingPath(serviceId, serviceUpdate, user, true,"updateForwardingPath", lock); + public Service updateForwardingPath(String serviceId, Service serviceUpdate, User user, boolean lock) { + return createOrUpdateForwardingPath(serviceId, serviceUpdate, user, true, "updateForwardingPath", lock); } - public Either createForwardingPath(String serviceId, Service serviceUpdate, User user, boolean lock) { + public Service createForwardingPath(String serviceId, Service serviceUpdate, User user, boolean lock) { return createOrUpdateForwardingPath(serviceId, serviceUpdate, user, false, "createForwardingPath", lock); } - private ForwardingPathDataDefinition getTrimmedValues(ForwardingPathDataDefinition path){ + private ForwardingPathDataDefinition getTrimmedValues(ForwardingPathDataDefinition path) { ForwardingPathDataDefinition dataDefinition = new ForwardingPathDataDefinition(path.getName()); dataDefinition.setName(Strings.nullToEmpty(path.getName()).trim()); dataDefinition.setProtocol(Strings.nullToEmpty(path.getProtocol()).trim()); @@ -665,295 +971,375 @@ public class ServiceBusinessLogic extends ComponentBusinessLogic { dataDefinition.setPathElements(path.getPathElements()); dataDefinition.setDescription(path.getDescription()); dataDefinition.setToscaResourceName(path.getToscaResourceName()); - return dataDefinition; + return dataDefinition; } - private Either createOrUpdateForwardingPath(String serviceId, Service serviceUpdate, User user, boolean isUpdate, String errorContext, boolean lock) { - Either eitherCreator1 = validateUserAndRole(serviceUpdate, user, errorContext); - if (eitherCreator1 != null) return eitherCreator1; - + private Service createOrUpdateForwardingPath(String serviceId, Service serviceUpdate, User user, boolean isUpdate, String errorContext, + boolean lock) { + validateUserAndRole(serviceUpdate, user, errorContext); Map forwardingPaths = serviceUpdate.getForwardingPaths(); - - Map trimmedForwardingPaths = - forwardingPaths.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, - entry -> new ForwardingPathDataDefinition(getTrimmedValues(entry.getValue())))); - - Either booleanResponseFormatEither = forwardingPathValidator.validateForwardingPaths(trimmedForwardingPaths.values(), - serviceId, isUpdate); - if(booleanResponseFormatEither.isRight()){ - return Either.right(booleanResponseFormatEither.right().value()); - } - + Map trimmedForwardingPaths = forwardingPaths.entrySet().stream() + .collect(Collectors.toMap(Map.Entry::getKey, entry -> new ForwardingPathDataDefinition(getTrimmedValues(entry.getValue())))); + forwardingPathValidator.validateForwardingPaths(trimmedForwardingPaths.values(), serviceId, isUpdate); Either serviceStorageOperationStatusEither = toscaOperationFacade.getToscaElement(serviceId); - - if(serviceStorageOperationStatusEither.isRight()){ + if (serviceStorageOperationStatusEither.isRight()) { StorageOperationStatus errorStatus = serviceStorageOperationStatusEither.right().value(); log.debug("Failed to fetch service information by service id, error {}", errorStatus); - return Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(errorStatus))); + throw new ByActionStatusComponentException(componentsUtils.convertFromStorageResponse(errorStatus)); } Service storedService = serviceStorageOperationStatusEither.left().value(); - - Set forwardingPathDataDefinitions = trimmedForwardingPaths.entrySet().stream().map(entry -> entry.getValue()) - .collect(Collectors.toSet()); - Either result; - Either forwardingPathOrigin = toscaOperationFacade.getLatestByName(ForwardingPathUtils.FORWARDING_PATH_NODE_NAME); - if (forwardingPathOrigin.isRight()) { - StorageOperationStatus errorStatus = forwardingPathOrigin.right().value(); - log.debug("Failed to fetch normative forwarding path resource by tosca name, error {}", errorStatus); - return Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(errorStatus))); - } - Component component = forwardingPathOrigin.left().value(); + Component component = getForwardingPathOriginComponent(); final String toscaResourceName; - if ( component.getComponentType() == ComponentTypeEnum.RESOURCE) { + if (component.getComponentType() == ComponentTypeEnum.RESOURCE) { toscaResourceName = ((Resource) component).getToscaResourceName(); } else { toscaResourceName = ""; } - Either lockResult = null; if (lock) { - lockResult = - lockComponent(storedService.getUniqueId(), storedService, "Add or Update Forwarding Path on Service"); - if (lockResult.isRight()) { - log.debug("Failed to lock service {}. Response is {}. ", storedService.getName(), - lockResult.right().value().getFormattedMessage()); - return Either.right(lockResult.right().value()); - } else { - log.debug("The service with system name {} locked. ", storedService.getSystemName()); - } + lockComponent(storedService.getUniqueId(), storedService, "Add or Update Forwarding Path on Service"); + log.debug(THE_SERVICE_WITH_SYSTEM_NAME_LOCKED, storedService.getSystemName()); } Map resultMap = new HashMap<>(); try { trimmedForwardingPaths.values().forEach(fp -> fp.setToscaResourceName(toscaResourceName)); - - try { - for (ForwardingPathDataDefinition forwardingPathDataDefinition : trimmedForwardingPaths.values()) { - if (isUpdate) { - result = forwardingPathOperation.updateForwardingPath(serviceId, forwardingPathDataDefinition); - } else { - result = forwardingPathOperation.addForwardingPath(serviceId, forwardingPathDataDefinition); - } - if (result.isRight()) { - titanDao.rollback(); - return Either.right(componentsUtils.getResponseFormat( - componentsUtils.convertFromStorageResponse(result.right().value(), ComponentTypeEnum.SERVICE), - "")); - } else { - ForwardingPathDataDefinition fpDataDefinition = result.left().value(); - resultMap.put(fpDataDefinition.getUniqueId(), forwardingPathDataDefinition); - } - } - - } catch (Exception e) { - titanDao.rollback(); - log.error("Exception occurred during add or update forwarding path property values: {}", e.getMessage(), - e); - return Either.right(componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR)); - } - titanDao.commit(); + populateForwardingPaths(serviceId, isUpdate, trimmedForwardingPaths, resultMap); + janusGraphDao.commit(); } finally { - if (lockResult != null && lockResult.isLeft() && lockResult.left().value()) { + if (lock) { graphLockOperation.unlockComponent(storedService.getUniqueId(), NodeTypeEnum.Service); } } - Service service = createServiceWithForwardingPathForResponse(serviceId, resultMap); - return Either.left(service); + return createServiceWithForwardingPathForResponse(serviceId, resultMap); + } + + private Component getForwardingPathOriginComponent() { + Either forwardingPathOrigin = toscaOperationFacade + .getLatestByName(ForwardingPathUtils.FORWARDING_PATH_NODE_NAME, null); + if (forwardingPathOrigin.isRight()) { + StorageOperationStatus errorStatus = forwardingPathOrigin.right().value(); + log.debug("Failed to fetch normative forwarding path resource by tosca name, error {}", errorStatus); + throw new ByActionStatusComponentException(componentsUtils.convertFromStorageResponse(errorStatus)); + } + return forwardingPathOrigin.left().value(); + } + + private void populateForwardingPaths(String serviceId, boolean isUpdate, Map trimmedForwardingPaths, + Map resultMap) { + Either result; + try { + for (ForwardingPathDataDefinition forwardingPathDataDefinition : trimmedForwardingPaths.values()) { + if (isUpdate) { + result = forwardingPathOperation.updateForwardingPath(serviceId, forwardingPathDataDefinition); + } else { + result = forwardingPathOperation.addForwardingPath(serviceId, forwardingPathDataDefinition); + } + if (result.isRight()) { + janusGraphDao.rollback(); + throw new ByResponseFormatComponentException(componentsUtils + .getResponseFormat(componentsUtils.convertFromStorageResponse(result.right().value(), ComponentTypeEnum.SERVICE), "")); + } else { + ForwardingPathDataDefinition fpDataDefinition = result.left().value(); + resultMap.put(fpDataDefinition.getUniqueId(), forwardingPathDataDefinition); + } + } + } catch (ComponentException e) { + janusGraphDao.rollback(); + log.error("Exception occurred during add or update forwarding path property values: {}", e.getMessage(), e); + throw new ByActionStatusComponentException(ActionStatus.GENERAL_ERROR); + } } - private Service createServiceWithForwardingPathForResponse(String serviceId, Map forwardingPathDataDefinitionMap) { + private Service createServiceWithForwardingPathForResponse(String serviceId, + Map forwardingPathDataDefinitionMap) { Service service = new Service(); service.setUniqueId(serviceId); service.setForwardingPaths(forwardingPathDataDefinitionMap); return service; } - private Either validateUserAndRole(Service serviceUpdate, User user, String errorContext) { - Either eitherCreator = validateUser(user, errorContext, serviceUpdate, null, false); - if (eitherCreator.isRight()) { - return Either.right(eitherCreator.right().value()); - } - user = eitherCreator.left().value(); - - // validate user role - Either validateRes = validateUserRole(user, serviceUpdate, new ArrayList<>(), null, null); - if (validateRes.isRight()) { - return Either.right(validateRes.right().value()); - } - return null; - }private Either validateAndUpdateServiceMetadata(User user, Service currentService, Service serviceUpdate) { - - boolean hasBeenCertified = ValidationUtils.hasBeenCertified(currentService.getVersion()); - Either response = validateAndUpdateCategory(user, currentService, serviceUpdate, hasBeenCertified, null); - if (response.isRight()) { - ResponseFormat errorResponse = response.right().value(); - return Either.right(errorResponse); - } - - String creatorUserIdUpdated = serviceUpdate.getCreatorUserId(); - String creatorUserIdCurrent = currentService.getCreatorUserId(); - if (creatorUserIdUpdated != null && !creatorUserIdCurrent.equals(creatorUserIdUpdated)) { - log.info("update srvice: recived request to update creatorUserId to {} the field is not updatable ignoring.", creatorUserIdUpdated); - } - - String creatorFullNameUpdated = serviceUpdate.getCreatorFullName(); - String creatorFullNameCurrent = currentService.getCreatorFullName(); - if (creatorFullNameUpdated != null && !creatorFullNameCurrent.equals(creatorFullNameUpdated)) { - log.info("update srvice: recived request to update creatorFullName to {} the field is not updatable ignoring.", creatorFullNameUpdated); - } - - String lastUpdaterUserIdUpdated = serviceUpdate.getLastUpdaterUserId(); - String lastUpdaterUserIdCurrent = currentService.getLastUpdaterUserId(); - if (lastUpdaterUserIdUpdated != null && !lastUpdaterUserIdCurrent.equals(lastUpdaterUserIdUpdated)) { - log.info("update srvice: recived request to update lastUpdaterUserId to {} the field is not updatable ignoring.", lastUpdaterUserIdUpdated); - } - - String lastUpdaterFullNameUpdated = serviceUpdate.getLastUpdaterFullName(); - String lastUpdaterFullNameCurrent = currentService.getLastUpdaterFullName(); - if (lastUpdaterFullNameUpdated != null && !lastUpdaterFullNameCurrent.equals(lastUpdaterFullNameUpdated)) { - log.info("update srvice: recived request to update lastUpdaterFullName to {} the field is not updatable ignoring.", lastUpdaterFullNameUpdated); - } - - response = validateAndUpdateServiceName(user, currentService, serviceUpdate, hasBeenCertified, null); - if (response.isRight()) { - ResponseFormat errorResponse = response.right().value(); - return Either.right(errorResponse); - } + private void validateUserAndRole(Service serviceUpdate, User user, String errorContext) { + user = validateUser(user, errorContext, serviceUpdate, null, false); + validateUserRole(user, serviceUpdate, new ArrayList<>(), null, null); + } - DistributionStatusEnum distributionStatusUpdated = serviceUpdate.getDistributionStatus(); - DistributionStatusEnum distributionStatusCurrent = currentService.getDistributionStatus(); - if (distributionStatusUpdated != null && !distributionStatusUpdated.name().equals(distributionStatusCurrent != null ? distributionStatusCurrent.name() : null)) { - log.info("update service: received request to update distributionStatus to {}. the field is read only, ignoring.", distributionStatusUpdated); - } + @VisibleForTesting + Either validateAndUpdateServiceMetadata(User user, Service currentService, Service serviceUpdate, boolean subNodeChanged, + List subNodePropsToBeRemoved) { + try { + boolean hasBeenCertified = ValidationUtils.hasBeenCertified(currentService.getVersion()); + if (subNodeChanged) { + if (!subNodePropsToBeRemoved.isEmpty()) { + areSubstitutionNodePropertiesInUse(currentService, subNodePropsToBeRemoved); + } + currentService.setDerivedFromGenericVersion(serviceUpdate.getDerivedFromGenericVersion()); + currentService.setDerivedFromGenericType(serviceUpdate.getDerivedFromGenericType()); + } - if (serviceUpdate.getProjectCode() != null) { - response = validateAndUpdateProjectCode(user, currentService, serviceUpdate, null); + Either response = validateAndUpdateCategory(user, currentService, serviceUpdate, hasBeenCertified, + UPDATE_SERVICE_METADATA); if (response.isRight()) { ResponseFormat errorResponse = response.right().value(); return Either.right(errorResponse); } + verifyValuesAreIdentical(serviceUpdate.getCreatorUserId(), currentService.getCreatorUserId(), "creatorUserId"); + verifyValuesAreIdentical(serviceUpdate.getCreatorFullName(), currentService.getCreatorFullName(), "creatorFullName"); + verifyValuesAreIdentical(serviceUpdate.getLastUpdaterUserId(), currentService.getLastUpdaterUserId(), "lastUpdaterUserId"); + verifyValuesAreIdentical(serviceUpdate.getLastUpdaterFullName(), currentService.getLastUpdaterFullName(), "lastUpdaterFullName"); + response = validateAndUpdateServiceName(user, currentService, serviceUpdate, hasBeenCertified, null); + if (response.isRight()) { + return Either.right(response.right().value()); + } + verifyValuesAreIdentical(serviceUpdate.getDistributionStatus(), currentService.getDistributionStatus(), "distributionStatus"); + if (serviceUpdate.getProjectCode() != null) { + response = validateAndUpdateProjectCode(user, currentService, serviceUpdate, UPDATE_SERVICE_METADATA); + if (response.isRight()) { + return Either.right(response.right().value()); + } + } + response = validateAndUpdateIcon(user, currentService, serviceUpdate, hasBeenCertified, UPDATE_SERVICE_METADATA); + if (response.isRight()) { + return Either.right(response.right().value()); + } + verifyValuesAreIdentical(serviceUpdate.getCreationDate(), currentService.getCreationDate(), "creationDate"); + verifyValuesAreIdentical(serviceUpdate.getVersion(), currentService.getVersion(), "version"); + response = validateAndUpdateDescription(user, currentService, serviceUpdate, UPDATE_SERVICE_METADATA); + if (response.isRight()) { + return Either.right(response.right().value()); + } + response = validateAndUpdateTags(user, currentService, serviceUpdate, UPDATE_SERVICE_METADATA); + if (response.isRight()) { + return Either.right(response.right().value()); + } + response = validateAndUpdateContactId(user, currentService, serviceUpdate, UPDATE_SERVICE_METADATA); + if (response.isRight()) { + return Either.right(response.right().value()); + } + verifyValuesAreIdentical(serviceUpdate.getLastUpdateDate(), currentService.getLastUpdateDate(), "lastUpdateDate"); + verifyValuesAreIdentical(serviceUpdate.getLifecycleState(), currentService.getLifecycleState(), "lifecycleState"); + verifyValuesAreIdentical(serviceUpdate.isHighestVersion(), currentService.isHighestVersion(), "isHighestVersion"); + verifyValuesAreIdentical(serviceUpdate.getUUID(), currentService.getUUID(), "uuid"); + validateAndUpdateServiceType(currentService, serviceUpdate); + validateAndUpdateServiceFunction(currentService, serviceUpdate); + response = validateAndUpdateServiceRole(user, currentService, serviceUpdate, UPDATE_SERVICE_METADATA); + if (response.isRight()) { + return Either.right(response.right().value()); + } + response = validateAndUpdateInstantiationTypeValue(user, currentService, serviceUpdate, UPDATE_SERVICE_METADATA); + if (response.isRight()) { + return Either.right(response.right().value()); + } + verifyValuesAreIdentical(serviceUpdate.getInvariantUUID(), currentService.getInvariantUUID(), "invariantUUID"); + validateAndUpdateEcompNaming(currentService, serviceUpdate); + currentService.setEnvironmentContext(serviceUpdate.getEnvironmentContext()); + currentService.setCategorySpecificMetadata(serviceUpdate.getCategorySpecificMetadata()); + return Either.left(currentService); + } catch (ComponentException exception) { + ResponseFormat responseFormat = componentsUtils.getResponseFormat(exception); + componentsUtils + .auditComponentAdmin(responseFormat, user, serviceUpdate, AuditingActionEnum.UPDATE_SERVICE_METADATA, ComponentTypeEnum.SERVICE); + return Either.right(responseFormat); } + } - response = validateAndUpdateIcon(user, currentService, serviceUpdate, hasBeenCertified, null); - if (response.isRight()) { - ResponseFormat errorResponse = response.right().value(); - return Either.right(errorResponse); - } + private void addPropertiesToService(Service currentService, List subNodePropsToBeAdded) { + ListUtils.emptyIfNull(subNodePropsToBeAdded).forEach(prop -> { + Either addPropertyEither = + toscaOperationFacade.addPropertyToComponent(prop, currentService); + if (addPropertyEither.isRight()) { + throw new ByActionStatusComponentException(ActionStatus.GENERAL_ERROR); + } + }); + } - Long creationDateUpdated = serviceUpdate.getCreationDate(); - Long creationDateCurrent = currentService.getCreationDate(); - if (creationDateUpdated != null && !creationDateCurrent.equals(creationDateUpdated)) { - log.info("update srvice: recived request to update creationDate to {} the field is not updatable ignoring.", creationDateUpdated); - } + private void addInputsToService(Service currentService, List subNodePropsToBeAdded) { + ListUtils.emptyIfNull(subNodePropsToBeAdded).forEach(prop -> { + InputDefinition inputDef = new InputDefinition(prop); + Either status = + toscaOperationFacade.addInputToComponent(prop.getName(), inputDef, currentService); + if (status.isRight()) { + throw new ByActionStatusComponentException(ActionStatus.GENERAL_ERROR); + } + }); + } + + private void removePropertiesFromService(Service currentService, List subNodePropsToBeRemoved) { + List props = currentService.getProperties(); + List propsUniqueIdsToBeRemoved = + props.stream().filter(prop -> subNodePropsToBeRemoved.contains(prop.getName())).map(PropertyDefinition::getUniqueId) + .collect(Collectors.toList()); + ListUtils.emptyIfNull(props).stream().filter(prop -> propsUniqueIdsToBeRemoved.contains(prop.getUniqueId())).forEach(prop -> { + StorageOperationStatus status = toscaOperationFacade.deletePropertyOfComponent(currentService, prop.getName()); + if (status != StorageOperationStatus.OK) { + throw new ByActionStatusComponentException(ActionStatus.GENERAL_ERROR); + } + }); + } + + private void removeInputsFromService(Service currentService, List subNodePropsToBeRemoved) { + List props = currentService.getProperties(); + List inputs = currentService.getInputs(); + List propsUniqueIdsToBeRemoved = + props.stream().filter(prop -> subNodePropsToBeRemoved.contains(prop.getName())).map(PropertyDefinition::getUniqueId) + .collect(Collectors.toList()); + ListUtils.emptyIfNull(inputs).stream().filter(input -> input.isMappedToComponentProperty() && + (propsUniqueIdsToBeRemoved.contains(input.getPropertyId()) || subNodePropsToBeRemoved.contains(input.getName()))).forEach(input -> { + StorageOperationStatus status = toscaOperationFacade.deleteInputOfResource(currentService, input.getName()); + if (status != StorageOperationStatus.OK) { + throw new ByActionStatusComponentException(ActionStatus.GENERAL_ERROR); + } + }); + } + + private void areSubstitutionNodePropertiesInUse(Service service, List subNodePropsToBeRemoved) { + Map> componentInstancesProps = service.getComponentInstancesProperties(); + List propsUniqueIdsToBeRemoved = + ListUtils.emptyIfNull(service.getProperties()).stream().filter(prop -> subNodePropsToBeRemoved.contains(prop.getName())) + .map(PropertyDefinition::getUniqueId) + .collect(Collectors.toList()); + List inputsUniqueIdsToBeRemoved = ListUtils.emptyIfNull(service.getInputs()).stream() + .filter(input -> propsUniqueIdsToBeRemoved.contains(input.getPropertyId()) || subNodePropsToBeRemoved.contains(input.getName())) + .map(PropertyDefinition::getUniqueId) + .collect(Collectors.toList()); + Map> inUse = new HashMap<>(); + if (componentInstancesProps != null && !componentInstancesProps.isEmpty()) { + componentInstancesProps.forEach((compInstanceId, listOfProps) -> { + List propsInUse = new ArrayList<>(); + listOfProps.stream() + .filter(PropertyDataDefinition::isToscaFunction) + .filter(compProp -> ToscaFunctionType.isGetFunction(compProp.getToscaFunction().getType())) + .forEach(compProp -> { + ToscaFunction toscaFunction = compProp.getToscaFunction(); + ToscaGetFunctionDataDefinition toscaGetFunction = (ToscaGetFunctionDataDefinition) toscaFunction; + String propName = toscaGetFunction.getPropertyName(); + String propUniqueId = toscaGetFunction.getPropertyUniqueId(); + if (inputsUniqueIdsToBeRemoved.contains(propUniqueId) || propsUniqueIdsToBeRemoved.contains(propUniqueId) || + subNodePropsToBeRemoved.contains(propName)) { + propsInUse.add(compProp.getName()); + } + }); + if (!propsInUse.isEmpty()) { + Optional componentInstance = service.getComponentInstanceById(compInstanceId); + componentInstance.ifPresent(instance -> inUse.put(instance.getName(), propsInUse)); + } - String versionUpdated = serviceUpdate.getVersion(); - String versionCurrent = currentService.getVersion(); - if (versionUpdated != null && !versionCurrent.equals(versionUpdated)) { - log.info("update srvice: recived request to update version to {} the field is not updatable ignoring.", versionUpdated); + }); } - - response = validateAndUpdateDescription(user, currentService, serviceUpdate, hasBeenCertified, null); - if (response.isRight()) { - ResponseFormat errorResponse = response.right().value(); - return Either.right(errorResponse); + if (!inUse.isEmpty()) { + String propsInUse = inUse.entrySet().stream().map(entry -> { + String properties = entry.getValue().stream().map(Object::toString).collect(Collectors.joining(", ")); + return properties + " on " + entry.getKey(); + }).collect(Collectors.joining(", properties ")); + throw new ByActionStatusComponentException(ActionStatus.SUBSTITUTION_NODE_TYPE_PROPERTY_IN_USE, propsInUse); } + } - response = validateAndUpdateTags(user, currentService, serviceUpdate, hasBeenCertified, null); - if (response.isRight()) { - ResponseFormat errorResponse = response.right().value(); - return Either.right(errorResponse); - } - response = validateAndUpdateContactId(user, currentService, serviceUpdate, null); - if (response.isRight()) { - ResponseFormat errorResponse = response.right().value(); - return Either.right(errorResponse); - } + private boolean isSubstitutionNodeChanged(Service currentService, Service updatedService) { + String currentServiceType = currentService.getDerivedFromGenericType(); + String updatedServiceType = updatedService.getDerivedFromGenericType(); + String currentServiceVersion = currentService.getDerivedFromGenericVersion(); + String updatedServiceVersion = updatedService.getDerivedFromGenericVersion(); + return !(StringUtils.equals(currentServiceType, updatedServiceType) && StringUtils.equals(currentServiceVersion, updatedServiceVersion)); + } - Long lastUpdateDateUpdated = serviceUpdate.getLastUpdateDate(); - Long lastUpdateDateCurrent = currentService.getLastUpdateDate(); - if (lastUpdateDateUpdated != null && !lastUpdateDateCurrent.equals(lastUpdateDateUpdated)) { - log.info("update srvice: recived request to update lastUpdateDate to {} the field is not updatable ignoring.", lastUpdateDateUpdated); + private List getSubstitutionNodePropertiesToBeRemoved(Service currentService, Service serviceUpdate) { + List currentProps = ListUtils.emptyIfNull(fetchDerivedFromGenericType(currentService, null).getProperties()); + List updatedProps = ListUtils.emptyIfNull(fetchDerivedFromGenericType(serviceUpdate, null).getProperties()); + if (!StringUtils.equals(currentService.getDerivedFromGenericType(), serviceUpdate.getDerivedFromGenericType())) { + return currentProps.stream().map(PropertyDefinition::getName).collect(Collectors.toList()); } - LifecycleStateEnum lifecycleStateUpdated = serviceUpdate.getLifecycleState(); - LifecycleStateEnum lifecycleStateCurrent = currentService.getLifecycleState(); - if (lifecycleStateUpdated != null && !lifecycleStateCurrent.name().equals(lifecycleStateUpdated.name())) { - log.info("update srvice: recived request to update lifecycleState to {} the field is not updatable ignoring.", lifecycleStateUpdated); - } + Map currentPropsMap = currentProps.stream().collect(Collectors.toMap(prop -> prop.getName(), prop -> prop)); + Map updatedPropsMap = updatedProps.stream().collect(Collectors.toMap(prop -> prop.getName(), prop -> prop)); - Boolean isHighestVersionUpdated = serviceUpdate.isHighestVersion(); - Boolean isHighestVersionCurrent = currentService.isHighestVersion(); - if (isHighestVersionUpdated != null && !isHighestVersionCurrent.equals(isHighestVersionUpdated)) { - log.info("update srvice: recived request to update isHighestVersion to {} the field is not updatable ignoring.", isHighestVersionUpdated); + List propNamesToBeRemoved = new ArrayList<>(); + for (String currentPropertyName : currentPropsMap.keySet()) { + if (updatedPropsMap.containsKey(currentPropertyName)) { + if (!haveSameType(currentPropsMap.get(currentPropertyName), updatedPropsMap.get(currentPropertyName))) { + propNamesToBeRemoved.add(currentPropertyName); + } + } else { + propNamesToBeRemoved.add(currentPropertyName); + } } - String uuidUpdated = serviceUpdate.getUUID(); - String uuidCurrent = currentService.getUUID(); - if (!uuidCurrent.equals(uuidUpdated)) { - log.info("update service: recived request to update uuid to {} the field is not updatable ignoring.", uuidUpdated); - } + return propNamesToBeRemoved; + } - response = validateAndUpdateServiceType(user, currentService, serviceUpdate, null); - if (response.isRight()) { - ResponseFormat errorResponse = response.right().value(); - return Either.right(errorResponse); + private boolean haveSameType(final PropertyDefinition property1, final PropertyDefinition property2) { + if (property1.getType().equals("list")) { + return property2.getType().equals("list") && property1.getSchema().equals(property2.getSchema()); } + if (property1.getType().equals("map")) { + return property2.getType().equals("map") && property1.getSchema().equals(property2.getSchema()); + } + return property1.getType().equals(property2.getType()); + } - response = validateAndUpdateServiceRole(user, currentService, serviceUpdate, null); - if (response.isRight()) { - ResponseFormat errorResponse = response.right().value(); - return Either.right(errorResponse); + private List getSubstitutionNodePropertiesToBeAdded(Service currentService, Service serviceUpdate) { + List propsInCurrentVersion = ListUtils.emptyIfNull(fetchDerivedFromGenericType(currentService, null).getProperties()); + List propsInUpdatedVersion = ListUtils.emptyIfNull(fetchDerivedFromGenericType(serviceUpdate, null).getProperties()); + if (!StringUtils.equals(currentService.getDerivedFromGenericType(), serviceUpdate.getDerivedFromGenericType())) { + return propsInUpdatedVersion; } - String currentInvariantUuid = currentService.getInvariantUUID(); - String updatedInvariantUuid = serviceUpdate.getInvariantUUID(); + Map mapOfPropsInCurrentVersion = propsInCurrentVersion.stream() + .collect(Collectors.toMap(prop -> prop.getName(), prop -> prop)); + Map mapOfPropsInUpdatedVersion = propsInUpdatedVersion.stream() + .collect(Collectors.toMap(prop -> prop.getName(), prop -> prop)); - if ((updatedInvariantUuid != null) && (!updatedInvariantUuid.equals(currentInvariantUuid))) { - log.warn("Product invariant UUID is automatically set and cannot be updated"); - serviceUpdate.setInvariantUUID(currentInvariantUuid); + List propsToBeAdded = new ArrayList<>(); + for (Entry propertyInUpdatedVersion : mapOfPropsInUpdatedVersion.entrySet()) { + if (mapOfPropsInCurrentVersion.containsKey(propertyInUpdatedVersion.getKey())) { + if (!haveSameType(mapOfPropsInCurrentVersion.get(propertyInUpdatedVersion.getKey()), propertyInUpdatedVersion.getValue())) { + propsToBeAdded.add(propertyInUpdatedVersion.getValue()); + } + } else { + propsToBeAdded.add(propertyInUpdatedVersion.getValue()); + } } - validateAndUpdateEcompNaming(currentService, serviceUpdate); - currentService.setEnvironmentContext(serviceUpdate.getEnvironmentContext()); + return propsToBeAdded; + } - return Either.left(currentService); + private void verifyValuesAreIdentical(Object updatedValue, Object originalValue, String fieldName) { + if (updatedValue != null && !updatedValue.equals(originalValue)) { + log.info("update service: received request to update {} to {} the field is not updatable ignoring.", fieldName, updatedValue); + } } private void validateAndUpdateEcompNaming(Service currentService, Service serviceUpdate) { - Boolean isEcompoGeneratedCurr = currentService.isEcompGeneratedNaming(); - Boolean isEcompoGeneratedUpdate = serviceUpdate.isEcompGeneratedNaming(); - if (isEcompoGeneratedUpdate != null && isEcompoGeneratedCurr.equals(isEcompoGeneratedUpdate)) { - currentService.setEcompGeneratedNaming(isEcompoGeneratedUpdate); - } - String namingPolicyUpd = serviceUpdate.getNamingPolicy(); - if (!currentService.isEcompGeneratedNaming()) { - if (ValidationUtils.validateStringNotEmpty(namingPolicyUpd)) { + Boolean isEcompGeneratedCurr = currentService.isEcompGeneratedNaming(); + Boolean isEcompGeneratedUpdate = serviceUpdate.isEcompGeneratedNaming(); + if (isEcompGeneratedUpdate != null && !isEcompGeneratedUpdate.equals(isEcompGeneratedCurr)) { + currentService.setEcompGeneratedNaming(isEcompGeneratedUpdate); + } + String namingPolicyUpdate = serviceUpdate.getNamingPolicy(); + if (currentService.isEcompGeneratedNaming() != null && currentService.isEcompGeneratedNaming()) { + currentService.setNamingPolicy(namingPolicyUpdate); + } else { + if (!StringUtils.isEmpty(namingPolicyUpdate)) { log.warn("NamingPolicy must be empty for EcompGeneratedNaming=false"); - currentService.setNamingPolicy(""); - } else { - currentService.setNamingPolicy(namingPolicyUpd); } - }else{ - currentService.setNamingPolicy(namingPolicyUpd); + currentService.setNamingPolicy(""); } } - private Either validateAndUpdateContactId(User user, Service currentService, Service serviceUpdate, AuditingActionEnum audatingAction) { + private Either validateAndUpdateContactId(User user, Service currentService, Service serviceUpdate, + AuditingActionEnum audatingAction) { String contactIdUpdated = serviceUpdate.getContactId(); String contactIdCurrent = currentService.getContactId(); if (!contactIdCurrent.equals(contactIdUpdated)) { - Either validatContactId = validateContactId(user, serviceUpdate, audatingAction); - if (validatContactId.isRight()) { - ResponseFormat errorRespons = validatContactId.right().value(); - return Either.right(errorRespons); - } + componentContactIdValidator.validateAndCorrectField(user, serviceUpdate, audatingAction); currentService.setContactId(contactIdUpdated.toLowerCase()); } return Either.left(true); } - private Either validateAndUpdateTags(User user, Service currentService, Service serviceUpdate, boolean hasBeenCertified, AuditingActionEnum audatingAction) { + private Either validateAndUpdateTags(User user, Service currentService, Service serviceUpdate, + AuditingActionEnum audatingAction) { List tagsUpdated = serviceUpdate.getTags(); List tagsCurrent = currentService.getTags(); if (tagsUpdated == null || tagsUpdated.isEmpty()) { @@ -961,58 +1347,47 @@ public class ServiceBusinessLogic extends ComponentBusinessLogic { componentsUtils.auditComponentAdmin(responseFormat, user, serviceUpdate, audatingAction, ComponentTypeEnum.SERVICE); return Either.right(responseFormat); } - if (!(tagsCurrent.containsAll(tagsUpdated) && tagsUpdated.containsAll(tagsCurrent))) { - Either validatResponse = validateTagsListAndRemoveDuplicates(user, serviceUpdate, audatingAction); - if (validatResponse.isRight()) { - ResponseFormat errorRespons = validatResponse.right().value(); - return Either.right(errorRespons); - } + componentTagsValidator.validateAndCorrectField(user, serviceUpdate, audatingAction); currentService.setTags(tagsUpdated); } return Either.left(true); } - private Either validateAndUpdateDescription(User user, Service currentService, Service serviceUpdate, boolean hasBeenCertified, AuditingActionEnum audatingAction) { + private Either validateAndUpdateDescription(User user, Service currentService, Service serviceUpdate, + AuditingActionEnum audatingAction) { String descriptionUpdated = serviceUpdate.getDescription(); String descriptionCurrent = currentService.getDescription(); if (!descriptionCurrent.equals(descriptionUpdated)) { - Either validateDescriptionResponse = validateDescriptionAndCleanup(user, serviceUpdate, audatingAction); - if (validateDescriptionResponse.isRight()) { - ResponseFormat errorRespons = validateDescriptionResponse.right().value(); - return Either.right(errorRespons); - } + componentDescriptionValidator.validateAndCorrectField(user, serviceUpdate, audatingAction); currentService.setDescription(serviceUpdate.getDescription()); } return Either.left(true); } - private Either validateAndUpdateProjectCode(User user, Service currentService, Service serviceUpdate, AuditingActionEnum audatingAction) { + private Either validateAndUpdateProjectCode(User user, Service currentService, Service serviceUpdate, + AuditingActionEnum audatingAction) { String projectCodeUpdated = serviceUpdate.getProjectCode(); String projectCodeCurrent = currentService.getProjectCode(); - if (!projectCodeCurrent.equals(projectCodeUpdated)) { - - Either validatProjectCodeResponse = validateProjectCode(user, serviceUpdate, audatingAction); - if (validatProjectCodeResponse.isRight()) { - ResponseFormat errorRespons = validatProjectCodeResponse.right().value(); + if (StringUtils.isEmpty(projectCodeCurrent) || !projectCodeCurrent.equals(projectCodeUpdated)) { + try { + componentProjectCodeValidator.validateAndCorrectField(user, serviceUpdate, audatingAction); + } catch (ComponentException exp) { + ResponseFormat errorRespons = exp.getResponseFormat(); return Either.right(errorRespons); } currentService.setProjectCode(projectCodeUpdated); - } return Either.left(true); } - private Either validateAndUpdateIcon(User user, Service currentService, Service serviceUpdate, boolean hasBeenCertified, AuditingActionEnum audatingAction) { + private Either validateAndUpdateIcon(User user, Service currentService, Service serviceUpdate, boolean hasBeenCertified, + AuditingActionEnum audatingAction) { String iconUpdated = serviceUpdate.getIcon(); String iconCurrent = currentService.getIcon(); if (!iconCurrent.equals(iconUpdated)) { if (!hasBeenCertified) { - Either validatIconResponse = validateIcon(user, serviceUpdate, audatingAction); - if (validatIconResponse.isRight()) { - ResponseFormat errorRespons = validatIconResponse.right().value(); - return Either.right(errorRespons); - } + componentIconValidator.validateAndCorrectField(user, serviceUpdate, audatingAction); currentService.setIcon(iconUpdated); } else { log.info("icon {} cannot be updated once the service has been certified once.", iconUpdated); @@ -1023,25 +1398,23 @@ public class ServiceBusinessLogic extends ComponentBusinessLogic { return Either.left(true); } - private Either validateAndUpdateServiceName(User user, Service currentService, Service serviceUpdate, boolean hasBeenCertified, AuditingActionEnum auditingAction) { + private Either validateAndUpdateServiceName(User user, Service currentService, Service serviceUpdate, + boolean hasBeenCertified, AuditingActionEnum auditingAction) { String serviceNameUpdated = serviceUpdate.getName(); String serviceNameCurrent = currentService.getName(); if (!serviceNameCurrent.equals(serviceNameUpdated)) { if (!hasBeenCertified) { - Either validatServiceNameResponse = validateComponentName(user, serviceUpdate, auditingAction); - if (validatServiceNameResponse.isRight()) { - ResponseFormat errorRespons = validatServiceNameResponse.right().value(); - return Either.right(errorRespons); - } - - Either serviceNameUniquenessValidation = validateComponentNameUnique(user, serviceUpdate, auditingAction); - if (serviceNameUniquenessValidation.isRight()) { - return serviceNameUniquenessValidation; + componentNameValidator.validateAndCorrectField(user, serviceUpdate, auditingAction); + try { + componentNameValidator.validateComponentNameUnique(user, serviceUpdate, auditingAction); + } catch (ComponentException exp) { + return Either.right(exp.getResponseFormat()); } currentService.setName(serviceNameUpdated); - currentService.getComponentMetadataDefinition().getMetadataDataDefinition().setNormalizedName(ValidationUtils.normaliseComponentName(serviceNameUpdated)); - currentService.getComponentMetadataDefinition().getMetadataDataDefinition().setSystemName(ValidationUtils.convertToSystemName(serviceNameUpdated)); - + currentService.getComponentMetadataDefinition().getMetadataDataDefinition() + .setNormalizedName(ValidationUtils.normaliseComponentName(serviceNameUpdated)); + currentService.getComponentMetadataDefinition().getMetadataDataDefinition() + .setSystemName(ValidationUtils.convertToSystemName(serviceNameUpdated)); } else { log.info("service name {} cannot be updated once the service has been certified once.", serviceNameUpdated); ResponseFormat errorResponse = componentsUtils.getResponseFormat(ActionStatus.SERVICE_NAME_CANNOT_BE_CHANGED); @@ -1051,64 +1424,33 @@ public class ServiceBusinessLogic extends ComponentBusinessLogic { return Either.left(true); } - private Either validateAndUpdateServiceType(User user, Service currentService, Service updatedService, AuditingActionEnum auditingAction) { + private void validateAndUpdateServiceType(Service currentService, Service updatedService) { String updatedServiceType = updatedService.getServiceType(); String currentServiceType = currentService.getServiceType(); if (!currentServiceType.equals(updatedServiceType)) { - Either validateServiceType = validateServiceTypeAndCleanup(user, updatedService , auditingAction); - if (validateServiceType.isRight()) { - ResponseFormat errorResponse = validateServiceType.right().value(); - componentsUtils.auditComponentAdmin(errorResponse, user, updatedService, auditingAction, ComponentTypeEnum.SERVICE); - return Either.right(errorResponse); - } + serviceTypeValidator.validateAndCorrectField(null, updatedService, null); currentService.setServiceType(updatedServiceType); } - return Either.left(true); - } - - protected Either validateServiceTypeAndCleanup(User user, Component component, AuditingActionEnum actionEnum) { - String serviceType = ((Service)component).getServiceType(); - if (serviceType != null){ - serviceType = cleanUpText(serviceType); - Either validateServiceType = validateServiceType(serviceType); - if (validateServiceType.isRight()) { - ResponseFormat responseFormat = validateServiceType.right().value(); - componentsUtils.auditComponentAdmin(responseFormat, user, component, actionEnum, ComponentTypeEnum.SERVICE); - return Either.right(responseFormat); - } - return Either.left(true); - } else { - return Either.left(false); - } } - - private Either validateServiceType(String serviceType) { - if (serviceType.equals("")){ - return Either.left(true); - } else { - if (!ValidationUtils.validateServiceTypeLength(serviceType)) { - log.info("service type exceeds limit."); - ResponseFormat errorResponse = componentsUtils.getResponseFormat(ActionStatus.SERVICE_TYPE_EXCEEDS_LIMIT, "" + ValidationUtils.SERVICE_TYPE_MAX_LENGTH); - return Either.right(errorResponse); - } - - if (!ValidationUtils.validateIsEnglish(serviceType)) { - log.info("service type is not valid."); - ResponseFormat errorResponse = componentsUtils.getResponseFormat(ActionStatus.INVALID_SERVICE_TYPE); - return Either.right(errorResponse); - } - return Either.left(true); + private void validateAndUpdateServiceFunction(Service currentService, Service updatedService) { + String updatedServiceFunction = updatedService.getServiceFunction(); + String currentServiceFunction = currentService.getServiceFunction(); + if (!currentServiceFunction.equals(updatedServiceFunction)) { + serviceFunctionValidator.validateAndCorrectField(null, updatedService, null); + currentService.setServiceFunction(updatedService.getServiceFunction()); } } - private Either validateAndUpdateServiceRole(User user, Service currentService, Service updatedService, AuditingActionEnum auditingAction) { + private Either validateAndUpdateServiceRole(User user, Service currentService, Service updatedService, + AuditingActionEnum auditingAction) { String updatedServiceRole = updatedService.getServiceRole(); String currentServiceRole = currentService.getServiceRole(); if (!currentServiceRole.equals(updatedServiceRole)) { - Either validateServiceRole = validateServiceRoleAndCleanup(user, updatedService , auditingAction); - if (validateServiceRole.isRight()) { - ResponseFormat errorResponse = validateServiceRole.right().value(); + try { + serviceRoleValidator.validateAndCorrectField(user, updatedService, auditingAction); + } catch (ComponentException exp) { + ResponseFormat errorResponse = exp.getResponseFormat(); componentsUtils.auditComponentAdmin(errorResponse, user, updatedService, auditingAction, ComponentTypeEnum.SERVICE); return Either.right(errorResponse); } @@ -1117,157 +1459,116 @@ public class ServiceBusinessLogic extends ComponentBusinessLogic { return Either.left(true); } - protected Either validateServiceRoleAndCleanup(User user, Component component, AuditingActionEnum actionEnum) { - String serviceRole = ((Service)component).getServiceRole(); - if (serviceRole != null){ - serviceRole = cleanUpText(serviceRole); - - Either validateServiceRole = validateServiceRole(serviceRole); - if (validateServiceRole.isRight()) { - ResponseFormat responseFormat = validateServiceRole.right().value(); - componentsUtils.auditComponentAdmin(responseFormat, user, component, actionEnum, ComponentTypeEnum.SERVICE); - return Either.right(responseFormat); - } - return Either.left(true); - } else { - return Either.left(false); - } - } - - - private Either validateServiceRole(String serviceRole) { - if (serviceRole.equals("")){ - return Either.left(true); - } else { - if (!ValidationUtils.validateServiceRoleLength(serviceRole)) { - log.info("service role exceeds limit."); - ResponseFormat errorResponse = componentsUtils.getResponseFormat(ActionStatus.SERVICE_ROLE_EXCEEDS_LIMIT, "" + ValidationUtils.SERVICE_ROLE_MAX_LENGTH); - return Either.right(errorResponse); - } - - if (!ValidationUtils.validateIsEnglish(serviceRole)) { - log.info("service role is not valid."); - ResponseFormat errorResponse = componentsUtils.getResponseFormat(ActionStatus.INVALID_SERVICE_ROLE); - return Either.right(errorResponse); - } - return Either.left(true); - } - } - - - - private Either validateAndUpdateCategory(User user, Service currentService, Service serviceUpdate, boolean hasBeenCertified, AuditingActionEnum audatingAction) { - List categoryUpdated = serviceUpdate.getCategories(); - List categoryCurrent = currentService.getCategories(); - Either validatCategoryResponse = validateServiceCategory(user, serviceUpdate, audatingAction); - if (validatCategoryResponse.isRight()) { - ResponseFormat errorRespons = validatCategoryResponse.right().value(); - return Either.right(errorRespons); - } - if (!categoryCurrent.get(0).getName().equals(categoryUpdated.get(0).getName())) { - if (!hasBeenCertified) { - currentService.setCategories(categoryUpdated); - } else { - log.info("category {} cannot be updated once the service has been certified once.", categoryUpdated); - ResponseFormat errorResponse = componentsUtils.getResponseFormat(ActionStatus.SERVICE_CATEGORY_CANNOT_BE_CHANGED); + private Either validateAndUpdateInstantiationTypeValue(User user, Service currentService, Service updatedService, + AuditingActionEnum auditingAction) { + String updatedInstaType = updatedService.getInstantiationType(); + String currentInstaType = currentService.getInstantiationType(); + if (!currentInstaType.equals(updatedInstaType)) { + try { + serviceInstantiationTypeValidator.validateAndCorrectField(user, updatedService, auditingAction); + } catch (ComponentException exp) { + ResponseFormat errorResponse = exp.getResponseFormat(); + componentsUtils.auditComponentAdmin(errorResponse, user, updatedService, auditingAction, ComponentTypeEnum.SERVICE); return Either.right(errorResponse); } + currentService.setInstantiationType(updatedInstaType); } return Either.left(true); - } - public Either validateServiceCategory(List list) { - if (list != null) { - if (list.size() > 1) { - log.debug("Must be only one category for service"); - ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.COMPONENT_TOO_MUCH_CATEGORIES, ComponentTypeEnum.SERVICE.getValue()); - return Either.right(responseFormat); - } - CategoryDefinition category = list.get(0); - if (category.getSubcategories() != null) { - log.debug("Subcategories cannot be defined for service"); - ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.SERVICE_CANNOT_CONTAIN_SUBCATEGORY); - return Either.right(responseFormat); - } - if (!ValidationUtils.validateStringNotEmpty(category.getName())) { - log.debug("Resource category is empty"); - ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.COMPONENT_MISSING_CATEGORY, ComponentTypeEnum.SERVICE.getValue()); - return Either.right(responseFormat); - } - - log.debug("validating service category {} against valid categories list", list); - Either, ActionStatus> categorys = elementDao.getAllServiceCategories(); - if (categorys.isRight()) { - log.debug("failed to retrive service categories from Titan"); - ResponseFormat responseFormat = componentsUtils.getResponseFormat(categorys.right().value()); - return Either.right(responseFormat); - } - List categoryList = categorys.left().value(); - for (CategoryDefinition value : categoryList) { - if (value.getName().equals(category.getName())) { - return Either.left(true); + private Either validateAndUpdateCategory(User user, Service currentService, Service serviceUpdate, + boolean hasBeenCertified, AuditingActionEnum audatingAction) { + try { + List categoryUpdated = serviceUpdate.getCategories(); + List categoryCurrent = currentService.getCategories(); + serviceCategoryValidator.validateAndCorrectField(user, serviceUpdate, audatingAction); + if (!categoryCurrent.get(0).getName().equals(categoryUpdated.get(0).getName())) { + if (!hasBeenCertified) { + currentService.setCategories(categoryUpdated); + } else { + log.info("category {} cannot be updated once the service has been certified once.", categoryUpdated); + ResponseFormat errorResponse = componentsUtils.getResponseFormat(ActionStatus.SERVICE_CATEGORY_CANNOT_BE_CHANGED); + return Either.right(errorResponse); } } - log.debug("Category {} is not part of service category group. Service category valid values are {}", list, categoryList); - return Either.right(componentsUtils.getResponseFormat(ActionStatus.COMPONENT_INVALID_CATEGORY, ComponentTypeEnum.SERVICE.getValue())); + } catch (ComponentException exp) { + return Either.right(exp.getResponseFormat()); } - return Either.left(false); + return Either.left(true); } public Either getServiceComponentsRelations(String serviceId, User user) { Either serviceResponseFormatEither = getService(serviceId, user); - if (serviceResponseFormatEither.isRight()){ + if (serviceResponseFormatEither.isRight()) { return Either.right(serviceResponseFormatEither.right().value()); } - final ServiceRelations serviceRelations = new ForwardingPathUtils().convertServiceToServiceRelations(serviceResponseFormatEither.left().value()); + final ServiceRelations serviceRelations = new ForwardingPathUtils() + .convertServiceToServiceRelations(serviceResponseFormatEither.left().value()); return Either.left(serviceRelations); + } - - }public ResponseFormat deleteService(String serviceId, User user) { - ResponseFormat responseFormat; - String ecompErrorContext = "delete service"; - - Either eitherCreator = validateUserExists(user, ecompErrorContext, false); - if (eitherCreator.isRight()) { - return eitherCreator.right().value(); + public void deleteServiceAllVersions(String serviceId, User user) { + validateUserExists(user); + Either serviceStatus = toscaOperationFacade.getToscaElement(serviceId); + if (serviceStatus.isRight()) { + log.debug("Failed to get service {}", serviceId); + componentException(serviceStatus.right().value()); + } + Service service = serviceStatus.left().value(); + if (Boolean.FALSE.equals(service.isArchived())) { + log.debug("The service, {}, requested for delete has not been archived.", serviceId); + throw new ComponentException(ActionStatus.COMPONENT_NOT_ARCHIVED, serviceId); + } + List deletedServiceList = new ArrayList<>(); + try { + String model = service.getModel(); + final Optional modelOptional = modelOperation.findModelByName(model); + deletedServiceList = toscaOperationFacade.deleteService(service.getInvariantUUID(), true); + if (log.isDebugEnabled()) { + deletedServiceList.forEach(deletedS -> log.debug("Component {} was deleted.", deletedS)); + } + if (modelOptional.isPresent() && modelOptional.get().getModelType() == ModelTypeEnum.NORMATIVE_EXTENSION) { + modelOperation.deleteModel(modelOptional.get(), false); + } + toscaOperationFacade.commitAndCheck(service.getUniqueId()); + updateCatalog(service, ChangeTypeEnum.DELETE); + } catch (ComponentException exception) { + log.debug("Failed to delete service, {}, in ServiceServlet", serviceId); + janusGraphDao.rollback(); + throw exception; } - user = eitherCreator.left().value(); + } + public ResponseFormat markServiceForDeletion(String serviceId, User user) { + ResponseFormat responseFormat; + validateUserExists(user); Either serviceStatus = toscaOperationFacade.getToscaElement(serviceId); if (serviceStatus.isRight()) { log.debug("failed to get service {}", serviceId); return componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(serviceStatus.right().value()), ""); } - Service service = serviceStatus.left().value(); - StorageOperationStatus result = StorageOperationStatus.OK; - Either lockResult = lockComponent(service, "Mark service to delete"); - if (lockResult.isRight()) { - result = StorageOperationStatus.GENERAL_ERROR; - return componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR); - } - try { - + lockComponent(service, "Mark service to delete"); result = markComponentToDelete(service); - if (result.equals(StorageOperationStatus.OK)) { + if (result == StorageOperationStatus.OK) { responseFormat = componentsUtils.getResponseFormat(ActionStatus.NO_CONTENT); } else { ActionStatus actionStatus = componentsUtils.convertFromStorageResponse(result); responseFormat = componentsUtils.getResponseFormatByResource(actionStatus, service.getName()); } return responseFormat; - + } catch (ComponentException e) { + return e.getResponseFormat(); } finally { - if (result == null || !result.equals(StorageOperationStatus.OK)) { + if (result == null || result != StorageOperationStatus.OK) { log.warn("operation failed. do rollback"); BeEcompErrorManager.getInstance().logBeSystemError("Delete Service"); - titanDao.rollback(); + janusGraphDao.rollback(); } else { log.debug("operation success. do commit"); - titanDao.commit(); + janusGraphDao.commit(); } graphLockOperation.unlockComponent(serviceId, NodeTypeEnum.Service); } @@ -1276,48 +1577,35 @@ public class ServiceBusinessLogic extends ComponentBusinessLogic { public ResponseFormat deleteServiceByNameAndVersion(String serviceName, String version, User user) { ResponseFormat responseFormat; String ecompErrorContext = "delete service"; - Either validateEmptyResult = validateUserNotEmpty(user, ecompErrorContext); - if (validateEmptyResult.isRight()) { - return validateEmptyResult.right().value(); - } - - Either eitherCreator = validateUserExists(user, ecompErrorContext, false); - if (eitherCreator.isRight()) { - return eitherCreator.right().value(); - } - user = eitherCreator.left().value(); - + validateUserNotEmpty(user, ecompErrorContext); + user = validateUserExists(user); Either getResult = getServiceByNameAndVersion(serviceName, version, user.getUserId()); if (getResult.isRight()) { return getResult.right().value(); } Service service = getResult.left().value(); - StorageOperationStatus result = StorageOperationStatus.OK; - Either lockResult = lockComponent(service, "Mark service to delete"); - if (lockResult.isRight()) { - result = StorageOperationStatus.GENERAL_ERROR; - return componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR); - } - try { + lockComponent(service, "Mark service to delete"); result = markComponentToDelete(service); - if (result.equals(StorageOperationStatus.OK)) { + if (result == StorageOperationStatus.OK) { responseFormat = componentsUtils.getResponseFormat(ActionStatus.NO_CONTENT); } else { ActionStatus actionStatus = componentsUtils.convertFromStorageResponse(result); responseFormat = componentsUtils.getResponseFormatByResource(actionStatus, service.getName()); } return responseFormat; - + } catch (ComponentException e) { + result = StorageOperationStatus.GENERAL_ERROR; + return componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR); } finally { - if (result == null || !result.equals(StorageOperationStatus.OK)) { + if (result == null || result != StorageOperationStatus.OK) { log.warn("operation failed. do rollback"); BeEcompErrorManager.getInstance().logBeSystemError("Delete Service"); - titanDao.rollback(); + janusGraphDao.rollback(); } else { log.debug("operation success. do commit"); - titanDao.commit(); + janusGraphDao.commit(); } graphLockOperation.unlockComponent(service.getUniqueId(), NodeTypeEnum.Service); } @@ -1325,42 +1613,31 @@ public class ServiceBusinessLogic extends ComponentBusinessLogic { public Either getService(String serviceId, User user) { String ecompErrorContext = "Get service"; - Either validateEmptyResult = validateUserNotEmpty(user, ecompErrorContext); - if (validateEmptyResult.isRight()) { - return Either.right(validateEmptyResult.right().value()); - } - - Either eitherCreator = validateUserExists(user, ecompErrorContext, false); - if (eitherCreator.isRight()) { - return Either.right(eitherCreator.right().value()); - } - + validateUserNotEmpty(user, ecompErrorContext); + validateUserExists(user); Either storageStatus = toscaOperationFacade.getToscaElement(serviceId); if (storageStatus.isRight()) { log.debug("failed to get service by id {}", serviceId); - return Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(storageStatus.right().value(), ComponentTypeEnum.SERVICE), serviceId)); + return Either.right(componentsUtils + .getResponseFormat(componentsUtils.convertFromStorageResponse(storageStatus.right().value(), ComponentTypeEnum.SERVICE), serviceId)); } - - if(!(storageStatus.left().value() instanceof Service)){ - return Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(StorageOperationStatus.NOT_FOUND), serviceId)); + if (!(storageStatus.left().value() instanceof Service)) { + return Either + .right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(StorageOperationStatus.NOT_FOUND), serviceId)); } Service service = storageStatus.left().value(); return Either.left(service); - - - - } public Either getServiceByNameAndVersion(String serviceName, String serviceVersion, String userId) { - Either resp = validateUserExists(userId, "get Service By Name And Version", false); - if (resp.isRight()) { - return Either.right(resp.right().value()); - } - Either storageStatus = toscaOperationFacade.getComponentByNameAndVersion(ComponentTypeEnum.SERVICE, serviceName, serviceVersion); + validateUserExists(userId); + Either storageStatus = toscaOperationFacade + .getComponentByNameAndVersion(ComponentTypeEnum.SERVICE, serviceName, serviceVersion); if (storageStatus.isRight()) { log.debug("failed to get service by name {} and version {}", serviceName, serviceVersion); - return Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(storageStatus.right().value(), ComponentTypeEnum.SERVICE), serviceName)); + return Either.right(componentsUtils + .getResponseFormat(componentsUtils.convertFromStorageResponse(storageStatus.right().value(), ComponentTypeEnum.SERVICE), + serviceName)); } Service service = storageStatus.left().value(); return Either.left(service); @@ -1371,15 +1648,16 @@ public class ServiceBusinessLogic extends ComponentBusinessLogic { // create mandatory artifacts // TODO it must be removed after that artifact uniqueId creation will be + // moved to ArtifactOperation String serviceUniqueId = service.getUniqueId(); Map artifactMap = service.getArtifacts(); - if (artifactMap == null) - artifactMap = new HashMap(); - - Map informationalServiceArtifacts = ConfigurationManager.getConfigurationManager().getConfiguration().getInformationalServiceArtifacts(); + if (artifactMap == null) { + artifactMap = new HashMap<>(); + } + Map informationalServiceArtifacts = ConfigurationManager.getConfigurationManager().getConfiguration() + .getInformationalServiceArtifacts(); List exludeServiceCategory = ConfigurationManager.getConfigurationManager().getConfiguration().getExcludeServiceCategory(); - String category = service.getCategories().get(0).getName(); boolean isCreateArtifact = true; if (category != null && exludeServiceCategory != null && !exludeServiceCategory.isEmpty()) { @@ -1387,118 +1665,30 @@ public class ServiceBusinessLogic extends ComponentBusinessLogic { if (exlude.equalsIgnoreCase(category)) { isCreateArtifact = false; break; - } - } - - } - - if (informationalServiceArtifacts != null && isCreateArtifact) { - Set keys = informationalServiceArtifacts.keySet(); - for (String informationalServiceArtifactName : keys) { - Map artifactInfoMap = (Map) informationalServiceArtifacts.get(informationalServiceArtifactName); - ArtifactDefinition artifactDefinition = createArtifactDefinition(serviceUniqueId, informationalServiceArtifactName, artifactInfoMap, user, false); - artifactMap.put(artifactDefinition.getArtifactLabel(), artifactDefinition); - - } - - service.setArtifacts(artifactMap); - } - } - - private ArtifactDefinition createArtifactDefinition(String serviceId, String logicalName, Map artifactInfoMap, User user, Boolean isServiceApi) { - - ArtifactDefinition artifactInfo = artifactsBusinessLogic.createArtifactPlaceHolderInfo(serviceId, logicalName, artifactInfoMap, user, ArtifactGroupTypeEnum.INFORMATIONAL); - - if (isServiceApi) { - artifactInfo.setMandatory(false); - artifactInfo.setServiceApi(true); - } - return artifactInfo; - } - - private Either validateTransitionEnum(String distributionTransition, User user) { - DistributionTransitionEnum transitionEnum = null; - - transitionEnum = DistributionTransitionEnum.getFromDisplayName(distributionTransition); - if (transitionEnum == null) { - BeEcompErrorManager.getInstance().logBeSystemError("Change Service Distribution"); - log.info("state operation is not valid. operations allowed are: {}", DistributionTransitionEnum.valuesAsString()); - ResponseFormat error = componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR); - return Either.right(error); - } - - return Either.left(transitionEnum); - } - - private Either validateComment(LifecycleChangeInfoWithAction comment, User user, AuditingActionEnum auditAction) { - String data = comment.getUserRemarks(); - - if (data == null || data.trim().isEmpty()) { - BeEcompErrorManager.getInstance().logBeInvalidJsonInput("Change Service Distribution"); - log.debug("user comment cannot be empty or null."); - return Either.right(componentsUtils.getResponseFormat(ActionStatus.INVALID_CONTENT)); - } - data = ValidationUtils.removeNoneUtf8Chars(data); - data = ValidationUtils.removeHtmlTags(data); - data = ValidationUtils.normaliseWhitespace(data); - data = ValidationUtils.stripOctets(data); - - if (!ValidationUtils.validateLength(data, ValidationUtils.COMMENT_MAX_LENGTH)) { - BeEcompErrorManager.getInstance().logBeInvalidJsonInput("Change Service Distribution"); - log.debug("user comment exceeds limit."); - return Either.right(componentsUtils.getResponseFormat(ActionStatus.EXCEEDS_LIMIT, "comment", String.valueOf(ValidationUtils.COMMENT_MAX_LENGTH))); - } - if (!ValidationUtils.validateIsEnglish(data)) { - return Either.right(componentsUtils.getResponseFormat(ActionStatus.INVALID_CONTENT)); - } - return Either.left(data); - } - - private Either validateServiceDistributionChange(User user, String serviceId, AuditingActionEnum auditAction, String comment) { - Either storageStatus = toscaOperationFacade.getToscaElement(serviceId); - if (storageStatus.isRight()) { - ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.SERVICE_NOT_FOUND, serviceId); - log.debug("audit before sending response"); - componentsUtils.auditComponent(responseFormat, user, auditAction, serviceId, ComponentTypeEnum.SERVICE, comment); - return Either.right(responseFormat); - } - Service service = storageStatus.left().value(); - - if (service.getLifecycleState() != LifecycleStateEnum.CERTIFIED) { - log.info("service {} is not available for distribution. Should be in certified state", service.getUniqueId()); - ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.SERVICE_NOT_AVAILABLE_FOR_DISTRIBUTION, service.getVersion(), service.getName()); - createAudit(user, auditAction, comment, service, responseFormat); - return Either.right(responseFormat); - } - return Either.left(service); - } - - private Either validateUserDistributionChange(User user, Service service, AuditingActionEnum auditAction, String comment) { - log.debug("get user from DB"); - - // get user details - Either eitherCreator = validateUser(user, "Activate Distribution", service, auditAction, false); - if (eitherCreator.isRight()) { - return Either.right(eitherCreator.right().value()); + } + } } - user = eitherCreator.left().value(); - - // validate user role - List roles = new ArrayList<>(); - roles.add(Role.ADMIN); - roles.add(Role.GOVERNOR); - roles.add(Role.OPS); - Either validateRes = validateUserRole(user, service, roles, auditAction, comment); - if (validateRes.isRight()) { - return Either.right(validateRes.right().value()); + if (informationalServiceArtifacts != null && isCreateArtifact) { + Set keys = informationalServiceArtifacts.keySet(); + for (String informationalServiceArtifactName : keys) { + Map artifactInfoMap = (Map) informationalServiceArtifacts.get(informationalServiceArtifactName); + ArtifactDefinition artifactDefinition = createArtifactDefinition(serviceUniqueId, informationalServiceArtifactName, artifactInfoMap, + user, false); + artifactMap.put(artifactDefinition.getArtifactLabel(), artifactDefinition); + } + service.setArtifacts(artifactMap); } - return Either.left(user); } - private void createAudit(User user, AuditingActionEnum auditAction, String comment, Service component, ResponseFormat responseFormat) { - log.debug("audit before sending response"); - componentsUtils.auditComponent(responseFormat, user, component, auditAction, ComponentTypeEnum.SERVICE, - ResourceAuditData.newBuilder().state(component.getLifecycleState().name()).version(component.getVersion()).build(), comment); + private ArtifactDefinition createArtifactDefinition(String serviceId, String logicalName, Map artifactInfoMap, User user, + Boolean isServiceApi) { + ArtifactDefinition artifactInfo = artifactsBusinessLogic + .createArtifactPlaceHolderInfo(serviceId, logicalName, artifactInfoMap, user, ArtifactGroupTypeEnum.INFORMATIONAL); + if (Boolean.TRUE.equals(isServiceApi)) { + artifactInfo.setMandatory(false); + artifactInfo.setServiceApi(true); + } + return artifactInfo; } private String getEnvNameFromConfiguration() { @@ -1507,34 +1697,24 @@ public class ServiceBusinessLogic extends ComponentBusinessLogic { return configuredEnvName; } - public Either activateServiceOnTenantEnvironment(String serviceId, String envId, User modifier, ServiceDistributionReqInfo data) { - - Either activationRequestInformationEither = serviceDistributionValidation.validateActivateServiceRequest(serviceId, envId, modifier, data); + public Either activateServiceOnTenantEnvironment(String serviceId, String envId, User modifier, + ServiceDistributionReqInfo data) { + Either activationRequestInformationEither = serviceDistributionValidation + .validateActivateServiceRequest(serviceId, envId, modifier, data); if (activationRequestInformationEither.isRight()) { return Either.right(activationRequestInformationEither.right().value()); } - ActivationRequestInformation activationRequestInformation = activationRequestInformationEither.left().value(); - - Either result = null; - ResponseFormat response = null; String did = ThreadLocalsHolder.getUuid(); Service service = activationRequestInformation.getServiceToActivate(); - - StorageOperationStatus readyForDistribution = distributionEngine.verifyServiceHasDeploymentArtifacts(service); - if (readyForDistribution.equals(StorageOperationStatus.OK)) { - result = buildAndSendServiceNotification(service, envId, did, activationRequestInformation.getWorkloadContext(), modifier); - } else { - response = componentsUtils.getResponseFormatByDE(componentsUtils.convertFromStorageResponse(readyForDistribution), service.getName(), envId); - result = Either.right(response); - } - return result; + return buildAndSendServiceNotification(service, envId, did, activationRequestInformation.getWorkloadContext(), modifier); } - public Either buildAndSendServiceNotification(Service service, String envId, String did, String workloadContext, User modifier) { + private Either buildAndSendServiceNotification(Service service, String envId, String did, String workloadContext, + User modifier) { String envName = getEnvNameFromConfiguration(); INotificationData notificationData = distributionEngine.buildServiceForDistribution(service, did, workloadContext); - ActionStatus notifyServiceResponse = distributionEngine.notifyService(did, service, notificationData, envId, envName, modifier.getUserId(), modifier.getFullName()); + ActionStatus notifyServiceResponse = distributionEngine.notifyService(did, service, notificationData, envId, envName, modifier); if (notifyServiceResponse == ActionStatus.OK) { return Either.left(did); } else { @@ -1546,52 +1726,58 @@ public class ServiceBusinessLogic extends ComponentBusinessLogic { } public Either activateDistribution(String serviceId, String envName, User modifier, HttpServletRequest request) { - - Either eitherCreator = validateUserExists(modifier.getUserId(), "activate Distribution", false); - if (eitherCreator.isRight()) { - return Either.right(eitherCreator.right().value()); - } - - User user = eitherCreator.left().value(); - - Either result = null; - ResponseFormat response = null; - Service updatedService = null; + User user = validateUserExists(modifier.getUserId()); + validateUserRole(user, Collections.singletonList(Role.DESIGNER)); + Either result; + ResponseFormat response; + Service updatedService; String did = ThreadLocalsHolder.getUuid(); - // DE194021 + // DE194021 String configuredEnvName = ConfigurationManager.getConfigurationManager().getDistributionEngineConfiguration().getEnvironments().get(0); - if (configuredEnvName != null && false == envName.equals(configuredEnvName)) { + if (configuredEnvName != null && !configuredEnvName.equals(envName)) { log.trace("Update environment name to be {} instead of {}", configuredEnvName, envName); envName = configuredEnvName; } - // DE194021 - - ServletContext servletContext = request.getSession().getServletContext(); - boolean isDistributionEngineUp = getHealthCheckBL(servletContext).isDistributionEngineUp(); // DE - if (!isDistributionEngineUp) { - BeEcompErrorManager.getInstance().logBeSystemError("Distribution Engine is DOWN"); - log.debug("Distribution Engine is DOWN"); - response = componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR); - return Either.right(response); + if (!kafkaHandler.isKafkaActive()) { + // DE194021 + ServletContext servletContext = request.getSession().getServletContext(); + boolean isDistributionEngineUp = getHealthCheckBL(servletContext).isDistributionEngineUp(); // DE + if (!isDistributionEngineUp) { + BeEcompErrorManager.getInstance().logBeSystemError("Distribution Engine is DOWN"); + log.debug("Distribution Engine is DOWN"); + response = componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR); + return Either.right(response); + } } - Either serviceRes = toscaOperationFacade.getToscaElement(serviceId); if (serviceRes.isRight()) { log.debug("failed retrieving service"); - response = componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(serviceRes.right().value(), ComponentTypeEnum.SERVICE), serviceId); - componentsUtils.auditComponent(response, user, null, AuditingActionEnum.DISTRIBUTION_STATE_CHANGE_REQUEST, ComponentTypeEnum.SERVICE, - ResourceAuditData.newBuilder().build(), did); + response = componentsUtils + .getResponseFormat(componentsUtils.convertFromStorageResponse(serviceRes.right().value(), ComponentTypeEnum.SERVICE), serviceId); + componentsUtils.auditComponent(response, user, null, AuditingActionEnum.DISTRIBUTION_STATE_CHANGE_REQUEST, + new ResourceCommonInfo(ComponentTypeEnum.SERVICE.getValue()), ResourceVersionInfo.newBuilder().build(), did); return Either.right(response); } Service service = serviceRes.left().value(); + if (Boolean.TRUE.equals(service.isArchived())) { + log.info("Component is archived. Component id: {}", serviceId); + return Either.right(componentsUtils.getResponseFormat(ActionStatus.COMPONENT_IS_ARCHIVED, service.getName())); + } + if (service.getLifecycleState() != LifecycleStateEnum.CERTIFIED) { + log.info("service {} is not available for distribution. Should be in certified state", service.getUniqueId()); + ResponseFormat responseFormat = componentsUtils + .getResponseFormat(ActionStatus.SERVICE_NOT_AVAILABLE_FOR_DISTRIBUTION, service.getVersion(), service.getName()); + return Either.right(responseFormat); + } String dcurrStatus = service.getDistributionStatus().name(); String updatedStatus = dcurrStatus; - StorageOperationStatus readyForDistribution = distributionEngine.isReadyForDistribution(service, envName); - if (readyForDistribution.equals(StorageOperationStatus.OK)) { + StorageOperationStatus readyForDistribution = distributionEngine.isReadyForDistribution(envName); + if (readyForDistribution == StorageOperationStatus.OK) { INotificationData notificationData = distributionEngine.buildServiceForDistribution(service, did, null); - ActionStatus notifyServiceResponse = distributionEngine.notifyService(did, service, notificationData, envName, user.getUserId(), user.getFullName()); + ActionStatus notifyServiceResponse = distributionEngine.notifyService(did, service, notificationData, envName, user); if (notifyServiceResponse == ActionStatus.OK) { - Either updateStateRes = updateDistributionStatusForActivation(service, user, DistributionStatusEnum.DISTRIBUTED); + Either updateStateRes = updateDistributionStatusForActivation(service, user, + DistributionStatusEnum.DISTRIBUTED); if (updateStateRes.isLeft() && updateStateRes.left().value() != null) { updatedService = updateStateRes.left().value(); updatedStatus = updatedService.getDistributionStatus().name(); @@ -1609,38 +1795,32 @@ public class ServiceBusinessLogic extends ComponentBusinessLogic { result = Either.right(response); } } else { - response = componentsUtils.getResponseFormatByDE(componentsUtils.convertFromStorageResponse(readyForDistribution), service.getName(), envName); + response = componentsUtils + .getResponseFormatByDE(componentsUtils.convertFromStorageResponse(readyForDistribution, ComponentTypeEnum.SERVICE), envName); result = Either.right(response); } - componentsUtils.auditComponent(response, user, service, AuditingActionEnum.DISTRIBUTION_STATE_CHANGE_REQUEST, ComponentTypeEnum.SERVICE, - ResourceAuditData.newBuilder().distributionStatus(dcurrStatus).build(), - ResourceAuditData.newBuilder().distributionStatus(updatedStatus).build(), service.getName(), - null, null, did); + componentsUtils.auditComponent(response, user, service, AuditingActionEnum.DISTRIBUTION_STATE_CHANGE_REQUEST, + new ResourceCommonInfo(service.getName(), ComponentTypeEnum.SERVICE.getValue()), + ResourceVersionInfo.newBuilder().distributionStatus(dcurrStatus).build(), + ResourceVersionInfo.newBuilder().distributionStatus(updatedStatus).build(), null, null, did); return result; } // convert to private after deletion of temp url public Either updateDistributionStatusForActivation(Service service, User user, DistributionStatusEnum state) { - - Either resp = validateUserExists(user.getUserId(), "update Distribution Status For Activation", false); - if (resp.isRight()) { - return Either.right(resp.right().value()); - } - + validateUserExists(user.getUserId()); String serviceId = service.getUniqueId(); - Either lockResult = lockComponent(serviceId, service, "updateDistributionStatusForActivation"); - if (lockResult.isRight()) { - return Either.right(lockResult.right().value()); - } + lockComponent(serviceId, service, "updateDistributionStatusForActivation"); try { Either result = toscaOperationFacade.updateDistributionStatus(service, user, state); if (result.isRight()) { - titanDao.rollback(); + janusGraphDao.rollback(); BeEcompErrorManager.getInstance().logBeSystemError("updateDistributionStatusForActivation"); log.debug("service {} change distribution status failed", serviceId); return Either.right(componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR)); } - titanDao.commit(); + janusGraphDao.commit(); + updateCatalog(service, ChangeTypeEnum.LIFECYCLE); return Either.left(result.left().value()); } finally { graphLockOperation.unlockComponent(serviceId, NodeTypeEnum.Service); @@ -1648,314 +1828,132 @@ public class ServiceBusinessLogic extends ComponentBusinessLogic { } public Either markDistributionAsDeployed(String serviceId, String did, User user) { - - Either resp = validateUserExists(user.getUserId(), "mark Distribution As Deployed", false); - if (resp.isRight()) { - return Either.right(resp.right().value()); - } - + validateUserExists(user.getUserId()); log.debug("mark distribution deployed"); - AuditingActionEnum auditAction = AuditingActionEnum.DISTRIBUTION_DEPLOY; Either getServiceResponse = toscaOperationFacade.getToscaElement(serviceId); if (getServiceResponse.isRight()) { - BeEcompErrorManager.getInstance().logBeComponentMissingError("markDistributionAsDeployed", ComponentTypeEnum.SERVICE.getValue(), serviceId); + BeEcompErrorManager.getInstance() + .logBeComponentMissingError("markDistributionAsDeployed", ComponentTypeEnum.SERVICE.getValue(), serviceId); log.debug("service {} not found", serviceId); - ResponseFormat responseFormat = auditDeployError(did, user, auditAction, null, componentsUtils.convertFromStorageResponse(getServiceResponse.right().value(), ComponentTypeEnum.SERVICE), ""); - + ResponseFormat responseFormat = auditDeployError(did, user, auditAction, null, + componentsUtils.convertFromStorageResponse(getServiceResponse.right().value(), ComponentTypeEnum.SERVICE), ""); return Either.right(responseFormat); } - Service service = getServiceResponse.left().value(); - - Either validateRoleForDeploy = validateRoleForDeploy(did, user, auditAction, service); - if (validateRoleForDeploy.isRight()) { - return Either.right(validateRoleForDeploy.right().value()); - } - user = validateRoleForDeploy.left().value(); - + user = validateRoleForDeploy(did, user, auditAction, service); return checkDistributionAndDeploy(did, user, auditAction, service); - } public Either generateVfModuleArtifacts(Service service, User modifier, boolean shouldLock, boolean inTransaction) { Function>> artifactTaskGeneratorCreator = ri -> - // Only one VF Module Artifact per instance - add it to a list of one - buildArtifactGenList(service, modifier, shouldLock, inTransaction, ri); - + // Only one VF Module Artifact per instance - add it to a list of one + buildArtifactGenList(service, modifier, shouldLock, inTransaction, ri); return generateDeploymentArtifacts(service, artifactTaskGeneratorCreator); - } - private List> buildArtifactGenList(Service service, User modifier, boolean shouldLock, boolean inTransaction, ComponentInstance ri) { - List> asList = new ArrayList>(); - + private List> buildArtifactGenList(Service service, User modifier, boolean shouldLock, + boolean inTransaction, ComponentInstance ri) { + List> asList = new ArrayList<>(); if (ri.getOriginType() == OriginTypeEnum.VF) { - asList = Arrays.asList(new VfModuleArtifacGenerator(modifier, ri, service, shouldLock, inTransaction)); + asList = Arrays.asList(new VfModuleArtifactGenerator(modifier, ri, service, shouldLock, inTransaction)); } return asList; } - private List collectGroupsInstanceForCompInstance(ComponentInstance currVF, Wrapper responseWrapper) { - Map deploymentArtifacts = currVF.getDeploymentArtifacts(); - if(currVF.getGroupInstances() != null){ - currVF.getGroupInstances().stream().forEach(gi -> gi.alignArtifactsUuid(deploymentArtifacts)); - } - return currVF.getGroupInstances(); - } - - private ArtifactDefinition getVfModuleInstArtifactForCompInstance(ComponentInstance currVF, Service service, User modifier, List groupsForCurrVF, Wrapper payloadWrapper, Wrapper responseWrapper) { - ArtifactDefinition vfModuleAertifact = null; - if (MapUtils.isNotEmpty(currVF.getDeploymentArtifacts())) { - Optional optionalVfModuleArtifact = currVF.getDeploymentArtifacts().values().stream().filter(p -> p.getArtifactType().equals(ArtifactTypeEnum.VF_MODULES_METADATA.name())).findAny(); - if (optionalVfModuleArtifact.isPresent()) { - vfModuleAertifact = optionalVfModuleArtifact.get(); - } - } - if (vfModuleAertifact == null) { - Either createVfModuleArtifact = createVfModuleArtifact(modifier, currVF, service, payloadWrapper.getInnerElement()); - if (createVfModuleArtifact.isLeft()) { - vfModuleAertifact = createVfModuleArtifact.left().value(); - } else { - responseWrapper.setInnerElement(createVfModuleArtifact.right().value()); - } - } - return vfModuleAertifact; - } - - private void fillVfModuleInstHeatEnvPayload(List groupsForCurrVF, Wrapper payloadWrapper) { - // Converts GroupDefinition to VfModuleArtifactPayload which is the - // format used in the payload - - List vfModulePayloadForCurrVF = new ArrayList(); - if (groupsForCurrVF != null) { - for (GroupInstance groupInstance : groupsForCurrVF) { - VfModuleArtifactPayload modulePayload = new VfModuleArtifactPayload(groupInstance); - vfModulePayloadForCurrVF.add(modulePayload); - } - Collections.sort(vfModulePayloadForCurrVF, (art1, art2) -> VfModuleArtifactPayload.compareByGroupName(art1, art2)); - - final Gson gson = new GsonBuilder().setPrettyPrinting().create(); - - String vfModulePayloadString = gson.toJson(vfModulePayloadForCurrVF); - payloadWrapper.setInnerElement(vfModulePayloadString); - } - - } - - private Either generateVfModuleInstanceArtifact(User modifier, ComponentInstance currVFInstance, Service service, boolean shouldLock, boolean inTransaction) { - ArtifactDefinition vfModuleAertifact = null; - Wrapper responseWrapper = new Wrapper<>(); - Wrapper payloadWrapper = new Wrapper<>(); - List groupsForCurrVF = collectGroupsInstanceForCompInstance(currVFInstance, responseWrapper); - if (responseWrapper.isEmpty()) { - fillVfModuleInstHeatEnvPayload(groupsForCurrVF, payloadWrapper); - } - if (responseWrapper.isEmpty() && payloadWrapper.getInnerElement() != null) { - vfModuleAertifact = getVfModuleInstArtifactForCompInstance(currVFInstance, service, modifier, groupsForCurrVF, payloadWrapper, responseWrapper); - } - if (responseWrapper.isEmpty() && vfModuleAertifact != null) { - vfModuleAertifact = fillVfModulePayload(modifier, currVFInstance, vfModuleAertifact, shouldLock, inTransaction, payloadWrapper, responseWrapper, service); - } - - Either result; - if (responseWrapper.isEmpty()) { - result = Either.left(vfModuleAertifact); - } else { - result = Either.right(responseWrapper.getInnerElement()); - } - - return result; - } - - private ArtifactDefinition fillVfModulePayload(User modifier, ComponentInstance currVF, ArtifactDefinition vfModuleArtifact, boolean shouldLock, boolean inTransaction, Wrapper payloadWrapper, Wrapper responseWrapper, Service service) { - ArtifactDefinition result = null; - Either eitherPayload = artifactsBusinessLogic.generateArtifactPayload(vfModuleArtifact, ComponentTypeEnum.RESOURCE_INSTANCE, service, currVF.getName(), modifier, shouldLock, inTransaction, () -> System.currentTimeMillis(), - () -> Either.left(artifactsBusinessLogic.createEsArtifactData(vfModuleArtifact, payloadWrapper.getInnerElement().getBytes(StandardCharsets.UTF_8))), currVF.getUniqueId()); - if (eitherPayload.isLeft()) { - result = eitherPayload.left().value(); - } else { - responseWrapper.setInnerElement(eitherPayload.right().value()); - } - if (result == null) { - result = vfModuleArtifact; - } - - return result; - } - - private Either createVfModuleArtifact(User modifier, ComponentInstance currVF, Service service, String vfModulePayloadString) { - - ArtifactDefinition vfModuleArtifactDefinition = new ArtifactDefinition(); - String newCheckSum = null; - - vfModuleArtifactDefinition.setDescription("Auto-generated VF Modules information artifact"); - vfModuleArtifactDefinition.setArtifactDisplayName("Vf Modules Metadata"); - vfModuleArtifactDefinition.setArtifactType(ArtifactTypeEnum.VF_MODULES_METADATA.getType()); - vfModuleArtifactDefinition.setArtifactGroupType(ArtifactGroupTypeEnum.DEPLOYMENT); - vfModuleArtifactDefinition.setArtifactLabel("vfModulesMetadata"); - vfModuleArtifactDefinition.setTimeout(0); - vfModuleArtifactDefinition.setArtifactName(currVF.getNormalizedName() + "_modules.json"); - vfModuleArtifactDefinition.setPayloadData(vfModulePayloadString); - if (vfModulePayloadString != null) { - newCheckSum = GeneralUtility.calculateMD5Base64EncodedByByteArray(vfModulePayloadString.getBytes()); - } - vfModuleArtifactDefinition.setArtifactChecksum(newCheckSum); - - Either addArifactToComponent = artifactToscaOperation.addArifactToComponent(vfModuleArtifactDefinition, service.getUniqueId(), NodeTypeEnum.ResourceInstance, true, currVF.getUniqueId()); - - Either result; - if (addArifactToComponent.isLeft()) { - result = Either.left(addArifactToComponent.left().value()); - } else { - result = Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(addArifactToComponent.right().value()))); - } - - return result; - } - public Either generateHeatEnvArtifacts(Service service, User modifier, boolean shouldLock, boolean inTransaction) { - Function>> artifactTaskGeneratorCreator = resourceInstance -> - // Get All Deployment Artifacts - service.getComponentInstances().stream().filter(ri -> ri != null && ri == resourceInstance).filter(ri -> ri.getDeploymentArtifacts() != null).flatMap(ri -> ri.getDeploymentArtifacts().values().stream()). - // Filter in Only Heat Env - filter(depArtifact -> ArtifactTypeEnum.HEAT_ENV.getType().equals(depArtifact.getArtifactType())). - // Create ArtifactGenerator from those Artifacts - map(depArtifact -> new HeatEnvArtifactGenerator(depArtifact, service, resourceInstance.getName(), modifier, shouldLock, inTransaction, resourceInstance.getUniqueId())).collect(Collectors.toList()); - + // Get All Deployment Artifacts + service.getComponentInstances().stream().filter(ri -> ri != null && ri == resourceInstance) + .filter(ri -> ri.getDeploymentArtifacts() != null).flatMap(ri -> ri.getDeploymentArtifacts().values().stream()). + // Filter in Only Heat Env + filter(depArtifact -> ArtifactTypeEnum.HEAT_ENV.getType().equals(depArtifact.getArtifactType())). + // Create ArtifactGenerator from those Artifacts + map( + depArtifact -> new HeatEnvArtifactGenerator(depArtifact, service, resourceInstance.getName(), modifier, shouldLock, inTransaction, + resourceInstance.getUniqueId())).collect(Collectors.toList()); return generateDeploymentArtifacts(service, artifactTaskGeneratorCreator); - } - private Either generateDeploymentArtifacts(Service service, Function>> artifactTaskGeneratorCreator) { - + private Either generateDeploymentArtifacts(Service service, + Function>> artifactTaskGeneratorCreator) { // Get Flat List of (Callable) ArtifactGenerator for all the RI in the + // service if (service.getComponentInstances() != null) { - List> artifactGenList = service.getComponentInstances().stream().flatMap(ri -> artifactTaskGeneratorCreator.apply(ri).stream()).collect(Collectors.toList()); + List> artifactGenList = service.getComponentInstances().stream() + .flatMap(ri -> artifactTaskGeneratorCreator.apply(ri).stream()).collect(Collectors.toList()); if (artifactGenList != null && !artifactGenList.isEmpty()) { - for (ArtifactGenerator entry : artifactGenList) { - Either callRes; - try { - callRes = entry.call(); - if (callRes.isRight()) { - log.debug("Failed to generate artifact error : {}", callRes.right().value()); - return Either.right(callRes.right().value()); - } - } catch (Exception e) { - log.debug("Failed to generate artifact exception : {}", e); - return Either.right(componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR)); - } + Either callRes = checkDeploymentArtifact(artifactGenList); + if (callRes != null) { + return callRes; } } } Either storageStatus = toscaOperationFacade.getToscaFullElement(service.getUniqueId()); if (storageStatus.isRight()) { - return Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(storageStatus.right().value(), ComponentTypeEnum.SERVICE), "")); + return Either.right(componentsUtils + .getResponseFormat(componentsUtils.convertFromStorageResponse(storageStatus.right().value(), ComponentTypeEnum.SERVICE), "")); } - Service currentService = storageStatus.left().value(); - return Either.left(currentService); - - } - - abstract class ArtifactGenerator implements Callable> { - - } - - class HeatEnvArtifactGenerator extends ArtifactGenerator { - ArtifactDefinition artifactDefinition; - Service service; - String resourceInstanceName; - User modifier; - String instanceId; - boolean shouldLock; - boolean inTransaction; - - HeatEnvArtifactGenerator(ArtifactDefinition artifactDefinition, Service service, String resourceInstanceName, User modifier, boolean shouldLock, boolean inTransaction, String instanceId) { - this.artifactDefinition = artifactDefinition; - this.service = service; - this.resourceInstanceName = resourceInstanceName; - this.modifier = modifier; - this.shouldLock = shouldLock; - this.instanceId = instanceId; - this.inTransaction = inTransaction; - } - - @Override - public Either call() throws Exception { - return artifactsBusinessLogic.forceGenerateHeatEnvArtifact(artifactDefinition, ComponentTypeEnum.RESOURCE_INSTANCE, service, resourceInstanceName, modifier, shouldLock, inTransaction, instanceId); - } - - public ArtifactDefinition getArtifactDefinition() { - return artifactDefinition; - } - } - class VfModuleArtifacGenerator extends ArtifactGenerator { - private User user; - private ComponentInstance componentInstance; - private Service service; - boolean shouldLock; - boolean inTransaction; - - @Override - public Either call() throws Exception { - return generateVfModuleInstanceArtifact(user, componentInstance, service, shouldLock, inTransaction); - } - - private VfModuleArtifacGenerator(User user, ComponentInstance componentInstance, Service service, boolean shouldLock, boolean inTransaction) { - super(); - this.user = user; - this.componentInstance = componentInstance; - this.service = service; - this.shouldLock = shouldLock; - this.inTransaction = inTransaction; + private Either checkDeploymentArtifact(List> artifactGenList) { + for (ArtifactGenerator entry : artifactGenList) { + Either callRes; + try { + callRes = entry.call(); + if (callRes.isRight()) { + log.debug("Failed to generate artifact error : {}", callRes.right().value()); + return Either.right(callRes.right().value()); + } + } catch (Exception e) { + log.debug("Failed to generate artifact exception : {}", e); + return Either.right(componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR)); + } } - + return null; } - private synchronized Either checkDistributionAndDeploy(String did, User user, AuditingActionEnum auditAction, Service service) { - boolean isDeployed = isDistributionDeployed(did, service); + private synchronized Either checkDistributionAndDeploy(String distributionId, User user, AuditingActionEnum auditAction, + Service service) { + boolean isDeployed = isDistributionDeployed(distributionId); if (isDeployed) { return Either.left(service); } - Either distributionSuccess = checkDistributionSuccess(did, user, auditAction, service); + Either distributionSuccess = checkDistributionSuccess(distributionId, user, auditAction, service); if (distributionSuccess.isRight()) { return Either.right(distributionSuccess.right().value()); } - - log.debug("mark distribution {} as deployed - success", did); - componentsUtils.auditServiceDistributionDeployed(auditAction, service.getName(), service.getVersion(), service.getUUID(), did, STATUS_DEPLOYED, "OK", user); + log.debug("mark distribution {} as deployed - success", distributionId); + componentsUtils + .auditServiceDistributionDeployed(service.getName(), service.getVersion(), service.getUUID(), distributionId, STATUS_DEPLOYED, "OK", + user); return Either.left(service); } - private boolean isDistributionDeployed(String did, Service service) { - Either, ActionStatus> alreadyDeployed = auditCassandraDao.getDistributionDeployByStatus(did, AuditingActionEnum.DISTRIBUTION_DEPLOY.getName(), STATUS_DEPLOYED); - + private boolean isDistributionDeployed(String distributionId) { + Either, ActionStatus> alreadyDeployed = auditCassandraDao + .getDistributionDeployByStatus(distributionId, AuditingActionEnum.DISTRIBUTION_DEPLOY.getName(), STATUS_DEPLOYED); boolean isDeployed = false; if (alreadyDeployed.isLeft() && !alreadyDeployed.left().value().isEmpty()) { // already deployed - log.debug("distribution {} is already deployed", did); + log.debug("distribution {} is already deployed", distributionId); isDeployed = true; } return isDeployed; } protected Either checkDistributionSuccess(String did, User user, AuditingActionEnum auditAction, Service service) { - log.trace("checkDistributionSuccess"); // get all "DRequest" records for this distribution - - Either, ActionStatus> distRequestsResponse = auditCassandraDao.getDistributionRequest(did, AuditingActionEnum.DISTRIBUTION_STATE_CHANGE_REQUEST.getName()); + Either, ActionStatus> distRequestsResponse = auditCassandraDao + .getDistributionRequest(did, AuditingActionEnum.DISTRIBUTION_STATE_CHANGE_REQUEST.getName()); if (distRequestsResponse.isRight()) { ResponseFormat error = auditDeployError(did, user, auditAction, service, distRequestsResponse.right().value()); return Either.right(error); } - List distributionRequests = distRequestsResponse.left().value(); if (distributionRequests.isEmpty()) { BeEcompErrorManager.getInstance().logBeDistributionMissingError("markDistributionAsDeployed", did); @@ -1971,14 +1969,13 @@ public class ServiceBusinessLogic extends ComponentBusinessLogic { break; } } - // get all "DNotify" records for this distribution - Either, ActionStatus> distNotificationsResponse = auditCassandraDao.getDistributionNotify(did, AuditingActionEnum.DISTRIBUTION_NOTIFY.getName()); + Either, ActionStatus> distNotificationsResponse = auditCassandraDao + .getDistributionNotify(did, AuditingActionEnum.DISTRIBUTION_NOTIFY.getName()); if (distNotificationsResponse.isRight()) { ResponseFormat error = auditDeployError(did, user, auditAction, service, distNotificationsResponse.right().value()); return Either.right(error); } - List distributionNotifications = distNotificationsResponse.left().value(); boolean isNotificationsSucceeded = false; for (DistributionNotificationEvent event : distributionNotifications) { @@ -1988,10 +1985,8 @@ public class ServiceBusinessLogic extends ComponentBusinessLogic { break; } } - // if request failed OR there are notifications that failed if (!(isRequestSucceeded && isNotificationsSucceeded)) { - log.info("distribution {} has failed", did); ResponseFormat error = componentsUtils.getResponseFormat(ActionStatus.DISTRIBUTION_REQUESTED_FAILED, did); auditDeployError(did, user, auditAction, service, ActionStatus.DISTRIBUTION_REQUESTED_FAILED, did); @@ -2000,51 +1995,83 @@ public class ServiceBusinessLogic extends ComponentBusinessLogic { return Either.left(true); } - private ResponseFormat auditDeployError(String did, User user, AuditingActionEnum auditAction, Service service, ActionStatus status, String... params) { - + private ResponseFormat auditDeployError(String did, User user, AuditingActionEnum auditAction, Service service, ActionStatus status, + String... params) { ResponseFormat error = componentsUtils.getResponseFormat(status, params); String message = ""; if (error.getMessageId() != null) { message = error.getMessageId() + ": "; } message += error.getFormattedMessage(); - if (service != null) { - componentsUtils.auditServiceDistributionDeployed(auditAction, service.getName(), service.getVersion(), service.getUUID(), did, error.getStatus().toString(), message, user); + componentsUtils + .auditServiceDistributionDeployed(service.getName(), service.getVersion(), service.getUUID(), did, error.getStatus().toString(), + message, user); } else { - componentsUtils.auditServiceDistributionDeployed(auditAction, "", "", "", did, error.getStatus().toString(), message, user); + componentsUtils.auditServiceDistributionDeployed("", "", "", did, error.getStatus().toString(), message, user); } return error; } - private Either validateRoleForDeploy(String did, User user, AuditingActionEnum auditAction, Service service) { - Either eitherCreator = userAdmin.getUser(user.getUserId(), false); - if (eitherCreator.isRight() || eitherCreator.left().value() == null) { - BeEcompErrorManager.getInstance().logBeUserMissingError("Deploy Service", user.getUserId()); - log.debug("validateRoleForDeploy method - user is not listed. userId= {}", user.getUserId()); - ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.USER_NOT_FOUND, user.getUserId()); - auditDeployError(did, user, auditAction, service, ActionStatus.USER_NOT_FOUND); - return Either.right(responseFormat); - } - user = eitherCreator.left().value(); + private User validateRoleForDeploy(String did, User user, AuditingActionEnum auditAction, Service service) { + user = userAdmin.getUser(user.getUserId()); log.debug("validate user role"); List roles = new ArrayList<>(); roles.add(Role.ADMIN); - roles.add(Role.OPS); - Either validateRes = validateUserRole(user, service, roles, auditAction, null); - if (validateRes.isRight()) { + roles.add(Role.DESIGNER); + try { + validateUserRole(user, service, roles, auditAction, null); + } catch (ByActionStatusComponentException e) { log.info("role {} is not allowed to perform this action", user.getRole()); - ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.RESTRICTED_OPERATION); - auditDeployError(did, user, auditAction, service, ActionStatus.RESTRICTED_OPERATION); - return Either.right(responseFormat); + auditDeployError(did, user, auditAction, service, e.getActionStatus()); + throw e; } - return Either.left(user); - + return user; } @Override public void setDeploymentArtifactsPlaceHolder(Component component, User user) { + if (component instanceof Service) { + Service service = (Service) component; + Map artifactMap = service.getDeploymentArtifacts(); + if (artifactMap == null) { + artifactMap = new HashMap<>(); + } + service.setDeploymentArtifacts(artifactMap); + } else if (component instanceof Resource) { + Resource resource = (Resource) component; + Map artifactMap = resource.getDeploymentArtifacts(); + if (artifactMap == null) { + artifactMap = new HashMap<>(); + } + Map deploymentResourceArtifacts = ConfigurationManager.getConfigurationManager().getConfiguration() + .getDeploymentResourceArtifacts(); + if (deploymentResourceArtifacts != null) { + Map finalArtifactMap = artifactMap; + deploymentResourceArtifacts.forEach((k, v) -> processDeploymentResourceArtifacts(user, resource, finalArtifactMap, k, v)); + } + resource.setDeploymentArtifacts(artifactMap); + } + } + private void processDeploymentResourceArtifacts(User user, Resource resource, Map artifactMap, String k, Object v) { + Map artifactDetails = (Map) v; + Object object = artifactDetails.get(PLACE_HOLDER_RESOURCE_TYPES); + if (object != null) { + List artifactTypes = (List) object; + if (!artifactTypes.contains(resource.getResourceType().name())) { + return; + } + } else { + log.info("resource types for artifact placeholder {} were not defined. default is all resources", k); + } + if (artifactsBusinessLogic != null) { + ArtifactDefinition artifactDefinition = artifactsBusinessLogic + .createArtifactPlaceHolderInfo(resource.getUniqueId(), k, (Map) v, user, ArtifactGroupTypeEnum.DEPLOYMENT); + if (artifactDefinition != null && !artifactMap.containsKey(artifactDefinition.getArtifactLabel())) { + artifactMap.put(artifactDefinition.getArtifactLabel(), artifactDefinition); + } + } } @Override @@ -2053,10 +2080,10 @@ public class ServiceBusinessLogic extends ComponentBusinessLogic { } private HealthCheckBusinessLogic getHealthCheckBL(ServletContext context) { - WebAppContextWrapper webApplicationContextWrapper = (WebAppContextWrapper) context.getAttribute(Constants.WEB_APPLICATION_CONTEXT_WRAPPER_ATTR); + WebAppContextWrapper webApplicationContextWrapper = (WebAppContextWrapper) context + .getAttribute(Constants.WEB_APPLICATION_CONTEXT_WRAPPER_ATTR); WebApplicationContext webApplicationContext = webApplicationContextWrapper.getWebAppContext(context); - HealthCheckBusinessLogic healthCheckBl = webApplicationContext.getBean(HealthCheckBusinessLogic.class); - return healthCheckBl; + return webApplicationContext.getBean(HealthCheckBusinessLogic.class); } @Override @@ -2065,50 +2092,30 @@ public class ServiceBusinessLogic extends ComponentBusinessLogic { } @Override - public Either, ResponseFormat> getComponentInstancesFilteredByPropertiesAndInputs(String componentId, ComponentTypeEnum componentTypeEnum, String userId, String searchText) { - - Either resp = validateUserExists(userId, "Get Component Instances", false); - if (resp.isRight()) { - return Either.right(resp.right().value()); - } + public Either, ResponseFormat> getComponentInstancesFilteredByPropertiesAndInputs(String componentId, String userId) { + validateUserExists(userId); Either getComponentRes = toscaOperationFacade.getToscaElement(componentId, JsonParseFlagEnum.ParseAll); if (getComponentRes.isRight()) { - ResponseFormat responseFormat = componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(getComponentRes.right().value())); + ResponseFormat responseFormat = componentsUtils + .getResponseFormat(componentsUtils.convertFromStorageResponse(getComponentRes.right().value())); return Either.right(responseFormat); } - List componentInstances = getComponentRes.left().value().getComponentInstances(); - return Either.left(componentInstances); } - public ICacheMangerOperation getCacheManagerOperation() { - return cacheManagerOperation; - } - - public void setCacheManagerOperation(ICacheMangerOperation cacheManagerOperation) { - this.cacheManagerOperation = cacheManagerOperation; - } - + @Autowired public void setForwardingPathOperation(ForwardingPathOperation forwardingPathOperation) { this.forwardingPathOperation = forwardingPathOperation; } - @Override - public void setToscaOperationFacade(ToscaOperationFacade toscaOperationFacade) { - this.toscaOperationFacade = toscaOperationFacade; - }/** - * updates group instance with new property values in case of successful update of group instance related component instance will be updated with new modification time and related service will be updated with new last update date - * - * @param modifier - * @param serviceId - * @param componentInstanceId - * @param groupInstanceId - * @param newProperties - * @return + /** + * updates group instance with new property values in case of successful update of group instance related component instance will be updated with + * new modification time and related service will be updated with new last update date */ - public Either, ResponseFormat> updateGroupInstancePropertyValues(User modifier, String serviceId, String componentInstanceId, String groupInstanceId, List newProperties) { - + public Either, ResponseFormat> updateGroupInstancePropertyValues(User modifier, String serviceId, + String componentInstanceId, String groupInstanceId, + List newProperties) { Either, ResponseFormat> actionResult = null; Either, ResponseFormat> validateUserAndComponentRes; Component component = null; @@ -2124,31 +2131,35 @@ public class ServiceBusinessLogic extends ComponentBusinessLogic { component = validateUserAndComponentRes.left().value().getKey(); lockResult = lockComponentByName(component.getSystemName(), component, "Update Group Instance on Service"); if (lockResult.isRight()) { - log.debug("Failed to lock service {}. Response is {}. ", component.getName(), lockResult.right().value().getFormattedMessage()); + log.debug(FAILED_TO_LOCK_SERVICE_RESPONSE_IS, component.getName(), lockResult.right().value().getFormattedMessage()); actionResult = Either.right(lockResult.right().value()); } else { - log.debug("The service with system name {} locked. ", component.getSystemName()); + log.debug(THE_SERVICE_WITH_SYSTEM_NAME_LOCKED, component.getSystemName()); } } if (actionResult == null) { - actionResult = validateAndUpdateGroupInstancePropertyValuesAndContainingParents(component, componentInstanceId, groupInstanceId, newProperties); + actionResult = validateAndUpdateGroupInstancePropertyValuesAndContainingParents(component, componentInstanceId, groupInstanceId, + newProperties); if (actionResult.isRight()) { - log.debug("Failed to validate and update group instance {} property values and containing parents. The message is {}. ", groupInstanceId, actionResult.right().value().getFormattedMessage()); + log.debug("Failed to validate and update group instance {} property values and containing parents. The message is {}. ", + groupInstanceId, actionResult.right().value().getFormattedMessage()); } } } catch (Exception e) { log.error("Exception occured during update Group Instance property values: {}", e.getMessage(), e); actionResult = Either.right(componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR)); } finally { - if (lockResult != null && lockResult.isLeft() && lockResult.left().value()) { + if (lockResult != null && lockResult.isLeft() && Boolean.TRUE.equals(lockResult.left().value())) { graphLockOperation.unlockComponentByName(component.getSystemName(), component.getUniqueId(), NodeTypeEnum.Service); } } return actionResult; } - private Either, ResponseFormat> validateAndUpdateGroupInstancePropertyValuesAndContainingParents(Component component, String componentInstanceId, String groupInstanceId, List newProperties) { - + private Either, ResponseFormat> validateAndUpdateGroupInstancePropertyValuesAndContainingParents(Component component, + String componentInstanceId, + String groupInstanceId, + List newProperties) { Either, ResponseFormat> actionResult = null; Either, ResponseFormat> findGroupInstanceRes; Either, ResponseFormat> updateParentsModificationTimeRes; @@ -2157,27 +2168,31 @@ public class ServiceBusinessLogic extends ComponentBusinessLogic { Either updateGroupInstanceResult = null; GroupInstance updatedGroupInstance = null; boolean inTransaction = true; - boolean shouldCloseTransaction = true; findGroupInstanceRes = findGroupInstanceOnRelatedComponentInstance(component, componentInstanceId, groupInstanceId); if (findGroupInstanceRes.isRight()) { - log.debug("Group instance {} not found. ", groupInstanceId); + log.debug("#validateAndUpdateGroupInstancePropertyValuesAndContainingParents - Group instance {} not found. ", groupInstanceId); actionResult = Either.right(findGroupInstanceRes.right().value()); } if (actionResult == null) { oldGroupInstance = findGroupInstanceRes.left().value().getValue(); relatedComponentInstance = findGroupInstanceRes.left().value().getKey(); - updateGroupInstanceResult = groupBusinessLogic.validateAndUpdateGroupInstancePropertyValues(component.getUniqueId(), componentInstanceId, oldGroupInstance, newProperties, inTransaction); + updateGroupInstanceResult = groupBusinessLogic + .validateAndUpdateGroupInstancePropertyValues(component.getUniqueId(), componentInstanceId, oldGroupInstance, newProperties); if (updateGroupInstanceResult.isRight()) { - log.debug("Failed to update group instance {} property values. ", oldGroupInstance.getName()); + log.debug("#validateAndUpdateGroupInstancePropertyValuesAndContainingParents - Failed to update group instance {} property values. ", + oldGroupInstance.getName()); actionResult = Either.right(updateGroupInstanceResult.right().value()); } } if (actionResult == null) { updatedGroupInstance = updateGroupInstanceResult.left().value(); if (!oldGroupInstance.getModificationTime().equals(updatedGroupInstance.getModificationTime())) { - updateParentsModificationTimeRes = updateParentsModificationTimeAndCustomizationUuid(component, relatedComponentInstance, updatedGroupInstance, inTransaction, shouldCloseTransaction); + updateParentsModificationTimeRes = updateParentsModificationTimeAndCustomizationUuid(component, relatedComponentInstance, + updatedGroupInstance, inTransaction); if (updateParentsModificationTimeRes.isRight()) { - log.debug("Failed to update modification time. ", oldGroupInstance.getName()); + log.debug( + "#validateAndUpdateGroupInstancePropertyValuesAndContainingParents - Failed to update modification time for group instance {}. ", + oldGroupInstance.getName()); actionResult = Either.right(updateParentsModificationTimeRes.right().value()); } } @@ -2188,52 +2203,53 @@ public class ServiceBusinessLogic extends ComponentBusinessLogic { return actionResult; } - private Either, ResponseFormat> updateParentsModificationTimeAndCustomizationUuid(Component component, ComponentInstance relatedComponentInstance, GroupInstance updatedGroupInstance, - boolean inTranscation, boolean shouldCloseTransaction) { - + private Either, ResponseFormat> updateParentsModificationTimeAndCustomizationUuid( + Component component, ComponentInstance relatedComponentInstance, GroupInstance updatedGroupInstance, boolean inTranscation) { Either, ResponseFormat> actionResult; Either serviceMetadataUpdateResult; - Either updateComponentInstanceRes = componentInstanceBusinessLogic.updateComponentInstanceModificationTimeAndCustomizationUuid(relatedComponentInstance, NodeTypeEnum.ResourceInstance, + Either updateComponentInstanceRes = componentInstanceBusinessLogic + .updateComponentInstanceModificationTimeAndCustomizationUuid(relatedComponentInstance, NodeTypeEnum.ResourceInstance, updatedGroupInstance.getModificationTime(), inTranscation); if (updateComponentInstanceRes.isRight()) { - log.debug("Failed to update component instance {} after update of group instance {}. ", relatedComponentInstance.getName(), updatedGroupInstance.getName()); + log.debug("Failed to update component instance {} after update of group instance {}. ", relatedComponentInstance.getName(), + updatedGroupInstance.getName()); actionResult = Either.right(updateComponentInstanceRes.right().value()); } else { - serviceMetadataUpdateResult = toscaOperationFacade.updateComponentLastUpdateDateOnGraph(component, updatedGroupInstance.getModificationTime()); + serviceMetadataUpdateResult = toscaOperationFacade.updateComponentLastUpdateDateOnGraph(component); if (serviceMetadataUpdateResult.isRight()) { - log.debug("Failed to update service {} after update of component instance {} with new property values of group instance {}. ", component.getName(), relatedComponentInstance.getName(), updatedGroupInstance.getName()); - actionResult = Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(serviceMetadataUpdateResult.right().value()))); + log.debug("Failed to update service {} after update of component instance {} with new property values of group instance {}. ", + component.getName(), relatedComponentInstance.getName(), updatedGroupInstance.getName()); + actionResult = Either.right( + componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(serviceMetadataUpdateResult.right().value()))); } else { - actionResult = Either.left(new ImmutablePair<>(serviceMetadataUpdateResult.left().value(), updateComponentInstanceRes.left().value())); + actionResult = Either + .left(new ImmutablePair<>(serviceMetadataUpdateResult.left().value(), updateComponentInstanceRes.left().value())); } } return actionResult; } private Either, ResponseFormat> validateUserAndComponent(String serviceId, User modifier) { - Either, ResponseFormat> result = null; - Either validateComponentExistsRes = null; User currUser = null; Component component = null; Either validationUserResult = validateUserIgnoreAudit(modifier, "updateGroupInstancePropertyValues"); if (validationUserResult.isRight()) { - log.debug("Failed to validate user with userId for update service {}. ", modifier.getUserId(), serviceId); + log.debug("#validateUserAndComponent - Failed to validate user with userId {}, for update service {}. ", modifier.getUserId(), serviceId); result = Either.right(validationUserResult.right().value()); } if (result == null) { currUser = validationUserResult.left().value(); - validateComponentExistsRes = validateComponentExists(serviceId, ComponentTypeEnum.SERVICE, null); - if (validateComponentExistsRes.isRight()) { - log.debug("Failed to validate service existing {}. ", serviceId); - result = Either.right(validateComponentExistsRes.right().value()); - } - } - if (result == null) { - component = validateComponentExistsRes.left().value(); - if (!ComponentValidationUtils.canWorkOnComponent(component, currUser.getUserId())) { - log.info("Restricted operation for user: {}, on service: {}", currUser.getUserId(), component.getCreatorUserId()); - return Either.right(componentsUtils.getResponseFormat(ActionStatus.RESTRICTED_OPERATION)); + try { + component = validateComponentExists(serviceId, ComponentTypeEnum.SERVICE, null); + if (!ComponentValidationUtils.canWorkOnComponent(component, currUser.getUserId())) { + log.info("#validateUserAndComponent - Restricted operation for user: {}, on service: {}", currUser.getUserId(), + component.getCreatorUserId()); + return Either.right(componentsUtils.getResponseFormat(ActionStatus.RESTRICTED_OPERATION)); + } + } catch (ComponentException e) { + log.debug("#validateUserAndComponent - Failed to validate service existing {}. ", serviceId); + result = Either.right(e.getResponseFormat()); } } if (result == null) { @@ -2242,19 +2258,25 @@ public class ServiceBusinessLogic extends ComponentBusinessLogic { return result; } - private Either, ResponseFormat> findGroupInstanceOnRelatedComponentInstance(Component component, String componentInstanceId, String groupInstanceId) { - + private Either, ResponseFormat> findGroupInstanceOnRelatedComponentInstance(Component component, + String componentInstanceId, + String groupInstanceId) { Either, ResponseFormat> actionResult = null; GroupInstance groupInstance = null; ComponentInstance foundComponentInstance = findRelatedComponentInstance(component, componentInstanceId); if (foundComponentInstance == null) { log.debug("Component instance {} not found on service {}. ", componentInstanceId, component.getName()); - actionResult = Either.right(componentsUtils.getResponseFormat(ActionStatus.COMPONENT_INSTANCE_NOT_FOUND_ON_CONTAINER, componentInstanceId, "resource instance", "service", component.getName())); - } else if (!CollectionUtils.isEmpty(foundComponentInstance.getGroupInstances())) { - groupInstance = foundComponentInstance.getGroupInstances().stream().filter(gi -> gi.getUniqueId().equals(groupInstanceId)).findFirst().orElse(null); + actionResult = Either.right(componentsUtils + .getResponseFormat(ActionStatus.COMPONENT_INSTANCE_NOT_FOUND_ON_CONTAINER, componentInstanceId, "resource instance", "service", + component.getName())); + } else if (isNotEmpty(foundComponentInstance.getGroupInstances())) { + groupInstance = foundComponentInstance.getGroupInstances().stream().filter(gi -> gi.getUniqueId().equals(groupInstanceId)).findFirst() + .orElse(null); if (groupInstance == null) { log.debug("Group instance {} not found on component instance {}. ", groupInstanceId, foundComponentInstance.getName()); - actionResult = Either.right(componentsUtils.getResponseFormat(ActionStatus.GROUP_INSTANCE_NOT_FOUND_ON_COMPONENT_INSTANCE, groupInstanceId, foundComponentInstance.getName())); + actionResult = Either.right(componentsUtils + .getResponseFormat(ActionStatus.GROUP_INSTANCE_NOT_FOUND_ON_COMPONENT_INSTANCE, groupInstanceId, + foundComponentInstance.getName())); } } if (actionResult == null) { @@ -2265,43 +2287,205 @@ public class ServiceBusinessLogic extends ComponentBusinessLogic { private ComponentInstance findRelatedComponentInstance(Component component, String componentInstanceId) { ComponentInstance componentInstance = null; - if (!CollectionUtils.isEmpty(component.getComponentInstances())) { - componentInstance = component.getComponentInstances().stream().filter(ci -> ci.getUniqueId().equals(componentInstanceId)).findFirst().orElse(null); + if (isNotEmpty(component.getComponentInstances())) { + componentInstance = component.getComponentInstances().stream().filter(ci -> ci.getUniqueId().equals(componentInstanceId)).findFirst() + .orElse(null); } return componentInstance; } private Either validateUserIgnoreAudit(User modifier, String ecompErrorContext) { - Either result = validateUser(modifier, ecompErrorContext, null, null, false); - if (result.isLeft()) { - List roles = new ArrayList<>(); - roles.add(Role.ADMIN); - roles.add(Role.DESIGNER); - Either validationRoleRes = validateUserRole(result.left().value(), roles); - if (validationRoleRes.isRight()) { - result = Either.right(validationRoleRes.right().value()); + User user = validateUser(modifier, ecompErrorContext, null, null, false); + List roles = new ArrayList<>(); + roles.add(Role.ADMIN); + roles.add(Role.DESIGNER); + validateUserRole(user, roles); + return Either.left(user); + } + + public Either getUiComponentDataTransferByComponentId(String serviceId, + List dataParamsToReturn) { + ComponentParametersView paramsToReturn = new ComponentParametersView(dataParamsToReturn); + paramsToReturn.setIgnoreComponentInstancesProperties(false); + Either serviceResultEither = toscaOperationFacade.getToscaElement(serviceId, paramsToReturn); + if (serviceResultEither.isRight()) { + if (serviceResultEither.right().value() == StorageOperationStatus.NOT_FOUND) { + log.debug("#getUiComponentDataTransferByComponentId - Failed to find service with id {} ", serviceId); + return Either.right(componentsUtils.getResponseFormat(ActionStatus.SERVICE_NOT_FOUND, serviceId)); } + log.debug("#getUiComponentDataTransferByComponentId - failed to get service by id {} with filters {}", serviceId, dataParamsToReturn); + return Either.right( + componentsUtils.getResponseFormatByResource(componentsUtils.convertFromStorageResponse(serviceResultEither.right().value()), "")); } - return result; + Service service = serviceResultEither.left().value(); + if (dataParamsToReturn.contains(ComponentFieldsEnum.INPUTS.getValue())) { + ListUtils.emptyIfNull(service.getInputs()).stream().filter(input -> CollectionUtils.isEmpty(input.getConstraints())) + .forEach(input -> input.setConstraints(setInputConstraint(input))); + } + UiComponentDataTransfer dataTransfer = uiComponentDataConverter.getUiDataTransferFromServiceByParams(service, dataParamsToReturn); + return Either.left(dataTransfer); + } + + @Autowired(required = false) + public void setServiceCreationPluginList(List serviceCreationPluginList) { + this.serviceCreationPluginList = serviceCreationPluginList; } - public Either getUiComponentDataTransferByComponentId(String serviceId, List dataParamsToReturn) { + public boolean isServiceExist(String serviceName) { + return toscaOperationFacade.getLatestByServiceName(serviceName).isLeft(); + } - ComponentParametersView paramsToRetuen = new ComponentParametersView(dataParamsToReturn); - Either serviceResultEither = toscaOperationFacade.getToscaElement(serviceId, paramsToRetuen); + interface ArtifactGenerator extends Callable> { - if (serviceResultEither.isRight()) { - if(serviceResultEither.right().value().equals(StorageOperationStatus.NOT_FOUND)) { - log.debug("Failed to found service with id {} ", serviceId); - Either.right(componentsUtils.getResponseFormat(ActionStatus.SERVICE_NOT_FOUND, serviceId)); + } + + @Getter + @AllArgsConstructor(access = AccessLevel.PRIVATE) + class HeatEnvArtifactGenerator implements ArtifactGenerator { + + private ArtifactDefinition artifactDefinition; + private Service service; + private String resourceInstanceName; + private User modifier; + private boolean shouldLock; + private boolean inTransaction; + private String instanceId; + + @Override + public Either call() throws Exception { + return artifactsBusinessLogic + .forceGenerateHeatEnvArtifact(artifactDefinition, ComponentTypeEnum.RESOURCE_INSTANCE, service, resourceInstanceName, modifier, + shouldLock, inTransaction, instanceId); + } + } + + @AllArgsConstructor(access = AccessLevel.PRIVATE) + class VfModuleArtifactGenerator implements ArtifactGenerator { + + private User user; + private ComponentInstance componentInstance; + private Service service; + private boolean shouldLock; + private boolean inTransaction; + + private Either generateVfModuleInstanceArtifact(User modifier, ComponentInstance currVFInstance, + Service service, boolean shouldLock, + boolean inTransaction) { + ArtifactDefinition vfModuleArtifact = null; + Wrapper responseWrapper = new Wrapper<>(); + Wrapper payloadWrapper = new Wrapper<>(); + List groupsForCurrVF = collectGroupsInstanceForCompInstance(currVFInstance); + if (responseWrapper.isEmpty()) { + fillVfModuleInstHeatEnvPayload(groupsForCurrVF, payloadWrapper); + } + if (responseWrapper.isEmpty() && payloadWrapper.getInnerElement() != null) { + vfModuleArtifact = getVfModuleInstArtifactForCompInstance(currVFInstance, service, payloadWrapper, responseWrapper); + } + if (responseWrapper.isEmpty() && vfModuleArtifact != null) { + vfModuleArtifact = fillVfModulePayload(modifier, currVFInstance, vfModuleArtifact, shouldLock, inTransaction, payloadWrapper, + responseWrapper, service); + } + if (responseWrapper.isEmpty()) { + return Either.left(vfModuleArtifact); + } else { + return Either.right(responseWrapper.getInnerElement()); } + } - log.debug("failed to get service by id {} with filters {}", serviceId, dataParamsToReturn.toString()); - return Either.right(componentsUtils.getResponseFormatByResource(componentsUtils.convertFromStorageResponse(serviceResultEither.right().value()), "")); + private void fillVfModuleInstHeatEnvPayload(List groupsForCurrVF, Wrapper payloadWrapper) { + List vfModulePayloads = new ArrayList<>(); + if (groupsForCurrVF != null) { + for (GroupInstance groupInstance : groupsForCurrVF) { + VfModuleArtifactPayload modulePayload = new VfModuleArtifactPayload(groupInstance); + vfModulePayloads.add(modulePayload); + } + vfModulePayloads.sort(VfModuleArtifactPayload::compareByGroupName); + final Gson gson = new GsonBuilder().setPrettyPrinting().create(); + String vfModulePayloadString = gson.toJson(vfModulePayloads); + payloadWrapper.setInnerElement(vfModulePayloadString); + } } - Service service = serviceResultEither.left().value(); - UiComponentDataTransfer dataTransfer = UiComponentDataConverter.getUiDataTransferFromServiceByParams(service, dataParamsToReturn); - return Either.left(dataTransfer); + private ArtifactDefinition getVfModuleInstArtifactForCompInstance(ComponentInstance currVF, Service service, Wrapper payloadWrapper, + Wrapper responseWrapper) { + ArtifactDefinition vfModuleAertifact = null; + if (MapUtils.isNotEmpty(currVF.getDeploymentArtifacts())) { + final Optional optionalVfModuleArtifact = currVF.getDeploymentArtifacts().values().stream() + .filter(p -> p.getArtifactType().equals(ArtifactTypeEnum.VF_MODULES_METADATA.getType())).findAny(); + if (optionalVfModuleArtifact.isPresent()) { + vfModuleAertifact = optionalVfModuleArtifact.get(); + } + } + if (vfModuleAertifact == null) { + Either createVfModuleArtifact = createVfModuleArtifact(currVF, service, + payloadWrapper.getInnerElement()); + if (createVfModuleArtifact.isLeft()) { + vfModuleAertifact = createVfModuleArtifact.left().value(); + } else { + responseWrapper.setInnerElement(createVfModuleArtifact.right().value()); + } + } + return vfModuleAertifact; + } + + private List collectGroupsInstanceForCompInstance(ComponentInstance currVF) { + if (currVF.getGroupInstances() != null) { + currVF.getGroupInstances().forEach(gi -> gi.alignArtifactsUuid(currVF.getDeploymentArtifacts())); + } + return currVF.getGroupInstances(); + } + + private Either createVfModuleArtifact(ComponentInstance currVF, Service service, + String vfModulePayloadString) { + ArtifactDefinition vfModuleArtifactDefinition = new ArtifactDefinition(); + String newCheckSum = null; + vfModuleArtifactDefinition.setDescription("Auto-generated VF Modules information artifact"); + vfModuleArtifactDefinition.setArtifactDisplayName("Vf Modules Metadata"); + vfModuleArtifactDefinition.setArtifactType(ArtifactTypeEnum.VF_MODULES_METADATA.getType()); + vfModuleArtifactDefinition.setArtifactGroupType(ArtifactGroupTypeEnum.DEPLOYMENT); + vfModuleArtifactDefinition.setArtifactLabel("vfModulesMetadata"); + vfModuleArtifactDefinition.setTimeout(0); + vfModuleArtifactDefinition.setArtifactName(currVF.getNormalizedName() + "_modules.json"); + vfModuleArtifactDefinition.setPayloadData(vfModulePayloadString); + if (vfModulePayloadString != null) { + newCheckSum = GeneralUtility.calculateMD5Base64EncodedByByteArray(vfModulePayloadString.getBytes()); + } + vfModuleArtifactDefinition.setArtifactChecksum(newCheckSum); + Either addArtifactToComponent = artifactToscaOperation + .addArtifactToComponent(vfModuleArtifactDefinition, service, NodeTypeEnum.ResourceInstance, true, currVF.getUniqueId()); + if (addArtifactToComponent.isLeft()) { + return Either.left(addArtifactToComponent.left().value()); + } else { + return Either + .right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(addArtifactToComponent.right().value()))); + } + } + + private ArtifactDefinition fillVfModulePayload(User modifier, ComponentInstance currVF, ArtifactDefinition vfModuleArtifact, + boolean shouldLock, + boolean inTransaction, Wrapper payloadWrapper, Wrapper responseWrapper, + Service service) { + ArtifactDefinition result = null; + Either eitherPayload = artifactsBusinessLogic + .generateArtifactPayload(vfModuleArtifact, ComponentTypeEnum.RESOURCE_INSTANCE, service, currVF.getName(), modifier, shouldLock, + inTransaction, System::currentTimeMillis, () -> Either.left( + artifactsBusinessLogic.createEsArtifactData(vfModuleArtifact, + payloadWrapper.getInnerElement().getBytes(StandardCharsets.UTF_8))), + currVF.getUniqueId()); + if (eitherPayload.isLeft()) { + result = eitherPayload.left().value(); + } else { + responseWrapper.setInnerElement(eitherPayload.right().value()); + } + if (result == null) { + result = vfModuleArtifact; + } + return result; + } + + @Override + public Either call() throws Exception { + return generateVfModuleInstanceArtifact(user, componentInstance, service, shouldLock, inTransaction); + } } }