diff options
86 files changed, 5506 insertions, 3580 deletions
diff --git a/common-parameters/pom.xml b/common-parameters/pom.xml index 828a0845..87b2d3a5 100644 --- a/common-parameters/pom.xml +++ b/common-parameters/pom.xml @@ -36,5 +36,16 @@ <artifactId>junit</artifactId> <scope>test</scope> </dependency> + <dependency> + <groupId>com.google.code.gson</groupId> + <artifactId>gson</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.yaml</groupId> + <artifactId>snakeyaml</artifactId> + <version>1.21</version> + <scope>test</scope> + </dependency> </dependencies> </project> diff --git a/common-parameters/src/main/java/org/onap/policy/common/parameters/GroupMapValidationResult.java b/common-parameters/src/main/java/org/onap/policy/common/parameters/GroupMapValidationResult.java new file mode 100644 index 00000000..09cdc1de --- /dev/null +++ b/common-parameters/src/main/java/org/onap/policy/common/parameters/GroupMapValidationResult.java @@ -0,0 +1,163 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2018 Ericsson. 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========================================================= + */ + +package org.onap.policy.common.parameters; + +import java.lang.reflect.Field; +import java.util.LinkedHashMap; +import java.util.Map; +import java.util.Map.Entry; + +/** + * This class holds the result of the validation of a map of parameter groups. + */ +public class GroupMapValidationResult implements ValidationResult { + // The name of the parameter group map + final String mapParameterName; + + // Validation status for the entire parameter class + private ValidationStatus status = ValidationStatus.CLEAN; + private String message = ParameterConstants.PARAMETER_GROUP_MAP_HAS_STATUS_MESSAGE + status.toString(); + + // Validation results for each parameter in the group + private final Map<String, ValidationResult> validationResultMap = new LinkedHashMap<>(); + + /** + * Constructor, create the group map validation result. + * + * @param field the map parameter field + * @param mapObject the value of the map parameter field + */ + protected GroupMapValidationResult(final Field field, final Object mapObject) { + this.mapParameterName = field.getName(); + + // Cast the map object to a map of parameter groups keyed by string, we can't type check maps + // due to restrictions on generics so we have to check each entry key is a string and each entry + // value is a parameter group + @SuppressWarnings("unchecked") + Map<String, ParameterGroup> parameterGroupMap = (Map<String, ParameterGroup>) mapObject; + + // Add a validation result per map entry + for (Entry<String, ParameterGroup> parameterGroupMapEntry : parameterGroupMap.entrySet()) { + // Create a validation status entry for the map + validationResultMap.put(parameterGroupMapEntry.getKey(), + new GroupValidationResult(parameterGroupMapEntry.getValue())); + } + } + + /** + * Gets the name of the parameter being validated. + * + * @return the name + */ + @Override + public String getName() { + return mapParameterName; + } + + /** + * Gets the status of validation. + * + * @return the status + */ + @Override + public ValidationStatus getStatus() { + return status; + } + + /** + * Set the validation result on on a parameter group. + * + * @param status The validation status the field is receiving + * @param message The validation message explaining the validation status + */ + @Override + public void setResult(ValidationStatus status, String message) { + setResult(status); + this.message = message; + } + + /** + * Set the validation result on on a parameter group. + * + * @param status The validation status the field is receiving + */ + public void setResult(final ValidationStatus status) { + // We record the most serious validation status, assuming the status enum ordinals + // increase in order of severity + if (this.status.ordinal() < status.ordinal()) { + this.status = status; + this.message = ParameterConstants.PARAMETER_GROUP_HAS_STATUS_MESSAGE + status.toString(); + } + } + + /** + * Set the validation result on a parameter map entry. + * + * @param entryName The name of the parameter map entry + * @param mapEntryValidationResult The validation result for the entry + */ + public void setResult(String entryName, ValidationResult mapEntryValidationResult) { + ValidationResult validationResult = validationResultMap.get(entryName); + if (validationResult == null) { + throw new ParameterRuntimeException("no entry with name \"" + entryName + "\" exists"); + } + + // Set the status of the parameter group and replace the field result + validationResultMap.put(entryName, mapEntryValidationResult); + this.setResult(status); + } + + /** + * Gets the validation result. + * + * @param initialIndentation the indentation to use on the main result output + * @param subIndentation the indentation to use on sub parts of the result output + * @param showClean output information on clean fields + * @return the result + */ + @Override + public String getResult(final String initialIndentation, final String subIndentation, final boolean showClean) { + if (status == ValidationStatus.CLEAN && !showClean) { + return null; + } + + StringBuilder validationResultBuilder = new StringBuilder(); + + validationResultBuilder.append(initialIndentation); + validationResultBuilder.append("parameter group map \""); + validationResultBuilder.append(mapParameterName); + validationResultBuilder.append("\" "); + validationResultBuilder.append(status); + validationResultBuilder.append(", "); + validationResultBuilder.append(message); + validationResultBuilder.append('\n'); + + for (ValidationResult fieldResult : validationResultMap.values()) { + String fieldResultMessage = fieldResult.getResult(initialIndentation + subIndentation, subIndentation, + showClean); + if (fieldResultMessage != null) { + validationResultBuilder.append(fieldResultMessage); + } + } + + return validationResultBuilder.toString(); + } +}
\ No newline at end of file diff --git a/common-parameters/src/main/java/org/onap/policy/common/parameters/GroupValidationResult.java b/common-parameters/src/main/java/org/onap/policy/common/parameters/GroupValidationResult.java new file mode 100644 index 00000000..91c3d145 --- /dev/null +++ b/common-parameters/src/main/java/org/onap/policy/common/parameters/GroupValidationResult.java @@ -0,0 +1,324 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2018 Ericsson. 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========================================================= + */ + +package org.onap.policy.common.parameters; + +import java.lang.reflect.Field; +import java.util.Collection; +import java.util.LinkedHashMap; +import java.util.Map; +import java.util.Map.Entry; + +/** + * This class holds the result of the validation of a parameter group. + */ +public class GroupValidationResult implements ValidationResult { + // The parameter group which the validation result applies + private final ParameterGroup parameterGroup; + + // Validation status for the entire parameter class + private ValidationStatus status = ValidationStatus.CLEAN; + private String message = ParameterConstants.PARAMETER_GROUP_HAS_STATUS_MESSAGE + status.toString(); + + // Validation results for each parameter in the group + private final Map<String, ValidationResult> validationResultMap = new LinkedHashMap<>(); + + /** + * Constructor, create the field validation result with default arguments. + * + * @param parameterGroup the parameter group being validated + */ + public GroupValidationResult(final ParameterGroup parameterGroup) { + this.parameterGroup = parameterGroup; + + // Add a validation result per field + for (Field field : parameterGroup.getClass().getDeclaredFields()) { + // Exclude system fields + if (field.getName().startsWith("$") || field.getName().startsWith("_")) { + continue; + } + + // Make the field accessible + boolean savedAccessibilityValue = field.isAccessible(); + field.setAccessible(true); + + try { + // Set the validation result + validationResultMap.put(field.getName(), getValidationResult(field, parameterGroup)); + } catch (IllegalArgumentException | IllegalAccessException e) { + throw new ParameterRuntimeException("could not get value of parameter \"" + field.getName() + "\"", e); + } finally { + field.setAccessible(savedAccessibilityValue); + } + } + } + + /** + * Construct a validation result for a field + * + * @param field The parameter field + * @param ParameterGroup The parameter group containing the field + * @return the validation result + * @throws IllegalAccessException on accessing private fields + */ + private ValidationResult getValidationResult(final Field field, final ParameterGroup parameterGroup) + throws IllegalAccessException { + final String fieldName = field.getName(); + final Class<?> fieldType = field.getType(); + final Object fieldObject = field.get(parameterGroup); + + // Nested parameter groups are allowed + if (ParameterGroup.class.isAssignableFrom(fieldType)) { + return new GroupValidationResult((ParameterGroup) field.get(parameterGroup)); + } + + // Nested maps of parameter groups are allowed + if (Map.class.isAssignableFrom(field.getType())) { + checkMapIsParameterGroupMap(fieldName, fieldObject); + return new GroupMapValidationResult(field, fieldObject); + } + + // Collections of parameter groups are not allowed + if (Collection.class.isAssignableFrom(field.getType())) { + checkCollection4ParameterGroups(fieldName, fieldObject); + return new ParameterValidationResult(field, fieldObject); + } + + // It's a regular parameter + return new ParameterValidationResult(field, fieldObject); + } + + /** + * Check if this field is a map of parameter groups indexed by string keys. + * + * @param fieldName the name of the collection field. + * @param mapObject the map object to check + */ + private void checkMapIsParameterGroupMap(String fieldName, Object mapObject) { + if (mapObject == null) { + throw new ParameterRuntimeException("map parameter \"" + fieldName + "\" is null"); + } + + Map<?, ?> incomingMap = (Map<?, ?>) mapObject; + + for (Entry<?, ?> mapEntry : incomingMap.entrySet()) { + // Check the key is a string + if (!String.class.isAssignableFrom(mapEntry.getKey().getClass())) { + throw new ParameterRuntimeException("map entry is not a parameter group keyed by a string, key \"" + + mapEntry.getKey() + "\" in map \"" + fieldName + "\" is not a string"); + } + + // Check the value is a parameter group + if (!ParameterGroup.class.isAssignableFrom(mapEntry.getValue().getClass())) { + throw new ParameterRuntimeException("map entry is not a parameter group keyed by a string, value \"" + + mapEntry.getValue() + "\" in map \"" + fieldName + "\" is not a parameter group"); + } + } + } + + /** + * Check if this field contains parameter groups. + * + * @param fieldName the name of the collection field. + * @param collectionObject the collection object to check + */ + private void checkCollection4ParameterGroups(final String fieldName, final Object collectionObject) { + if (collectionObject == null) { + throw new ParameterRuntimeException("collection parameter \"" + fieldName + "\" is null"); + } + + Collection<?> collection2Check = (Collection<?>) collectionObject; + + for (Object collectionMember : collection2Check) { + if (ParameterGroup.class.isAssignableFrom(collectionMember.getClass())) { + throw new ParameterRuntimeException("collection parameter \"" + fieldName + "\" is illegal," + + " parameter groups are not allowed as collection members"); + } + } + } + + /** + * Gets the parameter group for this validation result. + * + * @return the parameter class + */ + public ParameterGroup getParameterGroup() { + return parameterGroup; + } + + /** + * Gets the name of the parameter group being validated. + * + * @return the name + */ + @Override + public String getName() { + return parameterGroup.getName(); + } + + /** + * Gets the status of validation. + * + * @return the status + */ + @Override + public ValidationStatus getStatus() { + return status; + } + + /** + * Set the validation result on on a parameter group. + * + * @param status The validation status the parameter group is receiving + * @param message The validation message explaining the validation status + */ + @Override + public void setResult(ValidationStatus status, String message) { + setResult(status); + this.message = message; + } + + /** + * Set the validation result on on a parameter group. + * + * On a sequence of calls, the most serious validation status is recorded, assuming the status enum ordinal increase + * in order of severity + * + * @param status The validation status the parameter group is receiving + */ + public void setResult(final ValidationStatus status) { + // + if (this.status.ordinal() < status.ordinal()) { + this.status = status; + this.message = ParameterConstants.PARAMETER_GROUP_HAS_STATUS_MESSAGE + status.toString(); + } + } + + /** + * Set the validation result on a parameter group. + * + * @param parameterGroupName The name of the parameter group + * @param status The validation status the field is receiving + * @param message The validation message explaining the validation status + */ + public void setResult(final String parameterGroupName, final ValidationStatus status, final String message) { + ParameterValidationResult parameterValidationResult; + try { + parameterValidationResult = (ParameterValidationResult) validationResultMap.get(parameterGroupName); + } catch (ClassCastException e) { + throw new ParameterRuntimeException("parameter not a regular parameter: " + parameterGroupName, e); + } + + if (parameterValidationResult == null) { + throw new ParameterRuntimeException( + "no regular parameter field exists for parameter: " + parameterGroupName); + } + + // Set the status of the parameter group and the field + parameterValidationResult.setResult(status, message); + this.setResult(status); + } + + /** + * Set the validation result on a nested parameter group. + * + * @param parameterName The name of the parameter field + * @param nestedValidationResult The validation result from a nested field + */ + public void setResult(String parameterName, ValidationResult nestedValidationResult) { + GroupValidationResult groupValidationResult; + try { + groupValidationResult = (GroupValidationResult) validationResultMap.get(parameterName); + } catch (ClassCastException e) { + throw new ParameterRuntimeException("parameter is not a nested group parameter: " + parameterName, e); + } + + if (groupValidationResult == null) { + throw new ParameterRuntimeException("no nested parameter field exists for parameter: " + parameterName); + } + + // Set the status of the parameter group and replace the field result + validationResultMap.put(parameterName, nestedValidationResult); + this.setResult(status); + } + + /** + * Set the validation result on a nested parameter group map entry. + * + * @param parameterName The name of the parameter field + * @param key The key of the map entry + * @param nestedMapValidationResult The validation result from a nested map entry + */ + public void setResult(final String parameterName, final String key, + final ValidationResult nestedMapValidationResult) { + GroupMapValidationResult groupMapValidationResult; + try { + groupMapValidationResult = (GroupMapValidationResult) validationResultMap.get(parameterName); + } catch (ClassCastException e) { + throw new ParameterRuntimeException("parameter is not a nested group map parameter: " + parameterName, e); + } + + if (groupMapValidationResult == null) { + throw new ParameterRuntimeException("no group map parameter field exists for parameter: " + parameterName); + } + + // Set the status of the parameter group and the field + groupMapValidationResult.setResult(key, nestedMapValidationResult); + this.setResult(status); + } + + /** + * Gets the validation result. + * + * @param initialIndentation the indentation to use on the main result output + * @param subIndentation the indentation to use on sub parts of the result output + * @param showClean output information on clean fields + * @return the result + */ + @Override + public String getResult(final String initialIndentation, final String subIndentation, final boolean showClean) { + if (status == ValidationStatus.CLEAN && !showClean) { + return null; + } + + StringBuilder validationResultBuilder = new StringBuilder(); + + validationResultBuilder.append(initialIndentation); + validationResultBuilder.append("parameter group \""); + validationResultBuilder.append(parameterGroup.getName()); + validationResultBuilder.append("\" type \""); + validationResultBuilder.append(parameterGroup.getClass().getCanonicalName()); + validationResultBuilder.append("\" "); + validationResultBuilder.append(status); + validationResultBuilder.append(", "); + validationResultBuilder.append(message); + validationResultBuilder.append('\n'); + + for (ValidationResult fieldResult : validationResultMap.values()) { + String fieldResultMessage = fieldResult.getResult(initialIndentation + subIndentation, subIndentation, + showClean); + if (fieldResultMessage != null) { + validationResultBuilder.append(fieldResultMessage); + } + } + + return validationResultBuilder.toString(); + } +}
\ No newline at end of file diff --git a/common-parameters/src/main/java/org/onap/policy/common/parameters/ParameterConstants.java b/common-parameters/src/main/java/org/onap/policy/common/parameters/ParameterConstants.java new file mode 100644 index 00000000..240b1f40 --- /dev/null +++ b/common-parameters/src/main/java/org/onap/policy/common/parameters/ParameterConstants.java @@ -0,0 +1,45 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2018 Ericsson. 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========================================================= + */ + +package org.onap.policy.common.parameters; + +/** + * This static class holds the values of constants for parameter handling. + */ +public final class ParameterConstants { + // Indentation is 0 on the left and 2 for each level of hierarchy + public static final String DEFAULT_INITIAL_RESULT_INDENTATION = ""; + public static final String DEFAULT_RESULT_INDENTATION = " "; + + // By default we do not show validation results for parameters that are validated as clean + public static final boolean DO_NOT_SHOW_CLEAN_RESULTS = false; + + // Messages for clean validations + public static final String PARAMETER_GROUP_HAS_STATUS_MESSAGE = "parameter group has status "; + public static final String PARAMETER_GROUP_MAP_HAS_STATUS_MESSAGE = "parameter group map has status "; + public static final String PARAMETER_HAS_STATUS_MESSAGE = "parameter has status "; + + /** + * Private constructor to prevent subclassing. + */ + private ParameterConstants() { + // Private constructor to prevent subclassing + } +}
\ No newline at end of file diff --git a/common-parameters/src/main/java/org/onap/policy/common/parameters/ParameterException.java b/common-parameters/src/main/java/org/onap/policy/common/parameters/ParameterException.java index 129d5390..3a6e17e8 100644 --- a/common-parameters/src/main/java/org/onap/policy/common/parameters/ParameterException.java +++ b/common-parameters/src/main/java/org/onap/policy/common/parameters/ParameterException.java @@ -55,21 +55,21 @@ public class ParameterException extends Exception { * Instantiates a new parameter exception. * * @param message the message on the exception - * @param e the exception that caused this parameter exception + * @param exception the exception that caused this parameter exception */ - public ParameterException(final String message, final Exception e) { - this(message, e, null); + public ParameterException(final String message, final Exception exception) { + this(message, exception, null); } /** * Instantiates a new parameter exception. * * @param message the message on the exception - * @param e the exception that caused this parameter exception + * @param exception the exception that caused this parameter exception * @param object the object that the exception was thrown on */ - public ParameterException(final String message, final Exception e, final Object object) { - super(message, e); + public ParameterException(final String message, final Exception exception, final Object object) { + super(message, exception); this.object = object; } @@ -83,7 +83,7 @@ public class ParameterException extends Exception { } /** - * Build a cascaded message from an exception and all its nested exceptions + * Build a cascaded message from an exception and all its nested exceptions. * * @param throwable the top level exception * @return cascaded message string @@ -101,7 +101,6 @@ public class ParameterException extends Exception { } /** - * * Get the object on which the exception was thrown. * * @return The object on which the exception was thrown diff --git a/common-parameters/src/main/java/org/onap/policy/common/parameters/AbstractParameters.java b/common-parameters/src/main/java/org/onap/policy/common/parameters/ParameterGroup.java index 5430dfeb..48e8379f 100644 --- a/common-parameters/src/main/java/org/onap/policy/common/parameters/AbstractParameters.java +++ b/common-parameters/src/main/java/org/onap/policy/common/parameters/ParameterGroup.java @@ -1,5 +1,4 @@ -package org.onap.policy.common.parameters; -/* +/*- * ============LICENSE_START======================================================= * Copyright (C) 2018 Ericsson. All rights reserved. * ================================================================================ @@ -19,29 +18,35 @@ package org.onap.policy.common.parameters; * ============LICENSE_END========================================================= */ +package org.onap.policy.common.parameters; + /** - * This class defines an abstract parameter interface that acts as a base interface for all parameters in the ONAP - * Policy Framework. All parameter POJOs are subclass of the abstract parameter class and can be used with the - * {@link ParameterService}. + * This interface acts as a base interface for all parameter groups in the ONAP Policy Framework. All parameter group + * POJOs are implementations of the parameter group interface and can be used with the {@link ParameterService}. * * @author Liam Fallon (liam.fallon@ericsson.com) */ -public interface AbstractParameters { +public interface ParameterGroup { /** - * Gets the parameter class. - * - * @return the parameter class + * Get the group name. + * + * @return the group name */ - default Class<? extends AbstractParameters> getParameterClass() { - return this.getClass(); - } + public String getName(); + + /** + * Validate parameters. + * + * @return the result of the parameter validation + */ + GroupValidationResult validate(); /** - * Gets the parameter class name. - * - * @return the parameter class name + * Check if the parameters are valid. + * + * @return true if the parameters are valid */ - default String getParameterClassName() { - return this.getClass().getCanonicalName(); + default boolean isValid() { + return validate().getStatus().isValid(); } } diff --git a/common-parameters/src/main/java/org/onap/policy/common/parameters/ParameterRuntimeException.java b/common-parameters/src/main/java/org/onap/policy/common/parameters/ParameterRuntimeException.java index 4b7d5874..071593a9 100644 --- a/common-parameters/src/main/java/org/onap/policy/common/parameters/ParameterRuntimeException.java +++ b/common-parameters/src/main/java/org/onap/policy/common/parameters/ParameterRuntimeException.java @@ -55,21 +55,21 @@ public class ParameterRuntimeException extends RuntimeException { * Instantiates a new parameter runtime exception. * * @param message the message on the exception - * @param e the exception that caused this parameter exception + * @param exception the exception that caused this parameter exception */ - public ParameterRuntimeException(final String message, final Exception e) { - this(message, e, null); + public ParameterRuntimeException(final String message, final Exception exception) { + this(message, exception, null); } /** * Instantiates a new parameter runtime exception. * * @param message the message on the exception - * @param e the exception that caused this parameter exception + * @param exception the exception that caused this parameter exception * @param object the object that the exception was thrown on */ - public ParameterRuntimeException(final String message, final Exception e, final Object object) { - super(message, e); + public ParameterRuntimeException(final String message, final Exception exception, final Object object) { + super(message, exception); this.object = object; } diff --git a/common-parameters/src/main/java/org/onap/policy/common/parameters/ParameterService.java b/common-parameters/src/main/java/org/onap/policy/common/parameters/ParameterService.java index f411937d..db6995c5 100644 --- a/common-parameters/src/main/java/org/onap/policy/common/parameters/ParameterService.java +++ b/common-parameters/src/main/java/org/onap/policy/common/parameters/ParameterService.java @@ -1,5 +1,4 @@ -package org.onap.policy.common.parameters; -/* +/*- * ============LICENSE_START======================================================= * Copyright (C) 2018 Ericsson. All rights reserved. * ================================================================================ @@ -19,25 +18,27 @@ package org.onap.policy.common.parameters; * ============LICENSE_END========================================================= */ +package org.onap.policy.common.parameters; + import java.util.Map; import java.util.Map.Entry; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; /** - * The parameter service makes ONAP PF parameters available to all classes in a JVM. - * - * The reason for having a parameter service is to avoid having to pass parameters down long call chains in modules such - * as PDPs and editors. The parameter service makes correct and verified parameters available statically. - * - * The parameter service must be used with care because changing a parameter set anywhere in a JVM will affect all users - * of those parameters anywhere in the JVM. + * The parameter service makes ONAP PF parameter groups available to all classes in a JVM. + * + * <p>The reason for having a parameter service is to avoid having to pass parameters down long call chains in modules + * such as PDPs and editors. The parameter service makes correct and verified parameters available statically. + * + * <p>The parameter service must be used with care because changing a parameter set anywhere in a JVM will affect all + * users of those parameters anywhere in the JVM. * * @author Liam Fallon (liam.fallon@ericsson.com) */ public abstract class ParameterService { // The map holding the parameters - private static Map<Class<? extends AbstractParameters>, AbstractParameters> parameterMap = new ConcurrentHashMap<>(); + private static Map<String, ParameterGroup> parameterGroupMap = new ConcurrentHashMap<>(); /** * This class is an abstract static class that cannot be extended. @@ -46,69 +47,71 @@ public abstract class ParameterService { } /** - * Register parameters with the parameter service. + * Register a parameter group with the parameter service. * - * @param <P> the generic type - * @param parametersClass the class of the parameter, used to index the parameter - * @param parameters the parameters + * @param parameterGroup the parameter group */ - public static <P extends AbstractParameters> void registerParameters(final P parameters) { - parameterMap.put(parameters.getClass(), parameters); + public static void register(final ParameterGroup parameterGroup) { + if (!parameterGroupMap.containsKey(parameterGroup.getName())) { + parameterGroupMap.put(parameterGroup.getName(), parameterGroup); + } else { + throw new ParameterRuntimeException( + "\"" + parameterGroup.getName() + "\" already registered in parameter service"); + } } /** - * Remove parameters from the parameter service. + * Remove a parameter group from the parameter service. * - * @param <P> the generic type - * @param parametersClass the class of the parameter, used to index the parameter + * @param parameterGroupName the name of the parameter group */ - public static <P extends AbstractParameters> void deregisterParameters(final Class<P> parametersClass) { - parameterMap.remove(parametersClass); + public static void deregister(final String parameterGroupName) { + if (parameterGroupMap.containsKey(parameterGroupName)) { + parameterGroupMap.remove(parameterGroupName); + } else { + throw new ParameterRuntimeException("\"" + parameterGroupName + "\" not registered in parameter service"); + } } /** - * Get parameters from the parameter service. + * Get a parameter group from the parameter service. * - * @param <P> the generic type - * @param parametersClass the class of the parameter, used to index the parameter - * @return The parameter + * @param parameterGroupName the name of the parameter group + * @return The parameter group */ - @SuppressWarnings("unchecked") - public static <P extends AbstractParameters> P getParameters(final Class<P> parametersClass) { - final P parameter = (P) parameterMap.get(parametersClass); + public static ParameterGroup get(final String parameterGroupName) { + final ParameterGroup parameterGroup = parameterGroupMap.get(parameterGroupName); - if (parameter == null) { - throw new ParameterRuntimeException( - "Parameters for " + parametersClass.getCanonicalName() + " not found in parameter service"); + if (parameterGroup == null) { + throw new ParameterRuntimeException("\"" + parameterGroupName + "\" not found in parameter service"); } - return parameter; + return parameterGroup; } /** - * Check if parameters is defined on the parameter service. + * Check if a parameter group is defined on the parameter service. * - * @param <P> the generic type - * @param parametersClass the class of the parameter, used to index the parameter + * @param parameterGroupName the name of the parameter group * @return true if the parameter is defined */ - public static <P extends AbstractParameters> boolean existsParameters(final Class<P> parametersClass) { - return parameterMap.get(parametersClass) != null; + public static boolean contains(final String parameterGroupName) { + return parameterGroupMap.containsKey(parameterGroupName) && parameterGroupMap.get(parameterGroupName) != null; } /** - * Get all the entries in the parameters map. + * Get all parameter groups. * * @return The entries */ - public static Set<Entry<Class<? extends AbstractParameters>, AbstractParameters>> getAll() { - return parameterMap.entrySet(); + public static Set<Entry<String, ParameterGroup>> getAll() { + return parameterGroupMap.entrySet(); } /** - * Clear all parameters in the parameter service. + * Clear all parameter groups in the parameter service. */ public static void clear() { - parameterMap.clear(); + parameterGroupMap.clear(); } } diff --git a/common-parameters/src/main/java/org/onap/policy/common/parameters/ParameterValidationResult.java b/common-parameters/src/main/java/org/onap/policy/common/parameters/ParameterValidationResult.java new file mode 100644 index 00000000..9c829f4b --- /dev/null +++ b/common-parameters/src/main/java/org/onap/policy/common/parameters/ParameterValidationResult.java @@ -0,0 +1,110 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2018 Ericsson. 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========================================================= + */ + +package org.onap.policy.common.parameters; + +import java.lang.reflect.Field; + +/** + * This class holds the result of the validation of a parameter. + */ +public class ParameterValidationResult implements ValidationResult { + // The field and value of the parameter to which the validation result applies + private final Field field; + private final Object parameterValue; + + // Validation status and message + private ValidationStatus status = ValidationStatus.CLEAN; + private String message = ParameterConstants.PARAMETER_HAS_STATUS_MESSAGE + status.toString(); + + /** + * Constructor, create validation result for a parameter with default arguments. + * + * @param field the parameter field + * @param parameterValue the value of the parameter field + */ + protected ParameterValidationResult(final Field field, final Object parameterValue) { + this.field = field; + this.parameterValue = parameterValue; + } + + /** + * Gets the name of the parameter being validated. + * + * @return the name + */ + @Override + public String getName() { + return field.getName(); + } + + /** + * Gets the validation status. + * + * @return the validation status + */ + @Override + public ValidationStatus getStatus() { + return status; + } + + /** + * Set the validation result on on a parameter field. + * @param status The validation status the field is receiving + * @param message The validation message explaining the validation status + */ + @Override + public void setResult(final ValidationStatus status, final String message) { + this.status = status; + this.message = message; + } + + /** + * Gets the validation result. + * + * @param initialIndentation the result indentation + * @param subIndentation the indentation to use on sub parts of the result output + * @param showClean output information on clean fields + * @return the result + */ + @Override + public String getResult(final String initialIndentation, final String subIndentation, final boolean showClean) { + if (status == ValidationStatus.CLEAN && !showClean) { + return null; + } + + StringBuilder validationResultBuilder = new StringBuilder(); + + validationResultBuilder.append(initialIndentation); + validationResultBuilder.append("field \""); + validationResultBuilder.append(getName()); + validationResultBuilder.append("\" type \""); + validationResultBuilder.append(field.getType().getCanonicalName()); + validationResultBuilder.append("\" value \""); + validationResultBuilder.append(parameterValue); + validationResultBuilder.append("\" "); + validationResultBuilder.append(getStatus()); + validationResultBuilder.append(", "); + validationResultBuilder.append(message); + validationResultBuilder.append('\n'); + + return validationResultBuilder.toString(); + } +} diff --git a/common-parameters/src/main/java/org/onap/policy/common/parameters/ValidationResult.java b/common-parameters/src/main/java/org/onap/policy/common/parameters/ValidationResult.java new file mode 100644 index 00000000..b97ccda0 --- /dev/null +++ b/common-parameters/src/main/java/org/onap/policy/common/parameters/ValidationResult.java @@ -0,0 +1,78 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2018 Ericsson. 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========================================================= + */ + +package org.onap.policy.common.parameters; + +/** + * This interface defines the result of a parameter validation. + */ +public interface ValidationResult { + /** + * Gets the name of the entity being validated. + * + * @return the name + */ + public String getName(); + + /** + * Gets the status of validation. + * + * @return the status + */ + public ValidationStatus getStatus(); + + /** + * Checks if the result is valid. + * + * @return true, if is valid + */ + default boolean isValid() { + return getStatus().isValid(); + } + + /** + * Gets the validation result. + * + * @return the full validation result + */ + default String getResult() { + return getResult( + ParameterConstants.DEFAULT_INITIAL_RESULT_INDENTATION, + ParameterConstants.DEFAULT_RESULT_INDENTATION, + ParameterConstants.DO_NOT_SHOW_CLEAN_RESULTS); + } + + /** + * Gets the validation result. + * + * @param initialIndentation the indentation to use on the main result output + * @param subIndentation the indentation to use on sub parts of the result output + * @param showClean output information on clean fields + * @return the result + */ + public String getResult(final String initialIndentation, final String subIndentation, final boolean showClean); + + /** + * Set a validation result. + * @param status The validation status the field is receiving + * @param message The validation message explaining the validation status + */ + public void setResult(final ValidationStatus status, final String message); +}
\ No newline at end of file diff --git a/common-parameters/src/test/java/org/onap/policy/common/parameters/LegalParameters.java b/common-parameters/src/main/java/org/onap/policy/common/parameters/ValidationStatus.java index fb1bd478..ff453a1b 100644 --- a/common-parameters/src/test/java/org/onap/policy/common/parameters/LegalParameters.java +++ b/common-parameters/src/main/java/org/onap/policy/common/parameters/ValidationStatus.java @@ -1,4 +1,4 @@ -/* +/*- * ============LICENSE_START======================================================= * Copyright (C) 2018 Ericsson. All rights reserved. * ================================================================================ @@ -20,12 +20,17 @@ package org.onap.policy.common.parameters; -import org.onap.policy.common.parameters.AbstractParameters; - -/** - * @author Liam Fallon (liam.fallon@ericsson.com) - */ -public class LegalParameters implements AbstractParameters { - public LegalParameters() { +public enum ValidationStatus { + CLEAN, + OBSERVATION, + WARNING, + INVALID; + + /** + * The result of a validation is valid unless the status is INVALID. + * @return true if the validation has passed + */ + public boolean isValid() { + return !this.equals(INVALID); } } diff --git a/common-parameters/src/test/java/org/onap/policy/common/parameters/ExceptionTest.java b/common-parameters/src/test/java/org/onap/policy/common/parameters/ExceptionTest.java index 510b6e36..b65db953 100644 --- a/common-parameters/src/test/java/org/onap/policy/common/parameters/ExceptionTest.java +++ b/common-parameters/src/test/java/org/onap/policy/common/parameters/ExceptionTest.java @@ -1,4 +1,4 @@ -/* +/*- * ============LICENSE_START======================================================= * Copyright (C) 2018 Ericsson. All rights reserved. * ================================================================================ @@ -17,6 +17,7 @@ * SPDX-License-Identifier: Apache-2.0 * ============LICENSE_END========================================================= */ + package org.onap.policy.common.parameters; import static org.junit.Assert.assertEquals; diff --git a/common-parameters/src/test/java/org/onap/policy/common/parameters/TestAbstractParameters.java b/common-parameters/src/test/java/org/onap/policy/common/parameters/TestAbstractParameters.java index f8003cfe..b568d7fb 100644 --- a/common-parameters/src/test/java/org/onap/policy/common/parameters/TestAbstractParameters.java +++ b/common-parameters/src/test/java/org/onap/policy/common/parameters/TestAbstractParameters.java @@ -20,16 +20,16 @@ package org.onap.policy.common.parameters; -import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; import org.junit.Test; +import org.onap.policy.common.parameters.testclasses.EmptyParameterGroup; public class TestAbstractParameters { @Test public void testAbstractParameters() { - final LegalParameters parameters = new LegalParameters(); - assertEquals(LegalParameters.class, parameters.getParameterClass()); - assertEquals("org.onap.policy.common.parameters.LegalParameters", parameters.getParameterClassName()); + final EmptyParameterGroup parameters = new EmptyParameterGroup("Empty Group"); + assertTrue(parameters.isValid()); } } diff --git a/common-parameters/src/test/java/org/onap/policy/common/parameters/TestJsonInput.java b/common-parameters/src/test/java/org/onap/policy/common/parameters/TestJsonInput.java new file mode 100644 index 00000000..6d0aae5e --- /dev/null +++ b/common-parameters/src/test/java/org/onap/policy/common/parameters/TestJsonInput.java @@ -0,0 +1,62 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2018 Ericsson. 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========================================================= + */ + +package org.onap.policy.common.parameters; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import java.io.FileReader; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Paths; + +import org.junit.Test; +import org.onap.policy.common.parameters.testclasses.TestParametersL00; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; + +public class TestJsonInput { + + @Test + public void testJsonInput() throws IOException { + TestParametersL00 testParameterGroup = null; + + // Read the parameters + try { + // Read the parameters from JSON using Gson + final Gson gson = new GsonBuilder().create(); + testParameterGroup = gson.fromJson(new FileReader("src/test/resources/parameters/TestParameters.json"), TestParametersL00.class); + } catch (final Exception e) { + fail("test should not throw an exception here: " + e.getMessage()); + } + + GroupValidationResult validationResult = testParameterGroup.validate(); + assertTrue(validationResult.isValid()); + assertEquals("l00NameFromFile", testParameterGroup.getName()); + + String expectedResult = new String(Files.readAllBytes( + Paths.get("src/test/resources/expectedValidationResults/TestJsonYamlValidationResult.txt"))) + .replaceAll("\\s+", ""); + assertEquals(expectedResult, validationResult.getResult("", " ", true).replaceAll("\\s+", "")); + } +} diff --git a/common-parameters/src/test/java/org/onap/policy/common/parameters/TestParameterService.java b/common-parameters/src/test/java/org/onap/policy/common/parameters/TestParameterService.java index efd3e304..0b7e46ca 100644 --- a/common-parameters/src/test/java/org/onap/policy/common/parameters/TestParameterService.java +++ b/common-parameters/src/test/java/org/onap/policy/common/parameters/TestParameterService.java @@ -27,6 +27,9 @@ import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import org.junit.Test; +import org.onap.policy.common.parameters.ParameterRuntimeException; +import org.onap.policy.common.parameters.ParameterService; +import org.onap.policy.common.parameters.testclasses.EmptyParameterGroup; public class TestParameterService { @@ -34,47 +37,57 @@ public class TestParameterService { public void testParameterService() { ParameterService.clear(); - assertFalse(ParameterService.existsParameters(LegalParameters.class)); + assertFalse(ParameterService.contains("EmptyGroup")); try { - ParameterService.getParameters(LegalParameters.class); + ParameterService.get("EmptyGroup"); fail("Test should throw an exception here"); } catch (final Exception e) { - assertEquals( - "Parameters for org.onap.policy.common.parameters.LegalParameters not found in parameter service", - e.getMessage()); + assertEquals("\"EmptyGroup\" not found in parameter service", e.getMessage()); } - ParameterService.registerParameters(new LegalParameters()); - assertTrue(ParameterService.existsParameters(LegalParameters.class)); - assertNotNull(ParameterService.getParameters(LegalParameters.class)); + ParameterService.register(new EmptyParameterGroup("Empty Group")); + assertTrue(ParameterService.contains("Empty Group")); + assertNotNull(ParameterService.get("Empty Group")); + + try { + ParameterService.register(new EmptyParameterGroup("Empty Group")); + fail("this test should throw an exception"); + } + catch (ParameterRuntimeException e) { + assertEquals("\"Empty Group\" already registered in parameter service", e.getMessage()); + } + + ParameterService.deregister("Empty Group"); + assertFalse(ParameterService.contains("Empty Group")); - ParameterService.deregisterParameters(LegalParameters.class); + try { + ParameterService.deregister("Empty Group"); + fail("this test should throw an exception"); + } + catch (ParameterRuntimeException e) { + assertEquals("\"Empty Group\" not registered in parameter service", e.getMessage()); + } - assertFalse(ParameterService.existsParameters(LegalParameters.class)); try { - ParameterService.getParameters(LegalParameters.class); + ParameterService.get("Empty Group"); fail("Test should throw an exception here"); } catch (final Exception e) { - assertEquals( - "Parameters for org.onap.policy.common.parameters.LegalParameters not found in parameter service", - e.getMessage()); + assertEquals("\"Empty Group\" not found in parameter service", e.getMessage()); } - ParameterService.registerParameters(new LegalParameters()); - assertTrue(ParameterService.existsParameters(LegalParameters.class)); - assertNotNull(ParameterService.getParameters(LegalParameters.class)); + ParameterService.register(new EmptyParameterGroup("Empty Group")); + assertTrue(ParameterService.contains("Empty Group")); + assertNotNull(ParameterService.get("Empty Group")); assertEquals(1, ParameterService.getAll().size()); ParameterService.clear(); assertEquals(0, ParameterService.getAll().size()); - assertFalse(ParameterService.existsParameters(LegalParameters.class)); + assertFalse(ParameterService.contains("Empty Group")); try { - ParameterService.getParameters(LegalParameters.class); + ParameterService.get("Empty Group"); fail("Test should throw an exception here"); } catch (final Exception e) { - assertEquals( - "Parameters for org.onap.policy.common.parameters.LegalParameters not found in parameter service", - e.getMessage()); + assertEquals("\"Empty Group\" not found in parameter service", e.getMessage()); } } } diff --git a/common-parameters/src/test/java/org/onap/policy/common/parameters/TestValidation.java b/common-parameters/src/test/java/org/onap/policy/common/parameters/TestValidation.java new file mode 100644 index 00000000..2ca24a05 --- /dev/null +++ b/common-parameters/src/test/java/org/onap/policy/common/parameters/TestValidation.java @@ -0,0 +1,183 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2018 Ericsson. 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========================================================= + */ + +package org.onap.policy.common.parameters; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Paths; + +import org.junit.Test; +import org.onap.policy.common.parameters.testclasses.TestParametersL00; + +public class TestValidation { + @Test + public void testValidationOk() throws IOException { + TestParametersL00 l0Parameters = new TestParametersL00("l0Parameters"); + + GroupValidationResult validationResult = l0Parameters.validate(); + assertTrue(validationResult.isValid()); + assertNull(validationResult.getResult()); + assertEquals(l0Parameters, validationResult.getParameterGroup()); + assertEquals(l0Parameters.getName(), validationResult.getName()); + + String expectedResult = new String(Files.readAllBytes( + Paths.get("src/test/resources/expectedValidationResults/TestParametersL0_0_OK.txt"))) + .replaceAll("\\s+", ""); + assertEquals(expectedResult, validationResult.getResult("", " ", true).replaceAll("\\s+", "")); + } + + @Test + public void testValidationObservation() throws IOException { + TestParametersL00 l0Parameters = new TestParametersL00("l0Parameters"); + + l0Parameters.triggerValidationStatus(ValidationStatus.OBSERVATION, 3); + + String expectedResult = new String(Files.readAllBytes( + Paths.get("src/test/resources/expectedValidationResults/TestParametersL0_3_Observation.txt"))) + .replaceAll("\\s+", ""); + + GroupValidationResult validationResult = l0Parameters.validate(); + assertTrue(validationResult.isValid()); + assertEquals(expectedResult, validationResult.getResult().replaceAll("\\s+", "")); + + l0Parameters.triggerValidationStatus(ValidationStatus.CLEAN, 3); + l0Parameters.triggerValidationStatus(ValidationStatus.OBSERVATION, 2); + + expectedResult = new String(Files.readAllBytes( + Paths.get("src/test/resources/expectedValidationResults/TestParametersL0_2_Observation.txt"))) + .replaceAll("\\s+", ""); + + validationResult = l0Parameters.validate(); + assertTrue(validationResult.isValid()); + assertEquals(expectedResult, validationResult.getResult().replaceAll("\\s+", "")); + + l0Parameters.triggerValidationStatus(ValidationStatus.CLEAN, 3); + l0Parameters.triggerValidationStatus(ValidationStatus.OBSERVATION, 1); + + expectedResult = new String(Files.readAllBytes( + Paths.get("src/test/resources/expectedValidationResults/TestParametersL0_1_Observation.txt"))) + .replaceAll("\\s+", ""); + + validationResult = l0Parameters.validate(); + assertTrue(validationResult.isValid()); + assertEquals(expectedResult, validationResult.getResult().replaceAll("\\s+", "")); + + l0Parameters.triggerValidationStatus(ValidationStatus.CLEAN, 3); + l0Parameters.triggerValidationStatus(ValidationStatus.OBSERVATION, 0); + + validationResult = l0Parameters.validate(); + assertTrue(validationResult.isValid()); + assertEquals(null, validationResult.getResult()); + } + + @Test + public void testValidationWarning() throws IOException { + TestParametersL00 l0Parameters = new TestParametersL00("l0Parameters"); + + l0Parameters.triggerValidationStatus(ValidationStatus.WARNING, 3); + + String expectedResult = new String(Files.readAllBytes( + Paths.get("src/test/resources/expectedValidationResults/TestParametersL0_3_Warning.txt"))) + .replaceAll("\\s+", ""); + + GroupValidationResult validationResult = l0Parameters.validate(); + assertTrue(validationResult.isValid()); + assertEquals(expectedResult, validationResult.getResult().replaceAll("\\s+", "")); + + l0Parameters.triggerValidationStatus(ValidationStatus.CLEAN, 3); + l0Parameters.triggerValidationStatus(ValidationStatus.WARNING, 2); + + expectedResult = new String(Files.readAllBytes( + Paths.get("src/test/resources/expectedValidationResults/TestParametersL0_2_Warning.txt"))) + .replaceAll("\\s+", ""); + + validationResult = l0Parameters.validate(); + assertTrue(validationResult.isValid()); + assertEquals(expectedResult, validationResult.getResult().replaceAll("\\s+", "")); + + l0Parameters.triggerValidationStatus(ValidationStatus.CLEAN, 3); + l0Parameters.triggerValidationStatus(ValidationStatus.WARNING, 1); + + expectedResult = new String(Files.readAllBytes( + Paths.get("src/test/resources/expectedValidationResults/TestParametersL0_1_Warning.txt"))) + .replaceAll("\\s+", ""); + + validationResult = l0Parameters.validate(); + assertTrue(validationResult.isValid()); + assertEquals(expectedResult, validationResult.getResult().replaceAll("\\s+", "")); + + l0Parameters.triggerValidationStatus(ValidationStatus.CLEAN, 3); + l0Parameters.triggerValidationStatus(ValidationStatus.WARNING, 0); + + validationResult = l0Parameters.validate(); + assertTrue(validationResult.isValid()); + assertEquals(null, validationResult.getResult()); + } + + @Test + public void testValidationInvalid() throws IOException { + TestParametersL00 l0Parameters = new TestParametersL00("l0Parameters"); + + l0Parameters.triggerValidationStatus(ValidationStatus.INVALID, 3); + + String expectedResult = new String(Files.readAllBytes( + Paths.get("src/test/resources/expectedValidationResults/TestParametersL0_3_Invalid.txt"))) + .replaceAll("\\s+", ""); + + GroupValidationResult validationResult = l0Parameters.validate(); + assertFalse(validationResult.isValid()); + assertEquals(expectedResult, validationResult.getResult().replaceAll("\\s+", "")); + + l0Parameters.triggerValidationStatus(ValidationStatus.CLEAN, 3); + l0Parameters.triggerValidationStatus(ValidationStatus.INVALID, 2); + + expectedResult = new String(Files.readAllBytes( + Paths.get("src/test/resources/expectedValidationResults/TestParametersL0_2_Invalid.txt"))) + .replaceAll("\\s+", ""); + + validationResult = l0Parameters.validate(); + assertFalse(validationResult.isValid()); + assertEquals(expectedResult, validationResult.getResult().replaceAll("\\s+", "")); + + l0Parameters.triggerValidationStatus(ValidationStatus.CLEAN, 3); + l0Parameters.triggerValidationStatus(ValidationStatus.INVALID, 1); + + expectedResult = new String(Files.readAllBytes( + Paths.get("src/test/resources/expectedValidationResults/TestParametersL0_1_Invalid.txt"))) + .replaceAll("\\s+", ""); + + validationResult = l0Parameters.validate(); + assertFalse(validationResult.isValid()); + assertEquals(expectedResult, validationResult.getResult().replaceAll("\\s+", "")); + + l0Parameters.triggerValidationStatus(ValidationStatus.CLEAN, 3); + l0Parameters.triggerValidationStatus(ValidationStatus.INVALID, 0); + + validationResult = l0Parameters.validate(); + assertTrue(validationResult.isValid()); + assertEquals(null, validationResult.getResult()); + } +} diff --git a/common-parameters/src/test/java/org/onap/policy/common/parameters/TestValidationErrors.java b/common-parameters/src/test/java/org/onap/policy/common/parameters/TestValidationErrors.java new file mode 100644 index 00000000..6b781472 --- /dev/null +++ b/common-parameters/src/test/java/org/onap/policy/common/parameters/TestValidationErrors.java @@ -0,0 +1,105 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2018 Ericsson. 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========================================================= + */ + +package org.onap.policy.common.parameters; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import org.junit.Test; +import org.onap.policy.common.parameters.testclasses.ParameterGroupWithArray; +import org.onap.policy.common.parameters.testclasses.ParameterGroupWithCollection; +import org.onap.policy.common.parameters.testclasses.ParameterGroupWithIllegalMapKey; +import org.onap.policy.common.parameters.testclasses.ParameterGroupWithIllegalMapValue; +import org.onap.policy.common.parameters.testclasses.ParameterGroupWithNullCollection; +import org.onap.policy.common.parameters.testclasses.ParameterGroupWithNullMapValue; +import org.onap.policy.common.parameters.testclasses.ParameterGroupWithParameterGroupCollection; + +public class TestValidationErrors { + @Test + public void testBadArrayValidation() { + ParameterGroupWithArray groupWithArray = new ParameterGroupWithArray("Illegal Array Group"); + assertTrue(groupWithArray.isValid()); + } + + @Test + public void testCollectionValidation() { + ParameterGroupWithCollection legalCollection = new ParameterGroupWithCollection("Legal Collection"); + assertTrue(legalCollection.isValid()); + + ParameterGroupWithParameterGroupCollection illegalCollection = new ParameterGroupWithParameterGroupCollection( + "Illegal Collection"); + try { + illegalCollection.isValid(); + fail("test should throw an exception"); + } catch (ParameterRuntimeException e) { + assertEquals("collection parameter \"parameterGroupArrayList\" is illegal," + + " parameter groups are not allowed as collection members", e.getMessage()); + } + } + + @Test + public void testNullCollection() { + ParameterGroupWithNullCollection nullCollection = new ParameterGroupWithNullCollection("Null Collection"); + + try { + nullCollection.isValid(); + fail("test should throw an exception"); + } catch (ParameterRuntimeException e) { + assertEquals("collection parameter \"nullList\" is null", e.getMessage()); + } + } + + @Test + public void testMapNullValueValidation() { + ParameterGroupWithNullMapValue nullMap = new ParameterGroupWithNullMapValue("Null Map value"); + try { + nullMap.isValid(); + fail("test should throw an exception"); + } catch (ParameterRuntimeException e) { + assertEquals("map parameter \"nullMap\" is null", e.getMessage()); + } + } + + @Test + public void testBadMapKeyValidation() { + ParameterGroupWithIllegalMapKey illegalMap = new ParameterGroupWithIllegalMapKey("Illegal Map"); + try { + illegalMap.isValid(); + fail("test should throw an exception"); + } catch (ParameterRuntimeException e) { + assertEquals("map entry is not a parameter group keyed by a string, key \"1\" " + + "in map \"badMap\" is not a string", e.getMessage()); + } + } + + @Test + public void testBadMapValueValidation() { + ParameterGroupWithIllegalMapValue illegalMap = new ParameterGroupWithIllegalMapValue("Illegal Map"); + try { + illegalMap.isValid(); + fail("test should throw an exception"); + } catch (ParameterRuntimeException e) { + assertEquals("map entry is not a parameter group keyed by a string, value \"1\" in " + + "map \"intMap\" is not a parameter group", e.getMessage()); + } + } +} diff --git a/common-parameters/src/test/java/org/onap/policy/common/parameters/TestValidationResults.java b/common-parameters/src/test/java/org/onap/policy/common/parameters/TestValidationResults.java new file mode 100644 index 00000000..8f22765b --- /dev/null +++ b/common-parameters/src/test/java/org/onap/policy/common/parameters/TestValidationResults.java @@ -0,0 +1,147 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2018 Ericsson. 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========================================================= + */ + +package org.onap.policy.common.parameters; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import java.util.LinkedHashMap; +import java.util.Map; + +import org.junit.Before; +import org.junit.Test; +import org.onap.policy.common.parameters.testclasses.TestParametersL10; +import org.onap.policy.common.parameters.testclasses.TestParametersLGeneric; + +public class TestValidationResults { + private Map<String, ParameterGroup> pgMap = new LinkedHashMap<>(); + private ParameterGroup pg = new TestParametersL10("pg"); + + @Before + public void initMap() { + pgMap.put("entry0", new TestParametersLGeneric("entry0")); + } + + @Test + public void testGroupMapValidationResult() throws NoSuchFieldException, SecurityException { + GroupMapValidationResult result = new GroupMapValidationResult(this.getClass().getDeclaredField("pgMap"), + pgMap); + + assertTrue(result.isValid()); + assertEquals("pgMap", result.getName()); + + result.setResult(ValidationStatus.OBSERVATION); + assertTrue(result.isValid()); + assertEquals(ValidationStatus.OBSERVATION, result.getStatus()); + + // Once the status is stepped, it can't be reset back because it is the status of map members + result.setResult(ValidationStatus.CLEAN); + assertTrue(result.isValid()); + assertEquals(ValidationStatus.OBSERVATION, result.getStatus()); + + result.setResult(ValidationStatus.OBSERVATION, "Something was observed"); + assertTrue(result.isValid()); + assertEquals(ValidationStatus.OBSERVATION, result.getStatus()); + assertEquals("parameter group map \"pgMap\" OBSERVATION, Something was observed", result.getResult().trim()); + + result.setResult("entry0", new GroupValidationResult(pgMap.get("entry0"))); + assertTrue(result.isValid()); + assertEquals(ValidationStatus.OBSERVATION, result.getStatus()); + assertEquals("parameter group map \"pgMap\" OBSERVATION, Something was observed", result.getResult().trim()); + + try { + result.setResult("nonExistantEntry", new GroupValidationResult(pgMap.get("entry0"))); + fail("test shold throw an exception here"); + } catch (Exception e) { + assertEquals("no entry with name \"nonExistantEntry\" exists", e.getMessage()); + } + } + + @Test + public void testGroupValidationResult() throws NoSuchFieldException, SecurityException { + GroupValidationResult result = new GroupValidationResult(pg); + + assertTrue(result.isValid()); + assertEquals(pg, result.getParameterGroup()); + assertEquals("pg", result.getName()); + + result.setResult(ValidationStatus.OBSERVATION); + assertTrue(result.isValid()); + assertEquals(ValidationStatus.OBSERVATION, result.getStatus()); + + // Once the status is stepped, it can't be reset back because it is the status of map members + result.setResult(ValidationStatus.CLEAN); + assertTrue(result.isValid()); + assertEquals(ValidationStatus.OBSERVATION, result.getStatus()); + + result.setResult(ValidationStatus.OBSERVATION, "Something was observed"); + assertTrue(result.isValid()); + assertEquals(ValidationStatus.OBSERVATION, result.getStatus()); + assertEquals("parameter group \"pg\" type \"org.onap.policy.common.parameters.testclasses.TestParametersL10\"" + + " OBSERVATION, Something was observed", result.getResult().trim()); + + try { + result.setResult("nonExistantParameter", ValidationStatus.OBSERVATION, "Something was observed"); + fail("test shold throw an exception here"); + } catch (Exception e) { + assertEquals("no regular parameter field exists for parameter: nonExistantParameter", e.getMessage()); + } + + try { + result.setResult("l10LGenericNestedMap", ValidationStatus.OBSERVATION, "Something was observed"); + fail("test shold throw an exception here"); + } catch (Exception e) { + assertEquals("parameter not a regular parameter: l10LGenericNestedMap", e.getMessage()); + } + + try { + result.setResult("nonExistantParameter", new GroupValidationResult(pg)); + fail("test shold throw an exception here"); + } catch (Exception e) { + assertEquals("no nested parameter field exists for parameter: nonExistantParameter", e.getMessage()); + } + + try { + result.setResult("l10IntField", new GroupValidationResult(pg)); + fail("test shold throw an exception here"); + } catch (Exception e) { + assertEquals("parameter is not a nested group parameter: l10IntField", e.getMessage()); + } + + GroupMapValidationResult groupMapResult = new GroupMapValidationResult( + this.getClass().getDeclaredField("pgMap"), pgMap); + + try { + result.setResult("nonExistantParameter", "entry0", groupMapResult); + fail("test shold throw an exception here"); + } catch (Exception e) { + assertEquals("no group map parameter field exists for parameter: nonExistantParameter", e.getMessage()); + } + + try { + result.setResult("l10IntField", "entry0", groupMapResult); + fail("test shold throw an exception here"); + } catch (Exception e) { + assertEquals("parameter is not a nested group map parameter: l10IntField", e.getMessage()); + } + } +} diff --git a/common-parameters/src/test/java/org/onap/policy/common/parameters/TestYamlInput.java b/common-parameters/src/test/java/org/onap/policy/common/parameters/TestYamlInput.java new file mode 100644 index 00000000..7d803525 --- /dev/null +++ b/common-parameters/src/test/java/org/onap/policy/common/parameters/TestYamlInput.java @@ -0,0 +1,59 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2018 Ericsson. 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========================================================= + */ + +package org.onap.policy.common.parameters; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import java.io.FileReader; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Paths; + +import org.junit.Test; +import org.onap.policy.common.parameters.testclasses.TestParametersL00; +import org.yaml.snakeyaml.Yaml; + +public class TestYamlInput { + @Test + public void testYamlInput() throws IOException { + TestParametersL00 testParameterGroup = null; + + // Read the parameters + try { + // Read the parameters from JSON using Gson + final Yaml yaml = new Yaml(); + testParameterGroup = yaml.loadAs(new FileReader("src/test/resources/parameters/TestParameters.yaml"), TestParametersL00.class); + } catch (final Exception e) { + fail("test should not throw an exception here: " + e.getMessage()); + } + + GroupValidationResult validationResult = testParameterGroup.validate(); + assertTrue(validationResult.isValid()); + assertEquals("l00NameFromFile", testParameterGroup.getName()); + + String expectedResult = new String(Files.readAllBytes( + Paths.get("src/test/resources/expectedValidationResults/TestJsonYamlValidationResult.txt"))) + .replaceAll("\\s+", ""); + assertEquals(expectedResult, validationResult.getResult("", " ", true).replaceAll("\\s+", "")); + } +} diff --git a/common-parameters/src/main/java/org/onap/policy/common/parameters/ParameterValidator.java b/common-parameters/src/test/java/org/onap/policy/common/parameters/testclasses/EmptyParameterGroup.java index 0d718267..f545ce65 100644 --- a/common-parameters/src/main/java/org/onap/policy/common/parameters/ParameterValidator.java +++ b/common-parameters/src/test/java/org/onap/policy/common/parameters/testclasses/EmptyParameterGroup.java @@ -18,19 +18,25 @@ * ============LICENSE_END========================================================= */ -package org.onap.policy.common.parameters; +package org.onap.policy.common.parameters.testclasses; -/** - * This interface is implemented by ONAP PF parameter classes so that they can be validated. - * - * @author Liam Fallon (liam.fallon@ericsson.com) - */ -public interface ParameterValidator { - /** - * Validate a parameter java bean, if the parameter bean is valid, an empty string is returned, - * otherwise the string gives details of the invalid parameters. - * - * @return the string with validation errors - */ - String validate(); +import org.onap.policy.common.parameters.ParameterGroup; +import org.onap.policy.common.parameters.GroupValidationResult; + +public class EmptyParameterGroup implements ParameterGroup { + private String name; + + public EmptyParameterGroup(final String name) { + this.name = name; + } + + @Override + public String getName() { + return name; + } + + @Override + public GroupValidationResult validate() { + return new GroupValidationResult(this); + } } diff --git a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/event/comm/bus/internal/impl/DmaapAafPublisherWrapper.java b/common-parameters/src/test/java/org/onap/policy/common/parameters/testclasses/ParameterGroupWithArray.java index 1308bb36..cdab1272 100644 --- a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/event/comm/bus/internal/impl/DmaapAafPublisherWrapper.java +++ b/common-parameters/src/test/java/org/onap/policy/common/parameters/testclasses/ParameterGroupWithArray.java @@ -1,8 +1,6 @@ -/* +/*- * ============LICENSE_START======================================================= - * policy-endpoints - * ================================================================================ - * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2018 Ericsson. 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. @@ -15,25 +13,35 @@ * 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========================================================= */ -package org.onap.policy.common.endpoints.event.comm.bus.internal.impl; +package org.onap.policy.common.parameters.testclasses; -import com.att.nsa.mr.test.clients.ProtocolTypeConstants; +import org.onap.policy.common.parameters.ParameterGroup; +import org.onap.policy.common.parameters.GroupValidationResult; -import java.util.List; +public class ParameterGroupWithArray implements ParameterGroup { + private String name; + private int[] intArray = {1, 2, 3}; -/** - * DmaapClient library wrapper - */ -public class DmaapAafPublisherWrapper extends DmaapPublisherWrapper { - /** - * MR based Publisher - */ - public DmaapAafPublisherWrapper(List<String> servers, String topic, String aafLogin, String aafPassword, - boolean useHttps) { + public ParameterGroupWithArray(final String name) { + this.name = name; + } - super(ProtocolTypeConstants.AAF_AUTH, servers, topic, aafLogin, aafPassword, useHttps); + public int[] getIntArray() { + return intArray; + } + + @Override + public String getName() { + return name; + } + + @Override + public GroupValidationResult validate() { + return new GroupValidationResult(this); } } diff --git a/common-parameters/src/test/java/org/onap/policy/common/parameters/testclasses/ParameterGroupWithCollection.java b/common-parameters/src/test/java/org/onap/policy/common/parameters/testclasses/ParameterGroupWithCollection.java new file mode 100644 index 00000000..0f993099 --- /dev/null +++ b/common-parameters/src/test/java/org/onap/policy/common/parameters/testclasses/ParameterGroupWithCollection.java @@ -0,0 +1,58 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2018 Ericsson. 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========================================================= + */ + +package org.onap.policy.common.parameters.testclasses; + +import java.util.ArrayList; +import java.util.List; + +import org.onap.policy.common.parameters.ParameterGroup; +import org.onap.policy.common.parameters.GroupValidationResult; + +public class ParameterGroupWithCollection implements ParameterGroup { + private String name; + private List<Integer> intArrayList = new ArrayList<>(); + + /** + * Create a test parameter group. + * @param name the parameter group name + */ + public ParameterGroupWithCollection(final String name) { + this.name = name; + + intArrayList.add(1); + intArrayList.add(2); + intArrayList.add(3); + } + + public List<Integer> getIntArrayList() { + return intArrayList; + } + + @Override + public String getName() { + return name; + } + + @Override + public GroupValidationResult validate() { + return new GroupValidationResult(this); + } +} diff --git a/common-parameters/src/test/java/org/onap/policy/common/parameters/testclasses/ParameterGroupWithIllegalMapKey.java b/common-parameters/src/test/java/org/onap/policy/common/parameters/testclasses/ParameterGroupWithIllegalMapKey.java new file mode 100644 index 00000000..3de3270e --- /dev/null +++ b/common-parameters/src/test/java/org/onap/policy/common/parameters/testclasses/ParameterGroupWithIllegalMapKey.java @@ -0,0 +1,59 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2018 Ericsson. 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========================================================= + */ + +package org.onap.policy.common.parameters.testclasses; + +import java.util.LinkedHashMap; +import java.util.Map; + +import org.onap.policy.common.parameters.ParameterGroup; +import org.onap.policy.common.parameters.GroupValidationResult; + +public class ParameterGroupWithIllegalMapKey implements ParameterGroup { + private String name; + private Map<Integer, ParameterGroup> badMap = new LinkedHashMap<>(); + + /** + * Create a test parameter group. + * @param name the parameter group name + */ + public ParameterGroupWithIllegalMapKey(final String name) { + this.name = name; + + badMap.put(1, new TestParametersLGeneric("One")); + badMap.put(2, new TestParametersLGeneric("Two")); + badMap.put(3, new TestParametersLGeneric("Three")); + } + + public Map<Integer, ParameterGroup> getBadMap() { + return badMap; + } + + @Override + public String getName() { + return name; + } + + @Override + public GroupValidationResult validate() { + return new GroupValidationResult(this); + } + +} diff --git a/common-parameters/src/test/java/org/onap/policy/common/parameters/testclasses/ParameterGroupWithIllegalMapValue.java b/common-parameters/src/test/java/org/onap/policy/common/parameters/testclasses/ParameterGroupWithIllegalMapValue.java new file mode 100644 index 00000000..8eb697a4 --- /dev/null +++ b/common-parameters/src/test/java/org/onap/policy/common/parameters/testclasses/ParameterGroupWithIllegalMapValue.java @@ -0,0 +1,58 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2018 Ericsson. 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========================================================= + */ + +package org.onap.policy.common.parameters.testclasses; + +import java.util.LinkedHashMap; +import java.util.Map; + +import org.onap.policy.common.parameters.ParameterGroup; +import org.onap.policy.common.parameters.GroupValidationResult; + +public class ParameterGroupWithIllegalMapValue implements ParameterGroup { + private String name; + private Map<String, Integer> intMap = new LinkedHashMap<>(); + + /** + * Create a test parameter group. + * @param name the parameter group name + */ + public ParameterGroupWithIllegalMapValue(final String name) { + this.name = name; + + intMap.put("One", 1); + intMap.put("Two", 2); + intMap.put("Three", 3); + } + + public Map<String, Integer> getIntMap() { + return intMap; + } + + @Override + public String getName() { + return name; + } + + @Override + public GroupValidationResult validate() { + return new GroupValidationResult(this); + } +} diff --git a/common-parameters/src/test/java/org/onap/policy/common/parameters/testclasses/ParameterGroupWithNullCollection.java b/common-parameters/src/test/java/org/onap/policy/common/parameters/testclasses/ParameterGroupWithNullCollection.java new file mode 100644 index 00000000..37399da3 --- /dev/null +++ b/common-parameters/src/test/java/org/onap/policy/common/parameters/testclasses/ParameterGroupWithNullCollection.java @@ -0,0 +1,53 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2018 Ericsson. 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========================================================= + */ + +package org.onap.policy.common.parameters.testclasses; + +import java.util.List; + +import org.onap.policy.common.parameters.ParameterGroup; +import org.onap.policy.common.parameters.GroupValidationResult; + +public class ParameterGroupWithNullCollection implements ParameterGroup { + private String name; + private List<Integer> nullList = null; + + /** + * Create a test parameter group. + * @param name the parameter group name + */ + public ParameterGroupWithNullCollection(final String name) { + this.name = name; + } + + public List<Integer> getNullList() { + return nullList; + } + + @Override + public String getName() { + return name; + } + + @Override + public GroupValidationResult validate() { + return new GroupValidationResult(this); + } +} diff --git a/common-parameters/src/test/java/org/onap/policy/common/parameters/testclasses/ParameterGroupWithNullMapValue.java b/common-parameters/src/test/java/org/onap/policy/common/parameters/testclasses/ParameterGroupWithNullMapValue.java new file mode 100644 index 00000000..80f55355 --- /dev/null +++ b/common-parameters/src/test/java/org/onap/policy/common/parameters/testclasses/ParameterGroupWithNullMapValue.java @@ -0,0 +1,53 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2018 Ericsson. 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========================================================= + */ + +package org.onap.policy.common.parameters.testclasses; + +import java.util.Map; + +import org.onap.policy.common.parameters.ParameterGroup; +import org.onap.policy.common.parameters.GroupValidationResult; + +public class ParameterGroupWithNullMapValue implements ParameterGroup { + private String name; + private Map<String, Integer> nullMap = null; + + /** + * Create a test parameter group. + * @param name the parameter group name + */ + public ParameterGroupWithNullMapValue(final String name) { + this.name = name; + } + + public Map<String, Integer> getNullMap() { + return nullMap; + } + + @Override + public String getName() { + return name; + } + + @Override + public GroupValidationResult validate() { + return new GroupValidationResult(this); + } +} diff --git a/common-parameters/src/test/java/org/onap/policy/common/parameters/testclasses/ParameterGroupWithParameterGroupCollection.java b/common-parameters/src/test/java/org/onap/policy/common/parameters/testclasses/ParameterGroupWithParameterGroupCollection.java new file mode 100644 index 00000000..e7d1de75 --- /dev/null +++ b/common-parameters/src/test/java/org/onap/policy/common/parameters/testclasses/ParameterGroupWithParameterGroupCollection.java @@ -0,0 +1,58 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2018 Ericsson. 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========================================================= + */ + +package org.onap.policy.common.parameters.testclasses; + +import java.util.ArrayList; +import java.util.List; + +import org.onap.policy.common.parameters.ParameterGroup; +import org.onap.policy.common.parameters.GroupValidationResult; + +public class ParameterGroupWithParameterGroupCollection implements ParameterGroup { + private String name; + private List<ParameterGroup> parameterGroupArrayList = new ArrayList<>(); + + /** + * Create a test parameter group. + * @param name the parameter group name + */ + public ParameterGroupWithParameterGroupCollection(final String name) { + this.name = name; + + parameterGroupArrayList.add(new TestParametersLGeneric("Generic0")); + parameterGroupArrayList.add(new TestParametersLGeneric("Generic1")); + parameterGroupArrayList.add(new TestParametersLGeneric("Generic2")); + } + + public List<ParameterGroup> getIntArrayList() { + return parameterGroupArrayList; + } + + @Override + public String getName() { + return name; + } + + @Override + public GroupValidationResult validate() { + return new GroupValidationResult(this); + } +} diff --git a/common-parameters/src/test/java/org/onap/policy/common/parameters/testclasses/TestParametersL00.java b/common-parameters/src/test/java/org/onap/policy/common/parameters/testclasses/TestParametersL00.java new file mode 100644 index 00000000..6b8460cd --- /dev/null +++ b/common-parameters/src/test/java/org/onap/policy/common/parameters/testclasses/TestParametersL00.java @@ -0,0 +1,177 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2018 Ericsson. 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========================================================= + */ + +package org.onap.policy.common.parameters.testclasses; + +import java.util.LinkedHashMap; +import java.util.Map; +import java.util.Map.Entry; + +import org.onap.policy.common.parameters.ParameterGroup; +import org.onap.policy.common.parameters.GroupValidationResult; +import org.onap.policy.common.parameters.ParameterConstants; +import org.onap.policy.common.parameters.ValidationStatus; + +public class TestParametersL00 implements ParameterGroup { + private String name; + private int l00IntField = 0; + private String l00StringField = "Legal " + this.getClass().getCanonicalName(); + private TestParametersL10 l00L10Nested = new TestParametersL10("l00L10Nested"); + private TestParametersLGeneric l00LGenericNested = new TestParametersLGeneric("l00LGenericNested"); + private Map<String, TestParametersLGeneric> l00LGenericNestedMap = new LinkedHashMap<>(); + + /** + * Default constructor + */ + public TestParametersL00() { + } + + /** + * Create a test parameter group. + * + * @param name the parameter group name + */ + public TestParametersL00(final String name) { + this.name = name; + + TestParametersLGeneric l00LGenericNestedMapVal0 = new TestParametersLGeneric("l00LGenericNestedMapVal0"); + l00LGenericNestedMap.put(l00LGenericNestedMapVal0.getName(), l00LGenericNestedMapVal0); + TestParametersLGeneric l00LGenericNestedMapVal1 = new TestParametersLGeneric("l00LGenericNestedMapVal1"); + l00LGenericNestedMap.put(l00LGenericNestedMapVal1.getName(), l00LGenericNestedMapVal1); + } + + public void setName(String name) { + this.name = name; + } + + public void setL00IntField(int l00IntField) { + this.l00IntField = l00IntField; + } + + public void setL00StringField(String l00StringField) { + this.l00StringField = l00StringField; + } + + public void setL00L10Nested(TestParametersL10 l00l10Nested) { + l00L10Nested = l00l10Nested; + } + + public void setL00LGenericNested(TestParametersLGeneric l00lGenericNested) { + l00LGenericNested = l00lGenericNested; + } + + public void setL00LGenericNestedMap(Map<String, TestParametersLGeneric> l00lGenericNestedMap) { + l00LGenericNestedMap = l00lGenericNestedMap; + } + + /** + * Trigger a validation message. + * + * @param triggerStatus Validation status to trigger + * @param level Number of levels to recurse before stopping + */ + public void triggerValidationStatus(final ValidationStatus triggerStatus, int level) { + if (level == 0) { + return; + } else { + level--; + } + + switch (triggerStatus) { + case CLEAN: + l00StringField = "Legal " + this.getClass().getCanonicalName(); + l00IntField = 0; + break; + case OBSERVATION: + l00StringField = "aString"; + l00IntField = 2; + break; + case WARNING: + l00StringField = "l00StringField"; + l00IntField = 3; + break; + case INVALID: + l00StringField = ""; + l00IntField = -1; + break; + default: + break; + } + + l00L10Nested.triggerValidationStatus(triggerStatus, level); + l00LGenericNested.triggerValidationStatus(triggerStatus, level); + + for (TestParametersLGeneric nestedParameterGroup : l00LGenericNestedMap.values()) { + nestedParameterGroup.triggerValidationStatus(triggerStatus, level); + } + + } + + @Override + public String getName() { + return name; + } + + @Override + public GroupValidationResult validate() { + GroupValidationResult validationResult = new GroupValidationResult(this); + + if (name == null || name.trim().length() == 0) { + validationResult.setResult("name", ValidationStatus.INVALID, "name must be a non-blank string"); + } + + if (l00StringField == null || l00StringField.trim().length() == 0) { + validationResult.setResult("l00StringField", ValidationStatus.INVALID, + "l00StringField must be a non-blank string"); + } else if (l00StringField.equals("l00StringField")) { + validationResult.setResult("l00StringField", ValidationStatus.WARNING, + "using the field name for the parameter value is dangerous"); + } else if (l00StringField.equals("aString")) { + validationResult.setResult("l00StringField", ValidationStatus.OBSERVATION, + "this value for name is unhelpful"); + } else { + validationResult.setResult("l00StringField", ValidationStatus.CLEAN, + ParameterConstants.PARAMETER_HAS_STATUS_MESSAGE + ValidationStatus.CLEAN.toString()); + } + + if (l00IntField < 0) { + validationResult.setResult("l00IntField", ValidationStatus.INVALID, + "l00IntField must be a positive integer"); + } else if (l00IntField > 2) { + validationResult.setResult("l00IntField", ValidationStatus.WARNING, + "values greater than 2 are not recommended"); + } else if (l00IntField == 2) { + validationResult.setResult("l00IntField", ValidationStatus.OBSERVATION, "this field has been set to 2"); + } else { + validationResult.setResult("l00IntField", ValidationStatus.CLEAN, + ParameterConstants.PARAMETER_HAS_STATUS_MESSAGE + ValidationStatus.CLEAN.toString()); + } + + validationResult.setResult("l00L10Nested", l00L10Nested.validate()); + validationResult.setResult("l00LGenericNested", l00LGenericNested.validate()); + + for (Entry<String, TestParametersLGeneric> nestedGroupEntry : l00LGenericNestedMap.entrySet()) { + validationResult.setResult("l00LGenericNestedMap", nestedGroupEntry.getKey(), + nestedGroupEntry.getValue().validate()); + } + + return validationResult; + } +} diff --git a/common-parameters/src/test/java/org/onap/policy/common/parameters/testclasses/TestParametersL10.java b/common-parameters/src/test/java/org/onap/policy/common/parameters/testclasses/TestParametersL10.java new file mode 100644 index 00000000..94a67ec7 --- /dev/null +++ b/common-parameters/src/test/java/org/onap/policy/common/parameters/testclasses/TestParametersL10.java @@ -0,0 +1,173 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2018 Ericsson. 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========================================================= + */ + +package org.onap.policy.common.parameters.testclasses; + +import java.util.LinkedHashMap; +import java.util.Map; +import java.util.Map.Entry; + +import org.onap.policy.common.parameters.ParameterGroup; +import org.onap.policy.common.parameters.GroupValidationResult; +import org.onap.policy.common.parameters.ParameterConstants; +import org.onap.policy.common.parameters.ValidationStatus; + +public class TestParametersL10 implements ParameterGroup { + private String name; + private int l10IntField = 0; + private String l10StringField = "Legal " + this.getClass().getCanonicalName(); + private TestParametersLGeneric l10LGenericNested0 = new TestParametersLGeneric("l10LGenericNested0"); + private TestParametersLGeneric l10LGenericNested1 = new TestParametersLGeneric("l10LGenericNested1"); + private Map<String, TestParametersLGeneric> l10LGenericNestedMap = new LinkedHashMap<>(); + + /** + * Default constructor + */ + public TestParametersL10() { + } + + /** + * Create a test parameter group. + * + * @param name the parameter group name + */ + public TestParametersL10(final String name) { + this.name = name; + + TestParametersLGeneric l10LGenericNestedMapVal0 = new TestParametersLGeneric("l10LGenericNestedMapVal0"); + l10LGenericNestedMap.put(l10LGenericNestedMapVal0.getName(), l10LGenericNestedMapVal0); + TestParametersLGeneric l10LGenericNestedMapVal1 = new TestParametersLGeneric("l10LGenericNestedMapVal1"); + l10LGenericNestedMap.put(l10LGenericNestedMapVal1.getName(), l10LGenericNestedMapVal1); + } + + public void setName(String name) { + this.name = name; + } + + public void setL10IntField(int l10IntField) { + this.l10IntField = l10IntField; + } + + public void setL10StringField(String l10StringField) { + this.l10StringField = l10StringField; + } + + public void setL10LGenericNested0(TestParametersLGeneric l10lGenericNested0) { + l10LGenericNested0 = l10lGenericNested0; + } + + public void setL10LGenericNested1(TestParametersLGeneric l10lGenericNested1) { + l10LGenericNested1 = l10lGenericNested1; + } + + public void setL10LGenericNestedMap(Map<String, TestParametersLGeneric> l10lGenericNestedMap) { + l10LGenericNestedMap = l10lGenericNestedMap; + } + + /** + * Trigger a validation message. + * + * @param level Number of levels to recurse before stopping + */ + public void triggerValidationStatus(final ValidationStatus triggerStatus, int level) { + if (level == 0) { + return; + } + else { + level--; + } + + switch (triggerStatus) { + case CLEAN: + l10StringField = "Legal " + this.getClass().getCanonicalName(); + l10IntField = 0; + break; + case OBSERVATION: + l10StringField = "aString"; + l10IntField = 2; + break; + case WARNING: + l10StringField = "l10StringField"; + l10IntField = 3; + break; + case INVALID: + l10StringField = ""; + l10IntField = -1; + break; + default: + break; + } + + l10LGenericNested0.triggerValidationStatus(triggerStatus, level); + l10LGenericNested1.triggerValidationStatus(triggerStatus, level); + + for (TestParametersLGeneric nestedParameterGroup : l10LGenericNestedMap.values()) { + nestedParameterGroup.triggerValidationStatus(triggerStatus, level); + } + } + + @Override + public String getName() { + return this.name; + } + + @Override + public GroupValidationResult validate() { + GroupValidationResult validationResult = new GroupValidationResult(this); + + if (l10StringField == null || l10StringField.trim().length() == 0) { + validationResult.setResult("l10StringField", ValidationStatus.INVALID, + "l10StringField must be a non-blank string"); + } else if (l10StringField.equals("l10StringField")) { + validationResult.setResult("l10StringField", ValidationStatus.WARNING, + "using the field name for the parameter value is dangerous"); + } else if (l10StringField.equals("aString")) { + validationResult.setResult("l10StringField", ValidationStatus.OBSERVATION, + "this value for name is unhelpful"); + } else { + validationResult.setResult("l10StringField", ValidationStatus.CLEAN, + ParameterConstants.PARAMETER_HAS_STATUS_MESSAGE + ValidationStatus.CLEAN.toString()); + } + + if (l10IntField < 0) { + validationResult.setResult("l10IntField", ValidationStatus.INVALID, + "l10IntField must be a positive integer"); + } else if (l10IntField > 2) { + validationResult.setResult("l10IntField", ValidationStatus.WARNING, + "values greater than 2 are not recommended"); + } else if (l10IntField == 2) { + validationResult.setResult("l10IntField", ValidationStatus.OBSERVATION, "this field has been set to 2"); + } else { + validationResult.setResult("l10IntField", ValidationStatus.CLEAN, + ParameterConstants.PARAMETER_HAS_STATUS_MESSAGE + ValidationStatus.CLEAN.toString()); + } + + + validationResult.setResult("l10LGenericNested0", l10LGenericNested0.validate()); + validationResult.setResult("l10LGenericNested1", l10LGenericNested1.validate()); + + for (Entry<String, TestParametersLGeneric> nestedGroupEntry : l10LGenericNestedMap.entrySet()) { + validationResult.setResult("l10LGenericNestedMap", nestedGroupEntry.getKey(), + nestedGroupEntry.getValue().validate()); + } + + return validationResult; + } +} diff --git a/common-parameters/src/test/java/org/onap/policy/common/parameters/testclasses/TestParametersLGeneric.java b/common-parameters/src/test/java/org/onap/policy/common/parameters/testclasses/TestParametersLGeneric.java new file mode 100644 index 00000000..ce368bac --- /dev/null +++ b/common-parameters/src/test/java/org/onap/policy/common/parameters/testclasses/TestParametersLGeneric.java @@ -0,0 +1,135 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2018 Ericsson. 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========================================================= + */ + +package org.onap.policy.common.parameters.testclasses; + +import org.onap.policy.common.parameters.ParameterGroup; +import org.onap.policy.common.parameters.GroupValidationResult; +import org.onap.policy.common.parameters.ParameterConstants; +import org.onap.policy.common.parameters.ValidationStatus; + +public class TestParametersLGeneric implements ParameterGroup { + private String name; + private int lgenericIntField = 0; + private String lgenericStringField = "Legal " + this.getClass().getCanonicalName(); + + /** + * Default constructor + */ + public TestParametersLGeneric() { + } + + /** + * Create a test parameter group. + * + * @param name the parameter group name + */ + public TestParametersLGeneric(final String name) { + this.name = name; + } + + public void setName(String name) { + this.name = name; + } + + public void setLgenericIntField(int lgenericIntField) { + this.lgenericIntField = lgenericIntField; + } + + public void setLgenericStringField(String lgenericStringField) { + this.lgenericStringField = lgenericStringField; + } + + /** + * Trigger a validation message. + * + * @param level Number of levels to recurse before stopping + */ + public void triggerValidationStatus(final ValidationStatus triggerStatus, int level) { + if (level == 0) { + return; + } + else { + level--; + } + + switch (triggerStatus) { + case CLEAN: + lgenericStringField = "Legal " + this.getClass().getCanonicalName(); + lgenericIntField = 0; + break; + case OBSERVATION: + lgenericStringField = "aString"; + lgenericIntField = 2; + break; + case WARNING: + lgenericStringField = "lgenericStringField"; + lgenericIntField = 3; + break; + case INVALID: + lgenericStringField = ""; + lgenericIntField = -1; + break; + default: + break; + } + + } + + @Override + public String getName() { + return this.name; + } + + @Override + public GroupValidationResult validate() { + GroupValidationResult validationResult = new GroupValidationResult(this); + + if (lgenericStringField == null || lgenericStringField.trim().length() == 0) { + validationResult.setResult("lgenericStringField", ValidationStatus.INVALID, + "lgenericStringField must be a non-blank string"); + } else if (lgenericStringField.equals("lgenericStringField")) { + validationResult.setResult("lgenericStringField", ValidationStatus.WARNING, + "using the field name for the parameter value is dangerous"); + } else if (lgenericStringField.equals("aString")) { + validationResult.setResult("lgenericStringField", ValidationStatus.OBSERVATION, + "this value for name is unhelpful"); + } else { + validationResult.setResult("lgenericStringField", ValidationStatus.CLEAN, + ParameterConstants.PARAMETER_HAS_STATUS_MESSAGE + ValidationStatus.CLEAN.toString()); + } + + if (lgenericIntField < 0) { + validationResult.setResult("lgenericIntField", ValidationStatus.INVALID, + "lgenericIntField must be a positive integer"); + } else if (lgenericIntField > 2) { + validationResult.setResult("lgenericIntField", ValidationStatus.WARNING, + "values greater than 2 are not recommended"); + } else if (lgenericIntField == 2) { + validationResult.setResult("lgenericIntField", ValidationStatus.OBSERVATION, + "this field has been set to 2"); + } else { + validationResult.setResult("lgenericIntField", ValidationStatus.CLEAN, + ParameterConstants.PARAMETER_HAS_STATUS_MESSAGE + ValidationStatus.CLEAN.toString()); + } + + return validationResult; + } +} diff --git a/common-parameters/src/test/resources/expectedValidationResults/TestJsonYamlValidationResult.txt b/common-parameters/src/test/resources/expectedValidationResults/TestJsonYamlValidationResult.txt new file mode 100644 index 00000000..103321ff --- /dev/null +++ b/common-parameters/src/test/resources/expectedValidationResults/TestJsonYamlValidationResult.txt @@ -0,0 +1,39 @@ +parameter group "l00NameFromFile" type "org.onap.policy.common.parameters.testclasses.TestParametersL00" CLEAN, parameter group has status CLEAN + field "name" type "java.lang.String" value "l00NameFromFile" CLEAN, parameter has status CLEAN + field "l00IntField" type "int" value "1" CLEAN, parameter has status CLEAN + field "l00StringField" type "java.lang.String" value "l00 string field value from file" CLEAN, parameter has status CLEAN + parameter group "l00L10NestedNameFromFile" type "org.onap.policy.common.parameters.testclasses.TestParametersL10" CLEAN, parameter group has status CLEAN + field "name" type "java.lang.String" value "l00L10NestedNameFromFile" CLEAN, parameter has status CLEAN + field "l10IntField" type "int" value "1" CLEAN, parameter has status CLEAN + field "l10StringField" type "java.lang.String" value "l00 L10 nested string field value from file" CLEAN, parameter has status CLEAN + parameter group "l10LGenericNested0NameFromFile" type "org.onap.policy.common.parameters.testclasses.TestParametersLGeneric" CLEAN, parameter group has status CLEAN + field "name" type "java.lang.String" value "l10LGenericNested0NameFromFile" CLEAN, parameter has status CLEAN + field "lgenericIntField" type "int" value "1" CLEAN, parameter has status CLEAN + field "lgenericStringField" type "java.lang.String" value "l10 generic nested 0 string field value from file" CLEAN, parameter has status CLEAN + parameter group "l10LGenericNested1NameFromFile" type "org.onap.policy.common.parameters.testclasses.TestParametersLGeneric" CLEAN, parameter group has status CLEAN + field "name" type "java.lang.String" value "l10LGenericNested1NameFromFile" CLEAN, parameter has status CLEAN + field "lgenericIntField" type "int" value "1" CLEAN, parameter has status CLEAN + field "lgenericStringField" type "java.lang.String" value "l10 generic nested 1 string field value from file" CLEAN, parameter has status CLEAN + parameter group map "l10LGenericNestedMap" CLEAN, parameter group map has status CLEAN + parameter group "L10Entry0Name" type "org.onap.policy.common.parameters.testclasses.TestParametersLGeneric" CLEAN, parameter group has status CLEAN + field "name" type "java.lang.String" value "L10Entry0Name" CLEAN, parameter has status CLEAN + field "lgenericIntField" type "int" value "1" CLEAN, parameter has status CLEAN + field "lgenericStringField" type "java.lang.String" value "L10Entry0 value from file" CLEAN, parameter has status CLEAN + parameter group "L10Entry1Name" type "org.onap.policy.common.parameters.testclasses.TestParametersLGeneric" CLEAN, parameter group has status CLEAN + field "name" type "java.lang.String" value "L10Entry1Name" CLEAN, parameter has status CLEAN + field "lgenericIntField" type "int" value "1" CLEAN, parameter has status CLEAN + field "lgenericStringField" type "java.lang.String" value "L10Entry1 value from file" CLEAN, parameter has status CLEAN + parameter group "l00GenericNestedNameFromFile" type "org.onap.policy.common.parameters.testclasses.TestParametersLGeneric" CLEAN, parameter group has status CLEAN + field "name" type "java.lang.String" value "l00GenericNestedNameFromFile" CLEAN, parameter has status CLEAN + field "lgenericIntField" type "int" value "1" CLEAN, parameter has status CLEAN + field "lgenericStringField" type "java.lang.String" value "l00 generic nested string field value from file" CLEAN, parameter has status CLEAN + parameter group map "l00LGenericNestedMap" CLEAN, parameter group map has status CLEAN + parameter group "L00Entry0Name" type "org.onap.policy.common.parameters.testclasses.TestParametersLGeneric" CLEAN, parameter group has status CLEAN + field "name" type "java.lang.String" value "L00Entry0Name" CLEAN, parameter has status CLEAN + field "lgenericIntField" type "int" value "1" CLEAN, parameter has status CLEAN + field "lgenericStringField" type "java.lang.String" value "L00Entry0 value from file" CLEAN, parameter has status CLEAN + parameter group "L00Entry1Name" type "org.onap.policy.common.parameters.testclasses.TestParametersLGeneric" CLEAN, parameter group has status CLEAN + field "name" type "java.lang.String" value "L00Entry1Name" CLEAN, parameter has status CLEAN + field "lgenericIntField" type "int" value "1" CLEAN, parameter has status CLEAN + field "lgenericStringField" type "java.lang.String" value "L00Entry1 value from file" CLEAN, parameter has status CLEAN + diff --git a/common-parameters/src/test/resources/expectedValidationResults/TestParametersL0_0_OK.txt b/common-parameters/src/test/resources/expectedValidationResults/TestParametersL0_0_OK.txt new file mode 100644 index 00000000..7f6d298c --- /dev/null +++ b/common-parameters/src/test/resources/expectedValidationResults/TestParametersL0_0_OK.txt @@ -0,0 +1,39 @@ +parameter group "l0Parameters" type "org.onap.policy.common.parameters.testclasses.TestParametersL00" CLEAN, parameter group has status CLEAN + field "name" type "java.lang.String" value "l0Parameters" CLEAN, parameter has status CLEAN + field "l00IntField" type "int" value "0" CLEAN, parameter has status CLEAN + field "l00StringField" type "java.lang.String" value "Legal org.onap.policy.common.parameters.testclasses.TestParametersL00" CLEAN, parameter has status CLEAN + parameter group "l00L10Nested" type "org.onap.policy.common.parameters.testclasses.TestParametersL10" CLEAN, parameter group has status CLEAN + field "name" type "java.lang.String" value "l00L10Nested" CLEAN, parameter has status CLEAN + field "l10IntField" type "int" value "0" CLEAN, parameter has status CLEAN + field "l10StringField" type "java.lang.String" value "Legal org.onap.policy.common.parameters.testclasses.TestParametersL10" CLEAN, parameter has status CLEAN + parameter group "l10LGenericNested0" type "org.onap.policy.common.parameters.testclasses.TestParametersLGeneric" CLEAN, parameter group has status CLEAN + field "name" type "java.lang.String" value "l10LGenericNested0" CLEAN, parameter has status CLEAN + field "lgenericIntField" type "int" value "0" CLEAN, parameter has status CLEAN + field "lgenericStringField" type "java.lang.String" value "Legal org.onap.policy.common.parameters.testclasses.TestParametersLGeneric" CLEAN, parameter has status CLEAN + parameter group "l10LGenericNested1" type "org.onap.policy.common.parameters.testclasses.TestParametersLGeneric" CLEAN, parameter group has status CLEAN + field "name" type "java.lang.String" value "l10LGenericNested1" CLEAN, parameter has status CLEAN + field "lgenericIntField" type "int" value "0" CLEAN, parameter has status CLEAN + field "lgenericStringField" type "java.lang.String" value "Legal org.onap.policy.common.parameters.testclasses.TestParametersLGeneric" CLEAN, parameter has status CLEAN + parameter group map "l10LGenericNestedMap" CLEAN, parameter group map has status CLEAN + parameter group "l10LGenericNestedMapVal0" type "org.onap.policy.common.parameters.testclasses.TestParametersLGeneric" CLEAN, parameter group has status CLEAN + field "name" type "java.lang.String" value "l10LGenericNestedMapVal0" CLEAN, parameter has status CLEAN + field "lgenericIntField" type "int" value "0" CLEAN, parameter has status CLEAN + field "lgenericStringField" type "java.lang.String" value "Legal org.onap.policy.common.parameters.testclasses.TestParametersLGeneric" CLEAN, parameter has status CLEAN + parameter group "l10LGenericNestedMapVal1" type "org.onap.policy.common.parameters.testclasses.TestParametersLGeneric" CLEAN, parameter group has status CLEAN + field "name" type "java.lang.String" value "l10LGenericNestedMapVal1" CLEAN, parameter has status CLEAN + field "lgenericIntField" type "int" value "0" CLEAN, parameter has status CLEAN + field "lgenericStringField" type "java.lang.String" value "Legal org.onap.policy.common.parameters.testclasses.TestParametersLGeneric" CLEAN, parameter has status CLEAN + parameter group "l00LGenericNested" type "org.onap.policy.common.parameters.testclasses.TestParametersLGeneric" CLEAN, parameter group has status CLEAN + field "name" type "java.lang.String" value "l00LGenericNested" CLEAN, parameter has status CLEAN + field "lgenericIntField" type "int" value "0" CLEAN, parameter has status CLEAN + field "lgenericStringField" type "java.lang.String" value "Legal org.onap.policy.common.parameters.testclasses.TestParametersLGeneric" CLEAN, parameter has status CLEAN + parameter group map "l00LGenericNestedMap" CLEAN, parameter group map has status CLEAN + parameter group "l00LGenericNestedMapVal0" type "org.onap.policy.common.parameters.testclasses.TestParametersLGeneric" CLEAN, parameter group has status CLEAN + field "name" type "java.lang.String" value "l00LGenericNestedMapVal0" CLEAN, parameter has status CLEAN + field "lgenericIntField" type "int" value "0" CLEAN, parameter has status CLEAN + field "lgenericStringField" type "java.lang.String" value "Legal org.onap.policy.common.parameters.testclasses.TestParametersLGeneric" CLEAN, parameter has status CLEAN + parameter group "l00LGenericNestedMapVal1" type "org.onap.policy.common.parameters.testclasses.TestParametersLGeneric" CLEAN, parameter group has status CLEAN + field "name" type "java.lang.String" value "l00LGenericNestedMapVal1" CLEAN, parameter has status CLEAN + field "lgenericIntField" type "int" value "0" CLEAN, parameter has status CLEAN + field "lgenericStringField" type "java.lang.String" value "Legal org.onap.policy.common.parameters.testclasses.TestParametersLGeneric" CLEAN, parameter has status CLEAN + diff --git a/common-parameters/src/test/resources/expectedValidationResults/TestParametersL0_1_Invalid.txt b/common-parameters/src/test/resources/expectedValidationResults/TestParametersL0_1_Invalid.txt new file mode 100644 index 00000000..dcc3cee3 --- /dev/null +++ b/common-parameters/src/test/resources/expectedValidationResults/TestParametersL0_1_Invalid.txt @@ -0,0 +1,3 @@ +parameter group "l0Parameters" type "org.onap.policy.common.parameters.testclasses.TestParametersL00" INVALID, parameter group has status INVALID + field "l00IntField" type "int" value "-1" INVALID, l00IntField must be a positive integer + field "l00StringField" type "java.lang.String" value "" INVALID, l00StringField must be a non-blank string diff --git a/common-parameters/src/test/resources/expectedValidationResults/TestParametersL0_1_Observation.txt b/common-parameters/src/test/resources/expectedValidationResults/TestParametersL0_1_Observation.txt new file mode 100644 index 00000000..ae627926 --- /dev/null +++ b/common-parameters/src/test/resources/expectedValidationResults/TestParametersL0_1_Observation.txt @@ -0,0 +1,3 @@ +parameter group "l0Parameters" type "org.onap.policy.common.parameters.testclasses.TestParametersL00" OBSERVATION, parameter group has status OBSERVATION + field "l00IntField" type "int" value "2" OBSERVATION, this field has been set to 2 + field "l00StringField" type "java.lang.String" value "aString" OBSERVATION, this value for name is unhelpful diff --git a/common-parameters/src/test/resources/expectedValidationResults/TestParametersL0_1_Warning.txt b/common-parameters/src/test/resources/expectedValidationResults/TestParametersL0_1_Warning.txt new file mode 100644 index 00000000..14a65aa8 --- /dev/null +++ b/common-parameters/src/test/resources/expectedValidationResults/TestParametersL0_1_Warning.txt @@ -0,0 +1,3 @@ +parameter group "l0Parameters" type "org.onap.policy.common.parameters.testclasses.TestParametersL00" WARNING, parameter group has status WARNING + field "l00IntField" type "int" value "3" WARNING, values greater than 2 are not recommended + field "l00StringField" type "java.lang.String" value "l00StringField" WARNING, using the field name for the parameter value is dangerous diff --git a/common-parameters/src/test/resources/expectedValidationResults/TestParametersL0_2_Invalid.txt b/common-parameters/src/test/resources/expectedValidationResults/TestParametersL0_2_Invalid.txt new file mode 100644 index 00000000..cec8f208 --- /dev/null +++ b/common-parameters/src/test/resources/expectedValidationResults/TestParametersL0_2_Invalid.txt @@ -0,0 +1,9 @@ +parameter group "l0Parameters" type "org.onap.policy.common.parameters.testclasses.TestParametersL00" INVALID, parameter group has status INVALID + field "l00IntField" type "int" value "-1" INVALID, l00IntField must be a positive integer + field "l00StringField" type "java.lang.String" value "" INVALID, l00StringField must be a non-blank string + parameter group "l00L10Nested" type "org.onap.policy.common.parameters.testclasses.TestParametersL10" INVALID, parameter group has status INVALID + field "l10IntField" type "int" value "-1" INVALID, l10IntField must be a positive integer + field "l10StringField" type "java.lang.String" value "" INVALID, l10StringField must be a non-blank string + parameter group "l00LGenericNested" type "org.onap.policy.common.parameters.testclasses.TestParametersLGeneric" INVALID, parameter group has status INVALID + field "lgenericIntField" type "int" value "-1" INVALID, lgenericIntField must be a positive integer + field "lgenericStringField" type "java.lang.String" value "" INVALID, lgenericStringField must be a non-blank string
\ No newline at end of file diff --git a/common-parameters/src/test/resources/expectedValidationResults/TestParametersL0_2_Observation.txt b/common-parameters/src/test/resources/expectedValidationResults/TestParametersL0_2_Observation.txt new file mode 100644 index 00000000..45402033 --- /dev/null +++ b/common-parameters/src/test/resources/expectedValidationResults/TestParametersL0_2_Observation.txt @@ -0,0 +1,9 @@ +parameter group "l0Parameters" type "org.onap.policy.common.parameters.testclasses.TestParametersL00" OBSERVATION, parameter group has status OBSERVATION + field "l00IntField" type "int" value "2" OBSERVATION, this field has been set to 2 + field "l00StringField" type "java.lang.String" value "aString" OBSERVATION, this value for name is unhelpful + parameter group "l00L10Nested" type "org.onap.policy.common.parameters.testclasses.TestParametersL10" OBSERVATION, parameter group has status OBSERVATION + field "l10IntField" type "int" value "2" OBSERVATION, this field has been set to 2 + field "l10StringField" type "java.lang.String" value "aString" OBSERVATION, this value for name is unhelpful + parameter group "l00LGenericNested" type "org.onap.policy.common.parameters.testclasses.TestParametersLGeneric" OBSERVATION, parameter group has status OBSERVATION + field "lgenericIntField" type "int" value "2" OBSERVATION, this field has been set to 2 + field "lgenericStringField" type "java.lang.String" value "aString" OBSERVATION, this value for name is unhelpful diff --git a/common-parameters/src/test/resources/expectedValidationResults/TestParametersL0_2_Warning.txt b/common-parameters/src/test/resources/expectedValidationResults/TestParametersL0_2_Warning.txt new file mode 100644 index 00000000..33d1d16a --- /dev/null +++ b/common-parameters/src/test/resources/expectedValidationResults/TestParametersL0_2_Warning.txt @@ -0,0 +1,9 @@ +parameter group "l0Parameters" type "org.onap.policy.common.parameters.testclasses.TestParametersL00" WARNING, parameter group has status WARNING + field "l00IntField" type "int" value "3" WARNING, values greater than 2 are not recommended + field "l00StringField" type "java.lang.String" value "l00StringField" WARNING, using the field name for the parameter value is dangerous + parameter group "l00L10Nested" type "org.onap.policy.common.parameters.testclasses.TestParametersL10" WARNING, parameter group has status WARNING + field "l10IntField" type "int" value "3" WARNING, values greater than 2 are not recommended + field "l10StringField" type "java.lang.String" value "l10StringField" WARNING, using the field name for the parameter value is dangerous + parameter group "l00LGenericNested" type "org.onap.policy.common.parameters.testclasses.TestParametersLGeneric" WARNING, parameter group has status WARNING + field "lgenericIntField" type "int" value "3" WARNING, values greater than 2 are not recommended + field "lgenericStringField" type "java.lang.String" value "lgenericStringField" WARNING, using the field name for the parameter value is dangerous
\ No newline at end of file diff --git a/common-parameters/src/test/resources/expectedValidationResults/TestParametersL0_3_Invalid.txt b/common-parameters/src/test/resources/expectedValidationResults/TestParametersL0_3_Invalid.txt new file mode 100644 index 00000000..fecfe70d --- /dev/null +++ b/common-parameters/src/test/resources/expectedValidationResults/TestParametersL0_3_Invalid.txt @@ -0,0 +1,15 @@ +parameter group "l0Parameters" type "org.onap.policy.common.parameters.testclasses.TestParametersL00" INVALID, parameter group has status INVALID + field "l00IntField" type "int" value "-1" INVALID, l00IntField must be a positive integer + field "l00StringField" type "java.lang.String" value "" INVALID, l00StringField must be a non-blank string + parameter group "l00L10Nested" type "org.onap.policy.common.parameters.testclasses.TestParametersL10" INVALID, parameter group has status INVALID + field "l10IntField" type "int" value "-1" INVALID, l10IntField must be a positive integer + field "l10StringField" type "java.lang.String" value "" INVALID, l10StringField must be a non-blank string + parameter group "l10LGenericNested0" type "org.onap.policy.common.parameters.testclasses.TestParametersLGeneric" INVALID, parameter group has status INVALID + field "lgenericIntField" type "int" value "-1" INVALID, lgenericIntField must be a positive integer + field "lgenericStringField" type "java.lang.String" value "" INVALID, lgenericStringField must be a non-blank string + parameter group "l10LGenericNested1" type "org.onap.policy.common.parameters.testclasses.TestParametersLGeneric" INVALID, parameter group has status INVALID + field "lgenericIntField" type "int" value "-1" INVALID, lgenericIntField must be a positive integer + field "lgenericStringField" type "java.lang.String" value "" INVALID, lgenericStringField must be a non-blank string + parameter group "l00LGenericNested" type "org.onap.policy.common.parameters.testclasses.TestParametersLGeneric" INVALID, parameter group has status INVALID + field "lgenericIntField" type "int" value "-1" INVALID, lgenericIntField must be a positive integer + field "lgenericStringField" type "java.lang.String" value "" INVALID, lgenericStringField must be a non-blank string
\ No newline at end of file diff --git a/common-parameters/src/test/resources/expectedValidationResults/TestParametersL0_3_Observation.txt b/common-parameters/src/test/resources/expectedValidationResults/TestParametersL0_3_Observation.txt new file mode 100644 index 00000000..00fe6627 --- /dev/null +++ b/common-parameters/src/test/resources/expectedValidationResults/TestParametersL0_3_Observation.txt @@ -0,0 +1,15 @@ +parameter group "l0Parameters" type "org.onap.policy.common.parameters.testclasses.TestParametersL00" OBSERVATION, parameter group has status OBSERVATION + field "l00IntField" type "int" value "2" OBSERVATION, this field has been set to 2 + field "l00StringField" type "java.lang.String" value "aString" OBSERVATION, this value for name is unhelpful + parameter group "l00L10Nested" type "org.onap.policy.common.parameters.testclasses.TestParametersL10" OBSERVATION, parameter group has status OBSERVATION + field "l10IntField" type "int" value "2" OBSERVATION, this field has been set to 2 + field "l10StringField" type "java.lang.String" value "aString" OBSERVATION, this value for name is unhelpful + parameter group "l10LGenericNested0" type "org.onap.policy.common.parameters.testclasses.TestParametersLGeneric" OBSERVATION, parameter group has status OBSERVATION + field "lgenericIntField" type "int" value "2" OBSERVATION, this field has been set to 2 + field "lgenericStringField" type "java.lang.String" value "aString" OBSERVATION, this value for name is unhelpful + parameter group "l10LGenericNested1" type "org.onap.policy.common.parameters.testclasses.TestParametersLGeneric" OBSERVATION, parameter group has status OBSERVATION + field "lgenericIntField" type "int" value "2" OBSERVATION, this field has been set to 2 + field "lgenericStringField" type "java.lang.String" value "aString" OBSERVATION, this value for name is unhelpful + parameter group "l00LGenericNested" type "org.onap.policy.common.parameters.testclasses.TestParametersLGeneric" OBSERVATION, parameter group has status OBSERVATION + field "lgenericIntField" type "int" value "2" OBSERVATION, this field has been set to 2 + field "lgenericStringField" type "java.lang.String" value "aString" OBSERVATION, this value for name is unhelpful diff --git a/common-parameters/src/test/resources/expectedValidationResults/TestParametersL0_3_Warning.txt b/common-parameters/src/test/resources/expectedValidationResults/TestParametersL0_3_Warning.txt new file mode 100644 index 00000000..6c1c639d --- /dev/null +++ b/common-parameters/src/test/resources/expectedValidationResults/TestParametersL0_3_Warning.txt @@ -0,0 +1,15 @@ +parameter group "l0Parameters" type "org.onap.policy.common.parameters.testclasses.TestParametersL00" WARNING, parameter group has status WARNING + field "l00IntField" type "int" value "3" WARNING, values greater than 2 are not recommended + field "l00StringField" type "java.lang.String" value "l00StringField" WARNING, using the field name for the parameter value is dangerous + parameter group "l00L10Nested" type "org.onap.policy.common.parameters.testclasses.TestParametersL10" WARNING, parameter group has status WARNING + field "l10IntField" type "int" value "3" WARNING, values greater than 2 are not recommended + field "l10StringField" type "java.lang.String" value "l10StringField" WARNING, using the field name for the parameter value is dangerous + parameter group "l10LGenericNested0" type "org.onap.policy.common.parameters.testclasses.TestParametersLGeneric" WARNING, parameter group has status WARNING + field "lgenericIntField" type "int" value "3" WARNING, values greater than 2 are not recommended + field "lgenericStringField" type "java.lang.String" value "lgenericStringField" WARNING, using the field name for the parameter value is dangerous + parameter group "l10LGenericNested1" type "org.onap.policy.common.parameters.testclasses.TestParametersLGeneric" WARNING, parameter group has status WARNING + field "lgenericIntField" type "int" value "3" WARNING, values greater than 2 are not recommended + field "lgenericStringField" type "java.lang.String" value "lgenericStringField" WARNING, using the field name for the parameter value is dangerous + parameter group "l00LGenericNested" type "org.onap.policy.common.parameters.testclasses.TestParametersLGeneric" WARNING, parameter group has status WARNING + field "lgenericIntField" type "int" value "3" WARNING, values greater than 2 are not recommended + field "lgenericStringField" type "java.lang.String" value "lgenericStringField" WARNING, using the field name for the parameter value is dangerous
\ No newline at end of file diff --git a/common-parameters/src/test/resources/parameters/TestParameters.json b/common-parameters/src/test/resources/parameters/TestParameters.json new file mode 100644 index 00000000..8e312166 --- /dev/null +++ b/common-parameters/src/test/resources/parameters/TestParameters.json @@ -0,0 +1,49 @@ +{ + "name" : "l00NameFromFile", + "l00IntField" : 1, + "l00StringField" : "l00 string field value from file", + "l00L10Nested" : { + "name" : "l00L10NestedNameFromFile", + "l10IntField" : 1, + "l10StringField" : "l00 L10 nested string field value from file", + "l10LGenericNested0" : { + "name" : "l10LGenericNested0NameFromFile", + "lgenericIntField" : 1, + "lgenericStringField" : "l10 generic nested 0 string field value from file" + }, + "l10LGenericNested1" : { + "name" : "l10LGenericNested1NameFromFile", + "lgenericIntField" : 1, + "lgenericStringField" : "l10 generic nested 1 string field value from file" + }, + "l10LGenericNestedMap": { + "L10Entry0": { + "name" : "L10Entry0Name", + "lgenericIntField" : 1, + "lgenericStringField" : "L10Entry0 value from file" + }, + "L10Entry1": { + "name" : "L10Entry1Name", + "lgenericIntField" : 1, + "lgenericStringField" : "L10Entry1 value from file" + } + } + }, + "l00LGenericNested" : { + "name" : "l00GenericNestedNameFromFile", + "lgenericIntField" : 1, + "lgenericStringField" : "l00 generic nested string field value from file" + }, + "l00LGenericNestedMap": { + "L00Entry0": { + "name" : "L00Entry0Name", + "lgenericIntField" : 1, + "lgenericStringField" : "L00Entry0 value from file" + }, + "L00Entry1": { + "name" : "L00Entry1Name", + "lgenericIntField" : 1, + "lgenericStringField" : "L00Entry1 value from file" + } + } +}
\ No newline at end of file diff --git a/common-parameters/src/test/resources/parameters/TestParameters.yaml b/common-parameters/src/test/resources/parameters/TestParameters.yaml new file mode 100644 index 00000000..c7e17f6a --- /dev/null +++ b/common-parameters/src/test/resources/parameters/TestParameters.yaml @@ -0,0 +1,37 @@ +name: l00NameFromFile +l00IntField: 1 +l00StringField: l00 string field value from file +l00L10Nested: + name: l00L10NestedNameFromFile + l10IntField: 1 + l10StringField: l00 L10 nested string field value from file + l10LGenericNested0: + name: l10LGenericNested0NameFromFile + lgenericIntField: 1 + lgenericStringField: l10 generic nested 0 string field value from file + l10LGenericNested1: + name: l10LGenericNested1NameFromFile + lgenericIntField: 1 + lgenericStringField: l10 generic nested 1 string field value from file + l10LGenericNestedMap: + l10Entry0: + name: L10Entry0Name + lgenericIntField: 1 + lgenericStringField: L10Entry0 value from file + L10Entry1: + name: L10Entry1Name + lgenericIntField: 1 + lgenericStringField: L10Entry1 value from file +l00LGenericNested: + name: l00GenericNestedNameFromFile + lgenericIntField: 1 + lgenericStringField: l00 generic nested string field value from file +l00LGenericNestedMap: + L00Entry0: + name: L00Entry0Name + lgenericIntField: 1 + lgenericStringField: L00Entry0 value from file + L00Entry1: + name: L00Entry1Name + lgenericIntField: 1 + lgenericStringField: L00Entry1 value from file diff --git a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/event/comm/TopicEndpoint.java b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/event/comm/TopicEndpoint.java index 03e6776a..e7a21ca1 100644 --- a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/event/comm/TopicEndpoint.java +++ b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/event/comm/TopicEndpoint.java @@ -20,6 +20,9 @@ package org.onap.policy.common.endpoints.event.comm; +import com.fasterxml.jackson.annotation.JsonIgnore; + +import java.util.ArrayList; import java.util.List; import java.util.Properties; @@ -30,6 +33,8 @@ import org.onap.policy.common.endpoints.event.comm.bus.DmaapTopicSource; import org.onap.policy.common.endpoints.event.comm.bus.NoopTopicSink; import org.onap.policy.common.endpoints.event.comm.bus.UebTopicSink; import org.onap.policy.common.endpoints.event.comm.bus.UebTopicSource; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * Abstraction to managed the system's Networked Topic Endpoints, sources of all events input into @@ -38,6 +43,11 @@ import org.onap.policy.common.endpoints.event.comm.bus.UebTopicSource; public interface TopicEndpoint extends Startable, Lockable { /** + * singleton for global access + */ + public static final TopicEndpoint manager = new ProxyTopicEndpointManager(); + + /** * Add Topic Sources to the communication infrastructure initialized per properties * * @param properties properties for Topic Source construction @@ -227,3 +237,431 @@ public interface TopicEndpoint extends Startable, Lockable { */ public List<NoopTopicSink> getNoopTopicSinks(); } + + +/* + * ----------------- implementation ------------------- + */ + +/** + * This implementation of the Topic Endpoint Manager, proxies operations to appropriate + * implementations according to the communication infrastructure that are supported + */ +class ProxyTopicEndpointManager implements TopicEndpoint { + /** + * Logger + */ + private static Logger logger = LoggerFactory.getLogger(ProxyTopicEndpointManager.class); + /** + * Is this element locked? + */ + protected volatile boolean locked = false; + + /** + * Is this element alive? + */ + protected volatile boolean alive = false; + + @Override + public List<TopicSource> addTopicSources(Properties properties) { + + // 1. Create UEB Sources + // 2. Create DMAAP Sources + + final List<TopicSource> sources = new ArrayList<>(); + + sources.addAll(UebTopicSource.factory.build(properties)); + sources.addAll(DmaapTopicSource.factory.build(properties)); + + if (this.isLocked()) { + for (final TopicSource source : sources) { + source.lock(); + } + } + + return sources; + } + + @Override + public List<TopicSink> addTopicSinks(Properties properties) { + // 1. Create UEB Sinks + // 2. Create DMAAP Sinks + + final List<TopicSink> sinks = new ArrayList<>(); + + sinks.addAll(UebTopicSink.factory.build(properties)); + sinks.addAll(DmaapTopicSink.factory.build(properties)); + sinks.addAll(NoopTopicSink.factory.build(properties)); + + if (this.isLocked()) { + for (final TopicSink sink : sinks) { + sink.lock(); + } + } + + return sinks; + } + + @Override + public List<TopicSource> getTopicSources() { + + final List<TopicSource> sources = new ArrayList<>(); + + sources.addAll(UebTopicSource.factory.inventory()); + sources.addAll(DmaapTopicSource.factory.inventory()); + + return sources; + } + + @Override + public List<TopicSink> getTopicSinks() { + + final List<TopicSink> sinks = new ArrayList<>(); + + sinks.addAll(UebTopicSink.factory.inventory()); + sinks.addAll(DmaapTopicSink.factory.inventory()); + sinks.addAll(NoopTopicSink.factory.inventory()); + + return sinks; + } + + @JsonIgnore + @Override + public List<UebTopicSource> getUebTopicSources() { + return UebTopicSource.factory.inventory(); + } + + @JsonIgnore + @Override + public List<DmaapTopicSource> getDmaapTopicSources() { + return DmaapTopicSource.factory.inventory(); + } + + @JsonIgnore + @Override + public List<UebTopicSink> getUebTopicSinks() { + return UebTopicSink.factory.inventory(); + } + + @JsonIgnore + @Override + public List<DmaapTopicSink> getDmaapTopicSinks() { + return DmaapTopicSink.factory.inventory(); + } + + @JsonIgnore + @Override + public List<NoopTopicSink> getNoopTopicSinks() { + return NoopTopicSink.factory.inventory(); + } + + @Override + public boolean start() { + + synchronized (this) { + if (this.locked) { + throw new IllegalStateException(this + " is locked"); + } + + if (this.alive) { + return true; + } + + this.alive = true; + } + + final List<Startable> endpoints = this.getEndpoints(); + + boolean success = true; + for (final Startable endpoint : endpoints) { + try { + success = endpoint.start() && success; + } catch (final Exception e) { + success = false; + logger.error("Problem starting endpoint: {}", endpoint, e); + } + } + + return success; + } + + + @Override + public boolean stop() { + + /* + * stop regardless if it is locked, in other words, stop operation has precedence over + * locks. + */ + synchronized (this) { + this.alive = false; + } + + final List<Startable> endpoints = this.getEndpoints(); + + boolean success = true; + for (final Startable endpoint : endpoints) { + try { + success = endpoint.stop() && success; + } catch (final Exception e) { + success = false; + logger.error("Problem stopping endpoint: {}", endpoint, e); + } + } + + return success; + } + + /** + * + * @return list of managed endpoints + */ + @JsonIgnore + protected List<Startable> getEndpoints() { + final List<Startable> endpoints = new ArrayList<>(); + + endpoints.addAll(this.getTopicSources()); + endpoints.addAll(this.getTopicSinks()); + + return endpoints; + } + + @Override + public void shutdown() { + UebTopicSource.factory.destroy(); + UebTopicSink.factory.destroy(); + NoopTopicSink.factory.destroy(); + + DmaapTopicSource.factory.destroy(); + DmaapTopicSink.factory.destroy(); + } + + @Override + public boolean isAlive() { + return this.alive; + } + + @Override + public boolean lock() { + + synchronized (this) { + if (this.locked) { + return true; + } + + this.locked = true; + } + + for (final TopicSource source : this.getTopicSources()) { + source.lock(); + } + + for (final TopicSink sink : this.getTopicSinks()) { + sink.lock(); + } + + return true; + } + + @Override + public boolean unlock() { + synchronized (this) { + if (!this.locked) { + return true; + } + + this.locked = false; + } + + for (final TopicSource source : this.getTopicSources()) { + source.unlock(); + } + + for (final TopicSink sink : this.getTopicSinks()) { + sink.unlock(); + } + + return true; + } + + @Override + public boolean isLocked() { + return this.locked; + } + + @Override + public List<TopicSource> getTopicSources(List<String> topicNames) { + + if (topicNames == null) { + throw new IllegalArgumentException("must provide a list of topics"); + } + + final List<TopicSource> sources = new ArrayList<>(); + for (final String topic : topicNames) { + try { + final TopicSource uebSource = this.getUebTopicSource(topic); + if (uebSource != null) { + sources.add(uebSource); + } + } catch (final Exception e) { + logger.debug("No UEB source for topic: {}", topic, e); + } + + try { + final TopicSource dmaapSource = this.getDmaapTopicSource(topic); + if (dmaapSource != null) { + sources.add(dmaapSource); + } + } catch (final Exception e) { + logger.debug("No DMAAP source for topic: {}", topic, e); + } + } + return sources; + } + + @Override + public List<TopicSink> getTopicSinks(List<String> topicNames) { + + if (topicNames == null) { + throw new IllegalArgumentException("must provide a list of topics"); + } + + final List<TopicSink> sinks = new ArrayList<>(); + for (final String topic : topicNames) { + try { + final TopicSink uebSink = this.getUebTopicSink(topic); + if (uebSink != null) { + sinks.add(uebSink); + } + } catch (final Exception e) { + logger.debug("No UEB sink for topic: {}", topic, e); + } + + try { + final TopicSink dmaapSink = this.getDmaapTopicSink(topic); + if (dmaapSink != null) { + sinks.add(dmaapSink); + } + } catch (final Exception e) { + logger.debug("No DMAAP sink for topic: {}", topic, e); + } + + try { + final TopicSink noopSink = this.getNoopTopicSink(topic); + if (noopSink != null) { + sinks.add(noopSink); + } + } catch (final Exception e) { + logger.debug("No NOOP sink for topic: {}", topic, e); + } + } + return sinks; + } + + @Override + public TopicSource getTopicSource(Topic.CommInfrastructure commType, String topicName) { + + if (commType == null) { + throw parmException(topicName); + } + + if (topicName == null) { + throw parmException(topicName); + } + + switch (commType) { + case UEB: + return this.getUebTopicSource(topicName); + case DMAAP: + return this.getDmaapTopicSource(topicName); + default: + throw new UnsupportedOperationException("Unsupported " + commType.name()); + } + } + + private IllegalArgumentException parmException(String topicName) { + return new IllegalArgumentException( + "Invalid parameter: a communication infrastructure required to fetch " + topicName); + } + + @Override + public TopicSink getTopicSink(Topic.CommInfrastructure commType, String topicName) { + if (commType == null) { + throw parmException(topicName); + } + + if (topicName == null) { + throw parmException(topicName); + } + + switch (commType) { + case UEB: + return this.getUebTopicSink(topicName); + case DMAAP: + return this.getDmaapTopicSink(topicName); + case NOOP: + return this.getNoopTopicSink(topicName); + default: + throw new UnsupportedOperationException("Unsupported " + commType.name()); + } + } + + @Override + public List<TopicSink> getTopicSinks(String topicName) { + if (topicName == null) { + throw parmException(topicName); + } + + final List<TopicSink> sinks = new ArrayList<>(); + + try { + sinks.add(this.getUebTopicSink(topicName)); + } catch (final Exception e) { + logNoSink(topicName, e); + } + + try { + sinks.add(this.getDmaapTopicSink(topicName)); + } catch (final Exception e) { + logNoSink(topicName, e); + } + + try { + sinks.add(this.getNoopTopicSink(topicName)); + } catch (final Exception e) { + logNoSink(topicName, e); + } + + return sinks; + } + + private void logNoSink(String topicName, Exception ex) { + logger.debug("No sink for topic: {}", topicName, ex); + } + + @Override + public UebTopicSource getUebTopicSource(String topicName) { + return UebTopicSource.factory.get(topicName); + } + + @Override + public UebTopicSink getUebTopicSink(String topicName) { + return UebTopicSink.factory.get(topicName); + } + + @Override + public DmaapTopicSource getDmaapTopicSource(String topicName) { + return DmaapTopicSource.factory.get(topicName); + } + + @Override + public DmaapTopicSink getDmaapTopicSink(String topicName) { + return DmaapTopicSink.factory.get(topicName); + } + + @Override + public NoopTopicSink getNoopTopicSink(String topicName) { + return NoopTopicSink.factory.get(topicName); + } + +} diff --git a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/event/comm/bus/DmaapTopicSink.java b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/event/comm/bus/DmaapTopicSink.java index 845945cd..ba219322 100644 --- a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/event/comm/bus/DmaapTopicSink.java +++ b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/event/comm/bus/DmaapTopicSink.java @@ -21,4 +21,10 @@ package org.onap.policy.common.endpoints.event.comm.bus; public interface DmaapTopicSink extends BusTopicSink { + + /** + * Factory of UebTopicWriter for instantiation and management purposes + */ + + public static final DmaapTopicSinkFactory factory = new IndexedDmaapTopicSinkFactory(); } diff --git a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/event/comm/bus/DmaapTopicSinkFactory.java b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/event/comm/bus/DmaapTopicSinkFactory.java index 2e3ecf29..26e8d413 100644 --- a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/event/comm/bus/DmaapTopicSinkFactory.java +++ b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/event/comm/bus/DmaapTopicSinkFactory.java @@ -20,10 +20,18 @@ package org.onap.policy.common.endpoints.event.comm.bus; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Properties; +import org.onap.policy.common.endpoints.event.comm.bus.internal.InlineDmaapTopicSink; +import org.onap.policy.common.endpoints.properties.PolicyEndPointProperties; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + /** * DMAAP Topic Sink Factory */ @@ -133,3 +141,284 @@ public interface DmaapTopicSinkFactory { */ public void destroy(); } + + +/* ------------- implementation ----------------- */ + +/** + * Factory of DMAAP Reader Topics indexed by topic name + */ +class IndexedDmaapTopicSinkFactory implements DmaapTopicSinkFactory { + private static final String MISSING_TOPIC = "A topic must be provided"; + + /** + * Logger + */ + private static Logger logger = LoggerFactory.getLogger(IndexedDmaapTopicSinkFactory.class); + + /** + * DMAAP Topic Name Index + */ + protected HashMap<String, DmaapTopicSink> dmaapTopicWriters = new HashMap<>(); + + @Override + public DmaapTopicSink build(List<String> servers, String topic, String apiKey, String apiSecret, String userName, + String password, String partitionKey, String environment, String aftEnvironment, String partner, + String latitude, String longitude, Map<String, String> additionalProps, boolean managed, boolean useHttps, + boolean allowSelfSignedCerts) { + + if (topic == null || topic.isEmpty()) { + throw new IllegalArgumentException(MISSING_TOPIC); + } + + synchronized (this) { + if (dmaapTopicWriters.containsKey(topic)) { + return dmaapTopicWriters.get(topic); + } + + DmaapTopicSink dmaapTopicSink = new InlineDmaapTopicSink(servers, topic, apiKey, apiSecret, userName, + password, partitionKey, environment, aftEnvironment, partner, latitude, longitude, additionalProps, + useHttps, allowSelfSignedCerts); + + if (managed) { + dmaapTopicWriters.put(topic, dmaapTopicSink); + } + return dmaapTopicSink; + } + } + + @Override + public DmaapTopicSink build(List<String> servers, String topic, String apiKey, String apiSecret, String userName, + String password, String partitionKey, boolean managed, boolean useHttps, boolean allowSelfSignedCerts) { + + if (topic == null || topic.isEmpty()) { + throw new IllegalArgumentException(MISSING_TOPIC); + } + + synchronized (this) { + if (dmaapTopicWriters.containsKey(topic)) { + return dmaapTopicWriters.get(topic); + } + + DmaapTopicSink dmaapTopicSink = new InlineDmaapTopicSink(servers, topic, apiKey, apiSecret, userName, + password, partitionKey, useHttps, allowSelfSignedCerts); + + if (managed) { + dmaapTopicWriters.put(topic, dmaapTopicSink); + } + return dmaapTopicSink; + } + } + + @Override + public DmaapTopicSink build(List<String> servers, String topic) { + return this.build(servers, topic, null, null, null, null, null, true, false, false); + } + + @Override + public List<DmaapTopicSink> build(Properties properties) { + + String writeTopics = properties.getProperty(PolicyEndPointProperties.PROPERTY_DMAAP_SINK_TOPICS); + if (writeTopics == null || writeTopics.isEmpty()) { + logger.info("{}: no topic for DMaaP Sink", this); + return new ArrayList<>(); + } + + List<String> writeTopicList = new ArrayList<>(Arrays.asList(writeTopics.split("\\s*,\\s*"))); + List<DmaapTopicSink> newDmaapTopicSinks = new ArrayList<>(); + synchronized (this) { + for (String topic : writeTopicList) { + if (this.dmaapTopicWriters.containsKey(topic)) { + newDmaapTopicSinks.add(this.dmaapTopicWriters.get(topic)); + continue; + } + String servers = properties.getProperty(PolicyEndPointProperties.PROPERTY_DMAAP_SINK_TOPICS + "." + + topic + PolicyEndPointProperties.PROPERTY_TOPIC_SERVERS_SUFFIX); + + List<String> serverList; + if (servers != null && !servers.isEmpty()) { + serverList = new ArrayList<>(Arrays.asList(servers.split("\\s*,\\s*"))); + } else { + serverList = new ArrayList<>(); + } + + String apiKey = properties.getProperty(PolicyEndPointProperties.PROPERTY_DMAAP_SINK_TOPICS + "." + topic + + PolicyEndPointProperties.PROPERTY_TOPIC_API_KEY_SUFFIX); + String apiSecret = properties.getProperty(PolicyEndPointProperties.PROPERTY_DMAAP_SINK_TOPICS + "." + + topic + PolicyEndPointProperties.PROPERTY_TOPIC_API_SECRET_SUFFIX); + + String aafMechId = properties.getProperty(PolicyEndPointProperties.PROPERTY_DMAAP_SINK_TOPICS + "." + + topic + PolicyEndPointProperties.PROPERTY_TOPIC_AAF_MECHID_SUFFIX); + String aafPassword = properties.getProperty(PolicyEndPointProperties.PROPERTY_DMAAP_SINK_TOPICS + "." + + topic + PolicyEndPointProperties.PROPERTY_TOPIC_AAF_PASSWORD_SUFFIX); + + String partitionKey = properties.getProperty(PolicyEndPointProperties.PROPERTY_DMAAP_SINK_TOPICS + "." + + topic + PolicyEndPointProperties.PROPERTY_TOPIC_SINK_PARTITION_KEY_SUFFIX); + + String managedString = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SINK_TOPICS + "." + + topic + PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX); + + /* DME2 Properties */ + + String dme2Environment = properties.getProperty(PolicyEndPointProperties.PROPERTY_DMAAP_SINK_TOPICS + + "." + topic + PolicyEndPointProperties.PROPERTY_DMAAP_DME2_ENVIRONMENT_SUFFIX); + + String dme2AftEnvironment = properties.getProperty(PolicyEndPointProperties.PROPERTY_DMAAP_SINK_TOPICS + + "." + topic + PolicyEndPointProperties.PROPERTY_DMAAP_DME2_AFT_ENVIRONMENT_SUFFIX); + + String dme2Partner = properties.getProperty(PolicyEndPointProperties.PROPERTY_DMAAP_SINK_TOPICS + "." + + topic + PolicyEndPointProperties.PROPERTY_DMAAP_DME2_PARTNER_SUFFIX); + + String dme2RouteOffer = properties.getProperty(PolicyEndPointProperties.PROPERTY_DMAAP_SINK_TOPICS + "." + + topic + PolicyEndPointProperties.PROPERTY_DMAAP_DME2_ROUTE_OFFER_SUFFIX); + + String dme2Latitude = properties.getProperty(PolicyEndPointProperties.PROPERTY_DMAAP_SINK_TOPICS + "." + + topic + PolicyEndPointProperties.PROPERTY_DMAAP_DME2_LATITUDE_SUFFIX); + + String dme2Longitude = properties.getProperty(PolicyEndPointProperties.PROPERTY_DMAAP_SINK_TOPICS + "." + + topic + PolicyEndPointProperties.PROPERTY_DMAAP_DME2_LONGITUDE_SUFFIX); + + String dme2EpReadTimeoutMs = properties.getProperty(PolicyEndPointProperties.PROPERTY_DMAAP_SINK_TOPICS + + "." + topic + PolicyEndPointProperties.PROPERTY_DMAAP_DME2_EP_READ_TIMEOUT_MS_SUFFIX); + + String dme2EpConnTimeout = properties.getProperty(PolicyEndPointProperties.PROPERTY_DMAAP_SINK_TOPICS + + "." + topic + PolicyEndPointProperties.PROPERTY_DMAAP_DME2_EP_CONN_TIMEOUT_SUFFIX); + + String dme2RoundtripTimeoutMs = + properties.getProperty(PolicyEndPointProperties.PROPERTY_DMAAP_SINK_TOPICS + "." + topic + + PolicyEndPointProperties.PROPERTY_DMAAP_DME2_ROUNDTRIP_TIMEOUT_MS_SUFFIX); + + String dme2Version = properties.getProperty(PolicyEndPointProperties.PROPERTY_DMAAP_SINK_TOPICS + "." + + topic + PolicyEndPointProperties.PROPERTY_DMAAP_DME2_VERSION_SUFFIX); + + String dme2SubContextPath = properties.getProperty(PolicyEndPointProperties.PROPERTY_DMAAP_SINK_TOPICS + + "." + topic + PolicyEndPointProperties.PROPERTY_DMAAP_DME2_SUB_CONTEXT_PATH_SUFFIX); + + String dme2SessionStickinessRequired = + properties.getProperty(PolicyEndPointProperties.PROPERTY_DMAAP_SINK_TOPICS + "." + topic + + PolicyEndPointProperties.PROPERTY_DMAAP_DME2_SESSION_STICKINESS_REQUIRED_SUFFIX); + + Map<String, String> dme2AdditionalProps = new HashMap<>(); + + if (dme2EpReadTimeoutMs != null && !dme2EpReadTimeoutMs.isEmpty()) { + dme2AdditionalProps.put(DME2_READ_TIMEOUT_PROPERTY, dme2EpReadTimeoutMs); + } + if (dme2EpConnTimeout != null && !dme2EpConnTimeout.isEmpty()) { + dme2AdditionalProps.put(DME2_EP_CONN_TIMEOUT_PROPERTY, dme2EpConnTimeout); + } + if (dme2RoundtripTimeoutMs != null && !dme2RoundtripTimeoutMs.isEmpty()) { + dme2AdditionalProps.put(DME2_ROUNDTRIP_TIMEOUT_PROPERTY, dme2RoundtripTimeoutMs); + } + if (dme2Version != null && !dme2Version.isEmpty()) { + dme2AdditionalProps.put(DME2_VERSION_PROPERTY, dme2Version); + } + if (dme2RouteOffer != null && !dme2RouteOffer.isEmpty()) { + dme2AdditionalProps.put(DME2_ROUTE_OFFER_PROPERTY, dme2RouteOffer); + } + if (dme2SubContextPath != null && !dme2SubContextPath.isEmpty()) { + dme2AdditionalProps.put(DME2_SUBCONTEXT_PATH_PROPERTY, dme2SubContextPath); + } + if (dme2SessionStickinessRequired != null && !dme2SessionStickinessRequired.isEmpty()) { + dme2AdditionalProps.put(DME2_SESSION_STICKINESS_REQUIRED_PROPERTY, dme2SessionStickinessRequired); + } + + if (servers == null || servers.isEmpty()) { + logger.error("{}: no DMaaP servers or DME2 ServiceName provided", this); + continue; + } + + boolean managed = true; + if (managedString != null && !managedString.isEmpty()) { + managed = Boolean.parseBoolean(managedString); + } + + String useHttpsString = properties.getProperty(PolicyEndPointProperties.PROPERTY_DMAAP_SINK_TOPICS + "." + + topic + PolicyEndPointProperties.PROPERTY_HTTP_HTTPS_SUFFIX); + + // default is to use HTTP if no https property exists + boolean useHttps = false; + if (useHttpsString != null && !useHttpsString.isEmpty()) { + useHttps = Boolean.parseBoolean(useHttpsString); + } + + + String allowSelfSignedCertsString = + properties.getProperty(PolicyEndPointProperties.PROPERTY_DMAAP_SINK_TOPICS + "." + topic + + PolicyEndPointProperties.PROPERTY_ALLOW_SELF_SIGNED_CERTIFICATES_SUFFIX); + + // default is to disallow self-signed certs + boolean allowSelfSignedCerts = false; + if (allowSelfSignedCertsString != null && !allowSelfSignedCertsString.isEmpty()) { + allowSelfSignedCerts = Boolean.parseBoolean(allowSelfSignedCertsString); + } + + DmaapTopicSink dmaapTopicSink = this.build(serverList, topic, apiKey, apiSecret, aafMechId, aafPassword, + partitionKey, dme2Environment, dme2AftEnvironment, dme2Partner, dme2Latitude, dme2Longitude, + dme2AdditionalProps, managed, useHttps, allowSelfSignedCerts); + + newDmaapTopicSinks.add(dmaapTopicSink); + } + return newDmaapTopicSinks; + } + } + + @Override + public void destroy(String topic) { + + if (topic == null || topic.isEmpty()) { + throw new IllegalArgumentException(MISSING_TOPIC); + } + + DmaapTopicSink dmaapTopicWriter; + synchronized (this) { + if (!dmaapTopicWriters.containsKey(topic)) { + return; + } + + dmaapTopicWriter = dmaapTopicWriters.remove(topic); + } + + dmaapTopicWriter.shutdown(); + } + + @Override + public void destroy() { + List<DmaapTopicSink> writers = this.inventory(); + for (DmaapTopicSink writer : writers) { + writer.shutdown(); + } + + synchronized (this) { + this.dmaapTopicWriters.clear(); + } + } + + @Override + public DmaapTopicSink get(String topic) { + + if (topic == null || topic.isEmpty()) { + throw new IllegalArgumentException(MISSING_TOPIC); + } + + synchronized (this) { + if (dmaapTopicWriters.containsKey(topic)) { + return dmaapTopicWriters.get(topic); + } else { + throw new IllegalStateException("DmaapTopicSink for " + topic + " not found"); + } + } + } + + @Override + public synchronized List<DmaapTopicSink> inventory() { + return new ArrayList<>(this.dmaapTopicWriters.values()); + } + + @Override + public String toString() { + StringBuilder builder = new StringBuilder(); + builder.append("IndexedDmaapTopicSinkFactory []"); + return builder.toString(); + } + +} diff --git a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/event/comm/bus/DmaapTopicSource.java b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/event/comm/bus/DmaapTopicSource.java index b50c752f..ffed5bab 100644 --- a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/event/comm/bus/DmaapTopicSource.java +++ b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/event/comm/bus/DmaapTopicSource.java @@ -21,4 +21,9 @@ package org.onap.policy.common.endpoints.event.comm.bus; public interface DmaapTopicSource extends BusTopicSource { + + /** + * factory for managing and tracking DMAAP sources + */ + public static DmaapTopicSourceFactory factory = new IndexedDmaapTopicSourceFactory(); } diff --git a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/event/comm/bus/DmaapTopicSourceFactory.java b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/event/comm/bus/DmaapTopicSourceFactory.java index adfb4b42..96ab6c63 100644 --- a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/event/comm/bus/DmaapTopicSourceFactory.java +++ b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/event/comm/bus/DmaapTopicSourceFactory.java @@ -20,10 +20,18 @@ package org.onap.policy.common.endpoints.event.comm.bus; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Properties; +import org.onap.policy.common.endpoints.event.comm.bus.internal.SingleThreadedDmaapTopicSource; +import org.onap.policy.common.endpoints.properties.PolicyEndPointProperties; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + /** * DMAAP Topic Source Factory */ @@ -156,3 +164,360 @@ public interface DmaapTopicSourceFactory { */ public List<DmaapTopicSource> inventory(); } + + +/* ------------- implementation ----------------- */ + +/** + * Factory of DMAAP Source Topics indexed by topic name + */ + +class IndexedDmaapTopicSourceFactory implements DmaapTopicSourceFactory { + private static final String MISSING_TOPIC = "A topic must be provided"; + + /** + * Logger + */ + private static Logger logger = LoggerFactory.getLogger(IndexedDmaapTopicSourceFactory.class); + + /** + * DMaaP Topic Name Index + */ + protected HashMap<String, DmaapTopicSource> dmaapTopicSources = new HashMap<>(); + + /** + * {@inheritDoc} + */ + @Override + public DmaapTopicSource build(List<String> servers, String topic, String apiKey, String apiSecret, String userName, + String password, String consumerGroup, String consumerInstance, int fetchTimeout, int fetchLimit, + String environment, String aftEnvironment, String partner, String latitude, String longitude, + Map<String, String> additionalProps, boolean managed, boolean useHttps, boolean allowSelfSignedCerts) { + + if (topic == null || topic.isEmpty()) { + throw new IllegalArgumentException(MISSING_TOPIC); + } + + synchronized (this) { + if (dmaapTopicSources.containsKey(topic)) { + return dmaapTopicSources.get(topic); + } + + DmaapTopicSource dmaapTopicSource = new SingleThreadedDmaapTopicSource(servers, topic, apiKey, apiSecret, + userName, password, consumerGroup, consumerInstance, fetchTimeout, fetchLimit, environment, + aftEnvironment, partner, latitude, longitude, additionalProps, useHttps, allowSelfSignedCerts); + + if (managed) { + dmaapTopicSources.put(topic, dmaapTopicSource); + } + + return dmaapTopicSource; + } + } + + /** + * {@inheritDoc} + */ + @Override + public DmaapTopicSource build(List<String> servers, String topic, String apiKey, String apiSecret, String userName, + String password, String consumerGroup, String consumerInstance, int fetchTimeout, int fetchLimit, + boolean managed, boolean useHttps, boolean allowSelfSignedCerts) { + + if (servers == null || servers.isEmpty()) { + throw new IllegalArgumentException("DMaaP Server(s) must be provided"); + } + + if (topic == null || topic.isEmpty()) { + throw new IllegalArgumentException(MISSING_TOPIC); + } + + synchronized (this) { + if (dmaapTopicSources.containsKey(topic)) { + return dmaapTopicSources.get(topic); + } + + DmaapTopicSource dmaapTopicSource = + new SingleThreadedDmaapTopicSource(servers, topic, apiKey, apiSecret, userName, password, + consumerGroup, consumerInstance, fetchTimeout, fetchLimit, useHttps, allowSelfSignedCerts); + + if (managed) { + dmaapTopicSources.put(topic, dmaapTopicSource); + } + + return dmaapTopicSource; + } + } + + /** + * {@inheritDoc} + */ + @Override + public List<DmaapTopicSource> build(Properties properties) { + + String readTopics = properties.getProperty(PolicyEndPointProperties.PROPERTY_DMAAP_SOURCE_TOPICS); + if (readTopics == null || readTopics.isEmpty()) { + logger.info("{}: no topic for DMaaP Source", this); + return new ArrayList<>(); + } + List<String> readTopicList = new ArrayList<>(Arrays.asList(readTopics.split("\\s*,\\s*"))); + + List<DmaapTopicSource> dmaapTopicSourceLst = new ArrayList<>(); + synchronized (this) { + for (String topic : readTopicList) { + if (this.dmaapTopicSources.containsKey(topic)) { + dmaapTopicSourceLst.add(this.dmaapTopicSources.get(topic)); + continue; + } + + String servers = properties.getProperty(PolicyEndPointProperties.PROPERTY_DMAAP_SOURCE_TOPICS + "." + + topic + PolicyEndPointProperties.PROPERTY_TOPIC_SERVERS_SUFFIX); + + List<String> serverList; + if (servers != null && !servers.isEmpty()) { + serverList = new ArrayList<>(Arrays.asList(servers.split("\\s*,\\s*"))); + } else { + serverList = new ArrayList<>(); + } + + String apiKey = properties.getProperty(PolicyEndPointProperties.PROPERTY_DMAAP_SOURCE_TOPICS + "." + + topic + PolicyEndPointProperties.PROPERTY_TOPIC_API_KEY_SUFFIX); + + String apiSecret = properties.getProperty(PolicyEndPointProperties.PROPERTY_DMAAP_SOURCE_TOPICS + "." + + topic + PolicyEndPointProperties.PROPERTY_TOPIC_API_SECRET_SUFFIX); + + String aafMechId = properties.getProperty(PolicyEndPointProperties.PROPERTY_DMAAP_SOURCE_TOPICS + "." + + topic + PolicyEndPointProperties.PROPERTY_TOPIC_AAF_MECHID_SUFFIX); + + String aafPassword = properties.getProperty(PolicyEndPointProperties.PROPERTY_DMAAP_SOURCE_TOPICS + "." + + topic + PolicyEndPointProperties.PROPERTY_TOPIC_AAF_PASSWORD_SUFFIX); + + String consumerGroup = properties.getProperty(PolicyEndPointProperties.PROPERTY_DMAAP_SOURCE_TOPICS + + "." + topic + PolicyEndPointProperties.PROPERTY_TOPIC_SOURCE_CONSUMER_GROUP_SUFFIX); + + String consumerInstance = properties.getProperty(PolicyEndPointProperties.PROPERTY_DMAAP_SOURCE_TOPICS + + "." + topic + PolicyEndPointProperties.PROPERTY_TOPIC_SOURCE_CONSUMER_INSTANCE_SUFFIX); + + String fetchTimeoutString = properties.getProperty(PolicyEndPointProperties.PROPERTY_DMAAP_SOURCE_TOPICS + + "." + topic + PolicyEndPointProperties.PROPERTY_TOPIC_SOURCE_FETCH_TIMEOUT_SUFFIX); + + /* DME2 Properties */ + + String dme2Environment = properties.getProperty(PolicyEndPointProperties.PROPERTY_DMAAP_SOURCE_TOPICS + + "." + topic + PolicyEndPointProperties.PROPERTY_DMAAP_DME2_ENVIRONMENT_SUFFIX); + + String dme2AftEnvironment = properties.getProperty(PolicyEndPointProperties.PROPERTY_DMAAP_SOURCE_TOPICS + + "." + topic + PolicyEndPointProperties.PROPERTY_DMAAP_DME2_AFT_ENVIRONMENT_SUFFIX); + + String dme2Partner = properties.getProperty(PolicyEndPointProperties.PROPERTY_DMAAP_SOURCE_TOPICS + "." + + topic + PolicyEndPointProperties.PROPERTY_DMAAP_DME2_PARTNER_SUFFIX); + + String dme2RouteOffer = properties.getProperty(PolicyEndPointProperties.PROPERTY_DMAAP_SOURCE_TOPICS + + "." + topic + PolicyEndPointProperties.PROPERTY_DMAAP_DME2_ROUTE_OFFER_SUFFIX); + + String dme2Latitude = properties.getProperty(PolicyEndPointProperties.PROPERTY_DMAAP_SOURCE_TOPICS + "." + + topic + PolicyEndPointProperties.PROPERTY_DMAAP_DME2_LATITUDE_SUFFIX); + + String dme2Longitude = properties.getProperty(PolicyEndPointProperties.PROPERTY_DMAAP_SOURCE_TOPICS + + "." + topic + PolicyEndPointProperties.PROPERTY_DMAAP_DME2_LONGITUDE_SUFFIX); + + String dme2EpReadTimeoutMs = + properties.getProperty(PolicyEndPointProperties.PROPERTY_DMAAP_SOURCE_TOPICS + "." + topic + + PolicyEndPointProperties.PROPERTY_DMAAP_DME2_EP_READ_TIMEOUT_MS_SUFFIX); + + String dme2EpConnTimeout = properties.getProperty(PolicyEndPointProperties.PROPERTY_DMAAP_SOURCE_TOPICS + + "." + topic + PolicyEndPointProperties.PROPERTY_DMAAP_DME2_EP_CONN_TIMEOUT_SUFFIX); + + String dme2RoundtripTimeoutMs = + properties.getProperty(PolicyEndPointProperties.PROPERTY_DMAAP_SOURCE_TOPICS + "." + topic + + PolicyEndPointProperties.PROPERTY_DMAAP_DME2_ROUNDTRIP_TIMEOUT_MS_SUFFIX); + + String dme2Version = properties.getProperty(PolicyEndPointProperties.PROPERTY_DMAAP_SOURCE_TOPICS + "." + + topic + PolicyEndPointProperties.PROPERTY_DMAAP_DME2_VERSION_SUFFIX); + + String dme2SubContextPath = properties.getProperty(PolicyEndPointProperties.PROPERTY_DMAAP_SOURCE_TOPICS + + "." + topic + PolicyEndPointProperties.PROPERTY_DMAAP_DME2_SUB_CONTEXT_PATH_SUFFIX); + + String dme2SessionStickinessRequired = + properties.getProperty(PolicyEndPointProperties.PROPERTY_DMAAP_SOURCE_TOPICS + "." + topic + + PolicyEndPointProperties.PROPERTY_DMAAP_DME2_SESSION_STICKINESS_REQUIRED_SUFFIX); + + Map<String, String> dme2AdditionalProps = new HashMap<>(); + + if (dme2EpReadTimeoutMs != null && !dme2EpReadTimeoutMs.isEmpty()) { + dme2AdditionalProps.put(DME2_READ_TIMEOUT_PROPERTY, dme2EpReadTimeoutMs); + } + if (dme2EpConnTimeout != null && !dme2EpConnTimeout.isEmpty()) { + dme2AdditionalProps.put(DME2_EP_CONN_TIMEOUT_PROPERTY, dme2EpConnTimeout); + } + if (dme2RoundtripTimeoutMs != null && !dme2RoundtripTimeoutMs.isEmpty()) { + dme2AdditionalProps.put(DME2_ROUNDTRIP_TIMEOUT_PROPERTY, dme2RoundtripTimeoutMs); + } + if (dme2Version != null && !dme2Version.isEmpty()) { + dme2AdditionalProps.put(DME2_VERSION_PROPERTY, dme2Version); + } + if (dme2RouteOffer != null && !dme2RouteOffer.isEmpty()) { + dme2AdditionalProps.put(DME2_ROUTE_OFFER_PROPERTY, dme2RouteOffer); + } + if (dme2SubContextPath != null && !dme2SubContextPath.isEmpty()) { + dme2AdditionalProps.put(DME2_SUBCONTEXT_PATH_PROPERTY, dme2SubContextPath); + } + if (dme2SessionStickinessRequired != null && !dme2SessionStickinessRequired.isEmpty()) { + dme2AdditionalProps.put(DME2_SESSION_STICKINESS_REQUIRED_PROPERTY, dme2SessionStickinessRequired); + } + + + if (servers == null || servers.isEmpty()) { + + logger.error("{}: no DMaaP servers or DME2 ServiceName provided", this); + continue; + } + + int fetchTimeout = DmaapTopicSource.DEFAULT_TIMEOUT_MS_FETCH; + if (fetchTimeoutString != null && !fetchTimeoutString.isEmpty()) { + try { + fetchTimeout = Integer.parseInt(fetchTimeoutString); + } catch (NumberFormatException nfe) { + logger.warn("{}: fetch timeout {} is in invalid format for topic {} ", this, fetchTimeoutString, + topic); + } + } + + String fetchLimitString = properties.getProperty(PolicyEndPointProperties.PROPERTY_DMAAP_SOURCE_TOPICS + + "." + topic + PolicyEndPointProperties.PROPERTY_TOPIC_SOURCE_FETCH_LIMIT_SUFFIX); + int fetchLimit = DmaapTopicSource.DEFAULT_LIMIT_FETCH; + if (fetchLimitString != null && !fetchLimitString.isEmpty()) { + try { + fetchLimit = Integer.parseInt(fetchLimitString); + } catch (NumberFormatException nfe) { + logger.warn("{}: fetch limit {} is in invalid format for topic {} ", this, fetchLimitString, + topic); + } + } + + String managedString = properties.getProperty(PolicyEndPointProperties.PROPERTY_DMAAP_SOURCE_TOPICS + + "." + topic + PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX); + boolean managed = true; + if (managedString != null && !managedString.isEmpty()) { + managed = Boolean.parseBoolean(managedString); + } + + String useHttpsString = properties.getProperty(PolicyEndPointProperties.PROPERTY_DMAAP_SOURCE_TOPICS + + "." + topic + PolicyEndPointProperties.PROPERTY_HTTP_HTTPS_SUFFIX); + + // default is to use HTTP if no https property exists + boolean useHttps = false; + if (useHttpsString != null && !useHttpsString.isEmpty()) { + useHttps = Boolean.parseBoolean(useHttpsString); + } + + String allowSelfSignedCertsString = + properties.getProperty(PolicyEndPointProperties.PROPERTY_DMAAP_SOURCE_TOPICS + "." + topic + + PolicyEndPointProperties.PROPERTY_ALLOW_SELF_SIGNED_CERTIFICATES_SUFFIX); + + // default is to disallow self-signed certs + boolean allowSelfSignedCerts = false; + if (allowSelfSignedCertsString != null && !allowSelfSignedCertsString.isEmpty()) { + allowSelfSignedCerts = Boolean.parseBoolean(allowSelfSignedCertsString); + } + + + DmaapTopicSource uebTopicSource = this.build(serverList, topic, apiKey, apiSecret, aafMechId, + aafPassword, consumerGroup, consumerInstance, fetchTimeout, fetchLimit, dme2Environment, + dme2AftEnvironment, dme2Partner, dme2Latitude, dme2Longitude, dme2AdditionalProps, managed, + useHttps, allowSelfSignedCerts); + + dmaapTopicSourceLst.add(uebTopicSource); + } + } + return dmaapTopicSourceLst; + } + + /** + * {@inheritDoc} + * + * @throws IllegalArgumentException + */ + @Override + public DmaapTopicSource build(List<String> servers, String topic, String apiKey, String apiSecret) { + return this.build(servers, topic, apiKey, apiSecret, null, null, null, null, + DmaapTopicSource.DEFAULT_TIMEOUT_MS_FETCH, DmaapTopicSource.DEFAULT_LIMIT_FETCH, true, false, false); + } + + /** + * {@inheritDoc} + * + * @throws IllegalArgumentException + */ + @Override + public DmaapTopicSource build(List<String> servers, String topic) { + return this.build(servers, topic, null, null); + } + + /** + * {@inheritDoc} + */ + @Override + public void destroy(String topic) { + + if (topic == null || topic.isEmpty()) { + throw new IllegalArgumentException(MISSING_TOPIC); + } + + DmaapTopicSource uebTopicSource; + + synchronized (this) { + if (!dmaapTopicSources.containsKey(topic)) { + return; + } + + uebTopicSource = dmaapTopicSources.remove(topic); + } + + uebTopicSource.shutdown(); + } + + /** + * {@inheritDoc} + */ + @Override + public DmaapTopicSource get(String topic) { + + if (topic == null || topic.isEmpty()) { + throw new IllegalArgumentException(MISSING_TOPIC); + } + + synchronized (this) { + if (dmaapTopicSources.containsKey(topic)) { + return dmaapTopicSources.get(topic); + } else { + throw new IllegalArgumentException("DmaapTopiceSource for " + topic + " not found"); + } + } + } + + @Override + public synchronized List<DmaapTopicSource> inventory() { + return new ArrayList<>(this.dmaapTopicSources.values()); + } + + @Override + public void destroy() { + List<DmaapTopicSource> readers = this.inventory(); + for (DmaapTopicSource reader : readers) { + reader.shutdown(); + } + + synchronized (this) { + this.dmaapTopicSources.clear(); + } + } + + @Override + public String toString() { + StringBuilder builder = new StringBuilder(); + builder.append("IndexedDmaapTopicSourceFactory []"); + return builder.toString(); + } + +} + diff --git a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/event/comm/bus/NoopTopicSink.java b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/event/comm/bus/NoopTopicSink.java index c6cbf343..6f9f0adc 100644 --- a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/event/comm/bus/NoopTopicSink.java +++ b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/event/comm/bus/NoopTopicSink.java @@ -33,6 +33,11 @@ import org.slf4j.LoggerFactory; public class NoopTopicSink extends TopicBase implements TopicSink { /** + * factory + */ + public static final NoopTopicSinkFactory factory = new IndexedNoopTopicSinkFactory(); + + /** * logger */ private static Logger logger = LoggerFactory.getLogger(NoopTopicSink.class); diff --git a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/event/comm/bus/NoopTopicSinkFactory.java b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/event/comm/bus/NoopTopicSinkFactory.java index c555d94f..ee1672d7 100644 --- a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/event/comm/bus/NoopTopicSinkFactory.java +++ b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/event/comm/bus/NoopTopicSinkFactory.java @@ -20,9 +20,16 @@ package org.onap.policy.common.endpoints.event.comm.bus; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; import java.util.List; import java.util.Properties; +import org.onap.policy.common.endpoints.properties.PolicyEndPointProperties; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + /** * Noop Topic Sink Factory */ @@ -80,3 +87,145 @@ public interface NoopTopicSinkFactory { */ public void destroy(); } + + +/* ------------- implementation ----------------- */ + +/** + * Factory of noop sinks + */ +class IndexedNoopTopicSinkFactory implements NoopTopicSinkFactory { + private static final String MISSING_TOPIC = "A topic must be provided"; + + /** + * Logger + */ + private static Logger logger = LoggerFactory.getLogger(IndexedUebTopicSinkFactory.class); + + /** + * noop topic sinks map + */ + protected HashMap<String, NoopTopicSink> noopTopicSinks = new HashMap<>(); + + @Override + public List<NoopTopicSink> build(Properties properties) { + + final String sinkTopics = properties.getProperty(PolicyEndPointProperties.PROPERTY_NOOP_SINK_TOPICS); + if (sinkTopics == null || sinkTopics.isEmpty()) { + logger.info("{}: no topic for noop sink", this); + return new ArrayList<>(); + } + + final List<String> sinkTopicList = new ArrayList<>(Arrays.asList(sinkTopics.split("\\s*,\\s*"))); + final List<NoopTopicSink> newSinks = new ArrayList<>(); + synchronized (this) { + for (final String topic : sinkTopicList) { + if (this.noopTopicSinks.containsKey(topic)) { + newSinks.add(this.noopTopicSinks.get(topic)); + continue; + } + + String servers = properties.getProperty(PolicyEndPointProperties.PROPERTY_NOOP_SINK_TOPICS + "." + topic + + PolicyEndPointProperties.PROPERTY_TOPIC_SERVERS_SUFFIX); + + if (servers == null || servers.isEmpty()) { + servers = "noop"; + } + + final List<String> serverList = new ArrayList<>(Arrays.asList(servers.split("\\s*,\\s*"))); + + final String managedString = properties.getProperty(PolicyEndPointProperties.PROPERTY_NOOP_SINK_TOPICS + + "." + topic + PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX); + boolean managed = true; + if (managedString != null && !managedString.isEmpty()) { + managed = Boolean.parseBoolean(managedString); + } + + final NoopTopicSink noopSink = this.build(serverList, topic, managed); + newSinks.add(noopSink); + } + return newSinks; + } + } + + @Override + public NoopTopicSink build(List<String> servers, String topic, boolean managed) { + + List<String> noopSinkServers = servers; + if (noopSinkServers == null) { + noopSinkServers = new ArrayList<>(); + } + + if (noopSinkServers.isEmpty()) { + noopSinkServers.add("noop"); + } + + if (topic == null || topic.isEmpty()) { + throw new IllegalArgumentException(MISSING_TOPIC); + } + + synchronized (this) { + if (this.noopTopicSinks.containsKey(topic)) { + return this.noopTopicSinks.get(topic); + } + + final NoopTopicSink sink = new NoopTopicSink(noopSinkServers, topic); + + if (managed) { + this.noopTopicSinks.put(topic, sink); + } + + return sink; + } + } + + @Override + public void destroy(String topic) { + if (topic == null || topic.isEmpty()) { + throw new IllegalArgumentException(MISSING_TOPIC); + } + + NoopTopicSink noopSink; + synchronized (this) { + if (!this.noopTopicSinks.containsKey(topic)) { + return; + } + + noopSink = this.noopTopicSinks.remove(topic); + } + + noopSink.shutdown(); + } + + @Override + public void destroy() { + final List<NoopTopicSink> sinks = this.inventory(); + for (final NoopTopicSink sink : sinks) { + sink.shutdown(); + } + + synchronized (this) { + this.noopTopicSinks.clear(); + } + } + + @Override + public NoopTopicSink get(String topic) { + if (topic == null || topic.isEmpty()) { + throw new IllegalArgumentException(MISSING_TOPIC); + } + + synchronized (this) { + if (this.noopTopicSinks.containsKey(topic)) { + return this.noopTopicSinks.get(topic); + } else { + throw new IllegalStateException("DmaapTopicSink for " + topic + " not found"); + } + } + } + + @Override + public List<NoopTopicSink> inventory() { + return new ArrayList<>(this.noopTopicSinks.values()); + } +} diff --git a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/event/comm/bus/UebTopicSink.java b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/event/comm/bus/UebTopicSink.java index 0e9398de..b6e4acbe 100644 --- a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/event/comm/bus/UebTopicSink.java +++ b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/event/comm/bus/UebTopicSink.java @@ -25,4 +25,8 @@ package org.onap.policy.common.endpoints.event.comm.bus; */ public interface UebTopicSink extends BusTopicSink { + /** + * Factory of UEB Topic Sinks for instantiation and management purposes + */ + public static final UebTopicSinkFactory factory = new IndexedUebTopicSinkFactory(); } diff --git a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/event/comm/bus/UebTopicSinkFactory.java b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/event/comm/bus/UebTopicSinkFactory.java index 37920635..a522e2c5 100644 --- a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/event/comm/bus/UebTopicSinkFactory.java +++ b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/event/comm/bus/UebTopicSinkFactory.java @@ -20,9 +20,17 @@ package org.onap.policy.common.endpoints.event.comm.bus; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; import java.util.List; import java.util.Properties; +import org.onap.policy.common.endpoints.event.comm.bus.internal.InlineUebTopicSink; +import org.onap.policy.common.endpoints.properties.PolicyEndPointProperties; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + /** * UEB Topic Sink Factory */ @@ -96,3 +104,188 @@ public interface UebTopicSinkFactory { */ public void destroy(); } + + +/* ------------- implementation ----------------- */ + +/** + * Factory of UEB Reader Topics indexed by topic name + */ +class IndexedUebTopicSinkFactory implements UebTopicSinkFactory { + private static final String MISSING_TOPIC = "A topic must be provided"; + + /** + * Logger + */ + private static Logger logger = LoggerFactory.getLogger(IndexedUebTopicSinkFactory.class); + + /** + * UEB Topic Name Index + */ + protected HashMap<String, UebTopicSink> uebTopicSinks = new HashMap<>(); + + @Override + public UebTopicSink build(List<String> servers, String topic, String apiKey, String apiSecret, String partitionKey, + boolean managed, boolean useHttps, boolean allowSelfSignedCerts) { + + if (servers == null || servers.isEmpty()) { + throw new IllegalArgumentException("UEB Server(s) must be provided"); + } + + if (topic == null || topic.isEmpty()) { + throw new IllegalArgumentException(MISSING_TOPIC); + } + + synchronized (this) { + if (uebTopicSinks.containsKey(topic)) { + return uebTopicSinks.get(topic); + } + + UebTopicSink uebTopicWriter = new InlineUebTopicSink(servers, topic, apiKey, apiSecret, partitionKey, + useHttps, allowSelfSignedCerts); + + if (managed) { + uebTopicSinks.put(topic, uebTopicWriter); + } + + return uebTopicWriter; + } + } + + + @Override + public UebTopicSink build(List<String> servers, String topic) { + return this.build(servers, topic, null, null, null, true, false, false); + } + + + @Override + public List<UebTopicSink> build(Properties properties) { + + String writeTopics = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SINK_TOPICS); + if (writeTopics == null || writeTopics.isEmpty()) { + logger.info("{}: no topic for UEB Sink", this); + return new ArrayList<>(); + } + + List<String> writeTopicList = new ArrayList<>(Arrays.asList(writeTopics.split("\\s*,\\s*"))); + List<UebTopicSink> newUebTopicSinks = new ArrayList<>(); + synchronized (this) { + for (String topic : writeTopicList) { + if (this.uebTopicSinks.containsKey(topic)) { + newUebTopicSinks.add(this.uebTopicSinks.get(topic)); + continue; + } + + String servers = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SINK_TOPICS + "." + topic + + PolicyEndPointProperties.PROPERTY_TOPIC_SERVERS_SUFFIX); + if (servers == null || servers.isEmpty()) { + logger.error("{}: no UEB servers configured for sink {}", this, topic); + continue; + } + + List<String> serverList = new ArrayList<>(Arrays.asList(servers.split("\\s*,\\s*"))); + + String apiKey = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SINK_TOPICS + "." + topic + + PolicyEndPointProperties.PROPERTY_TOPIC_API_KEY_SUFFIX); + String apiSecret = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SINK_TOPICS + "." + + topic + PolicyEndPointProperties.PROPERTY_TOPIC_API_SECRET_SUFFIX); + String partitionKey = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SINK_TOPICS + "." + + topic + PolicyEndPointProperties.PROPERTY_TOPIC_SINK_PARTITION_KEY_SUFFIX); + + String managedString = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SINK_TOPICS + "." + + topic + PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX); + boolean managed = true; + if (managedString != null && !managedString.isEmpty()) { + managed = Boolean.parseBoolean(managedString); + } + + String useHttpsString = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SINK_TOPICS + "." + + topic + PolicyEndPointProperties.PROPERTY_HTTP_HTTPS_SUFFIX); + + // default is to use HTTP if no https property exists + boolean useHttps = false; + if (useHttpsString != null && !useHttpsString.isEmpty()) { + useHttps = Boolean.parseBoolean(useHttpsString); + } + + + String allowSelfSignedCertsString = + properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SINK_TOPICS + "." + topic + + PolicyEndPointProperties.PROPERTY_ALLOW_SELF_SIGNED_CERTIFICATES_SUFFIX); + + // default is to disallow self-signed certs + boolean allowSelfSignedCerts = false; + if (allowSelfSignedCertsString != null && !allowSelfSignedCertsString.isEmpty()) { + allowSelfSignedCerts = Boolean.parseBoolean(allowSelfSignedCertsString); + } + + UebTopicSink uebTopicWriter = this.build(serverList, topic, apiKey, apiSecret, partitionKey, managed, + useHttps, allowSelfSignedCerts); + newUebTopicSinks.add(uebTopicWriter); + } + return newUebTopicSinks; + } + } + + @Override + public void destroy(String topic) { + + if (topic == null || topic.isEmpty()) { + throw new IllegalArgumentException(MISSING_TOPIC); + } + + UebTopicSink uebTopicWriter; + synchronized (this) { + if (!uebTopicSinks.containsKey(topic)) { + return; + } + + uebTopicWriter = uebTopicSinks.remove(topic); + } + + uebTopicWriter.shutdown(); + } + + @Override + public void destroy() { + List<UebTopicSink> writers = this.inventory(); + for (UebTopicSink writer : writers) { + writer.shutdown(); + } + + synchronized (this) { + this.uebTopicSinks.clear(); + } + } + + @Override + public UebTopicSink get(String topic) { + + if (topic == null || topic.isEmpty()) { + throw new IllegalArgumentException(MISSING_TOPIC); + } + + synchronized (this) { + if (uebTopicSinks.containsKey(topic)) { + return uebTopicSinks.get(topic); + } else { + throw new IllegalStateException("UebTopicSink for " + topic + " not found"); + } + } + } + + @Override + public synchronized List<UebTopicSink> inventory() { + return new ArrayList<>(this.uebTopicSinks.values()); + } + + + @Override + public String toString() { + StringBuilder builder = new StringBuilder(); + builder.append("IndexedUebTopicSinkFactory []"); + return builder.toString(); + } + +} diff --git a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/event/comm/bus/UebTopicSource.java b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/event/comm/bus/UebTopicSource.java index db14800c..25bbce62 100644 --- a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/event/comm/bus/UebTopicSource.java +++ b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/event/comm/bus/UebTopicSource.java @@ -26,4 +26,8 @@ package org.onap.policy.common.endpoints.event.comm.bus; */ public interface UebTopicSource extends BusTopicSource { + /** + * factory for managing and tracking UEB readers + */ + public static UebTopicSourceFactory factory = new IndexedUebTopicSourceFactory(); } diff --git a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/event/comm/bus/UebTopicSourceFactory.java b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/event/comm/bus/UebTopicSourceFactory.java index d3d632e0..c6cf3095 100644 --- a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/event/comm/bus/UebTopicSourceFactory.java +++ b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/event/comm/bus/UebTopicSourceFactory.java @@ -20,9 +20,17 @@ package org.onap.policy.common.endpoints.event.comm.bus; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; import java.util.List; import java.util.Properties; +import org.onap.policy.common.endpoints.event.comm.bus.internal.SingleThreadedUebTopicSource; +import org.onap.policy.common.endpoints.properties.PolicyEndPointProperties; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + /** * UEB Topic Source Factory */ @@ -112,3 +120,240 @@ public interface UebTopicSourceFactory { */ public List<UebTopicSource> inventory(); } + + +/* ------------- implementation ----------------- */ + +/** + * Factory of UEB Source Topics indexed by topic name + */ +class IndexedUebTopicSourceFactory implements UebTopicSourceFactory { + private static final String MISSING_TOPIC = "A topic must be provided"; + + /** + * Logger + */ + private static Logger logger = LoggerFactory.getLogger(IndexedUebTopicSourceFactory.class); + + /** + * UEB Topic Name Index + */ + protected HashMap<String, UebTopicSource> uebTopicSources = new HashMap<>(); + + /** + * {@inheritDoc} + */ + @Override + public UebTopicSource build(List<String> servers, String topic, String apiKey, String apiSecret, + String consumerGroup, String consumerInstance, int fetchTimeout, int fetchLimit, boolean managed, + boolean useHttps, boolean allowSelfSignedCerts) { + if (servers == null || servers.isEmpty()) { + throw new IllegalArgumentException("UEB Server(s) must be provided"); + } + + if (topic == null || topic.isEmpty()) { + throw new IllegalArgumentException(MISSING_TOPIC); + } + + synchronized (this) { + if (uebTopicSources.containsKey(topic)) { + return uebTopicSources.get(topic); + } + + UebTopicSource uebTopicSource = new SingleThreadedUebTopicSource(servers, topic, apiKey, apiSecret, + consumerGroup, consumerInstance, fetchTimeout, fetchLimit, useHttps, allowSelfSignedCerts); + + if (managed) { + uebTopicSources.put(topic, uebTopicSource); + } + + return uebTopicSource; + } + } + + /** + * {@inheritDoc} + */ + @Override + public List<UebTopicSource> build(Properties properties) { + + String readTopics = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SOURCE_TOPICS); + if (readTopics == null || readTopics.isEmpty()) { + logger.info("{}: no topic for UEB Source", this); + return new ArrayList<>(); + } + List<String> readTopicList = new ArrayList<>(Arrays.asList(readTopics.split("\\s*,\\s*"))); + + List<UebTopicSource> newUebTopicSources = new ArrayList<>(); + synchronized (this) { + for (String topic : readTopicList) { + if (this.uebTopicSources.containsKey(topic)) { + newUebTopicSources.add(this.uebTopicSources.get(topic)); + continue; + } + + String servers = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SOURCE_TOPICS + "." + + topic + PolicyEndPointProperties.PROPERTY_TOPIC_SERVERS_SUFFIX); + + if (servers == null || servers.isEmpty()) { + logger.error("{}: no UEB servers configured for sink {}", this, topic); + continue; + } + + List<String> serverList = new ArrayList<>(Arrays.asList(servers.split("\\s*,\\s*"))); + + String apiKey = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SOURCE_TOPICS + "." + topic + + PolicyEndPointProperties.PROPERTY_TOPIC_API_KEY_SUFFIX); + + String apiSecret = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SOURCE_TOPICS + "." + + topic + PolicyEndPointProperties.PROPERTY_TOPIC_API_SECRET_SUFFIX); + + String consumerGroup = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SOURCE_TOPICS + "." + + topic + PolicyEndPointProperties.PROPERTY_TOPIC_SOURCE_CONSUMER_GROUP_SUFFIX); + + String consumerInstance = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SOURCE_TOPICS + + "." + topic + PolicyEndPointProperties.PROPERTY_TOPIC_SOURCE_CONSUMER_INSTANCE_SUFFIX); + + String fetchTimeoutString = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SOURCE_TOPICS + + "." + topic + PolicyEndPointProperties.PROPERTY_TOPIC_SOURCE_FETCH_TIMEOUT_SUFFIX); + int fetchTimeout = UebTopicSource.DEFAULT_TIMEOUT_MS_FETCH; + if (fetchTimeoutString != null && !fetchTimeoutString.isEmpty()) { + try { + fetchTimeout = Integer.parseInt(fetchTimeoutString); + } catch (NumberFormatException nfe) { + logger.warn("{}: fetch timeout {} is in invalid format for topic {} ", this, fetchTimeoutString, + topic); + } + } + + String fetchLimitString = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SOURCE_TOPICS + + "." + topic + PolicyEndPointProperties.PROPERTY_TOPIC_SOURCE_FETCH_LIMIT_SUFFIX); + int fetchLimit = UebTopicSource.DEFAULT_LIMIT_FETCH; + if (fetchLimitString != null && !fetchLimitString.isEmpty()) { + try { + fetchLimit = Integer.parseInt(fetchLimitString); + } catch (NumberFormatException nfe) { + logger.warn("{}: fetch limit {} is in invalid format for topic {} ", this, fetchLimitString, + topic); + } + } + + String managedString = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SOURCE_TOPICS + "." + + topic + PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX); + boolean managed = true; + if (managedString != null && !managedString.isEmpty()) { + managed = Boolean.parseBoolean(managedString); + } + + String useHttpsString = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SOURCE_TOPICS + "." + + topic + PolicyEndPointProperties.PROPERTY_HTTP_HTTPS_SUFFIX); + + // default is to use HTTP if no https property exists + boolean useHttps = false; + if (useHttpsString != null && !useHttpsString.isEmpty()) { + useHttps = Boolean.parseBoolean(useHttpsString); + } + + String allowSelfSignedCertsString = + properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SOURCE_TOPICS + "." + topic + + PolicyEndPointProperties.PROPERTY_ALLOW_SELF_SIGNED_CERTIFICATES_SUFFIX); + + // default is to disallow self-signed certs + boolean allowSelfSignedCerts = false; + if (allowSelfSignedCertsString != null && !allowSelfSignedCertsString.isEmpty()) { + allowSelfSignedCerts = Boolean.parseBoolean(allowSelfSignedCertsString); + } + + UebTopicSource uebTopicSource = this.build(serverList, topic, apiKey, apiSecret, consumerGroup, + consumerInstance, fetchTimeout, fetchLimit, managed, useHttps, allowSelfSignedCerts); + newUebTopicSources.add(uebTopicSource); + } + } + return newUebTopicSources; + } + + /** + * {@inheritDoc} + */ + @Override + public UebTopicSource build(List<String> servers, String topic, String apiKey, String apiSecret) { + + return this.build(servers, topic, apiKey, apiSecret, null, null, UebTopicSource.DEFAULT_TIMEOUT_MS_FETCH, + UebTopicSource.DEFAULT_LIMIT_FETCH, true, false, true); + } + + /** + * {@inheritDoc} + */ + @Override + public UebTopicSource build(List<String> servers, String topic) { + return this.build(servers, topic, null, null); + } + + /** + * {@inheritDoc} + */ + @Override + public void destroy(String topic) { + + if (topic == null || topic.isEmpty()) { + throw new IllegalArgumentException(MISSING_TOPIC); + } + + UebTopicSource uebTopicSource; + + synchronized (this) { + if (!uebTopicSources.containsKey(topic)) { + return; + } + + uebTopicSource = uebTopicSources.remove(topic); + } + + uebTopicSource.shutdown(); + } + + /** + * {@inheritDoc} + */ + @Override + public UebTopicSource get(String topic) { + + if (topic == null || topic.isEmpty()) { + throw new IllegalArgumentException(MISSING_TOPIC); + } + + synchronized (this) { + if (uebTopicSources.containsKey(topic)) { + return uebTopicSources.get(topic); + } else { + throw new IllegalStateException("UebTopiceSource for " + topic + " not found"); + } + } + } + + @Override + public synchronized List<UebTopicSource> inventory() { + return new ArrayList<>(this.uebTopicSources.values()); + } + + @Override + public void destroy() { + List<UebTopicSource> readers = this.inventory(); + for (UebTopicSource reader : readers) { + reader.shutdown(); + } + + synchronized (this) { + this.uebTopicSources.clear(); + } + } + + @Override + public String toString() { + StringBuilder builder = new StringBuilder(); + builder.append("IndexedUebTopicSourceFactory []"); + return builder.toString(); + } + +} diff --git a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/event/comm/bus/impl/IndexedDmaapTopicSinkFactory.java b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/event/comm/bus/impl/IndexedDmaapTopicSinkFactory.java deleted file mode 100644 index e252988e..00000000 --- a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/event/comm/bus/impl/IndexedDmaapTopicSinkFactory.java +++ /dev/null @@ -1,325 +0,0 @@ -/* - * ============LICENSE_START======================================================= - * policy-endpoints - * ================================================================================ - * Copyright (C) 2017-2018 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. - * ============LICENSE_END========================================================= - */ - -package org.onap.policy.common.endpoints.event.comm.bus.impl; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Properties; - -import org.onap.policy.common.endpoints.event.comm.bus.DmaapTopicSink; -import org.onap.policy.common.endpoints.event.comm.bus.DmaapTopicSinkFactory; -import org.onap.policy.common.endpoints.event.comm.bus.internal.InlineDmaapTopicSink; -import org.onap.policy.common.endpoints.properties.PolicyEndPointProperties; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * Factory of DMAAP Reader Topics indexed by topic name - */ -public class IndexedDmaapTopicSinkFactory implements DmaapTopicSinkFactory { - private static final String MISSING_TOPIC = "A topic must be provided"; - - private static final IndexedDmaapTopicSinkFactory instance = new IndexedDmaapTopicSinkFactory(); - /** - * Logger - */ - private static Logger logger = LoggerFactory.getLogger(IndexedDmaapTopicSinkFactory.class); - - /** - * DMAAP Topic Name Index - */ - protected HashMap<String, DmaapTopicSink> dmaapTopicWriters = new HashMap<>(); - - /** - * Get the singleton instance. - * - * @return the instance - */ - public static IndexedDmaapTopicSinkFactory getInstance() { - return instance; - } - - private IndexedDmaapTopicSinkFactory() {} - - @Override - public DmaapTopicSink build(List<String> servers, String topic, String apiKey, String apiSecret, String userName, - String password, String partitionKey, String environment, String aftEnvironment, String partner, - String latitude, String longitude, Map<String, String> additionalProps, boolean managed, boolean useHttps, - boolean allowSelfSignedCerts) { - - if (topic == null || topic.isEmpty()) { - throw new IllegalArgumentException(MISSING_TOPIC); - } - - synchronized (this) { - if (dmaapTopicWriters.containsKey(topic)) { - return dmaapTopicWriters.get(topic); - } - - DmaapTopicSink dmaapTopicSink = new InlineDmaapTopicSink(servers, topic, apiKey, apiSecret, userName, - password, partitionKey, environment, aftEnvironment, partner, latitude, longitude, additionalProps, - useHttps, allowSelfSignedCerts); - - if (managed) { - dmaapTopicWriters.put(topic, dmaapTopicSink); - } - return dmaapTopicSink; - } - } - - @Override - public DmaapTopicSink build(List<String> servers, String topic, String apiKey, String apiSecret, String userName, - String password, String partitionKey, boolean managed, boolean useHttps, boolean allowSelfSignedCerts) { - - if (topic == null || topic.isEmpty()) { - throw new IllegalArgumentException(MISSING_TOPIC); - } - - synchronized (this) { - if (dmaapTopicWriters.containsKey(topic)) { - return dmaapTopicWriters.get(topic); - } - - DmaapTopicSink dmaapTopicSink = new InlineDmaapTopicSink(servers, topic, apiKey, apiSecret, userName, - password, partitionKey, useHttps, allowSelfSignedCerts); - - if (managed) { - dmaapTopicWriters.put(topic, dmaapTopicSink); - } - return dmaapTopicSink; - } - } - - @Override - public DmaapTopicSink build(List<String> servers, String topic) { - return this.build(servers, topic, null, null, null, null, null, true, false, false); - } - - @Override - public List<DmaapTopicSink> build(Properties properties) { - - String writeTopics = properties.getProperty(PolicyEndPointProperties.PROPERTY_DMAAP_SINK_TOPICS); - if (writeTopics == null || writeTopics.isEmpty()) { - logger.info("{}: no topic for DMaaP Sink", this); - return new ArrayList<>(); - } - - List<String> writeTopicList = new ArrayList<>(Arrays.asList(writeTopics.split("\\s*,\\s*"))); - List<DmaapTopicSink> newDmaapTopicSinks = new ArrayList<>(); - synchronized (this) { - for (String topic : writeTopicList) { - if (this.dmaapTopicWriters.containsKey(topic)) { - newDmaapTopicSinks.add(this.dmaapTopicWriters.get(topic)); - continue; - } - String servers = properties.getProperty(PolicyEndPointProperties.PROPERTY_DMAAP_SINK_TOPICS + "." - + topic + PolicyEndPointProperties.PROPERTY_TOPIC_SERVERS_SUFFIX); - - List<String> serverList; - if (servers != null && !servers.isEmpty()) { - serverList = new ArrayList<>(Arrays.asList(servers.split("\\s*,\\s*"))); - } else { - serverList = new ArrayList<>(); - } - - String apiKey = properties.getProperty(PolicyEndPointProperties.PROPERTY_DMAAP_SINK_TOPICS + "." + topic - + PolicyEndPointProperties.PROPERTY_TOPIC_API_KEY_SUFFIX); - String apiSecret = properties.getProperty(PolicyEndPointProperties.PROPERTY_DMAAP_SINK_TOPICS + "." - + topic + PolicyEndPointProperties.PROPERTY_TOPIC_API_SECRET_SUFFIX); - - String aafMechId = properties.getProperty(PolicyEndPointProperties.PROPERTY_DMAAP_SINK_TOPICS + "." - + topic + PolicyEndPointProperties.PROPERTY_TOPIC_AAF_MECHID_SUFFIX); - String aafPassword = properties.getProperty(PolicyEndPointProperties.PROPERTY_DMAAP_SINK_TOPICS + "." - + topic + PolicyEndPointProperties.PROPERTY_TOPIC_AAF_PASSWORD_SUFFIX); - - String partitionKey = properties.getProperty(PolicyEndPointProperties.PROPERTY_DMAAP_SINK_TOPICS + "." - + topic + PolicyEndPointProperties.PROPERTY_TOPIC_SINK_PARTITION_KEY_SUFFIX); - - String managedString = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SINK_TOPICS + "." - + topic + PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX); - - /* DME2 Properties */ - - String dme2Environment = properties.getProperty(PolicyEndPointProperties.PROPERTY_DMAAP_SINK_TOPICS - + "." + topic + PolicyEndPointProperties.PROPERTY_DMAAP_DME2_ENVIRONMENT_SUFFIX); - - String dme2AftEnvironment = properties.getProperty(PolicyEndPointProperties.PROPERTY_DMAAP_SINK_TOPICS - + "." + topic + PolicyEndPointProperties.PROPERTY_DMAAP_DME2_AFT_ENVIRONMENT_SUFFIX); - - String dme2Partner = properties.getProperty(PolicyEndPointProperties.PROPERTY_DMAAP_SINK_TOPICS + "." - + topic + PolicyEndPointProperties.PROPERTY_DMAAP_DME2_PARTNER_SUFFIX); - - String dme2RouteOffer = properties.getProperty(PolicyEndPointProperties.PROPERTY_DMAAP_SINK_TOPICS + "." - + topic + PolicyEndPointProperties.PROPERTY_DMAAP_DME2_ROUTE_OFFER_SUFFIX); - - String dme2Latitude = properties.getProperty(PolicyEndPointProperties.PROPERTY_DMAAP_SINK_TOPICS + "." - + topic + PolicyEndPointProperties.PROPERTY_DMAAP_DME2_LATITUDE_SUFFIX); - - String dme2Longitude = properties.getProperty(PolicyEndPointProperties.PROPERTY_DMAAP_SINK_TOPICS + "." - + topic + PolicyEndPointProperties.PROPERTY_DMAAP_DME2_LONGITUDE_SUFFIX); - - String dme2EpReadTimeoutMs = properties.getProperty(PolicyEndPointProperties.PROPERTY_DMAAP_SINK_TOPICS - + "." + topic + PolicyEndPointProperties.PROPERTY_DMAAP_DME2_EP_READ_TIMEOUT_MS_SUFFIX); - - String dme2EpConnTimeout = properties.getProperty(PolicyEndPointProperties.PROPERTY_DMAAP_SINK_TOPICS - + "." + topic + PolicyEndPointProperties.PROPERTY_DMAAP_DME2_EP_CONN_TIMEOUT_SUFFIX); - - String dme2RoundtripTimeoutMs = - properties.getProperty(PolicyEndPointProperties.PROPERTY_DMAAP_SINK_TOPICS + "." + topic - + PolicyEndPointProperties.PROPERTY_DMAAP_DME2_ROUNDTRIP_TIMEOUT_MS_SUFFIX); - - String dme2Version = properties.getProperty(PolicyEndPointProperties.PROPERTY_DMAAP_SINK_TOPICS + "." - + topic + PolicyEndPointProperties.PROPERTY_DMAAP_DME2_VERSION_SUFFIX); - - String dme2SubContextPath = properties.getProperty(PolicyEndPointProperties.PROPERTY_DMAAP_SINK_TOPICS - + "." + topic + PolicyEndPointProperties.PROPERTY_DMAAP_DME2_SUB_CONTEXT_PATH_SUFFIX); - - String dme2SessionStickinessRequired = - properties.getProperty(PolicyEndPointProperties.PROPERTY_DMAAP_SINK_TOPICS + "." + topic - + PolicyEndPointProperties.PROPERTY_DMAAP_DME2_SESSION_STICKINESS_REQUIRED_SUFFIX); - - Map<String, String> dme2AdditionalProps = new HashMap<>(); - - if (dme2EpReadTimeoutMs != null && !dme2EpReadTimeoutMs.isEmpty()) { - dme2AdditionalProps.put(DME2_READ_TIMEOUT_PROPERTY, dme2EpReadTimeoutMs); - } - if (dme2EpConnTimeout != null && !dme2EpConnTimeout.isEmpty()) { - dme2AdditionalProps.put(DME2_EP_CONN_TIMEOUT_PROPERTY, dme2EpConnTimeout); - } - if (dme2RoundtripTimeoutMs != null && !dme2RoundtripTimeoutMs.isEmpty()) { - dme2AdditionalProps.put(DME2_ROUNDTRIP_TIMEOUT_PROPERTY, dme2RoundtripTimeoutMs); - } - if (dme2Version != null && !dme2Version.isEmpty()) { - dme2AdditionalProps.put(DME2_VERSION_PROPERTY, dme2Version); - } - if (dme2RouteOffer != null && !dme2RouteOffer.isEmpty()) { - dme2AdditionalProps.put(DME2_ROUTE_OFFER_PROPERTY, dme2RouteOffer); - } - if (dme2SubContextPath != null && !dme2SubContextPath.isEmpty()) { - dme2AdditionalProps.put(DME2_SUBCONTEXT_PATH_PROPERTY, dme2SubContextPath); - } - if (dme2SessionStickinessRequired != null && !dme2SessionStickinessRequired.isEmpty()) { - dme2AdditionalProps.put(DME2_SESSION_STICKINESS_REQUIRED_PROPERTY, dme2SessionStickinessRequired); - } - - if (servers == null || servers.isEmpty()) { - logger.error("{}: no DMaaP servers or DME2 ServiceName provided", this); - continue; - } - - boolean managed = true; - if (managedString != null && !managedString.isEmpty()) { - managed = Boolean.parseBoolean(managedString); - } - - String useHttpsString = properties.getProperty(PolicyEndPointProperties.PROPERTY_DMAAP_SINK_TOPICS + "." - + topic + PolicyEndPointProperties.PROPERTY_HTTP_HTTPS_SUFFIX); - - // default is to use HTTP if no https property exists - boolean useHttps = false; - if (useHttpsString != null && !useHttpsString.isEmpty()) { - useHttps = Boolean.parseBoolean(useHttpsString); - } - - - String allowSelfSignedCertsString = - properties.getProperty(PolicyEndPointProperties.PROPERTY_DMAAP_SINK_TOPICS + "." + topic - + PolicyEndPointProperties.PROPERTY_ALLOW_SELF_SIGNED_CERTIFICATES_SUFFIX); - - // default is to disallow self-signed certs - boolean allowSelfSignedCerts = false; - if (allowSelfSignedCertsString != null && !allowSelfSignedCertsString.isEmpty()) { - allowSelfSignedCerts = Boolean.parseBoolean(allowSelfSignedCertsString); - } - - DmaapTopicSink dmaapTopicSink = this.build(serverList, topic, apiKey, apiSecret, aafMechId, aafPassword, - partitionKey, dme2Environment, dme2AftEnvironment, dme2Partner, dme2Latitude, dme2Longitude, - dme2AdditionalProps, managed, useHttps, allowSelfSignedCerts); - - newDmaapTopicSinks.add(dmaapTopicSink); - } - return newDmaapTopicSinks; - } - } - - @Override - public void destroy(String topic) { - - if (topic == null || topic.isEmpty()) { - throw new IllegalArgumentException(MISSING_TOPIC); - } - - DmaapTopicSink dmaapTopicWriter; - synchronized (this) { - if (!dmaapTopicWriters.containsKey(topic)) { - return; - } - - dmaapTopicWriter = dmaapTopicWriters.remove(topic); - } - - dmaapTopicWriter.shutdown(); - } - - @Override - public void destroy() { - List<DmaapTopicSink> writers = this.inventory(); - for (DmaapTopicSink writer : writers) { - writer.shutdown(); - } - - synchronized (this) { - this.dmaapTopicWriters.clear(); - } - } - - @Override - public DmaapTopicSink get(String topic) { - - if (topic == null || topic.isEmpty()) { - throw new IllegalArgumentException(MISSING_TOPIC); - } - - synchronized (this) { - if (dmaapTopicWriters.containsKey(topic)) { - return dmaapTopicWriters.get(topic); - } else { - throw new IllegalStateException("DmaapTopicSink for " + topic + " not found"); - } - } - } - - @Override - public synchronized List<DmaapTopicSink> inventory() { - return new ArrayList<>(this.dmaapTopicWriters.values()); - } - - @Override - public String toString() { - StringBuilder builder = new StringBuilder(); - builder.append("IndexedDmaapTopicSinkFactory []"); - return builder.toString(); - } - -} diff --git a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/event/comm/bus/impl/IndexedDmaapTopicSourceFactory.java b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/event/comm/bus/impl/IndexedDmaapTopicSourceFactory.java deleted file mode 100644 index 0e469a1d..00000000 --- a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/event/comm/bus/impl/IndexedDmaapTopicSourceFactory.java +++ /dev/null @@ -1,401 +0,0 @@ -/* - * ============LICENSE_START======================================================= - * policy-endpoints - * ================================================================================ - * Copyright (C) 2017-2018 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. - * ============LICENSE_END========================================================= - */ - -package org.onap.policy.common.endpoints.event.comm.bus.impl; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Properties; - -import org.onap.policy.common.endpoints.event.comm.bus.DmaapTopicSource; -import org.onap.policy.common.endpoints.event.comm.bus.DmaapTopicSourceFactory; -import org.onap.policy.common.endpoints.event.comm.bus.internal.SingleThreadedDmaapTopicSource; -import org.onap.policy.common.endpoints.properties.PolicyEndPointProperties; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * Factory of DMAAP Source Topics indexed by topic name - */ - -public class IndexedDmaapTopicSourceFactory implements DmaapTopicSourceFactory { - private static final String MISSING_TOPIC = "A topic must be provided"; - - private static final IndexedDmaapTopicSourceFactory instance = new IndexedDmaapTopicSourceFactory(); - - /** - * Logger - */ - private static Logger logger = LoggerFactory.getLogger(IndexedDmaapTopicSourceFactory.class); - - /** - * DMaaP Topic Name Index - */ - protected HashMap<String, DmaapTopicSource> dmaapTopicSources = new HashMap<>(); - - /** - * Get the singleton instance. - * - * @return the instance - */ - public static IndexedDmaapTopicSourceFactory getInstance() { - return instance; - } - - private IndexedDmaapTopicSourceFactory() {} - - /** - * {@inheritDoc} - */ - @Override - public DmaapTopicSource build(List<String> servers, String topic, String apiKey, String apiSecret, String userName, - String password, String consumerGroup, String consumerInstance, int fetchTimeout, int fetchLimit, - String environment, String aftEnvironment, String partner, String latitude, String longitude, - Map<String, String> additionalProps, boolean managed, boolean useHttps, boolean allowSelfSignedCerts) { - - if (topic == null || topic.isEmpty()) { - throw new IllegalArgumentException(MISSING_TOPIC); - } - - synchronized (this) { - if (dmaapTopicSources.containsKey(topic)) { - return dmaapTopicSources.get(topic); - } - - DmaapTopicSource dmaapTopicSource = new SingleThreadedDmaapTopicSource(servers, topic, apiKey, apiSecret, - userName, password, consumerGroup, consumerInstance, fetchTimeout, fetchLimit, environment, - aftEnvironment, partner, latitude, longitude, additionalProps, useHttps, allowSelfSignedCerts); - - if (managed) { - dmaapTopicSources.put(topic, dmaapTopicSource); - } - - return dmaapTopicSource; - } - } - - /** - * {@inheritDoc} - */ - @Override - public DmaapTopicSource build(List<String> servers, String topic, String apiKey, String apiSecret, String userName, - String password, String consumerGroup, String consumerInstance, int fetchTimeout, int fetchLimit, - boolean managed, boolean useHttps, boolean allowSelfSignedCerts) { - - if (servers == null || servers.isEmpty()) { - throw new IllegalArgumentException("DMaaP Server(s) must be provided"); - } - - if (topic == null || topic.isEmpty()) { - throw new IllegalArgumentException(MISSING_TOPIC); - } - - synchronized (this) { - if (dmaapTopicSources.containsKey(topic)) { - return dmaapTopicSources.get(topic); - } - - DmaapTopicSource dmaapTopicSource = - new SingleThreadedDmaapTopicSource(servers, topic, apiKey, apiSecret, userName, password, - consumerGroup, consumerInstance, fetchTimeout, fetchLimit, useHttps, allowSelfSignedCerts); - - if (managed) { - dmaapTopicSources.put(topic, dmaapTopicSource); - } - - return dmaapTopicSource; - } - } - - /** - * {@inheritDoc} - */ - @Override - public List<DmaapTopicSource> build(Properties properties) { - - String readTopics = properties.getProperty(PolicyEndPointProperties.PROPERTY_DMAAP_SOURCE_TOPICS); - if (readTopics == null || readTopics.isEmpty()) { - logger.info("{}: no topic for DMaaP Source", this); - return new ArrayList<>(); - } - List<String> readTopicList = new ArrayList<>(Arrays.asList(readTopics.split("\\s*,\\s*"))); - - List<DmaapTopicSource> dmaapTopicSourceLst = new ArrayList<>(); - synchronized (this) { - for (String topic : readTopicList) { - if (this.dmaapTopicSources.containsKey(topic)) { - dmaapTopicSourceLst.add(this.dmaapTopicSources.get(topic)); - continue; - } - - String servers = properties.getProperty(PolicyEndPointProperties.PROPERTY_DMAAP_SOURCE_TOPICS + "." - + topic + PolicyEndPointProperties.PROPERTY_TOPIC_SERVERS_SUFFIX); - - List<String> serverList; - if (servers != null && !servers.isEmpty()) { - serverList = new ArrayList<>(Arrays.asList(servers.split("\\s*,\\s*"))); - } else { - serverList = new ArrayList<>(); - } - - String apiKey = properties.getProperty(PolicyEndPointProperties.PROPERTY_DMAAP_SOURCE_TOPICS + "." - + topic + PolicyEndPointProperties.PROPERTY_TOPIC_API_KEY_SUFFIX); - - String apiSecret = properties.getProperty(PolicyEndPointProperties.PROPERTY_DMAAP_SOURCE_TOPICS + "." - + topic + PolicyEndPointProperties.PROPERTY_TOPIC_API_SECRET_SUFFIX); - - String aafMechId = properties.getProperty(PolicyEndPointProperties.PROPERTY_DMAAP_SOURCE_TOPICS + "." - + topic + PolicyEndPointProperties.PROPERTY_TOPIC_AAF_MECHID_SUFFIX); - - String aafPassword = properties.getProperty(PolicyEndPointProperties.PROPERTY_DMAAP_SOURCE_TOPICS + "." - + topic + PolicyEndPointProperties.PROPERTY_TOPIC_AAF_PASSWORD_SUFFIX); - - String consumerGroup = properties.getProperty(PolicyEndPointProperties.PROPERTY_DMAAP_SOURCE_TOPICS - + "." + topic + PolicyEndPointProperties.PROPERTY_TOPIC_SOURCE_CONSUMER_GROUP_SUFFIX); - - String consumerInstance = properties.getProperty(PolicyEndPointProperties.PROPERTY_DMAAP_SOURCE_TOPICS - + "." + topic + PolicyEndPointProperties.PROPERTY_TOPIC_SOURCE_CONSUMER_INSTANCE_SUFFIX); - - String fetchTimeoutString = properties.getProperty(PolicyEndPointProperties.PROPERTY_DMAAP_SOURCE_TOPICS - + "." + topic + PolicyEndPointProperties.PROPERTY_TOPIC_SOURCE_FETCH_TIMEOUT_SUFFIX); - - /* DME2 Properties */ - - String dme2Environment = properties.getProperty(PolicyEndPointProperties.PROPERTY_DMAAP_SOURCE_TOPICS - + "." + topic + PolicyEndPointProperties.PROPERTY_DMAAP_DME2_ENVIRONMENT_SUFFIX); - - String dme2AftEnvironment = properties.getProperty(PolicyEndPointProperties.PROPERTY_DMAAP_SOURCE_TOPICS - + "." + topic + PolicyEndPointProperties.PROPERTY_DMAAP_DME2_AFT_ENVIRONMENT_SUFFIX); - - String dme2Partner = properties.getProperty(PolicyEndPointProperties.PROPERTY_DMAAP_SOURCE_TOPICS + "." - + topic + PolicyEndPointProperties.PROPERTY_DMAAP_DME2_PARTNER_SUFFIX); - - String dme2RouteOffer = properties.getProperty(PolicyEndPointProperties.PROPERTY_DMAAP_SOURCE_TOPICS - + "." + topic + PolicyEndPointProperties.PROPERTY_DMAAP_DME2_ROUTE_OFFER_SUFFIX); - - String dme2Latitude = properties.getProperty(PolicyEndPointProperties.PROPERTY_DMAAP_SOURCE_TOPICS + "." - + topic + PolicyEndPointProperties.PROPERTY_DMAAP_DME2_LATITUDE_SUFFIX); - - String dme2Longitude = properties.getProperty(PolicyEndPointProperties.PROPERTY_DMAAP_SOURCE_TOPICS - + "." + topic + PolicyEndPointProperties.PROPERTY_DMAAP_DME2_LONGITUDE_SUFFIX); - - String dme2EpReadTimeoutMs = - properties.getProperty(PolicyEndPointProperties.PROPERTY_DMAAP_SOURCE_TOPICS + "." + topic - + PolicyEndPointProperties.PROPERTY_DMAAP_DME2_EP_READ_TIMEOUT_MS_SUFFIX); - - String dme2EpConnTimeout = properties.getProperty(PolicyEndPointProperties.PROPERTY_DMAAP_SOURCE_TOPICS - + "." + topic + PolicyEndPointProperties.PROPERTY_DMAAP_DME2_EP_CONN_TIMEOUT_SUFFIX); - - String dme2RoundtripTimeoutMs = - properties.getProperty(PolicyEndPointProperties.PROPERTY_DMAAP_SOURCE_TOPICS + "." + topic - + PolicyEndPointProperties.PROPERTY_DMAAP_DME2_ROUNDTRIP_TIMEOUT_MS_SUFFIX); - - String dme2Version = properties.getProperty(PolicyEndPointProperties.PROPERTY_DMAAP_SOURCE_TOPICS + "." - + topic + PolicyEndPointProperties.PROPERTY_DMAAP_DME2_VERSION_SUFFIX); - - String dme2SubContextPath = properties.getProperty(PolicyEndPointProperties.PROPERTY_DMAAP_SOURCE_TOPICS - + "." + topic + PolicyEndPointProperties.PROPERTY_DMAAP_DME2_SUB_CONTEXT_PATH_SUFFIX); - - String dme2SessionStickinessRequired = - properties.getProperty(PolicyEndPointProperties.PROPERTY_DMAAP_SOURCE_TOPICS + "." + topic - + PolicyEndPointProperties.PROPERTY_DMAAP_DME2_SESSION_STICKINESS_REQUIRED_SUFFIX); - - Map<String, String> dme2AdditionalProps = new HashMap<>(); - - if (dme2EpReadTimeoutMs != null && !dme2EpReadTimeoutMs.isEmpty()) { - dme2AdditionalProps.put(DME2_READ_TIMEOUT_PROPERTY, dme2EpReadTimeoutMs); - } - if (dme2EpConnTimeout != null && !dme2EpConnTimeout.isEmpty()) { - dme2AdditionalProps.put(DME2_EP_CONN_TIMEOUT_PROPERTY, dme2EpConnTimeout); - } - if (dme2RoundtripTimeoutMs != null && !dme2RoundtripTimeoutMs.isEmpty()) { - dme2AdditionalProps.put(DME2_ROUNDTRIP_TIMEOUT_PROPERTY, dme2RoundtripTimeoutMs); - } - if (dme2Version != null && !dme2Version.isEmpty()) { - dme2AdditionalProps.put(DME2_VERSION_PROPERTY, dme2Version); - } - if (dme2RouteOffer != null && !dme2RouteOffer.isEmpty()) { - dme2AdditionalProps.put(DME2_ROUTE_OFFER_PROPERTY, dme2RouteOffer); - } - if (dme2SubContextPath != null && !dme2SubContextPath.isEmpty()) { - dme2AdditionalProps.put(DME2_SUBCONTEXT_PATH_PROPERTY, dme2SubContextPath); - } - if (dme2SessionStickinessRequired != null && !dme2SessionStickinessRequired.isEmpty()) { - dme2AdditionalProps.put(DME2_SESSION_STICKINESS_REQUIRED_PROPERTY, dme2SessionStickinessRequired); - } - - - if (servers == null || servers.isEmpty()) { - - logger.error("{}: no DMaaP servers or DME2 ServiceName provided", this); - continue; - } - - int fetchTimeout = DmaapTopicSource.DEFAULT_TIMEOUT_MS_FETCH; - if (fetchTimeoutString != null && !fetchTimeoutString.isEmpty()) { - try { - fetchTimeout = Integer.parseInt(fetchTimeoutString); - } catch (NumberFormatException nfe) { - logger.warn("{}: fetch timeout {} is in invalid format for topic {} ", this, fetchTimeoutString, - topic); - } - } - - String fetchLimitString = properties.getProperty(PolicyEndPointProperties.PROPERTY_DMAAP_SOURCE_TOPICS - + "." + topic + PolicyEndPointProperties.PROPERTY_TOPIC_SOURCE_FETCH_LIMIT_SUFFIX); - int fetchLimit = DmaapTopicSource.DEFAULT_LIMIT_FETCH; - if (fetchLimitString != null && !fetchLimitString.isEmpty()) { - try { - fetchLimit = Integer.parseInt(fetchLimitString); - } catch (NumberFormatException nfe) { - logger.warn("{}: fetch limit {} is in invalid format for topic {} ", this, fetchLimitString, - topic); - } - } - - String managedString = properties.getProperty(PolicyEndPointProperties.PROPERTY_DMAAP_SOURCE_TOPICS - + "." + topic + PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX); - boolean managed = true; - if (managedString != null && !managedString.isEmpty()) { - managed = Boolean.parseBoolean(managedString); - } - - String useHttpsString = properties.getProperty(PolicyEndPointProperties.PROPERTY_DMAAP_SOURCE_TOPICS - + "." + topic + PolicyEndPointProperties.PROPERTY_HTTP_HTTPS_SUFFIX); - - // default is to use HTTP if no https property exists - boolean useHttps = false; - if (useHttpsString != null && !useHttpsString.isEmpty()) { - useHttps = Boolean.parseBoolean(useHttpsString); - } - - String allowSelfSignedCertsString = - properties.getProperty(PolicyEndPointProperties.PROPERTY_DMAAP_SOURCE_TOPICS + "." + topic - + PolicyEndPointProperties.PROPERTY_ALLOW_SELF_SIGNED_CERTIFICATES_SUFFIX); - - // default is to disallow self-signed certs - boolean allowSelfSignedCerts = false; - if (allowSelfSignedCertsString != null && !allowSelfSignedCertsString.isEmpty()) { - allowSelfSignedCerts = Boolean.parseBoolean(allowSelfSignedCertsString); - } - - - DmaapTopicSource uebTopicSource = this.build(serverList, topic, apiKey, apiSecret, aafMechId, - aafPassword, consumerGroup, consumerInstance, fetchTimeout, fetchLimit, dme2Environment, - dme2AftEnvironment, dme2Partner, dme2Latitude, dme2Longitude, dme2AdditionalProps, managed, - useHttps, allowSelfSignedCerts); - - dmaapTopicSourceLst.add(uebTopicSource); - } - } - return dmaapTopicSourceLst; - } - - /** - * {@inheritDoc} - * - * @throws IllegalArgumentException - */ - @Override - public DmaapTopicSource build(List<String> servers, String topic, String apiKey, String apiSecret) { - return this.build(servers, topic, apiKey, apiSecret, null, null, null, null, - DmaapTopicSource.DEFAULT_TIMEOUT_MS_FETCH, DmaapTopicSource.DEFAULT_LIMIT_FETCH, true, false, false); - } - - /** - * {@inheritDoc} - * - * @throws IllegalArgumentException - */ - @Override - public DmaapTopicSource build(List<String> servers, String topic) { - return this.build(servers, topic, null, null); - } - - /** - * {@inheritDoc} - */ - @Override - public void destroy(String topic) { - - if (topic == null || topic.isEmpty()) { - throw new IllegalArgumentException(MISSING_TOPIC); - } - - DmaapTopicSource uebTopicSource; - - synchronized (this) { - if (!dmaapTopicSources.containsKey(topic)) { - return; - } - - uebTopicSource = dmaapTopicSources.remove(topic); - } - - uebTopicSource.shutdown(); - } - - /** - * {@inheritDoc} - */ - @Override - public DmaapTopicSource get(String topic) { - - if (topic == null || topic.isEmpty()) { - throw new IllegalArgumentException(MISSING_TOPIC); - } - - synchronized (this) { - if (dmaapTopicSources.containsKey(topic)) { - return dmaapTopicSources.get(topic); - } else { - throw new IllegalArgumentException("DmaapTopiceSource for " + topic + " not found"); - } - } - } - - @Override - public synchronized List<DmaapTopicSource> inventory() { - return new ArrayList<>(this.dmaapTopicSources.values()); - } - - @Override - public void destroy() { - List<DmaapTopicSource> readers = this.inventory(); - for (DmaapTopicSource reader : readers) { - reader.shutdown(); - } - - synchronized (this) { - this.dmaapTopicSources.clear(); - } - } - - @Override - public String toString() { - StringBuilder builder = new StringBuilder(); - builder.append("IndexedDmaapTopicSourceFactory []"); - return builder.toString(); - } - -} diff --git a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/event/comm/bus/impl/IndexedNoopTopicSinkFactory.java b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/event/comm/bus/impl/IndexedNoopTopicSinkFactory.java deleted file mode 100644 index 78bfd46c..00000000 --- a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/event/comm/bus/impl/IndexedNoopTopicSinkFactory.java +++ /dev/null @@ -1,185 +0,0 @@ -/* - * ============LICENSE_START======================================================= - * policy-endpoints - * ================================================================================ - * Copyright (C) 2017-2018 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. - * ============LICENSE_END========================================================= - */ - -package org.onap.policy.common.endpoints.event.comm.bus.impl; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.HashMap; -import java.util.List; -import java.util.Properties; - -import org.onap.policy.common.endpoints.event.comm.bus.NoopTopicSink; -import org.onap.policy.common.endpoints.event.comm.bus.NoopTopicSinkFactory; -import org.onap.policy.common.endpoints.properties.PolicyEndPointProperties; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * Factory of noop sinks - */ -public class IndexedNoopTopicSinkFactory implements NoopTopicSinkFactory { - private static final String MISSING_TOPIC = "A topic must be provided"; - - private static final IndexedNoopTopicSinkFactory instance = new IndexedNoopTopicSinkFactory(); - - /** - * Logger - */ - private static Logger logger = LoggerFactory.getLogger(IndexedUebTopicSinkFactory.class); - - /** - * noop topic sinks map - */ - protected HashMap<String, NoopTopicSink> noopTopicSinks = new HashMap<>(); - - /** - * Get the singleton instance. - * - * @return the instance - */ - public static IndexedNoopTopicSinkFactory getInstance() { - return instance; - } - - private IndexedNoopTopicSinkFactory() {} - - @Override - public List<NoopTopicSink> build(Properties properties) { - - final String sinkTopics = properties.getProperty(PolicyEndPointProperties.PROPERTY_NOOP_SINK_TOPICS); - if (sinkTopics == null || sinkTopics.isEmpty()) { - logger.info("{}: no topic for noop sink", this); - return new ArrayList<>(); - } - - final List<String> sinkTopicList = new ArrayList<>(Arrays.asList(sinkTopics.split("\\s*,\\s*"))); - final List<NoopTopicSink> newSinks = new ArrayList<>(); - synchronized (this) { - for (final String topic : sinkTopicList) { - if (this.noopTopicSinks.containsKey(topic)) { - newSinks.add(this.noopTopicSinks.get(topic)); - continue; - } - - String servers = properties.getProperty(PolicyEndPointProperties.PROPERTY_NOOP_SINK_TOPICS + "." + topic - + PolicyEndPointProperties.PROPERTY_TOPIC_SERVERS_SUFFIX); - - if (servers == null || servers.isEmpty()) { - servers = "noop"; - } - - final List<String> serverList = new ArrayList<>(Arrays.asList(servers.split("\\s*,\\s*"))); - - final String managedString = properties.getProperty(PolicyEndPointProperties.PROPERTY_NOOP_SINK_TOPICS + "." - + topic + PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX); - boolean managed = true; - if (managedString != null && !managedString.isEmpty()) { - managed = Boolean.parseBoolean(managedString); - } - - final NoopTopicSink noopSink = this.build(serverList, topic, managed); - newSinks.add(noopSink); - } - return newSinks; - } - } - - @Override - public NoopTopicSink build(List<String> servers, String topic, boolean managed) { - - List<String> noopSinkServers = servers; - if (noopSinkServers == null) { - noopSinkServers = new ArrayList<>(); - } - - if (noopSinkServers.isEmpty()) { - noopSinkServers.add("noop"); - } - - if (topic == null || topic.isEmpty()) { - throw new IllegalArgumentException(MISSING_TOPIC); - } - - synchronized (this) { - if (this.noopTopicSinks.containsKey(topic)) { - return this.noopTopicSinks.get(topic); - } - - final NoopTopicSink sink = new NoopTopicSink(noopSinkServers, topic); - - if (managed) { - this.noopTopicSinks.put(topic, sink); - } - - return sink; - } - } - - @Override - public void destroy(String topic) { - if (topic == null || topic.isEmpty()) { - throw new IllegalArgumentException(MISSING_TOPIC); - } - - NoopTopicSink noopSink; - synchronized (this) { - if (!this.noopTopicSinks.containsKey(topic)) { - return; - } - - noopSink = this.noopTopicSinks.remove(topic); - } - - noopSink.shutdown(); - } - - @Override - public void destroy() { - final List<NoopTopicSink> sinks = this.inventory(); - for (final NoopTopicSink sink : sinks) { - sink.shutdown(); - } - - synchronized (this) { - this.noopTopicSinks.clear(); - } - } - - @Override - public NoopTopicSink get(String topic) { - if (topic == null || topic.isEmpty()) { - throw new IllegalArgumentException(MISSING_TOPIC); - } - - synchronized (this) { - if (this.noopTopicSinks.containsKey(topic)) { - return this.noopTopicSinks.get(topic); - } else { - throw new IllegalStateException("DmaapTopicSink for " + topic + " not found"); - } - } - } - - @Override - public List<NoopTopicSink> inventory() { - return new ArrayList<>(this.noopTopicSinks.values()); - } -} diff --git a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/event/comm/bus/impl/IndexedUebTopicSinkFactory.java b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/event/comm/bus/impl/IndexedUebTopicSinkFactory.java deleted file mode 100644 index 19bdc7b2..00000000 --- a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/event/comm/bus/impl/IndexedUebTopicSinkFactory.java +++ /dev/null @@ -1,229 +0,0 @@ -/* - * ============LICENSE_START======================================================= - * policy-endpoints - * ================================================================================ - * Copyright (C) 2017-2018 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. - * ============LICENSE_END========================================================= - */ - -package org.onap.policy.common.endpoints.event.comm.bus.impl; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.HashMap; -import java.util.List; -import java.util.Properties; - -import org.onap.policy.common.endpoints.event.comm.bus.UebTopicSink; -import org.onap.policy.common.endpoints.event.comm.bus.UebTopicSinkFactory; -import org.onap.policy.common.endpoints.event.comm.bus.internal.InlineUebTopicSink; -import org.onap.policy.common.endpoints.properties.PolicyEndPointProperties; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * Factory of UEB Reader Topics indexed by topic name - */ -public class IndexedUebTopicSinkFactory implements UebTopicSinkFactory { - - private static final IndexedUebTopicSinkFactory instance = new IndexedUebTopicSinkFactory(); - - private static final String MISSING_TOPIC = "A topic must be provided"; - - /** - * Logger - */ - private static Logger logger = LoggerFactory.getLogger(IndexedUebTopicSinkFactory.class); - - /** - * UEB Topic Name Index - */ - protected HashMap<String, UebTopicSink> uebTopicSinks = new HashMap<>(); - - /** - * Get the singleton instance. - * - * @return the instance - */ - public static IndexedUebTopicSinkFactory getInstance() { - return instance; - } - - private IndexedUebTopicSinkFactory() {} - - @Override - public UebTopicSink build(List<String> servers, String topic, String apiKey, String apiSecret, String partitionKey, - boolean managed, boolean useHttps, boolean allowSelfSignedCerts) { - - if (servers == null || servers.isEmpty()) { - throw new IllegalArgumentException("UEB Server(s) must be provided"); - } - - if (topic == null || topic.isEmpty()) { - throw new IllegalArgumentException(MISSING_TOPIC); - } - - synchronized (this) { - if (uebTopicSinks.containsKey(topic)) { - return uebTopicSinks.get(topic); - } - - UebTopicSink uebTopicWriter = new InlineUebTopicSink(servers, topic, apiKey, apiSecret, partitionKey, - useHttps, allowSelfSignedCerts); - - if (managed) { - uebTopicSinks.put(topic, uebTopicWriter); - } - - return uebTopicWriter; - } - } - - - @Override - public UebTopicSink build(List<String> servers, String topic) { - return this.build(servers, topic, null, null, null, true, false, false); - } - - - @Override - public List<UebTopicSink> build(Properties properties) { - - String writeTopics = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SINK_TOPICS); - if (writeTopics == null || writeTopics.isEmpty()) { - logger.info("{}: no topic for UEB Sink", this); - return new ArrayList<>(); - } - - List<String> writeTopicList = new ArrayList<>(Arrays.asList(writeTopics.split("\\s*,\\s*"))); - List<UebTopicSink> newUebTopicSinks = new ArrayList<>(); - synchronized (this) { - for (String topic : writeTopicList) { - if (this.uebTopicSinks.containsKey(topic)) { - newUebTopicSinks.add(this.uebTopicSinks.get(topic)); - continue; - } - - String servers = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SINK_TOPICS + "." + topic - + PolicyEndPointProperties.PROPERTY_TOPIC_SERVERS_SUFFIX); - if (servers == null || servers.isEmpty()) { - logger.error("{}: no UEB servers configured for sink {}", this, topic); - continue; - } - - List<String> serverList = new ArrayList<>(Arrays.asList(servers.split("\\s*,\\s*"))); - - String apiKey = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SINK_TOPICS + "." + topic - + PolicyEndPointProperties.PROPERTY_TOPIC_API_KEY_SUFFIX); - String apiSecret = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SINK_TOPICS + "." + topic - + PolicyEndPointProperties.PROPERTY_TOPIC_API_SECRET_SUFFIX); - String partitionKey = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SINK_TOPICS + "." + topic - + PolicyEndPointProperties.PROPERTY_TOPIC_SINK_PARTITION_KEY_SUFFIX); - - String managedString = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SINK_TOPICS + "." + topic - + PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX); - boolean managed = true; - if (managedString != null && !managedString.isEmpty()) { - managed = Boolean.parseBoolean(managedString); - } - - String useHttpsString = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SINK_TOPICS + "." + topic - + PolicyEndPointProperties.PROPERTY_HTTP_HTTPS_SUFFIX); - - // default is to use HTTP if no https property exists - boolean useHttps = false; - if (useHttpsString != null && !useHttpsString.isEmpty()) { - useHttps = Boolean.parseBoolean(useHttpsString); - } - - - String allowSelfSignedCertsString = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SINK_TOPICS - + "." + topic + PolicyEndPointProperties.PROPERTY_ALLOW_SELF_SIGNED_CERTIFICATES_SUFFIX); - - // default is to disallow self-signed certs - boolean allowSelfSignedCerts = false; - if (allowSelfSignedCertsString != null && !allowSelfSignedCertsString.isEmpty()) { - allowSelfSignedCerts = Boolean.parseBoolean(allowSelfSignedCertsString); - } - - UebTopicSink uebTopicWriter = this.build(serverList, topic, apiKey, apiSecret, partitionKey, managed, - useHttps, allowSelfSignedCerts); - newUebTopicSinks.add(uebTopicWriter); - } - return newUebTopicSinks; - } - } - - @Override - public void destroy(String topic) { - - if (topic == null || topic.isEmpty()) { - throw new IllegalArgumentException(MISSING_TOPIC); - } - - UebTopicSink uebTopicWriter; - synchronized (this) { - if (!uebTopicSinks.containsKey(topic)) { - return; - } - - uebTopicWriter = uebTopicSinks.remove(topic); - } - - uebTopicWriter.shutdown(); - } - - @Override - public void destroy() { - List<UebTopicSink> writers = this.inventory(); - for (UebTopicSink writer : writers) { - writer.shutdown(); - } - - synchronized (this) { - this.uebTopicSinks.clear(); - } - } - - @Override - public UebTopicSink get(String topic) { - - if (topic == null || topic.isEmpty()) { - throw new IllegalArgumentException(MISSING_TOPIC); - } - - synchronized (this) { - if (uebTopicSinks.containsKey(topic)) { - return uebTopicSinks.get(topic); - } else { - throw new IllegalStateException("UebTopicSink for " + topic + " not found"); - } - } - } - - @Override - public synchronized List<UebTopicSink> inventory() { - return new ArrayList<>(this.uebTopicSinks.values()); - } - - - @Override - public String toString() { - StringBuilder builder = new StringBuilder(); - builder.append("IndexedUebTopicSinkFactory []"); - return builder.toString(); - } - -} diff --git a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/event/comm/bus/impl/IndexedUebTopicSourceFactory.java b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/event/comm/bus/impl/IndexedUebTopicSourceFactory.java deleted file mode 100644 index 5363a30d..00000000 --- a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/event/comm/bus/impl/IndexedUebTopicSourceFactory.java +++ /dev/null @@ -1,280 +0,0 @@ -/* - * ============LICENSE_START======================================================= - * policy-endpoints - * ================================================================================ - * Copyright (C) 2017-2018 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. - * ============LICENSE_END========================================================= - */ - -package org.onap.policy.common.endpoints.event.comm.bus.impl; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.HashMap; -import java.util.List; -import java.util.Properties; - -import org.onap.policy.common.endpoints.event.comm.bus.UebTopicSource; -import org.onap.policy.common.endpoints.event.comm.bus.UebTopicSourceFactory; -import org.onap.policy.common.endpoints.event.comm.bus.internal.SingleThreadedUebTopicSource; -import org.onap.policy.common.endpoints.properties.PolicyEndPointProperties; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * Factory of UEB Source Topics indexed by topic name - */ -public class IndexedUebTopicSourceFactory implements UebTopicSourceFactory { - private static final String MISSING_TOPIC = "A topic must be provided"; - - private static final IndexedUebTopicSourceFactory instance = new IndexedUebTopicSourceFactory(); - - /** - * Logger - */ - private static Logger logger = LoggerFactory.getLogger(IndexedUebTopicSourceFactory.class); - - /** - * UEB Topic Name Index - */ - protected HashMap<String, UebTopicSource> uebTopicSources = new HashMap<>(); - - /** - * Get the singleton instance. - * - * @return the instance - */ - public static IndexedUebTopicSourceFactory getInstance() { - return instance; - } - - private IndexedUebTopicSourceFactory() {} - - /** - * {@inheritDoc} - */ - @Override - public UebTopicSource build(List<String> servers, String topic, String apiKey, String apiSecret, - String consumerGroup, String consumerInstance, int fetchTimeout, int fetchLimit, boolean managed, - boolean useHttps, boolean allowSelfSignedCerts) { - if (servers == null || servers.isEmpty()) { - throw new IllegalArgumentException("UEB Server(s) must be provided"); - } - - if (topic == null || topic.isEmpty()) { - throw new IllegalArgumentException(MISSING_TOPIC); - } - - synchronized (this) { - if (uebTopicSources.containsKey(topic)) { - return uebTopicSources.get(topic); - } - - UebTopicSource uebTopicSource = new SingleThreadedUebTopicSource(servers, topic, apiKey, apiSecret, - consumerGroup, consumerInstance, fetchTimeout, fetchLimit, useHttps, allowSelfSignedCerts); - - if (managed) { - uebTopicSources.put(topic, uebTopicSource); - } - - return uebTopicSource; - } - } - - /** - * {@inheritDoc} - */ - @Override - public List<UebTopicSource> build(Properties properties) { - - String readTopics = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SOURCE_TOPICS); - if (readTopics == null || readTopics.isEmpty()) { - logger.info("{}: no topic for UEB Source", this); - return new ArrayList<>(); - } - List<String> readTopicList = new ArrayList<>(Arrays.asList(readTopics.split("\\s*,\\s*"))); - - List<UebTopicSource> newUebTopicSources = new ArrayList<>(); - synchronized (this) { - for (String topic : readTopicList) { - if (this.uebTopicSources.containsKey(topic)) { - newUebTopicSources.add(this.uebTopicSources.get(topic)); - continue; - } - - String servers = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SOURCE_TOPICS + "." + topic - + PolicyEndPointProperties.PROPERTY_TOPIC_SERVERS_SUFFIX); - - if (servers == null || servers.isEmpty()) { - logger.error("{}: no UEB servers configured for sink {}", this, topic); - continue; - } - - List<String> serverList = new ArrayList<>(Arrays.asList(servers.split("\\s*,\\s*"))); - - String apiKey = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SOURCE_TOPICS + "." + topic - + PolicyEndPointProperties.PROPERTY_TOPIC_API_KEY_SUFFIX); - - String apiSecret = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SOURCE_TOPICS + "." + topic - + PolicyEndPointProperties.PROPERTY_TOPIC_API_SECRET_SUFFIX); - - String consumerGroup = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SOURCE_TOPICS + "." + topic - + PolicyEndPointProperties.PROPERTY_TOPIC_SOURCE_CONSUMER_GROUP_SUFFIX); - - String consumerInstance = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SOURCE_TOPICS + "." - + topic + PolicyEndPointProperties.PROPERTY_TOPIC_SOURCE_CONSUMER_INSTANCE_SUFFIX); - - String fetchTimeoutString = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SOURCE_TOPICS + "." - + topic + PolicyEndPointProperties.PROPERTY_TOPIC_SOURCE_FETCH_TIMEOUT_SUFFIX); - int fetchTimeout = UebTopicSource.DEFAULT_TIMEOUT_MS_FETCH; - if (fetchTimeoutString != null && !fetchTimeoutString.isEmpty()) { - try { - fetchTimeout = Integer.parseInt(fetchTimeoutString); - } catch (NumberFormatException nfe) { - logger.warn("{}: fetch timeout {} is in invalid format for topic {} ", this, fetchTimeoutString, - topic); - } - } - - String fetchLimitString = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SOURCE_TOPICS + "." - + topic + PolicyEndPointProperties.PROPERTY_TOPIC_SOURCE_FETCH_LIMIT_SUFFIX); - int fetchLimit = UebTopicSource.DEFAULT_LIMIT_FETCH; - if (fetchLimitString != null && !fetchLimitString.isEmpty()) { - try { - fetchLimit = Integer.parseInt(fetchLimitString); - } catch (NumberFormatException nfe) { - logger.warn("{}: fetch limit {} is in invalid format for topic {} ", this, fetchLimitString, - topic); - } - } - - String managedString = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SOURCE_TOPICS + "." + topic - + PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX); - boolean managed = true; - if (managedString != null && !managedString.isEmpty()) { - managed = Boolean.parseBoolean(managedString); - } - - String useHttpsString = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SOURCE_TOPICS + "." + topic - + PolicyEndPointProperties.PROPERTY_HTTP_HTTPS_SUFFIX); - - // default is to use HTTP if no https property exists - boolean useHttps = false; - if (useHttpsString != null && !useHttpsString.isEmpty()) { - useHttps = Boolean.parseBoolean(useHttpsString); - } - - String allowSelfSignedCertsString = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SOURCE_TOPICS - + "." + topic + PolicyEndPointProperties.PROPERTY_ALLOW_SELF_SIGNED_CERTIFICATES_SUFFIX); - - // default is to disallow self-signed certs - boolean allowSelfSignedCerts = false; - if (allowSelfSignedCertsString != null && !allowSelfSignedCertsString.isEmpty()) { - allowSelfSignedCerts = Boolean.parseBoolean(allowSelfSignedCertsString); - } - - UebTopicSource uebTopicSource = this.build(serverList, topic, apiKey, apiSecret, consumerGroup, - consumerInstance, fetchTimeout, fetchLimit, managed, useHttps, allowSelfSignedCerts); - newUebTopicSources.add(uebTopicSource); - } - } - return newUebTopicSources; - } - - /** - * {@inheritDoc} - */ - @Override - public UebTopicSource build(List<String> servers, String topic, String apiKey, String apiSecret) { - - return this.build(servers, topic, apiKey, apiSecret, null, null, UebTopicSource.DEFAULT_TIMEOUT_MS_FETCH, - UebTopicSource.DEFAULT_LIMIT_FETCH, true, false, true); - } - - /** - * {@inheritDoc} - */ - @Override - public UebTopicSource build(List<String> servers, String topic) { - return this.build(servers, topic, null, null); - } - - /** - * {@inheritDoc} - */ - @Override - public void destroy(String topic) { - - if (topic == null || topic.isEmpty()) { - throw new IllegalArgumentException(MISSING_TOPIC); - } - - UebTopicSource uebTopicSource; - - synchronized (this) { - if (!uebTopicSources.containsKey(topic)) { - return; - } - - uebTopicSource = uebTopicSources.remove(topic); - } - - uebTopicSource.shutdown(); - } - - /** - * {@inheritDoc} - */ - @Override - public UebTopicSource get(String topic) { - - if (topic == null || topic.isEmpty()) { - throw new IllegalArgumentException(MISSING_TOPIC); - } - - synchronized (this) { - if (uebTopicSources.containsKey(topic)) { - return uebTopicSources.get(topic); - } else { - throw new IllegalStateException("UebTopiceSource for " + topic + " not found"); - } - } - } - - @Override - public synchronized List<UebTopicSource> inventory() { - return new ArrayList<>(this.uebTopicSources.values()); - } - - @Override - public void destroy() { - List<UebTopicSource> readers = this.inventory(); - for (UebTopicSource reader : readers) { - reader.shutdown(); - } - - synchronized (this) { - this.uebTopicSources.clear(); - } - } - - @Override - public String toString() { - StringBuilder builder = new StringBuilder(); - builder.append("IndexedUebTopicSourceFactory []"); - return builder.toString(); - } - -} diff --git a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/event/comm/bus/internal/BusConsumer.java b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/event/comm/bus/internal/BusConsumer.java index 8cc631c8..636dc6e3 100644 --- a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/event/comm/bus/internal/BusConsumer.java +++ b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/event/comm/bus/internal/BusConsumer.java @@ -20,7 +20,26 @@ package org.onap.policy.common.endpoints.event.comm.bus.internal; +import com.att.nsa.cambria.client.CambriaClientBuilders; +import com.att.nsa.cambria.client.CambriaClientBuilders.ConsumerBuilder; +import com.att.nsa.cambria.client.CambriaConsumer; +import com.att.nsa.mr.client.MRClientFactory; +import com.att.nsa.mr.client.impl.MRConsumerImpl; +import com.att.nsa.mr.client.response.MRConsumerResponse; +import com.att.nsa.mr.test.clients.ProtocolTypeConstants; + import java.io.IOException; +import java.net.MalformedURLException; +import java.security.GeneralSecurityException; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.Properties; + +import org.onap.policy.common.endpoints.event.comm.bus.DmaapTopicSinkFactory; +import org.onap.policy.common.endpoints.properties.PolicyEndPointProperties; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * Wrapper around libraries to consume from message bus @@ -40,6 +59,485 @@ public interface BusConsumer { * close underlying library consumer */ public void close(); + + /** + * BusConsumer that supports server-side filtering. + */ + public interface FilterableBusConsumer extends BusConsumer { + + /** + * Sets the server-side filter. + * + * @param filter new filter value, or {@code null} + * @throws IllegalArgumentException if the consumer cannot be built with the new filter + */ + public void setFilter(String filter); + } + + /** + * Cambria based consumer + */ + public static class CambriaConsumerWrapper implements FilterableBusConsumer { + + /** + * logger + */ + private static Logger logger = LoggerFactory.getLogger(CambriaConsumerWrapper.class); + + /** + * Used to build the consumer. + */ + private final ConsumerBuilder builder; + + /** + * Locked while updating {@link #consumer} and {@link #newConsumer}. + */ + private final Object consLocker = new Object(); + + /** + * Cambria client + */ + private CambriaConsumer consumer; + + /** + * Cambria client to use for next fetch + */ + private CambriaConsumer newConsumer = null; + + /** + * fetch timeout + */ + protected int fetchTimeout; + + /** + * close condition + */ + protected Object closeCondition = new Object(); + + /** + * Cambria Consumer Wrapper + * + * @param servers messaging bus hosts + * @param topic topic + * @param apiKey API Key + * @param apiSecret API Secret + * @param consumerGroup Consumer Group + * @param consumerInstance Consumer Instance + * @param fetchTimeout Fetch Timeout + * @param fetchLimit Fetch Limit + * @throws GeneralSecurityException + * @throws MalformedURLException + */ + public CambriaConsumerWrapper(List<String> servers, String topic, String apiKey, String apiSecret, + String consumerGroup, String consumerInstance, int fetchTimeout, int fetchLimit, boolean useHttps, + boolean useSelfSignedCerts) { + this(servers, topic, apiKey, apiSecret, null, null, consumerGroup, consumerInstance, fetchTimeout, + fetchLimit, useHttps, useSelfSignedCerts); + } + + public CambriaConsumerWrapper(List<String> servers, String topic, String apiKey, String apiSecret, + String username, String password, String consumerGroup, String consumerInstance, int fetchTimeout, + int fetchLimit, boolean useHttps, boolean useSelfSignedCerts) { + + this.fetchTimeout = fetchTimeout; + + this.builder = new CambriaClientBuilders.ConsumerBuilder(); + + builder.knownAs(consumerGroup, consumerInstance).usingHosts(servers).onTopic(topic) + .waitAtServer(fetchTimeout).receivingAtMost(fetchLimit); + + // Set read timeout to fetch timeout + 30 seconds (TBD: this should be configurable) + builder.withSocketTimeout(fetchTimeout + 30000); + + if (useHttps) { + builder.usingHttps(); + + if (useSelfSignedCerts) { + builder.allowSelfSignedCertificates(); + } + } + + if (apiKey != null && !apiKey.isEmpty() && apiSecret != null && !apiSecret.isEmpty()) { + builder.authenticatedBy(apiKey, apiSecret); + } + + if (username != null && !username.isEmpty() && password != null && !password.isEmpty()) { + builder.authenticatedByHttp(username, password); + } + + try { + this.consumer = builder.build(); + } catch (MalformedURLException | GeneralSecurityException e) { + throw new IllegalArgumentException(e); + } + } + + @Override + public Iterable<String> fetch() throws IOException, InterruptedException { + try { + return getCurrentConsumer().fetch(); + } catch (final IOException e) { + logger.error("{}: cannot fetch because of {} - backoff for {} ms.", this, e.getMessage(), + this.fetchTimeout); + synchronized (this.closeCondition) { + this.closeCondition.wait(this.fetchTimeout); + } + + throw e; + } + } + + @Override + public void close() { + synchronized (closeCondition) { + closeCondition.notifyAll(); + } + + getCurrentConsumer().close(); + } + + private CambriaConsumer getCurrentConsumer() { + CambriaConsumer old = null; + CambriaConsumer ret; + + synchronized (consLocker) { + if (this.newConsumer != null) { + // replace old consumer with new consumer + old = this.consumer; + this.consumer = this.newConsumer; + this.newConsumer = null; + } + + ret = this.consumer; + } + + if (old != null) { + old.close(); + } + + return ret; + } + + @Override + public void setFilter(String filter) { + logger.info("{}: setting DMAAP server-side filter: {}", this, filter); + builder.withServerSideFilter(filter); + + try { + CambriaConsumer previous; + synchronized (consLocker) { + previous = this.newConsumer; + this.newConsumer = builder.build(); + } + + if (previous != null) { + // there was already a new consumer - close it + previous.close(); + } + + } catch (MalformedURLException | GeneralSecurityException e) { + /* + * Since an exception occurred, "consumer" still has its old value, thus it should + * not be closed at this point. + */ + throw new IllegalArgumentException(e); + } + } + + @Override + public String toString() { + return "CambriaConsumerWrapper [fetchTimeout=" + fetchTimeout + "]"; + } + } + + /** + * MR based consumer + */ + public abstract class DmaapConsumerWrapper implements BusConsumer { + + /** + * logger + */ + private static Logger logger = LoggerFactory.getLogger(DmaapConsumerWrapper.class); + + /** + * Name of the "protocol" property. + */ + protected static final String PROTOCOL_PROP = "Protocol"; + + /** + * fetch timeout + */ + protected int fetchTimeout; + + /** + * close condition + */ + protected Object closeCondition = new Object(); + + /** + * MR Consumer + */ + protected MRConsumerImpl consumer; + + /** + * MR Consumer Wrapper + * + * @param servers messaging bus hosts + * @param topic topic + * @param apiKey API Key + * @param apiSecret API Secret + * @param username AAF Login + * @param password AAF Password + * @param consumerGroup Consumer Group + * @param consumerInstance Consumer Instance + * @param fetchTimeout Fetch Timeout + * @param fetchLimit Fetch Limit + * @throws MalformedURLException + */ + public DmaapConsumerWrapper(List<String> servers, String topic, String apiKey, String apiSecret, + String username, String password, String consumerGroup, String consumerInstance, int fetchTimeout, + int fetchLimit) throws MalformedURLException { + + this.fetchTimeout = fetchTimeout; + + if (topic == null || topic.isEmpty()) { + throw new IllegalArgumentException("No topic for DMaaP"); + } + + this.consumer = new MRConsumerImpl(servers, topic, consumerGroup, consumerInstance, fetchTimeout, + fetchLimit, null, apiKey, apiSecret); + + this.consumer.setUsername(username); + this.consumer.setPassword(password); + } + + @Override + public Iterable<String> fetch() throws InterruptedException, IOException { + final MRConsumerResponse response = this.consumer.fetchWithReturnConsumerResponse(); + if (response == null) { + logger.warn("{}: DMaaP NULL response received", this); + + synchronized (closeCondition) { + closeCondition.wait(fetchTimeout); + } + return new ArrayList<>(); + } else { + logger.debug("DMaaP consumer received {} : {}" + response.getResponseCode(), + response.getResponseMessage()); + + if (response.getResponseCode() == null || !"200".equals(response.getResponseCode())) { + + logger.error("DMaaP consumer received: {} : {}", response.getResponseCode(), + response.getResponseMessage()); + + synchronized (closeCondition) { + closeCondition.wait(fetchTimeout); + } + + /* fall through */ + } + } + + if (response.getActualMessages() == null) { + return new ArrayList<>(); + } else { + return response.getActualMessages(); + } + } + + @Override + public void close() { + synchronized (closeCondition) { + closeCondition.notifyAll(); + } + + this.consumer.close(); + } + + @Override + public String toString() { + return "DmaapConsumerWrapper [" + "consumer.getAuthDate()=" + consumer.getAuthDate() + + ", consumer.getAuthKey()=" + consumer.getAuthKey() + ", consumer.getHost()=" + consumer.getHost() + + ", consumer.getProtocolFlag()=" + consumer.getProtocolFlag() + ", consumer.getUsername()=" + + consumer.getUsername() + "]"; + } + } + + /** + * MR based consumer + */ + public static class DmaapAafConsumerWrapper extends DmaapConsumerWrapper { + + private static Logger logger = LoggerFactory.getLogger(DmaapAafConsumerWrapper.class); + + private final Properties props; + + /** + * MR Consumer Wrapper + * + * @param servers messaging bus hosts + * @param topic topic + * @param apiKey API Key + * @param apiSecret API Secret + * @param aafLogin AAF Login + * @param aafPassword AAF Password + * @param consumerGroup Consumer Group + * @param consumerInstance Consumer Instance + * @param fetchTimeout Fetch Timeout + * @param fetchLimit Fetch Limit + * @throws MalformedURLException + */ + public DmaapAafConsumerWrapper(List<String> servers, String topic, String apiKey, String apiSecret, + String aafLogin, String aafPassword, String consumerGroup, String consumerInstance, int fetchTimeout, + int fetchLimit, boolean useHttps) throws MalformedURLException { + + super(servers, topic, apiKey, apiSecret, aafLogin, aafPassword, consumerGroup, consumerInstance, + fetchTimeout, fetchLimit); + + // super constructor sets servers = {""} if empty to avoid errors when using DME2 + if ((servers.size() == 1 && ("".equals(servers.get(0)))) || (servers == null) || (servers.isEmpty())) { + throw new IllegalArgumentException("Must provide at least one host for HTTP AAF"); + } + + this.consumer.setProtocolFlag(ProtocolTypeConstants.AAF_AUTH.getValue()); + + props = new Properties(); + + if (useHttps) { + props.setProperty(PROTOCOL_PROP, "https"); + this.consumer.setHost(servers.get(0) + ":3905"); + + } else { + props.setProperty(PROTOCOL_PROP, "http"); + this.consumer.setHost(servers.get(0) + ":3904"); + } + + this.consumer.setProps(props); + logger.info("{}: CREATION", this); + } + + @Override + public String toString() { + final MRConsumerImpl consumer = this.consumer; + + return "DmaapConsumerWrapper [" + "consumer.getAuthDate()=" + consumer.getAuthDate() + + ", consumer.getAuthKey()=" + consumer.getAuthKey() + ", consumer.getHost()=" + consumer.getHost() + + ", consumer.getProtocolFlag()=" + consumer.getProtocolFlag() + ", consumer.getUsername()=" + + consumer.getUsername() + "]"; + } + } + + public static class DmaapDmeConsumerWrapper extends DmaapConsumerWrapper { + + private static Logger logger = LoggerFactory.getLogger(DmaapDmeConsumerWrapper.class); + + private final Properties props; + + public DmaapDmeConsumerWrapper(List<String> servers, String topic, String apiKey, String apiSecret, + String dme2Login, String dme2Password, String consumerGroup, String consumerInstance, int fetchTimeout, + int fetchLimit, String environment, String aftEnvironment, String dme2Partner, String latitude, + String longitude, Map<String, String> additionalProps, boolean useHttps) throws MalformedURLException { + + + + super(servers, topic, apiKey, apiSecret, dme2Login, dme2Password, consumerGroup, consumerInstance, + fetchTimeout, fetchLimit); + + + final String dme2RouteOffer = additionalProps.get(DmaapTopicSinkFactory.DME2_ROUTE_OFFER_PROPERTY); + + if (environment == null || environment.isEmpty()) { + throw parmException(topic, PolicyEndPointProperties.PROPERTY_DMAAP_DME2_ENVIRONMENT_SUFFIX); + } + if (aftEnvironment == null || aftEnvironment.isEmpty()) { + throw parmException(topic, PolicyEndPointProperties.PROPERTY_DMAAP_DME2_AFT_ENVIRONMENT_SUFFIX); + } + if (latitude == null || latitude.isEmpty()) { + throw parmException(topic, PolicyEndPointProperties.PROPERTY_DMAAP_DME2_LATITUDE_SUFFIX); + } + if (longitude == null || longitude.isEmpty()) { + throw parmException(topic, PolicyEndPointProperties.PROPERTY_DMAAP_DME2_LONGITUDE_SUFFIX); + } + + if ((dme2Partner == null || dme2Partner.isEmpty()) + && (dme2RouteOffer == null || dme2RouteOffer.isEmpty())) { + throw new IllegalArgumentException( + "Must provide at least " + PolicyEndPointProperties.PROPERTY_DMAAP_SOURCE_TOPICS + "." + topic + + PolicyEndPointProperties.PROPERTY_DMAAP_DME2_PARTNER_SUFFIX + " or " + + PolicyEndPointProperties.PROPERTY_DMAAP_SOURCE_TOPICS + "." + topic + + PolicyEndPointProperties.PROPERTY_DMAAP_DME2_ROUTE_OFFER_SUFFIX + " for DME2"); + } + + final String serviceName = servers.get(0); + + this.consumer.setProtocolFlag(ProtocolTypeConstants.DME2.getValue()); + + this.consumer.setUsername(dme2Login); + this.consumer.setPassword(dme2Password); + + props = new Properties(); + + props.setProperty(DmaapTopicSinkFactory.DME2_SERVICE_NAME_PROPERTY, serviceName); + + props.setProperty("username", dme2Login); + props.setProperty("password", dme2Password); + + /* These are required, no defaults */ + props.setProperty("topic", topic); + + props.setProperty("Environment", environment); + props.setProperty("AFT_ENVIRONMENT", aftEnvironment); + + if (dme2Partner != null) { + props.setProperty("Partner", dme2Partner); + } + if (dme2RouteOffer != null) { + props.setProperty(DmaapTopicSinkFactory.DME2_ROUTE_OFFER_PROPERTY, dme2RouteOffer); + } + + props.setProperty("Latitude", latitude); + props.setProperty("Longitude", longitude); + + /* These are optional, will default to these values if not set in additionalProps */ + props.setProperty("AFT_DME2_EP_READ_TIMEOUT_MS", "50000"); + props.setProperty("AFT_DME2_ROUNDTRIP_TIMEOUT_MS", "240000"); + props.setProperty("AFT_DME2_EP_CONN_TIMEOUT", "15000"); + props.setProperty("Version", "1.0"); + props.setProperty("SubContextPath", "/"); + props.setProperty("sessionstickinessrequired", "no"); + + /* These should not change */ + props.setProperty("TransportType", "DME2"); + props.setProperty("MethodType", "GET"); + + if (useHttps) { + props.setProperty(PROTOCOL_PROP, "https"); + + } else { + props.setProperty(PROTOCOL_PROP, "http"); + } + + props.setProperty("contenttype", "application/json"); + + if (additionalProps != null) { + for (Map.Entry<String, String> entry : additionalProps.entrySet()) { + props.put(entry.getKey(), entry.getValue()); + } + } + + MRClientFactory.prop = props; + this.consumer.setProps(props); + + logger.info("{}: CREATION", this); + } + + private IllegalArgumentException parmException(String topic, String propnm) { + return new IllegalArgumentException("Missing " + PolicyEndPointProperties.PROPERTY_DMAAP_SOURCE_TOPICS + "." + + topic + propnm + " property for DME2 in DMaaP"); + + } + } } diff --git a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/event/comm/bus/internal/BusPublisher.java b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/event/comm/bus/internal/BusPublisher.java index f6c65ada..9db9131c 100644 --- a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/event/comm/bus/internal/BusPublisher.java +++ b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/event/comm/bus/internal/BusPublisher.java @@ -20,6 +20,28 @@ package org.onap.policy.common.endpoints.event.comm.bus.internal; +import com.att.nsa.apiClient.http.HttpClient.ConnectionType; +import com.att.nsa.cambria.client.CambriaBatchingPublisher; +import com.att.nsa.cambria.client.CambriaClientBuilders; +import com.att.nsa.cambria.client.CambriaClientBuilders.PublisherBuilder; +import com.att.nsa.mr.client.impl.MRSimplerBatchPublisher; +import com.att.nsa.mr.client.response.MRPublisherResponse; +import com.att.nsa.mr.test.clients.ProtocolTypeConstants; +import com.fasterxml.jackson.annotation.JsonIgnore; + +import java.net.MalformedURLException; +import java.security.GeneralSecurityException; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.Properties; +import java.util.concurrent.TimeUnit; + +import org.onap.policy.common.endpoints.event.comm.bus.DmaapTopicSinkFactory; +import org.onap.policy.common.endpoints.properties.PolicyEndPointProperties; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + public interface BusPublisher { /** @@ -36,4 +58,325 @@ public interface BusPublisher { * closes the publisher */ public void close(); + + /** + * Cambria based library publisher + */ + public static class CambriaPublisherWrapper implements BusPublisher { + + private static Logger logger = LoggerFactory.getLogger(CambriaPublisherWrapper.class); + + /** + * The actual Cambria publisher + */ + @JsonIgnore + protected volatile CambriaBatchingPublisher publisher; + + public CambriaPublisherWrapper(List<String> servers, String topic, String apiKey, String apiSecret, + boolean useHttps) { + this(servers, topic, apiKey, apiSecret, null, null, useHttps, false); + } + + public CambriaPublisherWrapper(List<String> servers, String topic, String apiKey, String apiSecret, + String username, String password, boolean useHttps, boolean selfSignedCerts) { + + PublisherBuilder builder = new CambriaClientBuilders.PublisherBuilder(); + + builder.usingHosts(servers).onTopic(topic); + + // Set read timeout to 30 seconds (TBD: this should be configurable) + builder.withSocketTimeout(30000); + + if (useHttps) { + if (selfSignedCerts) { + builder.withConnectionType(ConnectionType.HTTPS_NO_VALIDATION); + } else { + builder.withConnectionType(ConnectionType.HTTPS); + } + } + + + if (apiKey != null && !apiKey.isEmpty() && apiSecret != null && !apiSecret.isEmpty()) { + builder.authenticatedBy(apiKey, apiSecret); + } + + if (username != null && !username.isEmpty() && password != null && !password.isEmpty()) { + builder.authenticatedByHttp(username, password); + } + + try { + this.publisher = builder.build(); + } catch (MalformedURLException | GeneralSecurityException e) { + throw new IllegalArgumentException(e); + } + } + + /** + * {@inheritDoc} + */ + @Override + public boolean send(String partitionId, String message) { + if (message == null) { + throw new IllegalArgumentException("No message provided"); + } + + try { + this.publisher.send(partitionId, message); + } catch (Exception e) { + logger.warn("{}: SEND of {} cannot be performed because of {}", this, message, e.getMessage(), e); + return false; + } + return true; + } + + /** + * {@inheritDoc} + */ + @Override + public void close() { + logger.info("{}: CLOSE", this); + + try { + this.publisher.close(); + } catch (Exception e) { + logger.warn("{}: CLOSE FAILED because of {}", this, e.getMessage(), e); + } + } + + + @Override + public String toString() { + return "CambriaPublisherWrapper []"; + } + + } + + /** + * DmaapClient library wrapper + */ + public abstract class DmaapPublisherWrapper implements BusPublisher { + + private static Logger logger = LoggerFactory.getLogger(DmaapPublisherWrapper.class); + + /** + * MR based Publisher + */ + protected MRSimplerBatchPublisher publisher; + protected Properties props; + + /** + * MR Publisher Wrapper + * + * @param servers messaging bus hosts + * @param topic topic + * @param username AAF or DME2 Login + * @param password AAF or DME2 Password + */ + public DmaapPublisherWrapper(ProtocolTypeConstants protocol, List<String> servers, String topic, + String username, String password, boolean useHttps) { + + + if (topic == null || topic.isEmpty()) { + throw new IllegalArgumentException("No topic for DMaaP"); + } + + + if (protocol == ProtocolTypeConstants.AAF_AUTH) { + if (servers == null || servers.isEmpty()) { + throw new IllegalArgumentException("No DMaaP servers or DME2 partner provided"); + } + + ArrayList<String> dmaapServers = new ArrayList<>(); + if (useHttps) { + for (String server : servers) { + dmaapServers.add(server + ":3905"); + } + + } else { + for (String server : servers) { + dmaapServers.add(server + ":3904"); + } + } + + + this.publisher = new MRSimplerBatchPublisher.Builder().againstUrls(dmaapServers).onTopic(topic).build(); + + this.publisher.setProtocolFlag(ProtocolTypeConstants.AAF_AUTH.getValue()); + } else if (protocol == ProtocolTypeConstants.DME2) { + ArrayList<String> dmaapServers = new ArrayList<>(); + dmaapServers.add("0.0.0.0:3904"); + + this.publisher = new MRSimplerBatchPublisher.Builder().againstUrls(dmaapServers).onTopic(topic).build(); + + this.publisher.setProtocolFlag(ProtocolTypeConstants.DME2.getValue()); + } + + this.publisher.logTo(LoggerFactory.getLogger(MRSimplerBatchPublisher.class.getName())); + + this.publisher.setUsername(username); + this.publisher.setPassword(password); + + props = new Properties(); + + if (useHttps) { + props.setProperty("Protocol", "https"); + } else { + props.setProperty("Protocol", "http"); + } + + props.setProperty("contenttype", "application/json"); + props.setProperty("username", username); + props.setProperty("password", password); + + props.setProperty("topic", topic); + + this.publisher.setProps(props); + + if (protocol == ProtocolTypeConstants.AAF_AUTH) { + this.publisher.setHost(servers.get(0)); + } + + logger.info("{}: CREATION: using protocol {}", this, protocol.getValue()); + } + + /** + * {@inheritDoc} + */ + @Override + public void close() { + logger.info("{}: CLOSE", this); + + try { + this.publisher.close(1, TimeUnit.SECONDS); + } catch (Exception e) { + logger.warn("{}: CLOSE FAILED because of {}", this, e.getMessage(), e); + } + } + + /** + * {@inheritDoc} + */ + @Override + public boolean send(String partitionId, String message) { + if (message == null) { + throw new IllegalArgumentException("No message provided"); + } + + this.publisher.setPubResponse(new MRPublisherResponse()); + this.publisher.send(partitionId, message); + MRPublisherResponse response = this.publisher.sendBatchWithResponse(); + if (response != null) { + logger.debug("DMaaP publisher received {} : {}", response.getResponseCode(), + response.getResponseMessage()); + } + + return true; + } + + @Override + public String toString() { + return "DmaapPublisherWrapper [" + "publisher.getAuthDate()=" + publisher.getAuthDate() + + ", publisher.getAuthKey()=" + publisher.getAuthKey() + ", publisher.getHost()=" + + publisher.getHost() + ", publisher.getProtocolFlag()=" + publisher.getProtocolFlag() + + ", publisher.getUsername()=" + publisher.getUsername() + "]"; + } + } + + /** + * DmaapClient library wrapper + */ + public static class DmaapAafPublisherWrapper extends DmaapPublisherWrapper { + /** + * MR based Publisher + */ + public DmaapAafPublisherWrapper(List<String> servers, String topic, String aafLogin, String aafPassword, + boolean useHttps) { + + super(ProtocolTypeConstants.AAF_AUTH, servers, topic, aafLogin, aafPassword, useHttps); + } + } + + public static class DmaapDmePublisherWrapper extends DmaapPublisherWrapper { + public DmaapDmePublisherWrapper(List<String> servers, String topic, String username, String password, + String environment, String aftEnvironment, String dme2Partner, String latitude, String longitude, + Map<String, String> additionalProps, boolean useHttps) { + + super(ProtocolTypeConstants.DME2, servers, topic, username, password, useHttps); + + + + String dme2RouteOffer = additionalProps.get(DmaapTopicSinkFactory.DME2_ROUTE_OFFER_PROPERTY); + + if (environment == null || environment.isEmpty()) { + throw parmException(topic, PolicyEndPointProperties.PROPERTY_DMAAP_DME2_ENVIRONMENT_SUFFIX); + } + if (aftEnvironment == null || aftEnvironment.isEmpty()) { + throw parmException(topic, PolicyEndPointProperties.PROPERTY_DMAAP_DME2_AFT_ENVIRONMENT_SUFFIX); + } + if (latitude == null || latitude.isEmpty()) { + throw parmException(topic, PolicyEndPointProperties.PROPERTY_DMAAP_DME2_LATITUDE_SUFFIX); + } + if (longitude == null || longitude.isEmpty()) { + throw parmException(topic, PolicyEndPointProperties.PROPERTY_DMAAP_DME2_LONGITUDE_SUFFIX); + } + + if ((dme2Partner == null || dme2Partner.isEmpty()) + && (dme2RouteOffer == null || dme2RouteOffer.isEmpty())) { + throw new IllegalArgumentException( + "Must provide at least " + PolicyEndPointProperties.PROPERTY_DMAAP_SOURCE_TOPICS + "." + topic + + PolicyEndPointProperties.PROPERTY_DMAAP_DME2_PARTNER_SUFFIX + " or " + + PolicyEndPointProperties.PROPERTY_DMAAP_SINK_TOPICS + "." + topic + + PolicyEndPointProperties.PROPERTY_DMAAP_DME2_ROUTE_OFFER_SUFFIX + " for DME2"); + } + + String serviceName = servers.get(0); + + /* These are required, no defaults */ + props.setProperty("Environment", environment); + props.setProperty("AFT_ENVIRONMENT", aftEnvironment); + + props.setProperty(DmaapTopicSinkFactory.DME2_SERVICE_NAME_PROPERTY, serviceName); + + if (dme2Partner != null) { + props.setProperty("Partner", dme2Partner); + } + if (dme2RouteOffer != null) { + props.setProperty(DmaapTopicSinkFactory.DME2_ROUTE_OFFER_PROPERTY, dme2RouteOffer); + } + + props.setProperty("Latitude", latitude); + props.setProperty("Longitude", longitude); + + // ServiceName also a default, found in additionalProps + + /* These are optional, will default to these values if not set in optionalProps */ + props.setProperty("AFT_DME2_EP_READ_TIMEOUT_MS", "50000"); + props.setProperty("AFT_DME2_ROUNDTRIP_TIMEOUT_MS", "240000"); + props.setProperty("AFT_DME2_EP_CONN_TIMEOUT", "15000"); + props.setProperty("Version", "1.0"); + props.setProperty("SubContextPath", "/"); + props.setProperty("sessionstickinessrequired", "no"); + + /* These should not change */ + props.setProperty("TransportType", "DME2"); + props.setProperty("MethodType", "POST"); + + for (Map.Entry<String, String> entry : additionalProps.entrySet()) { + String key = entry.getKey(); + String value = entry.getValue(); + + if (value != null) { + props.setProperty(key, value); + } + } + + this.publisher.setProps(props); + } + + private IllegalArgumentException parmException(String topic, String propnm) { + return new IllegalArgumentException("Missing " + PolicyEndPointProperties.PROPERTY_DMAAP_SINK_TOPICS + "." + + topic + propnm + " property for DME2 in DMaaP"); + + } + } } diff --git a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/event/comm/bus/internal/FilterableBusConsumer.java b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/event/comm/bus/internal/FilterableBusConsumer.java deleted file mode 100644 index 76adf980..00000000 --- a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/event/comm/bus/internal/FilterableBusConsumer.java +++ /dev/null @@ -1,35 +0,0 @@ -/* - * ============LICENSE_START======================================================= - * policy-endpoints - * ================================================================================ - * Copyright (C) 2017-2018 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. - * ============LICENSE_END========================================================= - */ - -package org.onap.policy.common.endpoints.event.comm.bus.internal; - -/** - * BusConsumer that supports server-side filtering. - */ -public interface FilterableBusConsumer extends BusConsumer { - - /** - * Sets the server-side filter. - * - * @param filter new filter value, or {@code null} - * @throws IllegalArgumentException if the consumer cannot be built with the new filter - */ - public void setFilter(String filter); -} diff --git a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/event/comm/bus/internal/InlineDmaapTopicSink.java b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/event/comm/bus/internal/InlineDmaapTopicSink.java index 2ad8bb34..3ea7185e 100644 --- a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/event/comm/bus/internal/InlineDmaapTopicSink.java +++ b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/event/comm/bus/internal/InlineDmaapTopicSink.java @@ -25,8 +25,6 @@ import java.util.Map; import org.onap.policy.common.endpoints.event.comm.Topic; import org.onap.policy.common.endpoints.event.comm.bus.DmaapTopicSink; -import org.onap.policy.common.endpoints.event.comm.bus.internal.impl.CambriaPublisherWrapper; -import org.onap.policy.common.endpoints.event.comm.bus.internal.impl.DmaapDmePublisherWrapper; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -103,11 +101,11 @@ public class InlineDmaapTopicSink extends InlineBusTopicSink implements DmaapTop @Override public void init() { if (allNullOrEmpty(this.environment, this.aftEnvironment, this.latitude, this.longitude, this.partner)) { - this.publisher = new CambriaPublisherWrapper(this.servers, this.topic, this.apiKey, this.apiSecret, - this.userName, this.password, this.useHttps); + this.publisher = new BusPublisher.CambriaPublisherWrapper(this.servers, this.topic, this.apiKey, + this.apiSecret, this.userName, this.password, this.useHttps, this.allowSelfSignedCerts); } else { - this.publisher = new DmaapDmePublisherWrapper(this.servers, this.topic, this.userName, this.password, - this.environment, this.aftEnvironment, this.partner, this.latitude, this.longitude, + this.publisher = new BusPublisher.DmaapDmePublisherWrapper(this.servers, this.topic, this.userName, + this.password, this.environment, this.aftEnvironment, this.partner, this.latitude, this.longitude, this.additionalProps, this.useHttps); } diff --git a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/event/comm/bus/internal/InlineUebTopicSink.java b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/event/comm/bus/internal/InlineUebTopicSink.java index 24cf6073..fefe6493 100644 --- a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/event/comm/bus/internal/InlineUebTopicSink.java +++ b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/event/comm/bus/internal/InlineUebTopicSink.java @@ -24,7 +24,6 @@ import java.util.List; import org.onap.policy.common.endpoints.event.comm.Topic; import org.onap.policy.common.endpoints.event.comm.bus.UebTopicSink; -import org.onap.policy.common.endpoints.event.comm.bus.internal.impl.CambriaPublisherWrapper; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -63,8 +62,8 @@ public class InlineUebTopicSink extends InlineBusTopicSink implements UebTopicSi @Override public void init() { - this.publisher = - new CambriaPublisherWrapper(this.servers, this.topic, this.apiKey, this.apiSecret, this.useHttps); + this.publisher = new BusPublisher.CambriaPublisherWrapper(this.servers, this.topic, this.apiKey, this.apiSecret, + null, null, this.useHttps, this.allowSelfSignedCerts); logger.info("{}: UEB SINK created", this); } diff --git a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/event/comm/bus/internal/SingleThreadedBusTopicSource.java b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/event/comm/bus/internal/SingleThreadedBusTopicSource.java index d59c63f5..97ebbd50 100644 --- a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/event/comm/bus/internal/SingleThreadedBusTopicSource.java +++ b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/event/comm/bus/internal/SingleThreadedBusTopicSource.java @@ -27,6 +27,7 @@ import java.util.UUID; import org.onap.policy.common.endpoints.event.comm.FilterableTopicSource; import org.onap.policy.common.endpoints.event.comm.TopicListener; import org.onap.policy.common.endpoints.event.comm.bus.BusTopicSource; +import org.onap.policy.common.endpoints.event.comm.bus.internal.BusConsumer.FilterableBusConsumer; import org.onap.policy.common.utils.network.NetworkUtil; import org.slf4j.Logger; import org.slf4j.LoggerFactory; diff --git a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/event/comm/bus/internal/SingleThreadedDmaapTopicSource.java b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/event/comm/bus/internal/SingleThreadedDmaapTopicSource.java index de5fed6a..8ac41424 100644 --- a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/event/comm/bus/internal/SingleThreadedDmaapTopicSource.java +++ b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/event/comm/bus/internal/SingleThreadedDmaapTopicSource.java @@ -26,8 +26,6 @@ import java.util.Map; import org.onap.policy.common.endpoints.event.comm.Topic; import org.onap.policy.common.endpoints.event.comm.bus.DmaapTopicSource; -import org.onap.policy.common.endpoints.event.comm.bus.internal.impl.CambriaConsumerWrapper; -import org.onap.policy.common.endpoints.event.comm.bus.internal.impl.DmaapDmeConsumerWrapper; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -140,15 +138,15 @@ public class SingleThreadedDmaapTopicSource extends SingleThreadedBusTopicSource @Override public void init() throws MalformedURLException { if (anyNullOrEmpty(this.userName, this.password)) { - this.consumer = new CambriaConsumerWrapper(this.servers, this.topic, this.apiKey, this.apiSecret, - this.consumerGroup, this.consumerInstance, this.fetchTimeout, this.fetchLimit, this.useHttps, - this.allowSelfSignedCerts); + this.consumer = new BusConsumer.CambriaConsumerWrapper(this.servers, this.topic, this.apiKey, + this.apiSecret, this.consumerGroup, this.consumerInstance, this.fetchTimeout, this.fetchLimit, + this.useHttps, this.allowSelfSignedCerts); } else if (allNullOrEmpty(this.environment, this.aftEnvironment, this.latitude, this.longitude, this.partner)) { - this.consumer = new CambriaConsumerWrapper(this.servers, this.topic, this.apiKey, this.apiSecret, - this.userName, this.password, this.consumerGroup, this.consumerInstance, this.fetchTimeout, - this.fetchLimit, this.useHttps, this.allowSelfSignedCerts); + this.consumer = new BusConsumer.CambriaConsumerWrapper(this.servers, this.topic, this.apiKey, + this.apiSecret, this.userName, this.password, this.consumerGroup, this.consumerInstance, + this.fetchTimeout, this.fetchLimit, this.useHttps, this.allowSelfSignedCerts); } else { - this.consumer = new DmaapDmeConsumerWrapper(this.servers, this.topic, this.apiKey, + this.consumer = new BusConsumer.DmaapDmeConsumerWrapper(this.servers, this.topic, this.apiKey, this.apiSecret, this.userName, this.password, this.consumerGroup, this.consumerInstance, this.fetchTimeout, this.fetchLimit, this.environment, this.aftEnvironment, this.partner, this.latitude, this.longitude, this.additionalProps, this.useHttps); diff --git a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/event/comm/bus/internal/SingleThreadedUebTopicSource.java b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/event/comm/bus/internal/SingleThreadedUebTopicSource.java index 00e45422..b7a20503 100644 --- a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/event/comm/bus/internal/SingleThreadedUebTopicSource.java +++ b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/event/comm/bus/internal/SingleThreadedUebTopicSource.java @@ -24,7 +24,6 @@ import java.util.List; import org.onap.policy.common.endpoints.event.comm.Topic; import org.onap.policy.common.endpoints.event.comm.bus.UebTopicSource; -import org.onap.policy.common.endpoints.event.comm.bus.internal.impl.CambriaConsumerWrapper; /** * This topic source implementation specializes in reading messages over an UEB Bus topic source and @@ -66,7 +65,7 @@ public class SingleThreadedUebTopicSource extends SingleThreadedBusTopicSource i */ @Override public void init() { - this.consumer = new CambriaConsumerWrapper(this.servers, this.topic, this.apiKey, this.apiSecret, + this.consumer = new BusConsumer.CambriaConsumerWrapper(this.servers, this.topic, this.apiKey, this.apiSecret, this.consumerGroup, this.consumerInstance, this.fetchTimeout, this.fetchLimit, this.useHttps, this.allowSelfSignedCerts); } diff --git a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/event/comm/bus/internal/impl/CambriaConsumerWrapper.java b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/event/comm/bus/internal/impl/CambriaConsumerWrapper.java deleted file mode 100644 index fedde284..00000000 --- a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/event/comm/bus/internal/impl/CambriaConsumerWrapper.java +++ /dev/null @@ -1,210 +0,0 @@ -/* - * ============LICENSE_START======================================================= - * policy-endpoints - * ================================================================================ - * Copyright (C) 2017-2018 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. - * ============LICENSE_END========================================================= - */ - -package org.onap.policy.common.endpoints.event.comm.bus.internal.impl; - -import com.att.nsa.cambria.client.CambriaClientBuilders; -import com.att.nsa.cambria.client.CambriaClientBuilders.ConsumerBuilder; -import com.att.nsa.cambria.client.CambriaConsumer; - -import java.io.IOException; -import java.net.MalformedURLException; -import java.security.GeneralSecurityException; -import java.util.List; - -import org.onap.policy.common.endpoints.event.comm.bus.internal.FilterableBusConsumer; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * Cambria based consumer - */ -public class CambriaConsumerWrapper implements FilterableBusConsumer { - - /** - * logger - */ - private static Logger logger = LoggerFactory.getLogger(CambriaConsumerWrapper.class); - - /** - * Used to build the consumer. - */ - private final ConsumerBuilder builder; - - /** - * Locked while updating {@link #consumer} and {@link #newConsumer}. - */ - private final Object consLocker = new Object(); - - /** - * Cambria client - */ - private CambriaConsumer consumer; - - /** - * Cambria client to use for next fetch - */ - private CambriaConsumer newConsumer = null; - - /** - * fetch timeout - */ - protected int fetchTimeout; - - /** - * close condition - */ - protected Object closeCondition = new Object(); - - /** - * Cambria Consumer Wrapper - * - * @param servers messaging bus hosts - * @param topic topic - * @param apiKey API Key - * @param apiSecret API Secret - * @param consumerGroup Consumer Group - * @param consumerInstance Consumer Instance - * @param fetchTimeout Fetch Timeout - * @param fetchLimit Fetch Limit - * @throws GeneralSecurityException - * @throws MalformedURLException - */ - public CambriaConsumerWrapper(List<String> servers, String topic, String apiKey, String apiSecret, - String consumerGroup, String consumerInstance, int fetchTimeout, int fetchLimit, boolean useHttps, - boolean useSelfSignedCerts) { - this(servers, topic, apiKey, apiSecret, null, null, consumerGroup, consumerInstance, fetchTimeout, fetchLimit, - useHttps, useSelfSignedCerts); - } - - public CambriaConsumerWrapper(List<String> servers, String topic, String apiKey, String apiSecret, String username, - String password, String consumerGroup, String consumerInstance, int fetchTimeout, int fetchLimit, - boolean useHttps, boolean useSelfSignedCerts) { - - this.fetchTimeout = fetchTimeout; - - this.builder = new CambriaClientBuilders.ConsumerBuilder(); - - builder.knownAs(consumerGroup, consumerInstance).usingHosts(servers).onTopic(topic).waitAtServer(fetchTimeout) - .receivingAtMost(fetchLimit); - - // Set read timeout to fetch timeout + 30 seconds (TBD: this should be configurable) - builder.withSocketTimeout(fetchTimeout + 30000); - - if (useHttps) { - builder.usingHttps(); - - if (useSelfSignedCerts) { - builder.allowSelfSignedCertificates(); - } - } - - if (apiKey != null && !apiKey.isEmpty() && apiSecret != null && !apiSecret.isEmpty()) { - builder.authenticatedBy(apiKey, apiSecret); - } - - if (username != null && !username.isEmpty() && password != null && !password.isEmpty()) { - builder.authenticatedByHttp(username, password); - } - - try { - this.consumer = builder.build(); - } catch (MalformedURLException | GeneralSecurityException e) { - throw new IllegalArgumentException(e); - } - } - - @Override - public Iterable<String> fetch() throws IOException, InterruptedException { - try { - return getCurrentConsumer().fetch(); - } catch (final IOException e) { - logger.error("{}: cannot fetch because of {} - backoff for {} ms.", this, e.getMessage(), - this.fetchTimeout); - synchronized (this.closeCondition) { - this.closeCondition.wait(this.fetchTimeout); - } - - throw e; - } - } - - @Override - public void close() { - synchronized (closeCondition) { - closeCondition.notifyAll(); - } - - getCurrentConsumer().close(); - } - - private CambriaConsumer getCurrentConsumer() { - CambriaConsumer old = null; - CambriaConsumer ret; - - synchronized (consLocker) { - if (this.newConsumer != null) { - // replace old consumer with new consumer - old = this.consumer; - this.consumer = this.newConsumer; - this.newConsumer = null; - } - - ret = this.consumer; - } - - if (old != null) { - old.close(); - } - - return ret; - } - - @Override - public void setFilter(String filter) { - logger.info("{}: setting DMAAP server-side filter: {}", this, filter); - builder.withServerSideFilter(filter); - - try { - CambriaConsumer previous; - synchronized (consLocker) { - previous = this.newConsumer; - this.newConsumer = builder.build(); - } - - if (previous != null) { - // there was already a new consumer - close it - previous.close(); - } - - } catch (MalformedURLException | GeneralSecurityException e) { - /* - * Since an exception occurred, "consumer" still has its old value, thus it should not - * be closed at this point. - */ - throw new IllegalArgumentException(e); - } - } - - @Override - public String toString() { - return "CambriaConsumerWrapper [fetchTimeout=" + fetchTimeout + "]"; - } -} diff --git a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/event/comm/bus/internal/impl/CambriaPublisherWrapper.java b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/event/comm/bus/internal/impl/CambriaPublisherWrapper.java deleted file mode 100644 index ab5868ab..00000000 --- a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/event/comm/bus/internal/impl/CambriaPublisherWrapper.java +++ /dev/null @@ -1,122 +0,0 @@ -/* - * ============LICENSE_START======================================================= - * policy-endpoints - * ================================================================================ - * Copyright (C) 2017-2018 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. - * ============LICENSE_END========================================================= - */ - -package org.onap.policy.common.endpoints.event.comm.bus.internal.impl; - -import com.att.nsa.cambria.client.CambriaBatchingPublisher; -import com.att.nsa.cambria.client.CambriaClientBuilders; -import com.att.nsa.cambria.client.CambriaClientBuilders.PublisherBuilder; -import com.fasterxml.jackson.annotation.JsonIgnore; - -import java.net.MalformedURLException; -import java.security.GeneralSecurityException; -import java.util.List; - -import org.onap.policy.common.endpoints.event.comm.bus.internal.BusPublisher; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * Cambria based library publisher - */ -public class CambriaPublisherWrapper implements BusPublisher { - - private static Logger logger = LoggerFactory.getLogger(CambriaPublisherWrapper.class); - - /** - * The actual Cambria publisher - */ - @JsonIgnore - protected volatile CambriaBatchingPublisher publisher; - - public CambriaPublisherWrapper(List<String> servers, String topic, String apiKey, String apiSecret, - boolean useHttps) { - this(servers, topic, apiKey, apiSecret, null, null, useHttps); - } - - public CambriaPublisherWrapper(List<String> servers, String topic, String apiKey, String apiSecret, String username, - String password, boolean useHttps) { - - PublisherBuilder builder = new CambriaClientBuilders.PublisherBuilder(); - - builder.usingHosts(servers).onTopic(topic); - - // Set read timeout to 30 seconds (TBD: this should be configurable) - builder.withSocketTimeout(30000); - - if (useHttps) { - builder.usingHttps(); - } - - - if (apiKey != null && !apiKey.isEmpty() && apiSecret != null && !apiSecret.isEmpty()) { - builder.authenticatedBy(apiKey, apiSecret); - } - - if (username != null && !username.isEmpty() && password != null && !password.isEmpty()) { - builder.authenticatedByHttp(username, password); - } - - try { - this.publisher = builder.build(); - } catch (MalformedURLException | GeneralSecurityException e) { - throw new IllegalArgumentException(e); - } - } - - /** - * {@inheritDoc} - */ - @Override - public boolean send(String partitionId, String message) { - if (message == null) { - throw new IllegalArgumentException("No message provided"); - } - - try { - this.publisher.send(partitionId, message); - } catch (Exception e) { - logger.warn("{}: SEND of {} cannot be performed because of {}", this, message, e.getMessage(), e); - return false; - } - return true; - } - - /** - * {@inheritDoc} - */ - @Override - public void close() { - logger.info("{}: CLOSE", this); - - try { - this.publisher.close(); - } catch (Exception e) { - logger.warn("{}: CLOSE FAILED because of {}", this, e.getMessage(), e); - } - } - - - @Override - public String toString() { - return "CambriaPublisherWrapper []"; - } - -} diff --git a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/event/comm/bus/internal/impl/DmaapAafConsumerWrapper.java b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/event/comm/bus/internal/impl/DmaapAafConsumerWrapper.java deleted file mode 100644 index 29fbf1d5..00000000 --- a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/event/comm/bus/internal/impl/DmaapAafConsumerWrapper.java +++ /dev/null @@ -1,95 +0,0 @@ -/* - * ============LICENSE_START======================================================= - * policy-endpoints - * ================================================================================ - * Copyright (C) 2017-2018 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. - * ============LICENSE_END========================================================= - */ - -package org.onap.policy.common.endpoints.event.comm.bus.internal.impl; - -import com.att.nsa.mr.client.impl.MRConsumerImpl; -import com.att.nsa.mr.test.clients.ProtocolTypeConstants; - -import java.net.MalformedURLException; -import java.util.List; -import java.util.Properties; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * MR based consumer - */ -public class DmaapAafConsumerWrapper extends DmaapConsumerWrapper { - - private static Logger logger = LoggerFactory.getLogger(DmaapAafConsumerWrapper.class); - - private final Properties props; - - /** - * MR Consumer Wrapper - * - * @param servers messaging bus hosts - * @param topic topic - * @param apiKey API Key - * @param apiSecret API Secret - * @param aafLogin AAF Login - * @param aafPassword AAF Password - * @param consumerGroup Consumer Group - * @param consumerInstance Consumer Instance - * @param fetchTimeout Fetch Timeout - * @param fetchLimit Fetch Limit - * @throws MalformedURLException - */ - public DmaapAafConsumerWrapper(List<String> servers, String topic, String apiKey, String apiSecret, String aafLogin, - String aafPassword, String consumerGroup, String consumerInstance, int fetchTimeout, int fetchLimit, - boolean useHttps) throws MalformedURLException { - - super(servers, topic, apiKey, apiSecret, aafLogin, aafPassword, consumerGroup, consumerInstance, fetchTimeout, - fetchLimit); - - // super constructor sets servers = {""} if empty to avoid errors when using DME2 - if ((servers.size() == 1 && ("".equals(servers.get(0)))) || (servers == null) || (servers.isEmpty())) { - throw new IllegalArgumentException("Must provide at least one host for HTTP AAF"); - } - - this.consumer.setProtocolFlag(ProtocolTypeConstants.AAF_AUTH.getValue()); - - props = new Properties(); - - if (useHttps) { - props.setProperty(PROTOCOL_PROP, "https"); - this.consumer.setHost(servers.get(0) + ":3905"); - - } else { - props.setProperty(PROTOCOL_PROP, "http"); - this.consumer.setHost(servers.get(0) + ":3904"); - } - - this.consumer.setProps(props); - logger.info("{}: CREATION", this); - } - - @Override - public String toString() { - final MRConsumerImpl consumer = this.consumer; - - return "DmaapConsumerWrapper [" + "consumer.getAuthDate()=" + consumer.getAuthDate() - + ", consumer.getAuthKey()=" + consumer.getAuthKey() + ", consumer.getHost()=" + consumer.getHost() - + ", consumer.getProtocolFlag()=" + consumer.getProtocolFlag() + ", consumer.getUsername()=" - + consumer.getUsername() + "]"; - } -} diff --git a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/event/comm/bus/internal/impl/DmaapConsumerWrapper.java b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/event/comm/bus/internal/impl/DmaapConsumerWrapper.java deleted file mode 100644 index 0806c3d3..00000000 --- a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/event/comm/bus/internal/impl/DmaapConsumerWrapper.java +++ /dev/null @@ -1,146 +0,0 @@ -/* - * ============LICENSE_START======================================================= - * policy-endpoints - * ================================================================================ - * Copyright (C) 2017-2018 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. - * ============LICENSE_END========================================================= - */ - -package org.onap.policy.common.endpoints.event.comm.bus.internal.impl; - -import com.att.nsa.mr.client.impl.MRConsumerImpl; -import com.att.nsa.mr.client.response.MRConsumerResponse; - -import java.io.IOException; -import java.net.MalformedURLException; -import java.util.ArrayList; -import java.util.List; - -import org.onap.policy.common.endpoints.event.comm.bus.internal.BusConsumer; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * MR based consumer - */ -public abstract class DmaapConsumerWrapper implements BusConsumer { - - /** - * logger - */ - private static Logger logger = LoggerFactory.getLogger(DmaapConsumerWrapper.class); - - /** - * Name of the "protocol" property. - */ - protected static final String PROTOCOL_PROP = "Protocol"; - - /** - * fetch timeout - */ - protected int fetchTimeout; - - /** - * close condition - */ - protected Object closeCondition = new Object(); - - /** - * MR Consumer - */ - protected MRConsumerImpl consumer; - - /** - * MR Consumer Wrapper - * - * @param servers messaging bus hosts - * @param topic topic - * @param apiKey API Key - * @param apiSecret API Secret - * @param username AAF Login - * @param password AAF Password - * @param consumerGroup Consumer Group - * @param consumerInstance Consumer Instance - * @param fetchTimeout Fetch Timeout - * @param fetchLimit Fetch Limit - * @throws MalformedURLException - */ - public DmaapConsumerWrapper(List<String> servers, String topic, String apiKey, String apiSecret, String username, - String password, String consumerGroup, String consumerInstance, int fetchTimeout, int fetchLimit) - throws MalformedURLException { - - this.fetchTimeout = fetchTimeout; - - if (topic == null || topic.isEmpty()) { - throw new IllegalArgumentException("No topic for DMaaP"); - } - - this.consumer = new MRConsumerImpl(servers, topic, consumerGroup, consumerInstance, fetchTimeout, fetchLimit, - null, apiKey, apiSecret); - - this.consumer.setUsername(username); - this.consumer.setPassword(password); - } - - @Override - public Iterable<String> fetch() throws InterruptedException, IOException { - final MRConsumerResponse response = this.consumer.fetchWithReturnConsumerResponse(); - if (response == null) { - logger.warn("{}: DMaaP NULL response received", this); - - synchronized (closeCondition) { - closeCondition.wait(fetchTimeout); - } - return new ArrayList<>(); - } else { - logger.debug("DMaaP consumer received {} : {}" + response.getResponseCode(), response.getResponseMessage()); - - if (response.getResponseCode() == null || !"200".equals(response.getResponseCode())) { - - logger.error("DMaaP consumer received: {} : {}", response.getResponseCode(), - response.getResponseMessage()); - - synchronized (closeCondition) { - closeCondition.wait(fetchTimeout); - } - - /* fall through */ - } - } - - if (response.getActualMessages() == null) { - return new ArrayList<>(); - } else { - return response.getActualMessages(); - } - } - - @Override - public void close() { - synchronized (closeCondition) { - closeCondition.notifyAll(); - } - - this.consumer.close(); - } - - @Override - public String toString() { - return "DmaapConsumerWrapper [" + "consumer.getAuthDate()=" + consumer.getAuthDate() - + ", consumer.getAuthKey()=" + consumer.getAuthKey() + ", consumer.getHost()=" + consumer.getHost() - + ", consumer.getProtocolFlag()=" + consumer.getProtocolFlag() + ", consumer.getUsername()=" - + consumer.getUsername() + "]"; - } -} diff --git a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/event/comm/bus/internal/impl/DmaapDmeConsumerWrapper.java b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/event/comm/bus/internal/impl/DmaapDmeConsumerWrapper.java deleted file mode 100644 index fd998e2b..00000000 --- a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/event/comm/bus/internal/impl/DmaapDmeConsumerWrapper.java +++ /dev/null @@ -1,144 +0,0 @@ -/* - * ============LICENSE_START======================================================= - * policy-endpoints - * ================================================================================ - * Copyright (C) 2017-2018 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. - * ============LICENSE_END========================================================= - */ - -package org.onap.policy.common.endpoints.event.comm.bus.internal.impl; - -import com.att.nsa.mr.client.MRClientFactory; -import com.att.nsa.mr.test.clients.ProtocolTypeConstants; - -import java.net.MalformedURLException; -import java.util.List; -import java.util.Map; -import java.util.Properties; - -import org.onap.policy.common.endpoints.event.comm.bus.DmaapTopicSinkFactory; -import org.onap.policy.common.endpoints.properties.PolicyEndPointProperties; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public class DmaapDmeConsumerWrapper extends DmaapConsumerWrapper { - - private static Logger logger = LoggerFactory.getLogger(DmaapDmeConsumerWrapper.class); - - private final Properties props; - - public DmaapDmeConsumerWrapper(List<String> servers, String topic, String apiKey, String apiSecret, - String dme2Login, String dme2Password, String consumerGroup, String consumerInstance, int fetchTimeout, - int fetchLimit, String environment, String aftEnvironment, String dme2Partner, String latitude, - String longitude, Map<String, String> additionalProps, boolean useHttps) throws MalformedURLException { - - - - super(servers, topic, apiKey, apiSecret, dme2Login, dme2Password, consumerGroup, consumerInstance, fetchTimeout, - fetchLimit); - - - final String dme2RouteOffer = additionalProps.get(DmaapTopicSinkFactory.DME2_ROUTE_OFFER_PROPERTY); - - if (environment == null || environment.isEmpty()) { - throw parmException(topic, PolicyEndPointProperties.PROPERTY_DMAAP_DME2_ENVIRONMENT_SUFFIX); - } - if (aftEnvironment == null || aftEnvironment.isEmpty()) { - throw parmException(topic, PolicyEndPointProperties.PROPERTY_DMAAP_DME2_AFT_ENVIRONMENT_SUFFIX); - } - if (latitude == null || latitude.isEmpty()) { - throw parmException(topic, PolicyEndPointProperties.PROPERTY_DMAAP_DME2_LATITUDE_SUFFIX); - } - if (longitude == null || longitude.isEmpty()) { - throw parmException(topic, PolicyEndPointProperties.PROPERTY_DMAAP_DME2_LONGITUDE_SUFFIX); - } - - if ((dme2Partner == null || dme2Partner.isEmpty()) && (dme2RouteOffer == null || dme2RouteOffer.isEmpty())) { - throw new IllegalArgumentException( - "Must provide at least " + PolicyEndPointProperties.PROPERTY_DMAAP_SOURCE_TOPICS + "." + topic - + PolicyEndPointProperties.PROPERTY_DMAAP_DME2_PARTNER_SUFFIX + " or " - + PolicyEndPointProperties.PROPERTY_DMAAP_SOURCE_TOPICS + "." + topic - + PolicyEndPointProperties.PROPERTY_DMAAP_DME2_ROUTE_OFFER_SUFFIX + " for DME2"); - } - - final String serviceName = servers.get(0); - - this.consumer.setProtocolFlag(ProtocolTypeConstants.DME2.getValue()); - - this.consumer.setUsername(dme2Login); - this.consumer.setPassword(dme2Password); - - props = new Properties(); - - props.setProperty(DmaapTopicSinkFactory.DME2_SERVICE_NAME_PROPERTY, serviceName); - - props.setProperty("username", dme2Login); - props.setProperty("password", dme2Password); - - /* These are required, no defaults */ - props.setProperty("topic", topic); - - props.setProperty("Environment", environment); - props.setProperty("AFT_ENVIRONMENT", aftEnvironment); - - if (dme2Partner != null) { - props.setProperty("Partner", dme2Partner); - } - if (dme2RouteOffer != null) { - props.setProperty(DmaapTopicSinkFactory.DME2_ROUTE_OFFER_PROPERTY, dme2RouteOffer); - } - - props.setProperty("Latitude", latitude); - props.setProperty("Longitude", longitude); - - /* These are optional, will default to these values if not set in additionalProps */ - props.setProperty("AFT_DME2_EP_READ_TIMEOUT_MS", "50000"); - props.setProperty("AFT_DME2_ROUNDTRIP_TIMEOUT_MS", "240000"); - props.setProperty("AFT_DME2_EP_CONN_TIMEOUT", "15000"); - props.setProperty("Version", "1.0"); - props.setProperty("SubContextPath", "/"); - props.setProperty("sessionstickinessrequired", "no"); - - /* These should not change */ - props.setProperty("TransportType", "DME2"); - props.setProperty("MethodType", "GET"); - - if (useHttps) { - props.setProperty(PROTOCOL_PROP, "https"); - - } else { - props.setProperty(PROTOCOL_PROP, "http"); - } - - props.setProperty("contenttype", "application/json"); - - if (additionalProps != null) { - for (Map.Entry<String, String> entry : additionalProps.entrySet()) { - props.put(entry.getKey(), entry.getValue()); - } - } - - MRClientFactory.prop = props; - this.consumer.setProps(props); - - logger.info("{}: CREATION", this); - } - - private IllegalArgumentException parmException(String topic, String propnm) { - return new IllegalArgumentException("Missing " + PolicyEndPointProperties.PROPERTY_DMAAP_SOURCE_TOPICS + "." - + topic + propnm + " property for DME2 in DMaaP"); - - } -} diff --git a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/event/comm/bus/internal/impl/DmaapDmePublisherWrapper.java b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/event/comm/bus/internal/impl/DmaapDmePublisherWrapper.java deleted file mode 100644 index e39954ed..00000000 --- a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/event/comm/bus/internal/impl/DmaapDmePublisherWrapper.java +++ /dev/null @@ -1,112 +0,0 @@ -/* - * ============LICENSE_START======================================================= - * policy-endpoints - * ================================================================================ - * Copyright (C) 2017-2018 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. - * ============LICENSE_END========================================================= - */ - -package org.onap.policy.common.endpoints.event.comm.bus.internal.impl; - -import com.att.nsa.mr.test.clients.ProtocolTypeConstants; - -import java.util.List; -import java.util.Map; - -import org.onap.policy.common.endpoints.event.comm.bus.DmaapTopicSinkFactory; -import org.onap.policy.common.endpoints.properties.PolicyEndPointProperties; - -public class DmaapDmePublisherWrapper extends DmaapPublisherWrapper { - public DmaapDmePublisherWrapper(List<String> servers, String topic, String username, String password, - String environment, String aftEnvironment, String dme2Partner, String latitude, String longitude, - Map<String, String> additionalProps, boolean useHttps) { - - super(ProtocolTypeConstants.DME2, servers, topic, username, password, useHttps); - - - - String dme2RouteOffer = additionalProps.get(DmaapTopicSinkFactory.DME2_ROUTE_OFFER_PROPERTY); - - if (environment == null || environment.isEmpty()) { - throw parmException(topic, PolicyEndPointProperties.PROPERTY_DMAAP_DME2_ENVIRONMENT_SUFFIX); - } - if (aftEnvironment == null || aftEnvironment.isEmpty()) { - throw parmException(topic, PolicyEndPointProperties.PROPERTY_DMAAP_DME2_AFT_ENVIRONMENT_SUFFIX); - } - if (latitude == null || latitude.isEmpty()) { - throw parmException(topic, PolicyEndPointProperties.PROPERTY_DMAAP_DME2_LATITUDE_SUFFIX); - } - if (longitude == null || longitude.isEmpty()) { - throw parmException(topic, PolicyEndPointProperties.PROPERTY_DMAAP_DME2_LONGITUDE_SUFFIX); - } - - if ((dme2Partner == null || dme2Partner.isEmpty()) && (dme2RouteOffer == null || dme2RouteOffer.isEmpty())) { - throw new IllegalArgumentException( - "Must provide at least " + PolicyEndPointProperties.PROPERTY_DMAAP_SOURCE_TOPICS + "." + topic - + PolicyEndPointProperties.PROPERTY_DMAAP_DME2_PARTNER_SUFFIX + " or " - + PolicyEndPointProperties.PROPERTY_DMAAP_SINK_TOPICS + "." + topic - + PolicyEndPointProperties.PROPERTY_DMAAP_DME2_ROUTE_OFFER_SUFFIX + " for DME2"); - } - - String serviceName = servers.get(0); - - /* These are required, no defaults */ - props.setProperty("Environment", environment); - props.setProperty("AFT_ENVIRONMENT", aftEnvironment); - - props.setProperty(DmaapTopicSinkFactory.DME2_SERVICE_NAME_PROPERTY, serviceName); - - if (dme2Partner != null) { - props.setProperty("Partner", dme2Partner); - } - if (dme2RouteOffer != null) { - props.setProperty(DmaapTopicSinkFactory.DME2_ROUTE_OFFER_PROPERTY, dme2RouteOffer); - } - - props.setProperty("Latitude", latitude); - props.setProperty("Longitude", longitude); - - // ServiceName also a default, found in additionalProps - - /* These are optional, will default to these values if not set in optionalProps */ - props.setProperty("AFT_DME2_EP_READ_TIMEOUT_MS", "50000"); - props.setProperty("AFT_DME2_ROUNDTRIP_TIMEOUT_MS", "240000"); - props.setProperty("AFT_DME2_EP_CONN_TIMEOUT", "15000"); - props.setProperty("Version", "1.0"); - props.setProperty("SubContextPath", "/"); - props.setProperty("sessionstickinessrequired", "no"); - - /* These should not change */ - props.setProperty("TransportType", "DME2"); - props.setProperty("MethodType", "POST"); - - for (Map.Entry<String, String> entry : additionalProps.entrySet()) { - String key = entry.getKey(); - String value = entry.getValue(); - - if (value != null) { - props.setProperty(key, value); - } - } - - this.publisher.setProps(props); - } - - private IllegalArgumentException parmException(String topic, String propnm) { - return new IllegalArgumentException("Missing " + PolicyEndPointProperties.PROPERTY_DMAAP_SINK_TOPICS + "." - + topic + propnm + " property for DME2 in DMaaP"); - - } -} diff --git a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/event/comm/bus/internal/impl/DmaapPublisherWrapper.java b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/event/comm/bus/internal/impl/DmaapPublisherWrapper.java deleted file mode 100644 index 396580e9..00000000 --- a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/event/comm/bus/internal/impl/DmaapPublisherWrapper.java +++ /dev/null @@ -1,164 +0,0 @@ -/* - * ============LICENSE_START======================================================= - * policy-endpoints - * ================================================================================ - * Copyright (C) 2017-2018 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. - * ============LICENSE_END========================================================= - */ - -package org.onap.policy.common.endpoints.event.comm.bus.internal.impl; - -import com.att.nsa.mr.client.impl.MRSimplerBatchPublisher; -import com.att.nsa.mr.client.response.MRPublisherResponse; -import com.att.nsa.mr.test.clients.ProtocolTypeConstants; - -import java.util.ArrayList; -import java.util.List; -import java.util.Properties; -import java.util.concurrent.TimeUnit; - -import org.onap.policy.common.endpoints.event.comm.bus.internal.BusPublisher; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * DmaapClient library wrapper - */ -public abstract class DmaapPublisherWrapper implements BusPublisher { - - private static Logger logger = LoggerFactory.getLogger(DmaapPublisherWrapper.class); - - /** - * MR based Publisher - */ - protected MRSimplerBatchPublisher publisher; - protected Properties props; - - /** - * MR Publisher Wrapper - * - * @param servers messaging bus hosts - * @param topic topic - * @param username AAF or DME2 Login - * @param password AAF or DME2 Password - */ - public DmaapPublisherWrapper(ProtocolTypeConstants protocol, List<String> servers, String topic, String username, - String password, boolean useHttps) { - - - if (topic == null || topic.isEmpty()) { - throw new IllegalArgumentException("No topic for DMaaP"); - } - - - if (protocol == ProtocolTypeConstants.AAF_AUTH) { - if (servers == null || servers.isEmpty()) { - throw new IllegalArgumentException("No DMaaP servers or DME2 partner provided"); - } - - ArrayList<String> dmaapServers = new ArrayList<>(); - if (useHttps) { - for (String server : servers) { - dmaapServers.add(server + ":3905"); - } - - } else { - for (String server : servers) { - dmaapServers.add(server + ":3904"); - } - } - - - this.publisher = new MRSimplerBatchPublisher.Builder().againstUrls(dmaapServers).onTopic(topic).build(); - - this.publisher.setProtocolFlag(ProtocolTypeConstants.AAF_AUTH.getValue()); - } else if (protocol == ProtocolTypeConstants.DME2) { - ArrayList<String> dmaapServers = new ArrayList<>(); - dmaapServers.add("0.0.0.0:3904"); - - this.publisher = new MRSimplerBatchPublisher.Builder().againstUrls(dmaapServers).onTopic(topic).build(); - - this.publisher.setProtocolFlag(ProtocolTypeConstants.DME2.getValue()); - } - - this.publisher.logTo(LoggerFactory.getLogger(MRSimplerBatchPublisher.class.getName())); - - this.publisher.setUsername(username); - this.publisher.setPassword(password); - - props = new Properties(); - - if (useHttps) { - props.setProperty("Protocol", "https"); - } else { - props.setProperty("Protocol", "http"); - } - - props.setProperty("contenttype", "application/json"); - props.setProperty("username", username); - props.setProperty("password", password); - - props.setProperty("topic", topic); - - this.publisher.setProps(props); - - if (protocol == ProtocolTypeConstants.AAF_AUTH) { - this.publisher.setHost(servers.get(0)); - } - - logger.info("{}: CREATION: using protocol {}", this, protocol.getValue()); - } - - /** - * {@inheritDoc} - */ - @Override - public void close() { - logger.info("{}: CLOSE", this); - - try { - this.publisher.close(1, TimeUnit.SECONDS); - } catch (Exception e) { - logger.warn("{}: CLOSE FAILED because of {}", this, e.getMessage(), e); - } - } - - /** - * {@inheritDoc} - */ - @Override - public boolean send(String partitionId, String message) { - if (message == null) { - throw new IllegalArgumentException("No message provided"); - } - - this.publisher.setPubResponse(new MRPublisherResponse()); - this.publisher.send(partitionId, message); - MRPublisherResponse response = this.publisher.sendBatchWithResponse(); - if (response != null) { - logger.debug("DMaaP publisher received {} : {}", response.getResponseCode(), response.getResponseMessage()); - } - - return true; - } - - @Override - public String toString() { - return "DmaapPublisherWrapper [" + "publisher.getAuthDate()=" + publisher.getAuthDate() - + ", publisher.getAuthKey()=" + publisher.getAuthKey() + ", publisher.getHost()=" + publisher.getHost() - + ", publisher.getProtocolFlag()=" + publisher.getProtocolFlag() + ", publisher.getUsername()=" - + publisher.getUsername() + "]"; - } -} diff --git a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/event/comm/impl/ProxyTopicEndpointManager.java b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/event/comm/impl/ProxyTopicEndpointManager.java deleted file mode 100644 index 779ab74d..00000000 --- a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/event/comm/impl/ProxyTopicEndpointManager.java +++ /dev/null @@ -1,482 +0,0 @@ -/* - * ============LICENSE_START======================================================= - * policy-endpoints - * ================================================================================ - * Copyright (C) 2017-2018 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. - * ============LICENSE_END========================================================= - */ - -package org.onap.policy.common.endpoints.event.comm.impl; - -import com.fasterxml.jackson.annotation.JsonIgnore; - -import java.util.ArrayList; -import java.util.List; -import java.util.Properties; - -import org.onap.policy.common.capabilities.Startable; -import org.onap.policy.common.endpoints.event.comm.Topic; -import org.onap.policy.common.endpoints.event.comm.TopicEndpoint; -import org.onap.policy.common.endpoints.event.comm.TopicSink; -import org.onap.policy.common.endpoints.event.comm.TopicSource; -import org.onap.policy.common.endpoints.event.comm.bus.DmaapTopicSink; -import org.onap.policy.common.endpoints.event.comm.bus.DmaapTopicSource; -import org.onap.policy.common.endpoints.event.comm.bus.NoopTopicSink; -import org.onap.policy.common.endpoints.event.comm.bus.UebTopicSink; -import org.onap.policy.common.endpoints.event.comm.bus.UebTopicSource; -import org.onap.policy.common.endpoints.event.comm.bus.impl.IndexedDmaapTopicSinkFactory; -import org.onap.policy.common.endpoints.event.comm.bus.impl.IndexedDmaapTopicSourceFactory; -import org.onap.policy.common.endpoints.event.comm.bus.impl.IndexedNoopTopicSinkFactory; -import org.onap.policy.common.endpoints.event.comm.bus.impl.IndexedUebTopicSinkFactory; -import org.onap.policy.common.endpoints.event.comm.bus.impl.IndexedUebTopicSourceFactory; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * This implementation of the Topic Endpoint Manager, proxies operations to appropriate - * implementations according to the communication infrastructure that are supported - */ -public class ProxyTopicEndpointManager implements TopicEndpoint { - /** - * Logger - */ - private static Logger logger = LoggerFactory.getLogger(ProxyTopicEndpointManager.class); - /** - * Is this element locked? - */ - protected volatile boolean locked = false; - - /** - * Is this element alive? - */ - protected volatile boolean alive = false; - - /** - * singleton for global access - */ - private static final TopicEndpoint manager = new ProxyTopicEndpointManager(); - - /** - * Get the singelton instance. - * - * @return the instance - */ - public static TopicEndpoint getInstance() { - return manager; - } - - @Override - public List<TopicSource> addTopicSources(Properties properties) { - - // 1. Create UEB Sources - // 2. Create DMAAP Sources - - final List<TopicSource> sources = new ArrayList<>(); - - sources.addAll(IndexedUebTopicSourceFactory.getInstance().build(properties)); - sources.addAll(IndexedDmaapTopicSourceFactory.getInstance().build(properties)); - - if (this.isLocked()) { - for (final TopicSource source : sources) { - source.lock(); - } - } - - return sources; - } - - @Override - public List<TopicSink> addTopicSinks(Properties properties) { - // 1. Create UEB Sinks - // 2. Create DMAAP Sinks - - final List<TopicSink> sinks = new ArrayList<>(); - - sinks.addAll(IndexedUebTopicSinkFactory.getInstance().build(properties)); - sinks.addAll(IndexedDmaapTopicSinkFactory.getInstance().build(properties)); - sinks.addAll(IndexedNoopTopicSinkFactory.getInstance().build(properties)); - - if (this.isLocked()) { - for (final TopicSink sink : sinks) { - sink.lock(); - } - } - - return sinks; - } - - @Override - public List<TopicSource> getTopicSources() { - - final List<TopicSource> sources = new ArrayList<>(); - - sources.addAll(IndexedUebTopicSourceFactory.getInstance().inventory()); - sources.addAll(IndexedDmaapTopicSourceFactory.getInstance().inventory()); - - return sources; - } - - @Override - public List<TopicSink> getTopicSinks() { - - final List<TopicSink> sinks = new ArrayList<>(); - - sinks.addAll(IndexedUebTopicSinkFactory.getInstance().inventory()); - sinks.addAll(IndexedDmaapTopicSinkFactory.getInstance().inventory()); - sinks.addAll(IndexedNoopTopicSinkFactory.getInstance().inventory()); - - return sinks; - } - - @JsonIgnore - @Override - public List<UebTopicSource> getUebTopicSources() { - return IndexedUebTopicSourceFactory.getInstance().inventory(); - } - - @JsonIgnore - @Override - public List<DmaapTopicSource> getDmaapTopicSources() { - return IndexedDmaapTopicSourceFactory.getInstance().inventory(); - } - - @JsonIgnore - @Override - public List<UebTopicSink> getUebTopicSinks() { - return IndexedUebTopicSinkFactory.getInstance().inventory(); - } - - @JsonIgnore - @Override - public List<DmaapTopicSink> getDmaapTopicSinks() { - return IndexedDmaapTopicSinkFactory.getInstance().inventory(); - } - - @JsonIgnore - @Override - public List<NoopTopicSink> getNoopTopicSinks() { - return IndexedNoopTopicSinkFactory.getInstance().inventory(); - } - - @Override - public boolean start() { - - synchronized (this) { - if (this.locked) { - throw new IllegalStateException(this + " is locked"); - } - - if (this.alive) { - return true; - } - - this.alive = true; - } - - final List<Startable> endpoints = this.getEndpoints(); - - boolean success = true; - for (final Startable endpoint : endpoints) { - try { - success = endpoint.start() && success; - } catch (final Exception e) { - success = false; - logger.error("Problem starting endpoint: {}", endpoint, e); - } - } - - return success; - } - - - @Override - public boolean stop() { - - /* - * stop regardless if it is locked, in other words, stop operation has precedence over - * locks. - */ - synchronized (this) { - this.alive = false; - } - - final List<Startable> endpoints = this.getEndpoints(); - - boolean success = true; - for (final Startable endpoint : endpoints) { - try { - success = endpoint.stop() && success; - } catch (final Exception e) { - success = false; - logger.error("Problem stopping endpoint: {}", endpoint, e); - } - } - - return success; - } - - /** - * - * @return list of managed endpoints - */ - @JsonIgnore - protected List<Startable> getEndpoints() { - final List<Startable> endpoints = new ArrayList<>(); - - endpoints.addAll(this.getTopicSources()); - endpoints.addAll(this.getTopicSinks()); - - return endpoints; - } - - @Override - public void shutdown() { - IndexedUebTopicSourceFactory.getInstance().destroy(); - IndexedUebTopicSinkFactory.getInstance().destroy(); - IndexedNoopTopicSinkFactory.getInstance().destroy(); - - IndexedDmaapTopicSourceFactory.getInstance().destroy(); - IndexedDmaapTopicSinkFactory.getInstance().destroy(); - } - - @Override - public boolean isAlive() { - return this.alive; - } - - @Override - public boolean lock() { - - synchronized (this) { - if (this.locked) { - return true; - } - - this.locked = true; - } - - for (final TopicSource source : this.getTopicSources()) { - source.lock(); - } - - for (final TopicSink sink : this.getTopicSinks()) { - sink.lock(); - } - - return true; - } - - @Override - public boolean unlock() { - synchronized (this) { - if (!this.locked) { - return true; - } - - this.locked = false; - } - - for (final TopicSource source : this.getTopicSources()) { - source.unlock(); - } - - for (final TopicSink sink : this.getTopicSinks()) { - sink.unlock(); - } - - return true; - } - - @Override - public boolean isLocked() { - return this.locked; - } - - @Override - public List<TopicSource> getTopicSources(List<String> topicNames) { - - if (topicNames == null) { - throw new IllegalArgumentException("must provide a list of topics"); - } - - final List<TopicSource> sources = new ArrayList<>(); - for (final String topic : topicNames) { - try { - final TopicSource uebSource = this.getUebTopicSource(topic); - if (uebSource != null) { - sources.add(uebSource); - } - } catch (final Exception e) { - logger.debug("No UEB source for topic: {}", topic, e); - } - - try { - final TopicSource dmaapSource = this.getDmaapTopicSource(topic); - if (dmaapSource != null) { - sources.add(dmaapSource); - } - } catch (final Exception e) { - logger.debug("No DMAAP source for topic: {}", topic, e); - } - } - return sources; - } - - @Override - public List<TopicSink> getTopicSinks(List<String> topicNames) { - - if (topicNames == null) { - throw new IllegalArgumentException("must provide a list of topics"); - } - - final List<TopicSink> sinks = new ArrayList<>(); - for (final String topic : topicNames) { - try { - final TopicSink uebSink = this.getUebTopicSink(topic); - if (uebSink != null) { - sinks.add(uebSink); - } - } catch (final Exception e) { - logger.debug("No UEB sink for topic: {}", topic, e); - } - - try { - final TopicSink dmaapSink = this.getDmaapTopicSink(topic); - if (dmaapSink != null) { - sinks.add(dmaapSink); - } - } catch (final Exception e) { - logger.debug("No DMAAP sink for topic: {}", topic, e); - } - - try { - final TopicSink noopSink = this.getNoopTopicSink(topic); - if (noopSink != null) { - sinks.add(noopSink); - } - } catch (final Exception e) { - logger.debug("No NOOP sink for topic: {}", topic, e); - } - } - return sinks; - } - - @Override - public TopicSource getTopicSource(Topic.CommInfrastructure commType, String topicName) { - - if (commType == null) { - throw parmException(topicName); - } - - if (topicName == null) { - throw parmException(topicName); - } - - switch (commType) { - case UEB: - return this.getUebTopicSource(topicName); - case DMAAP: - return this.getDmaapTopicSource(topicName); - default: - throw new UnsupportedOperationException("Unsupported " + commType.name()); - } - } - - private IllegalArgumentException parmException(String topicName) { - return new IllegalArgumentException( - "Invalid parameter: a communication infrastructure required to fetch " + topicName); - } - - @Override - public TopicSink getTopicSink(Topic.CommInfrastructure commType, String topicName) { - if (commType == null) { - throw parmException(topicName); - } - - if (topicName == null) { - throw parmException(topicName); - } - - switch (commType) { - case UEB: - return this.getUebTopicSink(topicName); - case DMAAP: - return this.getDmaapTopicSink(topicName); - case NOOP: - return this.getNoopTopicSink(topicName); - default: - throw new UnsupportedOperationException("Unsupported " + commType.name()); - } - } - - @Override - public List<TopicSink> getTopicSinks(String topicName) { - if (topicName == null) { - throw parmException(topicName); - } - - final List<TopicSink> sinks = new ArrayList<>(); - - try { - sinks.add(this.getUebTopicSink(topicName)); - } catch (final Exception e) { - logNoSink(topicName, e); - } - - try { - sinks.add(this.getDmaapTopicSink(topicName)); - } catch (final Exception e) { - logNoSink(topicName, e); - } - - try { - sinks.add(this.getNoopTopicSink(topicName)); - } catch (final Exception e) { - logNoSink(topicName, e); - } - - return sinks; - } - - private void logNoSink(String topicName, Exception ex) { - logger.debug("No sink for topic: {}", topicName, ex); - } - - @Override - public UebTopicSource getUebTopicSource(String topicName) { - return IndexedUebTopicSourceFactory.getInstance().get(topicName); - } - - @Override - public UebTopicSink getUebTopicSink(String topicName) { - return IndexedUebTopicSinkFactory.getInstance().get(topicName); - } - - @Override - public DmaapTopicSource getDmaapTopicSource(String topicName) { - return IndexedDmaapTopicSourceFactory.getInstance().get(topicName); - } - - @Override - public DmaapTopicSink getDmaapTopicSink(String topicName) { - return IndexedDmaapTopicSinkFactory.getInstance().get(topicName); - } - - @Override - public NoopTopicSink getNoopTopicSink(String topicName) { - return IndexedNoopTopicSinkFactory.getInstance().get(topicName); - } - -} diff --git a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/client/HttpClient.java b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/client/HttpClient.java index b4aea22c..f886e5ca 100644 --- a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/client/HttpClient.java +++ b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/client/HttpClient.java @@ -51,4 +51,7 @@ public interface HttpClient extends Startable { public String getPassword(); public String getBaseUrl(); + + + public static final HttpClientFactory factory = new IndexedHttpClientFactory(); } diff --git a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/client/HttpClientFactory.java b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/client/HttpClientFactory.java index 5435ee9c..ee07d129 100644 --- a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/client/HttpClientFactory.java +++ b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/client/HttpClientFactory.java @@ -22,9 +22,17 @@ package org.onap.policy.common.endpoints.http.client; import java.security.KeyManagementException; import java.security.NoSuchAlgorithmException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; import java.util.List; import java.util.Properties; +import org.onap.policy.common.endpoints.http.client.internal.JerseyClient; +import org.onap.policy.common.endpoints.properties.PolicyEndPointProperties; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + /** * Http Client Factory */ @@ -66,3 +74,140 @@ public interface HttpClientFactory { public void destroy(); } + + +/** + * http client factory implementation indexed by name + */ +class IndexedHttpClientFactory implements HttpClientFactory { + + /** + * Logger + */ + private static Logger logger = LoggerFactory.getLogger(IndexedHttpClientFactory.class); + + protected HashMap<String, HttpClient> clients = new HashMap<>(); + + @Override + public synchronized HttpClient build(String name, boolean https, boolean selfSignedCerts, String hostname, int port, + String baseUrl, String userName, String password, boolean managed) + throws KeyManagementException, NoSuchAlgorithmException { + if (clients.containsKey(name)) { + return clients.get(name); + } + + JerseyClient client = + new JerseyClient(name, https, selfSignedCerts, hostname, port, baseUrl, userName, password); + + if (managed) { + clients.put(name, client); + } + + return client; + } + + @Override + public synchronized List<HttpClient> build(Properties properties) + throws KeyManagementException, NoSuchAlgorithmException { + ArrayList<HttpClient> clientList = new ArrayList<>(); + + String clientNames = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES); + if (clientNames == null || clientNames.isEmpty()) { + return clientList; + } + + List<String> clientNameList = new ArrayList<>(Arrays.asList(clientNames.split("\\s*,\\s*"))); + + for (String clientName : clientNameList) { + String httpsString = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + "." + + clientName + PolicyEndPointProperties.PROPERTY_HTTP_HTTPS_SUFFIX); + boolean https = false; + if (httpsString != null && !httpsString.isEmpty()) { + https = Boolean.parseBoolean(httpsString); + } + + String hostName = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + "." + + clientName + PolicyEndPointProperties.PROPERTY_HTTP_HOST_SUFFIX); + + String servicePortString = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + + "." + clientName + PolicyEndPointProperties.PROPERTY_HTTP_PORT_SUFFIX); + int port; + try { + if (servicePortString == null || servicePortString.isEmpty()) { + continue; + } + port = Integer.parseInt(servicePortString); + } catch (NumberFormatException nfe) { + logger.error("http-client-factory: cannot parse port {}", servicePortString, nfe); + continue; + } + + String baseUrl = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + "." + + clientName + PolicyEndPointProperties.PROPERTY_HTTP_URL_SUFFIX); + + String userName = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + "." + + clientName + PolicyEndPointProperties.PROPERTY_HTTP_AUTH_USERNAME_SUFFIX); + + String password = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + "." + + clientName + PolicyEndPointProperties.PROPERTY_HTTP_AUTH_PASSWORD_SUFFIX); + + String managedString = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + "." + + clientName + PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX); + boolean managed = true; + if (managedString != null && !managedString.isEmpty()) { + managed = Boolean.parseBoolean(managedString); + } + + try { + HttpClient client = + this.build(clientName, https, https, hostName, port, baseUrl, userName, password, managed); + clientList.add(client); + } catch (Exception e) { + logger.error("http-client-factory: cannot build client {}", clientName, e); + } + } + + return clientList; + } + + @Override + public synchronized HttpClient get(String name) { + if (clients.containsKey(name)) { + return clients.get(name); + } + + throw new IllegalArgumentException("Http Client " + name + " not found"); + } + + @Override + public synchronized List<HttpClient> inventory() { + return new ArrayList<>(this.clients.values()); + } + + @Override + public synchronized void destroy(String name) { + if (!clients.containsKey(name)) { + return; + } + + HttpClient client = clients.remove(name); + try { + client.shutdown(); + } catch (IllegalStateException e) { + logger.error("http-client-factory: cannot shutdown client {}", client, e); + } + } + + @Override + public void destroy() { + List<HttpClient> clientsInventory = this.inventory(); + for (HttpClient client : clientsInventory) { + client.shutdown(); + } + + synchronized (this) { + this.clients.clear(); + } + } + +} diff --git a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/client/impl/IndexedHttpClientFactory.java b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/client/impl/IndexedHttpClientFactory.java deleted file mode 100644 index 2e911c9a..00000000 --- a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/client/impl/IndexedHttpClientFactory.java +++ /dev/null @@ -1,185 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * policy-endpoints - * ================================================================================ - * Copyright (C) 2017 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. - * ============LICENSE_END========================================================= - */ - -package org.onap.policy.common.endpoints.http.client.impl; - -import java.security.KeyManagementException; -import java.security.NoSuchAlgorithmException; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.HashMap; -import java.util.List; -import java.util.Properties; - -import org.onap.policy.common.endpoints.http.client.HttpClient; -import org.onap.policy.common.endpoints.http.client.HttpClientFactory; -import org.onap.policy.common.endpoints.http.client.internal.JerseyClient; -import org.onap.policy.common.endpoints.properties.PolicyEndPointProperties; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * http client factory implementation indexed by name - */ -public class IndexedHttpClientFactory implements HttpClientFactory { - - private static final HttpClientFactory instance = new IndexedHttpClientFactory(); - - /** - * Logger - */ - private static Logger logger = LoggerFactory.getLogger(IndexedHttpClientFactory.class); - - protected HashMap<String, HttpClient> clients = new HashMap<>(); - - /** - * Get the singleton instance. - * - * @return the instance - */ - public static HttpClientFactory getInstance() { - return instance; - } - - private IndexedHttpClientFactory() {} - - @Override - public synchronized HttpClient build(String name, boolean https, boolean selfSignedCerts, String hostname, int port, - String baseUrl, String userName, String password, boolean managed) - throws KeyManagementException, NoSuchAlgorithmException { - if (clients.containsKey(name)) { - return clients.get(name); - } - - JerseyClient client = - new JerseyClient(name, https, selfSignedCerts, hostname, port, baseUrl, userName, password); - - if (managed) { - clients.put(name, client); - } - - return client; - } - - @Override - public synchronized List<HttpClient> build(Properties properties) - throws KeyManagementException, NoSuchAlgorithmException { - ArrayList<HttpClient> clientList = new ArrayList<>(); - - String clientNames = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES); - if (clientNames == null || clientNames.isEmpty()) { - return clientList; - } - - List<String> clientNameList = new ArrayList<>(Arrays.asList(clientNames.split("\\s*,\\s*"))); - - for (String clientName : clientNameList) { - String httpsString = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + "." - + clientName + PolicyEndPointProperties.PROPERTY_HTTP_HTTPS_SUFFIX); - boolean https = false; - if (httpsString != null && !httpsString.isEmpty()) { - https = Boolean.parseBoolean(httpsString); - } - - String hostName = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + "." - + clientName + PolicyEndPointProperties.PROPERTY_HTTP_HOST_SUFFIX); - - String servicePortString = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES - + "." + clientName + PolicyEndPointProperties.PROPERTY_HTTP_PORT_SUFFIX); - int port; - try { - if (servicePortString == null || servicePortString.isEmpty()) { - continue; - } - port = Integer.parseInt(servicePortString); - } catch (NumberFormatException nfe) { - logger.error("http-client-factory: cannot parse port {}", servicePortString, nfe); - continue; - } - - String baseUrl = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + "." - + clientName + PolicyEndPointProperties.PROPERTY_HTTP_URL_SUFFIX); - - String userName = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + "." - + clientName + PolicyEndPointProperties.PROPERTY_HTTP_AUTH_USERNAME_SUFFIX); - - String password = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + "." - + clientName + PolicyEndPointProperties.PROPERTY_HTTP_AUTH_PASSWORD_SUFFIX); - - String managedString = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + "." - + clientName + PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX); - boolean managed = true; - if (managedString != null && !managedString.isEmpty()) { - managed = Boolean.parseBoolean(managedString); - } - - try { - HttpClient client = - this.build(clientName, https, https, hostName, port, baseUrl, userName, password, managed); - clientList.add(client); - } catch (Exception e) { - logger.error("http-client-factory: cannot build client {}", clientName, e); - } - } - - return clientList; - } - - @Override - public synchronized HttpClient get(String name) { - if (clients.containsKey(name)) { - return clients.get(name); - } - - throw new IllegalArgumentException("Http Client " + name + " not found"); - } - - @Override - public synchronized List<HttpClient> inventory() { - return new ArrayList<>(this.clients.values()); - } - - @Override - public synchronized void destroy(String name) { - if (!clients.containsKey(name)) { - return; - } - - HttpClient client = clients.remove(name); - try { - client.shutdown(); - } catch (IllegalStateException e) { - logger.error("http-client-factory: cannot shutdown client {}", client, e); - } - } - - @Override - public void destroy() { - List<HttpClient> clientsInventory = this.inventory(); - for (HttpClient client : clientsInventory) { - client.shutdown(); - } - - synchronized (this) { - this.clients.clear(); - } - } - -} diff --git a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/server/HttpServletServer.java b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/server/HttpServletServer.java index d62eb028..c1d1a353 100644 --- a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/server/HttpServletServer.java +++ b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/server/HttpServletServer.java @@ -27,6 +27,12 @@ import org.onap.policy.common.capabilities.Startable; */ public interface HttpServletServer extends Startable { + + /** + * factory for managing and tracking DMAAP sources + */ + public static HttpServletServerFactory factory = new IndexedHttpServletServerFactory(); + /** * * @return port diff --git a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/server/HttpServletServerFactory.java b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/server/HttpServletServerFactory.java index d0909b58..f09893b2 100644 --- a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/server/HttpServletServerFactory.java +++ b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/server/HttpServletServerFactory.java @@ -20,9 +20,17 @@ package org.onap.policy.common.endpoints.http.server; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; import java.util.List; import java.util.Properties; +import org.onap.policy.common.endpoints.http.server.internal.JettyJerseyServer; +import org.onap.policy.common.endpoints.properties.PolicyEndPointProperties; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + /** * Factory of HTTP Servlet-Enabled Servlets */ @@ -79,3 +87,172 @@ public interface HttpServletServerFactory { */ public void destroy(); } + + +/** + * Indexed factory implementation + */ +class IndexedHttpServletServerFactory implements HttpServletServerFactory { + + private static final String SPACES_COMMA_SPACES = "\\s*,\\s*"; + + /** + * logger + */ + protected static Logger logger = LoggerFactory.getLogger(IndexedHttpServletServerFactory.class); + + /** + * servers index + */ + protected HashMap<Integer, HttpServletServer> servers = new HashMap<>(); + + @Override + public synchronized HttpServletServer build(String name, String host, int port, String contextPath, boolean swagger, + boolean managed) { + + if (servers.containsKey(port)) { + return servers.get(port); + } + + JettyJerseyServer server = new JettyJerseyServer(name, host, port, contextPath, swagger); + if (managed) { + servers.put(port, server); + } + + return server; + } + + @Override + public synchronized List<HttpServletServer> build(Properties properties) { + + ArrayList<HttpServletServer> serviceList = new ArrayList<>(); + + String serviceNames = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES); + if (serviceNames == null || serviceNames.isEmpty()) { + logger.warn("No topic for HTTP Service: {}", properties); + return serviceList; + } + + List<String> serviceNameList = Arrays.asList(serviceNames.split(SPACES_COMMA_SPACES)); + + for (String serviceName : serviceNameList) { + String servicePortString = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + + "." + serviceName + PolicyEndPointProperties.PROPERTY_HTTP_PORT_SUFFIX); + + int servicePort; + try { + if (servicePortString == null || servicePortString.isEmpty()) { + if (logger.isWarnEnabled()) { + logger.warn("No HTTP port for service in {}", serviceName); + } + continue; + } + servicePort = Integer.parseInt(servicePortString); + } catch (NumberFormatException nfe) { + if (logger.isWarnEnabled()) { + logger.warn("No HTTP port for service in {}", serviceName); + } + continue; + } + + String hostName = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "." + + serviceName + PolicyEndPointProperties.PROPERTY_HTTP_HOST_SUFFIX); + + String contextUriPath = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "." + + serviceName + PolicyEndPointProperties.PROPERTY_HTTP_CONTEXT_URIPATH_SUFFIX); + + String userName = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "." + + serviceName + PolicyEndPointProperties.PROPERTY_HTTP_AUTH_USERNAME_SUFFIX); + + String password = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "." + + serviceName + PolicyEndPointProperties.PROPERTY_HTTP_AUTH_PASSWORD_SUFFIX); + + String authUriPath = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "." + + serviceName + PolicyEndPointProperties.PROPERTY_HTTP_AUTH_URIPATH_SUFFIX); + + String restClasses = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "." + + serviceName + PolicyEndPointProperties.PROPERTY_HTTP_REST_CLASSES_SUFFIX); + + String restPackages = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "." + + serviceName + PolicyEndPointProperties.PROPERTY_HTTP_REST_PACKAGES_SUFFIX); + String restUriPath = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "." + + serviceName + PolicyEndPointProperties.PROPERTY_HTTP_REST_URIPATH_SUFFIX); + + String managedString = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "." + + serviceName + PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX); + boolean managed = true; + if (managedString != null && !managedString.isEmpty()) { + managed = Boolean.parseBoolean(managedString); + } + + String swaggerString = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "." + + serviceName + PolicyEndPointProperties.PROPERTY_HTTP_SWAGGER_SUFFIX); + boolean swagger = false; + if (swaggerString != null && !swaggerString.isEmpty()) { + swagger = Boolean.parseBoolean(swaggerString); + } + + HttpServletServer service = build(serviceName, hostName, servicePort, contextUriPath, swagger, managed); + if (userName != null && !userName.isEmpty() && password != null && !password.isEmpty()) { + service.setBasicAuthentication(userName, password, authUriPath); + } + + if (restClasses != null && !restClasses.isEmpty()) { + List<String> restClassesList = Arrays.asList(restClasses.split(SPACES_COMMA_SPACES)); + for (String restClass : restClassesList) { + service.addServletClass(restUriPath, restClass); + } + } + + if (restPackages != null && !restPackages.isEmpty()) { + List<String> restPackageList = Arrays.asList(restPackages.split(SPACES_COMMA_SPACES)); + for (String restPackage : restPackageList) { + service.addServletPackage(restUriPath, restPackage); + } + } + + serviceList.add(service); + } + + return serviceList; + } + + @Override + public synchronized HttpServletServer get(int port) { + + if (servers.containsKey(port)) { + return servers.get(port); + } + + throw new IllegalArgumentException("Http Server for " + port + " not found"); + } + + @Override + public synchronized List<HttpServletServer> inventory() { + return new ArrayList<>(this.servers.values()); + } + + @Override + public synchronized void destroy(int port) { + + if (!servers.containsKey(port)) { + return; + } + + HttpServletServer server = servers.remove(port); + server.shutdown(); + } + + @Override + public synchronized void destroy() { + List<HttpServletServer> httpServletServers = this.inventory(); + for (HttpServletServer server : httpServletServers) { + server.shutdown(); + } + + synchronized (this) { + this.servers.clear(); + } + } + +} diff --git a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/server/impl/IndexedHttpServletServerFactory.java b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/server/impl/IndexedHttpServletServerFactory.java deleted file mode 100644 index 9723d808..00000000 --- a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/server/impl/IndexedHttpServletServerFactory.java +++ /dev/null @@ -1,215 +0,0 @@ -/* - * ============LICENSE_START======================================================= - * policy-endpoints - * ================================================================================ - * Copyright (C) 2017-2018 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. - * ============LICENSE_END========================================================= - */ - -package org.onap.policy.common.endpoints.http.server.impl; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.HashMap; -import java.util.List; -import java.util.Properties; - -import org.onap.policy.common.endpoints.http.server.HttpServletServer; -import org.onap.policy.common.endpoints.http.server.HttpServletServerFactory; -import org.onap.policy.common.endpoints.http.server.internal.JettyJerseyServer; -import org.onap.policy.common.endpoints.properties.PolicyEndPointProperties; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * Indexed factory implementation - */ -public class IndexedHttpServletServerFactory implements HttpServletServerFactory { - - private static final HttpServletServerFactory instance = new IndexedHttpServletServerFactory(); - - /** - * Get the singleton instance. - * - * @return the instance - */ - public static HttpServletServerFactory getInstance() { - return instance; - } - - private IndexedHttpServletServerFactory() {} - - private static final String SPACES_COMMA_SPACES = "\\s*,\\s*"; - - /** - * logger - */ - protected static Logger logger = LoggerFactory.getLogger(IndexedHttpServletServerFactory.class); - - /** - * servers index - */ - protected HashMap<Integer, HttpServletServer> servers = new HashMap<>(); - - @Override - public synchronized HttpServletServer build(String name, String host, int port, String contextPath, boolean swagger, - boolean managed) { - - if (servers.containsKey(port)) { - return servers.get(port); - } - - JettyJerseyServer server = new JettyJerseyServer(name, host, port, contextPath, swagger); - if (managed) { - servers.put(port, server); - } - - return server; - } - - @Override - public synchronized List<HttpServletServer> build(Properties properties) { - - ArrayList<HttpServletServer> serviceList = new ArrayList<>(); - - String serviceNames = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES); - if (serviceNames == null || serviceNames.isEmpty()) { - logger.warn("No topic for HTTP Service: {}", properties); - return serviceList; - } - - List<String> serviceNameList = Arrays.asList(serviceNames.split(SPACES_COMMA_SPACES)); - - for (String serviceName : serviceNameList) { - String servicePortString = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "." - + serviceName + PolicyEndPointProperties.PROPERTY_HTTP_PORT_SUFFIX); - - int servicePort; - try { - if (servicePortString == null || servicePortString.isEmpty()) { - if (logger.isWarnEnabled()) { - logger.warn("No HTTP port for service in {}", serviceName); - } - continue; - } - servicePort = Integer.parseInt(servicePortString); - } catch (NumberFormatException nfe) { - if (logger.isWarnEnabled()) { - logger.warn("No HTTP port for service in {}", serviceName); - } - continue; - } - - String hostName = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "." + serviceName - + PolicyEndPointProperties.PROPERTY_HTTP_HOST_SUFFIX); - - String contextUriPath = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "." - + serviceName + PolicyEndPointProperties.PROPERTY_HTTP_CONTEXT_URIPATH_SUFFIX); - - String userName = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "." + serviceName - + PolicyEndPointProperties.PROPERTY_HTTP_AUTH_USERNAME_SUFFIX); - - String password = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "." + serviceName - + PolicyEndPointProperties.PROPERTY_HTTP_AUTH_PASSWORD_SUFFIX); - - String authUriPath = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "." - + serviceName + PolicyEndPointProperties.PROPERTY_HTTP_AUTH_URIPATH_SUFFIX); - - String restClasses = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "." - + serviceName + PolicyEndPointProperties.PROPERTY_HTTP_REST_CLASSES_SUFFIX); - - String restPackages = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "." - + serviceName + PolicyEndPointProperties.PROPERTY_HTTP_REST_PACKAGES_SUFFIX); - String restUriPath = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "." - + serviceName + PolicyEndPointProperties.PROPERTY_HTTP_REST_URIPATH_SUFFIX); - - String managedString = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "." - + serviceName + PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX); - boolean managed = true; - if (managedString != null && !managedString.isEmpty()) { - managed = Boolean.parseBoolean(managedString); - } - - String swaggerString = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "." - + serviceName + PolicyEndPointProperties.PROPERTY_HTTP_SWAGGER_SUFFIX); - boolean swagger = false; - if (swaggerString != null && !swaggerString.isEmpty()) { - swagger = Boolean.parseBoolean(swaggerString); - } - - HttpServletServer service = build(serviceName, hostName, servicePort, contextUriPath, swagger, managed); - if (userName != null && !userName.isEmpty() && password != null && !password.isEmpty()) { - service.setBasicAuthentication(userName, password, authUriPath); - } - - if (restClasses != null && !restClasses.isEmpty()) { - List<String> restClassesList = Arrays.asList(restClasses.split(SPACES_COMMA_SPACES)); - for (String restClass : restClassesList) { - service.addServletClass(restUriPath, restClass); - } - } - - if (restPackages != null && !restPackages.isEmpty()) { - List<String> restPackageList = Arrays.asList(restPackages.split(SPACES_COMMA_SPACES)); - for (String restPackage : restPackageList) { - service.addServletPackage(restUriPath, restPackage); - } - } - - serviceList.add(service); - } - - return serviceList; - } - - @Override - public synchronized HttpServletServer get(int port) { - - if (servers.containsKey(port)) { - return servers.get(port); - } - - throw new IllegalArgumentException("Http Server for " + port + " not found"); - } - - @Override - public synchronized List<HttpServletServer> inventory() { - return new ArrayList<>(this.servers.values()); - } - - @Override - public synchronized void destroy(int port) { - - if (!servers.containsKey(port)) { - return; - } - - HttpServletServer server = servers.remove(port); - server.shutdown(); - } - - @Override - public synchronized void destroy() { - List<HttpServletServer> httpServletServers = this.inventory(); - for (HttpServletServer server : httpServletServers) { - server.shutdown(); - } - - synchronized (this) { - this.servers.clear(); - } - } - -} diff --git a/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/HttpClientTest.java b/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/HttpClientTest.java index d4840e68..08399e91 100644 --- a/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/HttpClientTest.java +++ b/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/HttpClientTest.java @@ -32,9 +32,7 @@ import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.Test; import org.onap.policy.common.endpoints.http.client.HttpClient; -import org.onap.policy.common.endpoints.http.client.impl.IndexedHttpClientFactory; import org.onap.policy.common.endpoints.http.server.HttpServletServer; -import org.onap.policy.common.endpoints.http.server.impl.IndexedHttpServletServerFactory; import org.onap.policy.common.endpoints.properties.PolicyEndPointProperties; import org.onap.policy.common.utils.network.NetworkUtil; import org.slf4j.Logger; @@ -51,7 +49,7 @@ public class HttpClientTest { /* echo server */ final HttpServletServer echoServerNoAuth = - IndexedHttpServletServerFactory.getInstance().build("echo", "localhost", 6666, "/", false, true); + HttpServletServer.factory.build("echo", "localhost", 6666, "/", false, true); echoServerNoAuth.addServletPackage("/*", HttpClientTest.class.getPackage().getName()); echoServerNoAuth.waitedStart(5000); @@ -62,7 +60,7 @@ public class HttpClientTest { /* no auth echo server */ final HttpServletServer echoServerAuth = - IndexedHttpServletServerFactory.getInstance().build("echo", "localhost", 6667, "/", false, true); + HttpServletServer.factory.build("echo", "localhost", 6667, "/", false, true); echoServerAuth.setBasicAuthentication("x", "y", null); echoServerAuth.addServletPackage("/*", HttpClientTest.class.getPackage().getName()); echoServerAuth.waitedStart(5000); @@ -76,16 +74,16 @@ public class HttpClientTest { public static void tearDown() { logger.info("-- tearDown() --"); - IndexedHttpServletServerFactory.getInstance().destroy(); - IndexedHttpClientFactory.getInstance().destroy(); + HttpServletServer.factory.destroy(); + HttpClient.factory.destroy(); } @Test public void testHttpNoAuthClient() throws Exception { logger.info("-- testHttpNoAuthClient() --"); - final HttpClient client = IndexedHttpClientFactory.getInstance().build("testHttpNoAuthClient", false, false, - "localhost", 6666, "junit/echo", null, null, true); + final HttpClient client = HttpClient.factory.build("testHttpNoAuthClient", false, false, "localhost", 6666, + "junit/echo", null, null, true); final Response response = client.get("hello"); final String body = HttpClient.getBody(response, String.class); @@ -97,8 +95,8 @@ public class HttpClientTest { public void testHttpAuthClient() throws Exception { logger.info("-- testHttpAuthClient() --"); - final HttpClient client = IndexedHttpClientFactory.getInstance().build("testHttpAuthClient", false, false, - "localhost", 6667, "junit/echo", "x", "y", true); + final HttpClient client = HttpClient.factory.build("testHttpAuthClient", false, false, "localhost", 6667, + "junit/echo", "x", "y", true); final Response response = client.get("hello"); final String body = HttpClient.getBody(response, String.class); @@ -110,8 +108,8 @@ public class HttpClientTest { public void testHttpAuthClient401() throws Exception { logger.info("-- testHttpAuthClient401() --"); - final HttpClient client = IndexedHttpClientFactory.getInstance().build("testHttpAuthClient401", false, false, - "localhost", 6667, "junit/echo", null, null, true); + final HttpClient client = HttpClient.factory.build("testHttpAuthClient401", false, false, "localhost", 6667, + "junit/echo", null, null, true); final Response response = client.get("hello"); assertTrue(response.getStatus() == 401); } @@ -131,11 +129,12 @@ public class HttpClientTest { + PolicyEndPointProperties.PROPERTY_HTTP_AUTH_USERNAME_SUFFIX, "testpap"); httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "." + "PAP" + PolicyEndPointProperties.PROPERTY_HTTP_AUTH_PASSWORD_SUFFIX, "alpha123"); - httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "." + "PAP" - + PolicyEndPointProperties.PROPERTY_HTTP_REST_CLASSES_SUFFIX, RestMockHealthCheck.class.getName()); httpProperties.setProperty( - PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + "." + "PAP" + PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX, - "true"); + PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "." + "PAP" + + PolicyEndPointProperties.PROPERTY_HTTP_REST_CLASSES_SUFFIX, + RestMockHealthCheck.class.getName()); + httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + "." + "PAP" + + PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX, "true"); httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "." + "PDP" + PolicyEndPointProperties.PROPERTY_HTTP_HOST_SUFFIX, "localhost"); @@ -145,11 +144,12 @@ public class HttpClientTest { + PolicyEndPointProperties.PROPERTY_HTTP_AUTH_USERNAME_SUFFIX, "testpdp"); httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "." + "PDP" + PolicyEndPointProperties.PROPERTY_HTTP_AUTH_PASSWORD_SUFFIX, "alpha123"); - httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "." + "PDP" - + PolicyEndPointProperties.PROPERTY_HTTP_REST_CLASSES_SUFFIX, RestMockHealthCheck.class.getName()); httpProperties.setProperty( - PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + "." + "PAP" + PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX, - "true"); + PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "." + "PDP" + + PolicyEndPointProperties.PROPERTY_HTTP_REST_CLASSES_SUFFIX, + RestMockHealthCheck.class.getName()); + httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + "." + "PAP" + + PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX, "true"); httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES, "PAP,PDP"); httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + "." + "PAP" @@ -164,9 +164,8 @@ public class HttpClientTest { + PolicyEndPointProperties.PROPERTY_HTTP_AUTH_USERNAME_SUFFIX, "testpap"); httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + "." + "PAP" + PolicyEndPointProperties.PROPERTY_HTTP_AUTH_PASSWORD_SUFFIX, "alpha123"); - httpProperties.setProperty( - PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + "." + "PAP" + PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX, - "true"); + httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + "." + "PAP" + + PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX, "true"); httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + "." + "PDP" + PolicyEndPointProperties.PROPERTY_HTTP_HOST_SUFFIX, "localhost"); @@ -180,25 +179,24 @@ public class HttpClientTest { + PolicyEndPointProperties.PROPERTY_HTTP_AUTH_USERNAME_SUFFIX, "testpdp"); httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + "." + "PDP" + PolicyEndPointProperties.PROPERTY_HTTP_AUTH_PASSWORD_SUFFIX, "alpha123"); - httpProperties.setProperty( - PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + "." + "PDP" + PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX, - "true"); + httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + "." + "PDP" + + PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX, "true"); - final List<HttpServletServer> servers = IndexedHttpServletServerFactory.getInstance().build(httpProperties); + final List<HttpServletServer> servers = HttpServletServer.factory.build(httpProperties); assertTrue(servers.size() == 2); - final List<HttpClient> clients = IndexedHttpClientFactory.getInstance().build(httpProperties); + final List<HttpClient> clients = HttpClient.factory.build(httpProperties); assertTrue(clients.size() == 2); for (final HttpServletServer server : servers) { server.waitedStart(10000); } - final HttpClient clientPAP = IndexedHttpClientFactory.getInstance().get("PAP"); + final HttpClient clientPAP = HttpClient.factory.get("PAP"); final Response response = clientPAP.get(); assertTrue(response.getStatus() == 200); - final HttpClient clientPDP = IndexedHttpClientFactory.getInstance().get("PDP"); + final HttpClient clientPDP = HttpClient.factory.get("PDP"); final Response response2 = clientPDP.get("test"); assertTrue(response2.getStatus() == 500); } diff --git a/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/HttpServerTest.java b/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/HttpServerTest.java index eba96208..b6f0c0e8 100644 --- a/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/HttpServerTest.java +++ b/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/HttpServerTest.java @@ -32,8 +32,6 @@ import java.util.UUID; import org.junit.Test; import org.onap.policy.common.endpoints.http.server.HttpServletServer; -import org.onap.policy.common.endpoints.http.server.HttpServletServerFactory; -import org.onap.policy.common.endpoints.http.server.impl.IndexedHttpServletServerFactory; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -42,8 +40,6 @@ import org.slf4j.LoggerFactory; */ public class HttpServerTest { - HttpServletServerFactory httpServletServerFactory = IndexedHttpServletServerFactory.getInstance(); - /** * Logger */ @@ -53,64 +49,64 @@ public class HttpServerTest { public void testSingleServer() throws Exception { logger.info("-- testSingleServer() --"); - HttpServletServer server = httpServletServerFactory.build("echo", "localhost", 5678, "/", false, true); + HttpServletServer server = HttpServletServer.factory.build("echo", "localhost", 5678, "/", false, true); server.addServletPackage("/*", this.getClass().getPackage().getName()); server.waitedStart(5000); - assertTrue(httpServletServerFactory.get(5678).isAlive()); + assertTrue(HttpServletServer.factory.get(5678).isAlive()); - String response = http(httpServletServerFactory.get(5678), "http://localhost:5678/junit/echo/hello"); + String response = http(HttpServletServer.factory.get(5678), "http://localhost:5678/junit/echo/hello"); assertTrue("hello".equals(response)); response = null; try { - response = http(httpServletServerFactory.get(5678), "http://localhost:5678/swagger.json"); + response = http(HttpServletServer.factory.get(5678), "http://localhost:5678/swagger.json"); } catch (IOException e) { // Expected } assertTrue(response == null); - assertTrue(httpServletServerFactory.get(5678).isAlive()); - assertTrue(httpServletServerFactory.inventory().size() == 1); + assertTrue(HttpServletServer.factory.get(5678).isAlive()); + assertTrue(HttpServletServer.factory.inventory().size() == 1); - httpServletServerFactory.destroy(5678); - assertTrue(httpServletServerFactory.inventory().size() == 0); + HttpServletServer.factory.destroy(5678); + assertTrue(HttpServletServer.factory.inventory().size() == 0); } @Test public void testMultipleServers() throws Exception { logger.info("-- testMultipleServers() --"); - HttpServletServer server1 = httpServletServerFactory.build("echo-1", "localhost", 5688, "/", true, true); + HttpServletServer server1 = HttpServletServer.factory.build("echo-1", "localhost", 5688, "/", true, true); server1.addServletPackage("/*", this.getClass().getPackage().getName()); server1.waitedStart(5000); - HttpServletServer server2 = httpServletServerFactory.build("echo-2", "localhost", 5689, "/", false, true); + HttpServletServer server2 = HttpServletServer.factory.build("echo-2", "localhost", 5689, "/", false, true); server2.addServletPackage("/*", this.getClass().getPackage().getName()); server2.waitedStart(5000); - assertTrue(httpServletServerFactory.get(5688).isAlive()); - assertTrue(httpServletServerFactory.get(5689).isAlive()); + assertTrue(HttpServletServer.factory.get(5688).isAlive()); + assertTrue(HttpServletServer.factory.get(5689).isAlive()); - String response = http(httpServletServerFactory.get(5688), "http://localhost:5688/junit/echo/hello"); + String response = http(HttpServletServer.factory.get(5688), "http://localhost:5688/junit/echo/hello"); assertTrue("hello".equals(response)); - response = http(httpServletServerFactory.get(5688), "http://localhost:5688/swagger.json"); + response = http(HttpServletServer.factory.get(5688), "http://localhost:5688/swagger.json"); assertTrue(response != null); - response = http(httpServletServerFactory.get(5689), "http://localhost:5689/junit/echo/hello"); + response = http(HttpServletServer.factory.get(5689), "http://localhost:5689/junit/echo/hello"); assertTrue("hello".equals(response)); response = null; try { - response = http(httpServletServerFactory.get(5689), "http://localhost:5689/swagger.json"); + response = http(HttpServletServer.factory.get(5689), "http://localhost:5689/swagger.json"); } catch (IOException e) { // Expected } assertTrue(response == null); - httpServletServerFactory.destroy(); - assertTrue(httpServletServerFactory.inventory().size() == 0); + HttpServletServer.factory.destroy(); + assertTrue(HttpServletServer.factory.inventory().size() == 0); } @Test @@ -119,20 +115,20 @@ public class HttpServerTest { String randomName = UUID.randomUUID().toString(); - HttpServletServer server = httpServletServerFactory.build(randomName, "localhost", 5668, "/", false, true); + HttpServletServer server = HttpServletServer.factory.build(randomName, "localhost", 5668, "/", false, true); server.addServletPackage("/*", this.getClass().getPackage().getName()); server.waitedStart(5000); - assertTrue(httpServletServerFactory.get(5668).isAlive()); + assertTrue(HttpServletServer.factory.get(5668).isAlive()); - String response = http(httpServletServerFactory.get(5668), "http://localhost:5668/junit/echo/hello"); + String response = http(HttpServletServer.factory.get(5668), "http://localhost:5668/junit/echo/hello"); assertTrue("hello".equals(response)); - response = http(httpServletServerFactory.get(5668), "http://localhost:5668/junit/endpoints/http/servers"); + response = http(HttpServletServer.factory.get(5668), "http://localhost:5668/junit/endpoints/http/servers"); assertTrue(response.contains(randomName)); - httpServletServerFactory.destroy(); - assertTrue(httpServletServerFactory.inventory().size() == 0); + HttpServletServer.factory.destroy(); + assertTrue(HttpServletServer.factory.inventory().size() == 0); } @Test @@ -140,17 +136,17 @@ public class HttpServerTest { logger.info("-- testServiceClass() --"); String randomName = UUID.randomUUID().toString(); - HttpServletServer server = httpServletServerFactory.build(randomName, "localhost", 5658, "/", false, true); + HttpServletServer server = HttpServletServer.factory.build(randomName, "localhost", 5658, "/", false, true); server.addServletClass("/*", RestEchoService.class.getCanonicalName()); server.waitedStart(5000); - assertTrue(httpServletServerFactory.get(5658).isAlive()); + assertTrue(HttpServletServer.factory.get(5658).isAlive()); - String response = http(httpServletServerFactory.get(5658), "http://localhost:5658/junit/echo/hello"); + String response = http(HttpServletServer.factory.get(5658), "http://localhost:5658/junit/echo/hello"); assertTrue("hello".equals(response)); - httpServletServerFactory.destroy(); - assertTrue(httpServletServerFactory.inventory().size() == 0); + HttpServletServer.factory.destroy(); + assertTrue(HttpServletServer.factory.inventory().size() == 0); } @Test @@ -159,21 +155,21 @@ public class HttpServerTest { String randomName = UUID.randomUUID().toString(); - HttpServletServer server = httpServletServerFactory.build(randomName, "localhost", 5648, "/", false, true); + HttpServletServer server = HttpServletServer.factory.build(randomName, "localhost", 5648, "/", false, true); server.addServletClass("/*", RestEchoService.class.getCanonicalName()); server.addServletClass("/*", RestEndpoints.class.getCanonicalName()); server.waitedStart(5000); - assertTrue(httpServletServerFactory.get(5648).isAlive()); + assertTrue(HttpServletServer.factory.get(5648).isAlive()); - String response = http(httpServletServerFactory.get(5648), "http://localhost:5648/junit/echo/hello"); + String response = http(HttpServletServer.factory.get(5648), "http://localhost:5648/junit/echo/hello"); assertTrue("hello".equals(response)); - response = http(httpServletServerFactory.get(5648), "http://localhost:5648/junit/endpoints/http/servers"); + response = http(HttpServletServer.factory.get(5648), "http://localhost:5648/junit/endpoints/http/servers"); assertTrue(response.contains(randomName)); - httpServletServerFactory.destroy(); - assertTrue(httpServletServerFactory.inventory().size() == 0); + HttpServletServer.factory.destroy(); + assertTrue(HttpServletServer.factory.inventory().size() == 0); } /** diff --git a/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/NoopTopicTest.java b/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/NoopTopicTest.java index 1f8f56ec..849065c1 100644 --- a/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/NoopTopicTest.java +++ b/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/NoopTopicTest.java @@ -27,11 +27,10 @@ import java.util.Properties; import org.junit.Test; import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure; +import org.onap.policy.common.endpoints.event.comm.TopicEndpoint; import org.onap.policy.common.endpoints.event.comm.TopicListener; import org.onap.policy.common.endpoints.event.comm.TopicSink; -import org.onap.policy.common.endpoints.event.comm.bus.NoopTopicSinkFactory; -import org.onap.policy.common.endpoints.event.comm.bus.impl.IndexedNoopTopicSinkFactory; -import org.onap.policy.common.endpoints.event.comm.impl.ProxyTopicEndpointManager; +import org.onap.policy.common.endpoints.event.comm.bus.NoopTopicSink; import org.onap.policy.common.endpoints.properties.PolicyEndPointProperties; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -57,17 +56,14 @@ public class NoopTopicTest implements TopicListener { Properties noopSinkProperties = new Properties(); noopSinkProperties.put(PolicyEndPointProperties.PROPERTY_NOOP_SINK_TOPICS, topicName); - List<? extends TopicSink> noopTopics = - ProxyTopicEndpointManager.getInstance().addTopicSinks(noopSinkProperties); - - NoopTopicSinkFactory noopTopicSinkFactory = IndexedNoopTopicSinkFactory.getInstance(); - TopicSink sink = noopTopicSinkFactory.get(topicName); + List<? extends TopicSink> noopTopics = TopicEndpoint.manager.addTopicSinks(noopSinkProperties); + TopicSink sink = NoopTopicSink.factory.get(topicName); assertTrue(noopTopics.size() == 1); - assertTrue(noopTopics.size() == noopTopicSinkFactory.inventory().size()); + assertTrue(noopTopics.size() == NoopTopicSink.factory.inventory().size()); assertTrue(noopTopics.get(0) == sink); - assertTrue(sink == noopTopicSinkFactory.inventory().get(0)); + assertTrue(sink == NoopTopicSink.factory.inventory().get(0)); assertTrue(!sink.isAlive()); @@ -104,8 +100,8 @@ public class NoopTopicTest implements TopicListener { } assertTrue(badState); - noopTopicSinkFactory.destroy(topicName); - assertTrue(noopTopicSinkFactory.inventory().size() == 0); + NoopTopicSink.factory.destroy(topicName); + assertTrue(NoopTopicSink.factory.inventory().size() == 0); } @Override diff --git a/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/RestEndpoints.java b/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/RestEndpoints.java index 4733c119..9800a9d4 100644 --- a/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/RestEndpoints.java +++ b/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/RestEndpoints.java @@ -28,7 +28,6 @@ import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; import org.onap.policy.common.endpoints.http.server.HttpServletServer; -import org.onap.policy.common.endpoints.http.server.impl.IndexedHttpServletServerFactory; @Path("/junit/endpoints") public class RestEndpoints { @@ -36,10 +35,11 @@ public class RestEndpoints { @GET @Path("http/servers") @Produces(MediaType.TEXT_PLAIN) - public String httpServers() { - List<HttpServletServer> servers = IndexedHttpServletServerFactory.getInstance().inventory(); - return servers.toString(); + public String httpServers() { + List<HttpServletServer> servers = + HttpServletServer.factory.inventory(); + return servers.toString(); } - - + + } @@ -23,10 +23,10 @@ <modelVersion>4.0.0</modelVersion> <parent> - <groupId>org.onap.oparent</groupId> - <artifactId>oparent</artifactId> - <version>1.1.0</version> - <relativePath /> + <groupId>org.onap.policy.parent</groupId> + <artifactId>integration</artifactId> + <version>2.0.0-SNAPSHOT</version> + <relativePath/> </parent> <groupId>org.onap.policy.common</groupId> |