diff options
author | 2018-12-24 16:26:27 +0530 | |
---|---|---|
committer | 2018-12-24 16:26:27 +0530 | |
commit | 01f825bc81b0701dc35f239fb60d03eec39d9ea6 (patch) | |
tree | df397f8af9000d901b926e4ec1e0b40923732653 /catalog-be/src/main/java/org/openecomp/sdc/be/components | |
parent | b6e7850484ba61df259e38fd6b59e94c2a7de5b6 (diff) |
Interface operation feature enhancements
1. API restructuring to enhance model and provide more capabilities.
2. Allowed multiple interface creation under same resource/service
3. Enhanced validations to align with updated model
4. API restructuring to align UI model with Tosca model
5. Enhanced Junit and code coverage.
Change-Id: Ieb5a5d72bc752774f9d702c587efaa127e43bd24
Issue-ID: SDC-1999
Signed-off-by: priyanshu <pagarwal@amdocs.com>
Diffstat (limited to 'catalog-be/src/main/java/org/openecomp/sdc/be/components')
7 files changed, 372 insertions, 286 deletions
diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ArtifactsBusinessLogic.java b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ArtifactsBusinessLogic.java index 2b3ee0ed2e..ef29b93001 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ArtifactsBusinessLogic.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ArtifactsBusinessLogic.java @@ -56,7 +56,7 @@ import org.openecomp.sdc.be.model.*; import org.openecomp.sdc.be.model.heat.HeatParameterType; import org.openecomp.sdc.be.model.jsontitan.operations.InterfaceOperation; import org.openecomp.sdc.be.model.jsontitan.operations.NodeTemplateOperation; -import org.openecomp.sdc.be.model.jsontitan.utils.InterfaceUtils; +import org.openecomp.sdc.be.components.utils.InterfaceOperationUtils; import org.openecomp.sdc.be.model.operations.api.*; import org.openecomp.sdc.be.model.operations.impl.DaoStatusConverter; import org.openecomp.sdc.be.model.operations.impl.UniqueIdBuilder; @@ -3058,7 +3058,7 @@ public class ArtifactsBusinessLogic extends BaseBusinessLogic { } } else { return updateArtifactsFlowForInterfaceOperations(parent, parentId, artifactId, artifactInfo, user, - decodedPayload, componentType, auditingAction, operationUuid, artifactData, prevArtifactId, + decodedPayload, componentType, auditingAction, interfaceType, operationUuid, artifactData, prevArtifactId, currArtifactId, artifactDefinition); } @@ -3067,7 +3067,7 @@ public class ArtifactsBusinessLogic extends BaseBusinessLogic { private Either<Either<ArtifactDefinition, Operation>, ResponseFormat> updateArtifactsFlowForInterfaceOperations( Component parent, String parentId, String artifactId, ArtifactDefinition artifactInfo, User user, - byte[] decodedPayload, ComponentTypeEnum componentType, AuditingActionEnum auditingAction, + byte[] decodedPayload, ComponentTypeEnum componentType, AuditingActionEnum auditingAction, String interfaceType, String operationUuid, ESArtifactData artifactData, String prevArtifactId, String currArtifactId, ArtifactDefinition artifactDefinition) { StorageOperationStatus error; @@ -3110,24 +3110,15 @@ public class ArtifactsBusinessLogic extends BaseBusinessLogic { } Component storedComponent = componentStorageOperationStatusEither.left().value(); - String interfaceToscaName = InterfaceUtils.createInterfaceToscaResourceName( - storedComponent.getName()); - //fetch the interface from storage - Optional<InterfaceDefinition> interfaceDefinition = - storedComponent.getInterfaces().values().stream() - .filter(interfaceDef -> interfaceDef.getToscaResourceName() - .equals(interfaceToscaName)).findFirst(); - if (!interfaceDefinition.isPresent()) { + Optional<InterfaceDefinition> optionalInterface = InterfaceOperationUtils + .getInterfaceDefinitionFromComponentByInterfaceType(storedComponent, interfaceType); + if(!optionalInterface.isPresent()) { log.debug("Failed to get resource interface for resource Id {}", parentId); - ResponseFormat responseFormat = componentsUtils.getResponseFormat( - ActionStatus.INTERFACE_OPERATION_NOT_FOUND, parentId); - handleAuditing(auditingAction, parent, parentId, user, artifactInfo, prevArtifactId, - currArtifactId, responseFormat, componentType, null); - return Either.right(responseFormat); + return Either.right(componentsUtils.getResponseFormat(ActionStatus.INTERFACE_NOT_FOUND_IN_COMPONENT, interfaceType)); } //fetch the operation from storage - InterfaceDefinition gotInterface = interfaceDefinition.get(); + InterfaceDefinition gotInterface = optionalInterface.get(); Map<String, Operation> operationsMap = gotInterface.getOperationsMap(); Optional<Operation> optionalOperation = operationsMap.values() .stream() @@ -3152,8 +3143,8 @@ public class ArtifactsBusinessLogic extends BaseBusinessLogic { implementationArtifact.setEsId(artifactInfo.getEsId()); operation.setImplementation(implementationArtifact); gotInterface.setOperationsMap(operationsMap); - Either<InterfaceDefinition, StorageOperationStatus> interfaceDefinitionStorageOperationStatusEither = - interfaceOperation.updateInterface(storedComponent.getUniqueId(), gotInterface); + Either<List<InterfaceDefinition>, StorageOperationStatus> interfaceDefinitionStorageOperationStatusEither = + interfaceOperation.updateInterfaces(storedComponent.getUniqueId(), Collections.singletonList(gotInterface)); if (interfaceDefinitionStorageOperationStatusEither.isRight()){ StorageOperationStatus storageOperationStatus = interfaceDefinitionStorageOperationStatusEither.right().value(); ActionStatus actionStatus = @@ -5027,13 +5018,13 @@ public class ArtifactsBusinessLogic extends BaseBusinessLogic { * @param componentType * @param componentUuid * @param artifactUUID - * @param operation TODO + * @param operation * @return */ public Either<ArtifactDefinition, ResponseFormat> updateArtifactOnInterfaceOperationByResourceUUID( String data, HttpServletRequest request, ComponentTypeEnum componentType, - String componentUuid, String artifactUUID, String operationUUID, - ResourceCommonInfo resourceCommonInfo,ArtifactOperationInfo operation) { + String componentUuid, String interfaceUUID, String operationUUID, String artifactUUID, + ResourceCommonInfo resourceCommonInfo,ArtifactOperationInfo operation) { Wrapper<ResponseFormat> errorWrapper = new Wrapper<>(); Either<ArtifactDefinition, ResponseFormat> updateArtifactResult; Either<Either<ArtifactDefinition, Operation>, ResponseFormat> actionResult = null; @@ -5067,7 +5058,7 @@ public class ArtifactsBusinessLogic extends BaseBusinessLogic { resourceCommonInfo.setResourceName(componentName); } if (errorWrapper.isEmpty()) { - Either<String, ResponseFormat> interfaceName = fetchInterfaceName(componentId); + Either<String, ResponseFormat> interfaceName = fetchInterfaceName(componentId, interfaceUUID); if (interfaceName.isRight()) { errorWrapper.setInnerElement(interfaceName.right().value()); } @@ -5094,19 +5085,21 @@ public class ArtifactsBusinessLogic extends BaseBusinessLogic { return updateArtifactResult; } - private Either<String, ResponseFormat> fetchInterfaceName(String componentId) { - Either<Component, StorageOperationStatus> componentStorageOperationStatusEither = - toscaOperationFacade.getToscaElement(componentId); + private Either<String, ResponseFormat> fetchInterfaceName(String componentId, String interfaceUUID) { + Either<Component, StorageOperationStatus> componentStorageOperationStatusEither = toscaOperationFacade.getToscaElement(componentId); if (componentStorageOperationStatusEither.isRight()) { StorageOperationStatus errorStatus = componentStorageOperationStatusEither.right().value(); log.debug("Failed to fetch component information by component id, error {}", errorStatus); - return Either.right(componentsUtils - .getResponseFormat(componentsUtils.convertFromStorageResponse(errorStatus))); + return Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(errorStatus))); } Component storedComponent = componentStorageOperationStatusEither.left().value(); - return Either.left(InterfaceUtils.createInterfaceToscaResourceName( - storedComponent.getName())); + Optional<InterfaceDefinition> optionalInterface = InterfaceOperationUtils + .getInterfaceDefinitionFromComponentByInterfaceId(storedComponent, interfaceUUID); + if(!optionalInterface.isPresent()) { + return Either.right(componentsUtils.getResponseFormat(ActionStatus.INTERFACE_NOT_FOUND_IN_COMPONENT, interfaceUUID)); + } + return Either.left(optionalInterface.get().getType()); } diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/BaseBusinessLogic.java b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/BaseBusinessLogic.java index 02e64334f9..8ee1864862 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/BaseBusinessLogic.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/BaseBusinessLogic.java @@ -128,10 +128,7 @@ public abstract class BaseBusinessLogic { protected InterfaceOperation interfaceOperation; @Autowired - protected InterfaceOperationBusinessLogic interfaceOperationBusinessLogic; - - @Autowired - protected InterfaceLifecycleOperation interfaceLifecycleOperation; + protected InterfaceLifecycleOperation interfaceLifecycleTypeOperation; @javax.annotation.Resource private UserValidations userValidations; @@ -167,18 +164,6 @@ public abstract class BaseBusinessLogic { this.propertyOperation = propertyOperation; } - public void setInterfaceOperation(InterfaceOperation interfaceOperation) { - this.interfaceOperation = interfaceOperation; - } - public void setInterfaceOperationBusinessLogic(InterfaceOperationBusinessLogic interfaceOperationBusinessLogic) { - this.interfaceOperationBusinessLogic = interfaceOperationBusinessLogic; - } - - - public void setInterfaceLifecycleOperation(InterfaceLifecycleOperation interfaceLifecycleOperation) { - this.interfaceLifecycleOperation = interfaceLifecycleOperation; - } - User validateUserNotEmpty(User user, String ecompErrorContext) { return userValidations.validateUserNotEmpty(user, ecompErrorContext); } 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 a0efddbdda..2bb53d01e7 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 @@ -18,15 +18,28 @@ package org.openecomp.sdc.be.components.impl; import fj.data.Either; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Optional; +import java.util.UUID; +import java.util.stream.Collectors; +import org.openecomp.sdc.be.components.utils.InterfaceOperationUtils; import org.openecomp.sdc.be.components.validation.InterfaceOperationValidation; import org.openecomp.sdc.be.dao.api.ActionStatus; -import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum; +import org.openecomp.sdc.be.dao.cassandra.ArtifactCassandraDao; +import org.openecomp.sdc.be.dao.cassandra.CassandraOperationStatus; +import org.openecomp.sdc.be.datatypes.elements.OperationDataDefinition; import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum; import org.openecomp.sdc.be.model.ArtifactDefinition; import org.openecomp.sdc.be.model.InterfaceDefinition; import org.openecomp.sdc.be.model.Operation; import org.openecomp.sdc.be.model.User; -import org.openecomp.sdc.be.model.jsontitan.utils.InterfaceUtils; import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus; import org.openecomp.sdc.common.api.ArtifactGroupTypeEnum; import org.openecomp.sdc.common.api.ArtifactTypeEnum; @@ -47,7 +60,6 @@ import java.util.stream.Collectors; public class InterfaceOperationBusinessLogic extends BaseBusinessLogic { private static final Logger LOGGER = LoggerFactory.getLogger(InterfaceOperationBusinessLogic.class); - private static final String FAILED_TO_LOCK_COMPONENT_RESPONSE_IS = "Failed to lock component {}. Response is {}"; private static final String EXCEPTION_OCCURRED_DURING_INTERFACE_OPERATION = "Exception occurred during {}. Response is {}"; private static final String DELETE_INTERFACE_OPERATION = "deleteInterfaceOperation"; private static final String GET_INTERFACE_OPERATION = "getInterfaceOperation"; @@ -55,19 +67,19 @@ public class InterfaceOperationBusinessLogic extends BaseBusinessLogic { private static final String UPDATE_INTERFACE_OPERATION = "updateInterfaceOperation"; @Autowired + private ArtifactCassandraDao artifactCassandraDao; + + @Autowired private InterfaceOperationValidation interfaceOperationValidation; - public void setInterfaceOperationValidation(InterfaceOperationValidation interfaceOperationValidation) { - this.interfaceOperationValidation = interfaceOperationValidation; - } + public Either<List<InterfaceDefinition>, ResponseFormat> deleteInterfaceOperation(String componentId, String interfaceId, List<String> operationsToDelete, User user, boolean lock) { + validateUserExists(user.getUserId(), DELETE_INTERFACE_OPERATION, true); - public Either<Operation, ResponseFormat> deleteInterfaceOperation(String componentId, String interfaceOperationToDelete, User user, boolean lock) { Either<org.openecomp.sdc.be.model.Component, ResponseFormat> componentEither = getComponentDetails(componentId); if (componentEither.isRight()){ return Either.right(componentEither.right().value()); } org.openecomp.sdc.be.model.Component storedComponent = componentEither.left().value(); - validateUserExists(user.getUserId(), DELETE_INTERFACE_OPERATION, true); Either<Boolean, ResponseFormat> lockResult = lockComponentResult(lock, storedComponent, DELETE_INTERFACE_OPERATION); if (lockResult.isRight()) { @@ -75,26 +87,53 @@ public class InterfaceOperationBusinessLogic extends BaseBusinessLogic { } try { - Optional<InterfaceDefinition> optionalInterface = InterfaceUtils.getInterfaceDefinitionFromToscaName(storedComponent.getInterfaces().values(), storedComponent.getName()); - Either<InterfaceDefinition, ResponseFormat> getInterfaceEither = getInterfaceDefinition(storedComponent, optionalInterface.orElse(null)); - if (getInterfaceEither.isRight()) { - return Either.right(getInterfaceEither.right().value()); + Optional<InterfaceDefinition> optionalInterface = InterfaceOperationUtils + .getInterfaceDefinitionFromComponentByInterfaceId(storedComponent, interfaceId); + if(!optionalInterface.isPresent()) { + return Either.right(componentsUtils.getResponseFormat(ActionStatus.INTERFACE_NOT_FOUND_IN_COMPONENT, interfaceId)); } - InterfaceDefinition interfaceDefinition = getInterfaceEither.left().value(); + InterfaceDefinition interfaceDefinition = optionalInterface.get(); + + Map<String, Operation> operationsCollection = new HashMap<>(); + for (String operationId : operationsToDelete){ + Optional<Map.Entry<String, Operation>> optionalOperation = InterfaceOperationUtils + .getOperationFromInterfaceDefinition(interfaceDefinition, operationId); + if(!optionalOperation.isPresent()){ + return Either.right(componentsUtils.getResponseFormat(ActionStatus.INTERFACE_OPERATION_NOT_FOUND, storedComponent.getUniqueId())); + } - Either<Operation, ResponseFormat> getOperationEither = getOperationFromInterfaceDef(storedComponent, interfaceDefinition, interfaceOperationToDelete); - if (getOperationEither.isRight()){ - return Either.right(getOperationEither.right().value()); + Operation storedOperation = optionalOperation.get().getValue(); + String artifactUUID = storedOperation.getImplementation().getArtifactUUID(); + CassandraOperationStatus cassandraStatus = artifactCassandraDao.deleteArtifact(artifactUUID); + if (cassandraStatus != CassandraOperationStatus.OK) { + ResponseFormat responseFormatByArtifactId = componentsUtils.getResponseFormatByArtifactId( + componentsUtils.convertFromStorageResponse(componentsUtils.convertToStorageOperationStatus(cassandraStatus)), + storedOperation.getImplementation().getArtifactDisplayName()); + return Either.right(responseFormatByArtifactId); + } + + operationsCollection.put(operationId, interfaceDefinition.getOperationsMap().get(operationId)); + interfaceDefinition.getOperations().remove(operationId); + } + + Either<List<InterfaceDefinition>, StorageOperationStatus> deleteOperationEither = interfaceOperation.updateInterfaces(storedComponent.getUniqueId(), Collections.singletonList(interfaceDefinition)); + if (deleteOperationEither.isRight()) { + titanDao.rollback(); + return Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(deleteOperationEither.right().value(), storedComponent.getComponentType()))); } - Either<Operation, StorageOperationStatus> deleteEither = interfaceOperation.deleteInterfaceOperation(componentId, interfaceDefinition, interfaceOperationToDelete); - if (deleteEither.isRight()){ - LOGGER.error("Failed to delete interface operation from component {}. Response is {}", storedComponent.getName(), deleteEither.right().value()); - return Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(deleteEither.right().value(), storedComponent.getComponentType()))); + if(interfaceDefinition.getOperations().isEmpty()) { + Either<String, StorageOperationStatus> deleteInterfaceEither = interfaceOperation.deleteInterface(storedComponent.getUniqueId(), interfaceDefinition.getUniqueId()); + if (deleteInterfaceEither.isRight()) { + titanDao.rollback(); + return Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(deleteInterfaceEither.right().value(), storedComponent.getComponentType()))); + } } titanDao.commit(); - return Either.left(deleteEither.left().value()); + interfaceDefinition.getOperations().putAll(operationsCollection); + interfaceDefinition.getOperations().keySet().removeIf(key -> !(operationsToDelete.contains(key))); + return Either.left(Arrays.asList(interfaceDefinition)); } catch (Exception e){ LOGGER.error(EXCEPTION_OCCURRED_DURING_INTERFACE_OPERATION, "delete", e); @@ -108,13 +147,14 @@ public class InterfaceOperationBusinessLogic extends BaseBusinessLogic { } } - public Either<Operation, ResponseFormat> getInterfaceOperation(String componentId, String interfaceOperationToGet, User user, boolean lock) { + public Either<List<InterfaceDefinition>, ResponseFormat> getInterfaceOperation(String componentId, String interfaceId, List<String> operationsToGet, User user, boolean lock) { + validateUserExists(user.getUserId(), GET_INTERFACE_OPERATION, true); + Either<org.openecomp.sdc.be.model.Component, ResponseFormat> componentEither = getComponentDetails(componentId); if (componentEither.isRight()){ return Either.right(componentEither.right().value()); } org.openecomp.sdc.be.model.Component storedComponent = componentEither.left().value(); - validateUserExists(user.getUserId(), GET_INTERFACE_OPERATION, true); Either<Boolean, ResponseFormat> lockResult = lockComponentResult(lock, storedComponent, GET_INTERFACE_OPERATION); if (lockResult.isRight()) { @@ -122,20 +162,24 @@ public class InterfaceOperationBusinessLogic extends BaseBusinessLogic { } try { - Optional<InterfaceDefinition> optionalInterface = InterfaceUtils.getInterfaceDefinitionFromToscaName(storedComponent.getInterfaces().values(), storedComponent.getName()); - Either<InterfaceDefinition, ResponseFormat> getInterfaceEither = getInterfaceDefinition(storedComponent, optionalInterface.orElse(null)); - if (getInterfaceEither.isRight()) { - return Either.right(getInterfaceEither.right().value()); + Optional<InterfaceDefinition> optionalInterface = InterfaceOperationUtils + .getInterfaceDefinitionFromComponentByInterfaceId(storedComponent, interfaceId); + if(!optionalInterface.isPresent()) { + return Either.right(componentsUtils.getResponseFormat(ActionStatus.INTERFACE_NOT_FOUND_IN_COMPONENT, interfaceId)); } - InterfaceDefinition interfaceDefinition = getInterfaceEither.left().value(); + InterfaceDefinition interfaceDefinition = optionalInterface.get(); - Either<Operation, ResponseFormat> getOperationEither = getOperationFromInterfaceDef(storedComponent, interfaceDefinition, interfaceOperationToGet); - if (getOperationEither.isRight()){ - return Either.right(getOperationEither.right().value()); + for (String operationId : operationsToGet) { + Optional<Map.Entry<String, Operation>> optionalOperation = InterfaceOperationUtils + .getOperationFromInterfaceDefinition(interfaceDefinition, operationId); + if(!optionalOperation.isPresent()){ + return Either.right(componentsUtils.getResponseFormat(ActionStatus.INTERFACE_OPERATION_NOT_FOUND, storedComponent.getUniqueId())); + } } titanDao.commit(); - return Either.left(getOperationEither.left().value()); + interfaceDefinition.getOperations().keySet().removeIf(key -> !(operationsToGet.contains(key))); + return Either.left(Arrays.asList(interfaceDefinition)); } catch (Exception e){ LOGGER.error(EXCEPTION_OCCURRED_DURING_INTERFACE_OPERATION, "get", e); @@ -149,78 +193,110 @@ public class InterfaceOperationBusinessLogic extends BaseBusinessLogic { } } - private Either<InterfaceDefinition, ResponseFormat> 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(component.getName())); - Either<InterfaceDefinition, StorageOperationStatus> interfaceCreateEither = interfaceOperation.addInterface(component.getUniqueId(), interfaceDefinition); - if (interfaceCreateEither.isRight()){ - StorageOperationStatus sValue = interfaceCreateEither.right().value(); - LOGGER.error("Failed to get interface from component {}. Response is {}", component.getName(), sValue); - return Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(sValue, component.getComponentType()), "")); - } - return Either.left(interfaceCreateEither.left().value()); - } + public Either<List<InterfaceDefinition>, ResponseFormat> createInterfaceOperation(String componentId, List<InterfaceDefinition> interfaceDefinitions, User user, boolean lock) { + return createOrUpdateInterfaceOperation(componentId, interfaceDefinitions, user, false, CREATE_INTERFACE_OPERATION, lock); } - public Either<Operation, ResponseFormat> createInterfaceOperation(String componentId, Operation operation, User user, boolean lock) { - return createOrUpdateInterfaceOperation(componentId, operation, user, false, CREATE_INTERFACE_OPERATION, lock); + public Either<List<InterfaceDefinition>, ResponseFormat> updateInterfaceOperation(String componentId, List<InterfaceDefinition> interfaceDefinitions, User user, boolean lock) { + return createOrUpdateInterfaceOperation(componentId, interfaceDefinitions, user, true, UPDATE_INTERFACE_OPERATION, lock); } - public Either<Operation, ResponseFormat> updateInterfaceOperation(String componentId, Operation operation, User user, boolean lock) { - return createOrUpdateInterfaceOperation(componentId, operation, user, true, UPDATE_INTERFACE_OPERATION, lock); + public Either<Map<String, InterfaceDefinition>, ResponseFormat> getAllInterfaceLifecycleTypes() { + + Either<Map<String, InterfaceDefinition>, StorageOperationStatus> interfaceLifecycleTypes = interfaceLifecycleTypeOperation.getAllInterfaceLifecycleTypes(); + if(interfaceLifecycleTypes.isRight()) { + return Either.right(componentsUtils.getResponseFormat(ActionStatus.INTERFACE_LIFECYCLE_TYPES_NOT_FOUND)); + } + interfaceLifecycleTypes.left().value().values().stream().forEach( + id -> id.setOperations(id.getOperations().keySet().stream().collect(Collectors.toMap(key -> key.replaceFirst(id.getUniqueId()+".",""), i -> id.getOperations().get(i))))); + + return Either.left(interfaceLifecycleTypes.left().value()); } - private Either<Operation, ResponseFormat> createOrUpdateInterfaceOperation(String componentId, Operation operation, User user, boolean isUpdate, String errorContext, boolean lock) { + private Either<List<InterfaceDefinition>, ResponseFormat> createOrUpdateInterfaceOperation(String componentId, List<InterfaceDefinition> interfaceDefinitions, User user, boolean isUpdate, String errorContext, boolean lock) { + validateUserExists(user.getUserId(), errorContext, true); + Either<org.openecomp.sdc.be.model.Component, ResponseFormat> componentEither = getComponentDetails(componentId); if (componentEither.isRight()){ return Either.right(componentEither.right().value()); } org.openecomp.sdc.be.model.Component storedComponent = componentEither.left().value(); - validateUserExists(user.getUserId(), errorContext, true); - Either<Boolean, ResponseFormat> interfaceOperationValidationResponseEither = interfaceOperationValidation - .validateInterfaceOperations(Collections.singletonList(operation), storedComponent, isUpdate); - if(interfaceOperationValidationResponseEither.isRight()) { - return Either.right(interfaceOperationValidationResponseEither.right().value()); - } Either<Boolean, ResponseFormat> lockResult = lockComponentResult(lock, storedComponent, errorContext); if (lockResult.isRight()) { return Either.right(lockResult.right().value()); } + Either<Map<String, InterfaceDefinition>, ResponseFormat> interfaceLifecycleTypes = getAllInterfaceLifecycleTypes(); + if (interfaceLifecycleTypes.isRight()) { + return Either.right(interfaceLifecycleTypes.right().value()); + } + try { - Optional<InterfaceDefinition> optionalInterface = InterfaceUtils.getInterfaceDefinitionFromToscaName(storedComponent.getInterfaces().values(), storedComponent.getName()); - Either<InterfaceDefinition, ResponseFormat> getInterfaceEither = getInterfaceDefinition(storedComponent, optionalInterface.orElse(null)); - if (getInterfaceEither.isRight()) { - return Either.right(getInterfaceEither.right().value()); - } - InterfaceDefinition interfaceDefinition = getInterfaceEither.left().value(); + List<InterfaceDefinition> interfacesCollection = new ArrayList<>(); + Map<String, Operation> operationsCollection = new HashMap<>(); + for(InterfaceDefinition inputInterfaceDefinition : interfaceDefinitions) { + Optional<InterfaceDefinition> optionalInterface = InterfaceOperationUtils.getInterfaceDefinitionFromComponentByInterfaceType(storedComponent, inputInterfaceDefinition.getType()); + Either<Boolean, ResponseFormat> interfaceOperationValidationResponseEither = interfaceOperationValidation.validateInterfaceOperations(inputInterfaceDefinition, storedComponent, + optionalInterface.orElse(null), interfaceLifecycleTypes.left().value(), isUpdate); + if(interfaceOperationValidationResponseEither.isRight()) { + return Either.right(interfaceOperationValidationResponseEither.right().value()); + } - Either<Operation, StorageOperationStatus> result; - if(!isUpdate){ - initNewOperation(operation); - result = interfaceOperation.addInterfaceOperation(componentId, interfaceDefinition, operation); - } - else { - Either<Operation, ResponseFormat> getOperationEither = getOperationFromInterfaceDef(storedComponent, interfaceDefinition, operation.getUniqueId()); - if (getOperationEither.isRight()){ - return Either.right(getOperationEither.right().value()); + Map<String, Operation> operationsToAddOrUpdate = inputInterfaceDefinition.getOperationsMap(); + operationsCollection.putAll(operationsToAddOrUpdate); + inputInterfaceDefinition.getOperations().clear(); + + Either<InterfaceDefinition, ResponseFormat> getInterfaceEither = getOrCreateInterfaceDefinition(storedComponent, inputInterfaceDefinition, optionalInterface.orElse(null)); + if (getInterfaceEither.isRight()) { + return Either.right(getInterfaceEither.right().value()); } - updateExistingOperation(operation, getOperationEither.left().value().getImplementation().getArtifactUUID()); - result = interfaceOperation.updateInterfaceOperation(componentId, interfaceDefinition, operation); + InterfaceDefinition interfaceDef = getInterfaceEither.left().value(); + + updateOperationInputDefs(storedComponent, operationsToAddOrUpdate.values()); + + for (Operation operation : operationsToAddOrUpdate.values()) { + if (!isUpdate) { + addOperationToInterface(interfaceDef, operation); + } + else { + Optional<Map.Entry<String, Operation>> optionalOperation = InterfaceOperationUtils + .getOperationFromInterfaceDefinition(interfaceDef, operation.getUniqueId()); + if(!optionalOperation.isPresent()){ + titanDao.rollback(); + return Either.right(componentsUtils.getResponseFormat(ActionStatus.INTERFACE_OPERATION_NOT_FOUND, storedComponent.getUniqueId())); + } + + Operation storedOperation = optionalOperation.get().getValue(); + String artifactUUID = storedOperation.getImplementation().getArtifactUUID(); + Either<Long, CassandraOperationStatus> artifactCount = artifactCassandraDao.getCountOfArtifactById(artifactUUID); + if(artifactCount.isLeft()){ + CassandraOperationStatus cassandraStatus = artifactCassandraDao.deleteArtifact(artifactUUID); + if (cassandraStatus != CassandraOperationStatus.OK) { + titanDao.rollback(); + ResponseFormat responseFormatByArtifactId = componentsUtils.getResponseFormatByArtifactId( + componentsUtils.convertFromStorageResponse(componentsUtils.convertToStorageOperationStatus(cassandraStatus)), + storedOperation.getImplementation().getArtifactDisplayName()); + return Either.right(responseFormatByArtifactId); + } + } + updateOperationOnInterface(interfaceDef, operation, artifactUUID); + } + } + interfacesCollection.add(interfaceDef); } - if (result.isRight()) { + Either<List<InterfaceDefinition>, StorageOperationStatus> addCreateOperationEither = interfaceOperation.updateInterfaces(storedComponent.getUniqueId(), interfacesCollection); + if (addCreateOperationEither.isRight()) { titanDao.rollback(); - LOGGER.debug("Failed to addOrUpdate interface operation on component {}. Response is {}", storedComponent.getName(), result.right().value()); - return Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(result.right().value(), storedComponent.getComponentType()))); + return Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(addCreateOperationEither.right().value(), storedComponent.getComponentType()))); } titanDao.commit(); - return Either.left(result.left().value()); + interfacesCollection.forEach(interfaceDefinition -> interfaceDefinition.getOperations().entrySet() + .removeIf(entry -> !operationsCollection.values().stream().map(OperationDataDefinition::getName) + .collect(Collectors.toList()).contains(entry.getValue().getName()))); + return Either.left(interfacesCollection); } catch (Exception e) { titanDao.rollback(); @@ -237,49 +313,15 @@ public class InterfaceOperationBusinessLogic extends BaseBusinessLogic { private Either<org.openecomp.sdc.be.model.Component, ResponseFormat> getComponentDetails(String componentId){ Either<org.openecomp.sdc.be.model.Component, StorageOperationStatus> componentStorageOperationStatusEither = toscaOperationFacade.getToscaElement(componentId); if (componentStorageOperationStatusEither.isRight()) { - StorageOperationStatus errorStatus = componentStorageOperationStatusEither.right().value(); - LOGGER.error("Failed to fetch component information by component id {}, Response is {}", componentId, errorStatus); - return Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(errorStatus))); + return Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(componentStorageOperationStatusEither.right().value()))); } return Either.left(componentStorageOperationStatusEither.left().value()); } - private Either<Operation, ResponseFormat> getOperationFromInterfaceDef( - org.openecomp.sdc.be.model.Component component, InterfaceDefinition interfaceDefinition, String operationToFetch) { - Optional<Map.Entry<String, Operation>> operationMap = interfaceDefinition.getOperationsMap().entrySet().stream() - .filter(entry -> entry.getValue().getUniqueId().equals(operationToFetch)).findAny(); - if (!operationMap.isPresent()) { - LOGGER.error("Failed to get interface operation from component {}. Response is {}", component.getUniqueId(), ActionStatus.INTERFACE_OPERATION_NOT_FOUND); - return Either.right(componentsUtils.getResponseFormat(ActionStatus.INTERFACE_OPERATION_NOT_FOUND, component.getUniqueId())); - } - return Either.left(operationMap.get().getValue()); - } - - private void initNewOperation(Operation operation){ - ArtifactDefinition artifactDefinition = new ArtifactDefinition(); - String artifactUUID = UUID.randomUUID().toString(); - artifactDefinition.setArtifactUUID(artifactUUID); - artifactDefinition.setUniqueId(artifactUUID); - artifactDefinition.setArtifactType(ArtifactTypeEnum.WORKFLOW.getType()); - artifactDefinition.setArtifactGroupType(ArtifactGroupTypeEnum.DEPLOYMENT); - operation.setUniqueId(UUID.randomUUID().toString()); - operation.setImplementation(artifactDefinition); - } - - private void updateExistingOperation(Operation operation, String artifactUUID){ - ArtifactDefinition artifactDefinition = new ArtifactDefinition(); - artifactDefinition.setArtifactUUID(artifactUUID); - artifactDefinition.setUniqueId(artifactUUID); - artifactDefinition.setArtifactType(ArtifactTypeEnum.WORKFLOW.getType()); - artifactDefinition.setArtifactGroupType(ArtifactGroupTypeEnum.DEPLOYMENT); - operation.setImplementation(artifactDefinition); - } - private Either<Boolean, ResponseFormat> lockComponentResult(boolean lock, org.openecomp.sdc.be.model.Component component, String action){ if (lock) { Either<Boolean, ResponseFormat> lockResult = lockComponent(component.getUniqueId(), component, action); if (lockResult.isRight()) { - LOGGER.debug(FAILED_TO_LOCK_COMPONENT_RESPONSE_IS, component.getName(), lockResult.right().value().getFormattedMessage()); titanDao.rollback(); return Either.right(lockResult.right().value()); } @@ -287,54 +329,53 @@ public class InterfaceOperationBusinessLogic extends BaseBusinessLogic { return Either.left(true); } - public Either<Boolean, ResponseFormat> validateComponentNameAndUpdateInterfaces(org.openecomp.sdc.be.model.Component oldComponent, - org.openecomp.sdc.be.model.Component newComponent) { - if(!oldComponent.getName().equals(newComponent.getName()) ) { - Collection<InterfaceDefinition> interfaceDefinitionListFromToscaName = InterfaceUtils - .getInterfaceDefinitionListFromToscaName(oldComponent.getInterfaces().values(), - oldComponent.getName()); - for (InterfaceDefinition interfaceDefinition : interfaceDefinitionListFromToscaName) { - - Either<InterfaceDefinition, ResponseFormat> interfaceDefinitionResponseEither = updateInterfaceDefinition(oldComponent, - newComponent, interfaceDefinition); - if(interfaceDefinitionResponseEither.isRight()) { - return Either.right(interfaceDefinitionResponseEither.right().value()); - } - } + private Either<InterfaceDefinition, ResponseFormat> getOrCreateInterfaceDefinition(org.openecomp.sdc.be.model.Component component, InterfaceDefinition interfaceDefinition, InterfaceDefinition storedInterfaceDef) { + if (storedInterfaceDef != null){ + return Either.left(storedInterfaceDef); } - return Either.left(Boolean.TRUE); - } - private Either<InterfaceDefinition, ResponseFormat > updateInterfaceDefinition(org.openecomp.sdc.be.model.Component oldComponent, - org.openecomp.sdc.be.model.Component newComponent, - InterfaceDefinition interfaceDefinition) { - InterfaceUtils.createInterfaceToscaResourceName(newComponent.getName()); - interfaceDefinition.setToscaResourceName(InterfaceUtils - .createInterfaceToscaResourceName(newComponent.getName())); - try { - Either<InterfaceDefinition, StorageOperationStatus> interfaceUpdate = interfaceOperation - .updateInterface(oldComponent.getUniqueId(), interfaceDefinition); - if (interfaceUpdate.isRight()) { - LOGGER.error("Failed to Update interface {}. Response is {}. ", newComponent.getName(), interfaceUpdate.right().value()); + else { + interfaceDefinition.setUniqueId(UUID.randomUUID().toString()); + interfaceDefinition.setToscaResourceName(interfaceDefinition.getType()); + Either<List<InterfaceDefinition>, StorageOperationStatus> interfaceCreateEither = interfaceOperation.addInterfaces(component.getUniqueId(), Collections.singletonList(interfaceDefinition)); + if (interfaceCreateEither.isRight()){ titanDao.rollback(); - return Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(interfaceUpdate.right().value(), ComponentTypeEnum.RESOURCE))); + return Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(interfaceCreateEither.right().value(), component.getComponentType()))); } - } catch (Exception e) { - LOGGER.error("Exception occurred during update interface toscaResourceName : {}", e); - titanDao.rollback(); - return Either.right(componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR)); + return Either.left(interfaceCreateEither.left().value().get(0)); } - return Either.left( interfaceDefinition); } - public Either<Map<String, InterfaceDefinition>, ResponseFormat> getAllInterfaceLifecycleTypes() { + private void addOperationToInterface(InterfaceDefinition interfaceDefinition, Operation interfaceOperation){ + ArtifactDefinition artifactDefinition = new ArtifactDefinition(); + String artifactUUID = UUID.randomUUID().toString(); + artifactDefinition.setArtifactUUID(artifactUUID); + artifactDefinition.setUniqueId(artifactUUID); + artifactDefinition.setArtifactType(ArtifactTypeEnum.WORKFLOW.getType()); + artifactDefinition.setArtifactGroupType(ArtifactGroupTypeEnum.DEPLOYMENT); + interfaceOperation.setUniqueId(UUID.randomUUID().toString()); + interfaceOperation.setImplementation(artifactDefinition); + interfaceDefinition.getOperations().put(interfaceOperation.getUniqueId(), new OperationDataDefinition(interfaceOperation)); + } - Either<Map<String, InterfaceDefinition>, StorageOperationStatus> interfaceLifecycleTypes = interfaceLifecycleOperation.getAllInterfaceLifecycleTypes(); - if(interfaceLifecycleTypes.isRight()) { - return Either.right(componentsUtils.getResponseFormat(ActionStatus.INTERFACE_LIFECYCLE_TYPES_NOT_FOUND)); - } - interfaceLifecycleTypes.left().value().values().stream().forEach( - id -> id.setOperations(id.getOperations().keySet().stream().collect(Collectors.toMap(key -> key.replaceFirst(id.getUniqueId()+".",""), i -> id.getOperations().get(i))))); + private void updateOperationOnInterface(InterfaceDefinition interfaceDefinition, Operation interfaceOperation, String artifactUUID){ + ArtifactDefinition artifactDefinition = new ArtifactDefinition(); + artifactDefinition.setArtifactUUID(artifactUUID); + artifactDefinition.setUniqueId(artifactUUID); + artifactDefinition.setArtifactType(ArtifactTypeEnum.WORKFLOW.getType()); + artifactDefinition.setArtifactGroupType(ArtifactGroupTypeEnum.DEPLOYMENT); + interfaceOperation.setImplementation(artifactDefinition); + interfaceDefinition.getOperations().put(interfaceOperation.getUniqueId(), new OperationDataDefinition(interfaceOperation)); + } - return Either.left(interfaceLifecycleTypes.left().value()); + private void updateOperationInputDefs(org.openecomp.sdc.be.model.Component component, Collection<Operation> interfaceOperations) { + interfaceOperations.stream().filter(operation -> Objects.nonNull(operation.getInputs())) + .forEach(operation -> operation.getInputs().getListToscaDataDefinition() + .forEach(inp -> component.getInputs().stream().filter(in -> inp.getInputId().equals(in.getUniqueId())) + .forEach(in -> { + inp.setDefaultValue(in.getDefaultValue()); + inp.setValue(in.getValue()); + inp.setSchema(in.getSchema()); + }))); } + } diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ResourceBusinessLogic.java b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ResourceBusinessLogic.java index 7917a0aa5d..4e842928ef 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ResourceBusinessLogic.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ResourceBusinessLogic.java @@ -3984,14 +3984,6 @@ public class ResourceBusinessLogic extends ComponentBusinessLogic { newResource.setDerivedFrom(null); } - Either<Boolean, ResponseFormat> validateAndUpdateInterfacesEither = - interfaceOperationBusinessLogic.validateComponentNameAndUpdateInterfaces(currentResource, newResource); - if (validateAndUpdateInterfacesEither.isRight()) { - log.error("failed to validate and update Interfaces"); - rollbackNeeded = true; - throw new ComponentException(validateAndUpdateInterfacesEither.right().value()); - } - Either<Resource, ResponseFormat> dataModelResponse = updateResourceMetadata(resourceIdToUpdate, newResource, user, currentResource, false, true); if (dataModelResponse.isRight()) { 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 e85afdc48f..8d4f896d47 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 @@ -518,11 +518,7 @@ public class ServiceBusinessLogic extends ComponentBusinessLogic { log.info("Restricted operation for user: {}, on service: {}", user.getUserId(), currentService.getCreatorUserId()); return Either.right(componentsUtils.getResponseFormat(ActionStatus.RESTRICTED_OPERATION)); } - Either<Boolean, ResponseFormat> validateAndUpdateInterfacesEither = interfaceOperationBusinessLogic.validateComponentNameAndUpdateInterfaces(currentService, serviceUpdate); - if (validateAndUpdateInterfacesEither.isRight()) { - log.info("failed to validate and update Interfaces on service {}", currentService.getCreatorUserId()); - return Either.right(validateAndUpdateInterfacesEither.right().value()); - } + Either<Service, ResponseFormat> validationRsponse = validateAndUpdateServiceMetadata(user, currentService, serviceUpdate); if (validationRsponse.isRight()) { log.info("service update metadata: validations field."); diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/components/utils/InterfaceOperationUtils.java b/catalog-be/src/main/java/org/openecomp/sdc/be/components/utils/InterfaceOperationUtils.java new file mode 100644 index 0000000000..bac23b2133 --- /dev/null +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/components/utils/InterfaceOperationUtils.java @@ -0,0 +1,54 @@ +/* + * Copyright © 2016-2018 European Support Limited + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.openecomp.sdc.be.components.utils; + +import java.util.Map; +import java.util.Optional; +import org.apache.commons.collections.MapUtils; +import org.openecomp.sdc.be.model.Component; +import org.openecomp.sdc.be.model.InterfaceDefinition; +import org.openecomp.sdc.be.model.Operation; + +public class InterfaceOperationUtils { + + private InterfaceOperationUtils(){} + + public static final Optional<InterfaceDefinition> getInterfaceDefinitionFromComponentByInterfaceType(Component component, String interfaceType) { + if (MapUtils.isEmpty(component.getInterfaces())) { + return Optional.empty(); + } + return component.getInterfaces().values().stream().filter(interfaceDefinition -> interfaceDefinition.getType() != null && interfaceDefinition.getType().equals(interfaceType)).findAny(); + } + + public static final Optional<InterfaceDefinition> getInterfaceDefinitionFromComponentByInterfaceId(Component component, String interfaceId) { + if (MapUtils.isEmpty(component.getInterfaces())) { + return Optional.empty(); + } + return component.getInterfaces().values().stream().filter(interfaceDefinition -> interfaceDefinition.getUniqueId() != null && interfaceDefinition.getUniqueId().equals(interfaceId)).findAny(); + } + + public static final Optional<Map.Entry<String, Operation>> getOperationFromInterfaceDefinition(InterfaceDefinition interfaceDefinition, String operationId) { + if (MapUtils.isEmpty(interfaceDefinition.getOperationsMap())) { + return Optional.empty(); + } + Optional<Map.Entry<String, Operation>> operationMap = interfaceDefinition.getOperationsMap().entrySet().stream().filter(entry -> entry.getValue().getUniqueId().equals(operationId)).findAny(); + if (operationMap.isPresent()) { + return operationMap; + } + return Optional.empty(); + } + +} diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/components/validation/InterfaceOperationValidation.java b/catalog-be/src/main/java/org/openecomp/sdc/be/components/validation/InterfaceOperationValidation.java index 7b650e4fad..bfb2429a83 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/components/validation/InterfaceOperationValidation.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/components/validation/InterfaceOperationValidation.java @@ -16,6 +16,7 @@ package org.openecomp.sdc.be.components.validation; +import fj.data.Either; import java.util.Collection; import java.util.HashMap; import java.util.HashSet; @@ -25,16 +26,11 @@ import java.util.Objects; import java.util.Optional; import java.util.Set; import java.util.regex.Pattern; -import java.util.stream.Collectors; - -import fj.data.Either; +import java.util.stream.Stream; import org.apache.commons.collections.CollectionUtils; -import org.apache.commons.collections.MapUtils; import org.apache.commons.lang.StringUtils; -import org.elasticsearch.common.Strings; import org.openecomp.sdc.be.components.impl.ResponseFormatManager; import org.openecomp.sdc.be.dao.api.ActionStatus; -import org.openecomp.sdc.be.datatypes.elements.ListDataDefinition; import org.openecomp.sdc.be.datatypes.elements.OperationInputDefinition; import org.openecomp.sdc.be.model.InputDefinition; import org.openecomp.sdc.be.model.InterfaceDefinition; @@ -48,64 +44,117 @@ import org.springframework.stereotype.Component; public class InterfaceOperationValidation { private static final String TYPE_VALIDATION_REGEX = "^[a-zA-Z]{1,200}$"; - private static final int DESCRIPTION_MAX_LENGTH = 200; private static final Logger LOGGER = LoggerFactory.getLogger(InterfaceOperationValidation.class); public Either<Boolean, ResponseFormat> validateInterfaceOperations( - Collection<Operation> interfaceOperations, org.openecomp.sdc.be.model.Component component, boolean isUpdate) { + InterfaceDefinition inputInterfaceDefinition, org.openecomp.sdc.be.model.Component component, + InterfaceDefinition storedInterfaceDefinition, Map<String, InterfaceDefinition> globalInterfaceTypes, boolean isUpdate) { + + Either<Boolean, ResponseFormat> validateAllowedOperationCountOnLocalInterfaceType = + validateAllowedOperationCountOnLocalInterfaceType(inputInterfaceDefinition, storedInterfaceDefinition, globalInterfaceTypes, isUpdate); + if(validateAllowedOperationCountOnLocalInterfaceType.isRight()){ + return validateAllowedOperationCountOnLocalInterfaceType; + } + + Either<Boolean, ResponseFormat> validateAllowedOperationsOnGlobalInterfaceType = validateAllowedOperationsOnGlobalInterfaceType(inputInterfaceDefinition, globalInterfaceTypes); + if(validateAllowedOperationsOnGlobalInterfaceType.isRight()){ + return validateAllowedOperationsOnGlobalInterfaceType; + } - for(Operation interfaceOperation : interfaceOperations) { + Either<Boolean, ResponseFormat> validateOperationNameUniqueness = validateOperationNameUniquenessInCollection(inputInterfaceDefinition.getOperationsMap().values()); + if(validateOperationNameUniqueness.isRight()){ + return validateOperationNameUniqueness; + } + + for(Operation interfaceOperation : inputInterfaceDefinition.getOperationsMap().values()) { Either<Boolean, ResponseFormat> interfaceOperationValidatorResponse = validateInterfaceOperation( - interfaceOperation, component, isUpdate); + interfaceOperation, storedInterfaceDefinition, component, isUpdate); if (interfaceOperationValidatorResponse.isRight()) { return interfaceOperationValidatorResponse; } } + + return Either.left(Boolean.TRUE); + } + + private Either<Boolean, ResponseFormat> validateOperationNameUniquenessInCollection(Collection<Operation> operationList){ + HashSet<String> operationNames = new HashSet<>(); + for (Operation operation : operationList) { + if(!operationNames.add(operation.getName())){ + return Either.right(getResponseFormatManager().getResponseFormat(ActionStatus.INTERFACE_OPERATION_NAME_ALREADY_IN_USE, operation.getName())); + } + } + return Either.left(Boolean.TRUE); + } + + private Either<Boolean, ResponseFormat> validateAllowedOperationCountOnLocalInterfaceType(InterfaceDefinition inputInterfaceDefinition, + InterfaceDefinition storedInterfaceDefinition, Map<String, InterfaceDefinition> globalInterfaceTypes, boolean isUpdate){ + + boolean isInterfaceTypeExistInGlobalType = globalInterfaceTypes.values().stream().map(InterfaceDefinition::getType) + .anyMatch(type -> type.equalsIgnoreCase(inputInterfaceDefinition.getType())); + if(!isInterfaceTypeExistInGlobalType && (inputInterfaceDefinition.getOperations().size() > 1 || (!isUpdate && storedInterfaceDefinition != null && storedInterfaceDefinition.getType().equalsIgnoreCase(inputInterfaceDefinition.getType())))){ + return Either.right(getResponseFormatManager().getResponseFormat(ActionStatus.INTERFACE_OPERATION_INVALID_FOR_LOCAL_TYPE, inputInterfaceDefinition.getType())); + } + + return Either.left(Boolean.TRUE); + } + + private Either<Boolean, ResponseFormat> validateAllowedOperationsOnGlobalInterfaceType(InterfaceDefinition interfaceDefinition, + Map<String, InterfaceDefinition> globalInterfaceTypes) { + + if(globalInterfaceTypes != null){ + boolean isOperationValidOnGlobalInterfaceType = Stream.of(interfaceDefinition) + .filter(interfaceDef -> globalInterfaceTypes.values().stream().anyMatch(interfaceDef1 -> interfaceDef1.getType().equalsIgnoreCase(interfaceDef.getType()))) + .flatMap(interfaceDef -> interfaceDef.getOperationsMap().values().stream().map(Operation::getName)) + .allMatch(operationName -> globalInterfaceTypes.values().stream() + .flatMap(interfaceDef -> interfaceDef.getOperationsMap().keySet().stream().map(operation -> operation)) + .anyMatch(opName -> opName.equalsIgnoreCase(operationName))); + if(!isOperationValidOnGlobalInterfaceType){ + return Either.right(getResponseFormatManager().getResponseFormat(ActionStatus.INTERFACE_OPERATION_INVALID_FOR_GLOBAL_TYPE, interfaceDefinition.getType())); + } + } return Either.left(Boolean.TRUE); } private Either<Boolean, ResponseFormat> validateInterfaceOperation(Operation interfaceOperation, + InterfaceDefinition interfaceDefinition, org.openecomp.sdc.be.model.Component component, boolean isUpdate) { - ResponseFormatManager responseFormatManager = getResponseFormatManager(); + ResponseFormatManager responseFormatManager = getResponseFormatManager(); Either<Boolean, ResponseFormat> interfaceOperationTypeResponse = isInterfaceOperationTypeValid(interfaceOperation, - responseFormatManager, component, isUpdate); + responseFormatManager, interfaceDefinition, isUpdate); if (interfaceOperationTypeResponse.isRight()) { return Either.right(interfaceOperationTypeResponse.right().value()); } - Either<Boolean, ResponseFormat> descriptionResponseEither = isValidDescription(responseFormatManager, - interfaceOperation.getDescription()); - if (descriptionResponseEither.isRight()) { - return Either.right(descriptionResponseEither.right().value()); - } - - Either<Boolean, ResponseFormat> inputPropertyExistInComponent = validateInputPropertyExistInComponent(interfaceOperation, - component.getInputs(), responseFormatManager); - if(inputPropertyExistInComponent.isRight()) { - return Either.right(inputPropertyExistInComponent.right().value()); + if(null != interfaceOperation.getInputs()) { + Either<Boolean, ResponseFormat> inputParametersResponse = validateInputParameters(interfaceOperation, responseFormatManager); + if (inputParametersResponse.isRight()) { + return Either.right(inputParametersResponse.right().value()); + } - } + Either<Boolean, ResponseFormat> inputPropertyExistInComponent = validateInputPropertyExistInComponent(interfaceOperation, + component, responseFormatManager); + if(inputPropertyExistInComponent.isRight()) { + return Either.right(inputPropertyExistInComponent.right().value()); - Either<Boolean, ResponseFormat> inputParametersResponse = validateInputParameters(interfaceOperation, - responseFormatManager); - if(inputParametersResponse.isRight()) { - return Either.right(inputParametersResponse.right().value()); + } } - Either<Boolean, ResponseFormat> outputParametersResponse = validateOutputParameters(interfaceOperation, - responseFormatManager); - if(outputParametersResponse.isRight()) { - return Either.right(outputParametersResponse.right().value()); + if(null != interfaceOperation.getOutputs()) { + Either<Boolean, ResponseFormat> outputParametersResponse = validateOutputParameters(interfaceOperation, responseFormatManager); + if (outputParametersResponse.isRight()) { + return Either.right(outputParametersResponse.right().value()); + } } return Either.left(Boolean.TRUE); } private Either<Boolean, ResponseFormat> isInterfaceOperationTypeValid(Operation interfaceOperation, - ResponseFormatManager responseFormatManager, - org.openecomp.sdc.be.model.Component component, boolean isUpdate) { + ResponseFormatManager responseFormatManager, InterfaceDefinition interfaceDefinition, + boolean isUpdate) { Either<Boolean, ResponseFormat> operationTypeEmptyEither = isOperationTypeEmpty(responseFormatManager, interfaceOperation.getName()); @@ -120,7 +169,7 @@ public class InterfaceOperationValidation { } Either<Boolean, ResponseFormat> operationTypeUniqueResponse = validateOperationTypeUnique(interfaceOperation, - component, isUpdate ); + interfaceDefinition, isUpdate ); if(operationTypeUniqueResponse.isRight()) { return Either.right(operationTypeUniqueResponse.right().value()); } @@ -156,42 +205,21 @@ public class InterfaceOperationValidation { return Either.left(Boolean.TRUE); } - private Either<Boolean, ResponseFormat> isValidDescription(ResponseFormatManager responseFormatManager, - String description) { - if (!Strings.isNullOrEmpty(description) && description.length() > DESCRIPTION_MAX_LENGTH) { - LOGGER.error("Interface Operation description {} is invalid, maximum 200 characters allowed", description); - ResponseFormat errorResponse = responseFormatManager.getResponseFormat(ActionStatus - .INTERFACE_OPERATION_DESCRIPTION_MAX_LENGTH, description); - return Either.right(errorResponse); - } - return Either.left(Boolean.TRUE); - } - private boolean isValidOperationType(String operationType) { return Pattern.matches(TYPE_VALIDATION_REGEX, operationType); } private Either<Boolean, ResponseFormat> validateOperationTypeUnique( - Operation interfaceOperation, - org.openecomp.sdc.be.model.Component component, + Operation interfaceOperation, InterfaceDefinition interfaceDefinition, boolean isUpdate) { boolean isOperationTypeUnique = false; - Map<String, InterfaceDefinition> interfaceDefinitionMap = component.getInterfaces(); - if(interfaceDefinitionMap.isEmpty()){ - return Either.left(true); - } - - Collection<Operation> allOperations = interfaceDefinitionMap.values().stream() - .filter(a -> MapUtils.isNotEmpty(a.getOperationsMap())) - .map(a -> a.getOperationsMap().values()).flatMap(Collection::stream) - .collect(Collectors.toList()); - if(CollectionUtils.isEmpty(allOperations)){ + if(interfaceDefinition == null || CollectionUtils.isEmpty(interfaceDefinition.getOperationsMap().values())){ return Either.left(true); } Map<String, String> operationTypes = new HashMap<>(); - allOperations.forEach(operationType -> operationTypes.put(operationType.getUniqueId(), operationType.getName())); + interfaceDefinition.getOperationsMap().values().forEach(operationType -> operationTypes.put(operationType.getUniqueId(), operationType.getName())); if (!operationTypes.values().contains(interfaceOperation.getName())){ isOperationTypeUnique = true; @@ -206,6 +234,7 @@ public class InterfaceOperationValidation { return Either.left(isOperationTypeUnique); } + private Either<Boolean, ResponseFormat> validateInputParameters(Operation interfaceOperation, ResponseFormatManager responseFormatManager) { if (isInputParameterNameEmpty(interfaceOperation)) { @@ -284,19 +313,15 @@ public class InterfaceOperationValidation { } private Either<Boolean, ResponseFormat> validateInputPropertyExistInComponent(Operation operation, - List<InputDefinition> inputs, - ResponseFormatManager responseFormatManager) { - ListDataDefinition<OperationInputDefinition> inputDefinitionListDataDefinition = operation.getInputs(); - if (inputDefinitionListDataDefinition == null) { - return Either.left(Boolean.TRUE); - } - List<OperationInputDefinition> inputListToscaDataDefinition = inputDefinitionListDataDefinition.getListToscaDataDefinition(); + org.openecomp.sdc.be.model.Component component, + ResponseFormatManager responseFormatManager) { + List<OperationInputDefinition> inputListToscaDataDefinition = operation.getInputs().getListToscaDataDefinition(); for(OperationInputDefinition inputDefinition : inputListToscaDataDefinition ) { - if(!validateInputExistsInComponent(inputDefinition, inputs)) { - String missingPropertyName = inputDefinition.getInputId().contains(".") ? inputDefinition.getInputId().substring(inputDefinition.getInputId().indexOf(".") + 1) : inputDefinition.getInputId(); + if(!validateInputExistsInComponent(inputDefinition, component.getInputs())) { + String missingPropertyName = inputDefinition.getInputId().contains(".") ? inputDefinition.getInputId().substring(inputDefinition.getInputId().indexOf('.') + 1) : inputDefinition.getInputId(); LOGGER.error("Interface operation input property {} not found in component input properties", missingPropertyName); - ResponseFormat inputResponse = responseFormatManager.getResponseFormat(ActionStatus.INTERFACE_OPERATION_INPUT_PROPERTY_NOT_FOUND_IN_COMPONENT, missingPropertyName); + ResponseFormat inputResponse = responseFormatManager.getResponseFormat(ActionStatus.INTERFACE_OPERATION_INPUT_PROPERTY_NOT_FOUND_IN_COMPONENT, missingPropertyName, component.getComponentType().getValue()); return Either.right(inputResponse); } } @@ -304,14 +329,14 @@ public class InterfaceOperationValidation { } private boolean validateInputExistsInComponent(OperationInputDefinition input, - List<InputDefinition> inputs) { + List<InputDefinition> inputs) { return inputs.stream().anyMatch(inp -> inp.getUniqueId().equals(input.getInputId())) - || ((input.getInputId().contains(".") - && inputs.stream().anyMatch(inp -> inp.getUniqueId().equals( - input.getInputId().substring(0, input.getInputId().lastIndexOf(".")))))) ; + || (input.getInputId().contains(".") + && inputs.stream().anyMatch(inp -> inp.getUniqueId().equals( + input.getInputId().substring(0, input.getInputId().lastIndexOf('.'))))) ; } - private ResponseFormatManager getResponseFormatManager() { + protected ResponseFormatManager getResponseFormatManager() { return ResponseFormatManager.getInstance(); } |