From e86be39dc5ff812b73398e0720aa3fbf0c48213c Mon Sep 17 00:00:00 2001 From: an4828 Date: Mon, 21 Aug 2017 11:05:08 -0400 Subject: Initial TCA commit into DCAEGEN2 Change-Id: I5f7f8af2a00419854cafc34b79277df60d1af095 Issue-ID: DCAEGEN2-53 Signed-off-by: an4828 --- .../apod/analytics/common/AnalyticsConstants.java | 148 +++++++++++++++++++ .../exception/DCAEAnalyticsRuntimeException.java | 54 +++++++ .../exception/MessageProcessingException.java | 51 +++++++ .../service/filter/GenericJsonMessageFilter.java | 112 ++++++++++++++ .../filter/JsonMessageFilterProcessorContext.java | 57 ++++++++ .../processor/AbstractMessageProcessor.java | 162 +++++++++++++++++++++ .../processor/AbstractProcessorContext.java | 96 ++++++++++++ .../processor/GenericMessageChainProcessor.java | 70 +++++++++ .../service/processor/GenericProcessorInfo.java | 57 ++++++++ .../common/service/processor/MessageProcessor.java | 84 +++++++++++ .../common/service/processor/ProcessingState.java | 36 +++++ .../common/service/processor/ProcessorContext.java | 70 +++++++++ .../common/service/processor/ProcessorInfo.java | 49 +++++++ .../apod/analytics/common/utils/HTTPUtils.java | 62 ++++++++ .../common/utils/MessageProcessorUtils.java | 162 +++++++++++++++++++++ .../analytics/common/utils/PersistenceUtils.java | 59 ++++++++ .../analytics/common/validation/DCAEValidator.java | 31 ++++ .../validation/GenericValidationResponse.java | 84 +++++++++++ .../common/validation/ValidationResponse.java | 90 ++++++++++++ 19 files changed, 1534 insertions(+) create mode 100644 dcae-analytics-common/src/main/java/org/openecomp/dcae/apod/analytics/common/AnalyticsConstants.java create mode 100644 dcae-analytics-common/src/main/java/org/openecomp/dcae/apod/analytics/common/exception/DCAEAnalyticsRuntimeException.java create mode 100644 dcae-analytics-common/src/main/java/org/openecomp/dcae/apod/analytics/common/exception/MessageProcessingException.java create mode 100644 dcae-analytics-common/src/main/java/org/openecomp/dcae/apod/analytics/common/service/filter/GenericJsonMessageFilter.java create mode 100644 dcae-analytics-common/src/main/java/org/openecomp/dcae/apod/analytics/common/service/filter/JsonMessageFilterProcessorContext.java create mode 100644 dcae-analytics-common/src/main/java/org/openecomp/dcae/apod/analytics/common/service/processor/AbstractMessageProcessor.java create mode 100644 dcae-analytics-common/src/main/java/org/openecomp/dcae/apod/analytics/common/service/processor/AbstractProcessorContext.java create mode 100644 dcae-analytics-common/src/main/java/org/openecomp/dcae/apod/analytics/common/service/processor/GenericMessageChainProcessor.java create mode 100644 dcae-analytics-common/src/main/java/org/openecomp/dcae/apod/analytics/common/service/processor/GenericProcessorInfo.java create mode 100644 dcae-analytics-common/src/main/java/org/openecomp/dcae/apod/analytics/common/service/processor/MessageProcessor.java create mode 100644 dcae-analytics-common/src/main/java/org/openecomp/dcae/apod/analytics/common/service/processor/ProcessingState.java create mode 100644 dcae-analytics-common/src/main/java/org/openecomp/dcae/apod/analytics/common/service/processor/ProcessorContext.java create mode 100644 dcae-analytics-common/src/main/java/org/openecomp/dcae/apod/analytics/common/service/processor/ProcessorInfo.java create mode 100644 dcae-analytics-common/src/main/java/org/openecomp/dcae/apod/analytics/common/utils/HTTPUtils.java create mode 100644 dcae-analytics-common/src/main/java/org/openecomp/dcae/apod/analytics/common/utils/MessageProcessorUtils.java create mode 100644 dcae-analytics-common/src/main/java/org/openecomp/dcae/apod/analytics/common/utils/PersistenceUtils.java create mode 100644 dcae-analytics-common/src/main/java/org/openecomp/dcae/apod/analytics/common/validation/DCAEValidator.java create mode 100644 dcae-analytics-common/src/main/java/org/openecomp/dcae/apod/analytics/common/validation/GenericValidationResponse.java create mode 100644 dcae-analytics-common/src/main/java/org/openecomp/dcae/apod/analytics/common/validation/ValidationResponse.java (limited to 'dcae-analytics-common/src/main/java/org') diff --git a/dcae-analytics-common/src/main/java/org/openecomp/dcae/apod/analytics/common/AnalyticsConstants.java b/dcae-analytics-common/src/main/java/org/openecomp/dcae/apod/analytics/common/AnalyticsConstants.java new file mode 100644 index 0000000..ec6a9b5 --- /dev/null +++ b/dcae-analytics-common/src/main/java/org/openecomp/dcae/apod/analytics/common/AnalyticsConstants.java @@ -0,0 +1,148 @@ +/* + * ===============================LICENSE_START====================================== + * dcae-analytics + * ================================================================================ + * Copyright © 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.openecomp.dcae.apod.analytics.common; + +/** + * Contains static variable for all DCAE Components. + * + * @author Rajiv Singla . Creation Date: 10/21/2016. + */ +public abstract class AnalyticsConstants { + + // =============== Common Constants for all DCAE Analytics Modules ==================== // + + + // =============== DMaaP Constants for all DCAE Analytics Modules ==================== // + + // DMaaP Config Constants + public static final Integer DEFAULT_PORT_NUMBER = 80; // default port number + public static final String DEFAULT_USER_NAME = null; // default to no username + public static final String DEFAULT_USER_PASSWORD = null; // defaults to no userPassword + public static final String DEFAULT_PROTOCOL = "https"; // defaults to using https protocol + public static final String DEFAULT_CONTENT_TYPE = "application/json"; // defaults to json content type + + public static final String DMAAP_URI_PATH_PREFIX = "/events/"; + public static final String DMAAP_GROUP_PREFIX = "OpenDCAE-"; + + // ================== DMaaP MR Constants ============================== // + // Publisher Constants + public static final int DEFAULT_PUBLISHER_MAX_BATCH_SIZE = 1; // disable batching by default + public static final int DEFAULT_PUBLISHER_MAX_RECOVERY_QUEUE_SIZE = 100000; // default recovery messages size + public static final int PUBLISHER_MAX_FLUSH_RETRIES_ON_CLOSE = 5; // number of retries when flushing messages + public static final int PUBLISHER_DELAY_MS_ON_RETRIES_ON_CLOSE = 5000; // delay in retrying for flushing messages + // Subscriber Constants + public static final int DEFAULT_SUBSCRIBER_TIMEOUT_MS = -1; + public static final int DEFAULT_SUBSCRIBER_MESSAGE_LIMIT = -1; + public static final String DEFAULT_SUBSCRIBER_GROUP_PREFIX = DMAAP_GROUP_PREFIX + "DMaaPSub-"; + public static final String SUBSCRIBER_TIMEOUT_QUERY_PARAM_NAME = "timeout"; + public static final String SUBSCRIBER_MSG_LIMIT_QUERY_PARAM_NAME = "limit"; + + + // ================== TCA Constants ============================== // + + // Default subscriber polling interval + public static final Integer TCA_DEFAULT_SUBSCRIBER_POLLING_INTERVAL_MS = 30000; + + // Default publisher polling interval + public static final Integer TCA_DEFAULT_PUBLISHER_POLLING_INTERVAL_MS = 30000; + + // Default publisher max batch queue size determines the minimum number of messages that need to be published in + // batch mode + public static final Integer TCA_DEFAULT_PUBLISHER_MAX_BATCH_QUEUE_SIZE = 10; + + // Default publisher max recovery queue size determines max number of messages can be cached in memory + // in case publisher is not responding + public static final Integer TCA_DEFAULT_PUBLISHER_MAX_RECOVERY_QUEUE_SIZE = 100000; + + // Default interval during which TCA DMaaP Worker checks if scheduler is shut down + public static final Integer TCA_DEFAULT_WORKER_SHUTDOWN_CHECK_INTERVAL_MS = 5000; + + // ***** TCA Quartz Scheduler Settings ******// + + public static final String TCA_QUARTZ_SUBSCRIBER_PROPERTIES_FILE_NAME = "quartz-subscriber.properties"; + + public static final String TCA_QUARTZ_PUBLISHER_PROPERTIES_FILE_NAME = "quartz-publisher.properties"; + + // TCA Quartz Group Settings + public static final String TCA_QUARTZ_GROUP_NAME = "TCAQuartzGroup"; + // TCA Quartz Trigger Settings + public static final String TCA_DMAAP_SUBSCRIBER_QUARTZ_TRIGGER_NAME = "TCADMaaPSubscriberTrigger"; + public static final String TCA_DMAAP_PUBLISHER_QUARTZ_TRIGGER_NAME = "TCADMaaPPublisherTrigger"; + + // TCA Quartz DMaaP Subscriber Job Settings + public static final String TCA_DMAAP_SUBSCRIBER_QUARTZ_JOB_NAME = "TCADMaaPSubscriberJob"; + public static final String TCA_DMAAP_PUBLISHER_QUARTZ_JOB_NAME = "TCADMaaPPublisherJob"; + + // TCA Quartz Publisher and Subscriber Job Parameters + // Common Job parameters for both Publisher and Subscriber + public static final String WORKER_CONTEXT_VARIABLE_NAME = "WORKER_CONTEXT"; + public static final String DMAAP_METRICS_VARIABLE_NAME = "DMAAP_METRICS"; + // TCA Quartz DMaaP Subscriber Job Parameter Settings + public static final String CDAP_STREAM_VARIABLE_NAME = "CDAP_STREAM_NAME"; + public static final String DMAAP_SUBSCRIBER_VARIABLE_NAME = "DMAAP_SUBSCRIBER"; + // TCA Quartz DMaaP Publisher Job Parameter Settings + public static final String CDAP_ALERTS_TABLE_VARIABLE_NAME = "CDAP_TCA_ALERTS_TABLE_NAME"; + public static final String DMAAP_PUBLISHER_VARIABLE_NAME = "DMAAP_PUBLISHER"; + + // TCA VES Response Constants + public static final String TCA_VES_RESPONSE_TARGET_TYPE = "VNF"; + public static final String TCA_VES_RESPONSE_TARGET = "generic-vnf.vnf-id"; + public static final String TCA_VES_RESPONSE_FROM = "DCAE"; + public static final String TCA_VES_RESPONSE_CLOSED_LOOP_EVENT_STATUS = "ONSET"; + + // TCA VES Response Constants for vLoadBalancer + public static final String LOAD_BALANCER_FUNCTIONAL_ROLE = "vLoadBalancer"; + public static final String LOAD_BALANCER_TCA_VES_RESPONSE_TARGET_TYPE = "VM"; + public static final String LOAD_BALANCER_TCA_VES_RESPONSE_TARGET = "vserver.vserver-name"; + + // TCA VES Message Router Partition Key + public static final String TCA_VES_MESSAGE_ROUTER_PARTITION_KEY = "VESMessageHash"; + + /** + * Default Number of instances for Threshold violation calculator flowlet + */ + public static final Integer TCA_DEFAULT_THRESHOLD_CALCULATOR_FLOWLET_INSTANCES = 2; + + /** + * Default TTL for TCA VES Message status table which contain status of all messages processed by TCA + */ + public static final Integer TCA_DEFAULT_VES_MESSAGE_STATUS_TTL_TABLE = 60 * 60 * 24 * 10; // 10 Days + + /** + * Default TTL for TCA VES Alerts table which contains alerts that can be send to downstream systems + */ + public static final Integer TCA_DEFAULT_VES_ALERTS_TTL_TABLE = 60 * 60 * 24 * 20; // 20 Days + + + // TCA Policy Runtime Argument Paths + public static final String TCA_POLICY_DELIMITER = "."; + public static final String TCA_POLICY_DOMAIN_PATH = "domain"; + public static final String TCA_POLICY_METRICS_PER_FUNCTIONAL_ROLE_PATH = "configuration.metricsPerFunctionalRole"; + public static final String TCA_POLICY_THRESHOLDS_PATH_POSTFIX = "thresholds"; + + public static final String TCA_POLICY_JSON_KEY = "tca_policy"; + public static final String TCA_POLICY_STRING_DELIMITER = "'"; + + private AnalyticsConstants() { + + } + +} diff --git a/dcae-analytics-common/src/main/java/org/openecomp/dcae/apod/analytics/common/exception/DCAEAnalyticsRuntimeException.java b/dcae-analytics-common/src/main/java/org/openecomp/dcae/apod/analytics/common/exception/DCAEAnalyticsRuntimeException.java new file mode 100644 index 0000000..4b515ab --- /dev/null +++ b/dcae-analytics-common/src/main/java/org/openecomp/dcae/apod/analytics/common/exception/DCAEAnalyticsRuntimeException.java @@ -0,0 +1,54 @@ +/* + * ===============================LICENSE_START====================================== + * dcae-analytics + * ================================================================================ + * Copyright © 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.openecomp.dcae.apod.analytics.common.exception; + +import org.slf4j.Logger; + +/** + * Runtime Exception for DCAE Analytics Exceptions. All DCAE Analytics + * runtime Exceptions must be wrapped inside this exception + *

+ * @author Rajiv Singla . Creation Date: 10/5/2016. + */ +public class DCAEAnalyticsRuntimeException extends RuntimeException { + + + /** + * @param message - Error Message for Exception + * @param cause - Actual Exception which caused {@link DCAEAnalyticsRuntimeException} + */ + public DCAEAnalyticsRuntimeException(String message, Throwable cause) { + super(message, cause); + } + + /** + * Creates and logs the DCAE Runtime Exception to given logger + * + * @param message - Error Message for Exception and logging + * @param logger - Logger used for logging exception + * @param cause - Actual exception which caused {@link DCAEAnalyticsRuntimeException} + */ + public DCAEAnalyticsRuntimeException(String message, Logger logger, Throwable cause) { + super(message, cause); + logger.error(message); + } + +} diff --git a/dcae-analytics-common/src/main/java/org/openecomp/dcae/apod/analytics/common/exception/MessageProcessingException.java b/dcae-analytics-common/src/main/java/org/openecomp/dcae/apod/analytics/common/exception/MessageProcessingException.java new file mode 100644 index 0000000..825f94a --- /dev/null +++ b/dcae-analytics-common/src/main/java/org/openecomp/dcae/apod/analytics/common/exception/MessageProcessingException.java @@ -0,0 +1,51 @@ +/* + * ===============================LICENSE_START====================================== + * dcae-analytics + * ================================================================================ + * Copyright © 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.openecomp.dcae.apod.analytics.common.exception; + +import org.openecomp.dcae.apod.analytics.common.service.processor.MessageProcessor; +import org.slf4j.Logger; + +/** + * Runtime Exception caused due to {@link MessageProcessor} failure while processing a message. + * + * @author Rajiv Singla . Creation Date: 11/7/2016. + */ +public class MessageProcessingException extends DCAEAnalyticsRuntimeException { + + /** + * @param message - Error Message for Exception + * @param cause - Actual Exception which caused {@link DCAEAnalyticsRuntimeException} + */ + public MessageProcessingException(String message, Throwable cause) { + super(message, cause); + } + + /** + * Creates and logs the DCAE Runtime Exception to given logger + * + * @param message - Error Message for Exception and logging + * @param logger - Logger used for logging exception + * @param cause - Actual exception which caused {@link DCAEAnalyticsRuntimeException} + */ + public MessageProcessingException(String message, Logger logger, Throwable cause) { + super(message, logger, cause); + } +} diff --git a/dcae-analytics-common/src/main/java/org/openecomp/dcae/apod/analytics/common/service/filter/GenericJsonMessageFilter.java b/dcae-analytics-common/src/main/java/org/openecomp/dcae/apod/analytics/common/service/filter/GenericJsonMessageFilter.java new file mode 100644 index 0000000..8583b9c --- /dev/null +++ b/dcae-analytics-common/src/main/java/org/openecomp/dcae/apod/analytics/common/service/filter/GenericJsonMessageFilter.java @@ -0,0 +1,112 @@ +/* + * ===============================LICENSE_START====================================== + * dcae-analytics + * ================================================================================ + * Copyright © 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.openecomp.dcae.apod.analytics.common.service.filter; + +import com.google.common.collect.ImmutableSet; +import com.jayway.jsonpath.DocumentContext; +import com.jayway.jsonpath.JsonPath; +import com.jayway.jsonpath.PathNotFoundException; +import org.apache.commons.lang3.StringUtils; +import org.openecomp.dcae.apod.analytics.common.service.processor.AbstractMessageProcessor; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.Set; + +/** + * A Generic Json Message Filter which filter the json message based on given json Path and list of expected values + * for that json path. The {@link JsonMessageFilterProcessorContext#isMatched} flag will be changed as per table below: + *

+ *      Incoming message is blank or invalid Json                               =  null
+ *      Incoming message path is matches expected values                        = true
+ *      Incoming message does not match expected values or path does not exist  = false
+ * 
+ *

+ * @author Rajiv Singla . Creation Date: 2/10/2017. + */ +public class GenericJsonMessageFilter extends AbstractMessageProcessor { + + private static final Logger LOG = LoggerFactory.getLogger(GenericJsonMessageFilter.class); + private static final long serialVersionUID = 1L; + + private final String filterName; + private final String jsonPath; + private final Set expectedValues; + + public GenericJsonMessageFilter(final String filterName, final String jsonPath, final Set expectedValues) { + this.filterName = filterName; + this.jsonPath = jsonPath; + this.expectedValues = expectedValues; + } + + public GenericJsonMessageFilter(final String filterName, final String jsonPath, final String expectedValue) { + this(filterName, jsonPath, ImmutableSet.of(expectedValue)); + } + + @Override + public String getProcessorDescription() { + return filterName; + } + + @Override + public JsonMessageFilterProcessorContext processMessage(final JsonMessageFilterProcessorContext processorContext) { + + final String jsonMessage = processorContext.getMessage().trim(); + + if (StringUtils.isNotBlank(jsonMessage) && jsonMessage.startsWith("{") && jsonMessage.endsWith("}")) { + + // locate json path value + final DocumentContext documentContext = JsonPath.parse(jsonMessage); + String jsonPathValue = null; + try { + jsonPathValue = documentContext.read(jsonPath, String.class); + } catch (PathNotFoundException ex) { + LOG.info("Unable to find json Path: {}. Exception: {}, Json Message: {}", jsonPath, ex, jsonMessage); + } + + LOG.debug("Value for jsonPath: {}, jsonPathValue: {}, expected Values: {}", + jsonPath, jsonPathValue, expectedValues); + + // if json path value is null or we json value is not present in expect values then terminate early + if (jsonPathValue == null || !expectedValues.contains(jsonPathValue)) { + final String terminatingMessage = String.format("Filter match unsuccessful. " + + "JsonPath: %s, Actual JsonPathValue: %s, Excepted Json Path Values: %s", + jsonPath, jsonPathValue, expectedValues); + processorContext.setMatched(false); + setTerminatingProcessingMessage(terminatingMessage, processorContext); + } else { + final String finishProcessingMessage = String.format("Filter match successful. " + + "JsonPath: %s, Actual JsonPathValue: %s, Excepted Json Path Values: %s", + jsonPath, jsonPathValue, expectedValues); + processorContext.setMatched(true); + setFinishedProcessingMessage(finishProcessingMessage, processorContext); + } + } else { + // if incoming message is blank of valid Json then matched flag will be null + final String terminatingMessage = "Incoming json message is blank or not json. " + + "Json filter cannot be applied"; + processorContext.setMatched(null); + setTerminatingProcessingMessage(terminatingMessage, processorContext); + } + + return processorContext; + } +} diff --git a/dcae-analytics-common/src/main/java/org/openecomp/dcae/apod/analytics/common/service/filter/JsonMessageFilterProcessorContext.java b/dcae-analytics-common/src/main/java/org/openecomp/dcae/apod/analytics/common/service/filter/JsonMessageFilterProcessorContext.java new file mode 100644 index 0000000..d27c363 --- /dev/null +++ b/dcae-analytics-common/src/main/java/org/openecomp/dcae/apod/analytics/common/service/filter/JsonMessageFilterProcessorContext.java @@ -0,0 +1,57 @@ +/* + * ===============================LICENSE_START====================================== + * dcae-analytics + * ================================================================================ + * Copyright © 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.openecomp.dcae.apod.analytics.common.service.filter; + +import org.openecomp.dcae.apod.analytics.common.service.processor.AbstractProcessorContext; + +/** + * A processor context for Json Message Filter Processor + *

+ * @author Rajiv Singla . Creation Date: 2/10/2017. + */ +public class JsonMessageFilterProcessorContext extends AbstractProcessorContext { + + private static final long serialVersionUID = 1L; + + private Boolean isMatched; + + public JsonMessageFilterProcessorContext(final String jsonMessageString) { + super(jsonMessageString, true); + } + + /** + * Returns true if Json Message Filter match was successful + * + * @return true if Json Message Filter match was successful, false if filter was match was unsuccessful + */ + public Boolean getMatched() { + return isMatched; + } + + /** + * Sets the value for Json Message Filter match + * + * @param matched new value for json message filter match + */ + public void setMatched(final Boolean matched) { + isMatched = matched; + } +} diff --git a/dcae-analytics-common/src/main/java/org/openecomp/dcae/apod/analytics/common/service/processor/AbstractMessageProcessor.java b/dcae-analytics-common/src/main/java/org/openecomp/dcae/apod/analytics/common/service/processor/AbstractMessageProcessor.java new file mode 100644 index 0000000..fdaa6cb --- /dev/null +++ b/dcae-analytics-common/src/main/java/org/openecomp/dcae/apod/analytics/common/service/processor/AbstractMessageProcessor.java @@ -0,0 +1,162 @@ +/* + * ===============================LICENSE_START====================================== + * dcae-analytics + * ================================================================================ + * Copyright © 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.openecomp.dcae.apod.analytics.common.service.processor; + +import com.google.common.base.Optional; +import org.openecomp.dcae.apod.analytics.common.exception.MessageProcessingException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.annotation.Nonnull; + +import static java.lang.String.format; + +/** + * An abstract Message Processor which can be extended by {@link MessageProcessor} implementations + * to get default behavior for Message Processors + * + * @param

Processor Context sub classes + * + * @author Rajiv Singla . Creation Date: 11/8/2016. + */ +public abstract class AbstractMessageProcessor

implements MessageProcessor

{ + + private static final Logger LOG = LoggerFactory.getLogger(AbstractMessageProcessor.class); + + /** + * By Default there is no processing message + */ + private String processingMessage = null; + + /** + * By Default Processing State is set to not required - subclasses must + * set processing state to {@link ProcessingState#PROCESSING_FINISHED_SUCCESSFULLY} on successful processing + * or {@link ProcessingState#PROCESSING_TERMINATED_EARLY} if processing fails + */ + protected ProcessingState processingState = ProcessingState.PROCESSING_NOT_REQUIRED; + + /** + * Sub classes must provide a description of a processor + * + * @return description of processor + * + */ + public abstract String getProcessorDescription(); + + + /** + * Sub classes must provide implementation to process Message + * + * @param processorContext incoming {@link ProcessorContext} + * @return outgoing {@link ProcessorContext} + */ + public abstract P processMessage(P processorContext); + + @Override + public ProcessorInfo getProcessorInfo() { + // by default the class of the Processor is assigned as Processor Name + final String processorClassName = getClass().getSimpleName(); + return new GenericProcessorInfo(processorClassName, getProcessorDescription()); + } + + @Override + public P preProcessor(P processorContext) { + LOG.debug("Processing Started for Processor: {}", getProcessorInfo().getProcessorName()); + // by default check to see if continue processing Flag is not false + final boolean okToContinue = processorContext.canProcessingContinue(); + if (!okToContinue) { + final String errorMessage = + format("Processor: %s. Processing Context flag okToContinue is false. Unable to proceed...", + getProcessorInfo().getProcessorName()); + throw new MessageProcessingException(errorMessage, LOG, new IllegalStateException(errorMessage)); + } + processingState = ProcessingState.PROCESSING_STARTED; + return processorContext; + } + + @Override + public ProcessingState getProcessingState() { + return processingState; + } + + @Override + public Optional getProcessingMessage() { + return Optional.fromNullable(processingMessage); + } + + @Override + public P postProcessor(P processorContext) { + // Default implementation updates the post processing flag if processing did not + // completed successfully + if (processingState != ProcessingState.PROCESSING_FINISHED_SUCCESSFULLY) { + LOG.debug("Processor: {}, Update Process Context State to stop Processing.", + getProcessorInfo().getProcessorName()); + processorContext.setProcessingContinueFlag(false); + } + // attaches itself to message processor context + processorContext.getMessageProcessors().add(this); + LOG.debug("Processing Completed for Processor: {}", getProcessorInfo()); + return processorContext; + } + + + @Override + public final P apply(@Nonnull P processorContext) { + final P preProcessedProcessorContext = preProcessor(processorContext); + final P processedProcessorContext = processMessage(preProcessedProcessorContext); + return postProcessor(processedProcessorContext); + } + + + /** + * Helper method that updates processing state in case of early termination, logs the processing + * termination reason, updates Processor processing state as Terminated and sets it processing message + * + * @param terminatingMessage error Message + * @param processorContext message processor context + */ + protected void setTerminatingProcessingMessage(final String terminatingMessage, + final P processorContext) { + + final String message = processorContext.getMessage(); + this.processingState = ProcessingState.PROCESSING_TERMINATED_EARLY; + this.processingMessage = terminatingMessage; + LOG.debug("Processor: {}, Early Terminating Message: {}, Incoming Message: {}", + getProcessorInfo().getProcessorName(), terminatingMessage, message); + } + + /** + * Helper method that updates Processing state and logs completion message + * passed + * + * @param processorPassingMessage Processor passing message + * @param processorContext message processor context + */ + protected void setFinishedProcessingMessage(final String processorPassingMessage, P processorContext) { + final String message = processorContext.getMessage(); + processingState = ProcessingState.PROCESSING_FINISHED_SUCCESSFULLY; + this.processingMessage = processorPassingMessage; + LOG.debug("Processor: {}, Successful Completion Message: {}, Incoming Message: {}", + getProcessorInfo().getProcessorName(), processorPassingMessage, message); + } + + +} diff --git a/dcae-analytics-common/src/main/java/org/openecomp/dcae/apod/analytics/common/service/processor/AbstractProcessorContext.java b/dcae-analytics-common/src/main/java/org/openecomp/dcae/apod/analytics/common/service/processor/AbstractProcessorContext.java new file mode 100644 index 0000000..a2501e3 --- /dev/null +++ b/dcae-analytics-common/src/main/java/org/openecomp/dcae/apod/analytics/common/service/processor/AbstractProcessorContext.java @@ -0,0 +1,96 @@ +/* + * ===============================LICENSE_START====================================== + * dcae-analytics + * ================================================================================ + * Copyright © 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.openecomp.dcae.apod.analytics.common.service.processor; + +import com.google.common.base.Objects; + +import java.util.LinkedList; +import java.util.List; + +/** + *

+ * An abstract implementation for {@link ProcessorContext} which other DCAE Analytics Modules + * can extend to add module specific functionality + *

+ * + * @author Rajiv Singla . Creation Date: 11/7/2016. + */ +public abstract class AbstractProcessorContext implements ProcessorContext { + + private final String message; + private List> messageProcessors; + private boolean canProcessingContinue; + + public AbstractProcessorContext(final String message, + boolean canProcessingContinue) { + this.message = message; + this.canProcessingContinue = canProcessingContinue; + this.messageProcessors = new LinkedList<>(); + } + + /** + * Returns JSON String of incoming CEF Message that needs to be processed + * + * @return incoming CEF message that needs to be processed + */ + @Override + public String getMessage() { + return message; + } + + /** + * Sets if it is ok to continue processing normally + * + * @return boolean which determines if it is ok to continue processing normally + */ + @Override + public boolean canProcessingContinue() { + return canProcessingContinue; + } + + + /** + * Set if it is ok to continue processing normally + * + * @param canProcessingContinue sets boolean which determines if it is ok to continue processing normally + */ + @Override + public void setProcessingContinueFlag(boolean canProcessingContinue) { + this.canProcessingContinue = canProcessingContinue; + } + + /** + * Provides List of message processors which were used in processing CEF message + * + * @return List of message processors which were used in processing CEF message + */ + @Override + public List> getMessageProcessors() { + return messageProcessors; + } + + @Override + public String toString() { + return Objects.toStringHelper(this) + .add("canProcessingContinue", canProcessingContinue) + .toString(); + } +} diff --git a/dcae-analytics-common/src/main/java/org/openecomp/dcae/apod/analytics/common/service/processor/GenericMessageChainProcessor.java b/dcae-analytics-common/src/main/java/org/openecomp/dcae/apod/analytics/common/service/processor/GenericMessageChainProcessor.java new file mode 100644 index 0000000..982641d --- /dev/null +++ b/dcae-analytics-common/src/main/java/org/openecomp/dcae/apod/analytics/common/service/processor/GenericMessageChainProcessor.java @@ -0,0 +1,70 @@ +/* + * ===============================LICENSE_START====================================== + * dcae-analytics + * ================================================================================ + * Copyright © 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.openecomp.dcae.apod.analytics.common.service.processor; + +import org.openecomp.dcae.apod.analytics.common.utils.MessageProcessorUtils.MessageProcessorFunction; + +import java.util.List; + +import static org.openecomp.dcae.apod.analytics.common.utils.MessageProcessorUtils.computeMessageProcessorChain; + +/** + *

+ * A Generic Message Processor which passes the {@link ProcessorContext} from first to second + * {@link MessageProcessor} + *

+ * + * @param

Processor Context sub classes + * + * @author Rajiv Singla . Creation Date: 11/8/2016. + */ +public class GenericMessageChainProcessor

{ + + private final List> messageProcessors; + private final P initialProcessorContext; + + public GenericMessageChainProcessor(List> messageProcessors, + P initialProcessorContext) { + this.messageProcessors = messageProcessors; + this.initialProcessorContext = initialProcessorContext; + } + + /** + * Process a processor chain + * + * @return Processor Context after processing the processor chain + */ + public P processChain() { + + final MessageProcessorFunction

messageProcessorFunction = + new MessageProcessorFunction

() { + @Override + public > P apply(P context, M processor) { + return processor.apply(context); + } + }; + + return computeMessageProcessorChain(messageProcessors, initialProcessorContext, + messageProcessorFunction); + + } + +} diff --git a/dcae-analytics-common/src/main/java/org/openecomp/dcae/apod/analytics/common/service/processor/GenericProcessorInfo.java b/dcae-analytics-common/src/main/java/org/openecomp/dcae/apod/analytics/common/service/processor/GenericProcessorInfo.java new file mode 100644 index 0000000..69d817f --- /dev/null +++ b/dcae-analytics-common/src/main/java/org/openecomp/dcae/apod/analytics/common/service/processor/GenericProcessorInfo.java @@ -0,0 +1,57 @@ +/* + * ===============================LICENSE_START====================================== + * dcae-analytics + * ================================================================================ + * Copyright © 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.openecomp.dcae.apod.analytics.common.service.processor; + +import javax.annotation.Nonnull; + +/** + * A Generic Implementation of {@link ProcessorInfo} + * + * @author Rajiv Singla . Creation Date: 11/7/2016. + */ +public class GenericProcessorInfo implements ProcessorInfo { + + private static final long serialVersionUID = 1L; + + private final String processorName; + private final String processorDescription; + + public GenericProcessorInfo(@Nonnull String processorName, @Nonnull String processorDescription) { + this.processorName = processorName; + this.processorDescription = processorDescription; + } + + @Override + public String getProcessorName() { + return processorName; + } + + @Override + public String getProcessorDescription() { + return processorDescription; + } + + + @Override + public String toString() { + return processorName; + } +} diff --git a/dcae-analytics-common/src/main/java/org/openecomp/dcae/apod/analytics/common/service/processor/MessageProcessor.java b/dcae-analytics-common/src/main/java/org/openecomp/dcae/apod/analytics/common/service/processor/MessageProcessor.java new file mode 100644 index 0000000..6af34f3 --- /dev/null +++ b/dcae-analytics-common/src/main/java/org/openecomp/dcae/apod/analytics/common/service/processor/MessageProcessor.java @@ -0,0 +1,84 @@ +/* + * ===============================LICENSE_START====================================== + * dcae-analytics + * ================================================================================ + * Copyright © 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.openecomp.dcae.apod.analytics.common.service.processor; + +import com.google.common.base.Function; +import com.google.common.base.Optional; + +import java.io.Serializable; + +/** + *

+ * A message processor can be used to process incoming messages. + * It uses implementations of {@link ProcessorContext} as input and output + *

+ * + * @param

Message Processor Context implementations + * + * @author Rajiv Singla . Creation Date: 11/7/2016. + */ +public interface MessageProcessor

extends Function, Serializable { + + /** + * Returns processor information + * + * @return processor Information + */ + ProcessorInfo getProcessorInfo(); + + + /** + * Does pre-processing of {@link ProcessorContext} e.g. validate input conditions and return + * pre processed context + * + * @param processorContext incoming Processor Context + * @return Pre processed Processor Context + */ + P preProcessor(P processorContext); + + + /** + * Return processing state of a processor + * + * @return Processing State + */ + ProcessingState getProcessingState(); + + + /** + * May return a message from a processor which indicates the reason for {@link ProcessingState} especially if + * there was some failure in processing + * + * @return processing Message + */ + Optional getProcessingMessage(); + + + /** + * Does post-processing of {@link ProcessorContext} + * + * @param processorContext incoming Processor Context + * @return processor Context after post processing is finished + */ + P postProcessor(P processorContext); + + +} diff --git a/dcae-analytics-common/src/main/java/org/openecomp/dcae/apod/analytics/common/service/processor/ProcessingState.java b/dcae-analytics-common/src/main/java/org/openecomp/dcae/apod/analytics/common/service/processor/ProcessingState.java new file mode 100644 index 0000000..cc6127d --- /dev/null +++ b/dcae-analytics-common/src/main/java/org/openecomp/dcae/apod/analytics/common/service/processor/ProcessingState.java @@ -0,0 +1,36 @@ +/* + * ===============================LICENSE_START====================================== + * dcae-analytics + * ================================================================================ + * Copyright © 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.openecomp.dcae.apod.analytics.common.service.processor; + +/** + *

+ * Processing state of a {@link MessageProcessor} + *

+ * @author Rajiv Singla . Creation Date: 11/5/2016. + */ +public enum ProcessingState { + + PROCESSING_STARTED, + PROCESSING_FINISHED_SUCCESSFULLY, + PROCESSING_TERMINATED_EARLY, + PROCESSING_NOT_REQUIRED; + +} diff --git a/dcae-analytics-common/src/main/java/org/openecomp/dcae/apod/analytics/common/service/processor/ProcessorContext.java b/dcae-analytics-common/src/main/java/org/openecomp/dcae/apod/analytics/common/service/processor/ProcessorContext.java new file mode 100644 index 0000000..2dde5e1 --- /dev/null +++ b/dcae-analytics-common/src/main/java/org/openecomp/dcae/apod/analytics/common/service/processor/ProcessorContext.java @@ -0,0 +1,70 @@ +/* + * ===============================LICENSE_START====================================== + * dcae-analytics + * ================================================================================ + * Copyright © 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.openecomp.dcae.apod.analytics.common.service.processor; + +import java.io.Serializable; +import java.util.List; + +/** + *

+ * A Processor Context is used a an input and output to a {@link MessageProcessor} + *
+ * DCAE Analytics sub projects should extend this interface and add specific fields + * required for input and output + *

+ * + * @author Rajiv Singla . Creation Date: 11/7/2016. + */ +public interface ProcessorContext extends Serializable { + + /** + * Returns Processor Context message that will be processed by Chain of Processors + * + * @return message that need to be processed by processors + */ + String getMessage(); + + /** + * Processing Context flag which determines if Processing can continue in a processing + * chain + * + * @return true if ok to continue processing normally + */ + boolean canProcessingContinue(); + + + /** + * Sets new value for ProcessingContinue flag which will cause early termination of processing in chain if + * set to false + * + * @param canProcessingContinue set new value for canProcessing Continue flag + */ + void setProcessingContinueFlag(boolean canProcessingContinue); + + + /** + * Provides a List of previous processors which have completed processing + * + * @return list of previous processors + */ + List> getMessageProcessors(); + +} diff --git a/dcae-analytics-common/src/main/java/org/openecomp/dcae/apod/analytics/common/service/processor/ProcessorInfo.java b/dcae-analytics-common/src/main/java/org/openecomp/dcae/apod/analytics/common/service/processor/ProcessorInfo.java new file mode 100644 index 0000000..c7045d8 --- /dev/null +++ b/dcae-analytics-common/src/main/java/org/openecomp/dcae/apod/analytics/common/service/processor/ProcessorInfo.java @@ -0,0 +1,49 @@ +/* + * ===============================LICENSE_START====================================== + * dcae-analytics + * ================================================================================ + * Copyright © 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.openecomp.dcae.apod.analytics.common.service.processor; + +import java.io.Serializable; + +/** + *

+ * Contains Information about a processor. For e.g. Processor name, processor description etc + *

+ * + * @author Rajiv Singla . Creation Date: 11/7/2016. + */ +public interface ProcessorInfo extends Serializable { + + /** + * Returns a name which should uniquely identify a particular {@link MessageProcessor} + * + * @return processor name + */ + String getProcessorName(); + + + /** + * Returns description of a {@link MessageProcessor} + * + * @return processor description + */ + String getProcessorDescription(); + +} diff --git a/dcae-analytics-common/src/main/java/org/openecomp/dcae/apod/analytics/common/utils/HTTPUtils.java b/dcae-analytics-common/src/main/java/org/openecomp/dcae/apod/analytics/common/utils/HTTPUtils.java new file mode 100644 index 0000000..149aaee --- /dev/null +++ b/dcae-analytics-common/src/main/java/org/openecomp/dcae/apod/analytics/common/utils/HTTPUtils.java @@ -0,0 +1,62 @@ +/* + * ===============================LICENSE_START====================================== + * dcae-analytics + * ================================================================================ + * Copyright © 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.openecomp.dcae.apod.analytics.common.utils; + +/** + * Contains common utils to check HTTP Related Utils + * + * @author Rajiv Singla . Creation Date: 11/2/2016. + */ +public abstract class HTTPUtils { + + /** + * HTTP Status code for successful HTTP call + */ + public static final Integer HTTP_SUCCESS_STATUS_CODE = 200; + + /** + * HTTP Response code when request has been accepted for processing, but the processing has not been completed + */ + public static final Integer HTTP_ACCEPTED_RESPONSE_CODE = 202; + + /** + * HTTP Response code when there is no content + */ + public static final Integer HTTP_NO_CONTENT_RESPONSE_CODE = 204; + + + public static final String JSON_APPLICATION_TYPE = "application/json"; + + + private HTTPUtils() { + + } + + /** + * Checks if HTTP Status code is less than or equal to 200 but less then 300 + * + * @param statusCode http status code + * @return true if response code between 200 and 300 + */ + public static boolean isSuccessfulResponseCode(Integer statusCode) { + return statusCode >= 200 && statusCode < 300; + } +} diff --git a/dcae-analytics-common/src/main/java/org/openecomp/dcae/apod/analytics/common/utils/MessageProcessorUtils.java b/dcae-analytics-common/src/main/java/org/openecomp/dcae/apod/analytics/common/utils/MessageProcessorUtils.java new file mode 100644 index 0000000..71f4bab --- /dev/null +++ b/dcae-analytics-common/src/main/java/org/openecomp/dcae/apod/analytics/common/utils/MessageProcessorUtils.java @@ -0,0 +1,162 @@ +/* + * ===============================LICENSE_START====================================== + * dcae-analytics + * ================================================================================ + * Copyright © 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.openecomp.dcae.apod.analytics.common.utils; + +import com.google.common.base.Preconditions; +import org.openecomp.dcae.apod.analytics.common.exception.MessageProcessingException; +import org.openecomp.dcae.apod.analytics.common.service.filter.GenericJsonMessageFilter; +import org.openecomp.dcae.apod.analytics.common.service.filter.JsonMessageFilterProcessorContext; +import org.openecomp.dcae.apod.analytics.common.service.processor.GenericMessageChainProcessor; +import org.openecomp.dcae.apod.analytics.common.service.processor.MessageProcessor; +import org.openecomp.dcae.apod.analytics.common.service.processor.ProcessorContext; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import javax.annotation.Nonnull; + + +/** + * + * @author Rajiv Singla . Creation Date: 11/8/2016. + */ +public abstract class MessageProcessorUtils { + + private static final Logger LOG = LoggerFactory.getLogger(MessageProcessorUtils.class); + + /** + * Provides an abstraction how to apply {@link ProcessorContext} to next {@link MessageProcessor} + * in the message processor chain + * + * @param

Sub classes of Processor Context + */ + public interface MessageProcessorFunction

{ + + /** + * Method which provides accumulated {@link ProcessorContext} from previous processors and a reference + * to next processor in the chain + * + * @param p accumulated {@link ProcessorContext} from previous processors + * @param m current {@link MessageProcessor} in the chain + * @param Message processor sub classes + * + * @return processing context after computing the current Message Processor + */ + > P apply(P p, M m); + } + + + /** + * Provides an abstraction to compute a chain of {@link MessageProcessor} + * + * @param messageProcessors An iterable containing one or more {@link MessageProcessor}s + * @param initialProcessorContext An initial processing Context + * @param messageProcessorFunction messageProcessor Function + * @param

Sub classes for Processor Context + * + * @return processing context which results after computing the whole chain + */ + public static

P computeMessageProcessorChain( + final Iterable> messageProcessors, + final P initialProcessorContext, + final MessageProcessorFunction

messageProcessorFunction) { + + // Get message processor iterator + final Iterator> processorIterator = messageProcessors.iterator(); + + // If no next message processor - return initial processor context + if (!processorIterator.hasNext()) { + return initialProcessorContext; + } + + // An accumulator for processor Context + P processorContextAccumulator = initialProcessorContext; + + while (processorIterator.hasNext()) { + + final MessageProcessor

nextProcessor = processorIterator.next(); + + // If Initial Processor Context is null + if (processorContextAccumulator == null) { + final String errorMessage = + String.format("Processor Context must not be null for Message Process: %s", + nextProcessor.getProcessorInfo().getProcessorName()); + throw new MessageProcessingException(errorMessage, LOG, new IllegalStateException(errorMessage)); + } + + + if (!processorContextAccumulator.canProcessingContinue()) { + LOG.debug("Triggering Early Termination, before Message Processor: {}, Incoming Message: {}", + nextProcessor.getProcessorInfo().getProcessorName(), processorContextAccumulator.getMessage()); + break; + } + processorContextAccumulator = messageProcessorFunction.apply(processorContextAccumulator, nextProcessor); + } + + return processorContextAccumulator; + } + + + /** + * Utility method to process Json Filter Mappings. Processes incoming json message and applies a list of json + * filter mappings and returns the resulting {@link JsonMessageFilterProcessorContext} + * + * @param jsonMessage json message to which filter mappings will be applies + * @param jsonFilterMappings Filter mappings contains a Map containing keys as filter json path + * and values as set of expected value corresponding to filter path + * + * @return json message processor context which contains the {@link JsonMessageFilterProcessorContext#isMatched} + * status after applying all filter mappings + */ + public static JsonMessageFilterProcessorContext processJsonFilterMappings( + final String jsonMessage, @Nonnull final Map> jsonFilterMappings) { + + Preconditions.checkState(jsonFilterMappings.size() > 0, "Json Filter Mappings must not be empty"); + + // create initial processor context containing the json message that need to be processed + final JsonMessageFilterProcessorContext initialProcessorContext = + new JsonMessageFilterProcessorContext(jsonMessage); + + // Create Json Message Filters + final List jsonMessageFilters = new LinkedList<>(); + + int i = 0; + for (Map.Entry> jsonFilterMapping : jsonFilterMappings.entrySet()) { + jsonMessageFilters.add(new GenericJsonMessageFilter("Filter-" + i, jsonFilterMapping.getKey(), + jsonFilterMapping.getValue())); + i++; + } + + // Create Generic Message Chain Processor + final GenericMessageChainProcessor messageChainProcessor = + new GenericMessageChainProcessor<>(jsonMessageFilters, initialProcessorContext); + + // Process chain and return resulting json Message Filter Processor Context + return messageChainProcessor.processChain(); + } + + +} diff --git a/dcae-analytics-common/src/main/java/org/openecomp/dcae/apod/analytics/common/utils/PersistenceUtils.java b/dcae-analytics-common/src/main/java/org/openecomp/dcae/apod/analytics/common/utils/PersistenceUtils.java new file mode 100644 index 0000000..36fea2d --- /dev/null +++ b/dcae-analytics-common/src/main/java/org/openecomp/dcae/apod/analytics/common/utils/PersistenceUtils.java @@ -0,0 +1,59 @@ +/* + * ===============================LICENSE_START====================================== + * dcae-analytics + * ================================================================================ + * Copyright © 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.openecomp.dcae.apod.analytics.common.utils; + +import java.util.Date; + +/** + * Contains Utility methods for creating persistence row keys etc. + * + * @author Rajiv Singla . Creation Date: 11/16/2016. + */ +public abstract class PersistenceUtils { + + + /** + * Name of the column which will contain Table Key + */ + public static final String TABLE_ROW_KEY_COLUMN_NAME = "key"; + + /** + * Delimited to be used when creating a row key with multiple fields + */ + public static final String ROW_KEY_DELIMITER = "-"; + + + private PersistenceUtils() { + + } + + /** + * Creates a decreasing number using current timestamp. Handy when you want to keep records most recent records + * close to the top of column table like HBase + * + * @return decreasing number + */ + public static String getCurrentTimeReverseSubKey() { + final long timeReverseLong = Long.MAX_VALUE - new Date().getTime(); + return String.format("%025d", timeReverseLong); + } + +} diff --git a/dcae-analytics-common/src/main/java/org/openecomp/dcae/apod/analytics/common/validation/DCAEValidator.java b/dcae-analytics-common/src/main/java/org/openecomp/dcae/apod/analytics/common/validation/DCAEValidator.java new file mode 100644 index 0000000..c8d3549 --- /dev/null +++ b/dcae-analytics-common/src/main/java/org/openecomp/dcae/apod/analytics/common/validation/DCAEValidator.java @@ -0,0 +1,31 @@ +/* + * ===============================LICENSE_START====================================== + * dcae-analytics + * ================================================================================ + * Copyright © 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.openecomp.dcae.apod.analytics.common.validation; + +import java.io.Serializable; + +/** + * Marker interface for all DCAE Validators + * + * @author Rajiv Singla . Creation Date: 10/24/2016. + */ +public interface DCAEValidator extends Serializable { +} diff --git a/dcae-analytics-common/src/main/java/org/openecomp/dcae/apod/analytics/common/validation/GenericValidationResponse.java b/dcae-analytics-common/src/main/java/org/openecomp/dcae/apod/analytics/common/validation/GenericValidationResponse.java new file mode 100644 index 0000000..06237f0 --- /dev/null +++ b/dcae-analytics-common/src/main/java/org/openecomp/dcae/apod/analytics/common/validation/GenericValidationResponse.java @@ -0,0 +1,84 @@ +/* + * ===============================LICENSE_START====================================== + * dcae-analytics + * ================================================================================ + * Copyright © 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.openecomp.dcae.apod.analytics.common.validation; + +import com.google.common.base.Joiner; +import com.google.common.base.Objects; + +import java.util.Collection; +import java.util.LinkedHashMap; +import java.util.Map; +import java.util.Set; + +/** + * A generic implementation of Validation Response + * + * @param Validation Entity Type + * + * @author Rajiv Singla . Creation Date: 10/24/2016. + */ +public class GenericValidationResponse implements ValidationResponse { + + private LinkedHashMap errorMessageMap = new LinkedHashMap<>(); + + @Override + public boolean hasErrors() { + return errorMessageMap.size() != 0; + } + + @Override + public Set getFieldNamesWithError() { + return errorMessageMap.keySet(); + } + + @Override + public Collection getErrorMessages() { + return errorMessageMap.values(); + } + + @Override + public Map getValidationResultsAsMap() { + return errorMessageMap; + } + + @Override + public String getAllErrorMessage() { + return getAllErrorMessage(","); + } + + @Override + public String getAllErrorMessage(String delimiter) { + return Joiner.on(delimiter).join(errorMessageMap.values()); + } + + @Override + public void addErrorMessage(String fieldName, String filedErrorMessage) { + errorMessageMap.put(fieldName, filedErrorMessage); + } + + @Override + public String toString() { + return Objects.toStringHelper(this) + .add("hasErrors", hasErrors()) + .add("errorMessageMap", errorMessageMap) + .toString(); + } +} diff --git a/dcae-analytics-common/src/main/java/org/openecomp/dcae/apod/analytics/common/validation/ValidationResponse.java b/dcae-analytics-common/src/main/java/org/openecomp/dcae/apod/analytics/common/validation/ValidationResponse.java new file mode 100644 index 0000000..f3ffa71 --- /dev/null +++ b/dcae-analytics-common/src/main/java/org/openecomp/dcae/apod/analytics/common/validation/ValidationResponse.java @@ -0,0 +1,90 @@ +/* + * ===============================LICENSE_START====================================== + * dcae-analytics + * ================================================================================ + * Copyright © 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.openecomp.dcae.apod.analytics.common.validation; + +import java.util.Collection; +import java.util.Map; +import java.util.Set; + +/** + * Validation Response contract + * + * @param Entity class type which is being validated + * + * @author Rajiv Singla . Creation Date: 10/24/2016. + */ +public interface ValidationResponse { + + /** + * Returns true if validation resulted in one or more errors + * + * @return true if validation has errors + */ + boolean hasErrors(); + + /** + * Returns all field names which have error + * + * @return names of fields which have error + */ + Set getFieldNamesWithError(); + + /** + * Returns list of all error messages + * + * @return list of error messages + */ + Collection getErrorMessages(); + + + /** + * Returns all error messages as string delimited by comma + * + * @return all error messages delimited by given delimiter + */ + String getAllErrorMessage(); + + /** + * Returns all error messages as string delimited by given delimited + * + * @param delimiter delimited to be used for error message + * @return all error messages delimited by given delimiter + */ + String getAllErrorMessage(String delimiter); + + /** + * Adds field name and error message to the validation response + * + * @param fieldName field name which has validation error + * @param filedErrorMessage validation error message + */ + void addErrorMessage(String fieldName, String filedErrorMessage); + + + /** + * Returns validation results as map containing values as keys and values + * as error Message + * + * @return Map containing field names and error message associated with those fields + */ + Map getValidationResultsAsMap(); + +} -- cgit 1.2.3-korg