From 1ecdbf74427467327de0b1e11cd0a839d6dc05bd Mon Sep 17 00:00:00 2001 From: priyanshu Date: Mon, 6 Aug 2018 14:31:02 +0530 Subject: Interface operations Model update 1. Interface operations Model update 2. Removed unnecessary dependencies on resource 3. Fixed the broken code after merge Change-Id: I2b6381493530f1d61f0803ee1d1d688648356f73 Issue-ID: SDC-1535 Signed-off-by: priyanshu --- .../impl/InterfaceOperationBusinessLogic.java | 397 +++++++-------------- .../ResourceInterfaceOperationServlet.java | 148 +++----- 2 files changed, 185 insertions(+), 360 deletions(-) (limited to 'catalog-be/src/main') diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/InterfaceOperationBusinessLogic.java b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/InterfaceOperationBusinessLogic.java index 3fd5df0ba9..803cdbf277 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/InterfaceOperationBusinessLogic.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/InterfaceOperationBusinessLogic.java @@ -17,37 +17,39 @@ package org.openecomp.sdc.be.components.impl; -import com.google.common.collect.Sets; import fj.data.Either; -import org.apache.commons.collections.CollectionUtils; -import org.apache.commons.collections.MapUtils; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Optional; import org.apache.commons.lang.StringUtils; -import org.apache.commons.lang3.tuple.Pair; import org.openecomp.sdc.be.components.validation.InterfaceOperationValidation; import org.openecomp.sdc.be.dao.api.ActionStatus; -import org.openecomp.sdc.be.dao.cassandra.ArtifactCassandraDao; -import org.openecomp.sdc.be.dao.cassandra.CassandraOperationStatus; import org.openecomp.sdc.be.datamodel.utils.UiComponentDataConverter; import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum; import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum; -import org.openecomp.sdc.be.model.*; +import org.openecomp.sdc.be.model.ComponentInstance; +import org.openecomp.sdc.be.model.ComponentParametersView; +import org.openecomp.sdc.be.model.InterfaceDefinition; +import org.openecomp.sdc.be.model.Operation; +import org.openecomp.sdc.be.model.Resource; +import org.openecomp.sdc.be.model.User; import org.openecomp.sdc.be.model.jsontitan.operations.InterfaceOperation; import org.openecomp.sdc.be.model.jsontitan.utils.InterfaceUtils; import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus; import org.openecomp.sdc.be.ui.model.UiComponentDataTransfer; -import org.openecomp.sdc.common.api.ArtifactGroupTypeEnum; -import org.openecomp.sdc.common.api.ArtifactTypeEnum; import org.openecomp.sdc.exception.ResponseFormat; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; -import java.util.*; - @Component("interfaceOperationBusinessLogic") public class InterfaceOperationBusinessLogic extends ComponentBusinessLogic{ + private static final Logger LOGGER = LoggerFactory.getLogger(InterfaceOperationBusinessLogic.class); + public static final String FAILED_TO_LOCK_COMPONENT_RESPONSE_IS = "Failed to lock component {}. Response is {}. "; + @Autowired private InterfaceOperationValidation interfaceOperationValidation; @@ -57,9 +59,6 @@ public class InterfaceOperationBusinessLogic extends ComponentBusinessLogic{ @Autowired private InterfaceOperation interfaceOperation; - @Autowired - private ArtifactCassandraDao artifactCassandraDao; - @Autowired private UiComponentDataConverter uiComponentDataConverter; @@ -67,323 +66,226 @@ public class InterfaceOperationBusinessLogic extends ComponentBusinessLogic{ this.interfaceOperation = interfaceOperation; } - public InterfaceOperationValidation getInterfaceOperationValidation() { - return interfaceOperationValidation; - } - - public void setInterfaceOperationValidation( - InterfaceOperationValidation interfaceOperationValidation) { + public void setInterfaceOperationValidation(InterfaceOperationValidation interfaceOperationValidation) { this.interfaceOperationValidation = interfaceOperationValidation; } - - public void setArtifactCassandraDao(ArtifactCassandraDao artifactCassandraDao) { - this.artifactCassandraDao = artifactCassandraDao; - } - - public Either deleteInterfaceOperation(String resourceId, Set interfaceOperationToDelete, User user, boolean lock) { - Resource resourceToDelete = initResourceToDeleteWFOp(resourceId, interfaceOperationToDelete); - validateUserAndRole(resourceToDelete, user, "deleteInterfaceOperation"); - if (CollectionUtils.isEmpty(interfaceOperationToDelete)){ + public Either deleteInterfaceOperation(String componentId, String interfaceOperationToDelete, User user, boolean lock) { + if (StringUtils.isEmpty(interfaceOperationToDelete)){ LOGGER.debug("Invalid parameter interfaceOperationToDelete was empty"); return Either.right(componentsUtils.getResponseFormat(ActionStatus.INVALID_PROPERTY)); } - Either storageStatus = toscaOperationFacade.getToscaElement(resourceId); - if (storageStatus.isRight()) { - return Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(storageStatus.right().value(), - ComponentTypeEnum.RESOURCE), StringUtils.EMPTY)); + Either componentEither = getComponentDetails(componentId); + if (componentEither.isRight()){ + return Either.right(componentEither.right().value()); } - Resource resource = storageStatus.left().value(); + org.openecomp.sdc.be.model.Component storedComponent = componentEither.left().value(); + validateUserAndRole(storedComponent, user, "deleteInterfaceOperation"); + if (lock) { - Either lockResult = lockComponent(resource.getUniqueId(), resource, - "Delete interface Operation on a resource"); + Either lockResult = lockComponent(storedComponent.getUniqueId(), storedComponent, "Delete interface Operation on a storedComponent"); if (lockResult.isRight()) { - LOGGER.debug("Failed to lock resource {}. Response is {}. ", resource.getName(), lockResult.right().value().getFormattedMessage()); + LOGGER.debug(FAILED_TO_LOCK_COMPONENT_RESPONSE_IS, storedComponent.getName(), lockResult.right().value().getFormattedMessage()); titanDao.rollback(); return Either.right(lockResult.right().value()); } } try { - Optional optionalInterface = InterfaceUtils - .getInterfaceDefinitionFromToscaName(resource.getInterfaces().values(), resource.getName()); - Either sValue = getInterfaceDefinition(resource, optionalInterface.orElse(null)); + Optional optionalInterface = InterfaceUtils.getInterfaceDefinitionFromToscaName(((Resource)storedComponent).getInterfaces().values(), storedComponent.getName()); + Either sValue = getInterfaceDefinition(storedComponent, optionalInterface.orElse(null)); if (sValue.isRight()) { return Either.right(sValue.right().value()); } InterfaceDefinition interfaceDefinition = sValue.left().value(); - for(String operationToDelete : interfaceOperationToDelete) { - Either, ResponseFormat> deleteEither = deleteOperationFromInterface(interfaceDefinition, operationToDelete); - if (deleteEither.isRight()){ - return Either.right(deleteEither.right().value()); - } - - Operation deletedOperation = deleteEither.left().value().getValue(); - ArtifactDefinition implementationArtifact = deletedOperation.getImplementationArtifact(); - String artifactUUID = implementationArtifact.getArtifactUUID(); - CassandraOperationStatus cassandraStatus = artifactCassandraDao.deleteArtifact(artifactUUID); - if (cassandraStatus != CassandraOperationStatus.OK) { - LOGGER.debug("Failed to delete the artifact {} from the database. ", artifactUUID); - ResponseFormat responseFormatByArtifactId = componentsUtils.getResponseFormatByArtifactId( - componentsUtils.convertFromStorageResponse(componentsUtils.convertToStorageOperationStatus(cassandraStatus)), - implementationArtifact.getArtifactDisplayName()); - return Either.right(responseFormatByArtifactId); - } + Either deleteEither = interfaceOperation.deleteInterfaceOperation(componentId, interfaceDefinition, interfaceOperationToDelete); + if (deleteEither.isRight()){ + LOGGER.error("Failed to delete interface operation from storedComponent {}. Response is {}. ", storedComponent.getName(), deleteEither.right().value()); + return Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(deleteEither.right().value(), ComponentTypeEnum.RESOURCE))); + } + titanDao.commit(); + return Either.left(deleteEither.left().value()); + } catch (Exception e){ + LOGGER.error("Exception occurred during delete interface operation : {}", e.getMessage(), e); + titanDao.rollback(); + return Either.right(componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR)); + } finally { + graphLockOperation.unlockComponent(storedComponent.getUniqueId(), NodeTypeEnum.Resource); + } + } + + public Either getInterfaceOperation(String componentId, String interfaceOperationToGet, User user, boolean lock) { + if (StringUtils.isEmpty(interfaceOperationToGet)){ + LOGGER.debug("Invalid parameter interfaceOperationToGet was empty"); + return Either.right(componentsUtils.getResponseFormat(ActionStatus.INVALID_PROPERTY)); + } + Either componentEither = getComponentDetails(componentId); + if (componentEither.isRight()){ + return Either.right(componentEither.right().value()); + } + org.openecomp.sdc.be.model.Component storedComponent = componentEither.left().value(); + validateUserAndRole(storedComponent, user, "getInterfaceOperation"); + if (lock) { + Either lockResult = lockComponent(storedComponent.getUniqueId(), storedComponent, "Get interface Operation on a storedComponent"); + if (lockResult.isRight()) { + LOGGER.debug(FAILED_TO_LOCK_COMPONENT_RESPONSE_IS, storedComponent.getName(), lockResult.right().value().getFormattedMessage()); + titanDao.rollback(); + return Either.right(lockResult.right().value()); } + } - Either interfaceUpdate = interfaceOperation.updateInterface(resource.getUniqueId(), interfaceDefinition); - if (interfaceUpdate.isRight()) { - LOGGER.debug("Failed to delete interface operation from resource {}. Response is {}. ", resource.getName(), interfaceUpdate.right().value()); - titanDao.rollback(); - return Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(interfaceUpdate.right().value(), ComponentTypeEnum.RESOURCE))); + try { + Optional optionalInterface = InterfaceUtils.getInterfaceDefinitionFromToscaName(((Resource)storedComponent).getInterfaces().values(), storedComponent.getName()); + Either sValue = getInterfaceDefinition(storedComponent, optionalInterface.orElse(null)); + if (sValue.isRight()) { + return Either.right(sValue.right().value()); } + InterfaceDefinition interfaceDefinition = sValue.left().value(); - if(interfaceDefinition.getOperationsMap().isEmpty()){ - Either, StorageOperationStatus> deleteInterface = interfaceOperation.deleteInterface(resource, Sets.newHashSet(interfaceDefinition.getUniqueId())); - if (deleteInterface.isRight()) { - LOGGER.debug("Failed to delete interface from resource {}. Response is {}. ", resource.getName(), deleteInterface.right().value()); - titanDao.rollback(); - return Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(deleteInterface.right().value(), ComponentTypeEnum.RESOURCE))); - } + Either getEither = interfaceOperation.getInterfaceOperation(interfaceDefinition, interfaceOperationToGet); + if (getEither.isRight()){ + LOGGER.error("Failed to get interface operation from storedComponent {}. Response is {}. ", storedComponent.getName(), getEither.right().value()); + return Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(getEither.right().value(), ComponentTypeEnum.RESOURCE))); } titanDao.commit(); - + return Either.left(getEither.left().value()); } catch (Exception e){ - LOGGER.error("Exception occurred during delete interface operation : {}", e.getMessage(), e); + LOGGER.error("Exception occurred during get interface operation : {}", e.getMessage(), e); titanDao.rollback(); return Either.right(componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR)); } finally { - graphLockOperation.unlockComponent(resource.getUniqueId(), NodeTypeEnum.Resource); + graphLockOperation.unlockComponent(storedComponent.getUniqueId(), NodeTypeEnum.Resource); } - return Either.left(resource); } - public Either getInterfaceDefinition(Resource resource, - InterfaceDefinition interfaceDef) { + public Either getInterfaceDefinition(org.openecomp.sdc.be.model.Component component, InterfaceDefinition interfaceDef) { if (interfaceDef != null){ return Either.left(interfaceDef); } else { InterfaceDefinition interfaceDefinition = new InterfaceDefinition(); - interfaceDefinition.setToscaResourceName(InterfaceUtils.createInterfaceToscaResourceName(resource.getName())); - Either interfaceCreateEither = interfaceOperation - .addInterface(resource.getUniqueId(), interfaceDefinition); + interfaceDefinition.setToscaResourceName(InterfaceUtils.createInterfaceToscaResourceName(component.getName())); + Either interfaceCreateEither = interfaceOperation.addInterface(component.getUniqueId(), interfaceDefinition); if (interfaceCreateEither.isRight()){ StorageOperationStatus sValue = interfaceCreateEither.right().value(); return Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(sValue, - ComponentTypeEnum.RESOURCE), "")); - + ComponentTypeEnum.RESOURCE), "")); } return Either.left(interfaceCreateEither.left().value()); } } - private Either,ResponseFormat> deleteOperationFromInterface(InterfaceDefinition interfaceDefinition, String operationId){ - Optional> operationToRemove = interfaceDefinition.getOperationsMap().entrySet().stream() - .filter(entry -> entry.getValue().getUniqueId().equals(operationId)).findAny(); - if (operationToRemove.isPresent()){ - Map.Entry stringOperationEntry = operationToRemove.get(); - Map tempMap = interfaceDefinition.getOperationsMap(); - tempMap.remove(stringOperationEntry.getKey()); - interfaceDefinition.setOperationsMap(tempMap); - return Either.left(Pair.of(interfaceDefinition,stringOperationEntry.getValue())); - } - LOGGER.debug("Failed to delete interface operation"); - return Either.right(componentsUtils.getResponseFormat(ActionStatus.INTERFACE_OPERATION_NOT_FOUND)); + public Either createInterfaceOperation(String componentId, Operation operation, User user, boolean lock) { + return createOrUpdateInterfaceOperation(componentId, operation, user, false, "createInterfaceOperation", lock); } - - private Either addOperationToInterface(InterfaceDefinition interfaceDefinition, Operation interfaceOperation){ - if(interfaceOperation.getUniqueId() == null) - interfaceOperation.setUniqueId(UUID.randomUUID().toString()); - if (interfaceOperation.getImplementationArtifact() == null){ - initNewOperation(interfaceOperation); - } - Map tempMap = interfaceDefinition.getOperationsMap(); - tempMap.put(interfaceOperation.getUniqueId(), interfaceOperation); - interfaceDefinition.setOperationsMap(tempMap); - return Either.left(interfaceDefinition); + public Either updateInterfaceOperation(String componentId, Operation operation, User user, boolean lock) { + return createOrUpdateInterfaceOperation(componentId, operation, user, true, "updateInterfaceOperation", lock); } - private Either updateOperationInInterface(InterfaceDefinition interfaceDefinition, Operation interfaceOperation){ - Optional> operationToUpdate = interfaceDefinition.getOperationsMap().entrySet().stream() - .filter(entry -> entry.getValue().getUniqueId().equals(interfaceOperation.getUniqueId())).findAny(); - if (operationToUpdate.isPresent()){ - Operation updatedOperation = updateOperation(operationToUpdate.get().getValue(),interfaceOperation); - Map tempMap = interfaceDefinition.getOperationsMap(); - tempMap.remove(updatedOperation.getUniqueId()); - tempMap.put(updatedOperation.getUniqueId(), updatedOperation); - interfaceDefinition.setOperationsMap(tempMap); - return Either.left(interfaceDefinition); - } - LOGGER.debug("Failed to update interface operation"); - return Either.right(componentsUtils.getResponseFormat(ActionStatus.INTERFACE_OPERATION_NOT_FOUND)); - } - - private Operation updateOperation(Operation dbOperation, Operation updatedOperation) { - dbOperation.setName(updatedOperation.getName()); - dbOperation.setDescription(updatedOperation.getDescription()); - dbOperation.setInputs(updatedOperation.getInputs()); - dbOperation.setOutputs(updatedOperation.getOutputs()); - dbOperation.setWorkflowId(updatedOperation.getWorkflowId()); - dbOperation.setWorkflowVersionId(updatedOperation.getWorkflowVersionId()); - return dbOperation; - } - - public Either updateInterfaceOperation(String resourceId, Resource resourceUpdate, User user, boolean lock) { - return createOrUpdateInterfaceOperation(resourceId, resourceUpdate, user, true, "updateInterfaceOperation", lock); - } - - public Either createInterfaceOperation(String resourceId, Resource resourceUpdate, User user, boolean lock) { - return createOrUpdateInterfaceOperation(resourceId, resourceUpdate, user, false, "createInterfaceOperation", lock); - } - - private Either createOrUpdateInterfaceOperation(String resourceId, Resource resourceUpdate, User user, boolean isUpdate, String errorContext, boolean lock) { - validateUserAndRole(resourceUpdate, user, errorContext); - - Either resourceEither = getResourceDetails(resourceId); - if (resourceEither.isRight()){ - return resourceEither; - } - - Resource storedResource = resourceEither.left().value(); - - Map interfaceOperations = InterfaceUtils - .getInterfaceOperationsFromInterfaces(resourceUpdate.getInterfaces(), storedResource); - if(MapUtils.isEmpty(interfaceOperations) ) { - LOGGER.debug("Failed to fetch interface operations from resource {}, error {}",resourceUpdate.getUniqueId(), - ActionStatus.INTERFACE_OPERATION_NOT_FOUND); - return Either.right(componentsUtils.getResponseFormat(ActionStatus.INTERFACE_OPERATION_NOT_FOUND, - resourceUpdate.getUniqueId())); + private Either createOrUpdateInterfaceOperation(String componentId, Operation operation, User user, boolean isUpdate, String errorContext, boolean lock) { + Either componentEither = getComponentDetails(componentId); + if (componentEither.isRight()){ + return Either.right(componentEither.right().value()); } + org.openecomp.sdc.be.model.Component storedComponent = componentEither.left().value(); + validateUserAndRole(storedComponent, user, errorContext); + InterfaceUtils.createInputOutput(operation, storedComponent.getInputs()); Either interfaceOperationValidationResponseEither = interfaceOperationValidation - .validateInterfaceOperations( interfaceOperations.values(), resourceId, isUpdate); - + .validateInterfaceOperations(Arrays.asList(operation), componentId, isUpdate); if(interfaceOperationValidationResponseEither.isRight()) { return Either.right(interfaceOperationValidationResponseEither.right().value()); } Either lockResult = null; if (lock) { - lockResult = lockComponent(storedResource.getUniqueId(), storedResource, - "Create or Update interface Operation on Resource"); + lockResult = lockComponent(storedComponent.getUniqueId(), storedComponent, "Create or Update interface Operation on Component"); if (lockResult.isRight()) { - LOGGER.debug("Failed to lock resource {}. Response is {}. ", storedResource.getName(), lockResult.right().value().getFormattedMessage()); + LOGGER.debug(FAILED_TO_LOCK_COMPONENT_RESPONSE_IS, storedComponent.getName(), lockResult.right().value().getFormattedMessage()); titanDao.rollback(); return Either.right(lockResult.right().value()); - } else { - LOGGER.debug("The resource with system name {} locked. ", storedResource.getSystemName()); } } - Either result; - Map resultMap = new HashMap<>(); - + Either result; try { - Optional optionalInterface = InterfaceUtils - .getInterfaceDefinitionFromToscaName(storedResource.getInterfaces().values(), storedResource.getName()); - Either sValue = getInterfaceDefinition(storedResource, optionalInterface.orElse(null)); + Optional optionalInterface = InterfaceUtils.getInterfaceDefinitionFromToscaName(((Resource)storedComponent).getInterfaces().values(), storedComponent.getName()); + Either sValue = getInterfaceDefinition(storedComponent, optionalInterface.orElse(null)); if (sValue.isRight()) { return Either.right(sValue.right().value()); } InterfaceDefinition interfaceDefinition = sValue.left().value(); - for (Operation interfaceOperation : interfaceOperations.values()) { - if (isUpdate) { - result = updateOperationInInterface(interfaceDefinition, interfaceOperation); - } else { - result = addOperationToInterface(interfaceDefinition, interfaceOperation); - } - if (result.isRight()) { - titanDao.rollback(); - return Either.right(componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR)); - } else { - interfaceDefinition = result.left().value(); - resultMap.put(interfaceDefinition.getUniqueId(), interfaceDefinition); - } + if (isUpdate) { + result = interfaceOperation.updateInterfaceOperation(componentId, interfaceDefinition, operation); + } else { + result = interfaceOperation.addInterfaceOperation(componentId, interfaceDefinition, operation); } - - Either interfaceUpdate = interfaceOperation - .updateInterface(storedResource.getUniqueId(), interfaceDefinition); - if (interfaceUpdate.isRight()) { - LOGGER.debug("Failed to add or update interface operation on resource {}. Response is {}. ", storedResource.getName(), interfaceUpdate.right().value()); + if (result.isRight()) { titanDao.rollback(); - return Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(interfaceUpdate.right().value(), ComponentTypeEnum.RESOURCE))); + LOGGER.debug("Failed to add/update interface operation on component {}. Response is {}. ", storedComponent.getName(), result.right().value()); + return Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(result.right().value(), ComponentTypeEnum.RESOURCE))); } titanDao.commit(); - - Resource resource = createVFWithInterfaceOperationForResponse(resourceId, resultMap); - return Either.left(resource); + return Either.left(result.left().value()); } catch (Exception e) { titanDao.rollback(); - LOGGER.error("Exception occurred during add or update interface operation property values:{}", - e.getMessage(), e); + LOGGER.error("Exception occurred during add or update interface operation property values:{}", e.getMessage(), e); return Either.right(componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR)); } finally { if (lockResult != null && lockResult.isLeft() && lockResult.left().value()) { - graphLockOperation.unlockComponent(storedResource.getUniqueId(), NodeTypeEnum.Resource); + graphLockOperation.unlockComponent(storedComponent.getUniqueId(), NodeTypeEnum.Resource); } } } - private void initNewOperation(Operation operation){ - ArtifactDefinition artifactDefinition = new ArtifactDefinition(); - String artifactUUID = UUID.randomUUID().toString(); - artifactDefinition.setArtifactUUID(artifactUUID); - artifactDefinition.setUniqueId(artifactUUID); - artifactDefinition.setArtifactType(ArtifactTypeEnum.PLAN.getType()); - artifactDefinition.setArtifactGroupType(ArtifactGroupTypeEnum.LIFE_CYCLE); - operation.setImplementation(artifactDefinition); + private void validateUserAndRole(org.openecomp.sdc.be.model.Component component, User user, String errorContext) { + user = validateUser(user, errorContext, component, null, false); + validateUserRole(user, component, new ArrayList<>(), null, null); } - private Resource initResourceToDeleteWFOp(String resourceId, Collection interfaceOperationsToDelete) { - InterfaceDefinition id = new InterfaceDefinition(); - id.setUniqueId(UUID.randomUUID().toString()); - interfaceOperationsToDelete.forEach(interfaceOpToDelete -> id.getOperationsMap().put(interfaceOpToDelete, new Operation())); - Map interfaceDefinitionMap = new HashMap<>(); - interfaceDefinitionMap.put(id.getUniqueId(), id); - - Resource resourceToDelete = new Resource(); - resourceToDelete.setUniqueId(resourceId); - resourceToDelete.setInterfaces(interfaceDefinitionMap); - - return resourceToDelete; - } - - private void validateUserAndRole(Resource resourceUpdate, User user, String errorContext) { - user = validateUser(user, errorContext, resourceUpdate, null, false); - validateUserRole(user, resourceUpdate, new ArrayList<>(), null, null); + private Either getComponentDetails(String componentId){ + Either componentStorageOperationStatusEither = toscaOperationFacade.getToscaElement(componentId); + if (componentStorageOperationStatusEither.isRight()) { + StorageOperationStatus errorStatus = componentStorageOperationStatusEither.right().value(); + LOGGER.error("Failed to fetch component information by component id {}, error {}", componentId, errorStatus); + return Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(errorStatus))); + } + return Either.left(componentStorageOperationStatusEither.left().value()); } + @Override + public Either getUiComponentDataTransferByComponentId(String componentId, List dataParamsToReturn) { + ComponentParametersView paramsToRetuen = new ComponentParametersView(dataParamsToReturn); + Either componentResultEither = toscaOperationFacade.getToscaElement(componentId, paramsToRetuen); + if (componentResultEither.isRight()) { + if (componentResultEither.right().value().equals(StorageOperationStatus.NOT_FOUND)) { + LOGGER.error("Failed to found component with id {} ", componentId); + Either.right(componentsUtils.getResponseFormat(ActionStatus.RESOURCE_NOT_FOUND, componentId)); + } + LOGGER.error("failed to get component by id {} with filters {}", componentId, dataParamsToReturn); + return Either.right(componentsUtils.getResponseFormatByResource(componentsUtils.convertFromStorageResponse(componentResultEither.right().value()), "")); + } - private Resource createVFWithInterfaceOperationForResponse(String resourceId, Map interfaceDefinitionMap) { - Resource resource = new Resource(); - resource.setUniqueId(resourceId); - resource.setInterfaces(interfaceDefinitionMap); - return resource; + org.openecomp.sdc.be.model.Component component = componentResultEither.left().value(); + UiComponentDataTransfer dataTransfer = uiComponentDataConverter.getUiDataTransferFromResourceByParams((Resource)component, dataParamsToReturn); + return Either.left(dataTransfer); } - public Either getResourceDetails(String resourceId){ - Either resourceStorageOperationStatusEither = - toscaOperationFacade.getToscaElement(resourceId); - if (resourceStorageOperationStatusEither.isRight()) { - StorageOperationStatus errorStatus = resourceStorageOperationStatusEither.right().value(); - LOGGER.error("Failed to fetch resource information by resource id {}, error {}", resourceId, errorStatus); - return Either.right(componentsUtils - .getResponseFormat(componentsUtils.convertFromStorageResponse(errorStatus))); - } - return Either.left(resourceStorageOperationStatusEither.left().value()); + @Override + public Either, ResponseFormat> getComponentInstancesFilteredByPropertiesAndInputs( + String componentId, String userId) { + return null; } - @Override public Either, ResponseFormat> deleteMarkedComponents() { return deleteMarkedComponents(ComponentTypeEnum.RESOURCE); @@ -394,35 +296,4 @@ public class InterfaceOperationBusinessLogic extends ComponentBusinessLogic{ return componentInstanceBusinessLogic; } - @Override - public Either getUiComponentDataTransferByComponentId(String resourceId, - List dataParamsToReturn) { - ComponentParametersView paramsToRetuen = new ComponentParametersView(dataParamsToReturn); - Either resourceResultEither = toscaOperationFacade.getToscaElement(resourceId, - paramsToRetuen); - - if (resourceResultEither.isRight()) { - if (resourceResultEither.right().value().equals(StorageOperationStatus.NOT_FOUND)) { - LOGGER.error("Failed to found resource with id {} ", resourceId); - Either.right(componentsUtils.getResponseFormat(ActionStatus.RESOURCE_NOT_FOUND, resourceId)); - } - - LOGGER.error("failed to get resource by id {} with filters {}", resourceId, dataParamsToReturn); - return Either.right(componentsUtils.getResponseFormatByResource( - componentsUtils.convertFromStorageResponse(resourceResultEither.right().value()), "")); - } - - Resource resource = resourceResultEither.left().value(); - UiComponentDataTransfer dataTransfer = uiComponentDataConverter.getUiDataTransferFromResourceByParams(resource, - dataParamsToReturn); - return Either.left(dataTransfer); - } - - @Override - public Either, ResponseFormat> getComponentInstancesFilteredByPropertiesAndInputs( - String componentId, String userId) { - return null; - } - - } diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/servlets/ResourceInterfaceOperationServlet.java b/catalog-be/src/main/java/org/openecomp/sdc/be/servlets/ResourceInterfaceOperationServlet.java index d5d65a39c5..4d84e2c1a0 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/servlets/ResourceInterfaceOperationServlet.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/servlets/ResourceInterfaceOperationServlet.java @@ -16,41 +16,45 @@ package org.openecomp.sdc.be.servlets; -import com.google.common.collect.Sets; import com.jcabi.aspects.Loggable; import fj.data.Either; -import io.swagger.annotations.*; -import org.apache.commons.collections.CollectionUtils; -import org.apache.commons.collections.MapUtils; +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; +import io.swagger.annotations.ApiParam; +import io.swagger.annotations.ApiResponse; +import io.swagger.annotations.ApiResponses; +import java.util.Optional; +import java.util.UUID; +import javax.inject.Singleton; +import javax.servlet.ServletContext; +import javax.servlet.http.HttpServletRequest; +import javax.ws.rs.Consumes; +import javax.ws.rs.DELETE; +import javax.ws.rs.GET; +import javax.ws.rs.HeaderParam; +import javax.ws.rs.POST; +import javax.ws.rs.PUT; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.Produces; +import javax.ws.rs.core.Context; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; import org.openecomp.sdc.be.components.impl.InterfaceOperationBusinessLogic; import org.openecomp.sdc.be.config.BeEcompErrorManager; import org.openecomp.sdc.be.dao.api.ActionStatus; import org.openecomp.sdc.be.datamodel.utils.InterfaceUIDataConverter; import org.openecomp.sdc.be.datatypes.elements.InterfaceOperationDataDefinition; -import org.openecomp.sdc.be.datatypes.enums.ComponentFieldsEnum; import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum; -import org.openecomp.sdc.be.model.InterfaceDefinition; import org.openecomp.sdc.be.model.Operation; -import org.openecomp.sdc.be.model.Resource; import org.openecomp.sdc.be.model.User; -import org.openecomp.sdc.be.model.jsontitan.utils.InterfaceUtils; import org.openecomp.sdc.be.resources.data.auditing.AuditingActionEnum; -import org.openecomp.sdc.be.ui.model.UiComponentDataTransfer; import org.openecomp.sdc.be.ui.model.UiResourceDataTransfer; import org.openecomp.sdc.common.api.Constants; import org.openecomp.sdc.exception.ResponseFormat; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import javax.inject.Singleton; -import javax.servlet.ServletContext; -import javax.servlet.http.HttpServletRequest; -import javax.ws.rs.*; -import javax.ws.rs.core.Context; -import javax.ws.rs.core.MediaType; -import javax.ws.rs.core.Response; -import java.util.*; - @Loggable(prepend = true, value = Loggable.DEBUG, trim = false) @Path("/v1/catalog/resources/{resourceId}/interfaceOperations") @Consumes(MediaType.APPLICATION_JSON) @@ -78,7 +82,6 @@ public class ResourceInterfaceOperationServlet extends AbstractValidationsServle return createOrUpdate(data, resourceId, request, userId, false); } - @PUT @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) @@ -95,7 +98,6 @@ public class ResourceInterfaceOperationServlet extends AbstractValidationsServle return createOrUpdate(data, resourceId, request, userId, true); } - @DELETE @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) @@ -137,20 +139,24 @@ public class ResourceInterfaceOperationServlet extends AbstractValidationsServle modifier.setUserId(userId); log.debug("Start get request of {} with modifier id {}", url, userId); + Response response; + try { + String resourceIdLower = resourceId.toLowerCase(); InterfaceOperationBusinessLogic businessLogic = getInterfaceOperationBL(context); - Either resourceResponse = businessLogic.getComponentDataFilteredByParams(resourceId, modifier, Collections - .singletonList(ComponentFieldsEnum.INTERFACES.getValue())); - if (resourceResponse.isRight()) { - return buildErrorResponse(resourceResponse.right().value()); - } - - UiResourceDataTransfer uiResourceDataTransfer = (UiResourceDataTransfer) resourceResponse.left().value(); - InterfaceOperationDataDefinition interfaceOperationDataDefinition = getInterfaceOperationForResponse(interfaceOperationId, uiResourceDataTransfer.getInterfaces()); - return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), RepresentationUtils.toFilteredRepresentation(interfaceOperationDataDefinition)); + Either actionResponse = businessLogic.getInterfaceOperation(resourceIdLower, interfaceOperationId, modifier, true); + if (actionResponse.isRight()) { + log.debug("failed to get interface operation"); + response = buildErrorResponse(actionResponse.right().value()); + return response; + } - } catch (Exception e) { + InterfaceOperationDataDefinition interfaceOperationDataDefinition = InterfaceUIDataConverter.convertOperationDataToInterfaceData(actionResponse.left().value()); + Object result = RepresentationUtils.toFilteredRepresentation(interfaceOperationDataDefinition); + return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), result); + } + catch (Exception e) { BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Get Resource interface operations"); log.debug("get resource interface operations failed with exception", e); return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR)); @@ -173,25 +179,22 @@ public class ResourceInterfaceOperationServlet extends AbstractValidationsServle String resourceIdLower = resourceId.toLowerCase(); InterfaceOperationBusinessLogic businessLogic = getInterfaceOperationBL(context); - Either actionResponse = businessLogic.deleteInterfaceOperation(resourceIdLower, Sets.newHashSet(interfaceOperationId), modifier, true); - + Either actionResponse = businessLogic.deleteInterfaceOperation(resourceIdLower, interfaceOperationId, modifier, true); if (actionResponse.isRight()) { log.debug("failed to delete interface operation"); response = buildErrorResponse(actionResponse.right().value()); return response; } - Resource resource = actionResponse.left().value(); - InterfaceOperationDataDefinition interfaceOperationDataDefinition = getInterfaceOperationForResponse(interfaceOperationId, resource.getInterfaces()); + InterfaceOperationDataDefinition interfaceOperationDataDefinition = InterfaceUIDataConverter.convertOperationDataToInterfaceData(actionResponse.left().value()); Object result = RepresentationUtils.toFilteredRepresentation(interfaceOperationDataDefinition); return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), result); - - } catch (Exception e) { + } + catch (Exception e) { BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Delete Interface Operation"); log.debug("Delete interface operation with an error", e); response = buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR)); return response; - } } @@ -208,22 +211,12 @@ public class ResourceInterfaceOperationServlet extends AbstractValidationsServle String resourceIdLower = resourceId.toLowerCase(); InterfaceOperationBusinessLogic businessLogic = getInterfaceOperationBL(context); - Either resourceEither = businessLogic.getResourceDetails(resourceId); - Resource origResource = resourceEither.left().value(); - - Either convertResponse = parseToResource(data, origResource, isUpdate, modifier); - if (convertResponse.isRight()) { - log.debug("failed to parse resource"); - response = buildErrorResponse(convertResponse.right().value()); - return response; - } - - Resource updatedResource = convertResponse.left().value(); - Either actionResponse ; + Operation operation = getMappedOperationData(data, isUpdate, modifier); + Either actionResponse ; if (isUpdate) { - actionResponse = businessLogic.updateInterfaceOperation(resourceIdLower, updatedResource, modifier, true); + actionResponse = businessLogic.updateInterfaceOperation(resourceIdLower, operation, modifier, true); } else { - actionResponse = businessLogic.createInterfaceOperation(resourceIdLower, updatedResource, modifier, true); + actionResponse = businessLogic.createInterfaceOperation(resourceIdLower, operation, modifier, true); } if (actionResponse.isRight()) { @@ -232,67 +225,28 @@ public class ResourceInterfaceOperationServlet extends AbstractValidationsServle return response; } - Resource resource = actionResponse.left().value(); - List operationData = InterfaceUtils.getOperationsFromInterface(updatedResource.getInterfaces()); - InterfaceOperationDataDefinition interfaceOperationDataDefinition = getInterfaceOperationForResponse(operationData.get(0).getUniqueId(), resource.getInterfaces()); - + InterfaceOperationDataDefinition interfaceOperationDataDefinition = InterfaceUIDataConverter.convertOperationDataToInterfaceData(actionResponse.left().value()); Object result = RepresentationUtils.toFilteredRepresentation(interfaceOperationDataDefinition); return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), result); - - } catch (Exception e) { + } + catch (Exception e) { BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Interface Operation Creation or update"); log.debug("create or update interface Operation with an error", e); response = buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR)); return response; - } } - private Either parseToResource(String resourceJson, Resource origResource, boolean isUpdate, User user) { - - Resource resource = convertToResourceObject(resourceJson, user).left().value(); - - Either uiResourceEither = getComponentsUtils().convertJsonToObjectUsingObjectMapper(resourceJson, user, UiResourceDataTransfer.class, AuditingActionEnum.CREATE_RESOURCE, ComponentTypeEnum.RESOURCE); + private Operation getMappedOperationData(String inputJson, boolean isUpdate, User user){ + Either uiResourceEither = getComponentsUtils().convertJsonToObjectUsingObjectMapper(inputJson, user, UiResourceDataTransfer.class, AuditingActionEnum.CREATE_RESOURCE, ComponentTypeEnum.RESOURCE); Optional opDef = uiResourceEither.left().value().getInterfaceOperations().values().stream().findFirst(); - InterfaceOperationDataDefinition interfaceOperationDataDefinition; + InterfaceOperationDataDefinition interfaceOperationDataDefinition = new InterfaceOperationDataDefinition(); if(opDef.isPresent()) { interfaceOperationDataDefinition = opDef.get(); - if(!isUpdate) interfaceOperationDataDefinition.setUniqueId(UUID.randomUUID().toString()); - - Map interfaceOperations = new HashMap<>(); - interfaceOperations.put(interfaceOperationDataDefinition.getUniqueId(), InterfaceUIDataConverter.convertInterfaceDataToOperationData(interfaceOperationDataDefinition)); - InterfaceDefinition interfaceDefinition = new InterfaceDefinition(); - interfaceDefinition.setUniqueId(UUID.randomUUID().toString()); - interfaceDefinition.setToscaResourceName(InterfaceUtils.createInterfaceToscaResourceName(origResource.getName())); - interfaceDefinition.setOperationsMap(interfaceOperations); - - Map interfaceMap = new HashMap<>(); - interfaceMap.put(interfaceDefinition.getUniqueId(), interfaceDefinition); - - resource.setInterfaces(interfaceMap); - } - - return Either.left(resource); - } - - private Either convertToResourceObject(String resourceJson, User user) { - return getComponentsUtils().convertJsonToObjectUsingObjectMapper(resourceJson, user, Resource.class, AuditingActionEnum.CREATE_RESOURCE, ComponentTypeEnum.RESOURCE); - } - - private InterfaceOperationDataDefinition getInterfaceOperationForResponse(String interfaceOperationId, Map interfaces){ - InterfaceOperationDataDefinition interfaceOperationDataDefinition = new InterfaceOperationDataDefinition(); - if(!MapUtils.isEmpty(interfaces)){ - List operationData = InterfaceUtils.getOperationsFromInterface(interfaces); - if(CollectionUtils.isNotEmpty(operationData)){ - Optional matchedOp = operationData.stream().filter(a -> a.getUniqueId().equals(interfaceOperationId)).findAny(); - if(matchedOp.isPresent()) { - interfaceOperationDataDefinition = InterfaceUIDataConverter.convertOperationDataToInterfaceData(matchedOp.get()); - } - } } - return interfaceOperationDataDefinition; + return InterfaceUIDataConverter.convertInterfaceDataToOperationData(interfaceOperationDataDefinition); } } -- cgit 1.2.3-korg