diff options
Diffstat (limited to 'services/services-engine/src/main/java')
13 files changed, 264 insertions, 508 deletions
diff --git a/services/services-engine/src/main/java/org/onap/policy/apex/service/engine/event/impl/eventrequestor/EventRequestorCarrierTechnologyParameters.java b/services/services-engine/src/main/java/org/onap/policy/apex/service/engine/event/impl/eventrequestor/EventRequestorCarrierTechnologyParameters.java index 6f352599c..635597548 100644 --- a/services/services-engine/src/main/java/org/onap/policy/apex/service/engine/event/impl/eventrequestor/EventRequestorCarrierTechnologyParameters.java +++ b/services/services-engine/src/main/java/org/onap/policy/apex/service/engine/event/impl/eventrequestor/EventRequestorCarrierTechnologyParameters.java @@ -1,19 +1,20 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2016-2018 Ericsson. All rights reserved. + * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved. * ================================================================================ * 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. - * + * * SPDX-License-Identifier: Apache-2.0 * ============LICENSE_END========================================================= */ @@ -21,7 +22,6 @@ package org.onap.policy.apex.service.engine.event.impl.eventrequestor; import org.onap.policy.apex.service.parameters.carriertechnology.CarrierTechnologyParameters; -import org.onap.policy.common.parameters.GroupValidationResult; /** * This class holds the parameters that allows an output event to to be sent back into APEX as one @@ -60,14 +60,6 @@ public class EventRequestorCarrierTechnologyParameters extends CarrierTechnology * {@inheritDoc}. */ @Override - public GroupValidationResult validate() { - return new GroupValidationResult(this); - } - - /** - * {@inheritDoc}. - */ - @Override public String getName() { return this.getLabel(); } diff --git a/services/services-engine/src/main/java/org/onap/policy/apex/service/engine/event/impl/filecarrierplugin/FileCarrierTechnologyParameters.java b/services/services-engine/src/main/java/org/onap/policy/apex/service/engine/event/impl/filecarrierplugin/FileCarrierTechnologyParameters.java index 5071824ce..eceb0b4f9 100644 --- a/services/services-engine/src/main/java/org/onap/policy/apex/service/engine/event/impl/filecarrierplugin/FileCarrierTechnologyParameters.java +++ b/services/services-engine/src/main/java/org/onap/policy/apex/service/engine/event/impl/filecarrierplugin/FileCarrierTechnologyParameters.java @@ -1,19 +1,20 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2016-2018 Ericsson. All rights reserved. + * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved. * ================================================================================ * 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. - * + * * SPDX-License-Identifier: Apache-2.0 * ============LICENSE_END========================================================= */ @@ -21,12 +22,18 @@ package org.onap.policy.apex.service.engine.event.impl.filecarrierplugin; import java.io.File; +import lombok.Getter; +import lombok.Setter; import org.onap.policy.apex.service.engine.event.impl.filecarrierplugin.consumer.ApexFileEventConsumer; import org.onap.policy.apex.service.engine.event.impl.filecarrierplugin.producer.ApexFileEventProducer; import org.onap.policy.apex.service.parameters.carriertechnology.CarrierTechnologyParameters; -import org.onap.policy.common.parameters.GroupValidationResult; +import org.onap.policy.common.parameters.BeanValidationResult; +import org.onap.policy.common.parameters.ObjectValidationResult; +import org.onap.policy.common.parameters.ValidationResult; import org.onap.policy.common.parameters.ValidationStatus; +import org.onap.policy.common.parameters.annotations.Min; import org.onap.policy.common.utils.validation.ParameterValidationUtils; +import org.onap.policy.models.base.Validated; /** * This class holds the parameters that allows transport of events into and out of Apex using files and standard input @@ -42,6 +49,8 @@ import org.onap.policy.common.utils.validation.ParameterValidationUtils; * * @author Liam Fallon (liam.fallon@ericsson.com) */ +@Getter +@Setter public class FileCarrierTechnologyParameters extends CarrierTechnologyParameters { // @formatter:off /** The label of this carrier technology. */ @@ -60,7 +69,7 @@ public class FileCarrierTechnologyParameters extends CarrierTechnologyParameters private boolean standardIo = false; private boolean standardError = false; private boolean streamingMode = false; - private long startDelay = 0; + private @Min(0) long startDelay = 0; // @formatter:on /** @@ -77,96 +86,6 @@ public class FileCarrierTechnologyParameters extends CarrierTechnologyParameters } /** - * Gets the file name from which to read or to which to write events. - * - * @return the file name from which to read or to which to write events - */ - public String getFileName() { - return fileName; - } - - /** - * Checks if is standard IO should be used for input or output. - * - * @return true, if standard IO should be used for input or output - */ - public boolean isStandardIo() { - return standardIo; - } - - /** - * Checks if is standard error should be used for output. - * - * @return true, if standard error should be used for output - */ - public boolean isStandardError() { - return standardError; - } - - /** - * Checks if is streaming mode is on. - * - * @return true, if streaming mode is on - */ - public boolean isStreamingMode() { - return streamingMode; - } - - /** - * Sets the file name from which to read or to which to write events. - * - * @param fileName the file name from which to read or to which to write events - */ - public void setFileName(final String fileName) { - this.fileName = fileName; - } - - /** - * Sets if standard IO should be used for event input or output. - * - * @param standardIo if standard IO should be used for event input or output - */ - public void setStandardIo(final boolean standardIo) { - this.standardIo = standardIo; - } - - /** - * Sets if standard error should be used for event output. - * - * @param standardError if standard error should be used for event output - */ - public void setStandardError(final boolean standardError) { - this.standardError = standardError; - } - - /** - * Sets streaming mode. - * - * @param streamingMode the streaming mode value - */ - public void setStreamingMode(final boolean streamingMode) { - this.streamingMode = streamingMode; - } - - /** - * Gets the delay in milliseconds before the plugin starts processing. - * - * @return the delay - */ - public long getStartDelay() { - return startDelay; - } - - /** - * Sets the delay in milliseconds before the plugin starts processing. - * - * @param startDelay the delay - */ - public void setStartDelay(final long startDelay) { - this.startDelay = startDelay; - } - - /** * {@inheritDoc}. */ @Override @@ -188,36 +107,29 @@ public class FileCarrierTechnologyParameters extends CarrierTechnologyParameters * {@inheritDoc}. */ @Override - public GroupValidationResult validate() { - final GroupValidationResult result = super.validate(); + public BeanValidationResult validate() { + final BeanValidationResult result = super.validate(); if (!standardIo && !standardError) { - validateFileName(result); + result.addResult(validateFileName()); } if (standardIo || standardError) { streamingMode = true; } - if (startDelay < 0) { - result.setResult("startDelay", ValidationStatus.INVALID, - "startDelay must be zero or a positive number of milliseconds"); - } - return result; } - + /** * Validate the file name parameter. - * - * @param result the variable in which to store the result of the validation + * + * @return the result of the validation */ - private void validateFileName(final GroupValidationResult result) { + private ValidationResult validateFileName() { if (!ParameterValidationUtils.validateStringParameter(fileName)) { - result.setResult(FILE_NAME_TOKEN, ValidationStatus.INVALID, - "\"" + fileName + "\" invalid, must be specified as a non-empty string"); - return; + return new ObjectValidationResult(FILE_NAME_TOKEN, fileName, ValidationStatus.INVALID, Validated.IS_BLANK); } String absoluteFileName = null; @@ -233,53 +145,65 @@ public class FileCarrierTechnologyParameters extends CarrierTechnologyParameters // Check if the file exists, the file should be a regular file and should be readable if (theFile.exists()) { - validateExistingFile(result, absoluteFileName, theFile); + return validateExistingFile(absoluteFileName, theFile); } else { // The path to the file should exist and should be writable - validateNewFileParent(result, absoluteFileName, theFile); + return validateNewFileParent(absoluteFileName, theFile); } } /** * Validate an existing file is OK. - * - * @param result the result of the validation + * * @param absoluteFileName the absolute file name of the file * @param theFile the file that exists + * @return the result of the validation */ - private void validateExistingFile(final GroupValidationResult result, String absoluteFileName, File theFile) { + private ValidationResult validateExistingFile(String absoluteFileName, File theFile) { // Check that the file is a regular file if (!theFile.isFile()) { - result.setResult(FILE_NAME_TOKEN, ValidationStatus.INVALID, "is not a plain file"); + return new ObjectValidationResult(FILE_NAME_TOKEN, absoluteFileName, ValidationStatus.INVALID, + "is not a plain file"); + } else { fileName = absoluteFileName; if (!theFile.canRead()) { - result.setResult(FILE_NAME_TOKEN, ValidationStatus.INVALID, "is not readable"); + return new ObjectValidationResult(FILE_NAME_TOKEN, absoluteFileName, ValidationStatus.INVALID, + "is not readable"); } + + return null; } } /** * Validate the parent of a new file is OK. - * - * @param result the result of the validation + * * @param absoluteFileName the absolute file name of the file * @param theFile the file that exists + * @return the result of the validation */ - private void validateNewFileParent(final GroupValidationResult result, String absoluteFileName, File theFile) { + private ValidationResult validateNewFileParent(String absoluteFileName, File theFile) { // Check that the parent of the file is a directory if (!theFile.getParentFile().exists()) { - result.setResult(FILE_NAME_TOKEN, ValidationStatus.INVALID, "parent of file does not exist"); + return new ObjectValidationResult(FILE_NAME_TOKEN, absoluteFileName, ValidationStatus.INVALID, + "parent of file does not exist"); + } else if (!theFile.getParentFile().isDirectory()) { // Check that the parent of the file is a directory - result.setResult(FILE_NAME_TOKEN, ValidationStatus.INVALID, "parent of file is not directory"); + return new ObjectValidationResult(FILE_NAME_TOKEN, absoluteFileName, ValidationStatus.INVALID, + "parent of file is not directory"); + } else { fileName = absoluteFileName; if (!theFile.getParentFile().canRead()) { - result.setResult(FILE_NAME_TOKEN, ValidationStatus.INVALID, "is not readable"); + return new ObjectValidationResult(FILE_NAME_TOKEN, absoluteFileName, ValidationStatus.INVALID, + "is not readable"); } + + return null; } } } diff --git a/services/services-engine/src/main/java/org/onap/policy/apex/service/engine/main/ApexMain.java b/services/services-engine/src/main/java/org/onap/policy/apex/service/engine/main/ApexMain.java index 9757c57e8..80974af31 100644 --- a/services/services-engine/src/main/java/org/onap/policy/apex/service/engine/main/ApexMain.java +++ b/services/services-engine/src/main/java/org/onap/policy/apex/service/engine/main/ApexMain.java @@ -3,6 +3,7 @@ * Copyright (C) 2016-2018 Ericsson. All rights reserved. * Modifications Copyright (C) 2019-2021 Nordix Foundation. * Modifications Copyright (C) 2020-2021 Bell Canada. All rights reserved. + * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -106,14 +107,12 @@ public class ApexMain { // The arguments return a string if there is a message to print and we should exit final String argumentMessage = arguments.parse(args); if (argumentMessage != null) { - LOGGER.info(argumentMessage); throw new ApexException(argumentMessage); } // Validate that the arguments are sane arguments.validateInputFiles(); } catch (final ApexException | CommandLineException e) { - LOGGER.error("Arguments validation failed.", e); throw new ApexException("Arguments validation failed.", e); } @@ -122,7 +121,6 @@ public class ApexMain { try { axParameters = apexParameterHandler.getParameters(arguments); } catch (final Exception e) { - LOGGER.error("Cannot create APEX Parameters from the arguments provided.", e); throw new ApexException("Cannot create APEX Parameters from the arguments provided.", e); } diff --git a/services/services-engine/src/main/java/org/onap/policy/apex/service/engine/runtime/impl/EngineServiceImpl.java b/services/services-engine/src/main/java/org/onap/policy/apex/service/engine/runtime/impl/EngineServiceImpl.java index a558b9946..3deb234c6 100644 --- a/services/services-engine/src/main/java/org/onap/policy/apex/service/engine/runtime/impl/EngineServiceImpl.java +++ b/services/services-engine/src/main/java/org/onap/policy/apex/service/engine/runtime/impl/EngineServiceImpl.java @@ -2,6 +2,7 @@ * ============LICENSE_START======================================================= * Copyright (C) 2016-2018 Ericsson. All rights reserved. * Modifications Copyright (C) 2019-2020 Nordix Foundation. + * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -48,7 +49,7 @@ import org.onap.policy.apex.service.engine.runtime.ApexEventListener; import org.onap.policy.apex.service.engine.runtime.EngineService; import org.onap.policy.apex.service.engine.runtime.EngineServiceEventInterface; import org.onap.policy.apex.service.parameters.engineservice.EngineServiceParameters; -import org.onap.policy.common.parameters.GroupValidationResult; +import org.onap.policy.common.parameters.ValidationResult; import org.slf4j.ext.XLogger; import org.slf4j.ext.XLoggerFactory; @@ -138,7 +139,7 @@ public final class EngineServiceImpl implements EngineService, EngineServiceEven throw new ApexException("engine service configuration parameters are null"); } - final GroupValidationResult validation = config.validate(); + final ValidationResult validation = config.validate(); if (!validation.isValid()) { LOGGER.warn("Invalid engine service configuration parameters: {}" + validation.getResult()); throw new ApexException("Invalid engine service configuration parameters: " + validation); diff --git a/services/services-engine/src/main/java/org/onap/policy/apex/service/parameters/ApexParameterHandler.java b/services/services-engine/src/main/java/org/onap/policy/apex/service/parameters/ApexParameterHandler.java index 673d9cab5..0a317d600 100644 --- a/services/services-engine/src/main/java/org/onap/policy/apex/service/parameters/ApexParameterHandler.java +++ b/services/services-engine/src/main/java/org/onap/policy/apex/service/parameters/ApexParameterHandler.java @@ -3,6 +3,7 @@ * Copyright (C) 2016-2018 Ericsson. All rights reserved. * Modifications Copyright (C) 2019 Nordix Foundation. * Modifications Copyright (C) 2020-2021 Bell Canada. All rights reserved. + * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -38,9 +39,9 @@ import org.onap.policy.apex.service.parameters.carriertechnology.CarrierTechnolo import org.onap.policy.apex.service.parameters.engineservice.EngineServiceParametersJsonAdapter; import org.onap.policy.apex.service.parameters.eventprotocol.EventProtocolParameters; import org.onap.policy.apex.service.parameters.eventprotocol.EventProtocolParametersJsonAdapter; -import org.onap.policy.common.parameters.GroupValidationResult; import org.onap.policy.common.parameters.ParameterException; import org.onap.policy.common.parameters.ParameterService; +import org.onap.policy.common.parameters.ValidationResult; import org.onap.policy.common.utils.coder.StandardCoder; import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate; import org.slf4j.ext.XLogger; @@ -107,7 +108,7 @@ public class ApexParameterHandler { } // Validate the parameters - final GroupValidationResult validationResult = parameters.validate(); + final ValidationResult validationResult = parameters.validate(); if (!validationResult.isValid()) { String returnMessage = "validation error(s) on parameters from \"" + toscaPolicyFilePath + "\"\n"; returnMessage += validationResult.getResult(); diff --git a/services/services-engine/src/main/java/org/onap/policy/apex/service/parameters/ApexParameters.java b/services/services-engine/src/main/java/org/onap/policy/apex/service/parameters/ApexParameters.java index 692d5aee3..1302661b9 100644 --- a/services/services-engine/src/main/java/org/onap/policy/apex/service/parameters/ApexParameters.java +++ b/services/services-engine/src/main/java/org/onap/policy/apex/service/parameters/ApexParameters.java @@ -1,19 +1,20 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2016-2018 Ericsson. All rights reserved. + * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved. * ================================================================================ * 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. - * + * * SPDX-License-Identifier: Apache-2.0 * ============LICENSE_END========================================================= */ @@ -24,20 +25,30 @@ import java.util.Arrays; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedHashMap; +import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.Set; +import lombok.Getter; +import lombok.Setter; import org.onap.policy.apex.service.parameters.engineservice.EngineServiceParameters; import org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters; import org.onap.policy.apex.service.parameters.eventhandler.EventHandlerPeeredMode; -import org.onap.policy.common.parameters.GroupValidationResult; +import org.onap.policy.common.parameters.BeanValidationResult; +import org.onap.policy.common.parameters.BeanValidator; import org.onap.policy.common.parameters.ParameterGroup; +import org.onap.policy.common.parameters.ValidationResult; import org.onap.policy.common.parameters.ValidationStatus; +import org.onap.policy.common.parameters.annotations.NotBlank; +import org.onap.policy.common.parameters.annotations.NotNull; +import org.onap.policy.common.parameters.annotations.Size; +import org.onap.policy.common.parameters.annotations.Valid; import org.onap.policy.common.utils.validation.ParameterValidationUtils; +import org.onap.policy.models.base.Validated; /** * The main container parameter class for an Apex service. - * + * * <p>The following parameters are defined: <ol> <li>engineServiceParameters: The parameters for the Apex engine service * itself, such as the number of engine threads to run and the deployment port number to use. <li>eventOutputParameters: * A map of parameters for event outputs that Apex will use to emit events. Apex emits events on all outputs @@ -48,6 +59,8 @@ import org.onap.policy.common.utils.validation.ParameterValidationUtils; */ public class ApexParameters implements ParameterGroup { // Parameter group name + @Getter + @Setter private String name; // Constants for recurring strings @@ -58,16 +71,27 @@ public class ApexParameters implements ParameterGroup { private static final String FOR_PEERED_MODE_STRING = " for peered mode "; // Properties for the Java JVM + @Getter private String[][] javaProperties = null; // Parameters for the engine service and the engine threads in the engine service - private EngineServiceParameters engineServiceParameters; + @Getter + @Setter + private @NotNull @Valid EngineServiceParameters engineServiceParameters; // Parameters for the event outputs that Apex will use to send events on its outputs - private Map<String, EventHandlerParameters> eventOutputParameters = new LinkedHashMap<>(); + @Getter + @Setter + @Size(min = 1) + private Map<@NotNull @NotBlank String, @NotNull @Valid EventHandlerParameters> eventOutputParameters = + new LinkedHashMap<>(); // Parameters for the event inputs that Apex will use to receive events on its inputs - private Map<String, EventHandlerParameters> eventInputParameters = new LinkedHashMap<>(); + @Getter + @Setter + @Size(min = 1) + private Map<@NotNull @NotBlank String, @NotNull @Valid EventHandlerParameters> eventInputParameters = + new LinkedHashMap<>(); /** * Constructor to create an apex parameters instance and register the instance with the parameter service. @@ -80,70 +104,6 @@ public class ApexParameters implements ParameterGroup { } /** - * Gets the parameters for the Apex engine service. - * - * @return the engine service parameters - */ - public EngineServiceParameters getEngineServiceParameters() { - return engineServiceParameters; - } - - /** - * Sets the engine service parameters. - * - * @param engineServiceParameters the engine service parameters - */ - public void setEngineServiceParameters(final EngineServiceParameters engineServiceParameters) { - this.engineServiceParameters = engineServiceParameters; - } - - /** - * Gets the event output parameter map. - * - * @return the parameters for all event outputs - */ - public Map<String, EventHandlerParameters> getEventOutputParameters() { - return eventOutputParameters; - } - - /** - * Sets the event output parameters. - * - * @param eventOutputParameters the event outputs parameters - */ - public void setEventOutputParameters(final Map<String, EventHandlerParameters> eventOutputParameters) { - this.eventOutputParameters = eventOutputParameters; - } - - /** - * Gets the event input parameter map. - * - * @return the parameters for all event inputs - */ - public Map<String, EventHandlerParameters> getEventInputParameters() { - return eventInputParameters; - } - - /** - * Sets the event input parameters. - * - * @param eventInputParameters the event input parameters - */ - public void setEventInputParameters(final Map<String, EventHandlerParameters> eventInputParameters) { - this.eventInputParameters = eventInputParameters; - } - - @Override - public String getName() { - return name; - } - - @Override - public void setName(final String name) { - this.name = name; - } - - /** * Check if Java properties have been specified. * * @return true if Java properties have been specified @@ -152,38 +112,11 @@ public class ApexParameters implements ParameterGroup { return javaProperties != null && javaProperties.length > 0; } - /** - * Gets the Java properties that have been specified. - * - * @return the Java properties that have been specified - */ - public String[][] getJavaProperties() { - return javaProperties; - } - @Override - public GroupValidationResult validate() { - GroupValidationResult result = new GroupValidationResult(this); + public BeanValidationResult validate() { + BeanValidationResult result = new BeanValidator().validateTop(getClass().getSimpleName(), this); - validateJavaProperties(result); - - if (engineServiceParameters == null) { - result.setResult("engineServiceParameters", ValidationStatus.INVALID, - "engine service parameters are not specified"); - } else { - result.setResult("engineServiceParameters", engineServiceParameters.validate()); - } - - // Sanity check, we must have an entry in both output and input maps - if (eventInputParameters.isEmpty()) { - result.setResult(EVENT_INPUT_PARAMETERS_STRING, ValidationStatus.INVALID, - "at least one event input must be specified"); - } - - if (eventOutputParameters.isEmpty()) { - result.setResult(EVENT_OUTPUT_PARAMETERS_STRING, ValidationStatus.INVALID, - "at least one event output must be specified"); - } + result.addResult(validateJavaProperties()); // Validate that the values of all parameters are ok validateEventHandlerMap(EVENT_INPUT_PARAMETERS_STRING, result, eventInputParameters); @@ -201,56 +134,50 @@ public class ApexParameters implements ParameterGroup { /** * This method validates the java properties variable if it is present. - * - * @param result the result of the validation */ - private void validateJavaProperties(GroupValidationResult result) { + private ValidationResult validateJavaProperties() { if (javaProperties == null) { - return; + return null; } - StringBuilder errorMessageBuilder = new StringBuilder(); + BeanValidationResult result = new BeanValidationResult(JAVA_PROPERTIES, javaProperties); + int item = 0; for (String[] javaProperty : javaProperties) { + final String label = "entry " + (item++); + final List<String> value = (javaProperty == null ? null : Arrays.asList(javaProperty)); + BeanValidationResult result2 = new BeanValidationResult(label, value); + if (javaProperty == null) { - errorMessageBuilder.append("java properties array entry is null\n"); + // note: add to result, not result2 + result.addResult(label, null, ValidationStatus.INVALID, Validated.IS_NULL); + } else if (javaProperty.length != 2) { - errorMessageBuilder.append("java properties array entries must have one key and one value: " - + Arrays.deepToString(javaProperty) + "\n"); + // note: add to result, not result2 + result.addResult(label, value, ValidationStatus.INVALID, "must have one key and one value"); + } else if (!ParameterValidationUtils.validateStringParameter(javaProperty[0])) { - errorMessageBuilder - .append("java properties key is null or blank: " + Arrays.deepToString(javaProperty) + "\n"); + result2.addResult("key", javaProperty[0], ValidationStatus.INVALID, Validated.IS_BLANK); + } else if (!ParameterValidationUtils.validateStringParameter(javaProperty[1])) { - errorMessageBuilder - .append("java properties value is null or blank: " + Arrays.deepToString(javaProperty) + "\n"); + result2.addResult("value", javaProperty[1], ValidationStatus.INVALID, Validated.IS_BLANK); } - } - if (errorMessageBuilder.length() > 0) { - result.setResult(JAVA_PROPERTIES, ValidationStatus.INVALID, errorMessageBuilder.toString()); + result.addResult(result2); } + + return result; } /** * This method validates the parameters in an event handler map. - * + * * @param eventHandlerType the type of the event handler to use on error messages * @param result the result object to use to return validation messages * @param parsForValidation The event handler parameters to validate (input or output) */ - private void validateEventHandlerMap(final String eventHandlerType, final GroupValidationResult result, + private void validateEventHandlerMap(final String eventHandlerType, final BeanValidationResult result, final Map<String, EventHandlerParameters> parsForValidation) { for (final Entry<String, EventHandlerParameters> parameterEntry : parsForValidation.entrySet()) { - if (parameterEntry.getKey() == null || parameterEntry.getKey().trim().isEmpty()) { - result.setResult(eventHandlerType, parameterEntry.getKey(), ValidationStatus.INVALID, - "invalid " + eventHandlerType + " name \"" + parameterEntry.getKey() + "\""); - } else if (parameterEntry.getValue() == null) { - result.setResult(eventHandlerType, parameterEntry.getKey(), ValidationStatus.INVALID, - "invalid/Null event input prameters specified for " + eventHandlerType + " name \"" - + parameterEntry.getKey() + "\" "); - } else { - result.setResult(eventHandlerType, parameterEntry.getKey(), parameterEntry.getValue().validate()); - } - parameterEntry.getValue().setName(parameterEntry.getKey()); // Validate parameters for peered mode settings @@ -262,46 +189,46 @@ public class ApexParameters implements ParameterGroup { /** * Validate parameter values for event handlers in a peered mode. - * + * * @param eventHandlerType The event handler type we are checking * @param result The result object to which to append any error messages * @param parameterEntry The entry to check the peered mode on * @param peeredMode The mode to check */ - private void validatePeeredModeParameters(final String eventHandlerType, final GroupValidationResult result, + private void validatePeeredModeParameters(final String eventHandlerType, final BeanValidationResult result, final Entry<String, EventHandlerParameters> parameterEntry, final EventHandlerPeeredMode peeredMode) { final String messagePreamble = "specified peered mode \"" + peeredMode + "\""; final String peer = parameterEntry.getValue().getPeer(peeredMode); if (parameterEntry.getValue().isPeeredMode(peeredMode)) { if (peer == null || peer.trim().isEmpty()) { - result.setResult(eventHandlerType, parameterEntry.getKey(), ValidationStatus.INVALID, + result.addResult(eventHandlerType, parameterEntry.getKey(), ValidationStatus.INVALID, messagePreamble + " mandatory parameter not specified or is null"); } if (parameterEntry.getValue().getPeerTimeout(peeredMode) < 0) { - result.setResult(eventHandlerType, parameterEntry.getKey(), ValidationStatus.INVALID, + result.addResult(eventHandlerType, parameterEntry.getKey(), ValidationStatus.INVALID, messagePreamble + " timeout value \"" + parameterEntry.getValue().getPeerTimeout(peeredMode) + "\" is illegal, specify a non-negative timeout value in milliseconds"); } } else { if (peer != null) { - result.setResult(eventHandlerType, parameterEntry.getKey(), ValidationStatus.INVALID, messagePreamble - + " peer is illegal on " + eventHandlerType + " \"" + parameterEntry.getKey() + "\" "); + result.addResult(eventHandlerType, parameterEntry.getKey(), ValidationStatus.INVALID, messagePreamble + + " peer is illegal"); } if (parameterEntry.getValue().getPeerTimeout(peeredMode) != 0) { - result.setResult(eventHandlerType, parameterEntry.getKey(), ValidationStatus.INVALID, messagePreamble - + " timeout is illegal on " + eventHandlerType + " \"" + parameterEntry.getKey() + "\""); + result.addResult(eventHandlerType, parameterEntry.getKey(), ValidationStatus.INVALID, messagePreamble + + " timeout is illegal"); } } } /** * This method validates that the settings are valid for the given peered mode. - * + * * @param result The result object to which to append any error messages * @param peeredMode The peered mode to check */ - private void validatePeeredMode(final GroupValidationResult result, final EventHandlerPeeredMode peeredMode) { + private void validatePeeredMode(final BeanValidationResult result, final EventHandlerPeeredMode peeredMode) { // Find the input and output event handlers that use this peered mode final Map<String, EventHandlerParameters> inputParametersUsingMode = new HashMap<>(); final Map<String, EventHandlerParameters> outputParametersUsingMode = new HashMap<>(); @@ -327,13 +254,13 @@ public class ApexParameters implements ParameterGroup { /** * This method validates that the settings are valid for the event handlers on one. - * + * * @param handlerMapVariableName the variable name of the map on which the paired parameters are being checked * @param result The result object to which to append any error messages * @param leftModeParameters The mode parameters being checked * @param rightModeParameters The mode parameters being referenced by the checked parameters */ - private void validatePeeredModePeers(final String handlerMapVariableName, final GroupValidationResult result, + private void validatePeeredModePeers(final String handlerMapVariableName, final BeanValidationResult result, final EventHandlerPeeredMode peeredMode, final Map<String, EventHandlerParameters> leftModeParameterMap, final Map<String, EventHandlerParameters> rightModeParameterMap) { @@ -351,7 +278,7 @@ public class ApexParameters implements ParameterGroup { // Check that the peer reference is OK if (rightModeParameters == null) { - result.setResult(handlerMapVariableName, leftModeParameterEntry.getKey(), ValidationStatus.INVALID, + result.addResult(handlerMapVariableName, leftModeParameterEntry.getKey(), ValidationStatus.INVALID, PEER_STRING + '"' + leftModeParameters.getPeer(peeredMode) + FOR_PEERED_MODE_STRING + peeredMode + " does not exist or is not defined with the same peered mode"); continue; @@ -360,20 +287,20 @@ public class ApexParameters implements ParameterGroup { // Now check that the right side peer is the left side event handler final String rightSidePeer = rightModeParameters.getPeer(peeredMode); if (!rightSidePeer.equals(leftModeParameterEntry.getKey())) { - result.setResult(handlerMapVariableName, leftModeParameterEntry.getKey(), ValidationStatus.INVALID, - PEER_STRING + '"' + leftModeParameters.getPeer(peeredMode) + FOR_PEERED_MODE_STRING + peeredMode - + ", value \"" + rightSidePeer + "\" on peer \"" + leftSidePeer - + "\" does not equal event handler \"" + leftModeParameterEntry.getKey() + "\""); + result.addResult(handlerMapVariableName, leftModeParameterEntry.getKey(), ValidationStatus.INVALID, + PEER_STRING + '"' + leftModeParameters.getPeer(peeredMode) + '"' + + FOR_PEERED_MODE_STRING + peeredMode + ", value \"" + rightSidePeer + + "\" on peer does not equal event handler"); } else { // Check for duplicates if (!leftCheckDuplicateSet.add(leftSidePeer)) { - result.setResult(handlerMapVariableName, leftModeParameterEntry.getKey(), ValidationStatus.INVALID, + result.addResult(handlerMapVariableName, leftModeParameterEntry.getKey(), ValidationStatus.INVALID, PEER_STRING + '"' + leftModeParameters.getPeer(peeredMode) + FOR_PEERED_MODE_STRING + peeredMode + ", peer value \"" + leftSidePeer + "\" on event handler \"" + leftModeParameterEntry.getKey() + "\" is used more than once"); } if (!rightCheckDuplicateSet.add(rightSidePeer)) { - result.setResult(handlerMapVariableName, leftModeParameterEntry.getKey(), ValidationStatus.INVALID, + result.addResult(handlerMapVariableName, leftModeParameterEntry.getKey(), ValidationStatus.INVALID, PEER_STRING + '"' + leftModeParameters.getPeer(peeredMode) + FOR_PEERED_MODE_STRING + peeredMode + ", peer value \"" + rightSidePeer + "\" on peer \"" + leftSidePeer + "\" on event handler \"" + leftModeParameterEntry.getKey() + "\" is used more than once"); @@ -381,12 +308,12 @@ public class ApexParameters implements ParameterGroup { } if (!crossCheckPeeredTimeoutValues(leftModeParameters, rightModeParameters, peeredMode)) { - result.setResult(handlerMapVariableName, leftModeParameterEntry.getKey(), ValidationStatus.INVALID, - PEER_STRING + '"' + leftModeParameters.getPeer(peeredMode) + FOR_PEERED_MODE_STRING + peeredMode - + " timeout " + leftModeParameters.getPeerTimeout(peeredMode) + " on event handler \"" - + leftModeParameters.getName() + "\" does not equal timeout " - + rightModeParameters.getPeerTimeout(peeredMode) + " on event handler \"" - + rightModeParameters.getName() + "\""); + result.addResult(handlerMapVariableName, leftModeParameterEntry.getKey(), ValidationStatus.INVALID, + PEER_STRING + '"' + leftModeParameters.getPeer(peeredMode) + '"' + + FOR_PEERED_MODE_STRING + peeredMode + " timeout " + + leftModeParameters.getPeerTimeout(peeredMode) + + " does not equal peer timeout " + + rightModeParameters.getPeerTimeout(peeredMode)); } } @@ -394,7 +321,7 @@ public class ApexParameters implements ParameterGroup { /** * Validate the timeout values on two peers. - * + * * @param leftModeParameters The parameters of the left hand peer * @param peeredMode The peered mode being checked * @return true if the timeout values are cross checked as being OK diff --git a/services/services-engine/src/main/java/org/onap/policy/apex/service/parameters/carriertechnology/CarrierTechnologyParameters.java b/services/services-engine/src/main/java/org/onap/policy/apex/service/parameters/carriertechnology/CarrierTechnologyParameters.java index e688aad17..503d4c5d7 100644 --- a/services/services-engine/src/main/java/org/onap/policy/apex/service/parameters/carriertechnology/CarrierTechnologyParameters.java +++ b/services/services-engine/src/main/java/org/onap/policy/apex/service/parameters/carriertechnology/CarrierTechnologyParameters.java @@ -21,10 +21,11 @@ package org.onap.policy.apex.service.parameters.carriertechnology; -import org.onap.policy.common.parameters.GroupValidationResult; -import org.onap.policy.common.parameters.ParameterGroup; +import org.onap.policy.common.parameters.ParameterGroupImpl; import org.onap.policy.common.parameters.ParameterRuntimeException; -import org.onap.policy.common.parameters.ValidationStatus; +import org.onap.policy.common.parameters.annotations.ClassName; +import org.onap.policy.common.parameters.annotations.NotBlank; +import org.onap.policy.common.parameters.annotations.NotNull; /** * The default carrier technology parameter class that may be specialized by carrier technology plugins that require @@ -37,15 +38,17 @@ import org.onap.policy.common.parameters.ValidationStatus; * * @author Liam Fallon (liam.fallon@ericsson.com) */ -public abstract class CarrierTechnologyParameters implements ParameterGroup { +@NotNull +@NotBlank +public abstract class CarrierTechnologyParameters extends ParameterGroupImpl { // The carrier technology label private String label = null; // Producer and Consumer plugin classes for the event producer and consumer for this carrier // technology - private String eventProducerPluginClass = null; - private String eventConsumerPluginClass = null; + private @ClassName String eventProducerPluginClass = null; + private @ClassName String eventConsumerPluginClass = null; /** * Constructor to create a carrier technology parameters instance with the name of a sub class of this class and @@ -130,30 +133,6 @@ public abstract class CarrierTechnologyParameters implements ParameterGroup { + ", eventConsumerPluginClass=" + eventConsumerPluginClass + "]"; } - /** - * {@inheritDoc}. - */ - @Override - public GroupValidationResult validate() { - final GroupValidationResult result = new GroupValidationResult(this); - - if (label == null || label.length() == 0) { - result.setResult("label", ValidationStatus.INVALID, "carrier technology label not specified or is blank"); - } - - if (eventProducerPluginClass == null || eventProducerPluginClass.length() == 0) { - result.setResult("eventProducerPluginClass", ValidationStatus.INVALID, - "carrier technology eventProducerPluginClass not specified or is blank"); - } - - if (eventConsumerPluginClass == null || eventConsumerPluginClass.length() == 0) { - result.setResult("eventConsumerPluginClass", ValidationStatus.INVALID, - "carrier technology eventConsumerPluginClass not specified or is blank"); - } - - return result; - } - @Override public String getName() { return this.getLabel(); diff --git a/services/services-engine/src/main/java/org/onap/policy/apex/service/parameters/carriertechnology/RestPluginCarrierTechnologyParameters.java b/services/services-engine/src/main/java/org/onap/policy/apex/service/parameters/carriertechnology/RestPluginCarrierTechnologyParameters.java index eab936edc..4e086e809 100644 --- a/services/services-engine/src/main/java/org/onap/policy/apex/service/parameters/carriertechnology/RestPluginCarrierTechnologyParameters.java +++ b/services/services-engine/src/main/java/org/onap/policy/apex/service/parameters/carriertechnology/RestPluginCarrierTechnologyParameters.java @@ -2,6 +2,7 @@ * ============LICENSE_START======================================================= * Copyright (C) 2016-2018 Ericsson. All rights reserved. * Modifications Copyright (C) 2019-2020 Nordix Foundation. + * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -23,6 +24,7 @@ package org.onap.policy.apex.service.parameters.carriertechnology; import java.util.Arrays; import java.util.HashSet; +import java.util.List; import java.util.Set; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -32,9 +34,12 @@ import javax.ws.rs.core.MultivaluedMap; import lombok.Getter; import lombok.Setter; import org.apache.commons.lang3.StringUtils; -import org.onap.policy.common.parameters.GroupValidationResult; +import org.onap.policy.common.parameters.BeanValidationResult; +import org.onap.policy.common.parameters.ObjectValidationResult; +import org.onap.policy.common.parameters.ValidationResult; import org.onap.policy.common.parameters.ValidationStatus; import org.onap.policy.common.utils.validation.ParameterValidationUtils; +import org.onap.policy.models.base.Validated; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -155,42 +160,42 @@ public class RestPluginCarrierTechnologyParameters extends CarrierTechnologyPara * {@inheritDoc}. */ @Override - public GroupValidationResult validate() { - GroupValidationResult result = super.validate(); + public BeanValidationResult validate() { + BeanValidationResult result = super.validate(); - validateUrl(result); - validateHttpHeaders(result); + result.addResult(validateUrl()); + result.addResult(validateHttpHeaders()); + result.addResult(validateHttpCodeFilter()); - return validateHttpCodeFilter(result); + return result; } // @formatter:off /** * Validate the URL. * - * <p>Checks: - * http://www.blah.com/{par1/somethingelse (Missing end tag) use {[^\\{}]*$ - * http://www.blah.com/{par1/{some}thingelse (Nested tag) use {[^}]*{ - * http://www.blah.com/{par1}/some}thingelse (Missing start tag1) use }[^{}]*.} - * http://www.blah.com/par1}/somethingelse (Missing start tag2) use }[^{}]*} - * http://www.blah.com/{}/somethingelse (Empty tag) use {[\s]*} - * @param result the result of the validation + * <p/>Checks: + * <br/>http://www.blah.com/{par1/somethingelse (Missing end tag) use {[^\\{}]*$ + * <br/>http://www.blah.com/{par1/{some}thingelse (Nested tag) use {[^}]*{ + * <br/>http://www.blah.com/{par1}/some}thingelse (Missing start tag1) use }[^{}]*.} + * <br/>http://www.blah.com/par1}/somethingelse (Missing start tag2) use }[^{}]*} + * <br/>http://www.blah.com/{}/somethingelse (Empty tag) use {[\s]*} */ // @formatter:on - public GroupValidationResult validateUrl(final GroupValidationResult result) { + public ValidationResult validateUrl() { // The URL may be optional so existence must be checked in the plugin code - if (getUrl() == null) { - return result; + String url2 = getUrl(); + if (url2 == null) { + return null; } - Matcher matcher = patternErrorKey.matcher(getUrl()); + Matcher matcher = patternErrorKey.matcher(url2); if (matcher.find()) { - final String urlInvalidMessage = "invalid URL " + getUrl() + " has been set for event sending on " - + getLabel(); - result.setResult("url", ValidationStatus.INVALID, urlInvalidMessage); + final String urlInvalidMessage = "invalid URL has been set for event sending on " + getLabel(); + return new ObjectValidationResult("url", url2, ValidationStatus.INVALID, urlInvalidMessage); } - return result; + return null; } /** @@ -198,24 +203,35 @@ public class RestPluginCarrierTechnologyParameters extends CarrierTechnologyPara * * @param result the result of the validation */ - private GroupValidationResult validateHttpHeaders(final GroupValidationResult result) { + private ValidationResult validateHttpHeaders() { if (httpHeaders == null) { - return result; + return null; } + BeanValidationResult result = new BeanValidationResult(HTTP_HEADERS, httpHeaders); + + int item = 0; for (String[] httpHeader : httpHeaders) { + final String label = "entry " + (item++); + final List<String> value = (httpHeader == null ? null : Arrays.asList(httpHeader)); + BeanValidationResult result2 = new BeanValidationResult(label, value); + if (httpHeader == null) { - result.setResult(HTTP_HEADERS, ValidationStatus.INVALID, "HTTP header array entry is null"); + // note: add to result, not result2 + result.addResult(label, null, ValidationStatus.INVALID, Validated.IS_NULL); + } else if (httpHeader.length != 2) { - result.setResult(HTTP_HEADERS, ValidationStatus.INVALID, - "HTTP header array entries must have one key and one value: " + Arrays.deepToString(httpHeader)); + // note: add to result, not result2 + result.addResult(label, value, ValidationStatus.INVALID, "must have one key and one value"); + } else if (!ParameterValidationUtils.validateStringParameter(httpHeader[0])) { - result.setResult(HTTP_HEADERS, ValidationStatus.INVALID, - "HTTP header key is null or blank: " + Arrays.deepToString(httpHeader)); + result2.addResult("key", httpHeader[0], ValidationStatus.INVALID, Validated.IS_BLANK); + } else if (!ParameterValidationUtils.validateStringParameter(httpHeader[1])) { - result.setResult(HTTP_HEADERS, ValidationStatus.INVALID, - "HTTP header value is null or blank: " + Arrays.deepToString(httpHeader)); + result2.addResult("value", httpHeader[1], ValidationStatus.INVALID, Validated.IS_BLANK); } + + result.addResult(result2); } return result; @@ -223,28 +239,27 @@ public class RestPluginCarrierTechnologyParameters extends CarrierTechnologyPara /** * Validate the HTTP code filter. - * - * @param result the result of the validation */ - public GroupValidationResult validateHttpCodeFilter(final GroupValidationResult result) { + public ValidationResult validateHttpCodeFilter() { if (httpCodeFilter == null) { httpCodeFilter = DEFAULT_HTTP_CODE_FILTER; } else if (StringUtils.isBlank(httpCodeFilter)) { - result.setResult(HTTP_CODE_FILTER, ValidationStatus.INVALID, - "HTTP code filter must be specified as a three digit regular expression"); + return new ObjectValidationResult(HTTP_CODE_FILTER, httpCodeFilter, ValidationStatus.INVALID, + "must be a three digit regular expression"); } else { try { Pattern.compile(httpCodeFilter); } catch (PatternSyntaxException pse) { + LOGGER.debug("Invalid HTTP code filter", pse); String message = "Invalid HTTP code filter, the filter must be specified as a three digit " + "regular expression: " + pse.getMessage(); - result.setResult(HTTP_CODE_FILTER, ValidationStatus.INVALID, message); - LOGGER.debug(message, pse); + return new ObjectValidationResult(HTTP_CODE_FILTER, httpCodeFilter, ValidationStatus.INVALID, + message); } } - return result; + return null; } /** diff --git a/services/services-engine/src/main/java/org/onap/policy/apex/service/parameters/engineservice/EngineServiceParameters.java b/services/services-engine/src/main/java/org/onap/policy/apex/service/parameters/engineservice/EngineServiceParameters.java index afede627e..d33ae1697 100644 --- a/services/services-engine/src/main/java/org/onap/policy/apex/service/parameters/engineservice/EngineServiceParameters.java +++ b/services/services-engine/src/main/java/org/onap/policy/apex/service/parameters/engineservice/EngineServiceParameters.java @@ -2,6 +2,7 @@ * ============LICENSE_START======================================================= * Copyright (C) 2016-2018 Ericsson. All rights reserved. * Modifications Copyright (C) 2020 Bell Canada. All rights reserved. + * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -21,16 +22,21 @@ package org.onap.policy.apex.service.parameters.engineservice; +import javax.validation.Valid; import lombok.Getter; import lombok.Setter; -import org.apache.commons.lang3.StringUtils; import org.onap.policy.apex.core.engine.EngineParameters; import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; import org.onap.policy.apex.model.basicmodel.concepts.AxKey; import org.onap.policy.apex.service.parameters.ApexParameterConstants; -import org.onap.policy.common.parameters.GroupValidationResult; +import org.onap.policy.common.parameters.BeanValidationResult; +import org.onap.policy.common.parameters.BeanValidator; import org.onap.policy.common.parameters.ParameterGroup; -import org.onap.policy.common.parameters.ValidationStatus; +import org.onap.policy.common.parameters.annotations.Max; +import org.onap.policy.common.parameters.annotations.Min; +import org.onap.policy.common.parameters.annotations.NotBlank; +import org.onap.policy.common.parameters.annotations.NotNull; +import org.onap.policy.common.parameters.annotations.Pattern; // @formatter:off /** @@ -58,6 +64,7 @@ import org.onap.policy.common.parameters.ValidationStatus; // @formatter:on @Getter @Setter +@NotNull public class EngineServiceParameters implements ParameterGroup { private static final int MAX_PORT = 65535; @@ -80,17 +87,25 @@ public class EngineServiceParameters implements ParameterGroup { // Constants for repeated strings // Apex engine service parameters + @Pattern(regexp = AxKey.NAME_REGEXP) private String name = DEFAULT_NAME; + @Pattern(regexp = AxKey.VERSION_REGEXP) private String version = DEFAULT_VERSION; + @Min(0) private int id = DEFAULT_ID; + @Min(1) private int instanceCount = DEFAULT_INSTANCE_COUNT; + @Min(1) + @Max(MAX_PORT) private int deploymentPort = DEFAULT_DEPLOYMENT_PORT; + @NotBlank private String policyModel = null; + @Min(0) private long periodicEventPeriod = 0; // @formatter:on // Apex engine internal parameters - private EngineParameters engineParameters = new EngineParameters(); + private @Valid EngineParameters engineParameters = new EngineParameters(); /** * Constructor to create an apex engine service parameters instance and register the instance with the parameter @@ -126,63 +141,8 @@ public class EngineServiceParameters implements ParameterGroup { * {@inheritDoc}. */ @Override - public GroupValidationResult validate() { - final GroupValidationResult result = new GroupValidationResult(this); - - validateStringParameters(result); - - validateNumericParameters(result); - - if (StringUtils.isBlank(policyModel)) { - result.setResult("policyModel", ValidationStatus.INVALID, "must be specified"); - } - result.setResult("engineParameters", engineParameters.validate()); - - return result; - } - - /** - * Validate string parameters. - * - * @param result the result of string parameter validation - */ - private void validateStringParameters(final GroupValidationResult result) { - if (name == null || !name.matches(AxKey.NAME_REGEXP)) { - result.setResult("name", ValidationStatus.INVALID, - "name is invalid, it must match regular expression" + AxKey.NAME_REGEXP); - } - - if (version == null || !version.matches(AxKey.VERSION_REGEXP)) { - result.setResult("version", ValidationStatus.INVALID, - "version is invalid, it must match regular expression" + AxKey.VERSION_REGEXP); - } - } - - /** - * Validate numeric parameters. - * - * @param result the result of numeric parameter validation - */ - private void validateNumericParameters(final GroupValidationResult result) { - if (id < 0) { - result.setResult("id", ValidationStatus.INVALID, - "id not specified or specified value [" + id + "] invalid, must be specified as id >= 0"); - } - - if (instanceCount < 1) { - result.setResult("instanceCount", ValidationStatus.INVALID, - "instanceCount [" + instanceCount + "] invalid, must be specified as instanceCount >= 1"); - } - - if (deploymentPort < 1 || deploymentPort > MAX_PORT) { - result.setResult("deploymentPort", ValidationStatus.INVALID, "deploymentPort [" + deploymentPort - + "] invalid, must be specified as 1024 <= port <= 65535"); - } - - if (periodicEventPeriod < 0) { - result.setResult("periodicEventPeriod", ValidationStatus.INVALID, "periodicEventPeriod [" - + periodicEventPeriod + "] invalid, must be specified in milliseconds as >=0"); - } + public BeanValidationResult validate() { + return new BeanValidator().validateTop(getClass().getSimpleName(), this); } } diff --git a/services/services-engine/src/main/java/org/onap/policy/apex/service/parameters/eventhandler/EventHandlerParameters.java b/services/services-engine/src/main/java/org/onap/policy/apex/service/parameters/eventhandler/EventHandlerParameters.java index 72eb58b0f..7fb4584fe 100644 --- a/services/services-engine/src/main/java/org/onap/policy/apex/service/parameters/eventhandler/EventHandlerParameters.java +++ b/services/services-engine/src/main/java/org/onap/policy/apex/service/parameters/eventhandler/EventHandlerParameters.java @@ -1,19 +1,20 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2016-2018 Ericsson. All rights reserved. + * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved. * ================================================================================ * 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. - * + * * SPDX-License-Identifier: Apache-2.0 * ============LICENSE_END========================================================= */ @@ -25,18 +26,21 @@ import java.util.regex.PatternSyntaxException; import org.onap.policy.apex.service.parameters.ApexParameterConstants; import org.onap.policy.apex.service.parameters.carriertechnology.CarrierTechnologyParameters; import org.onap.policy.apex.service.parameters.eventprotocol.EventProtocolParameters; -import org.onap.policy.common.parameters.GroupValidationResult; +import org.onap.policy.common.parameters.BeanValidationResult; +import org.onap.policy.common.parameters.BeanValidator; import org.onap.policy.common.parameters.ParameterGroup; import org.onap.policy.common.parameters.ValidationStatus; +import org.onap.policy.common.parameters.annotations.NotNull; +import org.onap.policy.common.parameters.annotations.Valid; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * The parameters for a single event producer, event consumer or synchronous event handler. - * + * * <p>Event producers, consumers, and synchronous event handlers all use a carrier technology and an event protocol so * the actual parameters for each one are the same. Therefore, we use the same class for the parameters of each one. - * + * * <p>The following parameters are defined: <ol> <li>carrierTechnologyParameters: The carrier technology is the type of * messaging infrastructure used to carry events. Examples are File, Kafka or REST. <li>eventProtocolParameters: The * format that the events are in when being carried. Examples are JSON, XML, or Java Beans. carrier technology @@ -56,8 +60,8 @@ public class EventHandlerParameters implements ParameterGroup { private static final Logger LOGGER = LoggerFactory.getLogger(EventHandlerParameters.class); private String name = null; - private CarrierTechnologyParameters carrierTechnologyParameters = null; - private EventProtocolParameters eventProtocolParameters = null; + private @NotNull @Valid CarrierTechnologyParameters carrierTechnologyParameters = null; + private @NotNull @Valid EventProtocolParameters eventProtocolParameters = null; private boolean synchronousMode = false; private String synchronousPeer = null; private long synchronousTimeout = 0; @@ -97,7 +101,7 @@ public class EventHandlerParameters implements ParameterGroup { /** * Checks if the name of the event handler is set. - * + * * @return true if the name is set */ public boolean checkSetName() { @@ -214,7 +218,7 @@ public class EventHandlerParameters implements ParameterGroup { /** * Get the timeout value for the event handler in peered mode. - * + * * @param peeredMode the peered mode to get the timeout for * @return the timeout value */ @@ -231,7 +235,7 @@ public class EventHandlerParameters implements ParameterGroup { /** * Set the timeout value for the event handler in peered mode. - * + * * @param peeredMode the peered mode to set the timeout for * @param timeout the timeout value */ @@ -306,22 +310,8 @@ public class EventHandlerParameters implements ParameterGroup { * {@inheritDoc}. */ @Override - public GroupValidationResult validate() { - final GroupValidationResult result = new GroupValidationResult(this); - - if (eventProtocolParameters == null) { - result.setResult("eventProtocolParameters", ValidationStatus.INVALID, - "event handler eventProtocolParameters not specified or blank"); - } else { - result.setResult("eventProtocolParameters", eventProtocolParameters.validate()); - } - - if (carrierTechnologyParameters == null) { - result.setResult("carrierTechnologyParameters", ValidationStatus.INVALID, - "event handler carrierTechnologyParameters not specified or blank"); - } else { - result.setResult("carrierTechnologyParameters", carrierTechnologyParameters.validate()); - } + public BeanValidationResult validate() { + final BeanValidationResult result = new BeanValidator().validateTop(getClass().getSimpleName(), this); if (eventNameFilter != null) { try { @@ -329,7 +319,7 @@ public class EventHandlerParameters implements ParameterGroup { } catch (final PatternSyntaxException pse) { String message = "event handler eventNameFilter is not a valid regular expression: " + pse.getMessage(); LOGGER.trace(message, pse); - result.setResult("eventNameFilter", ValidationStatus.INVALID, message); + result.addResult("eventNameFilter", eventNameFilter, ValidationStatus.INVALID, message); } } @@ -338,7 +328,7 @@ public class EventHandlerParameters implements ParameterGroup { /** * Check if we're using synchronous mode. - * + * * @return true if if we're using synchronous mode */ public boolean isSynchronousMode() { @@ -347,7 +337,7 @@ public class EventHandlerParameters implements ParameterGroup { /** * The synchronous peer for this event handler. - * + * * @return the synchronous peer for this event handler */ public String getSynchronousPeer() { @@ -356,7 +346,7 @@ public class EventHandlerParameters implements ParameterGroup { /** * Get the timeout for synchronous operations. - * + * * @return the timeout for synchronous operations */ public long getSynchronousTimeout() { @@ -365,7 +355,7 @@ public class EventHandlerParameters implements ParameterGroup { /** * Check if this event handler will use requestor mode. - * + * * @return true if this event handler will use requestor mode */ public boolean isRequestorMode() { @@ -374,7 +364,7 @@ public class EventHandlerParameters implements ParameterGroup { /** * The requestor peer for this event handler. - * + * * @return the requestor peer for this event handler */ public String getRequestorPeer() { @@ -383,7 +373,7 @@ public class EventHandlerParameters implements ParameterGroup { /** * Get the requestor timeout. - * + * * @return the requestorTimeout. */ public long getRequestorTimeout() { diff --git a/services/services-engine/src/main/java/org/onap/policy/apex/service/parameters/eventprotocol/EventProtocolParameters.java b/services/services-engine/src/main/java/org/onap/policy/apex/service/parameters/eventprotocol/EventProtocolParameters.java index 68d5f8a56..1fd8e7489 100644 --- a/services/services-engine/src/main/java/org/onap/policy/apex/service/parameters/eventprotocol/EventProtocolParameters.java +++ b/services/services-engine/src/main/java/org/onap/policy/apex/service/parameters/eventprotocol/EventProtocolParameters.java @@ -21,10 +21,11 @@ package org.onap.policy.apex.service.parameters.eventprotocol; -import org.onap.policy.common.parameters.GroupValidationResult; -import org.onap.policy.common.parameters.ParameterGroup; +import org.onap.policy.common.parameters.ParameterGroupImpl; import org.onap.policy.common.parameters.ParameterRuntimeException; -import org.onap.policy.common.parameters.ValidationStatus; +import org.onap.policy.common.parameters.annotations.ClassName; +import org.onap.policy.common.parameters.annotations.NotBlank; +import org.onap.policy.common.parameters.annotations.NotNull; /** * A default event protocol parameter class that may be specialized by event protocol plugins that require plugin @@ -39,12 +40,14 @@ import org.onap.policy.common.parameters.ValidationStatus; * * @author Liam Fallon (liam.fallon@ericsson.com) */ -public abstract class EventProtocolParameters implements ParameterGroup { +@NotNull +@NotBlank +public abstract class EventProtocolParameters extends ParameterGroupImpl { // The event protocol label private String label = null; // Event protocol converter plugin class for this event protocol - private String eventProtocolPluginClass; + private @ClassName String eventProtocolPluginClass; /** * Constructor to create an event protocol parameters instance with the name of a sub class of this class and @@ -99,25 +102,6 @@ public abstract class EventProtocolParameters implements ParameterGroup { + "]"; } - /** - * {@inheritDoc}. - */ - @Override - public GroupValidationResult validate() { - final GroupValidationResult result = new GroupValidationResult(this); - - if (label == null || label.length() == 0) { - result.setResult("label", ValidationStatus.INVALID, "event protocol label not specified or is blank"); - } - - if (eventProtocolPluginClass == null || eventProtocolPluginClass.length() == 0) { - result.setResult("eventProtocolPluginClass", ValidationStatus.INVALID, - "event protocol eventProtocolPluginClass not specified or is blank"); - } - - return result; - } - @Override public String getName() { return this.getLabel(); diff --git a/services/services-engine/src/main/java/org/onap/policy/apex/service/parameters/eventprotocol/EventProtocolTextCharDelimitedParameters.java b/services/services-engine/src/main/java/org/onap/policy/apex/service/parameters/eventprotocol/EventProtocolTextCharDelimitedParameters.java index d15d7ed47..3875d4a14 100644 --- a/services/services-engine/src/main/java/org/onap/policy/apex/service/parameters/eventprotocol/EventProtocolTextCharDelimitedParameters.java +++ b/services/services-engine/src/main/java/org/onap/policy/apex/service/parameters/eventprotocol/EventProtocolTextCharDelimitedParameters.java @@ -21,7 +21,7 @@ package org.onap.policy.apex.service.parameters.eventprotocol; -import org.onap.policy.common.parameters.GroupValidationResult; +import org.onap.policy.common.parameters.BeanValidationResult; import org.onap.policy.common.parameters.ValidationStatus; /** @@ -97,16 +97,16 @@ public abstract class EventProtocolTextCharDelimitedParameters extends EventProt * {@inheritDoc}. */ @Override - public GroupValidationResult validate() { - final GroupValidationResult result = super.validate(); + public BeanValidationResult validate() { + final BeanValidationResult result = super.validate(); if (startChar == '\0') { - result.setResult("startChar", ValidationStatus.INVALID, + result.addResult("startChar", null, ValidationStatus.INVALID, "text character delimited start character has not been specified"); } if (endChar == '\0') { - result.setResult("endChar", ValidationStatus.INVALID, + result.addResult("endChar", null, ValidationStatus.INVALID, "text character delimited end character has not been specified\n"); } diff --git a/services/services-engine/src/main/java/org/onap/policy/apex/service/parameters/eventprotocol/EventProtocolTextTokenDelimitedParameters.java b/services/services-engine/src/main/java/org/onap/policy/apex/service/parameters/eventprotocol/EventProtocolTextTokenDelimitedParameters.java index ff363aff6..32883301e 100644 --- a/services/services-engine/src/main/java/org/onap/policy/apex/service/parameters/eventprotocol/EventProtocolTextTokenDelimitedParameters.java +++ b/services/services-engine/src/main/java/org/onap/policy/apex/service/parameters/eventprotocol/EventProtocolTextTokenDelimitedParameters.java @@ -21,8 +21,8 @@ package org.onap.policy.apex.service.parameters.eventprotocol; -import org.onap.policy.common.parameters.GroupValidationResult; -import org.onap.policy.common.parameters.ValidationStatus; +import org.onap.policy.common.parameters.annotations.NotBlank; +import org.onap.policy.common.parameters.annotations.NotNull; /** * An event protocol parameter class for token delimited textual event protocols that may be specialized by event @@ -42,7 +42,7 @@ import org.onap.policy.common.parameters.ValidationStatus; */ public abstract class EventProtocolTextTokenDelimitedParameters extends EventProtocolParameters { // The delimiter token for text blocks - private String startDelimiterToken = null; + private @NotNull @NotBlank String startDelimiterToken = null; private String endDelimiterToken = null; private boolean delimiterAtStart = true; @@ -115,19 +115,4 @@ public abstract class EventProtocolTextTokenDelimitedParameters extends EventPro return "EventProtocolTextTokenDelimitedParameters [startDelimiterToken=" + startDelimiterToken + ", endDelimiterToken=" + endDelimiterToken + ", delimiterAtStart=" + delimiterAtStart + "]"; } - - /** - * {@inheritDoc}. - */ - @Override - public GroupValidationResult validate() { - final GroupValidationResult result = super.validate(); - - if (startDelimiterToken == null || startDelimiterToken.length() == 0) { - result.setResult("startDelimiterToken", ValidationStatus.INVALID, - "text start delimiter token not specified or is blank\n"); - } - - return result; - } } |