summaryrefslogtreecommitdiffstats
path: root/catalog-be/src/main/java/org/openecomp/sdc/be/components/validation/InterfaceOperationValidation.java
diff options
context:
space:
mode:
Diffstat (limited to 'catalog-be/src/main/java/org/openecomp/sdc/be/components/validation/InterfaceOperationValidation.java')
-rw-r--r--catalog-be/src/main/java/org/openecomp/sdc/be/components/validation/InterfaceOperationValidation.java171
1 files changed, 73 insertions, 98 deletions
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 bfb2429a83..7b650e4fad 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,6 @@
package org.openecomp.sdc.be.components.validation;
-import fj.data.Either;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
@@ -26,11 +25,16 @@ import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.regex.Pattern;
-import java.util.stream.Stream;
+import java.util.stream.Collectors;
+
+import fj.data.Either;
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;
@@ -44,117 +48,64 @@ 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(
- 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;
- }
+ Collection<Operation> interfaceOperations, org.openecomp.sdc.be.model.Component component, boolean isUpdate) {
- Either<Boolean, ResponseFormat> validateOperationNameUniqueness = validateOperationNameUniquenessInCollection(inputInterfaceDefinition.getOperationsMap().values());
- if(validateOperationNameUniqueness.isRight()){
- return validateOperationNameUniqueness;
- }
-
- for(Operation interfaceOperation : inputInterfaceDefinition.getOperationsMap().values()) {
+ for(Operation interfaceOperation : interfaceOperations) {
Either<Boolean, ResponseFormat> interfaceOperationValidatorResponse = validateInterfaceOperation(
- interfaceOperation, storedInterfaceDefinition, component, isUpdate);
+ interfaceOperation, 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();
+
Either<Boolean, ResponseFormat> interfaceOperationTypeResponse = isInterfaceOperationTypeValid(interfaceOperation,
- responseFormatManager, interfaceDefinition, isUpdate);
+ responseFormatManager, component, isUpdate);
if (interfaceOperationTypeResponse.isRight()) {
return Either.right(interfaceOperationTypeResponse.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> descriptionResponseEither = isValidDescription(responseFormatManager,
+ interfaceOperation.getDescription());
+ if (descriptionResponseEither.isRight()) {
+ return Either.right(descriptionResponseEither.right().value());
+ }
- Either<Boolean, ResponseFormat> inputPropertyExistInComponent = validateInputPropertyExistInComponent(interfaceOperation,
- component, responseFormatManager);
- if(inputPropertyExistInComponent.isRight()) {
- return Either.right(inputPropertyExistInComponent.right().value());
+ Either<Boolean, ResponseFormat> inputPropertyExistInComponent = validateInputPropertyExistInComponent(interfaceOperation,
+ component.getInputs(), responseFormatManager);
+ if(inputPropertyExistInComponent.isRight()) {
+ return Either.right(inputPropertyExistInComponent.right().value());
- }
}
- if(null != interfaceOperation.getOutputs()) {
- Either<Boolean, ResponseFormat> outputParametersResponse = validateOutputParameters(interfaceOperation, responseFormatManager);
- if (outputParametersResponse.isRight()) {
- return Either.right(outputParametersResponse.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());
}
return Either.left(Boolean.TRUE);
}
private Either<Boolean, ResponseFormat> isInterfaceOperationTypeValid(Operation interfaceOperation,
- ResponseFormatManager responseFormatManager, InterfaceDefinition interfaceDefinition,
- boolean isUpdate) {
+ ResponseFormatManager responseFormatManager,
+ org.openecomp.sdc.be.model.Component component, boolean isUpdate) {
Either<Boolean, ResponseFormat> operationTypeEmptyEither =
isOperationTypeEmpty(responseFormatManager, interfaceOperation.getName());
@@ -169,7 +120,7 @@ public class InterfaceOperationValidation {
}
Either<Boolean, ResponseFormat> operationTypeUniqueResponse = validateOperationTypeUnique(interfaceOperation,
- interfaceDefinition, isUpdate );
+ component, isUpdate );
if(operationTypeUniqueResponse.isRight()) {
return Either.right(operationTypeUniqueResponse.right().value());
}
@@ -205,21 +156,42 @@ 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, InterfaceDefinition interfaceDefinition,
+ Operation interfaceOperation,
+ org.openecomp.sdc.be.model.Component component,
boolean isUpdate) {
boolean isOperationTypeUnique = false;
- if(interfaceDefinition == null || CollectionUtils.isEmpty(interfaceDefinition.getOperationsMap().values())){
+ 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)){
return Either.left(true);
}
Map<String, String> operationTypes = new HashMap<>();
- interfaceDefinition.getOperationsMap().values().forEach(operationType -> operationTypes.put(operationType.getUniqueId(), operationType.getName()));
+ allOperations.forEach(operationType -> operationTypes.put(operationType.getUniqueId(), operationType.getName()));
if (!operationTypes.values().contains(interfaceOperation.getName())){
isOperationTypeUnique = true;
@@ -234,7 +206,6 @@ public class InterfaceOperationValidation {
return Either.left(isOperationTypeUnique);
}
-
private Either<Boolean, ResponseFormat> validateInputParameters(Operation interfaceOperation,
ResponseFormatManager responseFormatManager) {
if (isInputParameterNameEmpty(interfaceOperation)) {
@@ -313,15 +284,19 @@ public class InterfaceOperationValidation {
}
private Either<Boolean, ResponseFormat> validateInputPropertyExistInComponent(Operation operation,
- org.openecomp.sdc.be.model.Component component,
- ResponseFormatManager responseFormatManager) {
+ List<InputDefinition> inputs,
+ ResponseFormatManager responseFormatManager) {
+ ListDataDefinition<OperationInputDefinition> inputDefinitionListDataDefinition = operation.getInputs();
+ if (inputDefinitionListDataDefinition == null) {
+ return Either.left(Boolean.TRUE);
+ }
+ List<OperationInputDefinition> inputListToscaDataDefinition = inputDefinitionListDataDefinition.getListToscaDataDefinition();
- List<OperationInputDefinition> 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();
+ if(!validateInputExistsInComponent(inputDefinition, inputs)) {
+ 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());
+ ResponseFormat inputResponse = responseFormatManager.getResponseFormat(ActionStatus.INTERFACE_OPERATION_INPUT_PROPERTY_NOT_FOUND_IN_COMPONENT, missingPropertyName);
return Either.right(inputResponse);
}
}
@@ -329,14 +304,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(".")))))) ;
}
- protected ResponseFormatManager getResponseFormatManager() {
+ private ResponseFormatManager getResponseFormatManager() {
return ResponseFormatManager.getInstance();
}