From 9c0aa250313380895e9543e7e6fe83b9daf3849e Mon Sep 17 00:00:00 2001 From: ojasdubey Date: Mon, 28 Jan 2019 21:47:09 +0530 Subject: Operation Outputs - Operation screen BE 1. Added implementation for using operation outputs as mapping to input from create/update operation screen 2. Handled populating the mapped outputs to Tosca 3. Unit tests Change-Id: I3d4761b07be0d4e363ff84710e92438e694a3b07 Issue-ID: SDC-2085 Signed-off-by: ojasdubey --- .../components/utils/InterfaceOperationUtils.java | 17 + .../validation/InterfaceOperationValidation.java | 370 ++++++++++++++++----- .../tosca/utils/InterfacesOperationsToscaUtil.java | 49 ++- 3 files changed, 339 insertions(+), 97 deletions(-) (limited to 'catalog-be/src/main/java/org') 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 index 0233711a89..6615e9ef37 100644 --- 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 @@ -16,10 +16,15 @@ package org.openecomp.sdc.be.components.utils; +import java.util.List; import java.util.Map; import java.util.Optional; + +import org.apache.commons.collections.CollectionUtils; import org.apache.commons.collections.MapUtils; +import org.openecomp.sdc.be.datatypes.elements.OperationInputDefinition; import org.openecomp.sdc.be.model.Component; +import org.openecomp.sdc.be.model.InputDefinition; import org.openecomp.sdc.be.model.InterfaceDefinition; import org.openecomp.sdc.be.model.Operation; @@ -61,4 +66,16 @@ public class InterfaceOperationUtils { .filter(entry -> entry.getValue().getUniqueId().equals(operationId)).findAny(); } + public static boolean isOperationInputMappedToComponentProperty(OperationInputDefinition input, + List inputs) { + if (CollectionUtils.isEmpty(inputs)) { + return false; + } + 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('.'))))) ; + } + + } 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 c80f57486d..d17762fc90 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,7 +16,11 @@ package org.openecomp.sdc.be.components.validation; +import static org.openecomp.sdc.be.components.utils.InterfaceOperationUtils.isOperationInputMappedToComponentProperty; + +import com.google.common.collect.Sets; import fj.data.Either; +import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; import java.util.HashSet; @@ -26,13 +30,17 @@ import java.util.Objects; import java.util.Optional; import java.util.Set; import java.util.regex.Pattern; +import java.util.stream.Collectors; 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.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.OperationDataDefinition; import org.openecomp.sdc.be.datatypes.elements.OperationInputDefinition; -import org.openecomp.sdc.be.model.InputDefinition; +import org.openecomp.sdc.be.datatypes.elements.OperationOutputDefinition; import org.openecomp.sdc.be.model.InterfaceDefinition; import org.openecomp.sdc.be.model.Operation; import org.openecomp.sdc.exception.ResponseFormat; @@ -47,9 +55,10 @@ public class InterfaceOperationValidation { private static final Logger LOGGER = LoggerFactory.getLogger(InterfaceOperationValidation.class); - public Either validateInterfaceOperations(InterfaceDefinition inputInterfaceDefinition, - org.openecomp.sdc.be.model.Component component, InterfaceDefinition storedInterfaceDefinition, - Map globalInterfaceTypes, boolean isUpdate) { + public Either validateInterfaceOperations( + InterfaceDefinition inputInterfaceDefinition, org.openecomp.sdc.be.model.Component component, + InterfaceDefinition storedInterfaceDefinition, Map globalInterfaceTypes, + boolean isUpdate) { Either validateAllowedOperationCountOnLocalInterfaceType = validateAllowedOperationCountOnLocalInterfaceType(inputInterfaceDefinition, storedInterfaceDefinition, @@ -71,8 +80,8 @@ public class InterfaceOperationValidation { } for (Operation interfaceOperation : inputInterfaceDefinition.getOperationsMap().values()) { - Either interfaceOperationValidatorResponse = - validateInterfaceOperation(interfaceOperation, storedInterfaceDefinition, component, isUpdate); + Either interfaceOperationValidatorResponse = validateInterfaceOperation( + interfaceOperation, storedInterfaceDefinition, inputInterfaceDefinition, component, isUpdate); if (interfaceOperationValidatorResponse.isRight()) { return interfaceOperationValidatorResponse; } @@ -87,15 +96,13 @@ public class InterfaceOperationValidation { 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())))) { + .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())); + .getResponseFormat(ActionStatus.INTERFACE_OPERATION_INVALID_FOR_LOCAL_TYPE, + inputInterfaceDefinition.getType())); } return Either.left(Boolean.TRUE); @@ -105,22 +112,17 @@ public class InterfaceOperationValidation { InterfaceDefinition interfaceDefinition, Map 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()) - .anyMatch(opName -> - opName.equalsIgnoreCase( - operationName))); + 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()) + .anyMatch(opName -> opName.equalsIgnoreCase(operationName))); if (!isOperationValidOnGlobalInterfaceType) { return Either.right(getResponseFormatManager() - .getResponseFormat(ActionStatus.INTERFACE_OPERATION_INVALID_FOR_GLOBAL_TYPE, - interfaceDefinition.getType())); + .getResponseFormat(ActionStatus.INTERFACE_OPERATION_INVALID_FOR_GLOBAL_TYPE, + interfaceDefinition.getType())); } } return Either.left(Boolean.TRUE); @@ -140,16 +142,19 @@ public class InterfaceOperationValidation { } private Either validateInterfaceOperation(Operation interfaceOperation, - InterfaceDefinition interfaceDefinition, org.openecomp.sdc.be.model.Component component, boolean isUpdate) { + InterfaceDefinition storedInterfaceDefinition, InterfaceDefinition inputInterfaceDefinition, + org.openecomp.sdc.be.model.Component component, boolean isUpdate) { ResponseFormatManager responseFormatManager = getResponseFormatManager(); Either interfaceOperationTypeResponse = - isInterfaceOperationTypeValid(interfaceOperation, responseFormatManager, interfaceDefinition, isUpdate); + isInterfaceOperationTypeValid(interfaceOperation, responseFormatManager, storedInterfaceDefinition, + isUpdate); if (interfaceOperationTypeResponse.isRight()) { return Either.right(interfaceOperationTypeResponse.right().value()); } - if (null != interfaceOperation.getInputs()) { + if (null != interfaceOperation.getInputs() + && CollectionUtils.isNotEmpty(interfaceOperation.getInputs().getListToscaDataDefinition())) { Either inputParametersResponse = validateInputParameters(interfaceOperation, responseFormatManager); if (inputParametersResponse.isRight()) { @@ -157,14 +162,15 @@ public class InterfaceOperationValidation { } Either inputPropertyExistInComponent = - validateInputPropertyExistInComponent(interfaceOperation, component, responseFormatManager); + validateInputPropertyExistInComponent(interfaceOperation, + inputInterfaceDefinition, component, responseFormatManager); if (inputPropertyExistInComponent.isRight()) { return Either.right(inputPropertyExistInComponent.right().value()); - } } - if (null != interfaceOperation.getOutputs()) { + if (null != interfaceOperation.getOutputs() + && CollectionUtils.isNotEmpty(interfaceOperation.getOutputs().getListToscaDataDefinition())) { Either outputParametersResponse = validateOutputParameters(interfaceOperation, responseFormatManager); if (outputParametersResponse.isRight()) { @@ -172,15 +178,81 @@ public class InterfaceOperationValidation { } } + if (MapUtils.isNotEmpty(component.getInterfaces()) && isUpdate) { + Either mappedOutputDeletedResponse = + validateMappedOutputNotDeleted(interfaceOperation, component, inputInterfaceDefinition, + responseFormatManager); + if (mappedOutputDeletedResponse.isRight()) { + return Either.right(mappedOutputDeletedResponse.right().value()); + } + } + return Either.left(Boolean.TRUE); } + + private Either validateMappedOutputNotDeleted(Operation interfaceOperation, + org.openecomp.sdc.be.model.Component component, InterfaceDefinition interfaceDefinition, + ResponseFormatManager responseFormatManager) { + + List existingOperationOutputs = + getInterfaceOperationOutputs(interfaceOperation.getUniqueId(), component.getInterfaces()); + if (existingOperationOutputs.isEmpty()) { + return Either.left(Boolean.TRUE); + } + Set existingOperationOutputNames = existingOperationOutputs.stream() + .map(OperationOutputDefinition::getName) + .collect(Collectors.toSet()); + + ListDataDefinition currentOutputs = interfaceOperation.getOutputs(); + Set currentOperationOutputNames = new HashSet<>(); + if (currentOutputs != null && !currentOutputs.isEmpty()) { + currentOperationOutputNames = currentOutputs.getListToscaDataDefinition().stream() + .map(OperationOutputDefinition::getName) + .collect(Collectors.toSet()); + } + String mappedOutputPrefix = interfaceDefinition.getType() + "." + interfaceOperation.getName(); + Set deletedOutputs = Sets.difference(existingOperationOutputNames, currentOperationOutputNames); + Set deletedMappedOutputs = deletedOutputs.stream() + .filter(deletedOutputName -> isMappedOutputDeleted(mappedOutputPrefix, deletedOutputName, + component.getInterfaces())) + .map(this::getOperationOutputName) + .collect(Collectors.toSet()); + + if (CollectionUtils.isNotEmpty(deletedMappedOutputs)) { + return getMappedOutputErrorResponse(responseFormatManager, deletedMappedOutputs); + } + return Either.left(Boolean.TRUE); + } + + private boolean isMappedOutputDeleted(String mappedOutputPrefix, String outputName, + Map componentInterfaces) { + List interfaceOperationInputs = + getOtherOperationInputsOfComponent(mappedOutputPrefix, componentInterfaces); + return interfaceOperationInputs.stream() + .anyMatch(operationInputDefinition -> operationInputDefinition.getInputId() + .equals(mappedOutputPrefix + "." + outputName)); + } + + private Either getMappedOutputErrorResponse(ResponseFormatManager responseFormatManager, + Set deletedMappedOutputs) { + String deletedOutputNameList = String.join(",", deletedMappedOutputs); + LOGGER.error("Cannot update name or delete interface operation output(s) '{}' mapped to an operation input", + deletedOutputNameList); + ResponseFormat errorResponse = responseFormatManager.getResponseFormat(ActionStatus + .INTERFACE_OPERATION_MAPPED_OUTPUT_DELETED, deletedOutputNameList); + return Either.right(errorResponse); + } + + protected ResponseFormatManager getResponseFormatManager() { return ResponseFormatManager.getInstance(); } private Either isInterfaceOperationTypeValid(Operation interfaceOperation, - ResponseFormatManager responseFormatManager, InterfaceDefinition interfaceDefinition, boolean isUpdate) { + ResponseFormatManager responseFormatManager, + InterfaceDefinition interfaceDefinition, + boolean isUpdate) { Either operationTypeEmptyEither = isOperationTypeEmpty(responseFormatManager, interfaceOperation.getName()); @@ -194,8 +266,8 @@ public class InterfaceOperationValidation { return Either.right(operationTypeRegexValidationResponse.right().value()); } - Either operationTypeUniqueResponse = - validateOperationTypeUnique(interfaceOperation, interfaceDefinition, isUpdate); + Either operationTypeUniqueResponse = validateOperationTypeUnique(interfaceOperation, + interfaceDefinition, isUpdate); if (operationTypeUniqueResponse.isRight()) { return Either.right(operationTypeUniqueResponse.right().value()); } @@ -209,7 +281,7 @@ public class InterfaceOperationValidation { } private Either validateInputParameters(Operation interfaceOperation, - ResponseFormatManager responseFormatManager) { + ResponseFormatManager responseFormatManager) { if (isInputParameterNameEmpty(interfaceOperation)) { LOGGER.error("Interface operation input parameter name can't be empty"); ResponseFormat inputResponse = @@ -222,38 +294,16 @@ public class InterfaceOperationValidation { if (validateInputParametersUniqueResponse.isRight()) { LOGGER.error("Interface operation input parameter names {} already in use", validateInputParametersUniqueResponse.right().value()); - ResponseFormat inputResponse = responseFormatManager.getResponseFormat( - ActionStatus.INTERFACE_OPERATION_INPUT_NAME_ALREADY_IN_USE, - validateInputParametersUniqueResponse.right().value().toString()); + ResponseFormat inputResponse = + responseFormatManager.getResponseFormat(ActionStatus.INTERFACE_OPERATION_INPUT_NAME_ALREADY_IN_USE, + validateInputParametersUniqueResponse.right().value().toString()); return Either.right(inputResponse); } return Either.left(Boolean.TRUE); } - private Either validateInputPropertyExistInComponent(Operation operation, - org.openecomp.sdc.be.model.Component component, ResponseFormatManager responseFormatManager) { - - List inputListToscaDataDefinition = - operation.getInputs().getListToscaDataDefinition(); - for (OperationInputDefinition inputDefinition : inputListToscaDataDefinition) { - 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, - component.getComponentType().getValue()); - return Either.right(inputResponse); - } - } - return Either.left(Boolean.TRUE); - } - private Either validateOutputParameters(Operation interfaceOperation, - ResponseFormatManager responseFormatManager) { + ResponseFormatManager responseFormatManager) { if (isOutputParameterNameEmpty(interfaceOperation)) { LOGGER.error("Interface operation output parameter name can't be empty"); ResponseFormat inputResponse = @@ -266,9 +316,9 @@ public class InterfaceOperationValidation { if (validateOutputParametersUniqueResponse.isRight()) { LOGGER.error("Interface operation output parameter names {} already in use", validateOutputParametersUniqueResponse.right().value()); - ResponseFormat inputResponse = responseFormatManager.getResponseFormat( - ActionStatus.INTERFACE_OPERATION_OUTPUT_NAME_ALREADY_IN_USE, - validateOutputParametersUniqueResponse.right().value().toString()); + ResponseFormat inputResponse = + responseFormatManager.getResponseFormat(ActionStatus.INTERFACE_OPERATION_OUTPUT_NAME_ALREADY_IN_USE, + validateOutputParametersUniqueResponse.right().value().toString()); return Either.right(inputResponse); } return Either.left(Boolean.TRUE); @@ -330,31 +380,22 @@ public class InterfaceOperationValidation { inputParam -> inputParam.getName() == null || inputParam.getName().trim().equals(StringUtils.EMPTY)); } + private Either> isInputParametersUnique(Operation operationDataDefinition) { Set inputParamNamesSet = new HashSet<>(); Set duplicateParamNamesToReturn = new HashSet<>(); - operationDataDefinition.getInputs().getListToscaDataDefinition().forEach(inputParam -> { - if (!inputParamNamesSet.add(inputParam.getName().trim())) { - duplicateParamNamesToReturn.add(inputParam.getName().trim()); - } - }); + operationDataDefinition.getInputs().getListToscaDataDefinition() + .forEach(inputParam -> { + if (!inputParamNamesSet.add(inputParam.getName().trim())) { + duplicateParamNamesToReturn.add(inputParam.getName().trim()); + } + }); if (!duplicateParamNamesToReturn.isEmpty()) { return Either.right(duplicateParamNamesToReturn); } return Either.left(Boolean.TRUE); } - private boolean validateInputExistsInComponent(OperationInputDefinition input, List 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('.'))))); - } - private Boolean isOutputParameterNameEmpty(Operation operationDataDefinition) { return operationDataDefinition.getOutputs().getListToscaDataDefinition().stream().anyMatch( outputParam -> outputParam.getName() == null || outputParam.getName().trim().equals(StringUtils.EMPTY)); @@ -363,17 +404,170 @@ public class InterfaceOperationValidation { private Either> isOutputParametersUnique(Operation operationDataDefinition) { Set outputParamNamesSet = new HashSet<>(); Set duplicateParamNamesToReturn = new HashSet<>(); - operationDataDefinition.getOutputs().getListToscaDataDefinition().forEach(outputParam -> { - if (!outputParamNamesSet.add(outputParam.getName().trim())) { - duplicateParamNamesToReturn.add(outputParam.getName().trim()); - } - }); + operationDataDefinition.getOutputs().getListToscaDataDefinition() + .forEach(outputParam -> { + if (!outputParamNamesSet.add(outputParam.getName().trim())) { + duplicateParamNamesToReturn.add(outputParam.getName().trim()); + } + }); if (!duplicateParamNamesToReturn.isEmpty()) { return Either.right(duplicateParamNamesToReturn); } return Either.left(Boolean.TRUE); } + private Either validateInputPropertyExistInComponent(Operation operation, + InterfaceDefinition inputInterfaceDefinition, org.openecomp.sdc.be.model.Component component, + ResponseFormatManager responseFormatManager) { + + boolean isOperationInputToInputPropertyMappingValid = false; + boolean isOperationInputToOtherOperationOutputMappingValid = false; + String mappingName = ""; + List inputListToscaDataDefinition = + operation.getInputs().getListToscaDataDefinition(); + for (OperationInputDefinition inputDefinition : inputListToscaDataDefinition) { + if (isOperationInputMappedToComponentProperty(inputDefinition, component.getInputs())) { + isOperationInputToInputPropertyMappingValid = true; + } else { + mappingName = inputDefinition.getInputId().contains(".") + ? inputDefinition.getInputId().substring(inputDefinition.getInputId().lastIndexOf(".") + 1) + : inputDefinition.getInputId(); + break; + } + } + if (isOperationInputToInputPropertyMappingValid) { + return Either.left(Boolean.TRUE); + } + + //Mapped property not found in the component properties.. Check in other operation output parameters of + // component (other operation => not having the same full name) + ListDataDefinition outputListDataDefinition = + getOtherOperationOutputsOfComponent(inputInterfaceDefinition.getType(), operation.getName(), component); + + List componentOutputsFromOtherOperations = + outputListDataDefinition.getListToscaDataDefinition(); + if (validateOutputExistsInComponent(mappingName, componentOutputsFromOtherOperations)) { + isOperationInputToOtherOperationOutputMappingValid = true; + } else { + //Get the output parameter display name from the full name + mappingName = getOperationOutputName(mappingName); + } + + if (!isOperationInputToOtherOperationOutputMappingValid) { + LOGGER.error("Interface operation input parameter property {} not found in component input properties or" + + " outputs of other operations.", mappingName); + ResponseFormat inputResponse = responseFormatManager + .getResponseFormat(ActionStatus.INTERFACE_OPERATION_INPUT_PROPERTY_NOT_FOUND_IN_COMPONENT, + mappingName, component.getComponentType().getValue()); + return Either.right(inputResponse); + } + return Either.left(Boolean.TRUE); + } + + private boolean validateOutputExistsInComponent(String mappedOutputName, + List outputs) { + return outputs.stream() + .anyMatch(output -> output.getName().equals(mappedOutputName)); + } + + /** + * Get the list of outputs of other operations of all the interfaces in the component. + * @param currentInterfaceName Fully qualified interface name e.g. org.test.interfaces.node.lifecycle.Abc + * @param currentOperationName Name entered on the operation screen e.g. create + * @param component VF or service + */ + + private ListDataDefinition getOtherOperationOutputsOfComponent( + String currentInterfaceName, String currentOperationName, org.openecomp.sdc.be.model.Component component) { + ListDataDefinition componentOutputs = new ListDataDefinition<>(); + Map componentInterfaces = component.getInterfaces(); + if (MapUtils.isEmpty(componentInterfaces)) { + return componentOutputs; + } + for (Map.Entry interfaceDefinitionEntry : componentInterfaces.entrySet()) { + String interfaceName = interfaceDefinitionEntry.getKey(); + final Map operations = interfaceDefinitionEntry.getValue().getOperations(); + if (MapUtils.isEmpty(operations)) { + continue; + } + String actualOperationIdentifier = currentInterfaceName + "." + currentOperationName; + for (Map.Entry operationEntry : operations.entrySet()) { + ListDataDefinition outputs = operationEntry.getValue().getOutputs(); + String expectedOperationIdentifier = interfaceName + "." + operationEntry.getKey(); + if (!actualOperationIdentifier.equals(expectedOperationIdentifier) && !outputs.isEmpty()) { + outputs.getListToscaDataDefinition().forEach(componentOutputs::add); + } + } + } + return componentOutputs; + } + + /** + * Get the input definitions of other operations of the component from current as well as other interfaces. + * @param currentOperationIdentifier Identifier for the request operation (interface_name.operation_name) + * @param componentInterfaces Interfaces of the component + */ + private List getOtherOperationInputsOfComponent(String currentOperationIdentifier, + Map + componentInterfaces) { + List otherOperationInputs = new ArrayList<>(); + if (MapUtils.isEmpty(componentInterfaces)) { + return otherOperationInputs; + } + for (Map.Entry interfaceDefinitionEntry : componentInterfaces.entrySet()) { + final Map operations = interfaceDefinitionEntry.getValue().getOperations(); + if (MapUtils.isEmpty(operations)) { + continue; + } + for (Map.Entry operationEntry : operations.entrySet()) { + ListDataDefinition inputs = operationEntry.getValue().getInputs(); + String expectedOperationIdentifier = + interfaceDefinitionEntry.getValue().getType() + "." + operationEntry.getValue().getName(); + if (!currentOperationIdentifier.equals(expectedOperationIdentifier) && !inputs.isEmpty()) { + otherOperationInputs.addAll(inputs.getListToscaDataDefinition()); + } + } + } + return otherOperationInputs; + } + + private String getOperationOutputName(String outputName) { + return outputName.contains(".") + ? outputName.substring(outputName.lastIndexOf(".") + 1) + : outputName; + } + + /** + * Get the output of an operation in an interface. + * @param inputOperationId Unique identifier for the request operation + * @param componentInterfaces Interfaces of the component + */ + private List getInterfaceOperationOutputs(String inputOperationId, + Map + componentInterfaces) { + List operationOutputDefinitions = new ArrayList<>(); + if (MapUtils.isEmpty(componentInterfaces)) { + return operationOutputDefinitions; + } + for (Map.Entry interfaceDefinitionEntry : componentInterfaces.entrySet()) { + final Map operations = interfaceDefinitionEntry.getValue().getOperations(); + if (MapUtils.isEmpty(operations)) { + continue; + } + for (Map.Entry operationEntry : operations.entrySet()) { + String expectedOperationId = operationEntry.getValue().getUniqueId(); + if (expectedOperationId.equals(inputOperationId)) { + ListDataDefinition operationOutputs = + operationEntry.getValue().getOutputs(); + return (Objects.isNull(operationOutputs) || operationOutputs.isEmpty()) + ? operationOutputDefinitions + : operationOutputs.getListToscaDataDefinition(); + } + } + } + return operationOutputDefinitions; + } + private boolean isValidOperationType(String operationType) { return Pattern.matches(TYPE_VALIDATION_REGEX, operationType); } diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/utils/InterfacesOperationsToscaUtil.java b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/utils/InterfacesOperationsToscaUtil.java index fbef11d2e8..efed3e9f15 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/utils/InterfacesOperationsToscaUtil.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/utils/InterfacesOperationsToscaUtil.java @@ -16,6 +16,8 @@ package org.openecomp.sdc.be.tosca.utils; +import static org.openecomp.sdc.be.components.utils.InterfaceOperationUtils.isOperationInputMappedToComponentProperty; + import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.databind.ObjectMapper; @@ -47,10 +49,12 @@ public class InterfacesOperationsToscaUtil { private static final String DEFAULT = "default"; private static final String DEFAULT_HAS_UNDERSCORE = "_default"; private static final String DOT = "."; - private static final String SELF = "SELF"; - private static final String GET_PROPERTY = "get_property"; private static final String DEFAULTP = "defaultp"; + static final String SELF = "SELF"; + static final String GET_PROPERTY = "get_property"; + static final String GET_OPERATION_OUTPUT = "get_operation_output"; + private InterfacesOperationsToscaUtil() { } @@ -127,7 +131,7 @@ public class InterfacesOperationsToscaUtil { toscaOperation.setImplementation(operationArtifactPath); } toscaOperation.setDescription(operationEntry.getValue().getDescription()); - fillToscaOperationInputs(operationEntry.getValue(), toscaOperation); + fillToscaOperationInputs(operationEntry.getValue(), toscaOperation, interfaceType, component); toscaOperations.put(operationEntry.getValue().getName(), toscaOperation); } @@ -176,7 +180,9 @@ public class InterfacesOperationsToscaUtil { } private static void fillToscaOperationInputs(OperationDataDefinition operation, - ToscaLifecycleOperationDefinition toscaOperation) { + ToscaLifecycleOperationDefinition toscaOperation, + String interfaceType, + Component component) { if (Objects.isNull(operation.getInputs()) || operation.getInputs().isEmpty()) { toscaOperation.setInputs(null); return; @@ -186,12 +192,16 @@ public class InterfacesOperationsToscaUtil { for (OperationInputDefinition input : operation.getInputs().getListToscaDataDefinition()) { ToscaProperty toscaInput = new ToscaProperty(); toscaInput.setDescription(input.getDescription()); - String mappedPropertyName = null; + String mappedPropertyName; if (Objects.nonNull(input.getInputId())) { - mappedPropertyName = input.getInputId().substring(input.getInputId().indexOf(DOT) + 1); + if (isOperationInputMappedToComponentProperty(input, component.getInputs())) { + mappedPropertyName = input.getInputId().substring(input.getInputId().indexOf(DOT) + 1); + toscaInput.setDefaultp(createMappedInputPropertyDefaultValue(mappedPropertyName)); + } else { + mappedPropertyName = input.getInputId(); + toscaInput.setDefaultp(createMappedOutputDefaultValue(mappedPropertyName, interfaceType)); + } } - toscaInput.setDefaultp(createDefaultValue(mappedPropertyName)); - toscaInput.setType(input.getType()); toscaInput.setRequired(input.isRequired()); toscaInputs.put(input.getName(), toscaInput); @@ -200,7 +210,7 @@ public class InterfacesOperationsToscaUtil { toscaOperation.setInputs(toscaInputs); } - private static Map> createDefaultValue(String propertyName) { + private static Map> createMappedInputPropertyDefaultValue(String propertyName) { Map> getPropertyMap = new HashMap<>(); List values = new ArrayList<>(); values.add(SELF); @@ -213,6 +223,27 @@ public class InterfacesOperationsToscaUtil { return getPropertyMap; } + /** + * Create the value for operation input mapped to an operation output + * @param propertyName the mapped other operation output full name + * @param interfaceType full interface name + * @return input map for tosca + */ + private static Map> createMappedOutputDefaultValue(String propertyName, String interfaceType) { + Map> getOperationOutputMap = new HashMap<>(); + //For operation input mapped to other operation output parameter, the mapped property value + // should be of the format .. + List defaultMappedOperationOutputValue = new ArrayList<>(); + defaultMappedOperationOutputValue.add(SELF); + String fullOutputPropertyName = + propertyName.substring(propertyName.indexOf(interfaceType) + interfaceType.length() + 1); + defaultMappedOperationOutputValue.add(interfaceType); + //Output name should not contain dot + defaultMappedOperationOutputValue.addAll(Arrays.asList(fullOutputPropertyName.split("\\."))); + getOperationOutputMap.put(GET_OPERATION_OUTPUT, defaultMappedOperationOutputValue); + return getOperationOutputMap; + } + private static Map getObjectAsMap(Object obj) { ObjectMapper objectMapper = new ObjectMapper(); if (obj instanceof ToscaInterfaceDefinition) { -- cgit 1.2.3-korg