From e37de6e66c5cc554384a1f27f68f4d27962fe15d Mon Sep 17 00:00:00 2001 From: Lisa Revel Date: Mon, 20 Feb 2017 19:15:19 +0000 Subject: Re-add source from correct branch Change-Id: I515f8378ea1d2baf0aa2434ec46272f1b5bdb229 Signed-off-by: Lisa Revel --- .../analytics/tca/TCAAnalyticsApplication.java | 105 --- .../analytics/tca/flow/TCAVESCollectorFlow.java | 69 -- .../tca/flowlet/TCAVESAlertsSinkFlowlet.java | 69 -- .../tca/flowlet/TCAVESMessageRouterFlowlet.java | 62 -- .../TCAVESThresholdViolationCalculatorFlowlet.java | 137 ---- .../tca/persistance/TCACalculatorMessageType.java | 43 -- .../tca/persistance/TCAMessageStatusEntity.java | 306 -------- .../tca/persistance/TCAMessageStatusPersister.java | 243 ------ .../tca/persistance/TCAVESAlertEntity.java | 77 -- .../tca/persistance/TCAVESAlertsPersister.java | 98 --- .../processor/AbstractTCAECEFPolicyProcessor.java | 61 -- .../tca/processor/TCACEFJsonProcessor.java | 98 --- .../tca/processor/TCACEFPolicyDomainFilter.java | 83 --- .../TCACEFPolicyFunctionalRoleFilter.java | 90 --- .../processor/TCACEFPolicyThresholdsProcessor.java | 135 ---- .../tca/processor/TCACEFProcessorContext.java | 103 --- .../dcae/analytics/tca/settings/TCAAppConfig.java | 96 --- .../analytics/tca/settings/TCAAppPreferences.java | 262 ------- .../tca/settings/TCAPolicyPreferences.java | 36 - .../AppPreferencesToPublisherConfigMapper.java | 82 -- .../AppPreferencesToSubscriberConfigMapper.java | 99 --- .../dcae/analytics/tca/utils/TCAUtils.java | 825 --------------------- .../tca/validator/TCAAppConfigValidator.java | 60 -- .../validator/TCAPolicyPreferencesValidator.java | 94 --- .../tca/validator/TCAPreferencesValidator.java | 63 -- .../tca/worker/TCADMaaPMRPublisherJob.java | 189 ----- .../tca/worker/TCADMaaPMRSubscriberJob.java | 175 ----- .../tca/worker/TCADMaaPMockSubscriberWorker.java | 139 ---- .../tca/worker/TCADMaaPPublisherWorker.java | 230 ------ .../tca/worker/TCADMaaPSubscriberWorker.java | 226 ------ .../analytics/tca/TCAAnalyticsApplication.java | 105 +++ .../analytics/tca/flow/TCAVESCollectorFlow.java | 69 ++ .../tca/flowlet/TCAVESAlertsSinkFlowlet.java | 69 ++ .../tca/flowlet/TCAVESMessageRouterFlowlet.java | 62 ++ .../TCAVESThresholdViolationCalculatorFlowlet.java | 137 ++++ .../tca/persistance/TCACalculatorMessageType.java | 43 ++ .../tca/persistance/TCAMessageStatusEntity.java | 306 ++++++++ .../tca/persistance/TCAMessageStatusPersister.java | 243 ++++++ .../tca/persistance/TCAVESAlertEntity.java | 77 ++ .../tca/persistance/TCAVESAlertsPersister.java | 98 +++ .../processor/AbstractTCAECEFPolicyProcessor.java | 61 ++ .../tca/processor/TCACEFJsonProcessor.java | 98 +++ .../tca/processor/TCACEFPolicyDomainFilter.java | 83 +++ .../TCACEFPolicyFunctionalRoleFilter.java | 90 +++ .../processor/TCACEFPolicyThresholdsProcessor.java | 135 ++++ .../tca/processor/TCACEFProcessorContext.java | 103 +++ .../apod/analytics/tca/settings/TCAAppConfig.java | 96 +++ .../analytics/tca/settings/TCAAppPreferences.java | 262 +++++++ .../tca/settings/TCAPolicyPreferences.java | 36 + .../AppPreferencesToPublisherConfigMapper.java | 82 ++ .../AppPreferencesToSubscriberConfigMapper.java | 99 +++ .../dcae/apod/analytics/tca/utils/TCAUtils.java | 825 +++++++++++++++++++++ .../tca/validator/TCAAppConfigValidator.java | 60 ++ .../validator/TCAPolicyPreferencesValidator.java | 94 +++ .../tca/validator/TCAPreferencesValidator.java | 63 ++ .../tca/worker/TCADMaaPMRPublisherJob.java | 189 +++++ .../tca/worker/TCADMaaPMRSubscriberJob.java | 175 +++++ .../tca/worker/TCADMaaPMockSubscriberWorker.java | 139 ++++ .../tca/worker/TCADMaaPPublisherWorker.java | 230 ++++++ .../tca/worker/TCADMaaPSubscriberWorker.java | 226 ++++++ .../dcae/analytics/tca/BaseAnalyticsTCAIT.java | 141 ---- .../analytics/tca/BaseAnalyticsTCAUnitTest.java | 220 ------ .../analytics/tca/it/TCAnalyticsAppConfigIT.java | 53 -- .../tca/processor/TCACEFJsonProcessorTest.java | 117 --- .../TCACEFPolicyThresholdsProcessorTest.java | 77 -- .../tca/processor/TCACEFProcessorContextTest.java | 38 - .../analytics/tca/settings/TCATestAppConfig.java | 38 - .../tca/settings/TCATestAppConfigHolder.java | 40 - .../tca/settings/TCATestAppPreferences.java | 136 ---- .../AppPreferencesToPublisherConfigMapperTest.java | 58 -- ...AppPreferencesToSubscriberConfigMapperTest.java | 59 -- .../dcae/analytics/tca/utils/TCAUtilsTest.java | 357 --------- .../tca/worker/TCADMaaPMRSubscriberJobTest.java | 80 -- .../apod/analytics/tca/BaseAnalyticsTCAIT.java | 141 ++++ .../analytics/tca/BaseAnalyticsTCAUnitTest.java | 220 ++++++ .../analytics/tca/it/TCAnalyticsAppConfigIT.java | 53 ++ .../tca/processor/TCACEFJsonProcessorTest.java | 117 +++ .../TCACEFPolicyThresholdsProcessorTest.java | 77 ++ .../tca/processor/TCACEFProcessorContextTest.java | 38 + .../analytics/tca/settings/TCATestAppConfig.java | 38 + .../tca/settings/TCATestAppConfigHolder.java | 40 + .../tca/settings/TCATestAppPreferences.java | 136 ++++ .../AppPreferencesToPublisherConfigMapperTest.java | 58 ++ ...AppPreferencesToSubscriberConfigMapperTest.java | 59 ++ .../apod/analytics/tca/utils/TCAUtilsTest.java | 357 +++++++++ .../tca/worker/TCADMaaPMRSubscriberJobTest.java | 80 ++ 86 files changed, 5769 insertions(+), 5769 deletions(-) delete mode 100644 dcae-analytics-tca/src/main/java/org/openecomp/dcae/analytics/tca/TCAAnalyticsApplication.java delete mode 100644 dcae-analytics-tca/src/main/java/org/openecomp/dcae/analytics/tca/flow/TCAVESCollectorFlow.java delete mode 100644 dcae-analytics-tca/src/main/java/org/openecomp/dcae/analytics/tca/flowlet/TCAVESAlertsSinkFlowlet.java delete mode 100644 dcae-analytics-tca/src/main/java/org/openecomp/dcae/analytics/tca/flowlet/TCAVESMessageRouterFlowlet.java delete mode 100644 dcae-analytics-tca/src/main/java/org/openecomp/dcae/analytics/tca/flowlet/TCAVESThresholdViolationCalculatorFlowlet.java delete mode 100644 dcae-analytics-tca/src/main/java/org/openecomp/dcae/analytics/tca/persistance/TCACalculatorMessageType.java delete mode 100644 dcae-analytics-tca/src/main/java/org/openecomp/dcae/analytics/tca/persistance/TCAMessageStatusEntity.java delete mode 100644 dcae-analytics-tca/src/main/java/org/openecomp/dcae/analytics/tca/persistance/TCAMessageStatusPersister.java delete mode 100644 dcae-analytics-tca/src/main/java/org/openecomp/dcae/analytics/tca/persistance/TCAVESAlertEntity.java delete mode 100644 dcae-analytics-tca/src/main/java/org/openecomp/dcae/analytics/tca/persistance/TCAVESAlertsPersister.java delete mode 100644 dcae-analytics-tca/src/main/java/org/openecomp/dcae/analytics/tca/processor/AbstractTCAECEFPolicyProcessor.java delete mode 100644 dcae-analytics-tca/src/main/java/org/openecomp/dcae/analytics/tca/processor/TCACEFJsonProcessor.java delete mode 100644 dcae-analytics-tca/src/main/java/org/openecomp/dcae/analytics/tca/processor/TCACEFPolicyDomainFilter.java delete mode 100644 dcae-analytics-tca/src/main/java/org/openecomp/dcae/analytics/tca/processor/TCACEFPolicyFunctionalRoleFilter.java delete mode 100644 dcae-analytics-tca/src/main/java/org/openecomp/dcae/analytics/tca/processor/TCACEFPolicyThresholdsProcessor.java delete mode 100644 dcae-analytics-tca/src/main/java/org/openecomp/dcae/analytics/tca/processor/TCACEFProcessorContext.java delete mode 100644 dcae-analytics-tca/src/main/java/org/openecomp/dcae/analytics/tca/settings/TCAAppConfig.java delete mode 100644 dcae-analytics-tca/src/main/java/org/openecomp/dcae/analytics/tca/settings/TCAAppPreferences.java delete mode 100644 dcae-analytics-tca/src/main/java/org/openecomp/dcae/analytics/tca/settings/TCAPolicyPreferences.java delete mode 100644 dcae-analytics-tca/src/main/java/org/openecomp/dcae/analytics/tca/utils/AppPreferencesToPublisherConfigMapper.java delete mode 100644 dcae-analytics-tca/src/main/java/org/openecomp/dcae/analytics/tca/utils/AppPreferencesToSubscriberConfigMapper.java delete mode 100644 dcae-analytics-tca/src/main/java/org/openecomp/dcae/analytics/tca/utils/TCAUtils.java delete mode 100644 dcae-analytics-tca/src/main/java/org/openecomp/dcae/analytics/tca/validator/TCAAppConfigValidator.java delete mode 100644 dcae-analytics-tca/src/main/java/org/openecomp/dcae/analytics/tca/validator/TCAPolicyPreferencesValidator.java delete mode 100644 dcae-analytics-tca/src/main/java/org/openecomp/dcae/analytics/tca/validator/TCAPreferencesValidator.java delete mode 100644 dcae-analytics-tca/src/main/java/org/openecomp/dcae/analytics/tca/worker/TCADMaaPMRPublisherJob.java delete mode 100644 dcae-analytics-tca/src/main/java/org/openecomp/dcae/analytics/tca/worker/TCADMaaPMRSubscriberJob.java delete mode 100644 dcae-analytics-tca/src/main/java/org/openecomp/dcae/analytics/tca/worker/TCADMaaPMockSubscriberWorker.java delete mode 100644 dcae-analytics-tca/src/main/java/org/openecomp/dcae/analytics/tca/worker/TCADMaaPPublisherWorker.java delete mode 100644 dcae-analytics-tca/src/main/java/org/openecomp/dcae/analytics/tca/worker/TCADMaaPSubscriberWorker.java create mode 100644 dcae-analytics-tca/src/main/java/org/openecomp/dcae/apod/analytics/tca/TCAAnalyticsApplication.java create mode 100644 dcae-analytics-tca/src/main/java/org/openecomp/dcae/apod/analytics/tca/flow/TCAVESCollectorFlow.java create mode 100644 dcae-analytics-tca/src/main/java/org/openecomp/dcae/apod/analytics/tca/flowlet/TCAVESAlertsSinkFlowlet.java create mode 100644 dcae-analytics-tca/src/main/java/org/openecomp/dcae/apod/analytics/tca/flowlet/TCAVESMessageRouterFlowlet.java create mode 100644 dcae-analytics-tca/src/main/java/org/openecomp/dcae/apod/analytics/tca/flowlet/TCAVESThresholdViolationCalculatorFlowlet.java create mode 100644 dcae-analytics-tca/src/main/java/org/openecomp/dcae/apod/analytics/tca/persistance/TCACalculatorMessageType.java create mode 100644 dcae-analytics-tca/src/main/java/org/openecomp/dcae/apod/analytics/tca/persistance/TCAMessageStatusEntity.java create mode 100644 dcae-analytics-tca/src/main/java/org/openecomp/dcae/apod/analytics/tca/persistance/TCAMessageStatusPersister.java create mode 100644 dcae-analytics-tca/src/main/java/org/openecomp/dcae/apod/analytics/tca/persistance/TCAVESAlertEntity.java create mode 100644 dcae-analytics-tca/src/main/java/org/openecomp/dcae/apod/analytics/tca/persistance/TCAVESAlertsPersister.java create mode 100644 dcae-analytics-tca/src/main/java/org/openecomp/dcae/apod/analytics/tca/processor/AbstractTCAECEFPolicyProcessor.java create mode 100644 dcae-analytics-tca/src/main/java/org/openecomp/dcae/apod/analytics/tca/processor/TCACEFJsonProcessor.java create mode 100644 dcae-analytics-tca/src/main/java/org/openecomp/dcae/apod/analytics/tca/processor/TCACEFPolicyDomainFilter.java create mode 100644 dcae-analytics-tca/src/main/java/org/openecomp/dcae/apod/analytics/tca/processor/TCACEFPolicyFunctionalRoleFilter.java create mode 100644 dcae-analytics-tca/src/main/java/org/openecomp/dcae/apod/analytics/tca/processor/TCACEFPolicyThresholdsProcessor.java create mode 100644 dcae-analytics-tca/src/main/java/org/openecomp/dcae/apod/analytics/tca/processor/TCACEFProcessorContext.java create mode 100644 dcae-analytics-tca/src/main/java/org/openecomp/dcae/apod/analytics/tca/settings/TCAAppConfig.java create mode 100644 dcae-analytics-tca/src/main/java/org/openecomp/dcae/apod/analytics/tca/settings/TCAAppPreferences.java create mode 100644 dcae-analytics-tca/src/main/java/org/openecomp/dcae/apod/analytics/tca/settings/TCAPolicyPreferences.java create mode 100644 dcae-analytics-tca/src/main/java/org/openecomp/dcae/apod/analytics/tca/utils/AppPreferencesToPublisherConfigMapper.java create mode 100644 dcae-analytics-tca/src/main/java/org/openecomp/dcae/apod/analytics/tca/utils/AppPreferencesToSubscriberConfigMapper.java create mode 100644 dcae-analytics-tca/src/main/java/org/openecomp/dcae/apod/analytics/tca/utils/TCAUtils.java create mode 100644 dcae-analytics-tca/src/main/java/org/openecomp/dcae/apod/analytics/tca/validator/TCAAppConfigValidator.java create mode 100644 dcae-analytics-tca/src/main/java/org/openecomp/dcae/apod/analytics/tca/validator/TCAPolicyPreferencesValidator.java create mode 100644 dcae-analytics-tca/src/main/java/org/openecomp/dcae/apod/analytics/tca/validator/TCAPreferencesValidator.java create mode 100644 dcae-analytics-tca/src/main/java/org/openecomp/dcae/apod/analytics/tca/worker/TCADMaaPMRPublisherJob.java create mode 100644 dcae-analytics-tca/src/main/java/org/openecomp/dcae/apod/analytics/tca/worker/TCADMaaPMRSubscriberJob.java create mode 100644 dcae-analytics-tca/src/main/java/org/openecomp/dcae/apod/analytics/tca/worker/TCADMaaPMockSubscriberWorker.java create mode 100644 dcae-analytics-tca/src/main/java/org/openecomp/dcae/apod/analytics/tca/worker/TCADMaaPPublisherWorker.java create mode 100644 dcae-analytics-tca/src/main/java/org/openecomp/dcae/apod/analytics/tca/worker/TCADMaaPSubscriberWorker.java delete mode 100644 dcae-analytics-tca/src/test/java/org/openecomp/dcae/analytics/tca/BaseAnalyticsTCAIT.java delete mode 100644 dcae-analytics-tca/src/test/java/org/openecomp/dcae/analytics/tca/BaseAnalyticsTCAUnitTest.java delete mode 100644 dcae-analytics-tca/src/test/java/org/openecomp/dcae/analytics/tca/it/TCAnalyticsAppConfigIT.java delete mode 100644 dcae-analytics-tca/src/test/java/org/openecomp/dcae/analytics/tca/processor/TCACEFJsonProcessorTest.java delete mode 100644 dcae-analytics-tca/src/test/java/org/openecomp/dcae/analytics/tca/processor/TCACEFPolicyThresholdsProcessorTest.java delete mode 100644 dcae-analytics-tca/src/test/java/org/openecomp/dcae/analytics/tca/processor/TCACEFProcessorContextTest.java delete mode 100644 dcae-analytics-tca/src/test/java/org/openecomp/dcae/analytics/tca/settings/TCATestAppConfig.java delete mode 100644 dcae-analytics-tca/src/test/java/org/openecomp/dcae/analytics/tca/settings/TCATestAppConfigHolder.java delete mode 100644 dcae-analytics-tca/src/test/java/org/openecomp/dcae/analytics/tca/settings/TCATestAppPreferences.java delete mode 100644 dcae-analytics-tca/src/test/java/org/openecomp/dcae/analytics/tca/utils/AppPreferencesToPublisherConfigMapperTest.java delete mode 100644 dcae-analytics-tca/src/test/java/org/openecomp/dcae/analytics/tca/utils/AppPreferencesToSubscriberConfigMapperTest.java delete mode 100644 dcae-analytics-tca/src/test/java/org/openecomp/dcae/analytics/tca/utils/TCAUtilsTest.java delete mode 100644 dcae-analytics-tca/src/test/java/org/openecomp/dcae/analytics/tca/worker/TCADMaaPMRSubscriberJobTest.java create mode 100644 dcae-analytics-tca/src/test/java/org/openecomp/dcae/apod/analytics/tca/BaseAnalyticsTCAIT.java create mode 100644 dcae-analytics-tca/src/test/java/org/openecomp/dcae/apod/analytics/tca/BaseAnalyticsTCAUnitTest.java create mode 100644 dcae-analytics-tca/src/test/java/org/openecomp/dcae/apod/analytics/tca/it/TCAnalyticsAppConfigIT.java create mode 100644 dcae-analytics-tca/src/test/java/org/openecomp/dcae/apod/analytics/tca/processor/TCACEFJsonProcessorTest.java create mode 100644 dcae-analytics-tca/src/test/java/org/openecomp/dcae/apod/analytics/tca/processor/TCACEFPolicyThresholdsProcessorTest.java create mode 100644 dcae-analytics-tca/src/test/java/org/openecomp/dcae/apod/analytics/tca/processor/TCACEFProcessorContextTest.java create mode 100644 dcae-analytics-tca/src/test/java/org/openecomp/dcae/apod/analytics/tca/settings/TCATestAppConfig.java create mode 100644 dcae-analytics-tca/src/test/java/org/openecomp/dcae/apod/analytics/tca/settings/TCATestAppConfigHolder.java create mode 100644 dcae-analytics-tca/src/test/java/org/openecomp/dcae/apod/analytics/tca/settings/TCATestAppPreferences.java create mode 100644 dcae-analytics-tca/src/test/java/org/openecomp/dcae/apod/analytics/tca/utils/AppPreferencesToPublisherConfigMapperTest.java create mode 100644 dcae-analytics-tca/src/test/java/org/openecomp/dcae/apod/analytics/tca/utils/AppPreferencesToSubscriberConfigMapperTest.java create mode 100644 dcae-analytics-tca/src/test/java/org/openecomp/dcae/apod/analytics/tca/utils/TCAUtilsTest.java create mode 100644 dcae-analytics-tca/src/test/java/org/openecomp/dcae/apod/analytics/tca/worker/TCADMaaPMRSubscriberJobTest.java (limited to 'dcae-analytics-tca/src') diff --git a/dcae-analytics-tca/src/main/java/org/openecomp/dcae/analytics/tca/TCAAnalyticsApplication.java b/dcae-analytics-tca/src/main/java/org/openecomp/dcae/analytics/tca/TCAAnalyticsApplication.java deleted file mode 100644 index 655ddca..0000000 --- a/dcae-analytics-tca/src/main/java/org/openecomp/dcae/analytics/tca/TCAAnalyticsApplication.java +++ /dev/null @@ -1,105 +0,0 @@ -/* - * ============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.analytics.tca; - -import co.cask.cdap.api.app.AbstractApplication; -import co.cask.cdap.api.data.stream.Stream; -import co.cask.cdap.api.dataset.DatasetProperties; -import co.cask.cdap.api.dataset.lib.ObjectMappedTable; -import org.openecomp.dcae.analytics.common.utils.ValidationUtils; -import org.openecomp.dcae.analytics.tca.flow.TCAVESCollectorFlow; -import org.openecomp.dcae.analytics.tca.persistance.TCAMessageStatusPersister; -import org.openecomp.dcae.analytics.tca.persistance.TCAVESAlertsPersister; -import org.openecomp.dcae.analytics.tca.settings.TCAAppConfig; -import org.openecomp.dcae.analytics.tca.validator.TCAAppConfigValidator; -import org.openecomp.dcae.analytics.tca.worker.TCADMaaPMockSubscriberWorker; -import org.openecomp.dcae.analytics.tca.worker.TCADMaaPPublisherWorker; -import org.openecomp.dcae.analytics.tca.worker.TCADMaaPSubscriberWorker; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import static org.openecomp.dcae.analytics.common.CDAPComponentsConstants.TCA_FIXED_SUBSCRIBER_OUTPUT_DESCRIPTION_STREAM; - -/** - * @author Rajiv Singla. Creation Date: 10/21/2016. - */ -public class TCAAnalyticsApplication extends AbstractApplication { - - private static final Logger LOG = LoggerFactory.getLogger(TCAAnalyticsApplication.class); - - @Override - public void configure() { - - - // ========= Application configuration Setup ============== // - final TCAAppConfig tcaAppConfig = getConfig(); - - LOG.info("Configuring TCA Application with startup application configuration: {}", tcaAppConfig); - - // Validate application configuration - ValidationUtils.validateSettings(tcaAppConfig, new TCAAppConfigValidator()); - - // App Setup - setName(tcaAppConfig.getAppName()); - setDescription(tcaAppConfig.getAppDescription()); - - // ========== Streams Setup ============== // - // Create DMaaP MR Subscriber CDAP output stream - final String tcaSubscriberOutputStreamName = tcaAppConfig.getTcaSubscriberOutputStreamName(); - LOG.info("Creating TCA VES Output Stream: {}", tcaSubscriberOutputStreamName); - final Stream subscriberOutputStream = - new Stream(tcaSubscriberOutputStreamName, TCA_FIXED_SUBSCRIBER_OUTPUT_DESCRIPTION_STREAM); - addStream(subscriberOutputStream); - - - // ============ Datasets Setup ======== // - // Create TCA Message Status Table - final String tcaVESMessageStatusTableName = tcaAppConfig.getTcaVESMessageStatusTableName(); - final Integer messageStatusTableTTLSeconds = tcaAppConfig.getTcaVESMessageStatusTableTTLSeconds(); - LOG.info("Creating TCA Message Status Table: {} with TTL: {}", - tcaVESMessageStatusTableName, messageStatusTableTTLSeconds); - final DatasetProperties messageStatusTableProperties = - TCAMessageStatusPersister.getDatasetProperties(messageStatusTableTTLSeconds); - createDataset(tcaVESMessageStatusTableName, ObjectMappedTable.class, messageStatusTableProperties); - - // Create TCA VES Alerts Table - final String tcaVESAlertsTableName = tcaAppConfig.getTcaVESAlertsTableName(); - final Integer alertsTableTTLSeconds = tcaAppConfig.getTcaVESAlertsTableTTLSeconds(); - LOG.info("Creating TCA Alerts Table: {} with TTL: {}", - tcaVESAlertsTableName, alertsTableTTLSeconds); - final DatasetProperties alertTableProperties = - TCAVESAlertsPersister.getDatasetProperties(alertsTableTTLSeconds); - createDataset(tcaVESAlertsTableName, ObjectMappedTable.class, alertTableProperties); - - // =========== Flow Setup ============= // - addFlow(new TCAVESCollectorFlow(tcaAppConfig)); - - // ========== Workers Setup =========== // - LOG.info("Creating TCA DMaaP Subscriber Worker"); - addWorker(new TCADMaaPSubscriberWorker(tcaAppConfig.getTcaSubscriberOutputStreamName())); - LOG.info("Creating TCA DMaaP Publisher Worker"); - addWorker(new TCADMaaPPublisherWorker(tcaAppConfig.getTcaVESAlertsTableName())); - // TODO: Remove this before going to production - addWorker(new TCADMaaPMockSubscriberWorker(tcaAppConfig.getTcaSubscriberOutputStreamName())); - } - - -} diff --git a/dcae-analytics-tca/src/main/java/org/openecomp/dcae/analytics/tca/flow/TCAVESCollectorFlow.java b/dcae-analytics-tca/src/main/java/org/openecomp/dcae/analytics/tca/flow/TCAVESCollectorFlow.java deleted file mode 100644 index 33db3c1..0000000 --- a/dcae-analytics-tca/src/main/java/org/openecomp/dcae/analytics/tca/flow/TCAVESCollectorFlow.java +++ /dev/null @@ -1,69 +0,0 @@ -/* - * ============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.analytics.tca.flow; - -import co.cask.cdap.api.flow.AbstractFlow; -import org.openecomp.dcae.analytics.common.CDAPComponentsConstants; -import org.openecomp.dcae.analytics.tca.flowlet.TCAVESAlertsSinkFlowlet; -import org.openecomp.dcae.analytics.tca.flowlet.TCAVESMessageRouterFlowlet; -import org.openecomp.dcae.analytics.tca.flowlet.TCAVESThresholdViolationCalculatorFlowlet; -import org.openecomp.dcae.analytics.tca.settings.TCAAppConfig; - -/** - * TCA Flow for VES (Virtual Event Streaming) Collector Flow - * - * @author Rajiv Singla. Creation Date: 11/3/2016. - */ -public class TCAVESCollectorFlow extends AbstractFlow { - - private final TCAAppConfig tcaAppConfig; - - public TCAVESCollectorFlow(TCAAppConfig tcaAppConfig) { - this.tcaAppConfig = tcaAppConfig; - } - - @Override - protected void configure() { - - setName(CDAPComponentsConstants.TCA_FIXED_VES_COLLECTOR_NAME_FLOW); - setDescription(CDAPComponentsConstants.TCA_FIXED_VES_COLLECTOR_DESCRIPTION_FLOW); - - final TCAVESMessageRouterFlowlet messageRouterFlowlet = new TCAVESMessageRouterFlowlet(); - addFlowlet(messageRouterFlowlet); - - final TCAVESThresholdViolationCalculatorFlowlet thresholdViolationCalculatorFlowlet = - new TCAVESThresholdViolationCalculatorFlowlet(tcaAppConfig.getTcaVESMessageStatusTableName()); - addFlowlet(thresholdViolationCalculatorFlowlet, tcaAppConfig.getThresholdCalculatorFlowletInstances()); - - final TCAVESAlertsSinkFlowlet alertsSinkFlowlet = - new TCAVESAlertsSinkFlowlet(tcaAppConfig.getTcaVESAlertsTableName()); - addFlowlet(alertsSinkFlowlet); - - - // connect DMaaP MR VES Subscriber output stream to VES Message Router Flowlet - connectStream(tcaAppConfig.getTcaSubscriberOutputStreamName(), messageRouterFlowlet); - // connect message router to VES threshold calculator - connect(messageRouterFlowlet, thresholdViolationCalculatorFlowlet); - // connect VES threshold calculator flowlet to Alerts Sink Flowlet - connect(thresholdViolationCalculatorFlowlet, alertsSinkFlowlet); - - } -} diff --git a/dcae-analytics-tca/src/main/java/org/openecomp/dcae/analytics/tca/flowlet/TCAVESAlertsSinkFlowlet.java b/dcae-analytics-tca/src/main/java/org/openecomp/dcae/analytics/tca/flowlet/TCAVESAlertsSinkFlowlet.java deleted file mode 100644 index 597fcd0..0000000 --- a/dcae-analytics-tca/src/main/java/org/openecomp/dcae/analytics/tca/flowlet/TCAVESAlertsSinkFlowlet.java +++ /dev/null @@ -1,69 +0,0 @@ -/* - * ============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.analytics.tca.flowlet; - -import co.cask.cdap.api.annotation.ProcessInput; -import co.cask.cdap.api.annotation.Property; -import co.cask.cdap.api.dataset.lib.ObjectMappedTable; -import co.cask.cdap.api.flow.flowlet.AbstractFlowlet; -import co.cask.cdap.api.flow.flowlet.FlowletContext; -import org.openecomp.dcae.analytics.tca.persistance.TCAVESAlertEntity; -import org.openecomp.dcae.analytics.tca.persistance.TCAVESAlertsPersister; - -import static org.openecomp.dcae.analytics.common.CDAPComponentsConstants.TCA_FIXED_VES_ALERTS_SINK_DESCRIPTION_FLOWLET; -import static org.openecomp.dcae.analytics.common.CDAPComponentsConstants.TCA_FIXED_VES_ALERTS_SINK_NAME_FLOWLET; -import static org.openecomp.dcae.analytics.common.CDAPComponentsConstants.TCA_FIXED_VES_TCA_CALCULATOR_NAME_OUTPUT; - -/** - * Saves TCA VES Alert Messages in a Time series Table - * - * @author Rajiv Singla. Creation Date: 11/15/2016. - */ -public class TCAVESAlertsSinkFlowlet extends AbstractFlowlet { - - @Property - private final String tcaVESAlertsTableName; - - private ObjectMappedTable tcaVESAlertsTable; - - public TCAVESAlertsSinkFlowlet(String tcaVESAlertsTableName) { - this.tcaVESAlertsTableName = tcaVESAlertsTableName; - } - - @Override - public void configure() { - setName(TCA_FIXED_VES_ALERTS_SINK_NAME_FLOWLET); - setDescription(TCA_FIXED_VES_ALERTS_SINK_DESCRIPTION_FLOWLET); - } - - @Override - public void initialize(FlowletContext flowletContext) throws Exception { - super.initialize(flowletContext); - tcaVESAlertsTable = getContext().getDataset(tcaVESAlertsTableName); - } - - @ProcessInput(TCA_FIXED_VES_TCA_CALCULATOR_NAME_OUTPUT) - public void saveAlerts(String alertMessage) { - // Saves alert message in alerts table - TCAVESAlertsPersister.persist(alertMessage, tcaVESAlertsTable); - } - -} diff --git a/dcae-analytics-tca/src/main/java/org/openecomp/dcae/analytics/tca/flowlet/TCAVESMessageRouterFlowlet.java b/dcae-analytics-tca/src/main/java/org/openecomp/dcae/analytics/tca/flowlet/TCAVESMessageRouterFlowlet.java deleted file mode 100644 index 7ecd917..0000000 --- a/dcae-analytics-tca/src/main/java/org/openecomp/dcae/analytics/tca/flowlet/TCAVESMessageRouterFlowlet.java +++ /dev/null @@ -1,62 +0,0 @@ -/* - * ============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.analytics.tca.flowlet; - -import co.cask.cdap.api.annotation.Output; -import co.cask.cdap.api.annotation.ProcessInput; -import co.cask.cdap.api.flow.flowlet.AbstractFlowlet; -import co.cask.cdap.api.flow.flowlet.OutputEmitter; -import co.cask.cdap.api.flow.flowlet.StreamEvent; -import com.google.common.base.Charsets; - -import static org.openecomp.dcae.analytics.common.AnalyticsConstants.TCA_VES_MESSAGE_ROUTER_PARTITION_KEY; -import static org.openecomp.dcae.analytics.common.CDAPComponentsConstants.TCA_FIXED_VES_MESSAGE_ROUTER_DESCRIPTION_FLOWLET; -import static org.openecomp.dcae.analytics.common.CDAPComponentsConstants.TCA_FIXED_VES_MESSAGE_ROUTER_NAME_FLOWLET; -import static org.openecomp.dcae.analytics.common.CDAPComponentsConstants.TCA_FIXED_VES_MESSAGE_ROUTER_OUTPUT; - - -/** - * TCA Message Router Flowlet emits VES Message to {@link TCAVESThresholdViolationCalculatorFlowlet} instances - * - * @author Rajiv Singla. Creation Date: 11/14/2016. - */ -public class TCAVESMessageRouterFlowlet extends AbstractFlowlet { - - /** - * Emits ves message to TCA Calculator Instances - */ - @Output(TCA_FIXED_VES_MESSAGE_ROUTER_OUTPUT) - private OutputEmitter vesMessageEmitter; - - - @Override - public void configure() { - setName(TCA_FIXED_VES_MESSAGE_ROUTER_NAME_FLOWLET); - setDescription(TCA_FIXED_VES_MESSAGE_ROUTER_DESCRIPTION_FLOWLET); - } - - @ProcessInput - public void routeVESMessage(StreamEvent vesMessageStreamEvent) { - final String vesMessage = Charsets.UTF_8.decode(vesMessageStreamEvent.getBody()).toString(); - vesMessageEmitter.emit(vesMessage, TCA_VES_MESSAGE_ROUTER_PARTITION_KEY, vesMessage - .hashCode()); - } -} diff --git a/dcae-analytics-tca/src/main/java/org/openecomp/dcae/analytics/tca/flowlet/TCAVESThresholdViolationCalculatorFlowlet.java b/dcae-analytics-tca/src/main/java/org/openecomp/dcae/analytics/tca/flowlet/TCAVESThresholdViolationCalculatorFlowlet.java deleted file mode 100644 index d5ab109..0000000 --- a/dcae-analytics-tca/src/main/java/org/openecomp/dcae/analytics/tca/flowlet/TCAVESThresholdViolationCalculatorFlowlet.java +++ /dev/null @@ -1,137 +0,0 @@ -/* - * ============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.analytics.tca.flowlet; - -import co.cask.cdap.api.annotation.Output; -import co.cask.cdap.api.annotation.ProcessInput; -import co.cask.cdap.api.annotation.Property; -import co.cask.cdap.api.annotation.RoundRobin; -import co.cask.cdap.api.dataset.lib.ObjectMappedTable; -import co.cask.cdap.api.flow.flowlet.AbstractFlowlet; -import co.cask.cdap.api.flow.flowlet.FlowletContext; -import co.cask.cdap.api.flow.flowlet.OutputEmitter; -import co.cask.cdap.api.metrics.Metrics; -import com.fasterxml.jackson.core.JsonProcessingException; -import org.openecomp.dcae.analytics.common.CDAPComponentsConstants; -import org.openecomp.dcae.analytics.common.CDAPMetricsConstants; -import org.openecomp.dcae.analytics.model.domain.policy.tca.TCAPolicy; -import org.openecomp.dcae.analytics.model.facade.tca.TCAVESResponse; -import org.openecomp.dcae.analytics.tca.persistance.TCACalculatorMessageType; -import org.openecomp.dcae.analytics.tca.persistance.TCAMessageStatusEntity; -import org.openecomp.dcae.analytics.tca.persistance.TCAMessageStatusPersister; -import org.openecomp.dcae.analytics.tca.processor.TCACEFProcessorContext; -import org.openecomp.dcae.analytics.tca.utils.TCAUtils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import static org.openecomp.dcae.analytics.common.CDAPComponentsConstants.TCA_FIXED_VES_THRESHOLD_VIOLATION_CALCULATOR_DESCRIPTION_FLOWLET; -import static org.openecomp.dcae.analytics.common.CDAPComponentsConstants.TCA_FIXED_VES_THRESHOLD_VIOLATION_CALCULATOR_NAME_FLOWLET; - -/** - * TCA VES Message Filter filters out messages which are not applicable for TCA as per TCA Policy - * - * @author Rajiv Singla. Creation Date: 11/3/2016. - */ -public class TCAVESThresholdViolationCalculatorFlowlet extends AbstractFlowlet { - - private static final Logger LOG = LoggerFactory.getLogger(TCAVESThresholdViolationCalculatorFlowlet.class); - - @Output(CDAPComponentsConstants.TCA_FIXED_VES_TCA_CALCULATOR_NAME_OUTPUT) - private OutputEmitter tcaAlertOutputEmitter; - - @Property - private final String messageStatusTableName; - - private TCAPolicy tcaPolicy; - private Metrics metrics; - private ObjectMappedTable vesMessageStatusTable; - - public TCAVESThresholdViolationCalculatorFlowlet(String messageStatusTableName) { - this.messageStatusTableName = messageStatusTableName; - } - - @Override - public void configure() { - setName(TCA_FIXED_VES_THRESHOLD_VIOLATION_CALCULATOR_NAME_FLOWLET); - setDescription(TCA_FIXED_VES_THRESHOLD_VIOLATION_CALCULATOR_DESCRIPTION_FLOWLET); - } - - - @Override - public void initialize(FlowletContext flowletContext) throws Exception { - super.initialize(flowletContext); - - // parse Runtime Arguments to tca policy preferences - tcaPolicy = TCAUtils.getValidatedTCAPolicyPreferences(flowletContext); - - vesMessageStatusTable = getContext().getDataset(messageStatusTableName); - - } - - @ProcessInput(CDAPComponentsConstants.TCA_FIXED_VES_MESSAGE_ROUTER_OUTPUT) - @RoundRobin - public void filterVESMessages(String vesMessage) throws JsonProcessingException { - - TCACalculatorMessageType calculatorMessageType = TCACalculatorMessageType.INAPPLICABLE; - String alertMessage = null; - - // Step 1: Filter incoming messages - final TCACEFProcessorContext processorContext = TCAUtils.filterCEFMessage(vesMessage, tcaPolicy); - - if (processorContext.canProcessingContinue()) { - - // Step 2: Check if CEF Message violate any thresholds - final TCACEFProcessorContext processorContextWithViolations = - TCAUtils.computeThresholdViolations(processorContext); - - if (processorContextWithViolations.canProcessingContinue()) { - - // Step 3: Create Alert Message - final String tcaAppName = getContext().getApplicationSpecification().getName(); - final TCAVESResponse newTCAVESResponse = - TCAUtils.createNewTCAVESResponse(processorContextWithViolations, tcaAppName); - alertMessage = TCAUtils.writeValueAsString(newTCAVESResponse); - calculatorMessageType = TCACalculatorMessageType.NON_COMPLIANT; - LOG.debug("VES Threshold Violation Detected. An alert message is be generated. {}", alertMessage); - - metrics.count(CDAPMetricsConstants.TCA_VES_NON_COMPLIANT_MESSAGES_METRIC, 1); - - // Step 4: Emit message to Alert Sink Flowlet - tcaAlertOutputEmitter.emit(alertMessage); - - } else { - - calculatorMessageType = TCACalculatorMessageType.COMPLIANT; - metrics.count(CDAPMetricsConstants.TCA_VES_COMPLIANT_MESSAGES_METRIC, 1); - } - - } else { - - metrics.count(CDAPMetricsConstants.TCA_VES_INAPPLICABLE_MESSAGES_METRIC, 1); - } - - // save message to message status table - TCAMessageStatusPersister.persist( - processorContext, getContext(), calculatorMessageType, vesMessageStatusTable, alertMessage); - } - - -} diff --git a/dcae-analytics-tca/src/main/java/org/openecomp/dcae/analytics/tca/persistance/TCACalculatorMessageType.java b/dcae-analytics-tca/src/main/java/org/openecomp/dcae/analytics/tca/persistance/TCACalculatorMessageType.java deleted file mode 100644 index acef458..0000000 --- a/dcae-analytics-tca/src/main/java/org/openecomp/dcae/analytics/tca/persistance/TCACalculatorMessageType.java +++ /dev/null @@ -1,43 +0,0 @@ -/* - * ============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.analytics.tca.persistance; - -/** - * TCA Calculator applies TCA Policy to incoming VES messages and classifies them as per this enum - * - * @author Rajiv Singla. Creation Date: 11/15/2016. - */ -public enum TCACalculatorMessageType { - - /** - * VES messages that are not applicable as per TCA Policy - */ - INAPPLICABLE, - /** - * VES messages that are applicable as per TCA Policy but don't violate any thresholds - */ - COMPLIANT, - /** - * VES messages that are applicable as per TCA Policy and also in violation of TCA Policy thresholds - */ - NON_COMPLIANT; - -} diff --git a/dcae-analytics-tca/src/main/java/org/openecomp/dcae/analytics/tca/persistance/TCAMessageStatusEntity.java b/dcae-analytics-tca/src/main/java/org/openecomp/dcae/analytics/tca/persistance/TCAMessageStatusEntity.java deleted file mode 100644 index 35aeac4..0000000 --- a/dcae-analytics-tca/src/main/java/org/openecomp/dcae/analytics/tca/persistance/TCAMessageStatusEntity.java +++ /dev/null @@ -1,306 +0,0 @@ -/* - * ============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.analytics.tca.persistance; - -import org.apache.hadoop.io.Writable; -import org.apache.hadoop.io.WritableUtils; - -import java.io.DataInput; -import java.io.DataOutput; -import java.io.IOException; -import java.io.Serializable; - -/** - * TCA Message Status is an Entity which is used to persist TCA VES Message status information in Message Status Table - * - * @author Rajiv Singla. Creation Date: 11/16/2016. - */ -public class TCAMessageStatusEntity implements Writable, Serializable { - - private static final long serialVersionUID = -6186607668704516962L; - - private long creationTS; - private int flowletInstance; - private String messageType; - private String vesMessage; - private String domain; - private String functionalRole; - private String thresholdPath; - private String thresholdSeverity; - private String thresholdDirection; - private Long thresholdValue; - private String jsonProcessorStatus; - private String jsonProcessorMessage; - private String domainFilterStatus; - private String domainFilterMessage; - private String functionalRoleFilterStatus; - private String functionalRoleFilterMessage; - private String thresholdCalculatorStatus; - private String thresholdCalculatorMessage; - private String alertMessage; - - public TCAMessageStatusEntity() { - } - - public TCAMessageStatusEntity(long creationTS, int flowletInstance, String messageType, String vesMessage, String - domain, String functionalRole) { - this(creationTS, flowletInstance, messageType, vesMessage, domain, functionalRole, null, null, null, null, - null, null, null, null, null, null, null, null, null); - } - - public TCAMessageStatusEntity(long creationTS, int flowletInstance, String messageType, String vesMessage, - String domain, String functionalRole, - String thresholdPath, String thresholdSeverity, String thresholdDirection, - Long thresholdValue, - String jsonProcessorStatus, String jsonProcessorMessage, - String domainFilterStatus, String domainFilterMessage, - String functionalRoleFilterStatus, String functionalRoleFilterMessage, - String thresholdCalculatorStatus, String thresholdCalculatorMessage, - String alertMessage) { - this.creationTS = creationTS; - this.flowletInstance = flowletInstance; - this.messageType = messageType; - this.vesMessage = vesMessage; - this.domain = domain; - this.functionalRole = functionalRole; - this.thresholdPath = thresholdPath; - this.thresholdSeverity = thresholdSeverity; - this.thresholdDirection = thresholdDirection; - this.thresholdValue = thresholdValue; - this.jsonProcessorStatus = jsonProcessorStatus; - this.jsonProcessorMessage = jsonProcessorMessage; - this.domainFilterStatus = domainFilterStatus; - this.domainFilterMessage = domainFilterMessage; - this.functionalRoleFilterStatus = functionalRoleFilterStatus; - this.functionalRoleFilterMessage = functionalRoleFilterMessage; - this.thresholdCalculatorStatus = thresholdCalculatorStatus; - this.thresholdCalculatorMessage = thresholdCalculatorMessage; - this.alertMessage = alertMessage; - } - - public long getCreationTS() { - return creationTS; - } - - public void setCreationTS(long creationTS) { - this.creationTS = creationTS; - } - - public int getFlowletInstance() { - return flowletInstance; - } - - public void setFlowletInstance(int flowletInstance) { - this.flowletInstance = flowletInstance; - } - - public String getMessageType() { - return messageType; - } - - public void setMessageType(String messageType) { - this.messageType = messageType; - } - - public String getVesMessage() { - return vesMessage; - } - - public void setVesMessage(String vesMessage) { - this.vesMessage = vesMessage; - } - - public String getDomain() { - return domain; - } - - public void setDomain(String domain) { - this.domain = domain; - } - - public String getFunctionalRole() { - return functionalRole; - } - - public void setFunctionalRole(String functionalRole) { - this.functionalRole = functionalRole; - } - - public String getThresholdPath() { - return thresholdPath; - } - - public void setThresholdPath(String thresholdPath) { - this.thresholdPath = thresholdPath; - } - - public String getThresholdSeverity() { - return thresholdSeverity; - } - - public void setThresholdSeverity(String thresholdSeverity) { - this.thresholdSeverity = thresholdSeverity; - } - - public String getThresholdDirection() { - return thresholdDirection; - } - - public void setThresholdDirection(String thresholdDirection) { - this.thresholdDirection = thresholdDirection; - } - - public Long getThresholdValue() { - return thresholdValue; - } - - public void setThresholdValue(Long thresholdValue) { - this.thresholdValue = thresholdValue; - } - - public String getJsonProcessorStatus() { - return jsonProcessorStatus; - } - - public void setJsonProcessorStatus(String jsonProcessorStatus) { - this.jsonProcessorStatus = jsonProcessorStatus; - } - - public String getJsonProcessorMessage() { - return jsonProcessorMessage; - } - - public void setJsonProcessorMessage(String jsonProcessorMessage) { - this.jsonProcessorMessage = jsonProcessorMessage; - } - - public String getDomainFilterStatus() { - return domainFilterStatus; - } - - public void setDomainFilterStatus(String domainFilterStatus) { - this.domainFilterStatus = domainFilterStatus; - } - - public String getDomainFilterMessage() { - return domainFilterMessage; - } - - public void setDomainFilterMessage(String domainFilterMessage) { - this.domainFilterMessage = domainFilterMessage; - } - - public String getFunctionalRoleFilterStatus() { - return functionalRoleFilterStatus; - } - - public void setFunctionalRoleFilterStatus(String functionalRoleFilterStatus) { - this.functionalRoleFilterStatus = functionalRoleFilterStatus; - } - - public String getFunctionalRoleFilterMessage() { - return functionalRoleFilterMessage; - } - - public void setFunctionalRoleFilterMessage(String functionalRoleFilterMessage) { - this.functionalRoleFilterMessage = functionalRoleFilterMessage; - } - - public String getThresholdCalculatorStatus() { - return thresholdCalculatorStatus; - } - - public void setThresholdCalculatorStatus(String thresholdCalculatorStatus) { - this.thresholdCalculatorStatus = thresholdCalculatorStatus; - } - - public String getThresholdCalculatorMessage() { - return thresholdCalculatorMessage; - } - - public void setThresholdCalculatorMessage(String thresholdCalculatorMessage) { - this.thresholdCalculatorMessage = thresholdCalculatorMessage; - } - - public String getAlertMessage() { - return alertMessage; - } - - public void setAlertMessage(String alertMessage) { - this.alertMessage = alertMessage; - } - - @Override - public void write(DataOutput dataOutput) throws IOException { - WritableUtils.writeVLong(dataOutput, creationTS); - WritableUtils.writeVInt(dataOutput, flowletInstance); - WritableUtils.writeString(dataOutput, messageType); - WritableUtils.writeString(dataOutput, vesMessage); - - WritableUtils.writeString(dataOutput, domain); - WritableUtils.writeString(dataOutput, functionalRole); - - WritableUtils.writeString(dataOutput, thresholdPath); - WritableUtils.writeString(dataOutput, thresholdSeverity); - WritableUtils.writeString(dataOutput, thresholdDirection); - WritableUtils.writeVLong(dataOutput, thresholdValue); - - WritableUtils.writeString(dataOutput, jsonProcessorStatus); - WritableUtils.writeString(dataOutput, jsonProcessorMessage); - WritableUtils.writeString(dataOutput, domainFilterStatus); - WritableUtils.writeString(dataOutput, domainFilterMessage); - WritableUtils.writeString(dataOutput, functionalRoleFilterStatus); - WritableUtils.writeString(dataOutput, functionalRoleFilterMessage); - WritableUtils.writeString(dataOutput, thresholdCalculatorStatus); - WritableUtils.writeString(dataOutput, thresholdCalculatorMessage); - - WritableUtils.writeString(dataOutput, alertMessage); - - } - - @Override - public void readFields(DataInput dataInput) throws IOException { - creationTS = WritableUtils.readVLong(dataInput); - flowletInstance = WritableUtils.readVInt(dataInput); - messageType = WritableUtils.readString(dataInput); - vesMessage = WritableUtils.readString(dataInput); - - domain = WritableUtils.readString(dataInput); - functionalRole = WritableUtils.readString(dataInput); - - thresholdPath = WritableUtils.readString(dataInput); - thresholdSeverity = WritableUtils.readString(dataInput); - thresholdDirection = WritableUtils.readString(dataInput); - thresholdValue = WritableUtils.readVLong(dataInput); - - jsonProcessorStatus = WritableUtils.readString(dataInput); - jsonProcessorMessage = WritableUtils.readString(dataInput); - domainFilterStatus = WritableUtils.readString(dataInput); - domainFilterMessage = WritableUtils.readString(dataInput); - functionalRoleFilterStatus = WritableUtils.readString(dataInput); - functionalRoleFilterMessage = WritableUtils.readString(dataInput); - thresholdCalculatorStatus = WritableUtils.readString(dataInput); - thresholdCalculatorMessage = WritableUtils.readString(dataInput); - - alertMessage = WritableUtils.readString(dataInput); - - } -} diff --git a/dcae-analytics-tca/src/main/java/org/openecomp/dcae/analytics/tca/persistance/TCAMessageStatusPersister.java b/dcae-analytics-tca/src/main/java/org/openecomp/dcae/analytics/tca/persistance/TCAMessageStatusPersister.java deleted file mode 100644 index edd33dd..0000000 --- a/dcae-analytics-tca/src/main/java/org/openecomp/dcae/analytics/tca/persistance/TCAMessageStatusPersister.java +++ /dev/null @@ -1,243 +0,0 @@ -/* - * ============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.analytics.tca.persistance; - -import co.cask.cdap.api.data.schema.Schema; -import co.cask.cdap.api.data.schema.UnsupportedTypeException; -import co.cask.cdap.api.dataset.DatasetProperties; -import co.cask.cdap.api.dataset.lib.IndexedTable; -import co.cask.cdap.api.dataset.lib.ObjectMappedTable; -import co.cask.cdap.api.dataset.lib.ObjectMappedTableProperties; -import co.cask.cdap.api.flow.flowlet.FlowletContext; -import com.google.common.base.Joiner; -import org.apache.commons.lang3.StringEscapeUtils; -import org.apache.commons.lang3.tuple.Pair; -import org.openecomp.dcae.analytics.common.exception.DCAEAnalyticsRuntimeException; -import org.openecomp.dcae.analytics.common.service.processor.MessageProcessor; -import org.openecomp.dcae.analytics.common.service.processor.ProcessorContext; -import org.openecomp.dcae.analytics.common.utils.PersistenceUtils; -import org.openecomp.dcae.analytics.model.domain.policy.tca.MetricsPerFunctionalRole; -import org.openecomp.dcae.analytics.model.domain.policy.tca.Threshold; -import org.openecomp.dcae.analytics.tca.processor.TCACEFJsonProcessor; -import org.openecomp.dcae.analytics.tca.processor.TCACEFPolicyDomainFilter; -import org.openecomp.dcae.analytics.tca.processor.TCACEFPolicyFunctionalRoleFilter; -import org.openecomp.dcae.analytics.tca.processor.TCACEFPolicyThresholdsProcessor; -import org.openecomp.dcae.analytics.tca.processor.TCACEFProcessorContext; -import org.openecomp.dcae.analytics.tca.utils.TCAUtils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.util.Date; -import java.util.LinkedList; -import java.util.List; - -import javax.annotation.Nullable; - -import static org.openecomp.dcae.analytics.common.CDAPComponentsConstants.TCA_FIXED_VES_MESSAGE_STATUS_DESCRIPTION_TABLE; -import static org.openecomp.dcae.analytics.common.utils.PersistenceUtils.TABLE_ROW_KEY_COLUMN_NAME; - -/** - * - * - * @author Rajiv Singla. Creation Date: 11/15/2016. - */ -public abstract class TCAMessageStatusPersister { - - private static final Logger LOG = LoggerFactory.getLogger(TCAMessageStatusPersister.class); - - /** - * Saves Message Status in Table. Assumes no alert was generated - * - * @param processorContext processor Context - * @param flowletContext Flowlet Context - * @param calculatorMessageType Calculation Message Type - * @param messageStatusTable Message Status Table - */ - public static void persist(final TCACEFProcessorContext processorContext, - final FlowletContext flowletContext, - final TCACalculatorMessageType calculatorMessageType, - final ObjectMappedTable messageStatusTable) { - persist(processorContext, flowletContext, calculatorMessageType, messageStatusTable, null); - } - - /** - * Saves Message Status in Table. Sets up alert message aslo - * - * @param processorContext processor Context - * @param flowletContext Flowlet Context - * @param calculatorMessageType Calculation Message Type - * @param messageStatusTable Message Status Table - * @param alertMessage Alert message - */ - public static void persist(final TCACEFProcessorContext processorContext, - final FlowletContext flowletContext, - final TCACalculatorMessageType calculatorMessageType, - final ObjectMappedTable messageStatusTable, - @Nullable final String alertMessage) { - - final String rowKey = createKey(calculatorMessageType); - - final Long currentTS = new Date().getTime(); - final int flowletInstanceId = flowletContext.getInstanceId(); - final String vesMessage = StringEscapeUtils.unescapeJson(processorContext.getMessage()); - - // Find Functional Role and domain - final Pair domainAndFunctionalRole = TCAUtils.getDomainAndFunctionalRole(processorContext); - final String domain = domainAndFunctionalRole.getLeft(); - final String functionalRole = domainAndFunctionalRole.getRight(); - - final TCAMessageStatusEntity tcaMessageStatusEntity = new TCAMessageStatusEntity(currentTS, - flowletInstanceId, calculatorMessageType.name(), vesMessage, domain, functionalRole); - - // add threshold violation fields - addViolatedThreshold(tcaMessageStatusEntity, processorContext); - // add processor status and messages - addMessageProcessorMessages(tcaMessageStatusEntity, processorContext); - // add Alert message - tcaMessageStatusEntity.setAlertMessage( - alertMessage == null ? null : StringEscapeUtils.unescapeJson(alertMessage) - ); - - messageStatusTable.write(rowKey, tcaMessageStatusEntity); - - - LOG.debug("Finished persisting VES Status Message with rowKey: {} in Message Status Table.", rowKey); - - } - - - /** - * Create TCA VES Message Status Table Properties - * - * @param timeToLiveSeconds Message Status Table time to live in seconds - * - * @return Message Status table properties - */ - public static DatasetProperties getDatasetProperties(final int timeToLiveSeconds) { - - try { - return ObjectMappedTableProperties.builder() - .setType(TCAMessageStatusEntity.class) - .setRowKeyExploreName(TABLE_ROW_KEY_COLUMN_NAME) - .setRowKeyExploreType(Schema.Type.STRING) - .add(IndexedTable.PROPERTY_TTL, timeToLiveSeconds) - .setDescription(TCA_FIXED_VES_MESSAGE_STATUS_DESCRIPTION_TABLE) - .build(); - } catch (UnsupportedTypeException e) { - final String errorMessage = "Unable to convert TCAMessageStatusEntity class to Schema"; - throw new DCAEAnalyticsRuntimeException(errorMessage, LOG, new IllegalArgumentException(errorMessage)); - } - - } - - - /** - * Adds Violated Threshold Parameter values to {@link TCAMessageStatusEntity} - * - * @param tcaMessageStatusEntity message entity that needs to be populated with threshold fields - * @param processorContext processor context - * - * @return entity with populated threshold field values if present - */ - private static TCAMessageStatusEntity addViolatedThreshold(final TCAMessageStatusEntity tcaMessageStatusEntity, - final TCACEFProcessorContext processorContext) { - - final MetricsPerFunctionalRole metricsPerFunctionalRole = processorContext.getMetricsPerFunctionalRole(); - - if (metricsPerFunctionalRole != null - && metricsPerFunctionalRole.getThresholds() != null - && metricsPerFunctionalRole.getThresholds().get(0) != null) { - - final Threshold threshold = metricsPerFunctionalRole.getThresholds().get(0); - tcaMessageStatusEntity.setThresholdPath(threshold.getFieldPath()); - tcaMessageStatusEntity.setThresholdSeverity(threshold.getSeverity().name()); - tcaMessageStatusEntity.setThresholdDirection(threshold.getDirection().name()); - tcaMessageStatusEntity.setThresholdValue(threshold.getThresholdValue()); - } - - return tcaMessageStatusEntity; - } - - - /** - * Add TCA CEF Message Processor status information - * - * @param tcaMessageStatusEntity message entity that needs to be populated with message processor fields - * @param processorContext processor context - * - * @return entity with populated message process status information - */ - private static TCAMessageStatusEntity addMessageProcessorMessages( - final TCAMessageStatusEntity tcaMessageStatusEntity, final TCACEFProcessorContext processorContext) { - final List> messageProcessors = processorContext - .getMessageProcessors(); - - if (messageProcessors != null && !messageProcessors.isEmpty()) { - for (Object messageProcessor : messageProcessors) { - final MessageProcessor tcaMessageProcessor = - (MessageProcessor) messageProcessor; - - final String processingState = tcaMessageProcessor.getProcessingState().name(); - final String processingMessage = tcaMessageProcessor.getProcessingMessage().orNull(); - - if (messageProcessor.getClass().equals(TCACEFJsonProcessor.class)) { - tcaMessageStatusEntity.setJsonProcessorStatus(processingState); - tcaMessageStatusEntity.setJsonProcessorMessage(processingMessage); - } - - if (messageProcessor.getClass().equals(TCACEFPolicyDomainFilter.class)) { - tcaMessageStatusEntity.setDomainFilterStatus(processingState); - tcaMessageStatusEntity.setDomainFilterMessage(processingMessage); - } - - if (messageProcessor.getClass().equals(TCACEFPolicyFunctionalRoleFilter.class)) { - tcaMessageStatusEntity.setFunctionalRoleFilterStatus(processingState); - tcaMessageStatusEntity.setFunctionalRoleFilterMessage(processingMessage); - } - - if (messageProcessor.getClass().equals(TCACEFPolicyThresholdsProcessor.class)) { - tcaMessageStatusEntity.setThresholdCalculatorStatus(processingState); - tcaMessageStatusEntity.setThresholdCalculatorMessage(processingMessage); - } - - } - } - return tcaMessageStatusEntity; - } - - /** - * Creates Row Key for TCA VES Message Status table - * - * Row Key = (Message Type + Decreasing Value) - * - * @param calculatorMessageType calculator message type - * - * @return row key string - */ - private static String createKey(final TCACalculatorMessageType calculatorMessageType) { - - final List keyList = new LinkedList<>(); - keyList.add(calculatorMessageType.name()); - keyList.add(PersistenceUtils.getCurrentTimeReverseSubKey()); - return Joiner.on(PersistenceUtils.ROW_KEY_DELIMITER).join(keyList); - } - -} diff --git a/dcae-analytics-tca/src/main/java/org/openecomp/dcae/analytics/tca/persistance/TCAVESAlertEntity.java b/dcae-analytics-tca/src/main/java/org/openecomp/dcae/analytics/tca/persistance/TCAVESAlertEntity.java deleted file mode 100644 index ff3906c..0000000 --- a/dcae-analytics-tca/src/main/java/org/openecomp/dcae/analytics/tca/persistance/TCAVESAlertEntity.java +++ /dev/null @@ -1,77 +0,0 @@ -/* - * ============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.analytics.tca.persistance; - -import org.apache.hadoop.io.Writable; -import org.apache.hadoop.io.WritableUtils; - -import java.io.DataInput; -import java.io.DataOutput; -import java.io.IOException; -import java.io.Serializable; - -/** - * - * @author Rajiv Singla. Creation Date: 11/16/2016. - */ -public class TCAVESAlertEntity implements Writable, Serializable { - - private static final long serialVersionUID = -8587229518063660258L; - - private long creationTS; - private String alertMessage; - - public TCAVESAlertEntity() { - } - - public TCAVESAlertEntity(long creationTS, String alertMessage) { - this.creationTS = creationTS; - this.alertMessage = alertMessage; - } - - public long getCreationTS() { - return creationTS; - } - - public void setCreationTS(long creationTS) { - this.creationTS = creationTS; - } - - public String getAlertMessage() { - return alertMessage; - } - - public void setAlertMessage(String alertMessage) { - this.alertMessage = alertMessage; - } - - @Override - public void write(DataOutput dataOutput) throws IOException { - WritableUtils.writeVLong(dataOutput, creationTS); - WritableUtils.writeString(dataOutput, alertMessage); - } - - @Override - public void readFields(DataInput dataInput) throws IOException { - creationTS = WritableUtils.readVLong(dataInput); - alertMessage = WritableUtils.readString(dataInput); - } -} diff --git a/dcae-analytics-tca/src/main/java/org/openecomp/dcae/analytics/tca/persistance/TCAVESAlertsPersister.java b/dcae-analytics-tca/src/main/java/org/openecomp/dcae/analytics/tca/persistance/TCAVESAlertsPersister.java deleted file mode 100644 index bf94cde..0000000 --- a/dcae-analytics-tca/src/main/java/org/openecomp/dcae/analytics/tca/persistance/TCAVESAlertsPersister.java +++ /dev/null @@ -1,98 +0,0 @@ -/* - * ============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.analytics.tca.persistance; - -import co.cask.cdap.api.data.schema.Schema; -import co.cask.cdap.api.data.schema.UnsupportedTypeException; -import co.cask.cdap.api.dataset.DatasetProperties; -import co.cask.cdap.api.dataset.lib.IndexedTable; -import co.cask.cdap.api.dataset.lib.ObjectMappedTable; -import co.cask.cdap.api.dataset.lib.ObjectMappedTableProperties; -import org.apache.commons.lang3.StringEscapeUtils; -import org.openecomp.dcae.analytics.common.exception.DCAEAnalyticsRuntimeException; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.util.Date; - -import static org.openecomp.dcae.analytics.common.CDAPComponentsConstants.TCA_DEFAULT_VES_ALERTS_DESCRIPTION_TABLE; -import static org.openecomp.dcae.analytics.common.utils.PersistenceUtils.TABLE_ROW_KEY_COLUMN_NAME; - -/** - * - * @author Rajiv Singla. Creation Date: 11/16/2016. - */ -public abstract class TCAVESAlertsPersister { - - private static final Logger LOG = LoggerFactory.getLogger(TCAVESAlertsPersister.class); - - /** - * Persists Alert Message to Alerts Table - * - * @param alertMessage alert Message - * @param tcaVESAlertTable alert Table Name - */ - public static void persist(final String alertMessage, final ObjectMappedTable tcaVESAlertTable) { - final Date currentDate = new Date(); - final TCAVESAlertEntity alertEntity = new TCAVESAlertEntity(currentDate.getTime(), - StringEscapeUtils.unescapeJson(alertMessage)); - // row key is same as current timestamp - final String rowKey = createRowKey(currentDate); - tcaVESAlertTable.write(rowKey, alertEntity); - - LOG.debug("Finished persisting VES Alert message ID: {} in VES Alerts table.", rowKey); - } - - - /** - * Creates {@link DatasetProperties} for Alerts Table - * - * @param timeToLiveSeconds alerts table Time to Live - * @return Alerts table properties - */ - public static DatasetProperties getDatasetProperties(final int timeToLiveSeconds) { - try { - return ObjectMappedTableProperties.builder() - .setType(TCAVESAlertEntity.class) - .setRowKeyExploreName(TABLE_ROW_KEY_COLUMN_NAME) - .setRowKeyExploreType(Schema.Type.STRING) - .add(IndexedTable.PROPERTY_TTL, timeToLiveSeconds) - .setDescription(TCA_DEFAULT_VES_ALERTS_DESCRIPTION_TABLE) - .build(); - } catch (UnsupportedTypeException e) { - final String errorMessage = "Unable to convert TCAVESAlertEntity class to Schema"; - throw new DCAEAnalyticsRuntimeException(errorMessage, LOG, new IllegalArgumentException(errorMessage)); - } - - } - - /** - * Creates Row Key for Alerts Table - * - * @param date current Date - * - * @return row key - */ - public static String createRowKey(final Date date) { - return String.format("%025d", date.getTime()); - } - -} diff --git a/dcae-analytics-tca/src/main/java/org/openecomp/dcae/analytics/tca/processor/AbstractTCAECEFPolicyProcessor.java b/dcae-analytics-tca/src/main/java/org/openecomp/dcae/analytics/tca/processor/AbstractTCAECEFPolicyProcessor.java deleted file mode 100644 index 56ea38f..0000000 --- a/dcae-analytics-tca/src/main/java/org/openecomp/dcae/analytics/tca/processor/AbstractTCAECEFPolicyProcessor.java +++ /dev/null @@ -1,61 +0,0 @@ -/* - * ============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.analytics.tca.processor; - -import org.openecomp.dcae.analytics.common.exception.MessageProcessingException; -import org.openecomp.dcae.analytics.common.service.processor.AbstractMessageProcessor; -import org.openecomp.dcae.analytics.model.domain.cef.EventListener; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import javax.annotation.Nonnull; - -/** - *

- * Encapsulates common functionality for all TCA CEF Policy Processors - *

- * - * @author Rajiv Singla. Creation Date: 11/9/2016. - */ -public abstract class AbstractTCAECEFPolicyProcessor extends AbstractMessageProcessor { - - private static final Logger LOG = LoggerFactory.getLogger(AbstractTCAECEFPolicyProcessor.class); - - /** - * For all TCA Policy Processor the pre processor ensures that {@link EventListener} object is - * present - * - * @param processorContext incoming Processor Context - * @return Pre processed Processor Context - */ - @Override - public TCACEFProcessorContext preProcessor(@Nonnull TCACEFProcessorContext processorContext) { - // validates CEF Event Listener is Present - final EventListener cefEventListener = processorContext.getCEFEventListener(); - if (cefEventListener == null) { - final String errorMessage = String.format( - "CEF Event Listener is not Present.Invalid use of Processor: %s. CEF Message: %s", - getProcessorInfo().getProcessorName(), processorContext.getMessage()); - throw new MessageProcessingException(errorMessage, LOG, new IllegalArgumentException(errorMessage)); - } - return super.preProcessor(processorContext); - } -} diff --git a/dcae-analytics-tca/src/main/java/org/openecomp/dcae/analytics/tca/processor/TCACEFJsonProcessor.java b/dcae-analytics-tca/src/main/java/org/openecomp/dcae/analytics/tca/processor/TCACEFJsonProcessor.java deleted file mode 100644 index ab33fe2..0000000 --- a/dcae-analytics-tca/src/main/java/org/openecomp/dcae/analytics/tca/processor/TCACEFJsonProcessor.java +++ /dev/null @@ -1,98 +0,0 @@ -/* - * ============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.analytics.tca.processor; - -import org.apache.commons.lang3.StringUtils; -import org.openecomp.dcae.analytics.common.exception.MessageProcessingException; -import org.openecomp.dcae.analytics.common.service.processor.AbstractMessageProcessor; -import org.openecomp.dcae.analytics.model.domain.cef.EventListener; -import org.openecomp.dcae.analytics.tca.utils.TCAUtils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.io.IOException; - -/** - *

- * Processor that converts incoming presumed JSON string CEF message to {@link EventListener} object - *
- * Pre Conditions: None - *

- * - * @author Rajiv Singla. Creation Date: 11/5/2016. - */ -public class TCACEFJsonProcessor extends AbstractMessageProcessor { - - - private static final long serialVersionUID = -9031531679383821793L; - - private static final Logger LOG = LoggerFactory.getLogger(TCACEFJsonProcessor.class); - - - @Override - public String getProcessorDescription() { - return "Converts incoming TCA CEF Message to Event Listener object"; - } - - @Override - public TCACEFProcessorContext processMessage(TCACEFProcessorContext processorContext) { - - final String cefMessage = processorContext.getMessage(); - - // If CEF Message is null then processor should stop processing - if (cefMessage == null) { - String errorMessage = "Null CEF message cannot be converted to CEF Event Listener Object"; - throw new MessageProcessingException(errorMessage, LOG, new IllegalArgumentException(errorMessage)); - } - - // If CEF Message is blank then processor stop processing - if (StringUtils.isBlank(cefMessage)) { - setTerminatingProcessingMessage("Blank CEF message cannot be converted to CEF Event Listener Object", - processorContext); - return processorContext; - } - - // trim cef message - final String trimmedCEFMessage = cefMessage.trim(); - - // if message does not start with curly brace and ends with curly brace, it is not a valid cef message - // processor will stop processing - if (!(trimmedCEFMessage.startsWith("{") && trimmedCEFMessage.endsWith("}"))) { - setTerminatingProcessingMessage("CEF Message must start with curly brace and must end with curly brace", - processorContext); - return processorContext; - } - - // try parsing the cef message - try { - final EventListener eventListener = TCAUtils.readValue(trimmedCEFMessage, EventListener.class); - setFinishedProcessingMessage("CEF JSON to Event Listener Conversion Successful", processorContext); - // set new Event Listener in the Processor Context - processorContext.setCEFEventListener(eventListener); - return processorContext; - } catch (IOException e) { - final String errorMessage = String.format("Parsing Failed for CEF Message: %s, Error: %s", cefMessage, e); - // If parsing fails throw an exception - throw new MessageProcessingException(errorMessage, LOG, e); - } - - } -} diff --git a/dcae-analytics-tca/src/main/java/org/openecomp/dcae/analytics/tca/processor/TCACEFPolicyDomainFilter.java b/dcae-analytics-tca/src/main/java/org/openecomp/dcae/analytics/tca/processor/TCACEFPolicyDomainFilter.java deleted file mode 100644 index 639c6bc..0000000 --- a/dcae-analytics-tca/src/main/java/org/openecomp/dcae/analytics/tca/processor/TCACEFPolicyDomainFilter.java +++ /dev/null @@ -1,83 +0,0 @@ -/* - * ============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.analytics.tca.processor; - -import org.openecomp.dcae.analytics.model.domain.cef.EventListener; - -/** - *

- * TCA Processor which acts like a filter to filter out messages which does not belong to TCA Policy Domain - *
- * Pre Conditions: CEF Event Listener must be present - *

- * - * @author Rajiv Singla. Creation Date: 11/7/2016. - */ -public class TCACEFPolicyDomainFilter extends AbstractTCAECEFPolicyProcessor { - - - private static final long serialVersionUID = -7302116478207544378L; - - @Override - public String getProcessorDescription() { - return "Filters out CEF Messages which does not match TCAPolicy Domain"; - } - - @Override - public TCACEFProcessorContext processMessage(TCACEFProcessorContext processorContext) { - - // Safe to get event Listener here without null check as pre processor will validate if - // event listener is indeed present - final EventListener eventListener = processorContext.getCEFEventListener(); - - String cefMessageDomain; - - // Extract CEF domain as it is must be present as per CEF Schema - if (eventListener.getEvent() != null && - eventListener.getEvent().getCommonEventHeader() != null && - eventListener.getEvent().getCommonEventHeader().getDomain() != null) { - cefMessageDomain = eventListener.getEvent().getCommonEventHeader().getDomain(); - - } else { - final String terminatingMessage = "Invalid CEF Message.Common Event Header Domain not present."; - setTerminatingProcessingMessage(terminatingMessage, processorContext); - return processorContext; - } - - // Get Policy Domain. TCA Policy Validation must ensure that Domain is indeed present - // no null check will be required here - final String policyDomain = processorContext.getTCAPolicy().getDomain(); - - // If Policy domain matches CEF message domain then continue processing - if (cefMessageDomain.equals(policyDomain)) { - final String finishMessage = String.format("Policy Domain and CEF Message Domain match successful." + - " Message Domain: %s, Policy Domain: %s", cefMessageDomain, policyDomain); - setFinishedProcessingMessage(finishMessage, processorContext); - } else { - // If policy domain does not match with CEF message terminate processing chain - final String terminatingMessage = String.format("Policy Domain and CEF Message Domain match unsuccessful." + - " Message Domain: %s, Policy Domain: %s", cefMessageDomain, policyDomain); - setTerminatingProcessingMessage(terminatingMessage, processorContext); - } - - return processorContext; - } -} diff --git a/dcae-analytics-tca/src/main/java/org/openecomp/dcae/analytics/tca/processor/TCACEFPolicyFunctionalRoleFilter.java b/dcae-analytics-tca/src/main/java/org/openecomp/dcae/analytics/tca/processor/TCACEFPolicyFunctionalRoleFilter.java deleted file mode 100644 index 0f7e9b6..0000000 --- a/dcae-analytics-tca/src/main/java/org/openecomp/dcae/analytics/tca/processor/TCACEFPolicyFunctionalRoleFilter.java +++ /dev/null @@ -1,90 +0,0 @@ -/* - * ============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.analytics.tca.processor; - -import com.google.common.base.Joiner; -import org.openecomp.dcae.analytics.model.domain.cef.EventListener; -import org.openecomp.dcae.analytics.model.domain.policy.tca.TCAPolicy; -import org.openecomp.dcae.analytics.tca.utils.TCAUtils; - -import java.util.List; - -/** - *

- * TCA Processor that acts like a filter to filter out messages which does not belong to TCA Policy Functional Roles - *
- * Pre Conditions: CEF Event Listener must be present - *

- * - * @author Rajiv Singla. Creation Date: 11/9/2016. - */ -public class TCACEFPolicyFunctionalRoleFilter extends AbstractTCAECEFPolicyProcessor { - - private static final long serialVersionUID = -4550787922375040396L; - - @Override - public String getProcessorDescription() { - return "Filters out CEF Messages which does not match Policy Functional Roles"; - } - - @Override - public TCACEFProcessorContext processMessage(TCACEFProcessorContext processorContext) { - - // Safe to get event Listener here without null check as pre processor will validate if - // event listener is indeed present - final EventListener eventListener = processorContext.getCEFEventListener(); - - String cefMessageFunctionalRole; - - if (eventListener.getEvent() != null && - eventListener.getEvent().getCommonEventHeader() != null && - eventListener.getEvent().getCommonEventHeader().getFunctionalRole() != null) { - cefMessageFunctionalRole = eventListener.getEvent().getCommonEventHeader().getFunctionalRole(); - } else { - String terminationMessage = "Invalid CEF Message.Common Event Header Functional Role not present."; - setTerminatingProcessingMessage(terminationMessage, processorContext); - return processorContext; - } - - // Determine Policy Functional Roles - final TCAPolicy tcaPolicy = processorContext.getTCAPolicy(); - final List policyFunctionalRoles = TCAUtils.getPolicyFunctionalRoleSupplier(tcaPolicy).get(); - final String policyFunctionalRolesString = Joiner.on(",").join(policyFunctionalRoles); - - // If Policy functional Roles contains CEF message Functional Role then continue processing - if (policyFunctionalRoles.contains(cefMessageFunctionalRole)) { - final String finishMessage = String.format( - "Policy Functional Roles and CEF Message Functional match successful." + - "Message Functional Role: %s, Policy Functional Roles: %s", - cefMessageFunctionalRole, policyFunctionalRolesString); - setFinishedProcessingMessage(finishMessage, processorContext); - } else { - // If Policy functional Roles does not contain CEF message Functiona Role then terminate processing - final String terminatingMessage = String.format( - "Policy Domain and CEF Message Domain match unsuccessful." + - "Message Functional Role: %s, Policy Functional Roles: %s", - cefMessageFunctionalRole, policyFunctionalRolesString); - setTerminatingProcessingMessage(terminatingMessage, processorContext); - } - - return processorContext; - } -} diff --git a/dcae-analytics-tca/src/main/java/org/openecomp/dcae/analytics/tca/processor/TCACEFPolicyThresholdsProcessor.java b/dcae-analytics-tca/src/main/java/org/openecomp/dcae/analytics/tca/processor/TCACEFPolicyThresholdsProcessor.java deleted file mode 100644 index 6dd334f..0000000 --- a/dcae-analytics-tca/src/main/java/org/openecomp/dcae/analytics/tca/processor/TCACEFPolicyThresholdsProcessor.java +++ /dev/null @@ -1,135 +0,0 @@ -/* - * ============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.analytics.tca.processor; - -import com.google.common.base.Optional; -import com.google.common.collect.Table; -import org.openecomp.dcae.analytics.common.exception.MessageProcessingException; -import org.openecomp.dcae.analytics.model.domain.cef.EventListener; -import org.openecomp.dcae.analytics.model.domain.policy.tca.MetricsPerFunctionalRole; -import org.openecomp.dcae.analytics.model.domain.policy.tca.TCAPolicy; -import org.openecomp.dcae.analytics.model.domain.policy.tca.Threshold; -import org.openecomp.dcae.analytics.tca.utils.TCAUtils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Set; - -import javax.annotation.Nonnull; - -/** - *

- * TCA CEF Policy Threshold processor - *
- * Pre Conditions: Domain and Functional Role must be present in CEF Event Listener Object - *

- * - * @author Rajiv Singla. Creation Date: 11/9/2016. - */ -public class TCACEFPolicyThresholdsProcessor extends AbstractTCAECEFPolicyProcessor { - - private static final long serialVersionUID = -9075212160268522029L; - - private static final Logger LOG = LoggerFactory.getLogger(TCACEFPolicyThresholdsProcessor.class); - - @Override - public TCACEFProcessorContext preProcessor(@Nonnull TCACEFProcessorContext processorContext) { - // validates Domain and Functional Role are present - final EventListener eventListener = processorContext.getCEFEventListener(); - final String domain = eventListener.getEvent().getCommonEventHeader().getDomain(); - final String functionalRole = eventListener.getEvent().getCommonEventHeader().getFunctionalRole(); - if (domain == null || functionalRole == null) { - final String errorMessage = "CEF Event Listener domain or functional role not Present. " + - "Invalid use of this Processor"; - throw new MessageProcessingException(errorMessage, LOG, new IllegalArgumentException(errorMessage)); - } - return super.preProcessor(processorContext); - } - - @Override - public String getProcessorDescription() { - return "Applies TCA Policy rules to incoming CEF message. If any thresholds are violated attaches max " + - "Severity violated threshold to TCA Processor Context"; - } - - @Override - public TCACEFProcessorContext processMessage(TCACEFProcessorContext processorContext) { - - final String cefMessage = processorContext.getMessage(); - - // Determine domain and functional Role - final EventListener eventListener = processorContext.getCEFEventListener(); - final String functionalRole = eventListener.getEvent().getCommonEventHeader().getFunctionalRole(); - - // Get Table containing Functional Role and Thresholds Field Path - final TCAPolicy tcaPolicy = processorContext.getTCAPolicy(); - final Table> functionalRoleFieldPathsTable = - TCAUtils.getPolicyFRThresholdsTableSupplier(tcaPolicy).get(); - - // Get Policy Field Paths for that functional Role - final Map> policyFieldPathsMap = functionalRoleFieldPathsTable.row(functionalRole); - final Set policyFieldPaths = policyFieldPathsMap.keySet(); - - // Get Json Values for Policy Fields - final Map> messageFieldValuesMap = TCAUtils.getJsonPathValue(cefMessage, policyFieldPaths); - - // Determine all violated thresholds per message field Path - final Map violatedThresholdsMap = new HashMap<>(); - for (Map.Entry> messageFieldValuesMapEntry : messageFieldValuesMap.entrySet()) { - final String messageFieldPath = messageFieldValuesMapEntry.getKey(); - final List messageFieldAssociatedPolicyThresholds = policyFieldPathsMap.get(messageFieldPath); - if (messageFieldAssociatedPolicyThresholds != null) { - final Optional thresholdOptional = TCAUtils.thresholdCalculator( - messageFieldValuesMapEntry.getValue(), messageFieldAssociatedPolicyThresholds); - if (thresholdOptional.isPresent()) { - violatedThresholdsMap.put(messageFieldPath, thresholdOptional.get()); - } - } - } - - // No threshold were violated - if (violatedThresholdsMap.isEmpty()) { - - final String terminationMessage = "No Policy Threshold violated by the VES CEF Message."; - setTerminatingProcessingMessage(terminationMessage, processorContext); - - } else { - - // If there are policy violations then determine max priority violation - final Threshold maxSeverityThresholdViolation = - TCAUtils.prioritizeThresholdViolations(violatedThresholdsMap); - final MetricsPerFunctionalRole violatedMetrics = TCAUtils.createViolatedMetrics(tcaPolicy, - maxSeverityThresholdViolation, functionalRole); - // attach policy violation to processor Context - processorContext.setMetricsPerFunctionalRole(violatedMetrics); - - final String finishMessage = String.format("Policy Threshold violation detected for threshold: %s", - maxSeverityThresholdViolation); - setFinishedProcessingMessage(finishMessage, processorContext); - - } - - return processorContext; - } -} diff --git a/dcae-analytics-tca/src/main/java/org/openecomp/dcae/analytics/tca/processor/TCACEFProcessorContext.java b/dcae-analytics-tca/src/main/java/org/openecomp/dcae/analytics/tca/processor/TCACEFProcessorContext.java deleted file mode 100644 index 30ebe9d..0000000 --- a/dcae-analytics-tca/src/main/java/org/openecomp/dcae/analytics/tca/processor/TCACEFProcessorContext.java +++ /dev/null @@ -1,103 +0,0 @@ -/* - * ============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.analytics.tca.processor; - -import org.openecomp.dcae.analytics.common.service.processor.AbstractProcessorContext; -import org.openecomp.dcae.analytics.model.domain.cef.EventListener; -import org.openecomp.dcae.analytics.model.domain.policy.tca.MetricsPerFunctionalRole; -import org.openecomp.dcae.analytics.model.domain.policy.tca.TCAPolicy; - -/** - * TCA CEF Policy Processor Context - * - * @author Rajiv Singla. Creation Date: 11/7/2016. - */ -public class TCACEFProcessorContext extends AbstractProcessorContext { - - private static final long serialVersionUID = 1165992675657025413L; - - private final TCAPolicy tcaPolicy; - private EventListener eventListener; - private MetricsPerFunctionalRole metricsPerFunctionalRole; - - public TCACEFProcessorContext(final String message, boolean canProcessingContinue, final TCAPolicy tcaPolicy) { - super(message, canProcessingContinue); - this.tcaPolicy = tcaPolicy; - // present only if cef incoming message can be parsed successfully to Event Listener Object - this.eventListener = null; - // present only if there are any threshold violations are detected - this.metricsPerFunctionalRole = null; - } - - // Auxiliary Constructor which default canProcessingContinue Flag to true - public TCACEFProcessorContext(final String message, final TCAPolicy tcaPolicy) { - this(message, true, tcaPolicy); - } - - /** - * Returns {@link TCAPolicy} Object - * - * @return TCA Policy - */ - public TCAPolicy getTCAPolicy() { - return tcaPolicy; - } - - /** - * Returns Common Event Format {@link EventListener} if present else null - * - * @return CEF Event Listener - */ - public EventListener getCEFEventListener() { - return eventListener; - } - - - /** - * Sets new {@link EventListener} - * - * @param eventListener set new value for CEF event listener - */ - public void setCEFEventListener(final EventListener eventListener) { - this.eventListener = eventListener; - } - - - /** - * Returns TCA Policy {@link MetricsPerFunctionalRole} which was has violated Threshold for the CEF Message if - * present else null - * - * @return Violated Threshold Optional - */ - public MetricsPerFunctionalRole getMetricsPerFunctionalRole() { - return metricsPerFunctionalRole; - } - - /** - * Assign new TCA Policy {@link MetricsPerFunctionalRole} which was has violated Threshold for the CEF Message - * - * @param metricsPerFunctionalRole new value for Metrics Per Functional Role with violated threshold - */ - public void setMetricsPerFunctionalRole(MetricsPerFunctionalRole metricsPerFunctionalRole) { - this.metricsPerFunctionalRole = metricsPerFunctionalRole; - } - -} diff --git a/dcae-analytics-tca/src/main/java/org/openecomp/dcae/analytics/tca/settings/TCAAppConfig.java b/dcae-analytics-tca/src/main/java/org/openecomp/dcae/analytics/tca/settings/TCAAppConfig.java deleted file mode 100644 index 1f7c32d..0000000 --- a/dcae-analytics-tca/src/main/java/org/openecomp/dcae/analytics/tca/settings/TCAAppConfig.java +++ /dev/null @@ -1,96 +0,0 @@ -/* - * ============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.analytics.tca.settings; - -import com.google.common.base.Objects; -import org.openecomp.dcae.analytics.common.AnalyticsConstants; -import org.openecomp.dcae.analytics.common.CDAPComponentsConstants; -import org.openecomp.dcae.analytics.common.cdap.settings.CDAPBaseAppConfig; - - -/** - * Contains CDAP App Config Settings for TCA Application - * - * @author Rajiv Singla. Creation Date: 11/2/2016. - */ -public class TCAAppConfig extends CDAPBaseAppConfig { - - - private static final long serialVersionUID = -6775885444808279443L; - - private String tcaSubscriberOutputStreamName; - private Integer thresholdCalculatorFlowletInstances; - - private String tcaVESMessageStatusTableName; - private Integer tcaVESMessageStatusTableTTLSeconds; - private String tcaVESAlertsTableName; - private Integer tcaVESAlertsTableTTLSeconds; - - - public TCAAppConfig() { - appName = CDAPComponentsConstants.TCA_DEFAULT_NAME_APP; - appDescription = CDAPComponentsConstants.TCA_DEFAULT_DESCRIPTION_APP; - tcaSubscriberOutputStreamName = CDAPComponentsConstants.TCA_DEFAULT_SUBSCRIBER_OUTPUT_NAME_STREAM; - thresholdCalculatorFlowletInstances = AnalyticsConstants.TCA_DEFAULT_THRESHOLD_CALCULATOR_FLOWLET_INSTANCES; - tcaVESMessageStatusTableName = CDAPComponentsConstants.TCA_DEFAULT_VES_MESSAGE_STATUS_NAME_TABLE; - tcaVESMessageStatusTableTTLSeconds = AnalyticsConstants.TCA_DEFAULT_VES_MESSAGE_STATUS_TTL_TABLE; - tcaVESAlertsTableName = CDAPComponentsConstants.TCA_DEFAULT_VES_ALERTS_NAME_TABLE; - tcaVESAlertsTableTTLSeconds = AnalyticsConstants.TCA_DEFAULT_VES_ALERTS_TTL_TABLE; - } - - public String getTcaSubscriberOutputStreamName() { - return tcaSubscriberOutputStreamName; - } - - public String getTcaVESMessageStatusTableName() { - return tcaVESMessageStatusTableName; - } - - public Integer getTcaVESMessageStatusTableTTLSeconds() { - return tcaVESMessageStatusTableTTLSeconds; - } - - public String getTcaVESAlertsTableName() { - return tcaVESAlertsTableName; - } - - public Integer getTcaVESAlertsTableTTLSeconds() { - return tcaVESAlertsTableTTLSeconds; - } - - public Integer getThresholdCalculatorFlowletInstances() { - return thresholdCalculatorFlowletInstances; - } - - @Override - public String toString() { - return Objects.toStringHelper(this) - .add("appName", appName) - .add("appDescription", appDescription) - .add("tcaSubscriberOutputStreamName", tcaSubscriberOutputStreamName) - .add("thresholdCalculatorFlowletInstances", thresholdCalculatorFlowletInstances) - .add("tcaVESMessageStatusTableName", tcaVESMessageStatusTableName) - .add("tcaVESMessageStatusTableTTLSeconds", tcaVESMessageStatusTableTTLSeconds) - .add("tcaVESAlertsTableName", tcaVESAlertsTableName) - .add("tcaVESAlertsTableTTLSeconds", tcaVESAlertsTableTTLSeconds) - .toString(); - } -} diff --git a/dcae-analytics-tca/src/main/java/org/openecomp/dcae/analytics/tca/settings/TCAAppPreferences.java b/dcae-analytics-tca/src/main/java/org/openecomp/dcae/analytics/tca/settings/TCAAppPreferences.java deleted file mode 100644 index 8eddb3c..0000000 --- a/dcae-analytics-tca/src/main/java/org/openecomp/dcae/analytics/tca/settings/TCAAppPreferences.java +++ /dev/null @@ -1,262 +0,0 @@ -/* - * ============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.analytics.tca.settings; - -import com.google.common.base.Objects; -import org.openecomp.dcae.analytics.common.cdap.settings.CDAPAppPreferences; - -import static org.openecomp.dcae.analytics.common.AnalyticsConstants.TCA_DEFAULT_PUBLISHER_MAX_BATCH_QUEUE_SIZE; -import static org.openecomp.dcae.analytics.common.AnalyticsConstants.TCA_DEFAULT_PUBLISHER_MAX_RECOVERY_QUEUE_SIZE; -import static org.openecomp.dcae.analytics.common.AnalyticsConstants.TCA_DEFAULT_PUBLISHER_POLLING_INTERVAL_MS; -import static org.openecomp.dcae.analytics.common.AnalyticsConstants.TCA_DEFAULT_SUBSCRIBER_POLLING_INTERVAL_MS; - -/** - *

- * App Preferences for Analytics TCA (Threshold Crossing Alert) App - *

- * @author Rajiv Singla. Creation Date: 10/4/2016. - */ -public class TCAAppPreferences implements CDAPAppPreferences { - - private static final long serialVersionUID = -685010752560182764L; - - // subscriber preferences - protected String subscriberHostName; - - protected Integer subscriberHostPort; - - protected String subscriberTopicName; - - protected String subscriberProtocol; - - protected String subscriberUserName; - - protected String subscriberUserPassword; - - protected String subscriberContentType; - - protected String subscriberConsumerId; - - protected String subscriberConsumerGroup; - - protected Integer subscriberTimeoutMS; - - protected Integer subscriberMessageLimit; - - protected Integer subscriberPollingInterval; - - // publisher preferences - protected String publisherHostName; - - protected Integer publisherHostPort; - - protected String publisherTopicName; - - protected String publisherProtocol; - - protected String publisherUserName; - - protected String publisherUserPassword; - - protected String publisherContentType; - - protected Integer publisherMaxBatchSize; - - protected Integer publisherMaxRecoveryQueueSize; - - protected Integer publisherPollingInterval; - - - public TCAAppPreferences() { - - // subscriber defaults - subscriberPollingInterval = TCA_DEFAULT_SUBSCRIBER_POLLING_INTERVAL_MS; - - // publisher defaults - publisherMaxBatchSize = TCA_DEFAULT_PUBLISHER_MAX_BATCH_QUEUE_SIZE; - publisherMaxRecoveryQueueSize = TCA_DEFAULT_PUBLISHER_MAX_RECOVERY_QUEUE_SIZE; - publisherPollingInterval = TCA_DEFAULT_PUBLISHER_POLLING_INTERVAL_MS; - - } - - public String getSubscriberHostName() { - return subscriberHostName; - } - - public Integer getSubscriberHostPort() { - return subscriberHostPort; - } - - public String getSubscriberTopicName() { - return subscriberTopicName; - } - - public String getSubscriberProtocol() { - return subscriberProtocol; - } - - public String getSubscriberUserName() { - return subscriberUserName; - } - - public String getSubscriberUserPassword() { - return subscriberUserPassword; - } - - public String getSubscriberContentType() { - return subscriberContentType; - } - - public String getSubscriberConsumerId() { - return subscriberConsumerId; - } - - public String getSubscriberConsumerGroup() { - return subscriberConsumerGroup; - } - - public Integer getSubscriberTimeoutMS() { - return subscriberTimeoutMS; - } - - public Integer getSubscriberMessageLimit() { - return subscriberMessageLimit; - } - - public Integer getSubscriberPollingInterval() { - return subscriberPollingInterval; - } - - public String getPublisherHostName() { - return publisherHostName; - } - - public Integer getPublisherHostPort() { - return publisherHostPort; - } - - public String getPublisherTopicName() { - return publisherTopicName; - } - - public String getPublisherProtocol() { - return publisherProtocol; - } - - public String getPublisherUserName() { - return publisherUserName; - } - - public String getPublisherUserPassword() { - return publisherUserPassword; - } - - public String getPublisherContentType() { - return publisherContentType; - } - - public Integer getPublisherMaxBatchSize() { - return publisherMaxBatchSize; - } - - public Integer getPublisherMaxRecoveryQueueSize() { - return publisherMaxRecoveryQueueSize; - } - - public Integer getPublisherPollingInterval() { - return publisherPollingInterval; - } - - - public void setSubscriberHostName(String subscriberHostName) { - this.subscriberHostName = subscriberHostName; - } - - public void setSubscriberHostPort(Integer subscriberHostPort) { - this.subscriberHostPort = subscriberHostPort; - } - - public void setSubscriberTopicName(String subscriberTopicName) { - this.subscriberTopicName = subscriberTopicName; - } - - public void setSubscriberProtocol(String subscriberProtocol) { - this.subscriberProtocol = subscriberProtocol; - } - - public void setSubscriberUserName(String subscriberUserName) { - this.subscriberUserName = subscriberUserName; - } - - public void setSubscriberUserPassword(String subscriberUserPassword) { - this.subscriberUserPassword = subscriberUserPassword; - } - - public void setPublisherHostName(String publisherHostName) { - this.publisherHostName = publisherHostName; - } - - public void setPublisherHostPort(Integer publisherHostPort) { - this.publisherHostPort = publisherHostPort; - } - - public void setPublisherTopicName(String publisherTopicName) { - this.publisherTopicName = publisherTopicName; - } - - public void setPublisherProtocol(String publisherProtocol) { - this.publisherProtocol = publisherProtocol; - } - - public void setPublisherUserName(String publisherUserName) { - this.publisherUserName = publisherUserName; - } - - public void setPublisherUserPassword(String publisherUserPassword) { - this.publisherUserPassword = publisherUserPassword; - } - - @Override - public String toString() { - return Objects.toStringHelper(this) - .add("subscriberHostName", subscriberHostName) - .add("subscriberHostPort", subscriberHostPort) - .add("subscriberTopicName", subscriberTopicName) - .add("subscriberProtocol", subscriberProtocol) - .add("subscriberUserName", subscriberUserName) - .add("subscriberContentType", subscriberContentType) - .add("subscriberConsumerId", subscriberConsumerId) - .add("subscriberConsumerGroup", subscriberConsumerGroup) - .add("subscriberTimeoutMS", subscriberTimeoutMS) - .add("subscriberMessageLimit", subscriberMessageLimit) - .add("subscriberPollingInterval", subscriberPollingInterval) - .add("publisherHostName", publisherHostName) - .add("publisherHostPort", publisherHostPort) - .add("publisherTopicName", publisherTopicName) - .add("publisherProtocol", publisherProtocol) - .add("publisherUserName", publisherUserName) - .add("publisherContentType", publisherContentType) - .add("publisherMaxBatchSize", publisherMaxBatchSize) - .add("publisherMaxRecoveryQueueSize", publisherMaxRecoveryQueueSize) - .add("publisherPollingInterval", publisherPollingInterval) - .toString(); - } -} diff --git a/dcae-analytics-tca/src/main/java/org/openecomp/dcae/analytics/tca/settings/TCAPolicyPreferences.java b/dcae-analytics-tca/src/main/java/org/openecomp/dcae/analytics/tca/settings/TCAPolicyPreferences.java deleted file mode 100644 index cbbf550..0000000 --- a/dcae-analytics-tca/src/main/java/org/openecomp/dcae/analytics/tca/settings/TCAPolicyPreferences.java +++ /dev/null @@ -1,36 +0,0 @@ -/* - * ============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.analytics.tca.settings; - -import org.openecomp.dcae.analytics.common.cdap.settings.CDAPAppPreferences; -import org.openecomp.dcae.analytics.model.domain.policy.tca.TCAPolicy; - -/** - * A wrapper over {@link TCAPolicy} to act as app Preferences as TCA Policy is passed - * by controller as runtime arguments from CDAP app preferences - *

- * @author Rajiv Singla. Creation Date: 11/29/2016. - */ -public class TCAPolicyPreferences extends TCAPolicy implements CDAPAppPreferences { - - private static final long serialVersionUID = -2957485388641574043L; - -} diff --git a/dcae-analytics-tca/src/main/java/org/openecomp/dcae/analytics/tca/utils/AppPreferencesToPublisherConfigMapper.java b/dcae-analytics-tca/src/main/java/org/openecomp/dcae/analytics/tca/utils/AppPreferencesToPublisherConfigMapper.java deleted file mode 100644 index f332681..0000000 --- a/dcae-analytics-tca/src/main/java/org/openecomp/dcae/analytics/tca/utils/AppPreferencesToPublisherConfigMapper.java +++ /dev/null @@ -1,82 +0,0 @@ -/* - * ============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.analytics.tca.utils; - -import com.google.common.base.Function; -import org.openecomp.dcae.analytics.dmaap.domain.config.DMaaPMRPublisherConfig; -import org.openecomp.dcae.analytics.tca.settings.TCAAppPreferences; - -import javax.annotation.Nonnull; - -import static org.openecomp.dcae.analytics.common.utils.ValidationUtils.isEmpty; - -/** - * Function which translates {@link TCAAppPreferences} to {@link DMaaPMRPublisherConfig} - * - * @author Rajiv Singla. Creation Date: 11/17/2016. - */ -public class AppPreferencesToPublisherConfigMapper implements Function { - - public static DMaaPMRPublisherConfig map(final TCAAppPreferences tcaAppPreferences) { - return new AppPreferencesToPublisherConfigMapper().apply(tcaAppPreferences); - } - - @Nonnull - @Override - public DMaaPMRPublisherConfig apply(@Nonnull TCAAppPreferences tcaAppPreferences) { - - // Create a new publisher settings builder - final DMaaPMRPublisherConfig.Builder publisherConfigBuilder = new DMaaPMRPublisherConfig.Builder( - tcaAppPreferences.getPublisherHostName(), tcaAppPreferences.getPublisherTopicName()); - - // Setup up any optional publisher parameters if they are present - final Integer publisherHostPort = tcaAppPreferences.getPublisherHostPort(); - if (publisherHostPort != null) { - publisherConfigBuilder.setPortNumber(publisherHostPort); - } - final String publisherProtocol = tcaAppPreferences.getPublisherProtocol(); - if (!isEmpty(publisherProtocol)) { - publisherConfigBuilder.setProtocol(publisherProtocol); - } - final String publisherUserName = tcaAppPreferences.getPublisherUserName(); - if (!isEmpty(publisherUserName)) { - publisherConfigBuilder.setUserName(publisherUserName); - } - final String publisherUserPassword = tcaAppPreferences.getPublisherUserPassword(); - if (!isEmpty(publisherUserPassword)) { - publisherConfigBuilder.setUserPassword(publisherUserPassword); - } - final String publisherContentType = tcaAppPreferences.getPublisherContentType(); - if (!isEmpty(publisherContentType)) { - publisherConfigBuilder.setContentType(publisherContentType); - } - final Integer publisherMaxBatchSize = tcaAppPreferences.getPublisherMaxBatchSize(); - if (publisherMaxBatchSize != null) { - publisherConfigBuilder.setMaxBatchSize(publisherMaxBatchSize); - } - final Integer publisherMaxRecoveryQueueSize = tcaAppPreferences.getPublisherMaxRecoveryQueueSize(); - if (publisherMaxRecoveryQueueSize != null) { - publisherConfigBuilder.setMaxRecoveryQueueSize(publisherMaxRecoveryQueueSize); - } - - return publisherConfigBuilder.build(); - } -} diff --git a/dcae-analytics-tca/src/main/java/org/openecomp/dcae/analytics/tca/utils/AppPreferencesToSubscriberConfigMapper.java b/dcae-analytics-tca/src/main/java/org/openecomp/dcae/analytics/tca/utils/AppPreferencesToSubscriberConfigMapper.java deleted file mode 100644 index d0e7f1f..0000000 --- a/dcae-analytics-tca/src/main/java/org/openecomp/dcae/analytics/tca/utils/AppPreferencesToSubscriberConfigMapper.java +++ /dev/null @@ -1,99 +0,0 @@ -/* - * ============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.analytics.tca.utils; - -import com.google.common.base.Function; -import org.openecomp.dcae.analytics.dmaap.domain.config.DMaaPMRSubscriberConfig; -import org.openecomp.dcae.analytics.tca.settings.TCAAppPreferences; - -import javax.annotation.Nonnull; - -import static org.openecomp.dcae.analytics.common.utils.ValidationUtils.isEmpty; - -/** - * Function which translates {@link TCAAppPreferences} to {@link DMaaPMRSubscriberConfig} - * - * @author Rajiv Singla. Creation Date: 11/17/2016. - */ -public class AppPreferencesToSubscriberConfigMapper implements Function { - - public static DMaaPMRSubscriberConfig map(final TCAAppPreferences tcaAppPreferences) { - return new AppPreferencesToSubscriberConfigMapper().apply(tcaAppPreferences); - } - - @Nonnull - @Override - public DMaaPMRSubscriberConfig apply(@Nonnull TCAAppPreferences tcaAppPreferences) { - - // Create a new subscriber settings builder - final DMaaPMRSubscriberConfig.Builder subscriberConfigBuilder = new DMaaPMRSubscriberConfig.Builder( - tcaAppPreferences.getSubscriberHostName(), tcaAppPreferences.getSubscriberTopicName()); - - // Setup up any optional subscriber parameters if they are present - final Integer subscriberHostPortNumber = tcaAppPreferences.getSubscriberHostPort(); - if (subscriberHostPortNumber != null) { - subscriberConfigBuilder.setPortNumber(subscriberHostPortNumber); - } - - final String subscriberProtocol = tcaAppPreferences.getSubscriberProtocol(); - if (!isEmpty(subscriberProtocol)) { - subscriberConfigBuilder.setProtocol(subscriberProtocol); - } - - final String subscriberUserName = tcaAppPreferences.getSubscriberUserName(); - if (!isEmpty(subscriberUserName)) { - subscriberConfigBuilder.setUserName(subscriberUserName); - } - - final String subscriberUserPassword = tcaAppPreferences.getSubscriberUserPassword(); - if (!isEmpty(subscriberUserPassword)) { - subscriberConfigBuilder.setUserPassword(subscriberUserPassword); - } - - final String subscriberContentType = tcaAppPreferences.getSubscriberContentType(); - if (!isEmpty(subscriberContentType)) { - subscriberConfigBuilder.setContentType(subscriberContentType); - } - - final String subscriberConsumerId = tcaAppPreferences.getSubscriberConsumerId(); - if (!isEmpty(subscriberConsumerId)) { - subscriberConfigBuilder.setConsumerId(subscriberConsumerId); - } - - final String subscriberConsumerGroup = tcaAppPreferences.getSubscriberConsumerGroup(); - if (!isEmpty(subscriberConsumerGroup)) { - subscriberConfigBuilder.setConsumerGroup(subscriberConsumerGroup); - } - - final Integer subscriberTimeoutMS = tcaAppPreferences.getSubscriberTimeoutMS(); - if (subscriberTimeoutMS != null) { - subscriberConfigBuilder.setTimeoutMS(subscriberTimeoutMS); - } - final Integer subscriberMessageLimit = tcaAppPreferences.getSubscriberMessageLimit(); - if (subscriberMessageLimit != null) { - subscriberConfigBuilder.setMessageLimit(subscriberMessageLimit); - } - - // return Subscriber settings - return subscriberConfigBuilder.build(); - - } -} diff --git a/dcae-analytics-tca/src/main/java/org/openecomp/dcae/analytics/tca/utils/TCAUtils.java b/dcae-analytics-tca/src/main/java/org/openecomp/dcae/analytics/tca/utils/TCAUtils.java deleted file mode 100644 index 27df27f..0000000 --- a/dcae-analytics-tca/src/main/java/org/openecomp/dcae/analytics/tca/utils/TCAUtils.java +++ /dev/null @@ -1,825 +0,0 @@ -/* - * ============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.analytics.tca.utils; - -import co.cask.cdap.api.RuntimeContext; -import com.google.common.base.Function; -import com.google.common.base.Optional; -import com.google.common.base.Predicate; -import com.google.common.base.Predicates; -import com.google.common.base.Supplier; -import com.google.common.base.Suppliers; -import com.google.common.collect.HashBasedTable; -import com.google.common.collect.ImmutableList; -import com.google.common.collect.Iterables; -import com.google.common.collect.Lists; -import com.google.common.collect.Maps; -import com.google.common.collect.Table; -import com.jayway.jsonpath.DocumentContext; -import com.jayway.jsonpath.JsonPath; -import com.jayway.jsonpath.TypeRef; -import org.apache.commons.lang3.StringUtils; -import org.apache.commons.lang3.tuple.ImmutablePair; -import org.apache.commons.lang3.tuple.Pair; -import org.openecomp.dcae.analytics.common.AnalyticsConstants; -import org.openecomp.dcae.analytics.common.exception.DCAEAnalyticsRuntimeException; -import org.openecomp.dcae.analytics.common.exception.MessageProcessingException; -import org.openecomp.dcae.analytics.common.service.processor.AbstractMessageProcessor; -import org.openecomp.dcae.analytics.common.service.processor.GenericMessageChainProcessor; -import org.openecomp.dcae.analytics.model.domain.cef.CommonEventHeader; -import org.openecomp.dcae.analytics.model.domain.cef.EventListener; -import org.openecomp.dcae.analytics.model.domain.cef.EventSeverity; -import org.openecomp.dcae.analytics.model.domain.policy.tca.Direction; -import org.openecomp.dcae.analytics.model.domain.policy.tca.MetricsPerFunctionalRole; -import org.openecomp.dcae.analytics.model.domain.policy.tca.TCAPolicy; -import org.openecomp.dcae.analytics.model.domain.policy.tca.Threshold; -import org.openecomp.dcae.analytics.model.facade.tca.AAI; -import org.openecomp.dcae.analytics.model.facade.tca.TCAVESResponse; -import org.openecomp.dcae.analytics.model.util.AnalyticsModelJsonUtils; -import org.openecomp.dcae.analytics.tca.persistance.TCAVESAlertEntity; -import org.openecomp.dcae.analytics.tca.processor.TCACEFJsonProcessor; -import org.openecomp.dcae.analytics.tca.processor.TCACEFPolicyDomainFilter; -import org.openecomp.dcae.analytics.tca.processor.TCACEFPolicyFunctionalRoleFilter; -import org.openecomp.dcae.analytics.tca.processor.TCACEFPolicyThresholdsProcessor; -import org.openecomp.dcae.analytics.tca.processor.TCACEFProcessorContext; -import org.openecomp.dcae.analytics.tca.settings.TCAAppPreferences; -import org.openecomp.dcae.analytics.tca.settings.TCAPolicyPreferences; -import org.openecomp.dcae.analytics.tca.validator.TCAPolicyPreferencesValidator; -import org.openecomp.dcae.analytics.tca.validator.TCAPreferencesValidator; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.net.MalformedURLException; -import java.net.URL; -import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; -import java.util.Comparator; -import java.util.HashMap; -import java.util.LinkedHashMap; -import java.util.LinkedList; -import java.util.List; -import java.util.Map; -import java.util.Set; -import java.util.SortedMap; -import java.util.TreeMap; -import java.util.UUID; - -import javax.annotation.Nonnull; -import javax.annotation.Nullable; - -import static com.google.common.collect.Lists.newArrayList; -import static org.openecomp.dcae.analytics.common.AnalyticsConstants.TCA_POLICY_METRICS_PER_FUNCTIONAL_ROLE_PATH; -import static org.openecomp.dcae.analytics.common.utils.ValidationUtils.validateSettings; - -/** - * Utility Helper methods for TCA sub module only. Extends {@link AnalyticsModelJsonUtils} to get - * pre configured Json Object Mapper understand serialization and deserialization of CEF Message - * and TCA Policy - * - * @author Rajiv Singla. Creation Date: 10/24/2016. - */ -public abstract class TCAUtils extends AnalyticsModelJsonUtils { - - private static final Logger LOG = LoggerFactory.getLogger(TCAUtils.class); - - /** - * Threshold Comparator which is used to order thresholds based on their severity e.g. ( CRITICAL, MAJOR, MINOR, - * WARNING ) - */ - private static final Comparator THRESHOLD_COMPARATOR = new Comparator() { - @Override - public int compare(Threshold threshold1, Threshold threshold2) { - return threshold1.getSeverity().compareTo(threshold2.getSeverity()); - } - }; - - - /** - * {@link Function} that extracts {@link TCAPolicy#getMetricsPerFunctionalRole()} from {@link TCAPolicy} - * - * @return TCA Policy Metrics Per Functional Roles List - */ - public static Function> tcaPolicyMetricsExtractorFunction() { - return new Function>() { - @Nullable - @Override - public List apply(@Nonnull TCAPolicy tcaPolicy) { - return tcaPolicy.getMetricsPerFunctionalRole(); - } - }; - } - - /** - * {@link Function} that extracts {@link MetricsPerFunctionalRole#getFunctionalRole()} from - * {@link MetricsPerFunctionalRole} - * - * @return Functional role or a Metrics Per Functional Role object - */ - public static Function tcaFunctionalRoleExtractorFunction() { - return new Function() { - @Override - public String apply(@Nonnull MetricsPerFunctionalRole metricsPerFunctionalRole) { - return metricsPerFunctionalRole.getFunctionalRole(); - } - }; - } - - - /** - * Extracts {@link TCAPolicy} Functional Roles - * - * @param tcaPolicy TCA Policy - * @return List of functional Roles in the tca Policy - */ - public static List getPolicyFunctionalRoles(@Nonnull final TCAPolicy tcaPolicy) { - final List metricsPerFunctionalRoles = - tcaPolicyMetricsExtractorFunction().apply(tcaPolicy); - - return Lists.transform(metricsPerFunctionalRoles, tcaFunctionalRoleExtractorFunction()); - } - - /** - * A {@link Supplier} which caches {@link TCAPolicy} Functional Roles as they are not expected to - * change during runtime - * - * @param tcaPolicy TCA Policy - * @return a Supplier that memoize the Functional roles - */ - public static Supplier> getPolicyFunctionalRoleSupplier(@Nonnull final TCAPolicy tcaPolicy) { - return Suppliers.memoize(new Supplier>() { - @Override - public List get() { - return getPolicyFunctionalRoles(tcaPolicy); - } - }); - } - - - /** - * Creates a Table to lookup thresholds of a {@link TCAPolicy} by its Functional Role and Threshold Field path - * - * @param tcaPolicy TCA Policy - * @return A table with Keys of functional role and field path containing List of threshold as values - */ - public static Table> getPolicyFRThresholdsTable(final TCAPolicy tcaPolicy) { - final Table> domainFRTable = HashBasedTable.create(); - for (MetricsPerFunctionalRole metricsPerFunctionalRole : tcaPolicy.getMetricsPerFunctionalRole()) { - final String functionalRole = metricsPerFunctionalRole.getFunctionalRole(); - final List thresholds = metricsPerFunctionalRole.getThresholds(); - for (Threshold threshold : thresholds) { - final List existingThresholds = domainFRTable.get(functionalRole, threshold.getFieldPath()); - if (existingThresholds == null) { - final LinkedList newThresholdList = new LinkedList<>(); - newThresholdList.add(threshold); - domainFRTable.put(functionalRole, threshold.getFieldPath(), newThresholdList); - } else { - domainFRTable.get(functionalRole, threshold.getFieldPath()).add(threshold); - } - } - } - return domainFRTable; - } - - - /** - * A {@link Supplier} which caches Policy Functional Role and Threshold Field Path Thresholds lookup table - * - * @param tcaPolicy TCA Policy - * @return Cached Supplier for table with Keys of functional role and field path containing thresholds as values - */ - public static Supplier>> getPolicyFRThresholdsTableSupplier - (final TCAPolicy tcaPolicy) { - return Suppliers.memoize(new Supplier>>() { - @Override - public Table> get() { - return getPolicyFRThresholdsTable(tcaPolicy); - } - }); - } - - - /** - * Parses and validates Runtime Arguments to {@link TCAAppPreferences} object - * - * @param runtimeContext Runtime Context - * - * @return validated runtime arguments as {@link TCAAppPreferences} object - */ - public static TCAAppPreferences getValidatedTCAAppPreferences(final RuntimeContext runtimeContext) { - // Parse runtime arguments - final Map runtimeArguments = runtimeContext.getRuntimeArguments(); - final TCAAppPreferences tcaAppPreferences = - ANALYTICS_MODEL_OBJECT_MAPPER.convertValue(runtimeArguments, TCAAppPreferences.class); - - // Update values of app preferences based on controller passed arguments if required - final TCAAppPreferences updatedTCAAppPreferences = - updateDMaaPPubSubValues(runtimeArguments, tcaAppPreferences); - - // Validate runtime arguments - validateSettings(updatedTCAAppPreferences, new TCAPreferencesValidator()); - - return tcaAppPreferences; - } - - - /** - * Updates DMaaP Subscriber and Publisher Urls if present in runtime arguments. - * Maps runtime arguments property - dmaap.in.event-input.dmaapUrl to Subscriber host, port and topic - * Maps runtime arguments property - dmaap.out.alert-output.dmaapUrl to Publisher host, port and topic - * - * @param runtimeArguments Runtime arguments passed in to TCA App by controller - * @param tcaAppPreferences TCA App Preferences - * - * @return TCA App Preferences which updated Publisher and Subscriber host,port and topic values - */ - public static TCAAppPreferences updateDMaaPPubSubValues(final Map runtimeArguments, - final TCAAppPreferences tcaAppPreferences) { - - final String subscriberPropertyKey = "dmaap.in.event-input.dmaapUrl"; - final String subscriberPropertyValue = runtimeArguments.get(subscriberPropertyKey); - - if (subscriberPropertyValue != null) { - LOG.debug("Updating value for DMaaP Subscriber to values provided in property: {} with value: {}", - subscriberPropertyKey, subscriberPropertyValue); - final URL subscriberUrl = parseURL(subscriberPropertyValue); - tcaAppPreferences.setSubscriberProtocol(subscriberUrl.getProtocol()); - tcaAppPreferences.setSubscriberHostName(subscriberUrl.getHost()); - final int subscriberUrlPort = subscriberUrl.getPort() != -1 ? - Integer.valueOf(subscriberUrl.getPort()) : getDefaultDMaaPPort(subscriberUrl.getProtocol()); - tcaAppPreferences.setSubscriberHostPort(subscriberUrlPort); - tcaAppPreferences.setSubscriberTopicName(subscriberUrl.getPath().substring(1)); - } - - final String subscriberUserNamePropertyKey = "dmaap.in.event-input.dmaapUserName"; - if (runtimeArguments.containsKey(subscriberUserNamePropertyKey)) { - tcaAppPreferences.setSubscriberUserName(runtimeArguments.get(subscriberUserNamePropertyKey)); - } - final String subscriberPasswordPropertyKey = "dmaap.in.event-input.dmaapPassword"; - if (runtimeArguments.containsKey(subscriberPasswordPropertyKey)) { - tcaAppPreferences.setSubscriberUserPassword(runtimeArguments.get(subscriberPasswordPropertyKey)); - } - - final String publisherPropertyKey = "dmaap.out.alert-output.dmaapUrl"; - final String publisherPropertyValue = runtimeArguments.get(publisherPropertyKey); - if (publisherPropertyValue != null) { - LOG.debug("Updating value for DMaaP Publisher to values provided in property: {} with value: {}", - publisherPropertyKey, publisherPropertyValue); - final URL publisherUrl = parseURL(publisherPropertyValue); - tcaAppPreferences.setPublisherProtocol(publisherUrl.getProtocol()); - tcaAppPreferences.setPublisherHostName(publisherUrl.getHost()); - final int publisherUrlPort = publisherUrl.getPort() != -1 ? - Integer.valueOf(publisherUrl.getPort()) : getDefaultDMaaPPort(publisherUrl.getProtocol()); - tcaAppPreferences.setPublisherHostPort(publisherUrlPort); - tcaAppPreferences.setPublisherTopicName(publisherUrl.getPath().substring(1)); - } - - final String publisherUserNamePropertyKey = "dmaap.out.alert-output.dmaapUserName"; - if (runtimeArguments.containsKey(publisherUserNamePropertyKey)) { - tcaAppPreferences.setPublisherUserName(runtimeArguments.get(publisherUserNamePropertyKey)); - } - final String publisherPasswordPropertyKey = "dmaap.out.alert-output.dmaapPassword"; - if (runtimeArguments.containsKey(publisherPasswordPropertyKey)) { - tcaAppPreferences.setPublisherUserPassword(runtimeArguments.get(publisherPasswordPropertyKey)); - } - - - return tcaAppPreferences; - } - - /** - * Sets up default DMaaP Port if not provided with DMaaP URL - * - * @param protocol protocol e.g. http or https - * - * @return default DMaaP MR port number - */ - private static int getDefaultDMaaPPort(final String protocol) { - if ("http".equals(protocol)) { - return 3904; - } else if ("https".equals(protocol)) { - return 3905; - } else { - return 80; - } - } - - /** - * Parses provided DMaaP MR URL string to {@link URL} object - * - * @param urlString url string - * - * @return url object - */ - private static URL parseURL(final String urlString) { - try { - return new URL(urlString); - } catch (MalformedURLException e) { - final String errorMessage = String.format("Invalid URL format: %s", urlString); - throw new DCAEAnalyticsRuntimeException(errorMessage, LOG, e); - } - } - - - /** - * Creates a {@link GenericMessageChainProcessor} of {@link TCACEFJsonProcessor}, - * {@link TCACEFPolicyDomainFilter} and {@link TCACEFPolicyFunctionalRoleFilter}s to - * filter out messages which does not match policy domain or functional role - * - * @param cefMessage CEF Message - * @param tcaPolicy TCA Policy - * @return Message Process Context after processing filter chain - */ - public static TCACEFProcessorContext filterCEFMessage(@Nullable final String cefMessage, - @Nonnull final TCAPolicy tcaPolicy) { - - final TCACEFJsonProcessor jsonProcessor = new TCACEFJsonProcessor(); - final TCACEFPolicyDomainFilter domainFilter = new TCACEFPolicyDomainFilter(); - final TCACEFPolicyFunctionalRoleFilter functionalRoleFilter = new TCACEFPolicyFunctionalRoleFilter(); - // Create a list of message processors - final ImmutableList> messageProcessors = - ImmutableList.of(jsonProcessor, domainFilter, functionalRoleFilter); - final TCACEFProcessorContext processorContext = new TCACEFProcessorContext(cefMessage, tcaPolicy); - // Create a message processors chain - final GenericMessageChainProcessor tcaProcessingChain = - new GenericMessageChainProcessor<>(messageProcessors, processorContext); - // process chain - return tcaProcessingChain.processChain(); - } - - - /** - * Extracts json path values for given json Field Paths from using Json path notation. Assumes - * that values extracted are always long - * - * @param message CEF Message - * @param jsonFieldPaths Json Field Paths - * @return Map containing key as json path and values as values associated with that json path - */ - public static Map> getJsonPathValue(@Nonnull String message, @Nonnull Set - jsonFieldPaths) { - - final Map> jsonFieldPathMap = new HashMap<>(); - final DocumentContext documentContext = JsonPath.parse(message); - - for (String jsonFieldPath : jsonFieldPaths) { - final List jsonFieldValues = documentContext.read(jsonFieldPath, new TypeRef>() { - }); - // If Json Field Values are not or empty - if (jsonFieldValues != null && !jsonFieldValues.isEmpty()) { - // Filter out all null values in the filed values list - final List nonNullValues = Lists.newLinkedList(Iterables.filter(jsonFieldValues, - Predicates.notNull())); - // If there are non null values put them in the map - if (!nonNullValues.isEmpty()) { - jsonFieldPathMap.put(jsonFieldPath, nonNullValues); - } - } - } - - return jsonFieldPathMap; - } - - /** - * Computes if any CEF Message Fields have violated any Policy Thresholds. For the same policy field path - * it applies threshold in order of their severity and record the first threshold per message field path - * - * @param messageFieldValues Field Path Values extracted from CEF Message - * @param fieldThresholds Policy Thresholds for Field Path - * @return Optional of violated threshold for a field path - */ - public static Optional thresholdCalculator(final List messageFieldValues, final List - fieldThresholds) { - // order thresholds by severity - Collections.sort(fieldThresholds, THRESHOLD_COMPARATOR); - // Now apply each threshold to field values - for (Threshold fieldThreshold : fieldThresholds) { - for (Long messageFieldValue : messageFieldValues) { - final Boolean isThresholdViolated = - fieldThreshold.getDirection().operate(messageFieldValue, fieldThreshold.getThresholdValue()); - if (isThresholdViolated) { - return Optional.of(fieldThreshold); - } - } - } - return Optional.absent(); - } - - /** - * Prioritize Threshold to be reported in case there was multiple TCA violations in a single CEF message. - * Grabs first highest priority violated threshold - * - * @param violatedThresholdsMap Map containing field Path and associated violated Thresholds - * @return First Highest priority violated threshold - */ - public static Threshold prioritizeThresholdViolations(final Map violatedThresholdsMap) { - - final List violatedThresholds = newArrayList(violatedThresholdsMap.values()); - - if (violatedThresholds.size() == 1) { - return violatedThresholds.get(0); - } - Collections.sort(violatedThresholds, THRESHOLD_COMPARATOR); - // Just grab the first violated threshold with highest priority - return violatedThresholds.get(0); - } - - - /** - * Creates {@link MetricsPerFunctionalRole} object which contains violated thresholds - * - * @param tcaPolicy TCA Policy - * @param violatedThreshold Violated thresholds - * @param functionalRole Functiona Role - * - * @return MetricsPerFunctionalRole object containing one highest severity violated threshold - */ - public static MetricsPerFunctionalRole createViolatedMetrics(@Nonnull final TCAPolicy tcaPolicy, - @Nonnull final Threshold violatedThreshold, - @Nonnull final String functionalRole) { - - final ArrayList metricsPerFunctionalRoles = newArrayList( - Iterables.filter(tcaPolicy.getMetricsPerFunctionalRole(), new Predicate() { - @Override - public boolean apply(@Nonnull MetricsPerFunctionalRole metricsPerFunctionalRole) { - return metricsPerFunctionalRole.getFunctionalRole().equals(functionalRole); - } - })); - // TCA policy must have only one metrics role per functional role - if (metricsPerFunctionalRoles.size() == 1) { - final MetricsPerFunctionalRole policyMetrics = metricsPerFunctionalRoles.get(0); - final MetricsPerFunctionalRole violatedMetrics = new MetricsPerFunctionalRole(); - violatedMetrics.setFunctionalRole(policyMetrics.getFunctionalRole()); - violatedMetrics.setPolicyScope(policyMetrics.getPolicyScope()); - violatedMetrics.setPolicyName(policyMetrics.getPolicyName()); - violatedMetrics.setPolicyVersion(policyMetrics.getPolicyVersion()); - violatedMetrics.setThresholds(ImmutableList.of(violatedThreshold)); - return violatedMetrics; - } else { - final String errorMessage = String.format("TCA Policy must contain functional Role: %s", functionalRole); - throw new MessageProcessingException(errorMessage, LOG, new IllegalStateException(errorMessage)); - } - } - - /** - * Computes threshold violations - * - * @param processorContext Filtered processor Context - * @return processor context with any threshold violations - */ - public static TCACEFProcessorContext computeThresholdViolations(final TCACEFProcessorContext processorContext) { - final TCACEFPolicyThresholdsProcessor policyThresholdsProcessor = new TCACEFPolicyThresholdsProcessor(); - return policyThresholdsProcessor.apply(processorContext); - } - - /** - * Creates {@link TCAVESResponse} object - * - * @param processorContext processor Context with violations - * @param tcaAppName TCA App Name - * - * @return TCA VES Response Message - */ - public static TCAVESResponse createNewTCAVESResponse(final TCACEFProcessorContext processorContext, - final String tcaAppName) { - - final MetricsPerFunctionalRole metricsPerFunctionalRole = processorContext.getMetricsPerFunctionalRole(); - // confirm violations are indeed present - if (metricsPerFunctionalRole == null) { - final String errorMessage = "No violations metrics. Unable to create VES Response"; - throw new MessageProcessingException(errorMessage, LOG, new IllegalArgumentException(errorMessage)); - } - - final String functionalRole = metricsPerFunctionalRole.getFunctionalRole(); - final Threshold violatedThreshold = metricsPerFunctionalRole.getThresholds().get(0); - final EventListener eventListener = processorContext.getCEFEventListener(); - final CommonEventHeader commonEventHeader = eventListener.getEvent().getCommonEventHeader(); - - final TCAVESResponse tcavesResponse = new TCAVESResponse(); - // ClosedLoopControlName included in the DCAE configuration Policy - tcavesResponse.setClosedLoopControlName(violatedThreshold.getClosedLoopControlName()); - // version included in the DCAE configuration Policy - tcavesResponse.setVersion(violatedThreshold.getVersion()); - // Generate a UUID for this output message - tcavesResponse.setRequestID(UUID.randomUUID().toString()); - // commonEventHeader.startEpochMicrosec from the received VES measurementsForVfScaling message - tcavesResponse.setClosedLoopAlarmStart(commonEventHeader.getStartEpochMicrosec()); - // Concatenate name of this DCAE instance and name for this TCA instance, separated by dot - // TODO: Find out how to get this field - tcavesResponse.setClosedLoopEventClient("DCAE_INSTANCE_ID." + tcaAppName); - - final AAI aai = new AAI(); - tcavesResponse.setAai(aai); - - // vLoadBalancer specific settings - if (isFunctionalRoleVLoadBalancer(functionalRole)) { - // Hard Coded - "VM" - tcavesResponse.setTargetType(AnalyticsConstants.LOAD_BALANCER_TCA_VES_RESPONSE_TARGET_TYPE); - // Hard Coded - "vserver.vserver-name" - tcavesResponse.setTarget(AnalyticsConstants.LOAD_BALANCER_TCA_VES_RESPONSE_TARGET); - aai.setGenericServerId(commonEventHeader.getReportingEntityName()); - } else { - // Hard Coded - "VNF" - tcavesResponse.setTargetType(AnalyticsConstants.TCA_VES_RESPONSE_TARGET_TYPE); - // Hard Coded - "generic-vnf.vnf-id" - tcavesResponse.setTarget(AnalyticsConstants.TCA_VES_RESPONSE_TARGET); - // commonEventHeader.reportingEntityName from the received VES measurementsForVfScaling message (value for - // the data element used in A&AI) - aai.setGenericVNFId(commonEventHeader.getReportingEntityName()); - } - - // Hard Coded - "DCAE" - tcavesResponse.setFrom(AnalyticsConstants.TCA_VES_RESPONSE_FROM); - // policyScope included in the DCAE configuration Policy - tcavesResponse.setPolicyScope(metricsPerFunctionalRole.getPolicyScope()); - // policyName included in the DCAE configuration Policy - tcavesResponse.setPolicyName(metricsPerFunctionalRole.getPolicyName()); - // policyVersion included in the DCAE configuration Policy - tcavesResponse.setPolicyVersion(metricsPerFunctionalRole.getPolicyVersion()); - // Hard Coded - "ONSET" - tcavesResponse.setClosedLoopEventStatus(AnalyticsConstants.TCA_VES_RESPONSE_CLOSED_LOOP_EVENT_STATUS); - - return tcavesResponse; - } - - /** - * Determines if Functional Role is vLoadBlanacer - * - * @param functionalRole functional Role to check - * - * @return return true if functional role is for vLoadBalancer - */ - private static boolean isFunctionalRoleVLoadBalancer(final String functionalRole) { - return functionalRole.equals(AnalyticsConstants.LOAD_BALANCER_FUNCTIONAL_ROLE); - } - - - /** - * Extract Domain and functional Role from processor context if present - * - * @param processorContext processor context - * @return Tuple of domain and functional role - */ - public static Pair getDomainAndFunctionalRole(@Nullable final TCACEFProcessorContext - processorContext) { - - String domain = null; - String functionalRole = null; - - - if (processorContext != null && - processorContext.getCEFEventListener() != null && - processorContext.getCEFEventListener().getEvent() != null && - processorContext.getCEFEventListener().getEvent().getCommonEventHeader() != null) { - final CommonEventHeader commonEventHeader = processorContext.getCEFEventListener().getEvent() - .getCommonEventHeader(); - - if (commonEventHeader.getDomain() != null) { - domain = commonEventHeader.getDomain(); - } - - if (commonEventHeader.getFunctionalRole() != null) { - functionalRole = commonEventHeader.getFunctionalRole(); - } - - } - - return new ImmutablePair<>(domain, functionalRole); - - } - - /** - * Function that extracts alert message string from {@link TCAVESAlertEntity} - */ - public static final Function MAP_ALERT_ENTITY_TO_ALERT_STRING_FUNCTION = - new Function() { - @Override - public String apply(TCAVESAlertEntity alertEntity) { - return alertEntity == null ? null : alertEntity.getAlertMessage(); - } - }; - - /** - * Extracts alert message strings from {@link TCAVESAlertEntity} - * @param alertEntities collection of alert entities - * @return List of alert message strings - */ - public static List extractAlertFromAlertEntities(final Collection alertEntities) { - return Lists.transform(newArrayList(alertEntities), MAP_ALERT_ENTITY_TO_ALERT_STRING_FUNCTION); - } - - - /** - * Converts Runtime Arguments to {@link TCAPolicyPreferences} object - * - * @param runtimeContext CDAP Runtime Arguments - * - * @return TCA Policy Preferences - */ - public static TCAPolicy getValidatedTCAPolicyPreferences(final RuntimeContext runtimeContext) { - - final Map runtimeArguments = runtimeContext.getRuntimeArguments(); - final TreeMap sortedRuntimeArguments = new TreeMap<>(runtimeArguments); - - LOG.debug("Printing all Received Runtime Arguments:"); - for (Map.Entry runtimeArgsEntry : sortedRuntimeArguments.entrySet()) { - LOG.debug("{}:{}", runtimeArgsEntry.getKey(), runtimeArgsEntry.getValue()); - } - - // extract TCA Policy Domain from Runtime Arguments - final String policyDomain = sortedRuntimeArguments.get(AnalyticsConstants.TCA_POLICY_DOMAIN_PATH); - - // create new TCA Policy object - final TCAPolicyPreferences tcaPolicyPreferences = new TCAPolicyPreferences(); - tcaPolicyPreferences.setDomain(policyDomain); - - // filter out other non relevant fields which are not related to tca policy - final Map tcaPolicyMap = filterMapByKeyNamePrefix(sortedRuntimeArguments, - TCA_POLICY_METRICS_PER_FUNCTIONAL_ROLE_PATH); - - // determine functional Roles - final Map> functionalRolesMap = - extractSubTree(tcaPolicyMap, 2, 3, AnalyticsConstants.TCA_POLICY_DELIMITER); - - // create metrics per functional role list - tcaPolicyPreferences.setMetricsPerFunctionalRole( - createTCAPolicyMetricsPerFunctionalRoleList(functionalRolesMap)); - - // validate tca Policy Preferences - validateSettings(tcaPolicyPreferences, new TCAPolicyPreferencesValidator()); - - LOG.info("Printing Effective TCA Policy: {}", tcaPolicyPreferences); - - return tcaPolicyPreferences; - } - - /** - * Creates {@link TCAPolicy} Metrics per Functional Role list - * - * @param functionalRolesMap Map containing functional Roles as key and corresponding values - * - * @return List of {@link MetricsPerFunctionalRole} - */ - public static List createTCAPolicyMetricsPerFunctionalRoleList( - final Map> functionalRolesMap) { - - // create a new metrics per functional role list - final List metricsPerFunctionalRoles = new LinkedList<>(); - - for (Map.Entry> functionalRolesEntry : functionalRolesMap.entrySet()) { - - // create new metrics per functional role instance - final MetricsPerFunctionalRole newMetricsPerFunctionalRole = - createNewMetricsPerFunctionalRole(functionalRolesEntry); - metricsPerFunctionalRoles.add(newMetricsPerFunctionalRole); - - // determine all threshold related values - final Map thresholdsValuesMaps = - filterMapByKeyNamePrefix(functionalRolesEntry.getValue(), - AnalyticsConstants.TCA_POLICY_THRESHOLDS_PATH_POSTFIX); - - // create a map of all threshold values - final Map> thresholdsMap = - extractSubTree(thresholdsValuesMaps, 1, 2, - AnalyticsConstants.TCA_POLICY_DELIMITER); - - // add thresholds to nmetrics per functional roles threshold list - for (Map thresholdMap : thresholdsMap.values()) { - newMetricsPerFunctionalRole.getThresholds().add(createNewThreshold(thresholdMap)); - } - - } - - return metricsPerFunctionalRoles; - } - - /** - * Creates new instance of TCA Policy {@link Threshold} with values extracted from thresholdMap - * - * @param thresholdMap threshold map with threshold values - * - * @return new instance of TCA Policy Threshold - */ - public static Threshold createNewThreshold(final Map thresholdMap) { - final Threshold threshold = new Threshold(); - threshold.setClosedLoopControlName(thresholdMap.get("policy.closedLoopControlName")); - threshold.setVersion(thresholdMap.get("policy.version")); - threshold.setFieldPath(thresholdMap.get("policy.fieldPath")); - threshold.setDirection(Direction.valueOf(thresholdMap.get("policy.direction"))); - threshold.setSeverity(EventSeverity.valueOf(thresholdMap.get("policy.severity"))); - threshold.setThresholdValue(Long.valueOf(thresholdMap.get("policy.thresholdValue"))); - return threshold; - } - - /** - * Create new {@link MetricsPerFunctionalRole} instance with policy Name, policy Version and policy Scope - * extracted from given functionalRolesEntry - * - * @param functionalRolesEntry Functional Role Entry - * - * @return new instance of MetricsPerFunctionalRole - */ - public static MetricsPerFunctionalRole createNewMetricsPerFunctionalRole( - final Map.Entry> functionalRolesEntry) { - // determine functional Role - final String functionalRole = functionalRolesEntry.getKey(); - // determine functional Role thresholds - final Map metricsPerFunctionalRoleThresholdsMap = functionalRolesEntry.getValue(); - final MetricsPerFunctionalRole metricsPerFunctionalRole = new MetricsPerFunctionalRole(); - final List thresholds = new LinkedList<>(); - metricsPerFunctionalRole.setThresholds(thresholds); - metricsPerFunctionalRole.setFunctionalRole(functionalRole); - // bind policyName, policyVersion and policyScope - metricsPerFunctionalRole.setPolicyName(metricsPerFunctionalRoleThresholdsMap.get("policyName")); - metricsPerFunctionalRole.setPolicyVersion(metricsPerFunctionalRoleThresholdsMap.get("policyVersion")); - metricsPerFunctionalRole.setPolicyScope(metricsPerFunctionalRoleThresholdsMap.get("policyScope")); - return metricsPerFunctionalRole; - } - - /** - * Converts a flattened key/value map which has keys delimited by a given delimiter. - * The start Index and end index extract the sub-key value and returns a new map containing - * sub-keys and values. - * - * @param actualMap actual Map - * @param startIndex start index - * @param endIndex end index - * @param delimiter delimiter - * - * @return Map with new sub tree map - */ - public static Map> extractSubTree( - final Map actualMap, int startIndex, int endIndex, String delimiter) { - - final SortedMap> subTreeMap = new TreeMap<>(); - - // iterate over actual map entries - for (Map.Entry actualMapEntry : actualMap.entrySet()) { - final String actualMapKey = actualMapEntry.getKey(); - final String actualMapValue = actualMapEntry.getValue(); - - // determine delimiter start and end index - final int keyStartIndex = StringUtils.ordinalIndexOf(actualMapKey, delimiter, startIndex); - final int keyEndIndex = StringUtils.ordinalIndexOf(actualMapKey, delimiter, endIndex); - final int keyLength = actualMapKey.length(); - - // extract sub-tree map - if (keyStartIndex != -1 && keyEndIndex != -1 && keyEndIndex > keyStartIndex && keyLength > keyEndIndex) { - final String thresholdKey = actualMapKey.substring(keyStartIndex + 1, keyEndIndex); - final Map existingThresholdMap = subTreeMap.get(thresholdKey); - final String subMapKey = actualMapKey.substring(keyEndIndex + 1, keyLength); - if (existingThresholdMap == null) { - Map newThresholdMap = new LinkedHashMap<>(); - newThresholdMap.put(subMapKey, actualMapValue); - subTreeMap.put(thresholdKey, newThresholdMap); - } else { - existingThresholdMap.put(subMapKey, actualMapValue); - } - - } - } - - return subTreeMap; - - } - - - /** - * Provides a view of underlying map that filters out entries with keys starting with give prefix - * - * @param actualMap Target map that needs to be filtered - * @param keyNamePrefix key prefix - * - * @return a view of actual map which only show entries which have give prefix - */ - public static Map filterMapByKeyNamePrefix(final Map actualMap, - final String keyNamePrefix) { - return Maps.filterKeys(actualMap, - new Predicate() { - @Override - public boolean apply(@Nullable String key) { - return key != null && key.startsWith(keyNamePrefix); - } - }); - } - - -} diff --git a/dcae-analytics-tca/src/main/java/org/openecomp/dcae/analytics/tca/validator/TCAAppConfigValidator.java b/dcae-analytics-tca/src/main/java/org/openecomp/dcae/analytics/tca/validator/TCAAppConfigValidator.java deleted file mode 100644 index 05b0331..0000000 --- a/dcae-analytics-tca/src/main/java/org/openecomp/dcae/analytics/tca/validator/TCAAppConfigValidator.java +++ /dev/null @@ -1,60 +0,0 @@ -/* - * ============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.analytics.tca.validator; - -import org.openecomp.dcae.analytics.common.cdap.validation.CDAPAppSettingsValidator; -import org.openecomp.dcae.analytics.common.validation.GenericValidationResponse; -import org.openecomp.dcae.analytics.tca.settings.TCAAppConfig; - -import static org.openecomp.dcae.analytics.common.utils.ValidationUtils.isEmpty; - -/** - *

- * TCA App Config Validator validates any TCA App Config parameter values - *

- * - * @author Rajiv Singla. Creation Date: 10/24/2016. - */ -public class TCAAppConfigValidator implements CDAPAppSettingsValidator> { - - @Override - public GenericValidationResponse validateAppSettings(TCAAppConfig tcaAppConfig) { - - final GenericValidationResponse validationResponse = new GenericValidationResponse<>(); - - if (isEmpty(tcaAppConfig.getTcaSubscriberOutputStreamName())) { - validationResponse.addErrorMessage("tcaSubscriberOutputStreamName", - "tcaSubscriberOutputStreamName must be present"); - } - - if (isEmpty(tcaAppConfig.getTcaVESMessageStatusTableName())) { - validationResponse.addErrorMessage("tcaVESMessageStatusTableName", - "tcaVESMessageStatusTableName must be present"); - } - if (isEmpty(tcaAppConfig.getTcaVESAlertsTableName())) { - validationResponse.addErrorMessage("tcaVESAlertsTableName", - "tcaVESAlertsTableName must be present"); - } - - return validationResponse; - } -} diff --git a/dcae-analytics-tca/src/main/java/org/openecomp/dcae/analytics/tca/validator/TCAPolicyPreferencesValidator.java b/dcae-analytics-tca/src/main/java/org/openecomp/dcae/analytics/tca/validator/TCAPolicyPreferencesValidator.java deleted file mode 100644 index 0ff0eb6..0000000 --- a/dcae-analytics-tca/src/main/java/org/openecomp/dcae/analytics/tca/validator/TCAPolicyPreferencesValidator.java +++ /dev/null @@ -1,94 +0,0 @@ -/* - * ============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.analytics.tca.validator; - -import org.openecomp.dcae.analytics.common.cdap.validation.CDAPAppSettingsValidator; -import org.openecomp.dcae.analytics.common.validation.GenericValidationResponse; -import org.openecomp.dcae.analytics.model.domain.cef.EventSeverity; -import org.openecomp.dcae.analytics.model.domain.policy.tca.Direction; -import org.openecomp.dcae.analytics.model.domain.policy.tca.MetricsPerFunctionalRole; -import org.openecomp.dcae.analytics.model.domain.policy.tca.Threshold; -import org.openecomp.dcae.analytics.tca.settings.TCAPolicyPreferences; -import org.openecomp.dcae.analytics.tca.utils.TCAUtils; - -import java.util.List; - -import static org.openecomp.dcae.analytics.common.utils.ValidationUtils.isEmpty; - -/** - * Validates TCA Policy Preferences - *

- * @author Rajiv Singla. Creation Date: 11/29/2016. - */ -public class TCAPolicyPreferencesValidator implements CDAPAppSettingsValidator> { - - @Override - public GenericValidationResponse validateAppSettings( - final TCAPolicyPreferences tcaPolicyPreferences) { - - final GenericValidationResponse validationResponse = new GenericValidationResponse<>(); - - // validate TCA Policy must domain present - final String domain = tcaPolicyPreferences.getDomain(); - if (isEmpty(domain)) { - validationResponse.addErrorMessage("domain", "TCA Policy must have only one domain present"); - } - - // validate TCA Policy must have at least one functional role - final List policyFunctionalRoles = TCAUtils.getPolicyFunctionalRoles(tcaPolicyPreferences); - if (policyFunctionalRoles.size() <= 0) { - validationResponse.addErrorMessage("metricsPerFunctionalRoles", - "TCA Policy must have at least one or more functional roles"); - } - - final List metricsPerFunctionalRoles = - tcaPolicyPreferences.getMetricsPerFunctionalRole(); - - // validate each Functional Role must have at least one threshold - for (MetricsPerFunctionalRole metricsPerFunctionalRole : metricsPerFunctionalRoles) { - if (metricsPerFunctionalRole.getThresholds().size() <= 0) { - validationResponse.addErrorMessage("thresholds", - "TCA Policy Functional Role must have at least one threshold. " + - "Functional Role causing this validation error:" + metricsPerFunctionalRole); - } - } - - // validate each threshold must have non null - fieldPath, thresholdValue, direction and severity - for (MetricsPerFunctionalRole metricsPerFunctionalRole : metricsPerFunctionalRoles) { - final List functionalRoleThresholds = metricsPerFunctionalRole.getThresholds(); - for (Threshold functionalRoleThreshold : functionalRoleThresholds) { - final String fieldPath = functionalRoleThreshold.getFieldPath(); - final Long thresholdValue = functionalRoleThreshold.getThresholdValue(); - final Direction direction = functionalRoleThreshold.getDirection(); - final EventSeverity severity = functionalRoleThreshold.getSeverity(); - if (isEmpty(fieldPath) || thresholdValue == null || direction == null || severity == null) { - validationResponse.addErrorMessage("threshold", - "TCA Policy threshold must have fieldPath, thresholdValue, direction and severity present." - + "Threshold causing this validation error:" + functionalRoleThreshold); - } - } - } - - - return validationResponse; - } -} diff --git a/dcae-analytics-tca/src/main/java/org/openecomp/dcae/analytics/tca/validator/TCAPreferencesValidator.java b/dcae-analytics-tca/src/main/java/org/openecomp/dcae/analytics/tca/validator/TCAPreferencesValidator.java deleted file mode 100644 index a7cbbf5..0000000 --- a/dcae-analytics-tca/src/main/java/org/openecomp/dcae/analytics/tca/validator/TCAPreferencesValidator.java +++ /dev/null @@ -1,63 +0,0 @@ -/* - * ============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.analytics.tca.validator; - -import org.openecomp.dcae.analytics.common.cdap.validation.CDAPAppSettingsValidator; -import org.openecomp.dcae.analytics.common.validation.GenericValidationResponse; -import org.openecomp.dcae.analytics.tca.settings.TCAAppPreferences; - -import static org.openecomp.dcae.analytics.common.utils.ValidationUtils.isEmpty; - -/** - * - * @author Rajiv Singla. Creation Date: 11/3/2016. - */ -public class TCAPreferencesValidator implements CDAPAppSettingsValidator> { - - @Override - public GenericValidationResponse validateAppSettings(TCAAppPreferences appPreferences) { - - final GenericValidationResponse validationResponse = new GenericValidationResponse<>(); - - // subscriber validations - final String subscriberHostName = appPreferences.getSubscriberHostName(); - if (isEmpty(subscriberHostName)) { - validationResponse.addErrorMessage("subscriberHostName", "Subscriber host name must be present"); - } - final String subscriberTopicName = appPreferences.getSubscriberTopicName(); - if (isEmpty(subscriberTopicName)) { - validationResponse.addErrorMessage("subscriberTopicName", "Subscriber topic name must be present"); - } - - // publisher validations - final String publisherHostName = appPreferences.getPublisherHostName(); - if (isEmpty(publisherHostName)) { - validationResponse.addErrorMessage("publisherHostName", "Publisher host name must be present"); - } - final String publisherTopicName = appPreferences.getPublisherTopicName(); - if (isEmpty(publisherTopicName)) { - validationResponse.addErrorMessage("publisherTopicName", "Publisher topic name must be present"); - } - - return validationResponse; - } -} diff --git a/dcae-analytics-tca/src/main/java/org/openecomp/dcae/analytics/tca/worker/TCADMaaPMRPublisherJob.java b/dcae-analytics-tca/src/main/java/org/openecomp/dcae/analytics/tca/worker/TCADMaaPMRPublisherJob.java deleted file mode 100644 index 4a9a7ab..0000000 --- a/dcae-analytics-tca/src/main/java/org/openecomp/dcae/analytics/tca/worker/TCADMaaPMRPublisherJob.java +++ /dev/null @@ -1,189 +0,0 @@ -/* - * ============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.analytics.tca.worker; - -import co.cask.cdap.api.TxRunnable; -import co.cask.cdap.api.common.Bytes; -import co.cask.cdap.api.data.DatasetContext; -import co.cask.cdap.api.dataset.lib.CloseableIterator; -import co.cask.cdap.api.dataset.lib.KeyValue; -import co.cask.cdap.api.dataset.lib.ObjectMappedTable; -import co.cask.cdap.api.metrics.Metrics; -import co.cask.cdap.api.worker.WorkerContext; -import com.google.common.base.Joiner; -import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; -import org.openecomp.dcae.analytics.common.CDAPMetricsConstants; -import org.openecomp.dcae.analytics.common.exception.DCAEAnalyticsRuntimeException; -import org.openecomp.dcae.analytics.common.utils.HTTPUtils; -import org.openecomp.dcae.analytics.dmaap.domain.response.DMaaPMRPublisherResponse; -import org.openecomp.dcae.analytics.dmaap.service.publisher.DMaaPMRPublisher; -import org.openecomp.dcae.analytics.tca.persistance.TCAVESAlertEntity; -import org.openecomp.dcae.analytics.tca.persistance.TCAVESAlertsPersister; -import org.openecomp.dcae.analytics.tca.utils.TCAUtils; -import org.quartz.DisallowConcurrentExecution; -import org.quartz.Job; -import org.quartz.JobDataMap; -import org.quartz.JobExecutionContext; -import org.quartz.JobExecutionException; -import org.quartz.PersistJobDataAfterExecution; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.util.Date; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; -import java.util.Set; - -import static org.openecomp.dcae.analytics.common.AnalyticsConstants.CDAP_ALERTS_TABLE_VARIABLE_NAME; -import static org.openecomp.dcae.analytics.common.AnalyticsConstants.DMAAP_METRICS_VARIABLE_NAME; -import static org.openecomp.dcae.analytics.common.AnalyticsConstants.DMAAP_PUBLISHER_VARIABLE_NAME; -import static org.openecomp.dcae.analytics.common.AnalyticsConstants.WORKER_CONTEXT_VARIABLE_NAME; - -/** - * Quartz Job that will monitor any new alert messages in given TCA Alerts table and if any found publish them to - * DMaaP MR topic - *

- * @author Rajiv Singla. Creation Date: 11/17/2016. - */ -@DisallowConcurrentExecution -@PersistJobDataAfterExecution -@SuppressFBWarnings("SIC_INNER_SHOULD_BE_STATIC_ANON") -public class TCADMaaPMRPublisherJob implements Job { - - private static final Logger LOG = LoggerFactory.getLogger(TCADMaaPMRPublisherJob.class); - - @Override - public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException { - - LOG.debug("Starting DMaaP MR Topic Publisher fetch Job. Next firing time will be: {}", - jobExecutionContext.getNextFireTime()); - - // Get Job Data Map - final JobDataMap jobDataMap = jobExecutionContext.getMergedJobDataMap(); - - // Fetch all Job Params from Job Data Map - final String cdapAlertsTableName = jobDataMap.getString(CDAP_ALERTS_TABLE_VARIABLE_NAME); - final WorkerContext workerContext = (WorkerContext) jobDataMap.get(WORKER_CONTEXT_VARIABLE_NAME); - final DMaaPMRPublisher publisher = (DMaaPMRPublisher) jobDataMap.get(DMAAP_PUBLISHER_VARIABLE_NAME); - final Metrics metrics = (Metrics) jobDataMap.get(DMAAP_METRICS_VARIABLE_NAME); - - LOG.debug("Start looking for new message in Alerts Table: {}", cdapAlertsTableName); - - // Get new alerts from alerts table - final Map newAlertsMap = getNewAlertsMap(cdapAlertsTableName, workerContext); - - // If no new alerts are found - nothing to publish - if (newAlertsMap.isEmpty()) { - LOG.debug("No new alerts found in Alerts Table name: {}. Nothing to Publisher....", cdapAlertsTableName); - metrics.count(CDAPMetricsConstants.TCA_PUBLISHER_NO_NEW_ALERTS_LOOKUP_METRIC, 1); - return; - } - - final int newAlertsCount = newAlertsMap.size(); - LOG.debug("Found new alerts in Alerts Table name: {}. No of new alerts: {}", cdapAlertsTableName, - newAlertsCount); - metrics.count(CDAPMetricsConstants.TCA_PUBLISHER_NEW_ALERTS_METRIC, newAlertsCount); - - // Get alert message strings from alert Entities - final List newAlertsMessages = TCAUtils.extractAlertFromAlertEntities(newAlertsMap.values()); - - // Publish messages to DMaaP MR Topic - - try { - - final DMaaPMRPublisherResponse publisherResponse = publisher.publish(newAlertsMessages); - - final Integer responseCode = publisherResponse.getResponseCode(); - final String responseMessage = publisherResponse.getResponseMessage(); - final int pendingMessagesCount = publisherResponse.getPendingMessagesCount(); - - LOG.debug("Publisher Response Code: {}, Publisher message: {}, Pending Messages Count: {}", responseCode, - responseMessage, pendingMessagesCount); - - if (HTTPUtils.isSuccessfulResponseCode(responseCode)) { - LOG.debug("Successfully Published alerts to DMaaP MR Topic."); - metrics.count(CDAPMetricsConstants.TCA_PUBLISHER_SUCCESSFUL_DMAAP_RESPONSE_METRIC, 1); - } else { - LOG.warn("Unable to publish alerts to DMaaP MR Topic. Publisher will try to send it later...."); - metrics.count(CDAPMetricsConstants.TCA_PUBLISHER_UNSUCCESSFUL_DMAAP_RESPONSE_METRIC, 1); - } - - } catch (DCAEAnalyticsRuntimeException e) { - LOG.error("Exception while publishing messages to DMaaP MR Topic: {}", e); - } finally { - // delete send message from alerts table - deleteAlertsByKey(cdapAlertsTableName, workerContext, newAlertsMap.keySet(), metrics); - } - - LOG.debug("Finished DMaaP MR Topic Publisher fetch Job."); - - } - - /** - * Gets New Messages from alerts table as Map with row keys as keys and {@link TCAVESAlertEntity} as values - * - * @param cdapAlertsTableName alerts table name - * @param workerContext worker context - * @return Map with row keys as keys and {@link TCAVESAlertEntity} as values - */ - private Map getNewAlertsMap(final String cdapAlertsTableName, - final WorkerContext workerContext) { - final Map newAlertsMap = new LinkedHashMap<>(); - workerContext.execute(new TxRunnable() { - @Override - public void run(DatasetContext context) throws Exception { - final ObjectMappedTable alertsTable = context.getDataset(cdapAlertsTableName); - final Date currentTime = new Date(); - final String rowKey = TCAVESAlertsPersister.createRowKey(currentTime); - final CloseableIterator> scan = alertsTable.scan(null, rowKey); - while (scan.hasNext()) { - final KeyValue alertEntityKeyValue = scan.next(); - newAlertsMap.put(Bytes.toString(alertEntityKeyValue.getKey()), alertEntityKeyValue.getValue()); - } - } - }); - return newAlertsMap; - } - - /** - * Deletes rows in Alerts table for give rowKeys - * - * @param cdapAlertsTableName CDAP Alerts Table Name - * @param workerContext Worker Context - * @param rowKeys Row Key Set - * @param metrics CDAP metrics - */ - private void deleteAlertsByKey(final String cdapAlertsTableName, final WorkerContext workerContext, - final Set rowKeys, final Metrics metrics) { - LOG.debug("Deleting Published Alerts from alerts table with rowKeys: {}", Joiner.on(",").join(rowKeys)); - workerContext.execute(new TxRunnable() { - @Override - public void run(DatasetContext context) throws Exception { - final ObjectMappedTable alertsTable = context.getDataset(cdapAlertsTableName); - for (String rowKey : rowKeys) { - alertsTable.delete(rowKey); - metrics.count(CDAPMetricsConstants.TCA_PUBLISHER_DELETED_ALERTS_METRIC, 1); - } - } - }); - } -} diff --git a/dcae-analytics-tca/src/main/java/org/openecomp/dcae/analytics/tca/worker/TCADMaaPMRSubscriberJob.java b/dcae-analytics-tca/src/main/java/org/openecomp/dcae/analytics/tca/worker/TCADMaaPMRSubscriberJob.java deleted file mode 100644 index 7212527..0000000 --- a/dcae-analytics-tca/src/main/java/org/openecomp/dcae/analytics/tca/worker/TCADMaaPMRSubscriberJob.java +++ /dev/null @@ -1,175 +0,0 @@ -/* - * ============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.analytics.tca.worker; - -import co.cask.cdap.api.metrics.Metrics; -import co.cask.cdap.api.worker.WorkerContext; -import com.google.common.base.Optional; -import com.google.common.base.Stopwatch; -import org.openecomp.dcae.analytics.common.AnalyticsConstants; -import org.openecomp.dcae.analytics.common.CDAPMetricsConstants; -import org.openecomp.dcae.analytics.common.exception.DCAEAnalyticsRuntimeException; -import org.openecomp.dcae.analytics.common.utils.HTTPUtils; -import org.openecomp.dcae.analytics.dmaap.domain.response.DMaaPMRSubscriberResponse; -import org.openecomp.dcae.analytics.dmaap.service.subscriber.DMaaPMRSubscriber; -import org.quartz.DisallowConcurrentExecution; -import org.quartz.Job; -import org.quartz.JobDataMap; -import org.quartz.JobExecutionContext; -import org.quartz.JobExecutionException; -import org.quartz.PersistJobDataAfterExecution; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.io.IOException; -import java.util.List; - -import static java.lang.String.format; - -/** - * Quartz Job which polls DMaaP MR VES Collector Topic for messages and writes them to - * a given CDAP Stream - * - * @author Rajiv Singla. Creation Date: 10/24/2016. - */ -@DisallowConcurrentExecution -@PersistJobDataAfterExecution -public class TCADMaaPMRSubscriberJob implements Job { - - private static final Logger LOG = LoggerFactory.getLogger(TCADMaaPMRSubscriberJob.class); - - @Override - public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException { - - LOG.debug("Starting DMaaP MR Topic Subscriber fetch Job. Next firing time will be: {}", - jobExecutionContext.getNextFireTime()); - - // Get Job Data Map - final JobDataMap jobDataMap = jobExecutionContext.getMergedJobDataMap(); - - // Fetch all Job Params from Job Data Map - final String cdapStreamName = jobDataMap.getString(AnalyticsConstants.CDAP_STREAM_VARIABLE_NAME); - final WorkerContext workerContext = - (WorkerContext) jobDataMap.get(AnalyticsConstants.WORKER_CONTEXT_VARIABLE_NAME); - final DMaaPMRSubscriber subscriber = - (DMaaPMRSubscriber) jobDataMap.get(AnalyticsConstants.DMAAP_SUBSCRIBER_VARIABLE_NAME); - final Metrics metrics = (Metrics) jobDataMap.get(AnalyticsConstants.DMAAP_METRICS_VARIABLE_NAME); - - final Optional subscriberResponseOptional = - getSubscriberResponse(subscriber, metrics); - - // If response is not present, unable to proceed - if (!subscriberResponseOptional.isPresent()) { - return; - } - - final DMaaPMRSubscriberResponse subscriberResponse = subscriberResponseOptional.get(); - - // If response code return by the subscriber call is not successful, unable to do proceed - if (!HTTPUtils.isSuccessfulResponseCode(subscriberResponse.getResponseCode())) { - LOG.error("Subscriber was unable to fetch messages properly. Subscriber Response Code: {} " + - "Unable to proceed further....", subscriberResponse.getResponseCode()); - metrics.count(CDAPMetricsConstants.TCA_SUBSCRIBER_UNSUCCESSFUL_RESPONSES_METRIC, 1); - return; - } - - LOG.debug("Subscriber HTTP Response Status Code match successful: {}", subscriberResponse, - HTTPUtils.HTTP_SUCCESS_STATUS_CODE); - - final List actualMessages = subscriberResponse.getFetchedMessages(); - - // If there are no message returned during from Subscriber, nothing to write to CDAP Stream - if (actualMessages.isEmpty()) { - LOG.debug("Subscriber Response has no messages. Nothing to write to CDAP stream...."); - metrics.count(CDAPMetricsConstants.TCA_SUBSCRIBER_RESPONSES_WITH_NO_MESSAGES_METRIC, 1); - return; - } - - LOG.debug("DMaaP MR Subscriber found new messages in DMaaP Topic. Message count: {}", actualMessages.size()); - metrics.count(CDAPMetricsConstants.TCA_SUBSCRIBER_TOTAL_MESSAGES_PROCESSED_METRIC, actualMessages.size()); - - // Write message to CDAP Stream using Stream Batch Writer - LOG.debug("Writing message to CDAP Stream: {}, Message Count: {}", cdapStreamName, actualMessages.size()); - try { - - for (String message : actualMessages) { - workerContext.write(cdapStreamName, message); - } - - } catch (IOException e) { - metrics.count(CDAPMetricsConstants.TCA_SUBSCRIBER_FAILURE_TO_WRITE_TO_STREAM_METRIC, 1); - final String errorMessage = - format("Error while DMaaP message router subscriber attempting to write to CDAP Stream: %s, " + - "Exception: %s", cdapStreamName, e); - throw new DCAEAnalyticsRuntimeException(errorMessage, LOG, e); - } - - LOG.debug("DMaaP MR Subscriber successfully finished writing messages to CDAP Stream: {}, Message count: {}", - cdapStreamName, actualMessages.size()); - } - - - /** - * Get Subscriber response and records time taken to fetch messages. Returns Optional.None if Subscriber response - * is null or response status code is not present - * - * @param subscriber - DMaaP Subscriber - * @param metrics - CDAP Metrics collector - * - * @return - Optional of Subscriber Response - */ - private static Optional getSubscriberResponse(final DMaaPMRSubscriber subscriber, - final Metrics metrics) { - - // Check how long it took for subscriber to respond - final Stopwatch stopwatch = new Stopwatch(); - stopwatch.start(); - - DMaaPMRSubscriberResponse subscriberResponse = null; - // Fetch messages from DMaaP MR Topic - try { - subscriberResponse = subscriber.fetchMessages(); - } catch (DCAEAnalyticsRuntimeException e) { - LOG.error("Error while fetching messages for DMaaP MR Topic: {}", e); - } - - stopwatch.stop(); - final long subscriberResponseTimeMS = stopwatch.elapsedMillis(); - - // If response is null is null or response code is null, unable to proceed nothing to do - if (subscriberResponse == null || subscriberResponse.getResponseCode() == null) { - LOG.error("Subscriber Response is null or subscriber Response code is null. Unable to proceed further..."); - return Optional.absent(); - } - - LOG.debug("Subscriber Response:{}, Subscriber HTTP Response Status Code {}, Subscriber Response Time(ms): {}", - subscriberResponse, subscriberResponse.getResponseCode(), subscriberResponseTimeMS); - - // Record subscriber response time - metrics.gauge(CDAPMetricsConstants.TCA_SUBSCRIBER_RESPONSE_TIME_MS_METRIC, subscriberResponseTimeMS); - - // Record all response count from subscriber - metrics.count(CDAPMetricsConstants.TCA_SUBSCRIBER_ALL_RESPONSES_COUNT_METRIC, 1); - - return Optional.of(subscriberResponse); - } - -} diff --git a/dcae-analytics-tca/src/main/java/org/openecomp/dcae/analytics/tca/worker/TCADMaaPMockSubscriberWorker.java b/dcae-analytics-tca/src/main/java/org/openecomp/dcae/analytics/tca/worker/TCADMaaPMockSubscriberWorker.java deleted file mode 100644 index d8d1968..0000000 --- a/dcae-analytics-tca/src/main/java/org/openecomp/dcae/analytics/tca/worker/TCADMaaPMockSubscriberWorker.java +++ /dev/null @@ -1,139 +0,0 @@ -/* - * ============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.analytics.tca.worker; - -import co.cask.cdap.api.annotation.Property; -import co.cask.cdap.api.worker.AbstractWorker; -import co.cask.cdap.api.worker.WorkerContext; -import com.fasterxml.jackson.core.type.TypeReference; -import org.openecomp.dcae.analytics.common.exception.DCAEAnalyticsRuntimeException; -import org.openecomp.dcae.analytics.model.domain.cef.EventListener; -import org.openecomp.dcae.analytics.tca.settings.TCAAppPreferences; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.io.FileNotFoundException; -import java.io.IOException; -import java.io.InputStream; -import java.util.List; - -import static org.openecomp.dcae.analytics.tca.utils.TCAUtils.getValidatedTCAAppPreferences; -import static org.openecomp.dcae.analytics.tca.utils.TCAUtils.readValue; -import static org.openecomp.dcae.analytics.tca.utils.TCAUtils.writeValueAsString; - -/** - * CDAP Worker which mocks fetching VES Messages from DMaaP MR topic. - * The mock instead of making DMaaP MR calls will actually take messages - * from file and send them to stream at subscriber polling interval - * - * TODO: To be removed before going to production - only for testing purposes - * - * @author Rajiv Singla. Creation Date: 11/4/2016. - */ -public class TCADMaaPMockSubscriberWorker extends AbstractWorker { - - private static final Logger LOG = LoggerFactory.getLogger(TCADMaaPMockSubscriberWorker.class); - - // TODO: Remove this file before going to production - only for mocking purposes - private static final String MOCK_MESSAGE_FILE_LOCATION = "ves_mock_messages.json"; - private static final TypeReference> EVENT_LISTENER_TYPE_REFERENCE = - new TypeReference>() { - }; - - private TCAAppPreferences tcaAppPreferences; - private boolean stopSendingMessages; - @Property - private final String tcaSubscriberOutputStreamName; - - public TCADMaaPMockSubscriberWorker(final String tcaSubscriberOutputStreamName) { - this.tcaSubscriberOutputStreamName = tcaSubscriberOutputStreamName; - } - - @Override - public void configure() { - setName("MockTCASubscriberWorker"); - setDescription("Writes Mocked VES messages to CDAP Stream"); - LOG.info("Configuring Mock TCA MR DMaaP Subscriber worker with name: {}", "MockTCASubscriberWorker"); - } - - @Override - public void initialize(WorkerContext context) throws Exception { - super.initialize(context); - - final TCAAppPreferences tcaAppPreferences = getValidatedTCAAppPreferences(context); - LOG.info("Initializing Mock TCA MR DMaaP Subscriber worker with preferences: {}", tcaAppPreferences); - this.tcaAppPreferences = tcaAppPreferences; - this.stopSendingMessages = false; - } - - - @Override - public void run() { - final Integer subscriberPollingInterval = tcaAppPreferences.getSubscriberPollingInterval(); - LOG.debug("Mock TCA Subscriber Polling interval: {}", subscriberPollingInterval); - - final InputStream resourceAsStream = Thread.currentThread().getContextClassLoader().getResourceAsStream - (MOCK_MESSAGE_FILE_LOCATION); - - if (resourceAsStream == null) { - LOG.error("Unable to find file at location: {}", MOCK_MESSAGE_FILE_LOCATION); - throw new DCAEAnalyticsRuntimeException("Unable to find file", LOG, new FileNotFoundException()); - } - - - try { - List eventListeners = readValue(resourceAsStream, EVENT_LISTENER_TYPE_REFERENCE); - - final int totalMessageCount = eventListeners.size(); - LOG.debug("Mock message count to be written to cdap stream: ()", totalMessageCount); - - int i = 1; - for (EventListener eventListener : eventListeners) { - if (stopSendingMessages) { - LOG.debug("Stop sending messages......"); - break; - } - final String eventListenerString = writeValueAsString(eventListener); - LOG.debug("=======>> Writing message to cdap stream no: {} of {}", i, totalMessageCount); - getContext().write(tcaSubscriberOutputStreamName, eventListenerString); - i++; - try { - Thread.sleep(subscriberPollingInterval); - } catch (InterruptedException e) { - LOG.error("Error while sleeping"); - } - } - - LOG.debug("Finished writing mock messages to CDAP Stream"); - - } catch (IOException e) { - LOG.error("Error while parsing json file"); - throw new DCAEAnalyticsRuntimeException("Error while parsing mock json file", LOG, e); - } - - - } - - @Override - public void stop() { - stopSendingMessages = true; - } -} diff --git a/dcae-analytics-tca/src/main/java/org/openecomp/dcae/analytics/tca/worker/TCADMaaPPublisherWorker.java b/dcae-analytics-tca/src/main/java/org/openecomp/dcae/analytics/tca/worker/TCADMaaPPublisherWorker.java deleted file mode 100644 index 49f98fc..0000000 --- a/dcae-analytics-tca/src/main/java/org/openecomp/dcae/analytics/tca/worker/TCADMaaPPublisherWorker.java +++ /dev/null @@ -1,230 +0,0 @@ -/* - * ============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.analytics.tca.worker; - -import co.cask.cdap.api.annotation.Property; -import co.cask.cdap.api.metrics.Metrics; -import co.cask.cdap.api.worker.AbstractWorker; -import co.cask.cdap.api.worker.WorkerContext; -import org.openecomp.dcae.analytics.common.AnalyticsConstants; -import org.openecomp.dcae.analytics.common.CDAPComponentsConstants; -import org.openecomp.dcae.analytics.common.exception.DCAEAnalyticsRuntimeException; -import org.openecomp.dcae.analytics.dmaap.DMaaPMRFactory; -import org.openecomp.dcae.analytics.dmaap.domain.config.DMaaPMRPublisherConfig; -import org.openecomp.dcae.analytics.dmaap.service.publisher.DMaaPMRPublisher; -import org.openecomp.dcae.analytics.model.util.AnalyticsModelIOUtils; -import org.openecomp.dcae.analytics.tca.settings.TCAAppPreferences; -import org.openecomp.dcae.analytics.tca.utils.AppPreferencesToPublisherConfigMapper; -import org.openecomp.dcae.analytics.tca.utils.TCAUtils; -import org.quartz.JobBuilder; -import org.quartz.JobDataMap; -import org.quartz.JobDetail; -import org.quartz.Scheduler; -import org.quartz.SchedulerException; -import org.quartz.SimpleScheduleBuilder; -import org.quartz.SimpleTrigger; -import org.quartz.TriggerBuilder; -import org.quartz.impl.StdSchedulerFactory; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.util.Properties; -import java.util.concurrent.atomic.AtomicBoolean; - -import static java.lang.String.format; - -/** - * TCA DMaaP Publisher will monitor alerts table at regular intervals and publish any alerts to DMaaP MR Publishing - * Topic - *

- * @author Rajiv Singla. Creation Date: 11/16/2016. - */ -public class TCADMaaPPublisherWorker extends AbstractWorker { - - private static final Logger LOG = LoggerFactory.getLogger(TCADMaaPPublisherWorker.class); - - /** - * DMaaP MR Publisher - */ - private DMaaPMRPublisher publisher; - /** - * Quartz Scheduler - */ - private Scheduler scheduler; - /** - * Determines if scheduler is shutdown - */ - private AtomicBoolean isSchedulerShutdown; - /** - * Store runtime metrics - */ - private Metrics metrics; - - @Property - private final String tcaVESAlertsTableName; - - public TCADMaaPPublisherWorker(final String tcaVESAlertsTableName) { - this.tcaVESAlertsTableName = tcaVESAlertsTableName; - } - - @Override - public void configure() { - // configure - setName(CDAPComponentsConstants.TCA_FIXED_DMAAP_PUBLISHER_WORKER); - setDescription(CDAPComponentsConstants.TCA_FIXED_DMAAP_PUBLISHER_DESCRIPTION_WORKER); - LOG.debug("Configuring TCA MR DMaaP Publisher worker with name: {}", - CDAPComponentsConstants.TCA_FIXED_DMAAP_PUBLISHER_WORKER); - } - - - @Override - public void initialize(WorkerContext context) throws Exception { - super.initialize(context); - - // Parse runtime arguments - final TCAAppPreferences tcaAppPreferences = TCAUtils.getValidatedTCAAppPreferences(context); - - LOG.info("Initializing TCA MR DMaaP Publisher worker with preferences: {}", tcaAppPreferences); - - // Map TCA App Preferences to DMaaP MR Publisher Config - final DMaaPMRPublisherConfig publisherConfig = AppPreferencesToPublisherConfigMapper.map(tcaAppPreferences); - - LOG.info("TCA DMaaP MR Publisher worker will be polling TCA Alerts Table Name: {}", tcaVESAlertsTableName); - - // Create an instance of DMaaP MR Publisher - LOG.debug("Creating an instance of DMaaP Publisher"); - publisher = DMaaPMRFactory.create().createPublisher(publisherConfig); - - // initialize a new Quartz scheduler - initializeScheduler(tcaAppPreferences); - // initialize scheduler state - isSchedulerShutdown = new AtomicBoolean(true); - } - - - @Override - public void run() { - // Start Publisher scheduler - try { - scheduler.start(); - isSchedulerShutdown.getAndSet(false); - - } catch (SchedulerException e) { - final String errorMessage = - format("Error while starting TCA DMaaP MR Publisher scheduler: %s", e.toString()); - throw new DCAEAnalyticsRuntimeException(errorMessage, LOG, e); - } - - LOG.info("TCA DMaaP MR Publisher Scheduler started successfully"); - - // indefinite loop which wakes up and confirms scheduler is indeed running - while (!isSchedulerShutdown.get()) { - try { - - Thread.sleep(AnalyticsConstants.TCA_DEFAULT_WORKER_SHUTDOWN_CHECK_INTERVAL_MS); - - } catch (InterruptedException e) { - - final String errorMessage = - format("Error while checking TCA DMaaP MR Publisher worker status: %s", e); - throw new DCAEAnalyticsRuntimeException(errorMessage, LOG, e); - } - } - - LOG.info("Finished execution of TCA DMaaP MR Publisher worker thread"); - - } - - @Override - public void stop() { - // Stop Publisher - which will flush any batch messages if present - try { - publisher.close(); - } catch (Exception e) { - - final String errorMessage = format("Error while shutting down DMaaP MR Publisher: %s", e); - throw new DCAEAnalyticsRuntimeException(errorMessage, LOG, e); - - } - // Stop Publisher scheduler - try { - - LOG.info("Shutting TCA DMaaP MR Publisher Scheduler"); - - scheduler.shutdown(); - isSchedulerShutdown.getAndSet(true); - - } catch (SchedulerException e) { - - final String errorMessage = - format("Error while shutting down TCA DMaaP MR Publisher scheduler: %s", e); - throw new DCAEAnalyticsRuntimeException(errorMessage, LOG, e); - } - } - - - /** - * Initializes a scheduler instance for DMaaP MR Publisher Job - * - * @throws SchedulerException SchedulerException - */ - private void initializeScheduler(TCAAppPreferences tcaAnalyticsAppConfig) throws SchedulerException { - - // Initialize a new Quartz Standard scheduler - settings settings are in quartz-publisher.properties file - final StdSchedulerFactory stdSchedulerFactory = new StdSchedulerFactory(); - final String quartzPublisherPropertiesFileName = AnalyticsConstants.TCA_QUARTZ_PUBLISHER_PROPERTIES_FILE_NAME; - LOG.debug("Configuring quartz scheduler for TCA DMaaP MR Publisher with properties file: {}", - quartzPublisherPropertiesFileName); - final Properties publisherProperties = - AnalyticsModelIOUtils.loadPropertiesFile(quartzPublisherPropertiesFileName); - stdSchedulerFactory.initialize(publisherProperties); - scheduler = stdSchedulerFactory.getScheduler(); - - // Create a new JobDataMap containing information required by TCA DMaaP Publisher Job - final JobDataMap jobDataMap = new JobDataMap(); - jobDataMap.put(AnalyticsConstants.CDAP_ALERTS_TABLE_VARIABLE_NAME, tcaVESAlertsTableName); - jobDataMap.put(AnalyticsConstants.WORKER_CONTEXT_VARIABLE_NAME, getContext()); - jobDataMap.put(AnalyticsConstants.DMAAP_PUBLISHER_VARIABLE_NAME, publisher); - jobDataMap.put(AnalyticsConstants.DMAAP_METRICS_VARIABLE_NAME, metrics); - - // Create a new job detail - final JobDetail jobDetail = JobBuilder.newJob(TCADMaaPMRPublisherJob.class) - .withIdentity(AnalyticsConstants.TCA_DMAAP_PUBLISHER_QUARTZ_JOB_NAME, - AnalyticsConstants.TCA_QUARTZ_GROUP_NAME) - .usingJobData(jobDataMap).build(); - - // Create a new scheduling builder - final Integer publisherPollingInterval = tcaAnalyticsAppConfig.getPublisherPollingInterval(); - final SimpleScheduleBuilder simpleScheduleBuilder = SimpleScheduleBuilder.simpleSchedule() - .withIntervalInMilliseconds(publisherPollingInterval) // job will use custom polling schedule - .repeatForever(); // repeats while worker is running - - // Create a trigger for the TCA Publisher Job - final SimpleTrigger simpleTrigger = TriggerBuilder.newTrigger() - .withIdentity(AnalyticsConstants.TCA_DMAAP_PUBLISHER_QUARTZ_TRIGGER_NAME, - AnalyticsConstants.TCA_QUARTZ_GROUP_NAME) - .startNow() // job starts right away - .withSchedule(simpleScheduleBuilder).build(); - - scheduler.scheduleJob(jobDetail, simpleTrigger); - LOG.info("Initialized TCA DMaaP MR Publisher Scheduler"); - } -} diff --git a/dcae-analytics-tca/src/main/java/org/openecomp/dcae/analytics/tca/worker/TCADMaaPSubscriberWorker.java b/dcae-analytics-tca/src/main/java/org/openecomp/dcae/analytics/tca/worker/TCADMaaPSubscriberWorker.java deleted file mode 100644 index 8d773e4..0000000 --- a/dcae-analytics-tca/src/main/java/org/openecomp/dcae/analytics/tca/worker/TCADMaaPSubscriberWorker.java +++ /dev/null @@ -1,226 +0,0 @@ -/* - * ============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.analytics.tca.worker; - -import co.cask.cdap.api.annotation.Property; -import co.cask.cdap.api.metrics.Metrics; -import co.cask.cdap.api.worker.AbstractWorker; -import co.cask.cdap.api.worker.WorkerContext; -import org.openecomp.dcae.analytics.common.AnalyticsConstants; -import org.openecomp.dcae.analytics.common.CDAPComponentsConstants; -import org.openecomp.dcae.analytics.common.exception.DCAEAnalyticsRuntimeException; -import org.openecomp.dcae.analytics.dmaap.DMaaPMRFactory; -import org.openecomp.dcae.analytics.dmaap.domain.config.DMaaPMRSubscriberConfig; -import org.openecomp.dcae.analytics.dmaap.service.subscriber.DMaaPMRSubscriber; -import org.openecomp.dcae.analytics.model.util.AnalyticsModelIOUtils; -import org.openecomp.dcae.analytics.tca.settings.TCAAppPreferences; -import org.openecomp.dcae.analytics.tca.utils.AppPreferencesToSubscriberConfigMapper; -import org.openecomp.dcae.analytics.tca.utils.TCAUtils; -import org.quartz.JobBuilder; -import org.quartz.JobDataMap; -import org.quartz.JobDetail; -import org.quartz.Scheduler; -import org.quartz.SchedulerException; -import org.quartz.SimpleScheduleBuilder; -import org.quartz.SimpleTrigger; -import org.quartz.TriggerBuilder; -import org.quartz.impl.StdSchedulerFactory; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.util.Properties; -import java.util.concurrent.atomic.AtomicBoolean; - -import static java.lang.String.format; - -/** - * TCA DMaaP Subscriber will read messages and post them to cdap stream at regular intervals - *

- * @author Rajiv Singla. Creation Date: 10/14/2016. - */ -public class TCADMaaPSubscriberWorker extends AbstractWorker { - - private static final Logger LOG = LoggerFactory.getLogger(TCADMaaPSubscriberWorker.class); - - /** - * DMaaP MR Subscriber - */ - private DMaaPMRSubscriber subscriber; - /** - * Quartz Scheduler - */ - private Scheduler scheduler; - /** - * Determines if scheduler is shutdown - */ - private AtomicBoolean isSchedulerShutdown; - /** - * Store runtime metrics - */ - private Metrics metrics; - - /** - * CDAP output stream name - */ - @Property - private final String tcaSubscriberOutputStreamName; - - public TCADMaaPSubscriberWorker(final String tcaSubscriberOutputStreamName) { - this.tcaSubscriberOutputStreamName = tcaSubscriberOutputStreamName; - } - - - @Override - public void configure() { - // configure - setName(CDAPComponentsConstants.TCA_FIXED_DMAAP_SUBSCRIBER_WORKER); - setDescription(CDAPComponentsConstants.TCA_FIXED_DMAAP_SUBSCRIBER_DESCRIPTION_WORKER); - LOG.debug("Configuring TCA MR DMaaP Subscriber worker with name: {}", - CDAPComponentsConstants.TCA_FIXED_DMAAP_SUBSCRIBER_WORKER); - } - - @Override - public void initialize(WorkerContext context) throws Exception { - super.initialize(context); - - // Parse runtime arguments - final TCAAppPreferences tcaAppPreferences = TCAUtils.getValidatedTCAAppPreferences(context); - - LOG.info("Initializing TCA MR DMaaP Subscriber worker with preferences: {}", tcaAppPreferences); - - // Map TCA App Preferences to DMaaP MR Subscriber Config - final DMaaPMRSubscriberConfig subscriberConfig = AppPreferencesToSubscriberConfigMapper.map(tcaAppPreferences); - - LOG.info("TCA DMaaP MR Subscriber worker will be writing to CDAP Stream: {}", tcaSubscriberOutputStreamName); - - // Create an instance of DMaaP MR Subscriber - LOG.debug("Creating an instance of DMaaP Subscriber"); - subscriber = DMaaPMRFactory.create().createSubscriber(subscriberConfig); - - // initialize a new Quartz scheduler - initializeScheduler(tcaAppPreferences); - - // initialize scheduler state - isSchedulerShutdown = new AtomicBoolean(true); - } - - - @Override - public void run() { - // Start scheduler - try { - scheduler.start(); - isSchedulerShutdown.getAndSet(false); - - } catch (SchedulerException e) { - final String errorMessage = - format("Error while starting TCA DMaaP MR Subscriber scheduler: %s", e.toString()); - throw new DCAEAnalyticsRuntimeException(errorMessage, LOG, e); - } - - LOG.info("TCA DMaaP MR Subscriber Scheduler started successfully"); - - // indefinite loop which wakes up and confirms scheduler is indeed running - while (!isSchedulerShutdown.get()) { - try { - - Thread.sleep(AnalyticsConstants.TCA_DEFAULT_WORKER_SHUTDOWN_CHECK_INTERVAL_MS); - - } catch (InterruptedException e) { - - final String errorMessage = - format("Error while checking TCA DMaaP MR Subscriber worker status: %s", e); - throw new DCAEAnalyticsRuntimeException(errorMessage, LOG, e); - } - } - - LOG.info("Finished execution of TCA DMaaP MR Subscriber worker thread"); - - } - - @Override - public void stop() { - - // Stop scheduler - try { - - LOG.info("Shutting TCA DMaaP MR Subscriber Scheduler"); - - scheduler.shutdown(); - isSchedulerShutdown.getAndSet(true); - - } catch (SchedulerException e) { - - final String errorMessage = - format("Error while shutting down TCA DMaaP MR Subscriber scheduler: %s", e); - throw new DCAEAnalyticsRuntimeException(errorMessage, LOG, e); - } - } - - /** - * Initializes a scheduler instance for DMaaP MR Subscriber Job - * - * @throws SchedulerException SchedulerException - */ - private void initializeScheduler(TCAAppPreferences tcaAppPreferences) throws SchedulerException { - - // Initialize a new Quartz Standard scheduler - settings settings are in quartz-subscriber.properties file - final StdSchedulerFactory stdSchedulerFactory = new StdSchedulerFactory(); - final String quartzSubscriberPropertiesFileName = AnalyticsConstants.TCA_QUARTZ_SUBSCRIBER_PROPERTIES_FILE_NAME; - LOG.debug("Configuring quartz scheduler for TCA DMaaP MR Subscriber with properties file: {}", - quartzSubscriberPropertiesFileName); - final Properties subscriberProperties = - AnalyticsModelIOUtils.loadPropertiesFile(quartzSubscriberPropertiesFileName); - stdSchedulerFactory.initialize(subscriberProperties); - scheduler = stdSchedulerFactory.getScheduler(); - - // Create a new JobDataMap containing information required by TCA DMaaP Subscriber Job - final JobDataMap jobDataMap = new JobDataMap(); - jobDataMap.put(AnalyticsConstants.CDAP_STREAM_VARIABLE_NAME, tcaSubscriberOutputStreamName); - jobDataMap.put(AnalyticsConstants.WORKER_CONTEXT_VARIABLE_NAME, getContext()); - jobDataMap.put(AnalyticsConstants.DMAAP_SUBSCRIBER_VARIABLE_NAME, subscriber); - jobDataMap.put(AnalyticsConstants.DMAAP_METRICS_VARIABLE_NAME, metrics); - - // Create a new job detail - final JobDetail jobDetail = JobBuilder.newJob(TCADMaaPMRSubscriberJob.class) - .withIdentity(AnalyticsConstants.TCA_DMAAP_SUBSCRIBER_QUARTZ_JOB_NAME, - AnalyticsConstants.TCA_QUARTZ_GROUP_NAME) - .usingJobData(jobDataMap).build(); - - // Create a new scheduling builder - final Integer subscriberPollingInterval = tcaAppPreferences.getSubscriberPollingInterval(); - final SimpleScheduleBuilder simpleScheduleBuilder = SimpleScheduleBuilder.simpleSchedule() - .withIntervalInMilliseconds(subscriberPollingInterval) // job will use custom polling schedule - .repeatForever(); // repeats while worker is running - - // Create a trigger for the TCA Subscriber Job - final SimpleTrigger simpleTrigger = TriggerBuilder.newTrigger() - .withIdentity(AnalyticsConstants.TCA_DMAAP_SUBSCRIBER_QUARTZ_TRIGGER_NAME, - AnalyticsConstants.TCA_QUARTZ_GROUP_NAME) - .startNow() // job starts right away - .withSchedule(simpleScheduleBuilder).build(); - - scheduler.scheduleJob(jobDetail, simpleTrigger); - LOG.info("Initialized TCA DMaaP MR Subscriber Scheduler"); - } - - -} diff --git a/dcae-analytics-tca/src/main/java/org/openecomp/dcae/apod/analytics/tca/TCAAnalyticsApplication.java b/dcae-analytics-tca/src/main/java/org/openecomp/dcae/apod/analytics/tca/TCAAnalyticsApplication.java new file mode 100644 index 0000000..49e35b1 --- /dev/null +++ b/dcae-analytics-tca/src/main/java/org/openecomp/dcae/apod/analytics/tca/TCAAnalyticsApplication.java @@ -0,0 +1,105 @@ +/* + * ============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.tca; + +import co.cask.cdap.api.app.AbstractApplication; +import co.cask.cdap.api.data.stream.Stream; +import co.cask.cdap.api.dataset.DatasetProperties; +import co.cask.cdap.api.dataset.lib.ObjectMappedTable; +import org.openecomp.dcae.apod.analytics.common.utils.ValidationUtils; +import org.openecomp.dcae.apod.analytics.tca.flow.TCAVESCollectorFlow; +import org.openecomp.dcae.apod.analytics.tca.persistance.TCAMessageStatusPersister; +import org.openecomp.dcae.apod.analytics.tca.persistance.TCAVESAlertsPersister; +import org.openecomp.dcae.apod.analytics.tca.settings.TCAAppConfig; +import org.openecomp.dcae.apod.analytics.tca.validator.TCAAppConfigValidator; +import org.openecomp.dcae.apod.analytics.tca.worker.TCADMaaPMockSubscriberWorker; +import org.openecomp.dcae.apod.analytics.tca.worker.TCADMaaPPublisherWorker; +import org.openecomp.dcae.apod.analytics.tca.worker.TCADMaaPSubscriberWorker; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import static org.openecomp.dcae.apod.analytics.common.CDAPComponentsConstants.TCA_FIXED_SUBSCRIBER_OUTPUT_DESCRIPTION_STREAM; + +/** + * @author Rajiv Singla. Creation Date: 10/21/2016. + */ +public class TCAAnalyticsApplication extends AbstractApplication { + + private static final Logger LOG = LoggerFactory.getLogger(TCAAnalyticsApplication.class); + + @Override + public void configure() { + + + // ========= Application configuration Setup ============== // + final TCAAppConfig tcaAppConfig = getConfig(); + + LOG.info("Configuring TCA Application with startup application configuration: {}", tcaAppConfig); + + // Validate application configuration + ValidationUtils.validateSettings(tcaAppConfig, new TCAAppConfigValidator()); + + // App Setup + setName(tcaAppConfig.getAppName()); + setDescription(tcaAppConfig.getAppDescription()); + + // ========== Streams Setup ============== // + // Create DMaaP MR Subscriber CDAP output stream + final String tcaSubscriberOutputStreamName = tcaAppConfig.getTcaSubscriberOutputStreamName(); + LOG.info("Creating TCA VES Output Stream: {}", tcaSubscriberOutputStreamName); + final Stream subscriberOutputStream = + new Stream(tcaSubscriberOutputStreamName, TCA_FIXED_SUBSCRIBER_OUTPUT_DESCRIPTION_STREAM); + addStream(subscriberOutputStream); + + + // ============ Datasets Setup ======== // + // Create TCA Message Status Table + final String tcaVESMessageStatusTableName = tcaAppConfig.getTcaVESMessageStatusTableName(); + final Integer messageStatusTableTTLSeconds = tcaAppConfig.getTcaVESMessageStatusTableTTLSeconds(); + LOG.info("Creating TCA Message Status Table: {} with TTL: {}", + tcaVESMessageStatusTableName, messageStatusTableTTLSeconds); + final DatasetProperties messageStatusTableProperties = + TCAMessageStatusPersister.getDatasetProperties(messageStatusTableTTLSeconds); + createDataset(tcaVESMessageStatusTableName, ObjectMappedTable.class, messageStatusTableProperties); + + // Create TCA VES Alerts Table + final String tcaVESAlertsTableName = tcaAppConfig.getTcaVESAlertsTableName(); + final Integer alertsTableTTLSeconds = tcaAppConfig.getTcaVESAlertsTableTTLSeconds(); + LOG.info("Creating TCA Alerts Table: {} with TTL: {}", + tcaVESAlertsTableName, alertsTableTTLSeconds); + final DatasetProperties alertTableProperties = + TCAVESAlertsPersister.getDatasetProperties(alertsTableTTLSeconds); + createDataset(tcaVESAlertsTableName, ObjectMappedTable.class, alertTableProperties); + + // =========== Flow Setup ============= // + addFlow(new TCAVESCollectorFlow(tcaAppConfig)); + + // ========== Workers Setup =========== // + LOG.info("Creating TCA DMaaP Subscriber Worker"); + addWorker(new TCADMaaPSubscriberWorker(tcaAppConfig.getTcaSubscriberOutputStreamName())); + LOG.info("Creating TCA DMaaP Publisher Worker"); + addWorker(new TCADMaaPPublisherWorker(tcaAppConfig.getTcaVESAlertsTableName())); + // TODO: Remove this before going to production + addWorker(new TCADMaaPMockSubscriberWorker(tcaAppConfig.getTcaSubscriberOutputStreamName())); + } + + +} diff --git a/dcae-analytics-tca/src/main/java/org/openecomp/dcae/apod/analytics/tca/flow/TCAVESCollectorFlow.java b/dcae-analytics-tca/src/main/java/org/openecomp/dcae/apod/analytics/tca/flow/TCAVESCollectorFlow.java new file mode 100644 index 0000000..b62bc35 --- /dev/null +++ b/dcae-analytics-tca/src/main/java/org/openecomp/dcae/apod/analytics/tca/flow/TCAVESCollectorFlow.java @@ -0,0 +1,69 @@ +/* + * ============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.tca.flow; + +import co.cask.cdap.api.flow.AbstractFlow; +import org.openecomp.dcae.apod.analytics.common.CDAPComponentsConstants; +import org.openecomp.dcae.apod.analytics.tca.flowlet.TCAVESAlertsSinkFlowlet; +import org.openecomp.dcae.apod.analytics.tca.flowlet.TCAVESMessageRouterFlowlet; +import org.openecomp.dcae.apod.analytics.tca.flowlet.TCAVESThresholdViolationCalculatorFlowlet; +import org.openecomp.dcae.apod.analytics.tca.settings.TCAAppConfig; + +/** + * TCA Flow for VES (Virtual Event Streaming) Collector Flow + * + * @author Rajiv Singla. Creation Date: 11/3/2016. + */ +public class TCAVESCollectorFlow extends AbstractFlow { + + private final TCAAppConfig tcaAppConfig; + + public TCAVESCollectorFlow(TCAAppConfig tcaAppConfig) { + this.tcaAppConfig = tcaAppConfig; + } + + @Override + protected void configure() { + + setName(CDAPComponentsConstants.TCA_FIXED_VES_COLLECTOR_NAME_FLOW); + setDescription(CDAPComponentsConstants.TCA_FIXED_VES_COLLECTOR_DESCRIPTION_FLOW); + + final TCAVESMessageRouterFlowlet messageRouterFlowlet = new TCAVESMessageRouterFlowlet(); + addFlowlet(messageRouterFlowlet); + + final TCAVESThresholdViolationCalculatorFlowlet thresholdViolationCalculatorFlowlet = + new TCAVESThresholdViolationCalculatorFlowlet(tcaAppConfig.getTcaVESMessageStatusTableName()); + addFlowlet(thresholdViolationCalculatorFlowlet, tcaAppConfig.getThresholdCalculatorFlowletInstances()); + + final TCAVESAlertsSinkFlowlet alertsSinkFlowlet = + new TCAVESAlertsSinkFlowlet(tcaAppConfig.getTcaVESAlertsTableName()); + addFlowlet(alertsSinkFlowlet); + + + // connect DMaaP MR VES Subscriber output stream to VES Message Router Flowlet + connectStream(tcaAppConfig.getTcaSubscriberOutputStreamName(), messageRouterFlowlet); + // connect message router to VES threshold calculator + connect(messageRouterFlowlet, thresholdViolationCalculatorFlowlet); + // connect VES threshold calculator flowlet to Alerts Sink Flowlet + connect(thresholdViolationCalculatorFlowlet, alertsSinkFlowlet); + + } +} diff --git a/dcae-analytics-tca/src/main/java/org/openecomp/dcae/apod/analytics/tca/flowlet/TCAVESAlertsSinkFlowlet.java b/dcae-analytics-tca/src/main/java/org/openecomp/dcae/apod/analytics/tca/flowlet/TCAVESAlertsSinkFlowlet.java new file mode 100644 index 0000000..0277635 --- /dev/null +++ b/dcae-analytics-tca/src/main/java/org/openecomp/dcae/apod/analytics/tca/flowlet/TCAVESAlertsSinkFlowlet.java @@ -0,0 +1,69 @@ +/* + * ============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.tca.flowlet; + +import co.cask.cdap.api.annotation.ProcessInput; +import co.cask.cdap.api.annotation.Property; +import co.cask.cdap.api.dataset.lib.ObjectMappedTable; +import co.cask.cdap.api.flow.flowlet.AbstractFlowlet; +import co.cask.cdap.api.flow.flowlet.FlowletContext; +import org.openecomp.dcae.apod.analytics.tca.persistance.TCAVESAlertEntity; +import org.openecomp.dcae.apod.analytics.tca.persistance.TCAVESAlertsPersister; + +import static org.openecomp.dcae.apod.analytics.common.CDAPComponentsConstants.TCA_FIXED_VES_ALERTS_SINK_DESCRIPTION_FLOWLET; +import static org.openecomp.dcae.apod.analytics.common.CDAPComponentsConstants.TCA_FIXED_VES_ALERTS_SINK_NAME_FLOWLET; +import static org.openecomp.dcae.apod.analytics.common.CDAPComponentsConstants.TCA_FIXED_VES_TCA_CALCULATOR_NAME_OUTPUT; + +/** + * Saves TCA VES Alert Messages in a Time series Table + * + * @author Rajiv Singla. Creation Date: 11/15/2016. + */ +public class TCAVESAlertsSinkFlowlet extends AbstractFlowlet { + + @Property + private final String tcaVESAlertsTableName; + + private ObjectMappedTable tcaVESAlertsTable; + + public TCAVESAlertsSinkFlowlet(String tcaVESAlertsTableName) { + this.tcaVESAlertsTableName = tcaVESAlertsTableName; + } + + @Override + public void configure() { + setName(TCA_FIXED_VES_ALERTS_SINK_NAME_FLOWLET); + setDescription(TCA_FIXED_VES_ALERTS_SINK_DESCRIPTION_FLOWLET); + } + + @Override + public void initialize(FlowletContext flowletContext) throws Exception { + super.initialize(flowletContext); + tcaVESAlertsTable = getContext().getDataset(tcaVESAlertsTableName); + } + + @ProcessInput(TCA_FIXED_VES_TCA_CALCULATOR_NAME_OUTPUT) + public void saveAlerts(String alertMessage) { + // Saves alert message in alerts table + TCAVESAlertsPersister.persist(alertMessage, tcaVESAlertsTable); + } + +} diff --git a/dcae-analytics-tca/src/main/java/org/openecomp/dcae/apod/analytics/tca/flowlet/TCAVESMessageRouterFlowlet.java b/dcae-analytics-tca/src/main/java/org/openecomp/dcae/apod/analytics/tca/flowlet/TCAVESMessageRouterFlowlet.java new file mode 100644 index 0000000..102b116 --- /dev/null +++ b/dcae-analytics-tca/src/main/java/org/openecomp/dcae/apod/analytics/tca/flowlet/TCAVESMessageRouterFlowlet.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.tca.flowlet; + +import co.cask.cdap.api.annotation.Output; +import co.cask.cdap.api.annotation.ProcessInput; +import co.cask.cdap.api.flow.flowlet.AbstractFlowlet; +import co.cask.cdap.api.flow.flowlet.OutputEmitter; +import co.cask.cdap.api.flow.flowlet.StreamEvent; +import com.google.common.base.Charsets; + +import static org.openecomp.dcae.apod.analytics.common.AnalyticsConstants.TCA_VES_MESSAGE_ROUTER_PARTITION_KEY; +import static org.openecomp.dcae.apod.analytics.common.CDAPComponentsConstants.TCA_FIXED_VES_MESSAGE_ROUTER_DESCRIPTION_FLOWLET; +import static org.openecomp.dcae.apod.analytics.common.CDAPComponentsConstants.TCA_FIXED_VES_MESSAGE_ROUTER_NAME_FLOWLET; +import static org.openecomp.dcae.apod.analytics.common.CDAPComponentsConstants.TCA_FIXED_VES_MESSAGE_ROUTER_OUTPUT; + + +/** + * TCA Message Router Flowlet emits VES Message to {@link TCAVESThresholdViolationCalculatorFlowlet} instances + * + * @author Rajiv Singla. Creation Date: 11/14/2016. + */ +public class TCAVESMessageRouterFlowlet extends AbstractFlowlet { + + /** + * Emits ves message to TCA Calculator Instances + */ + @Output(TCA_FIXED_VES_MESSAGE_ROUTER_OUTPUT) + private OutputEmitter vesMessageEmitter; + + + @Override + public void configure() { + setName(TCA_FIXED_VES_MESSAGE_ROUTER_NAME_FLOWLET); + setDescription(TCA_FIXED_VES_MESSAGE_ROUTER_DESCRIPTION_FLOWLET); + } + + @ProcessInput + public void routeVESMessage(StreamEvent vesMessageStreamEvent) { + final String vesMessage = Charsets.UTF_8.decode(vesMessageStreamEvent.getBody()).toString(); + vesMessageEmitter.emit(vesMessage, TCA_VES_MESSAGE_ROUTER_PARTITION_KEY, vesMessage + .hashCode()); + } +} diff --git a/dcae-analytics-tca/src/main/java/org/openecomp/dcae/apod/analytics/tca/flowlet/TCAVESThresholdViolationCalculatorFlowlet.java b/dcae-analytics-tca/src/main/java/org/openecomp/dcae/apod/analytics/tca/flowlet/TCAVESThresholdViolationCalculatorFlowlet.java new file mode 100644 index 0000000..3d03cf7 --- /dev/null +++ b/dcae-analytics-tca/src/main/java/org/openecomp/dcae/apod/analytics/tca/flowlet/TCAVESThresholdViolationCalculatorFlowlet.java @@ -0,0 +1,137 @@ +/* + * ============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.tca.flowlet; + +import co.cask.cdap.api.annotation.Output; +import co.cask.cdap.api.annotation.ProcessInput; +import co.cask.cdap.api.annotation.Property; +import co.cask.cdap.api.annotation.RoundRobin; +import co.cask.cdap.api.dataset.lib.ObjectMappedTable; +import co.cask.cdap.api.flow.flowlet.AbstractFlowlet; +import co.cask.cdap.api.flow.flowlet.FlowletContext; +import co.cask.cdap.api.flow.flowlet.OutputEmitter; +import co.cask.cdap.api.metrics.Metrics; +import com.fasterxml.jackson.core.JsonProcessingException; +import org.openecomp.dcae.apod.analytics.common.CDAPComponentsConstants; +import org.openecomp.dcae.apod.analytics.common.CDAPMetricsConstants; +import org.openecomp.dcae.apod.analytics.model.domain.policy.tca.TCAPolicy; +import org.openecomp.dcae.apod.analytics.model.facade.tca.TCAVESResponse; +import org.openecomp.dcae.apod.analytics.tca.persistance.TCACalculatorMessageType; +import org.openecomp.dcae.apod.analytics.tca.persistance.TCAMessageStatusEntity; +import org.openecomp.dcae.apod.analytics.tca.persistance.TCAMessageStatusPersister; +import org.openecomp.dcae.apod.analytics.tca.processor.TCACEFProcessorContext; +import org.openecomp.dcae.apod.analytics.tca.utils.TCAUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import static org.openecomp.dcae.apod.analytics.common.CDAPComponentsConstants.TCA_FIXED_VES_THRESHOLD_VIOLATION_CALCULATOR_DESCRIPTION_FLOWLET; +import static org.openecomp.dcae.apod.analytics.common.CDAPComponentsConstants.TCA_FIXED_VES_THRESHOLD_VIOLATION_CALCULATOR_NAME_FLOWLET; + +/** + * TCA VES Message Filter filters out messages which are not applicable for TCA as per TCA Policy + * + * @author Rajiv Singla. Creation Date: 11/3/2016. + */ +public class TCAVESThresholdViolationCalculatorFlowlet extends AbstractFlowlet { + + private static final Logger LOG = LoggerFactory.getLogger(TCAVESThresholdViolationCalculatorFlowlet.class); + + @Output(CDAPComponentsConstants.TCA_FIXED_VES_TCA_CALCULATOR_NAME_OUTPUT) + private OutputEmitter tcaAlertOutputEmitter; + + @Property + private final String messageStatusTableName; + + private TCAPolicy tcaPolicy; + private Metrics metrics; + private ObjectMappedTable vesMessageStatusTable; + + public TCAVESThresholdViolationCalculatorFlowlet(String messageStatusTableName) { + this.messageStatusTableName = messageStatusTableName; + } + + @Override + public void configure() { + setName(TCA_FIXED_VES_THRESHOLD_VIOLATION_CALCULATOR_NAME_FLOWLET); + setDescription(TCA_FIXED_VES_THRESHOLD_VIOLATION_CALCULATOR_DESCRIPTION_FLOWLET); + } + + + @Override + public void initialize(FlowletContext flowletContext) throws Exception { + super.initialize(flowletContext); + + // parse Runtime Arguments to tca policy preferences + tcaPolicy = TCAUtils.getValidatedTCAPolicyPreferences(flowletContext); + + vesMessageStatusTable = getContext().getDataset(messageStatusTableName); + + } + + @ProcessInput(CDAPComponentsConstants.TCA_FIXED_VES_MESSAGE_ROUTER_OUTPUT) + @RoundRobin + public void filterVESMessages(String vesMessage) throws JsonProcessingException { + + TCACalculatorMessageType calculatorMessageType = TCACalculatorMessageType.INAPPLICABLE; + String alertMessage = null; + + // Step 1: Filter incoming messages + final TCACEFProcessorContext processorContext = TCAUtils.filterCEFMessage(vesMessage, tcaPolicy); + + if (processorContext.canProcessingContinue()) { + + // Step 2: Check if CEF Message violate any thresholds + final TCACEFProcessorContext processorContextWithViolations = + TCAUtils.computeThresholdViolations(processorContext); + + if (processorContextWithViolations.canProcessingContinue()) { + + // Step 3: Create Alert Message + final String tcaAppName = getContext().getApplicationSpecification().getName(); + final TCAVESResponse newTCAVESResponse = + TCAUtils.createNewTCAVESResponse(processorContextWithViolations, tcaAppName); + alertMessage = TCAUtils.writeValueAsString(newTCAVESResponse); + calculatorMessageType = TCACalculatorMessageType.NON_COMPLIANT; + LOG.debug("VES Threshold Violation Detected. An alert message is be generated. {}", alertMessage); + + metrics.count(CDAPMetricsConstants.TCA_VES_NON_COMPLIANT_MESSAGES_METRIC, 1); + + // Step 4: Emit message to Alert Sink Flowlet + tcaAlertOutputEmitter.emit(alertMessage); + + } else { + + calculatorMessageType = TCACalculatorMessageType.COMPLIANT; + metrics.count(CDAPMetricsConstants.TCA_VES_COMPLIANT_MESSAGES_METRIC, 1); + } + + } else { + + metrics.count(CDAPMetricsConstants.TCA_VES_INAPPLICABLE_MESSAGES_METRIC, 1); + } + + // save message to message status table + TCAMessageStatusPersister.persist( + processorContext, getContext(), calculatorMessageType, vesMessageStatusTable, alertMessage); + } + + +} diff --git a/dcae-analytics-tca/src/main/java/org/openecomp/dcae/apod/analytics/tca/persistance/TCACalculatorMessageType.java b/dcae-analytics-tca/src/main/java/org/openecomp/dcae/apod/analytics/tca/persistance/TCACalculatorMessageType.java new file mode 100644 index 0000000..afb434e --- /dev/null +++ b/dcae-analytics-tca/src/main/java/org/openecomp/dcae/apod/analytics/tca/persistance/TCACalculatorMessageType.java @@ -0,0 +1,43 @@ +/* + * ============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.tca.persistance; + +/** + * TCA Calculator applies TCA Policy to incoming VES messages and classifies them as per this enum + * + * @author Rajiv Singla. Creation Date: 11/15/2016. + */ +public enum TCACalculatorMessageType { + + /** + * VES messages that are not applicable as per TCA Policy + */ + INAPPLICABLE, + /** + * VES messages that are applicable as per TCA Policy but don't violate any thresholds + */ + COMPLIANT, + /** + * VES messages that are applicable as per TCA Policy and also in violation of TCA Policy thresholds + */ + NON_COMPLIANT; + +} diff --git a/dcae-analytics-tca/src/main/java/org/openecomp/dcae/apod/analytics/tca/persistance/TCAMessageStatusEntity.java b/dcae-analytics-tca/src/main/java/org/openecomp/dcae/apod/analytics/tca/persistance/TCAMessageStatusEntity.java new file mode 100644 index 0000000..a2d84c3 --- /dev/null +++ b/dcae-analytics-tca/src/main/java/org/openecomp/dcae/apod/analytics/tca/persistance/TCAMessageStatusEntity.java @@ -0,0 +1,306 @@ +/* + * ============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.tca.persistance; + +import org.apache.hadoop.io.Writable; +import org.apache.hadoop.io.WritableUtils; + +import java.io.DataInput; +import java.io.DataOutput; +import java.io.IOException; +import java.io.Serializable; + +/** + * TCA Message Status is an Entity which is used to persist TCA VES Message status information in Message Status Table + * + * @author Rajiv Singla. Creation Date: 11/16/2016. + */ +public class TCAMessageStatusEntity implements Writable, Serializable { + + private static final long serialVersionUID = -6186607668704516962L; + + private long creationTS; + private int flowletInstance; + private String messageType; + private String vesMessage; + private String domain; + private String functionalRole; + private String thresholdPath; + private String thresholdSeverity; + private String thresholdDirection; + private Long thresholdValue; + private String jsonProcessorStatus; + private String jsonProcessorMessage; + private String domainFilterStatus; + private String domainFilterMessage; + private String functionalRoleFilterStatus; + private String functionalRoleFilterMessage; + private String thresholdCalculatorStatus; + private String thresholdCalculatorMessage; + private String alertMessage; + + public TCAMessageStatusEntity() { + } + + public TCAMessageStatusEntity(long creationTS, int flowletInstance, String messageType, String vesMessage, String + domain, String functionalRole) { + this(creationTS, flowletInstance, messageType, vesMessage, domain, functionalRole, null, null, null, null, + null, null, null, null, null, null, null, null, null); + } + + public TCAMessageStatusEntity(long creationTS, int flowletInstance, String messageType, String vesMessage, + String domain, String functionalRole, + String thresholdPath, String thresholdSeverity, String thresholdDirection, + Long thresholdValue, + String jsonProcessorStatus, String jsonProcessorMessage, + String domainFilterStatus, String domainFilterMessage, + String functionalRoleFilterStatus, String functionalRoleFilterMessage, + String thresholdCalculatorStatus, String thresholdCalculatorMessage, + String alertMessage) { + this.creationTS = creationTS; + this.flowletInstance = flowletInstance; + this.messageType = messageType; + this.vesMessage = vesMessage; + this.domain = domain; + this.functionalRole = functionalRole; + this.thresholdPath = thresholdPath; + this.thresholdSeverity = thresholdSeverity; + this.thresholdDirection = thresholdDirection; + this.thresholdValue = thresholdValue; + this.jsonProcessorStatus = jsonProcessorStatus; + this.jsonProcessorMessage = jsonProcessorMessage; + this.domainFilterStatus = domainFilterStatus; + this.domainFilterMessage = domainFilterMessage; + this.functionalRoleFilterStatus = functionalRoleFilterStatus; + this.functionalRoleFilterMessage = functionalRoleFilterMessage; + this.thresholdCalculatorStatus = thresholdCalculatorStatus; + this.thresholdCalculatorMessage = thresholdCalculatorMessage; + this.alertMessage = alertMessage; + } + + public long getCreationTS() { + return creationTS; + } + + public void setCreationTS(long creationTS) { + this.creationTS = creationTS; + } + + public int getFlowletInstance() { + return flowletInstance; + } + + public void setFlowletInstance(int flowletInstance) { + this.flowletInstance = flowletInstance; + } + + public String getMessageType() { + return messageType; + } + + public void setMessageType(String messageType) { + this.messageType = messageType; + } + + public String getVesMessage() { + return vesMessage; + } + + public void setVesMessage(String vesMessage) { + this.vesMessage = vesMessage; + } + + public String getDomain() { + return domain; + } + + public void setDomain(String domain) { + this.domain = domain; + } + + public String getFunctionalRole() { + return functionalRole; + } + + public void setFunctionalRole(String functionalRole) { + this.functionalRole = functionalRole; + } + + public String getThresholdPath() { + return thresholdPath; + } + + public void setThresholdPath(String thresholdPath) { + this.thresholdPath = thresholdPath; + } + + public String getThresholdSeverity() { + return thresholdSeverity; + } + + public void setThresholdSeverity(String thresholdSeverity) { + this.thresholdSeverity = thresholdSeverity; + } + + public String getThresholdDirection() { + return thresholdDirection; + } + + public void setThresholdDirection(String thresholdDirection) { + this.thresholdDirection = thresholdDirection; + } + + public Long getThresholdValue() { + return thresholdValue; + } + + public void setThresholdValue(Long thresholdValue) { + this.thresholdValue = thresholdValue; + } + + public String getJsonProcessorStatus() { + return jsonProcessorStatus; + } + + public void setJsonProcessorStatus(String jsonProcessorStatus) { + this.jsonProcessorStatus = jsonProcessorStatus; + } + + public String getJsonProcessorMessage() { + return jsonProcessorMessage; + } + + public void setJsonProcessorMessage(String jsonProcessorMessage) { + this.jsonProcessorMessage = jsonProcessorMessage; + } + + public String getDomainFilterStatus() { + return domainFilterStatus; + } + + public void setDomainFilterStatus(String domainFilterStatus) { + this.domainFilterStatus = domainFilterStatus; + } + + public String getDomainFilterMessage() { + return domainFilterMessage; + } + + public void setDomainFilterMessage(String domainFilterMessage) { + this.domainFilterMessage = domainFilterMessage; + } + + public String getFunctionalRoleFilterStatus() { + return functionalRoleFilterStatus; + } + + public void setFunctionalRoleFilterStatus(String functionalRoleFilterStatus) { + this.functionalRoleFilterStatus = functionalRoleFilterStatus; + } + + public String getFunctionalRoleFilterMessage() { + return functionalRoleFilterMessage; + } + + public void setFunctionalRoleFilterMessage(String functionalRoleFilterMessage) { + this.functionalRoleFilterMessage = functionalRoleFilterMessage; + } + + public String getThresholdCalculatorStatus() { + return thresholdCalculatorStatus; + } + + public void setThresholdCalculatorStatus(String thresholdCalculatorStatus) { + this.thresholdCalculatorStatus = thresholdCalculatorStatus; + } + + public String getThresholdCalculatorMessage() { + return thresholdCalculatorMessage; + } + + public void setThresholdCalculatorMessage(String thresholdCalculatorMessage) { + this.thresholdCalculatorMessage = thresholdCalculatorMessage; + } + + public String getAlertMessage() { + return alertMessage; + } + + public void setAlertMessage(String alertMessage) { + this.alertMessage = alertMessage; + } + + @Override + public void write(DataOutput dataOutput) throws IOException { + WritableUtils.writeVLong(dataOutput, creationTS); + WritableUtils.writeVInt(dataOutput, flowletInstance); + WritableUtils.writeString(dataOutput, messageType); + WritableUtils.writeString(dataOutput, vesMessage); + + WritableUtils.writeString(dataOutput, domain); + WritableUtils.writeString(dataOutput, functionalRole); + + WritableUtils.writeString(dataOutput, thresholdPath); + WritableUtils.writeString(dataOutput, thresholdSeverity); + WritableUtils.writeString(dataOutput, thresholdDirection); + WritableUtils.writeVLong(dataOutput, thresholdValue); + + WritableUtils.writeString(dataOutput, jsonProcessorStatus); + WritableUtils.writeString(dataOutput, jsonProcessorMessage); + WritableUtils.writeString(dataOutput, domainFilterStatus); + WritableUtils.writeString(dataOutput, domainFilterMessage); + WritableUtils.writeString(dataOutput, functionalRoleFilterStatus); + WritableUtils.writeString(dataOutput, functionalRoleFilterMessage); + WritableUtils.writeString(dataOutput, thresholdCalculatorStatus); + WritableUtils.writeString(dataOutput, thresholdCalculatorMessage); + + WritableUtils.writeString(dataOutput, alertMessage); + + } + + @Override + public void readFields(DataInput dataInput) throws IOException { + creationTS = WritableUtils.readVLong(dataInput); + flowletInstance = WritableUtils.readVInt(dataInput); + messageType = WritableUtils.readString(dataInput); + vesMessage = WritableUtils.readString(dataInput); + + domain = WritableUtils.readString(dataInput); + functionalRole = WritableUtils.readString(dataInput); + + thresholdPath = WritableUtils.readString(dataInput); + thresholdSeverity = WritableUtils.readString(dataInput); + thresholdDirection = WritableUtils.readString(dataInput); + thresholdValue = WritableUtils.readVLong(dataInput); + + jsonProcessorStatus = WritableUtils.readString(dataInput); + jsonProcessorMessage = WritableUtils.readString(dataInput); + domainFilterStatus = WritableUtils.readString(dataInput); + domainFilterMessage = WritableUtils.readString(dataInput); + functionalRoleFilterStatus = WritableUtils.readString(dataInput); + functionalRoleFilterMessage = WritableUtils.readString(dataInput); + thresholdCalculatorStatus = WritableUtils.readString(dataInput); + thresholdCalculatorMessage = WritableUtils.readString(dataInput); + + alertMessage = WritableUtils.readString(dataInput); + + } +} diff --git a/dcae-analytics-tca/src/main/java/org/openecomp/dcae/apod/analytics/tca/persistance/TCAMessageStatusPersister.java b/dcae-analytics-tca/src/main/java/org/openecomp/dcae/apod/analytics/tca/persistance/TCAMessageStatusPersister.java new file mode 100644 index 0000000..1ac8bfa --- /dev/null +++ b/dcae-analytics-tca/src/main/java/org/openecomp/dcae/apod/analytics/tca/persistance/TCAMessageStatusPersister.java @@ -0,0 +1,243 @@ +/* + * ============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.tca.persistance; + +import co.cask.cdap.api.data.schema.Schema; +import co.cask.cdap.api.data.schema.UnsupportedTypeException; +import co.cask.cdap.api.dataset.DatasetProperties; +import co.cask.cdap.api.dataset.lib.IndexedTable; +import co.cask.cdap.api.dataset.lib.ObjectMappedTable; +import co.cask.cdap.api.dataset.lib.ObjectMappedTableProperties; +import co.cask.cdap.api.flow.flowlet.FlowletContext; +import com.google.common.base.Joiner; +import org.apache.commons.lang3.StringEscapeUtils; +import org.apache.commons.lang3.tuple.Pair; +import org.openecomp.dcae.apod.analytics.common.exception.DCAEAnalyticsRuntimeException; +import org.openecomp.dcae.apod.analytics.common.service.processor.MessageProcessor; +import org.openecomp.dcae.apod.analytics.common.service.processor.ProcessorContext; +import org.openecomp.dcae.apod.analytics.common.utils.PersistenceUtils; +import org.openecomp.dcae.apod.analytics.model.domain.policy.tca.MetricsPerFunctionalRole; +import org.openecomp.dcae.apod.analytics.model.domain.policy.tca.Threshold; +import org.openecomp.dcae.apod.analytics.tca.processor.TCACEFJsonProcessor; +import org.openecomp.dcae.apod.analytics.tca.processor.TCACEFPolicyDomainFilter; +import org.openecomp.dcae.apod.analytics.tca.processor.TCACEFPolicyFunctionalRoleFilter; +import org.openecomp.dcae.apod.analytics.tca.processor.TCACEFPolicyThresholdsProcessor; +import org.openecomp.dcae.apod.analytics.tca.processor.TCACEFProcessorContext; +import org.openecomp.dcae.apod.analytics.tca.utils.TCAUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.Date; +import java.util.LinkedList; +import java.util.List; + +import javax.annotation.Nullable; + +import static org.openecomp.dcae.apod.analytics.common.CDAPComponentsConstants.TCA_FIXED_VES_MESSAGE_STATUS_DESCRIPTION_TABLE; +import static org.openecomp.dcae.apod.analytics.common.utils.PersistenceUtils.TABLE_ROW_KEY_COLUMN_NAME; + +/** + * + * + * @author Rajiv Singla. Creation Date: 11/15/2016. + */ +public abstract class TCAMessageStatusPersister { + + private static final Logger LOG = LoggerFactory.getLogger(TCAMessageStatusPersister.class); + + /** + * Saves Message Status in Table. Assumes no alert was generated + * + * @param processorContext processor Context + * @param flowletContext Flowlet Context + * @param calculatorMessageType Calculation Message Type + * @param messageStatusTable Message Status Table + */ + public static void persist(final TCACEFProcessorContext processorContext, + final FlowletContext flowletContext, + final TCACalculatorMessageType calculatorMessageType, + final ObjectMappedTable messageStatusTable) { + persist(processorContext, flowletContext, calculatorMessageType, messageStatusTable, null); + } + + /** + * Saves Message Status in Table. Sets up alert message aslo + * + * @param processorContext processor Context + * @param flowletContext Flowlet Context + * @param calculatorMessageType Calculation Message Type + * @param messageStatusTable Message Status Table + * @param alertMessage Alert message + */ + public static void persist(final TCACEFProcessorContext processorContext, + final FlowletContext flowletContext, + final TCACalculatorMessageType calculatorMessageType, + final ObjectMappedTable messageStatusTable, + @Nullable final String alertMessage) { + + final String rowKey = createKey(calculatorMessageType); + + final Long currentTS = new Date().getTime(); + final int flowletInstanceId = flowletContext.getInstanceId(); + final String vesMessage = StringEscapeUtils.unescapeJson(processorContext.getMessage()); + + // Find Functional Role and domain + final Pair domainAndFunctionalRole = TCAUtils.getDomainAndFunctionalRole(processorContext); + final String domain = domainAndFunctionalRole.getLeft(); + final String functionalRole = domainAndFunctionalRole.getRight(); + + final TCAMessageStatusEntity tcaMessageStatusEntity = new TCAMessageStatusEntity(currentTS, + flowletInstanceId, calculatorMessageType.name(), vesMessage, domain, functionalRole); + + // add threshold violation fields + addViolatedThreshold(tcaMessageStatusEntity, processorContext); + // add processor status and messages + addMessageProcessorMessages(tcaMessageStatusEntity, processorContext); + // add Alert message + tcaMessageStatusEntity.setAlertMessage( + alertMessage == null ? null : StringEscapeUtils.unescapeJson(alertMessage) + ); + + messageStatusTable.write(rowKey, tcaMessageStatusEntity); + + + LOG.debug("Finished persisting VES Status Message with rowKey: {} in Message Status Table.", rowKey); + + } + + + /** + * Create TCA VES Message Status Table Properties + * + * @param timeToLiveSeconds Message Status Table time to live in seconds + * + * @return Message Status table properties + */ + public static DatasetProperties getDatasetProperties(final int timeToLiveSeconds) { + + try { + return ObjectMappedTableProperties.builder() + .setType(TCAMessageStatusEntity.class) + .setRowKeyExploreName(TABLE_ROW_KEY_COLUMN_NAME) + .setRowKeyExploreType(Schema.Type.STRING) + .add(IndexedTable.PROPERTY_TTL, timeToLiveSeconds) + .setDescription(TCA_FIXED_VES_MESSAGE_STATUS_DESCRIPTION_TABLE) + .build(); + } catch (UnsupportedTypeException e) { + final String errorMessage = "Unable to convert TCAMessageStatusEntity class to Schema"; + throw new DCAEAnalyticsRuntimeException(errorMessage, LOG, new IllegalArgumentException(errorMessage)); + } + + } + + + /** + * Adds Violated Threshold Parameter values to {@link TCAMessageStatusEntity} + * + * @param tcaMessageStatusEntity message entity that needs to be populated with threshold fields + * @param processorContext processor context + * + * @return entity with populated threshold field values if present + */ + private static TCAMessageStatusEntity addViolatedThreshold(final TCAMessageStatusEntity tcaMessageStatusEntity, + final TCACEFProcessorContext processorContext) { + + final MetricsPerFunctionalRole metricsPerFunctionalRole = processorContext.getMetricsPerFunctionalRole(); + + if (metricsPerFunctionalRole != null + && metricsPerFunctionalRole.getThresholds() != null + && metricsPerFunctionalRole.getThresholds().get(0) != null) { + + final Threshold threshold = metricsPerFunctionalRole.getThresholds().get(0); + tcaMessageStatusEntity.setThresholdPath(threshold.getFieldPath()); + tcaMessageStatusEntity.setThresholdSeverity(threshold.getSeverity().name()); + tcaMessageStatusEntity.setThresholdDirection(threshold.getDirection().name()); + tcaMessageStatusEntity.setThresholdValue(threshold.getThresholdValue()); + } + + return tcaMessageStatusEntity; + } + + + /** + * Add TCA CEF Message Processor status information + * + * @param tcaMessageStatusEntity message entity that needs to be populated with message processor fields + * @param processorContext processor context + * + * @return entity with populated message process status information + */ + private static TCAMessageStatusEntity addMessageProcessorMessages( + final TCAMessageStatusEntity tcaMessageStatusEntity, final TCACEFProcessorContext processorContext) { + final List> messageProcessors = processorContext + .getMessageProcessors(); + + if (messageProcessors != null && !messageProcessors.isEmpty()) { + for (Object messageProcessor : messageProcessors) { + final MessageProcessor tcaMessageProcessor = + (MessageProcessor) messageProcessor; + + final String processingState = tcaMessageProcessor.getProcessingState().name(); + final String processingMessage = tcaMessageProcessor.getProcessingMessage().orNull(); + + if (messageProcessor.getClass().equals(TCACEFJsonProcessor.class)) { + tcaMessageStatusEntity.setJsonProcessorStatus(processingState); + tcaMessageStatusEntity.setJsonProcessorMessage(processingMessage); + } + + if (messageProcessor.getClass().equals(TCACEFPolicyDomainFilter.class)) { + tcaMessageStatusEntity.setDomainFilterStatus(processingState); + tcaMessageStatusEntity.setDomainFilterMessage(processingMessage); + } + + if (messageProcessor.getClass().equals(TCACEFPolicyFunctionalRoleFilter.class)) { + tcaMessageStatusEntity.setFunctionalRoleFilterStatus(processingState); + tcaMessageStatusEntity.setFunctionalRoleFilterMessage(processingMessage); + } + + if (messageProcessor.getClass().equals(TCACEFPolicyThresholdsProcessor.class)) { + tcaMessageStatusEntity.setThresholdCalculatorStatus(processingState); + tcaMessageStatusEntity.setThresholdCalculatorMessage(processingMessage); + } + + } + } + return tcaMessageStatusEntity; + } + + /** + * Creates Row Key for TCA VES Message Status table + * + * Row Key = (Message Type + Decreasing Value) + * + * @param calculatorMessageType calculator message type + * + * @return row key string + */ + private static String createKey(final TCACalculatorMessageType calculatorMessageType) { + + final List keyList = new LinkedList<>(); + keyList.add(calculatorMessageType.name()); + keyList.add(PersistenceUtils.getCurrentTimeReverseSubKey()); + return Joiner.on(PersistenceUtils.ROW_KEY_DELIMITER).join(keyList); + } + +} diff --git a/dcae-analytics-tca/src/main/java/org/openecomp/dcae/apod/analytics/tca/persistance/TCAVESAlertEntity.java b/dcae-analytics-tca/src/main/java/org/openecomp/dcae/apod/analytics/tca/persistance/TCAVESAlertEntity.java new file mode 100644 index 0000000..4f11f39 --- /dev/null +++ b/dcae-analytics-tca/src/main/java/org/openecomp/dcae/apod/analytics/tca/persistance/TCAVESAlertEntity.java @@ -0,0 +1,77 @@ +/* + * ============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.tca.persistance; + +import org.apache.hadoop.io.Writable; +import org.apache.hadoop.io.WritableUtils; + +import java.io.DataInput; +import java.io.DataOutput; +import java.io.IOException; +import java.io.Serializable; + +/** + * + * @author Rajiv Singla. Creation Date: 11/16/2016. + */ +public class TCAVESAlertEntity implements Writable, Serializable { + + private static final long serialVersionUID = -8587229518063660258L; + + private long creationTS; + private String alertMessage; + + public TCAVESAlertEntity() { + } + + public TCAVESAlertEntity(long creationTS, String alertMessage) { + this.creationTS = creationTS; + this.alertMessage = alertMessage; + } + + public long getCreationTS() { + return creationTS; + } + + public void setCreationTS(long creationTS) { + this.creationTS = creationTS; + } + + public String getAlertMessage() { + return alertMessage; + } + + public void setAlertMessage(String alertMessage) { + this.alertMessage = alertMessage; + } + + @Override + public void write(DataOutput dataOutput) throws IOException { + WritableUtils.writeVLong(dataOutput, creationTS); + WritableUtils.writeString(dataOutput, alertMessage); + } + + @Override + public void readFields(DataInput dataInput) throws IOException { + creationTS = WritableUtils.readVLong(dataInput); + alertMessage = WritableUtils.readString(dataInput); + } +} diff --git a/dcae-analytics-tca/src/main/java/org/openecomp/dcae/apod/analytics/tca/persistance/TCAVESAlertsPersister.java b/dcae-analytics-tca/src/main/java/org/openecomp/dcae/apod/analytics/tca/persistance/TCAVESAlertsPersister.java new file mode 100644 index 0000000..83eaf7f --- /dev/null +++ b/dcae-analytics-tca/src/main/java/org/openecomp/dcae/apod/analytics/tca/persistance/TCAVESAlertsPersister.java @@ -0,0 +1,98 @@ +/* + * ============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.tca.persistance; + +import co.cask.cdap.api.data.schema.Schema; +import co.cask.cdap.api.data.schema.UnsupportedTypeException; +import co.cask.cdap.api.dataset.DatasetProperties; +import co.cask.cdap.api.dataset.lib.IndexedTable; +import co.cask.cdap.api.dataset.lib.ObjectMappedTable; +import co.cask.cdap.api.dataset.lib.ObjectMappedTableProperties; +import org.apache.commons.lang3.StringEscapeUtils; +import org.openecomp.dcae.apod.analytics.common.exception.DCAEAnalyticsRuntimeException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.Date; + +import static org.openecomp.dcae.apod.analytics.common.CDAPComponentsConstants.TCA_DEFAULT_VES_ALERTS_DESCRIPTION_TABLE; +import static org.openecomp.dcae.apod.analytics.common.utils.PersistenceUtils.TABLE_ROW_KEY_COLUMN_NAME; + +/** + * + * @author Rajiv Singla. Creation Date: 11/16/2016. + */ +public abstract class TCAVESAlertsPersister { + + private static final Logger LOG = LoggerFactory.getLogger(TCAVESAlertsPersister.class); + + /** + * Persists Alert Message to Alerts Table + * + * @param alertMessage alert Message + * @param tcaVESAlertTable alert Table Name + */ + public static void persist(final String alertMessage, final ObjectMappedTable tcaVESAlertTable) { + final Date currentDate = new Date(); + final TCAVESAlertEntity alertEntity = new TCAVESAlertEntity(currentDate.getTime(), + StringEscapeUtils.unescapeJson(alertMessage)); + // row key is same as current timestamp + final String rowKey = createRowKey(currentDate); + tcaVESAlertTable.write(rowKey, alertEntity); + + LOG.debug("Finished persisting VES Alert message ID: {} in VES Alerts table.", rowKey); + } + + + /** + * Creates {@link DatasetProperties} for Alerts Table + * + * @param timeToLiveSeconds alerts table Time to Live + * @return Alerts table properties + */ + public static DatasetProperties getDatasetProperties(final int timeToLiveSeconds) { + try { + return ObjectMappedTableProperties.builder() + .setType(TCAVESAlertEntity.class) + .setRowKeyExploreName(TABLE_ROW_KEY_COLUMN_NAME) + .setRowKeyExploreType(Schema.Type.STRING) + .add(IndexedTable.PROPERTY_TTL, timeToLiveSeconds) + .setDescription(TCA_DEFAULT_VES_ALERTS_DESCRIPTION_TABLE) + .build(); + } catch (UnsupportedTypeException e) { + final String errorMessage = "Unable to convert TCAVESAlertEntity class to Schema"; + throw new DCAEAnalyticsRuntimeException(errorMessage, LOG, new IllegalArgumentException(errorMessage)); + } + + } + + /** + * Creates Row Key for Alerts Table + * + * @param date current Date + * + * @return row key + */ + public static String createRowKey(final Date date) { + return String.format("%025d", date.getTime()); + } + +} diff --git a/dcae-analytics-tca/src/main/java/org/openecomp/dcae/apod/analytics/tca/processor/AbstractTCAECEFPolicyProcessor.java b/dcae-analytics-tca/src/main/java/org/openecomp/dcae/apod/analytics/tca/processor/AbstractTCAECEFPolicyProcessor.java new file mode 100644 index 0000000..3e599c4 --- /dev/null +++ b/dcae-analytics-tca/src/main/java/org/openecomp/dcae/apod/analytics/tca/processor/AbstractTCAECEFPolicyProcessor.java @@ -0,0 +1,61 @@ +/* + * ============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.tca.processor; + +import org.openecomp.dcae.apod.analytics.common.exception.MessageProcessingException; +import org.openecomp.dcae.apod.analytics.common.service.processor.AbstractMessageProcessor; +import org.openecomp.dcae.apod.analytics.model.domain.cef.EventListener; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.annotation.Nonnull; + +/** + *

+ * Encapsulates common functionality for all TCA CEF Policy Processors + *

+ * + * @author Rajiv Singla. Creation Date: 11/9/2016. + */ +public abstract class AbstractTCAECEFPolicyProcessor extends AbstractMessageProcessor { + + private static final Logger LOG = LoggerFactory.getLogger(AbstractTCAECEFPolicyProcessor.class); + + /** + * For all TCA Policy Processor the pre processor ensures that {@link EventListener} object is + * present + * + * @param processorContext incoming Processor Context + * @return Pre processed Processor Context + */ + @Override + public TCACEFProcessorContext preProcessor(@Nonnull TCACEFProcessorContext processorContext) { + // validates CEF Event Listener is Present + final EventListener cefEventListener = processorContext.getCEFEventListener(); + if (cefEventListener == null) { + final String errorMessage = String.format( + "CEF Event Listener is not Present.Invalid use of Processor: %s. CEF Message: %s", + getProcessorInfo().getProcessorName(), processorContext.getMessage()); + throw new MessageProcessingException(errorMessage, LOG, new IllegalArgumentException(errorMessage)); + } + return super.preProcessor(processorContext); + } +} diff --git a/dcae-analytics-tca/src/main/java/org/openecomp/dcae/apod/analytics/tca/processor/TCACEFJsonProcessor.java b/dcae-analytics-tca/src/main/java/org/openecomp/dcae/apod/analytics/tca/processor/TCACEFJsonProcessor.java new file mode 100644 index 0000000..7f45adb --- /dev/null +++ b/dcae-analytics-tca/src/main/java/org/openecomp/dcae/apod/analytics/tca/processor/TCACEFJsonProcessor.java @@ -0,0 +1,98 @@ +/* + * ============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.tca.processor; + +import org.apache.commons.lang3.StringUtils; +import org.openecomp.dcae.apod.analytics.common.exception.MessageProcessingException; +import org.openecomp.dcae.apod.analytics.common.service.processor.AbstractMessageProcessor; +import org.openecomp.dcae.apod.analytics.model.domain.cef.EventListener; +import org.openecomp.dcae.apod.analytics.tca.utils.TCAUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.IOException; + +/** + *

+ * Processor that converts incoming presumed JSON string CEF message to {@link EventListener} object + *
+ * Pre Conditions: None + *

+ * + * @author Rajiv Singla. Creation Date: 11/5/2016. + */ +public class TCACEFJsonProcessor extends AbstractMessageProcessor { + + + private static final long serialVersionUID = -9031531679383821793L; + + private static final Logger LOG = LoggerFactory.getLogger(TCACEFJsonProcessor.class); + + + @Override + public String getProcessorDescription() { + return "Converts incoming TCA CEF Message to Event Listener object"; + } + + @Override + public TCACEFProcessorContext processMessage(TCACEFProcessorContext processorContext) { + + final String cefMessage = processorContext.getMessage(); + + // If CEF Message is null then processor should stop processing + if (cefMessage == null) { + String errorMessage = "Null CEF message cannot be converted to CEF Event Listener Object"; + throw new MessageProcessingException(errorMessage, LOG, new IllegalArgumentException(errorMessage)); + } + + // If CEF Message is blank then processor stop processing + if (StringUtils.isBlank(cefMessage)) { + setTerminatingProcessingMessage("Blank CEF message cannot be converted to CEF Event Listener Object", + processorContext); + return processorContext; + } + + // trim cef message + final String trimmedCEFMessage = cefMessage.trim(); + + // if message does not start with curly brace and ends with curly brace, it is not a valid cef message + // processor will stop processing + if (!(trimmedCEFMessage.startsWith("{") && trimmedCEFMessage.endsWith("}"))) { + setTerminatingProcessingMessage("CEF Message must start with curly brace and must end with curly brace", + processorContext); + return processorContext; + } + + // try parsing the cef message + try { + final EventListener eventListener = TCAUtils.readValue(trimmedCEFMessage, EventListener.class); + setFinishedProcessingMessage("CEF JSON to Event Listener Conversion Successful", processorContext); + // set new Event Listener in the Processor Context + processorContext.setCEFEventListener(eventListener); + return processorContext; + } catch (IOException e) { + final String errorMessage = String.format("Parsing Failed for CEF Message: %s, Error: %s", cefMessage, e); + // If parsing fails throw an exception + throw new MessageProcessingException(errorMessage, LOG, e); + } + + } +} diff --git a/dcae-analytics-tca/src/main/java/org/openecomp/dcae/apod/analytics/tca/processor/TCACEFPolicyDomainFilter.java b/dcae-analytics-tca/src/main/java/org/openecomp/dcae/apod/analytics/tca/processor/TCACEFPolicyDomainFilter.java new file mode 100644 index 0000000..d283e35 --- /dev/null +++ b/dcae-analytics-tca/src/main/java/org/openecomp/dcae/apod/analytics/tca/processor/TCACEFPolicyDomainFilter.java @@ -0,0 +1,83 @@ +/* + * ============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.tca.processor; + +import org.openecomp.dcae.apod.analytics.model.domain.cef.EventListener; + +/** + *

+ * TCA Processor which acts like a filter to filter out messages which does not belong to TCA Policy Domain + *
+ * Pre Conditions: CEF Event Listener must be present + *

+ * + * @author Rajiv Singla. Creation Date: 11/7/2016. + */ +public class TCACEFPolicyDomainFilter extends AbstractTCAECEFPolicyProcessor { + + + private static final long serialVersionUID = -7302116478207544378L; + + @Override + public String getProcessorDescription() { + return "Filters out CEF Messages which does not match TCAPolicy Domain"; + } + + @Override + public TCACEFProcessorContext processMessage(TCACEFProcessorContext processorContext) { + + // Safe to get event Listener here without null check as pre processor will validate if + // event listener is indeed present + final EventListener eventListener = processorContext.getCEFEventListener(); + + String cefMessageDomain; + + // Extract CEF domain as it is must be present as per CEF Schema + if (eventListener.getEvent() != null && + eventListener.getEvent().getCommonEventHeader() != null && + eventListener.getEvent().getCommonEventHeader().getDomain() != null) { + cefMessageDomain = eventListener.getEvent().getCommonEventHeader().getDomain(); + + } else { + final String terminatingMessage = "Invalid CEF Message.Common Event Header Domain not present."; + setTerminatingProcessingMessage(terminatingMessage, processorContext); + return processorContext; + } + + // Get Policy Domain. TCA Policy Validation must ensure that Domain is indeed present + // no null check will be required here + final String policyDomain = processorContext.getTCAPolicy().getDomain(); + + // If Policy domain matches CEF message domain then continue processing + if (cefMessageDomain.equals(policyDomain)) { + final String finishMessage = String.format("Policy Domain and CEF Message Domain match successful." + + " Message Domain: %s, Policy Domain: %s", cefMessageDomain, policyDomain); + setFinishedProcessingMessage(finishMessage, processorContext); + } else { + // If policy domain does not match with CEF message terminate processing chain + final String terminatingMessage = String.format("Policy Domain and CEF Message Domain match unsuccessful." + + " Message Domain: %s, Policy Domain: %s", cefMessageDomain, policyDomain); + setTerminatingProcessingMessage(terminatingMessage, processorContext); + } + + return processorContext; + } +} diff --git a/dcae-analytics-tca/src/main/java/org/openecomp/dcae/apod/analytics/tca/processor/TCACEFPolicyFunctionalRoleFilter.java b/dcae-analytics-tca/src/main/java/org/openecomp/dcae/apod/analytics/tca/processor/TCACEFPolicyFunctionalRoleFilter.java new file mode 100644 index 0000000..82aa1d6 --- /dev/null +++ b/dcae-analytics-tca/src/main/java/org/openecomp/dcae/apod/analytics/tca/processor/TCACEFPolicyFunctionalRoleFilter.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.tca.processor; + +import com.google.common.base.Joiner; +import org.openecomp.dcae.apod.analytics.model.domain.cef.EventListener; +import org.openecomp.dcae.apod.analytics.model.domain.policy.tca.TCAPolicy; +import org.openecomp.dcae.apod.analytics.tca.utils.TCAUtils; + +import java.util.List; + +/** + *

+ * TCA Processor that acts like a filter to filter out messages which does not belong to TCA Policy Functional Roles + *
+ * Pre Conditions: CEF Event Listener must be present + *

+ * + * @author Rajiv Singla. Creation Date: 11/9/2016. + */ +public class TCACEFPolicyFunctionalRoleFilter extends AbstractTCAECEFPolicyProcessor { + + private static final long serialVersionUID = -4550787922375040396L; + + @Override + public String getProcessorDescription() { + return "Filters out CEF Messages which does not match Policy Functional Roles"; + } + + @Override + public TCACEFProcessorContext processMessage(TCACEFProcessorContext processorContext) { + + // Safe to get event Listener here without null check as pre processor will validate if + // event listener is indeed present + final EventListener eventListener = processorContext.getCEFEventListener(); + + String cefMessageFunctionalRole; + + if (eventListener.getEvent() != null && + eventListener.getEvent().getCommonEventHeader() != null && + eventListener.getEvent().getCommonEventHeader().getFunctionalRole() != null) { + cefMessageFunctionalRole = eventListener.getEvent().getCommonEventHeader().getFunctionalRole(); + } else { + String terminationMessage = "Invalid CEF Message.Common Event Header Functional Role not present."; + setTerminatingProcessingMessage(terminationMessage, processorContext); + return processorContext; + } + + // Determine Policy Functional Roles + final TCAPolicy tcaPolicy = processorContext.getTCAPolicy(); + final List policyFunctionalRoles = TCAUtils.getPolicyFunctionalRoleSupplier(tcaPolicy).get(); + final String policyFunctionalRolesString = Joiner.on(",").join(policyFunctionalRoles); + + // If Policy functional Roles contains CEF message Functional Role then continue processing + if (policyFunctionalRoles.contains(cefMessageFunctionalRole)) { + final String finishMessage = String.format( + "Policy Functional Roles and CEF Message Functional match successful." + + "Message Functional Role: %s, Policy Functional Roles: %s", + cefMessageFunctionalRole, policyFunctionalRolesString); + setFinishedProcessingMessage(finishMessage, processorContext); + } else { + // If Policy functional Roles does not contain CEF message Functiona Role then terminate processing + final String terminatingMessage = String.format( + "Policy Domain and CEF Message Domain match unsuccessful." + + "Message Functional Role: %s, Policy Functional Roles: %s", + cefMessageFunctionalRole, policyFunctionalRolesString); + setTerminatingProcessingMessage(terminatingMessage, processorContext); + } + + return processorContext; + } +} diff --git a/dcae-analytics-tca/src/main/java/org/openecomp/dcae/apod/analytics/tca/processor/TCACEFPolicyThresholdsProcessor.java b/dcae-analytics-tca/src/main/java/org/openecomp/dcae/apod/analytics/tca/processor/TCACEFPolicyThresholdsProcessor.java new file mode 100644 index 0000000..89e4941 --- /dev/null +++ b/dcae-analytics-tca/src/main/java/org/openecomp/dcae/apod/analytics/tca/processor/TCACEFPolicyThresholdsProcessor.java @@ -0,0 +1,135 @@ +/* + * ============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.tca.processor; + +import com.google.common.base.Optional; +import com.google.common.collect.Table; +import org.openecomp.dcae.apod.analytics.common.exception.MessageProcessingException; +import org.openecomp.dcae.apod.analytics.model.domain.cef.EventListener; +import org.openecomp.dcae.apod.analytics.model.domain.policy.tca.MetricsPerFunctionalRole; +import org.openecomp.dcae.apod.analytics.model.domain.policy.tca.TCAPolicy; +import org.openecomp.dcae.apod.analytics.model.domain.policy.tca.Threshold; +import org.openecomp.dcae.apod.analytics.tca.utils.TCAUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import javax.annotation.Nonnull; + +/** + *

+ * TCA CEF Policy Threshold processor + *
+ * Pre Conditions: Domain and Functional Role must be present in CEF Event Listener Object + *

+ * + * @author Rajiv Singla. Creation Date: 11/9/2016. + */ +public class TCACEFPolicyThresholdsProcessor extends AbstractTCAECEFPolicyProcessor { + + private static final long serialVersionUID = -9075212160268522029L; + + private static final Logger LOG = LoggerFactory.getLogger(TCACEFPolicyThresholdsProcessor.class); + + @Override + public TCACEFProcessorContext preProcessor(@Nonnull TCACEFProcessorContext processorContext) { + // validates Domain and Functional Role are present + final EventListener eventListener = processorContext.getCEFEventListener(); + final String domain = eventListener.getEvent().getCommonEventHeader().getDomain(); + final String functionalRole = eventListener.getEvent().getCommonEventHeader().getFunctionalRole(); + if (domain == null || functionalRole == null) { + final String errorMessage = "CEF Event Listener domain or functional role not Present. " + + "Invalid use of this Processor"; + throw new MessageProcessingException(errorMessage, LOG, new IllegalArgumentException(errorMessage)); + } + return super.preProcessor(processorContext); + } + + @Override + public String getProcessorDescription() { + return "Applies TCA Policy rules to incoming CEF message. If any thresholds are violated attaches max " + + "Severity violated threshold to TCA Processor Context"; + } + + @Override + public TCACEFProcessorContext processMessage(TCACEFProcessorContext processorContext) { + + final String cefMessage = processorContext.getMessage(); + + // Determine domain and functional Role + final EventListener eventListener = processorContext.getCEFEventListener(); + final String functionalRole = eventListener.getEvent().getCommonEventHeader().getFunctionalRole(); + + // Get Table containing Functional Role and Thresholds Field Path + final TCAPolicy tcaPolicy = processorContext.getTCAPolicy(); + final Table> functionalRoleFieldPathsTable = + TCAUtils.getPolicyFRThresholdsTableSupplier(tcaPolicy).get(); + + // Get Policy Field Paths for that functional Role + final Map> policyFieldPathsMap = functionalRoleFieldPathsTable.row(functionalRole); + final Set policyFieldPaths = policyFieldPathsMap.keySet(); + + // Get Json Values for Policy Fields + final Map> messageFieldValuesMap = TCAUtils.getJsonPathValue(cefMessage, policyFieldPaths); + + // Determine all violated thresholds per message field Path + final Map violatedThresholdsMap = new HashMap<>(); + for (Map.Entry> messageFieldValuesMapEntry : messageFieldValuesMap.entrySet()) { + final String messageFieldPath = messageFieldValuesMapEntry.getKey(); + final List messageFieldAssociatedPolicyThresholds = policyFieldPathsMap.get(messageFieldPath); + if (messageFieldAssociatedPolicyThresholds != null) { + final Optional thresholdOptional = TCAUtils.thresholdCalculator( + messageFieldValuesMapEntry.getValue(), messageFieldAssociatedPolicyThresholds); + if (thresholdOptional.isPresent()) { + violatedThresholdsMap.put(messageFieldPath, thresholdOptional.get()); + } + } + } + + // No threshold were violated + if (violatedThresholdsMap.isEmpty()) { + + final String terminationMessage = "No Policy Threshold violated by the VES CEF Message."; + setTerminatingProcessingMessage(terminationMessage, processorContext); + + } else { + + // If there are policy violations then determine max priority violation + final Threshold maxSeverityThresholdViolation = + TCAUtils.prioritizeThresholdViolations(violatedThresholdsMap); + final MetricsPerFunctionalRole violatedMetrics = TCAUtils.createViolatedMetrics(tcaPolicy, + maxSeverityThresholdViolation, functionalRole); + // attach policy violation to processor Context + processorContext.setMetricsPerFunctionalRole(violatedMetrics); + + final String finishMessage = String.format("Policy Threshold violation detected for threshold: %s", + maxSeverityThresholdViolation); + setFinishedProcessingMessage(finishMessage, processorContext); + + } + + return processorContext; + } +} diff --git a/dcae-analytics-tca/src/main/java/org/openecomp/dcae/apod/analytics/tca/processor/TCACEFProcessorContext.java b/dcae-analytics-tca/src/main/java/org/openecomp/dcae/apod/analytics/tca/processor/TCACEFProcessorContext.java new file mode 100644 index 0000000..b8c31c9 --- /dev/null +++ b/dcae-analytics-tca/src/main/java/org/openecomp/dcae/apod/analytics/tca/processor/TCACEFProcessorContext.java @@ -0,0 +1,103 @@ +/* + * ============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.tca.processor; + +import org.openecomp.dcae.apod.analytics.common.service.processor.AbstractProcessorContext; +import org.openecomp.dcae.apod.analytics.model.domain.cef.EventListener; +import org.openecomp.dcae.apod.analytics.model.domain.policy.tca.MetricsPerFunctionalRole; +import org.openecomp.dcae.apod.analytics.model.domain.policy.tca.TCAPolicy; + +/** + * TCA CEF Policy Processor Context + * + * @author Rajiv Singla. Creation Date: 11/7/2016. + */ +public class TCACEFProcessorContext extends AbstractProcessorContext { + + private static final long serialVersionUID = 1165992675657025413L; + + private final TCAPolicy tcaPolicy; + private EventListener eventListener; + private MetricsPerFunctionalRole metricsPerFunctionalRole; + + public TCACEFProcessorContext(final String message, boolean canProcessingContinue, final TCAPolicy tcaPolicy) { + super(message, canProcessingContinue); + this.tcaPolicy = tcaPolicy; + // present only if cef incoming message can be parsed successfully to Event Listener Object + this.eventListener = null; + // present only if there are any threshold violations are detected + this.metricsPerFunctionalRole = null; + } + + // Auxiliary Constructor which default canProcessingContinue Flag to true + public TCACEFProcessorContext(final String message, final TCAPolicy tcaPolicy) { + this(message, true, tcaPolicy); + } + + /** + * Returns {@link TCAPolicy} Object + * + * @return TCA Policy + */ + public TCAPolicy getTCAPolicy() { + return tcaPolicy; + } + + /** + * Returns Common Event Format {@link EventListener} if present else null + * + * @return CEF Event Listener + */ + public EventListener getCEFEventListener() { + return eventListener; + } + + + /** + * Sets new {@link EventListener} + * + * @param eventListener set new value for CEF event listener + */ + public void setCEFEventListener(final EventListener eventListener) { + this.eventListener = eventListener; + } + + + /** + * Returns TCA Policy {@link MetricsPerFunctionalRole} which was has violated Threshold for the CEF Message if + * present else null + * + * @return Violated Threshold Optional + */ + public MetricsPerFunctionalRole getMetricsPerFunctionalRole() { + return metricsPerFunctionalRole; + } + + /** + * Assign new TCA Policy {@link MetricsPerFunctionalRole} which was has violated Threshold for the CEF Message + * + * @param metricsPerFunctionalRole new value for Metrics Per Functional Role with violated threshold + */ + public void setMetricsPerFunctionalRole(MetricsPerFunctionalRole metricsPerFunctionalRole) { + this.metricsPerFunctionalRole = metricsPerFunctionalRole; + } + +} diff --git a/dcae-analytics-tca/src/main/java/org/openecomp/dcae/apod/analytics/tca/settings/TCAAppConfig.java b/dcae-analytics-tca/src/main/java/org/openecomp/dcae/apod/analytics/tca/settings/TCAAppConfig.java new file mode 100644 index 0000000..0e4cbe9 --- /dev/null +++ b/dcae-analytics-tca/src/main/java/org/openecomp/dcae/apod/analytics/tca/settings/TCAAppConfig.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.tca.settings; + +import com.google.common.base.Objects; +import org.openecomp.dcae.apod.analytics.common.AnalyticsConstants; +import org.openecomp.dcae.apod.analytics.common.CDAPComponentsConstants; +import org.openecomp.dcae.apod.analytics.common.cdap.settings.CDAPBaseAppConfig; + + +/** + * Contains CDAP App Config Settings for TCA Application + * + * @author Rajiv Singla. Creation Date: 11/2/2016. + */ +public class TCAAppConfig extends CDAPBaseAppConfig { + + + private static final long serialVersionUID = -6775885444808279443L; + + private String tcaSubscriberOutputStreamName; + private Integer thresholdCalculatorFlowletInstances; + + private String tcaVESMessageStatusTableName; + private Integer tcaVESMessageStatusTableTTLSeconds; + private String tcaVESAlertsTableName; + private Integer tcaVESAlertsTableTTLSeconds; + + + public TCAAppConfig() { + appName = CDAPComponentsConstants.TCA_DEFAULT_NAME_APP; + appDescription = CDAPComponentsConstants.TCA_DEFAULT_DESCRIPTION_APP; + tcaSubscriberOutputStreamName = CDAPComponentsConstants.TCA_DEFAULT_SUBSCRIBER_OUTPUT_NAME_STREAM; + thresholdCalculatorFlowletInstances = AnalyticsConstants.TCA_DEFAULT_THRESHOLD_CALCULATOR_FLOWLET_INSTANCES; + tcaVESMessageStatusTableName = CDAPComponentsConstants.TCA_DEFAULT_VES_MESSAGE_STATUS_NAME_TABLE; + tcaVESMessageStatusTableTTLSeconds = AnalyticsConstants.TCA_DEFAULT_VES_MESSAGE_STATUS_TTL_TABLE; + tcaVESAlertsTableName = CDAPComponentsConstants.TCA_DEFAULT_VES_ALERTS_NAME_TABLE; + tcaVESAlertsTableTTLSeconds = AnalyticsConstants.TCA_DEFAULT_VES_ALERTS_TTL_TABLE; + } + + public String getTcaSubscriberOutputStreamName() { + return tcaSubscriberOutputStreamName; + } + + public String getTcaVESMessageStatusTableName() { + return tcaVESMessageStatusTableName; + } + + public Integer getTcaVESMessageStatusTableTTLSeconds() { + return tcaVESMessageStatusTableTTLSeconds; + } + + public String getTcaVESAlertsTableName() { + return tcaVESAlertsTableName; + } + + public Integer getTcaVESAlertsTableTTLSeconds() { + return tcaVESAlertsTableTTLSeconds; + } + + public Integer getThresholdCalculatorFlowletInstances() { + return thresholdCalculatorFlowletInstances; + } + + @Override + public String toString() { + return Objects.toStringHelper(this) + .add("appName", appName) + .add("appDescription", appDescription) + .add("tcaSubscriberOutputStreamName", tcaSubscriberOutputStreamName) + .add("thresholdCalculatorFlowletInstances", thresholdCalculatorFlowletInstances) + .add("tcaVESMessageStatusTableName", tcaVESMessageStatusTableName) + .add("tcaVESMessageStatusTableTTLSeconds", tcaVESMessageStatusTableTTLSeconds) + .add("tcaVESAlertsTableName", tcaVESAlertsTableName) + .add("tcaVESAlertsTableTTLSeconds", tcaVESAlertsTableTTLSeconds) + .toString(); + } +} diff --git a/dcae-analytics-tca/src/main/java/org/openecomp/dcae/apod/analytics/tca/settings/TCAAppPreferences.java b/dcae-analytics-tca/src/main/java/org/openecomp/dcae/apod/analytics/tca/settings/TCAAppPreferences.java new file mode 100644 index 0000000..30c7aaf --- /dev/null +++ b/dcae-analytics-tca/src/main/java/org/openecomp/dcae/apod/analytics/tca/settings/TCAAppPreferences.java @@ -0,0 +1,262 @@ +/* + * ============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.tca.settings; + +import com.google.common.base.Objects; +import org.openecomp.dcae.apod.analytics.common.cdap.settings.CDAPAppPreferences; + +import static org.openecomp.dcae.apod.analytics.common.AnalyticsConstants.TCA_DEFAULT_PUBLISHER_MAX_BATCH_QUEUE_SIZE; +import static org.openecomp.dcae.apod.analytics.common.AnalyticsConstants.TCA_DEFAULT_PUBLISHER_MAX_RECOVERY_QUEUE_SIZE; +import static org.openecomp.dcae.apod.analytics.common.AnalyticsConstants.TCA_DEFAULT_PUBLISHER_POLLING_INTERVAL_MS; +import static org.openecomp.dcae.apod.analytics.common.AnalyticsConstants.TCA_DEFAULT_SUBSCRIBER_POLLING_INTERVAL_MS; + +/** + *

+ * App Preferences for Analytics TCA (Threshold Crossing Alert) App + *

+ * @author Rajiv Singla. Creation Date: 10/4/2016. + */ +public class TCAAppPreferences implements CDAPAppPreferences { + + private static final long serialVersionUID = -685010752560182764L; + + // subscriber preferences + protected String subscriberHostName; + + protected Integer subscriberHostPort; + + protected String subscriberTopicName; + + protected String subscriberProtocol; + + protected String subscriberUserName; + + protected String subscriberUserPassword; + + protected String subscriberContentType; + + protected String subscriberConsumerId; + + protected String subscriberConsumerGroup; + + protected Integer subscriberTimeoutMS; + + protected Integer subscriberMessageLimit; + + protected Integer subscriberPollingInterval; + + // publisher preferences + protected String publisherHostName; + + protected Integer publisherHostPort; + + protected String publisherTopicName; + + protected String publisherProtocol; + + protected String publisherUserName; + + protected String publisherUserPassword; + + protected String publisherContentType; + + protected Integer publisherMaxBatchSize; + + protected Integer publisherMaxRecoveryQueueSize; + + protected Integer publisherPollingInterval; + + + public TCAAppPreferences() { + + // subscriber defaults + subscriberPollingInterval = TCA_DEFAULT_SUBSCRIBER_POLLING_INTERVAL_MS; + + // publisher defaults + publisherMaxBatchSize = TCA_DEFAULT_PUBLISHER_MAX_BATCH_QUEUE_SIZE; + publisherMaxRecoveryQueueSize = TCA_DEFAULT_PUBLISHER_MAX_RECOVERY_QUEUE_SIZE; + publisherPollingInterval = TCA_DEFAULT_PUBLISHER_POLLING_INTERVAL_MS; + + } + + public String getSubscriberHostName() { + return subscriberHostName; + } + + public Integer getSubscriberHostPort() { + return subscriberHostPort; + } + + public String getSubscriberTopicName() { + return subscriberTopicName; + } + + public String getSubscriberProtocol() { + return subscriberProtocol; + } + + public String getSubscriberUserName() { + return subscriberUserName; + } + + public String getSubscriberUserPassword() { + return subscriberUserPassword; + } + + public String getSubscriberContentType() { + return subscriberContentType; + } + + public String getSubscriberConsumerId() { + return subscriberConsumerId; + } + + public String getSubscriberConsumerGroup() { + return subscriberConsumerGroup; + } + + public Integer getSubscriberTimeoutMS() { + return subscriberTimeoutMS; + } + + public Integer getSubscriberMessageLimit() { + return subscriberMessageLimit; + } + + public Integer getSubscriberPollingInterval() { + return subscriberPollingInterval; + } + + public String getPublisherHostName() { + return publisherHostName; + } + + public Integer getPublisherHostPort() { + return publisherHostPort; + } + + public String getPublisherTopicName() { + return publisherTopicName; + } + + public String getPublisherProtocol() { + return publisherProtocol; + } + + public String getPublisherUserName() { + return publisherUserName; + } + + public String getPublisherUserPassword() { + return publisherUserPassword; + } + + public String getPublisherContentType() { + return publisherContentType; + } + + public Integer getPublisherMaxBatchSize() { + return publisherMaxBatchSize; + } + + public Integer getPublisherMaxRecoveryQueueSize() { + return publisherMaxRecoveryQueueSize; + } + + public Integer getPublisherPollingInterval() { + return publisherPollingInterval; + } + + + public void setSubscriberHostName(String subscriberHostName) { + this.subscriberHostName = subscriberHostName; + } + + public void setSubscriberHostPort(Integer subscriberHostPort) { + this.subscriberHostPort = subscriberHostPort; + } + + public void setSubscriberTopicName(String subscriberTopicName) { + this.subscriberTopicName = subscriberTopicName; + } + + public void setSubscriberProtocol(String subscriberProtocol) { + this.subscriberProtocol = subscriberProtocol; + } + + public void setSubscriberUserName(String subscriberUserName) { + this.subscriberUserName = subscriberUserName; + } + + public void setSubscriberUserPassword(String subscriberUserPassword) { + this.subscriberUserPassword = subscriberUserPassword; + } + + public void setPublisherHostName(String publisherHostName) { + this.publisherHostName = publisherHostName; + } + + public void setPublisherHostPort(Integer publisherHostPort) { + this.publisherHostPort = publisherHostPort; + } + + public void setPublisherTopicName(String publisherTopicName) { + this.publisherTopicName = publisherTopicName; + } + + public void setPublisherProtocol(String publisherProtocol) { + this.publisherProtocol = publisherProtocol; + } + + public void setPublisherUserName(String publisherUserName) { + this.publisherUserName = publisherUserName; + } + + public void setPublisherUserPassword(String publisherUserPassword) { + this.publisherUserPassword = publisherUserPassword; + } + + @Override + public String toString() { + return Objects.toStringHelper(this) + .add("subscriberHostName", subscriberHostName) + .add("subscriberHostPort", subscriberHostPort) + .add("subscriberTopicName", subscriberTopicName) + .add("subscriberProtocol", subscriberProtocol) + .add("subscriberUserName", subscriberUserName) + .add("subscriberContentType", subscriberContentType) + .add("subscriberConsumerId", subscriberConsumerId) + .add("subscriberConsumerGroup", subscriberConsumerGroup) + .add("subscriberTimeoutMS", subscriberTimeoutMS) + .add("subscriberMessageLimit", subscriberMessageLimit) + .add("subscriberPollingInterval", subscriberPollingInterval) + .add("publisherHostName", publisherHostName) + .add("publisherHostPort", publisherHostPort) + .add("publisherTopicName", publisherTopicName) + .add("publisherProtocol", publisherProtocol) + .add("publisherUserName", publisherUserName) + .add("publisherContentType", publisherContentType) + .add("publisherMaxBatchSize", publisherMaxBatchSize) + .add("publisherMaxRecoveryQueueSize", publisherMaxRecoveryQueueSize) + .add("publisherPollingInterval", publisherPollingInterval) + .toString(); + } +} diff --git a/dcae-analytics-tca/src/main/java/org/openecomp/dcae/apod/analytics/tca/settings/TCAPolicyPreferences.java b/dcae-analytics-tca/src/main/java/org/openecomp/dcae/apod/analytics/tca/settings/TCAPolicyPreferences.java new file mode 100644 index 0000000..5ee4e7a --- /dev/null +++ b/dcae-analytics-tca/src/main/java/org/openecomp/dcae/apod/analytics/tca/settings/TCAPolicyPreferences.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.tca.settings; + +import org.openecomp.dcae.apod.analytics.common.cdap.settings.CDAPAppPreferences; +import org.openecomp.dcae.apod.analytics.model.domain.policy.tca.TCAPolicy; + +/** + * A wrapper over {@link TCAPolicy} to act as app Preferences as TCA Policy is passed + * by controller as runtime arguments from CDAP app preferences + *

+ * @author Rajiv Singla. Creation Date: 11/29/2016. + */ +public class TCAPolicyPreferences extends TCAPolicy implements CDAPAppPreferences { + + private static final long serialVersionUID = -2957485388641574043L; + +} diff --git a/dcae-analytics-tca/src/main/java/org/openecomp/dcae/apod/analytics/tca/utils/AppPreferencesToPublisherConfigMapper.java b/dcae-analytics-tca/src/main/java/org/openecomp/dcae/apod/analytics/tca/utils/AppPreferencesToPublisherConfigMapper.java new file mode 100644 index 0000000..7c6de64 --- /dev/null +++ b/dcae-analytics-tca/src/main/java/org/openecomp/dcae/apod/analytics/tca/utils/AppPreferencesToPublisherConfigMapper.java @@ -0,0 +1,82 @@ +/* + * ============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.tca.utils; + +import com.google.common.base.Function; +import org.openecomp.dcae.apod.analytics.dmaap.domain.config.DMaaPMRPublisherConfig; +import org.openecomp.dcae.apod.analytics.tca.settings.TCAAppPreferences; + +import javax.annotation.Nonnull; + +import static org.openecomp.dcae.apod.analytics.common.utils.ValidationUtils.isEmpty; + +/** + * Function which translates {@link TCAAppPreferences} to {@link DMaaPMRPublisherConfig} + * + * @author Rajiv Singla. Creation Date: 11/17/2016. + */ +public class AppPreferencesToPublisherConfigMapper implements Function { + + public static DMaaPMRPublisherConfig map(final TCAAppPreferences tcaAppPreferences) { + return new AppPreferencesToPublisherConfigMapper().apply(tcaAppPreferences); + } + + @Nonnull + @Override + public DMaaPMRPublisherConfig apply(@Nonnull TCAAppPreferences tcaAppPreferences) { + + // Create a new publisher settings builder + final DMaaPMRPublisherConfig.Builder publisherConfigBuilder = new DMaaPMRPublisherConfig.Builder( + tcaAppPreferences.getPublisherHostName(), tcaAppPreferences.getPublisherTopicName()); + + // Setup up any optional publisher parameters if they are present + final Integer publisherHostPort = tcaAppPreferences.getPublisherHostPort(); + if (publisherHostPort != null) { + publisherConfigBuilder.setPortNumber(publisherHostPort); + } + final String publisherProtocol = tcaAppPreferences.getPublisherProtocol(); + if (!isEmpty(publisherProtocol)) { + publisherConfigBuilder.setProtocol(publisherProtocol); + } + final String publisherUserName = tcaAppPreferences.getPublisherUserName(); + if (!isEmpty(publisherUserName)) { + publisherConfigBuilder.setUserName(publisherUserName); + } + final String publisherUserPassword = tcaAppPreferences.getPublisherUserPassword(); + if (!isEmpty(publisherUserPassword)) { + publisherConfigBuilder.setUserPassword(publisherUserPassword); + } + final String publisherContentType = tcaAppPreferences.getPublisherContentType(); + if (!isEmpty(publisherContentType)) { + publisherConfigBuilder.setContentType(publisherContentType); + } + final Integer publisherMaxBatchSize = tcaAppPreferences.getPublisherMaxBatchSize(); + if (publisherMaxBatchSize != null) { + publisherConfigBuilder.setMaxBatchSize(publisherMaxBatchSize); + } + final Integer publisherMaxRecoveryQueueSize = tcaAppPreferences.getPublisherMaxRecoveryQueueSize(); + if (publisherMaxRecoveryQueueSize != null) { + publisherConfigBuilder.setMaxRecoveryQueueSize(publisherMaxRecoveryQueueSize); + } + + return publisherConfigBuilder.build(); + } +} diff --git a/dcae-analytics-tca/src/main/java/org/openecomp/dcae/apod/analytics/tca/utils/AppPreferencesToSubscriberConfigMapper.java b/dcae-analytics-tca/src/main/java/org/openecomp/dcae/apod/analytics/tca/utils/AppPreferencesToSubscriberConfigMapper.java new file mode 100644 index 0000000..5188916 --- /dev/null +++ b/dcae-analytics-tca/src/main/java/org/openecomp/dcae/apod/analytics/tca/utils/AppPreferencesToSubscriberConfigMapper.java @@ -0,0 +1,99 @@ +/* + * ============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.tca.utils; + +import com.google.common.base.Function; +import org.openecomp.dcae.apod.analytics.dmaap.domain.config.DMaaPMRSubscriberConfig; +import org.openecomp.dcae.apod.analytics.tca.settings.TCAAppPreferences; + +import javax.annotation.Nonnull; + +import static org.openecomp.dcae.apod.analytics.common.utils.ValidationUtils.isEmpty; + +/** + * Function which translates {@link TCAAppPreferences} to {@link DMaaPMRSubscriberConfig} + * + * @author Rajiv Singla. Creation Date: 11/17/2016. + */ +public class AppPreferencesToSubscriberConfigMapper implements Function { + + public static DMaaPMRSubscriberConfig map(final TCAAppPreferences tcaAppPreferences) { + return new AppPreferencesToSubscriberConfigMapper().apply(tcaAppPreferences); + } + + @Nonnull + @Override + public DMaaPMRSubscriberConfig apply(@Nonnull TCAAppPreferences tcaAppPreferences) { + + // Create a new subscriber settings builder + final DMaaPMRSubscriberConfig.Builder subscriberConfigBuilder = new DMaaPMRSubscriberConfig.Builder( + tcaAppPreferences.getSubscriberHostName(), tcaAppPreferences.getSubscriberTopicName()); + + // Setup up any optional subscriber parameters if they are present + final Integer subscriberHostPortNumber = tcaAppPreferences.getSubscriberHostPort(); + if (subscriberHostPortNumber != null) { + subscriberConfigBuilder.setPortNumber(subscriberHostPortNumber); + } + + final String subscriberProtocol = tcaAppPreferences.getSubscriberProtocol(); + if (!isEmpty(subscriberProtocol)) { + subscriberConfigBuilder.setProtocol(subscriberProtocol); + } + + final String subscriberUserName = tcaAppPreferences.getSubscriberUserName(); + if (!isEmpty(subscriberUserName)) { + subscriberConfigBuilder.setUserName(subscriberUserName); + } + + final String subscriberUserPassword = tcaAppPreferences.getSubscriberUserPassword(); + if (!isEmpty(subscriberUserPassword)) { + subscriberConfigBuilder.setUserPassword(subscriberUserPassword); + } + + final String subscriberContentType = tcaAppPreferences.getSubscriberContentType(); + if (!isEmpty(subscriberContentType)) { + subscriberConfigBuilder.setContentType(subscriberContentType); + } + + final String subscriberConsumerId = tcaAppPreferences.getSubscriberConsumerId(); + if (!isEmpty(subscriberConsumerId)) { + subscriberConfigBuilder.setConsumerId(subscriberConsumerId); + } + + final String subscriberConsumerGroup = tcaAppPreferences.getSubscriberConsumerGroup(); + if (!isEmpty(subscriberConsumerGroup)) { + subscriberConfigBuilder.setConsumerGroup(subscriberConsumerGroup); + } + + final Integer subscriberTimeoutMS = tcaAppPreferences.getSubscriberTimeoutMS(); + if (subscriberTimeoutMS != null) { + subscriberConfigBuilder.setTimeoutMS(subscriberTimeoutMS); + } + final Integer subscriberMessageLimit = tcaAppPreferences.getSubscriberMessageLimit(); + if (subscriberMessageLimit != null) { + subscriberConfigBuilder.setMessageLimit(subscriberMessageLimit); + } + + // return Subscriber settings + return subscriberConfigBuilder.build(); + + } +} diff --git a/dcae-analytics-tca/src/main/java/org/openecomp/dcae/apod/analytics/tca/utils/TCAUtils.java b/dcae-analytics-tca/src/main/java/org/openecomp/dcae/apod/analytics/tca/utils/TCAUtils.java new file mode 100644 index 0000000..d0a17dd --- /dev/null +++ b/dcae-analytics-tca/src/main/java/org/openecomp/dcae/apod/analytics/tca/utils/TCAUtils.java @@ -0,0 +1,825 @@ +/* + * ============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.tca.utils; + +import co.cask.cdap.api.RuntimeContext; +import com.google.common.base.Function; +import com.google.common.base.Optional; +import com.google.common.base.Predicate; +import com.google.common.base.Predicates; +import com.google.common.base.Supplier; +import com.google.common.base.Suppliers; +import com.google.common.collect.HashBasedTable; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.Iterables; +import com.google.common.collect.Lists; +import com.google.common.collect.Maps; +import com.google.common.collect.Table; +import com.jayway.jsonpath.DocumentContext; +import com.jayway.jsonpath.JsonPath; +import com.jayway.jsonpath.TypeRef; +import org.apache.commons.lang3.StringUtils; +import org.apache.commons.lang3.tuple.ImmutablePair; +import org.apache.commons.lang3.tuple.Pair; +import org.openecomp.dcae.apod.analytics.common.AnalyticsConstants; +import org.openecomp.dcae.apod.analytics.common.exception.DCAEAnalyticsRuntimeException; +import org.openecomp.dcae.apod.analytics.common.exception.MessageProcessingException; +import org.openecomp.dcae.apod.analytics.common.service.processor.AbstractMessageProcessor; +import org.openecomp.dcae.apod.analytics.common.service.processor.GenericMessageChainProcessor; +import org.openecomp.dcae.apod.analytics.model.domain.cef.CommonEventHeader; +import org.openecomp.dcae.apod.analytics.model.domain.cef.EventListener; +import org.openecomp.dcae.apod.analytics.model.domain.cef.EventSeverity; +import org.openecomp.dcae.apod.analytics.model.domain.policy.tca.Direction; +import org.openecomp.dcae.apod.analytics.model.domain.policy.tca.MetricsPerFunctionalRole; +import org.openecomp.dcae.apod.analytics.model.domain.policy.tca.TCAPolicy; +import org.openecomp.dcae.apod.analytics.model.domain.policy.tca.Threshold; +import org.openecomp.dcae.apod.analytics.model.facade.tca.AAI; +import org.openecomp.dcae.apod.analytics.model.facade.tca.TCAVESResponse; +import org.openecomp.dcae.apod.analytics.model.util.AnalyticsModelJsonUtils; +import org.openecomp.dcae.apod.analytics.tca.persistance.TCAVESAlertEntity; +import org.openecomp.dcae.apod.analytics.tca.processor.TCACEFJsonProcessor; +import org.openecomp.dcae.apod.analytics.tca.processor.TCACEFPolicyDomainFilter; +import org.openecomp.dcae.apod.analytics.tca.processor.TCACEFPolicyFunctionalRoleFilter; +import org.openecomp.dcae.apod.analytics.tca.processor.TCACEFPolicyThresholdsProcessor; +import org.openecomp.dcae.apod.analytics.tca.processor.TCACEFProcessorContext; +import org.openecomp.dcae.apod.analytics.tca.settings.TCAAppPreferences; +import org.openecomp.dcae.apod.analytics.tca.settings.TCAPolicyPreferences; +import org.openecomp.dcae.apod.analytics.tca.validator.TCAPolicyPreferencesValidator; +import org.openecomp.dcae.apod.analytics.tca.validator.TCAPreferencesValidator; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.net.MalformedURLException; +import java.net.URL; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.Comparator; +import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.SortedMap; +import java.util.TreeMap; +import java.util.UUID; + +import javax.annotation.Nonnull; +import javax.annotation.Nullable; + +import static com.google.common.collect.Lists.newArrayList; +import static org.openecomp.dcae.apod.analytics.common.AnalyticsConstants.TCA_POLICY_METRICS_PER_FUNCTIONAL_ROLE_PATH; +import static org.openecomp.dcae.apod.analytics.common.utils.ValidationUtils.validateSettings; + +/** + * Utility Helper methods for TCA sub module only. Extends {@link AnalyticsModelJsonUtils} to get + * pre configured Json Object Mapper understand serialization and deserialization of CEF Message + * and TCA Policy + * + * @author Rajiv Singla. Creation Date: 10/24/2016. + */ +public abstract class TCAUtils extends AnalyticsModelJsonUtils { + + private static final Logger LOG = LoggerFactory.getLogger(TCAUtils.class); + + /** + * Threshold Comparator which is used to order thresholds based on their severity e.g. ( CRITICAL, MAJOR, MINOR, + * WARNING ) + */ + private static final Comparator THRESHOLD_COMPARATOR = new Comparator() { + @Override + public int compare(Threshold threshold1, Threshold threshold2) { + return threshold1.getSeverity().compareTo(threshold2.getSeverity()); + } + }; + + + /** + * {@link Function} that extracts {@link TCAPolicy#getMetricsPerFunctionalRole()} from {@link TCAPolicy} + * + * @return TCA Policy Metrics Per Functional Roles List + */ + public static Function> tcaPolicyMetricsExtractorFunction() { + return new Function>() { + @Nullable + @Override + public List apply(@Nonnull TCAPolicy tcaPolicy) { + return tcaPolicy.getMetricsPerFunctionalRole(); + } + }; + } + + /** + * {@link Function} that extracts {@link MetricsPerFunctionalRole#getFunctionalRole()} from + * {@link MetricsPerFunctionalRole} + * + * @return Functional role or a Metrics Per Functional Role object + */ + public static Function tcaFunctionalRoleExtractorFunction() { + return new Function() { + @Override + public String apply(@Nonnull MetricsPerFunctionalRole metricsPerFunctionalRole) { + return metricsPerFunctionalRole.getFunctionalRole(); + } + }; + } + + + /** + * Extracts {@link TCAPolicy} Functional Roles + * + * @param tcaPolicy TCA Policy + * @return List of functional Roles in the tca Policy + */ + public static List getPolicyFunctionalRoles(@Nonnull final TCAPolicy tcaPolicy) { + final List metricsPerFunctionalRoles = + tcaPolicyMetricsExtractorFunction().apply(tcaPolicy); + + return Lists.transform(metricsPerFunctionalRoles, tcaFunctionalRoleExtractorFunction()); + } + + /** + * A {@link Supplier} which caches {@link TCAPolicy} Functional Roles as they are not expected to + * change during runtime + * + * @param tcaPolicy TCA Policy + * @return a Supplier that memoize the Functional roles + */ + public static Supplier> getPolicyFunctionalRoleSupplier(@Nonnull final TCAPolicy tcaPolicy) { + return Suppliers.memoize(new Supplier>() { + @Override + public List get() { + return getPolicyFunctionalRoles(tcaPolicy); + } + }); + } + + + /** + * Creates a Table to lookup thresholds of a {@link TCAPolicy} by its Functional Role and Threshold Field path + * + * @param tcaPolicy TCA Policy + * @return A table with Keys of functional role and field path containing List of threshold as values + */ + public static Table> getPolicyFRThresholdsTable(final TCAPolicy tcaPolicy) { + final Table> domainFRTable = HashBasedTable.create(); + for (MetricsPerFunctionalRole metricsPerFunctionalRole : tcaPolicy.getMetricsPerFunctionalRole()) { + final String functionalRole = metricsPerFunctionalRole.getFunctionalRole(); + final List thresholds = metricsPerFunctionalRole.getThresholds(); + for (Threshold threshold : thresholds) { + final List existingThresholds = domainFRTable.get(functionalRole, threshold.getFieldPath()); + if (existingThresholds == null) { + final LinkedList newThresholdList = new LinkedList<>(); + newThresholdList.add(threshold); + domainFRTable.put(functionalRole, threshold.getFieldPath(), newThresholdList); + } else { + domainFRTable.get(functionalRole, threshold.getFieldPath()).add(threshold); + } + } + } + return domainFRTable; + } + + + /** + * A {@link Supplier} which caches Policy Functional Role and Threshold Field Path Thresholds lookup table + * + * @param tcaPolicy TCA Policy + * @return Cached Supplier for table with Keys of functional role and field path containing thresholds as values + */ + public static Supplier>> getPolicyFRThresholdsTableSupplier + (final TCAPolicy tcaPolicy) { + return Suppliers.memoize(new Supplier>>() { + @Override + public Table> get() { + return getPolicyFRThresholdsTable(tcaPolicy); + } + }); + } + + + /** + * Parses and validates Runtime Arguments to {@link TCAAppPreferences} object + * + * @param runtimeContext Runtime Context + * + * @return validated runtime arguments as {@link TCAAppPreferences} object + */ + public static TCAAppPreferences getValidatedTCAAppPreferences(final RuntimeContext runtimeContext) { + // Parse runtime arguments + final Map runtimeArguments = runtimeContext.getRuntimeArguments(); + final TCAAppPreferences tcaAppPreferences = + ANALYTICS_MODEL_OBJECT_MAPPER.convertValue(runtimeArguments, TCAAppPreferences.class); + + // Update values of app preferences based on controller passed arguments if required + final TCAAppPreferences updatedTCAAppPreferences = + updateDMaaPPubSubValues(runtimeArguments, tcaAppPreferences); + + // Validate runtime arguments + validateSettings(updatedTCAAppPreferences, new TCAPreferencesValidator()); + + return tcaAppPreferences; + } + + + /** + * Updates DMaaP Subscriber and Publisher Urls if present in runtime arguments. + * Maps runtime arguments property - dmaap.in.event-input.dmaapUrl to Subscriber host, port and topic + * Maps runtime arguments property - dmaap.out.alert-output.dmaapUrl to Publisher host, port and topic + * + * @param runtimeArguments Runtime arguments passed in to TCA App by controller + * @param tcaAppPreferences TCA App Preferences + * + * @return TCA App Preferences which updated Publisher and Subscriber host,port and topic values + */ + public static TCAAppPreferences updateDMaaPPubSubValues(final Map runtimeArguments, + final TCAAppPreferences tcaAppPreferences) { + + final String subscriberPropertyKey = "dmaap.in.event-input.dmaapUrl"; + final String subscriberPropertyValue = runtimeArguments.get(subscriberPropertyKey); + + if (subscriberPropertyValue != null) { + LOG.debug("Updating value for DMaaP Subscriber to values provided in property: {} with value: {}", + subscriberPropertyKey, subscriberPropertyValue); + final URL subscriberUrl = parseURL(subscriberPropertyValue); + tcaAppPreferences.setSubscriberProtocol(subscriberUrl.getProtocol()); + tcaAppPreferences.setSubscriberHostName(subscriberUrl.getHost()); + final int subscriberUrlPort = subscriberUrl.getPort() != -1 ? + Integer.valueOf(subscriberUrl.getPort()) : getDefaultDMaaPPort(subscriberUrl.getProtocol()); + tcaAppPreferences.setSubscriberHostPort(subscriberUrlPort); + tcaAppPreferences.setSubscriberTopicName(subscriberUrl.getPath().substring(1)); + } + + final String subscriberUserNamePropertyKey = "dmaap.in.event-input.dmaapUserName"; + if (runtimeArguments.containsKey(subscriberUserNamePropertyKey)) { + tcaAppPreferences.setSubscriberUserName(runtimeArguments.get(subscriberUserNamePropertyKey)); + } + final String subscriberPasswordPropertyKey = "dmaap.in.event-input.dmaapPassword"; + if (runtimeArguments.containsKey(subscriberPasswordPropertyKey)) { + tcaAppPreferences.setSubscriberUserPassword(runtimeArguments.get(subscriberPasswordPropertyKey)); + } + + final String publisherPropertyKey = "dmaap.out.alert-output.dmaapUrl"; + final String publisherPropertyValue = runtimeArguments.get(publisherPropertyKey); + if (publisherPropertyValue != null) { + LOG.debug("Updating value for DMaaP Publisher to values provided in property: {} with value: {}", + publisherPropertyKey, publisherPropertyValue); + final URL publisherUrl = parseURL(publisherPropertyValue); + tcaAppPreferences.setPublisherProtocol(publisherUrl.getProtocol()); + tcaAppPreferences.setPublisherHostName(publisherUrl.getHost()); + final int publisherUrlPort = publisherUrl.getPort() != -1 ? + Integer.valueOf(publisherUrl.getPort()) : getDefaultDMaaPPort(publisherUrl.getProtocol()); + tcaAppPreferences.setPublisherHostPort(publisherUrlPort); + tcaAppPreferences.setPublisherTopicName(publisherUrl.getPath().substring(1)); + } + + final String publisherUserNamePropertyKey = "dmaap.out.alert-output.dmaapUserName"; + if (runtimeArguments.containsKey(publisherUserNamePropertyKey)) { + tcaAppPreferences.setPublisherUserName(runtimeArguments.get(publisherUserNamePropertyKey)); + } + final String publisherPasswordPropertyKey = "dmaap.out.alert-output.dmaapPassword"; + if (runtimeArguments.containsKey(publisherPasswordPropertyKey)) { + tcaAppPreferences.setPublisherUserPassword(runtimeArguments.get(publisherPasswordPropertyKey)); + } + + + return tcaAppPreferences; + } + + /** + * Sets up default DMaaP Port if not provided with DMaaP URL + * + * @param protocol protocol e.g. http or https + * + * @return default DMaaP MR port number + */ + private static int getDefaultDMaaPPort(final String protocol) { + if ("http".equals(protocol)) { + return 3904; + } else if ("https".equals(protocol)) { + return 3905; + } else { + return 80; + } + } + + /** + * Parses provided DMaaP MR URL string to {@link URL} object + * + * @param urlString url string + * + * @return url object + */ + private static URL parseURL(final String urlString) { + try { + return new URL(urlString); + } catch (MalformedURLException e) { + final String errorMessage = String.format("Invalid URL format: %s", urlString); + throw new DCAEAnalyticsRuntimeException(errorMessage, LOG, e); + } + } + + + /** + * Creates a {@link GenericMessageChainProcessor} of {@link TCACEFJsonProcessor}, + * {@link TCACEFPolicyDomainFilter} and {@link TCACEFPolicyFunctionalRoleFilter}s to + * filter out messages which does not match policy domain or functional role + * + * @param cefMessage CEF Message + * @param tcaPolicy TCA Policy + * @return Message Process Context after processing filter chain + */ + public static TCACEFProcessorContext filterCEFMessage(@Nullable final String cefMessage, + @Nonnull final TCAPolicy tcaPolicy) { + + final TCACEFJsonProcessor jsonProcessor = new TCACEFJsonProcessor(); + final TCACEFPolicyDomainFilter domainFilter = new TCACEFPolicyDomainFilter(); + final TCACEFPolicyFunctionalRoleFilter functionalRoleFilter = new TCACEFPolicyFunctionalRoleFilter(); + // Create a list of message processors + final ImmutableList> messageProcessors = + ImmutableList.of(jsonProcessor, domainFilter, functionalRoleFilter); + final TCACEFProcessorContext processorContext = new TCACEFProcessorContext(cefMessage, tcaPolicy); + // Create a message processors chain + final GenericMessageChainProcessor tcaProcessingChain = + new GenericMessageChainProcessor<>(messageProcessors, processorContext); + // process chain + return tcaProcessingChain.processChain(); + } + + + /** + * Extracts json path values for given json Field Paths from using Json path notation. Assumes + * that values extracted are always long + * + * @param message CEF Message + * @param jsonFieldPaths Json Field Paths + * @return Map containing key as json path and values as values associated with that json path + */ + public static Map> getJsonPathValue(@Nonnull String message, @Nonnull Set + jsonFieldPaths) { + + final Map> jsonFieldPathMap = new HashMap<>(); + final DocumentContext documentContext = JsonPath.parse(message); + + for (String jsonFieldPath : jsonFieldPaths) { + final List jsonFieldValues = documentContext.read(jsonFieldPath, new TypeRef>() { + }); + // If Json Field Values are not or empty + if (jsonFieldValues != null && !jsonFieldValues.isEmpty()) { + // Filter out all null values in the filed values list + final List nonNullValues = Lists.newLinkedList(Iterables.filter(jsonFieldValues, + Predicates.notNull())); + // If there are non null values put them in the map + if (!nonNullValues.isEmpty()) { + jsonFieldPathMap.put(jsonFieldPath, nonNullValues); + } + } + } + + return jsonFieldPathMap; + } + + /** + * Computes if any CEF Message Fields have violated any Policy Thresholds. For the same policy field path + * it applies threshold in order of their severity and record the first threshold per message field path + * + * @param messageFieldValues Field Path Values extracted from CEF Message + * @param fieldThresholds Policy Thresholds for Field Path + * @return Optional of violated threshold for a field path + */ + public static Optional thresholdCalculator(final List messageFieldValues, final List + fieldThresholds) { + // order thresholds by severity + Collections.sort(fieldThresholds, THRESHOLD_COMPARATOR); + // Now apply each threshold to field values + for (Threshold fieldThreshold : fieldThresholds) { + for (Long messageFieldValue : messageFieldValues) { + final Boolean isThresholdViolated = + fieldThreshold.getDirection().operate(messageFieldValue, fieldThreshold.getThresholdValue()); + if (isThresholdViolated) { + return Optional.of(fieldThreshold); + } + } + } + return Optional.absent(); + } + + /** + * Prioritize Threshold to be reported in case there was multiple TCA violations in a single CEF message. + * Grabs first highest priority violated threshold + * + * @param violatedThresholdsMap Map containing field Path and associated violated Thresholds + * @return First Highest priority violated threshold + */ + public static Threshold prioritizeThresholdViolations(final Map violatedThresholdsMap) { + + final List violatedThresholds = newArrayList(violatedThresholdsMap.values()); + + if (violatedThresholds.size() == 1) { + return violatedThresholds.get(0); + } + Collections.sort(violatedThresholds, THRESHOLD_COMPARATOR); + // Just grab the first violated threshold with highest priority + return violatedThresholds.get(0); + } + + + /** + * Creates {@link MetricsPerFunctionalRole} object which contains violated thresholds + * + * @param tcaPolicy TCA Policy + * @param violatedThreshold Violated thresholds + * @param functionalRole Functiona Role + * + * @return MetricsPerFunctionalRole object containing one highest severity violated threshold + */ + public static MetricsPerFunctionalRole createViolatedMetrics(@Nonnull final TCAPolicy tcaPolicy, + @Nonnull final Threshold violatedThreshold, + @Nonnull final String functionalRole) { + + final ArrayList metricsPerFunctionalRoles = newArrayList( + Iterables.filter(tcaPolicy.getMetricsPerFunctionalRole(), new Predicate() { + @Override + public boolean apply(@Nonnull MetricsPerFunctionalRole metricsPerFunctionalRole) { + return metricsPerFunctionalRole.getFunctionalRole().equals(functionalRole); + } + })); + // TCA policy must have only one metrics role per functional role + if (metricsPerFunctionalRoles.size() == 1) { + final MetricsPerFunctionalRole policyMetrics = metricsPerFunctionalRoles.get(0); + final MetricsPerFunctionalRole violatedMetrics = new MetricsPerFunctionalRole(); + violatedMetrics.setFunctionalRole(policyMetrics.getFunctionalRole()); + violatedMetrics.setPolicyScope(policyMetrics.getPolicyScope()); + violatedMetrics.setPolicyName(policyMetrics.getPolicyName()); + violatedMetrics.setPolicyVersion(policyMetrics.getPolicyVersion()); + violatedMetrics.setThresholds(ImmutableList.of(violatedThreshold)); + return violatedMetrics; + } else { + final String errorMessage = String.format("TCA Policy must contain functional Role: %s", functionalRole); + throw new MessageProcessingException(errorMessage, LOG, new IllegalStateException(errorMessage)); + } + } + + /** + * Computes threshold violations + * + * @param processorContext Filtered processor Context + * @return processor context with any threshold violations + */ + public static TCACEFProcessorContext computeThresholdViolations(final TCACEFProcessorContext processorContext) { + final TCACEFPolicyThresholdsProcessor policyThresholdsProcessor = new TCACEFPolicyThresholdsProcessor(); + return policyThresholdsProcessor.apply(processorContext); + } + + /** + * Creates {@link TCAVESResponse} object + * + * @param processorContext processor Context with violations + * @param tcaAppName TCA App Name + * + * @return TCA VES Response Message + */ + public static TCAVESResponse createNewTCAVESResponse(final TCACEFProcessorContext processorContext, + final String tcaAppName) { + + final MetricsPerFunctionalRole metricsPerFunctionalRole = processorContext.getMetricsPerFunctionalRole(); + // confirm violations are indeed present + if (metricsPerFunctionalRole == null) { + final String errorMessage = "No violations metrics. Unable to create VES Response"; + throw new MessageProcessingException(errorMessage, LOG, new IllegalArgumentException(errorMessage)); + } + + final String functionalRole = metricsPerFunctionalRole.getFunctionalRole(); + final Threshold violatedThreshold = metricsPerFunctionalRole.getThresholds().get(0); + final EventListener eventListener = processorContext.getCEFEventListener(); + final CommonEventHeader commonEventHeader = eventListener.getEvent().getCommonEventHeader(); + + final TCAVESResponse tcavesResponse = new TCAVESResponse(); + // ClosedLoopControlName included in the DCAE configuration Policy + tcavesResponse.setClosedLoopControlName(violatedThreshold.getClosedLoopControlName()); + // version included in the DCAE configuration Policy + tcavesResponse.setVersion(violatedThreshold.getVersion()); + // Generate a UUID for this output message + tcavesResponse.setRequestID(UUID.randomUUID().toString()); + // commonEventHeader.startEpochMicrosec from the received VES measurementsForVfScaling message + tcavesResponse.setClosedLoopAlarmStart(commonEventHeader.getStartEpochMicrosec()); + // Concatenate name of this DCAE instance and name for this TCA instance, separated by dot + // TODO: Find out how to get this field + tcavesResponse.setClosedLoopEventClient("DCAE_INSTANCE_ID." + tcaAppName); + + final AAI aai = new AAI(); + tcavesResponse.setAai(aai); + + // vLoadBalancer specific settings + if (isFunctionalRoleVLoadBalancer(functionalRole)) { + // Hard Coded - "VM" + tcavesResponse.setTargetType(AnalyticsConstants.LOAD_BALANCER_TCA_VES_RESPONSE_TARGET_TYPE); + // Hard Coded - "vserver.vserver-name" + tcavesResponse.setTarget(AnalyticsConstants.LOAD_BALANCER_TCA_VES_RESPONSE_TARGET); + aai.setGenericServerId(commonEventHeader.getReportingEntityName()); + } else { + // Hard Coded - "VNF" + tcavesResponse.setTargetType(AnalyticsConstants.TCA_VES_RESPONSE_TARGET_TYPE); + // Hard Coded - "generic-vnf.vnf-id" + tcavesResponse.setTarget(AnalyticsConstants.TCA_VES_RESPONSE_TARGET); + // commonEventHeader.reportingEntityName from the received VES measurementsForVfScaling message (value for + // the data element used in A&AI) + aai.setGenericVNFId(commonEventHeader.getReportingEntityName()); + } + + // Hard Coded - "DCAE" + tcavesResponse.setFrom(AnalyticsConstants.TCA_VES_RESPONSE_FROM); + // policyScope included in the DCAE configuration Policy + tcavesResponse.setPolicyScope(metricsPerFunctionalRole.getPolicyScope()); + // policyName included in the DCAE configuration Policy + tcavesResponse.setPolicyName(metricsPerFunctionalRole.getPolicyName()); + // policyVersion included in the DCAE configuration Policy + tcavesResponse.setPolicyVersion(metricsPerFunctionalRole.getPolicyVersion()); + // Hard Coded - "ONSET" + tcavesResponse.setClosedLoopEventStatus(AnalyticsConstants.TCA_VES_RESPONSE_CLOSED_LOOP_EVENT_STATUS); + + return tcavesResponse; + } + + /** + * Determines if Functional Role is vLoadBlanacer + * + * @param functionalRole functional Role to check + * + * @return return true if functional role is for vLoadBalancer + */ + private static boolean isFunctionalRoleVLoadBalancer(final String functionalRole) { + return functionalRole.equals(AnalyticsConstants.LOAD_BALANCER_FUNCTIONAL_ROLE); + } + + + /** + * Extract Domain and functional Role from processor context if present + * + * @param processorContext processor context + * @return Tuple of domain and functional role + */ + public static Pair getDomainAndFunctionalRole(@Nullable final TCACEFProcessorContext + processorContext) { + + String domain = null; + String functionalRole = null; + + + if (processorContext != null && + processorContext.getCEFEventListener() != null && + processorContext.getCEFEventListener().getEvent() != null && + processorContext.getCEFEventListener().getEvent().getCommonEventHeader() != null) { + final CommonEventHeader commonEventHeader = processorContext.getCEFEventListener().getEvent() + .getCommonEventHeader(); + + if (commonEventHeader.getDomain() != null) { + domain = commonEventHeader.getDomain(); + } + + if (commonEventHeader.getFunctionalRole() != null) { + functionalRole = commonEventHeader.getFunctionalRole(); + } + + } + + return new ImmutablePair<>(domain, functionalRole); + + } + + /** + * Function that extracts alert message string from {@link TCAVESAlertEntity} + */ + public static final Function MAP_ALERT_ENTITY_TO_ALERT_STRING_FUNCTION = + new Function() { + @Override + public String apply(TCAVESAlertEntity alertEntity) { + return alertEntity == null ? null : alertEntity.getAlertMessage(); + } + }; + + /** + * Extracts alert message strings from {@link TCAVESAlertEntity} + * @param alertEntities collection of alert entities + * @return List of alert message strings + */ + public static List extractAlertFromAlertEntities(final Collection alertEntities) { + return Lists.transform(newArrayList(alertEntities), MAP_ALERT_ENTITY_TO_ALERT_STRING_FUNCTION); + } + + + /** + * Converts Runtime Arguments to {@link TCAPolicyPreferences} object + * + * @param runtimeContext CDAP Runtime Arguments + * + * @return TCA Policy Preferences + */ + public static TCAPolicy getValidatedTCAPolicyPreferences(final RuntimeContext runtimeContext) { + + final Map runtimeArguments = runtimeContext.getRuntimeArguments(); + final TreeMap sortedRuntimeArguments = new TreeMap<>(runtimeArguments); + + LOG.debug("Printing all Received Runtime Arguments:"); + for (Map.Entry runtimeArgsEntry : sortedRuntimeArguments.entrySet()) { + LOG.debug("{}:{}", runtimeArgsEntry.getKey(), runtimeArgsEntry.getValue()); + } + + // extract TCA Policy Domain from Runtime Arguments + final String policyDomain = sortedRuntimeArguments.get(AnalyticsConstants.TCA_POLICY_DOMAIN_PATH); + + // create new TCA Policy object + final TCAPolicyPreferences tcaPolicyPreferences = new TCAPolicyPreferences(); + tcaPolicyPreferences.setDomain(policyDomain); + + // filter out other non relevant fields which are not related to tca policy + final Map tcaPolicyMap = filterMapByKeyNamePrefix(sortedRuntimeArguments, + TCA_POLICY_METRICS_PER_FUNCTIONAL_ROLE_PATH); + + // determine functional Roles + final Map> functionalRolesMap = + extractSubTree(tcaPolicyMap, 2, 3, AnalyticsConstants.TCA_POLICY_DELIMITER); + + // create metrics per functional role list + tcaPolicyPreferences.setMetricsPerFunctionalRole( + createTCAPolicyMetricsPerFunctionalRoleList(functionalRolesMap)); + + // validate tca Policy Preferences + validateSettings(tcaPolicyPreferences, new TCAPolicyPreferencesValidator()); + + LOG.info("Printing Effective TCA Policy: {}", tcaPolicyPreferences); + + return tcaPolicyPreferences; + } + + /** + * Creates {@link TCAPolicy} Metrics per Functional Role list + * + * @param functionalRolesMap Map containing functional Roles as key and corresponding values + * + * @return List of {@link MetricsPerFunctionalRole} + */ + public static List createTCAPolicyMetricsPerFunctionalRoleList( + final Map> functionalRolesMap) { + + // create a new metrics per functional role list + final List metricsPerFunctionalRoles = new LinkedList<>(); + + for (Map.Entry> functionalRolesEntry : functionalRolesMap.entrySet()) { + + // create new metrics per functional role instance + final MetricsPerFunctionalRole newMetricsPerFunctionalRole = + createNewMetricsPerFunctionalRole(functionalRolesEntry); + metricsPerFunctionalRoles.add(newMetricsPerFunctionalRole); + + // determine all threshold related values + final Map thresholdsValuesMaps = + filterMapByKeyNamePrefix(functionalRolesEntry.getValue(), + AnalyticsConstants.TCA_POLICY_THRESHOLDS_PATH_POSTFIX); + + // create a map of all threshold values + final Map> thresholdsMap = + extractSubTree(thresholdsValuesMaps, 1, 2, + AnalyticsConstants.TCA_POLICY_DELIMITER); + + // add thresholds to nmetrics per functional roles threshold list + for (Map thresholdMap : thresholdsMap.values()) { + newMetricsPerFunctionalRole.getThresholds().add(createNewThreshold(thresholdMap)); + } + + } + + return metricsPerFunctionalRoles; + } + + /** + * Creates new instance of TCA Policy {@link Threshold} with values extracted from thresholdMap + * + * @param thresholdMap threshold map with threshold values + * + * @return new instance of TCA Policy Threshold + */ + public static Threshold createNewThreshold(final Map thresholdMap) { + final Threshold threshold = new Threshold(); + threshold.setClosedLoopControlName(thresholdMap.get("policy.closedLoopControlName")); + threshold.setVersion(thresholdMap.get("policy.version")); + threshold.setFieldPath(thresholdMap.get("policy.fieldPath")); + threshold.setDirection(Direction.valueOf(thresholdMap.get("policy.direction"))); + threshold.setSeverity(EventSeverity.valueOf(thresholdMap.get("policy.severity"))); + threshold.setThresholdValue(Long.valueOf(thresholdMap.get("policy.thresholdValue"))); + return threshold; + } + + /** + * Create new {@link MetricsPerFunctionalRole} instance with policy Name, policy Version and policy Scope + * extracted from given functionalRolesEntry + * + * @param functionalRolesEntry Functional Role Entry + * + * @return new instance of MetricsPerFunctionalRole + */ + public static MetricsPerFunctionalRole createNewMetricsPerFunctionalRole( + final Map.Entry> functionalRolesEntry) { + // determine functional Role + final String functionalRole = functionalRolesEntry.getKey(); + // determine functional Role thresholds + final Map metricsPerFunctionalRoleThresholdsMap = functionalRolesEntry.getValue(); + final MetricsPerFunctionalRole metricsPerFunctionalRole = new MetricsPerFunctionalRole(); + final List thresholds = new LinkedList<>(); + metricsPerFunctionalRole.setThresholds(thresholds); + metricsPerFunctionalRole.setFunctionalRole(functionalRole); + // bind policyName, policyVersion and policyScope + metricsPerFunctionalRole.setPolicyName(metricsPerFunctionalRoleThresholdsMap.get("policyName")); + metricsPerFunctionalRole.setPolicyVersion(metricsPerFunctionalRoleThresholdsMap.get("policyVersion")); + metricsPerFunctionalRole.setPolicyScope(metricsPerFunctionalRoleThresholdsMap.get("policyScope")); + return metricsPerFunctionalRole; + } + + /** + * Converts a flattened key/value map which has keys delimited by a given delimiter. + * The start Index and end index extract the sub-key value and returns a new map containing + * sub-keys and values. + * + * @param actualMap actual Map + * @param startIndex start index + * @param endIndex end index + * @param delimiter delimiter + * + * @return Map with new sub tree map + */ + public static Map> extractSubTree( + final Map actualMap, int startIndex, int endIndex, String delimiter) { + + final SortedMap> subTreeMap = new TreeMap<>(); + + // iterate over actual map entries + for (Map.Entry actualMapEntry : actualMap.entrySet()) { + final String actualMapKey = actualMapEntry.getKey(); + final String actualMapValue = actualMapEntry.getValue(); + + // determine delimiter start and end index + final int keyStartIndex = StringUtils.ordinalIndexOf(actualMapKey, delimiter, startIndex); + final int keyEndIndex = StringUtils.ordinalIndexOf(actualMapKey, delimiter, endIndex); + final int keyLength = actualMapKey.length(); + + // extract sub-tree map + if (keyStartIndex != -1 && keyEndIndex != -1 && keyEndIndex > keyStartIndex && keyLength > keyEndIndex) { + final String thresholdKey = actualMapKey.substring(keyStartIndex + 1, keyEndIndex); + final Map existingThresholdMap = subTreeMap.get(thresholdKey); + final String subMapKey = actualMapKey.substring(keyEndIndex + 1, keyLength); + if (existingThresholdMap == null) { + Map newThresholdMap = new LinkedHashMap<>(); + newThresholdMap.put(subMapKey, actualMapValue); + subTreeMap.put(thresholdKey, newThresholdMap); + } else { + existingThresholdMap.put(subMapKey, actualMapValue); + } + + } + } + + return subTreeMap; + + } + + + /** + * Provides a view of underlying map that filters out entries with keys starting with give prefix + * + * @param actualMap Target map that needs to be filtered + * @param keyNamePrefix key prefix + * + * @return a view of actual map which only show entries which have give prefix + */ + public static Map filterMapByKeyNamePrefix(final Map actualMap, + final String keyNamePrefix) { + return Maps.filterKeys(actualMap, + new Predicate() { + @Override + public boolean apply(@Nullable String key) { + return key != null && key.startsWith(keyNamePrefix); + } + }); + } + + +} diff --git a/dcae-analytics-tca/src/main/java/org/openecomp/dcae/apod/analytics/tca/validator/TCAAppConfigValidator.java b/dcae-analytics-tca/src/main/java/org/openecomp/dcae/apod/analytics/tca/validator/TCAAppConfigValidator.java new file mode 100644 index 0000000..de6d38e --- /dev/null +++ b/dcae-analytics-tca/src/main/java/org/openecomp/dcae/apod/analytics/tca/validator/TCAAppConfigValidator.java @@ -0,0 +1,60 @@ +/* + * ============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.tca.validator; + +import org.openecomp.dcae.apod.analytics.common.cdap.validation.CDAPAppSettingsValidator; +import org.openecomp.dcae.apod.analytics.common.validation.GenericValidationResponse; +import org.openecomp.dcae.apod.analytics.tca.settings.TCAAppConfig; + +import static org.openecomp.dcae.apod.analytics.common.utils.ValidationUtils.isEmpty; + +/** + *

+ * TCA App Config Validator validates any TCA App Config parameter values + *

+ * + * @author Rajiv Singla. Creation Date: 10/24/2016. + */ +public class TCAAppConfigValidator implements CDAPAppSettingsValidator> { + + @Override + public GenericValidationResponse validateAppSettings(TCAAppConfig tcaAppConfig) { + + final GenericValidationResponse validationResponse = new GenericValidationResponse<>(); + + if (isEmpty(tcaAppConfig.getTcaSubscriberOutputStreamName())) { + validationResponse.addErrorMessage("tcaSubscriberOutputStreamName", + "tcaSubscriberOutputStreamName must be present"); + } + + if (isEmpty(tcaAppConfig.getTcaVESMessageStatusTableName())) { + validationResponse.addErrorMessage("tcaVESMessageStatusTableName", + "tcaVESMessageStatusTableName must be present"); + } + if (isEmpty(tcaAppConfig.getTcaVESAlertsTableName())) { + validationResponse.addErrorMessage("tcaVESAlertsTableName", + "tcaVESAlertsTableName must be present"); + } + + return validationResponse; + } +} diff --git a/dcae-analytics-tca/src/main/java/org/openecomp/dcae/apod/analytics/tca/validator/TCAPolicyPreferencesValidator.java b/dcae-analytics-tca/src/main/java/org/openecomp/dcae/apod/analytics/tca/validator/TCAPolicyPreferencesValidator.java new file mode 100644 index 0000000..5984bac --- /dev/null +++ b/dcae-analytics-tca/src/main/java/org/openecomp/dcae/apod/analytics/tca/validator/TCAPolicyPreferencesValidator.java @@ -0,0 +1,94 @@ +/* + * ============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.tca.validator; + +import org.openecomp.dcae.apod.analytics.common.cdap.validation.CDAPAppSettingsValidator; +import org.openecomp.dcae.apod.analytics.common.validation.GenericValidationResponse; +import org.openecomp.dcae.apod.analytics.model.domain.cef.EventSeverity; +import org.openecomp.dcae.apod.analytics.model.domain.policy.tca.Direction; +import org.openecomp.dcae.apod.analytics.model.domain.policy.tca.MetricsPerFunctionalRole; +import org.openecomp.dcae.apod.analytics.model.domain.policy.tca.Threshold; +import org.openecomp.dcae.apod.analytics.tca.settings.TCAPolicyPreferences; +import org.openecomp.dcae.apod.analytics.tca.utils.TCAUtils; + +import java.util.List; + +import static org.openecomp.dcae.apod.analytics.common.utils.ValidationUtils.isEmpty; + +/** + * Validates TCA Policy Preferences + *

+ * @author Rajiv Singla. Creation Date: 11/29/2016. + */ +public class TCAPolicyPreferencesValidator implements CDAPAppSettingsValidator> { + + @Override + public GenericValidationResponse validateAppSettings( + final TCAPolicyPreferences tcaPolicyPreferences) { + + final GenericValidationResponse validationResponse = new GenericValidationResponse<>(); + + // validate TCA Policy must domain present + final String domain = tcaPolicyPreferences.getDomain(); + if (isEmpty(domain)) { + validationResponse.addErrorMessage("domain", "TCA Policy must have only one domain present"); + } + + // validate TCA Policy must have at least one functional role + final List policyFunctionalRoles = TCAUtils.getPolicyFunctionalRoles(tcaPolicyPreferences); + if (policyFunctionalRoles.size() <= 0) { + validationResponse.addErrorMessage("metricsPerFunctionalRoles", + "TCA Policy must have at least one or more functional roles"); + } + + final List metricsPerFunctionalRoles = + tcaPolicyPreferences.getMetricsPerFunctionalRole(); + + // validate each Functional Role must have at least one threshold + for (MetricsPerFunctionalRole metricsPerFunctionalRole : metricsPerFunctionalRoles) { + if (metricsPerFunctionalRole.getThresholds().size() <= 0) { + validationResponse.addErrorMessage("thresholds", + "TCA Policy Functional Role must have at least one threshold. " + + "Functional Role causing this validation error:" + metricsPerFunctionalRole); + } + } + + // validate each threshold must have non null - fieldPath, thresholdValue, direction and severity + for (MetricsPerFunctionalRole metricsPerFunctionalRole : metricsPerFunctionalRoles) { + final List functionalRoleThresholds = metricsPerFunctionalRole.getThresholds(); + for (Threshold functionalRoleThreshold : functionalRoleThresholds) { + final String fieldPath = functionalRoleThreshold.getFieldPath(); + final Long thresholdValue = functionalRoleThreshold.getThresholdValue(); + final Direction direction = functionalRoleThreshold.getDirection(); + final EventSeverity severity = functionalRoleThreshold.getSeverity(); + if (isEmpty(fieldPath) || thresholdValue == null || direction == null || severity == null) { + validationResponse.addErrorMessage("threshold", + "TCA Policy threshold must have fieldPath, thresholdValue, direction and severity present." + + "Threshold causing this validation error:" + functionalRoleThreshold); + } + } + } + + + return validationResponse; + } +} diff --git a/dcae-analytics-tca/src/main/java/org/openecomp/dcae/apod/analytics/tca/validator/TCAPreferencesValidator.java b/dcae-analytics-tca/src/main/java/org/openecomp/dcae/apod/analytics/tca/validator/TCAPreferencesValidator.java new file mode 100644 index 0000000..78da6f9 --- /dev/null +++ b/dcae-analytics-tca/src/main/java/org/openecomp/dcae/apod/analytics/tca/validator/TCAPreferencesValidator.java @@ -0,0 +1,63 @@ +/* + * ============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.tca.validator; + +import org.openecomp.dcae.apod.analytics.common.cdap.validation.CDAPAppSettingsValidator; +import org.openecomp.dcae.apod.analytics.common.validation.GenericValidationResponse; +import org.openecomp.dcae.apod.analytics.tca.settings.TCAAppPreferences; + +import static org.openecomp.dcae.apod.analytics.common.utils.ValidationUtils.isEmpty; + +/** + * + * @author Rajiv Singla. Creation Date: 11/3/2016. + */ +public class TCAPreferencesValidator implements CDAPAppSettingsValidator> { + + @Override + public GenericValidationResponse validateAppSettings(TCAAppPreferences appPreferences) { + + final GenericValidationResponse validationResponse = new GenericValidationResponse<>(); + + // subscriber validations + final String subscriberHostName = appPreferences.getSubscriberHostName(); + if (isEmpty(subscriberHostName)) { + validationResponse.addErrorMessage("subscriberHostName", "Subscriber host name must be present"); + } + final String subscriberTopicName = appPreferences.getSubscriberTopicName(); + if (isEmpty(subscriberTopicName)) { + validationResponse.addErrorMessage("subscriberTopicName", "Subscriber topic name must be present"); + } + + // publisher validations + final String publisherHostName = appPreferences.getPublisherHostName(); + if (isEmpty(publisherHostName)) { + validationResponse.addErrorMessage("publisherHostName", "Publisher host name must be present"); + } + final String publisherTopicName = appPreferences.getPublisherTopicName(); + if (isEmpty(publisherTopicName)) { + validationResponse.addErrorMessage("publisherTopicName", "Publisher topic name must be present"); + } + + return validationResponse; + } +} diff --git a/dcae-analytics-tca/src/main/java/org/openecomp/dcae/apod/analytics/tca/worker/TCADMaaPMRPublisherJob.java b/dcae-analytics-tca/src/main/java/org/openecomp/dcae/apod/analytics/tca/worker/TCADMaaPMRPublisherJob.java new file mode 100644 index 0000000..cba3d86 --- /dev/null +++ b/dcae-analytics-tca/src/main/java/org/openecomp/dcae/apod/analytics/tca/worker/TCADMaaPMRPublisherJob.java @@ -0,0 +1,189 @@ +/* + * ============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.tca.worker; + +import co.cask.cdap.api.TxRunnable; +import co.cask.cdap.api.common.Bytes; +import co.cask.cdap.api.data.DatasetContext; +import co.cask.cdap.api.dataset.lib.CloseableIterator; +import co.cask.cdap.api.dataset.lib.KeyValue; +import co.cask.cdap.api.dataset.lib.ObjectMappedTable; +import co.cask.cdap.api.metrics.Metrics; +import co.cask.cdap.api.worker.WorkerContext; +import com.google.common.base.Joiner; +import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; +import org.openecomp.dcae.apod.analytics.common.CDAPMetricsConstants; +import org.openecomp.dcae.apod.analytics.common.exception.DCAEAnalyticsRuntimeException; +import org.openecomp.dcae.apod.analytics.common.utils.HTTPUtils; +import org.openecomp.dcae.apod.analytics.dmaap.domain.response.DMaaPMRPublisherResponse; +import org.openecomp.dcae.apod.analytics.dmaap.service.publisher.DMaaPMRPublisher; +import org.openecomp.dcae.apod.analytics.tca.persistance.TCAVESAlertEntity; +import org.openecomp.dcae.apod.analytics.tca.persistance.TCAVESAlertsPersister; +import org.openecomp.dcae.apod.analytics.tca.utils.TCAUtils; +import org.quartz.DisallowConcurrentExecution; +import org.quartz.Job; +import org.quartz.JobDataMap; +import org.quartz.JobExecutionContext; +import org.quartz.JobExecutionException; +import org.quartz.PersistJobDataAfterExecution; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.Date; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import static org.openecomp.dcae.apod.analytics.common.AnalyticsConstants.CDAP_ALERTS_TABLE_VARIABLE_NAME; +import static org.openecomp.dcae.apod.analytics.common.AnalyticsConstants.DMAAP_METRICS_VARIABLE_NAME; +import static org.openecomp.dcae.apod.analytics.common.AnalyticsConstants.DMAAP_PUBLISHER_VARIABLE_NAME; +import static org.openecomp.dcae.apod.analytics.common.AnalyticsConstants.WORKER_CONTEXT_VARIABLE_NAME; + +/** + * Quartz Job that will monitor any new alert messages in given TCA Alerts table and if any found publish them to + * DMaaP MR topic + *

+ * @author Rajiv Singla. Creation Date: 11/17/2016. + */ +@DisallowConcurrentExecution +@PersistJobDataAfterExecution +@SuppressFBWarnings("SIC_INNER_SHOULD_BE_STATIC_ANON") +public class TCADMaaPMRPublisherJob implements Job { + + private static final Logger LOG = LoggerFactory.getLogger(TCADMaaPMRPublisherJob.class); + + @Override + public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException { + + LOG.debug("Starting DMaaP MR Topic Publisher fetch Job. Next firing time will be: {}", + jobExecutionContext.getNextFireTime()); + + // Get Job Data Map + final JobDataMap jobDataMap = jobExecutionContext.getMergedJobDataMap(); + + // Fetch all Job Params from Job Data Map + final String cdapAlertsTableName = jobDataMap.getString(CDAP_ALERTS_TABLE_VARIABLE_NAME); + final WorkerContext workerContext = (WorkerContext) jobDataMap.get(WORKER_CONTEXT_VARIABLE_NAME); + final DMaaPMRPublisher publisher = (DMaaPMRPublisher) jobDataMap.get(DMAAP_PUBLISHER_VARIABLE_NAME); + final Metrics metrics = (Metrics) jobDataMap.get(DMAAP_METRICS_VARIABLE_NAME); + + LOG.debug("Start looking for new message in Alerts Table: {}", cdapAlertsTableName); + + // Get new alerts from alerts table + final Map newAlertsMap = getNewAlertsMap(cdapAlertsTableName, workerContext); + + // If no new alerts are found - nothing to publish + if (newAlertsMap.isEmpty()) { + LOG.debug("No new alerts found in Alerts Table name: {}. Nothing to Publisher....", cdapAlertsTableName); + metrics.count(CDAPMetricsConstants.TCA_PUBLISHER_NO_NEW_ALERTS_LOOKUP_METRIC, 1); + return; + } + + final int newAlertsCount = newAlertsMap.size(); + LOG.debug("Found new alerts in Alerts Table name: {}. No of new alerts: {}", cdapAlertsTableName, + newAlertsCount); + metrics.count(CDAPMetricsConstants.TCA_PUBLISHER_NEW_ALERTS_METRIC, newAlertsCount); + + // Get alert message strings from alert Entities + final List newAlertsMessages = TCAUtils.extractAlertFromAlertEntities(newAlertsMap.values()); + + // Publish messages to DMaaP MR Topic + + try { + + final DMaaPMRPublisherResponse publisherResponse = publisher.publish(newAlertsMessages); + + final Integer responseCode = publisherResponse.getResponseCode(); + final String responseMessage = publisherResponse.getResponseMessage(); + final int pendingMessagesCount = publisherResponse.getPendingMessagesCount(); + + LOG.debug("Publisher Response Code: {}, Publisher message: {}, Pending Messages Count: {}", responseCode, + responseMessage, pendingMessagesCount); + + if (HTTPUtils.isSuccessfulResponseCode(responseCode)) { + LOG.debug("Successfully Published alerts to DMaaP MR Topic."); + metrics.count(CDAPMetricsConstants.TCA_PUBLISHER_SUCCESSFUL_DMAAP_RESPONSE_METRIC, 1); + } else { + LOG.warn("Unable to publish alerts to DMaaP MR Topic. Publisher will try to send it later...."); + metrics.count(CDAPMetricsConstants.TCA_PUBLISHER_UNSUCCESSFUL_DMAAP_RESPONSE_METRIC, 1); + } + + } catch (DCAEAnalyticsRuntimeException e) { + LOG.error("Exception while publishing messages to DMaaP MR Topic: {}", e); + } finally { + // delete send message from alerts table + deleteAlertsByKey(cdapAlertsTableName, workerContext, newAlertsMap.keySet(), metrics); + } + + LOG.debug("Finished DMaaP MR Topic Publisher fetch Job."); + + } + + /** + * Gets New Messages from alerts table as Map with row keys as keys and {@link TCAVESAlertEntity} as values + * + * @param cdapAlertsTableName alerts table name + * @param workerContext worker context + * @return Map with row keys as keys and {@link TCAVESAlertEntity} as values + */ + private Map getNewAlertsMap(final String cdapAlertsTableName, + final WorkerContext workerContext) { + final Map newAlertsMap = new LinkedHashMap<>(); + workerContext.execute(new TxRunnable() { + @Override + public void run(DatasetContext context) throws Exception { + final ObjectMappedTable alertsTable = context.getDataset(cdapAlertsTableName); + final Date currentTime = new Date(); + final String rowKey = TCAVESAlertsPersister.createRowKey(currentTime); + final CloseableIterator> scan = alertsTable.scan(null, rowKey); + while (scan.hasNext()) { + final KeyValue alertEntityKeyValue = scan.next(); + newAlertsMap.put(Bytes.toString(alertEntityKeyValue.getKey()), alertEntityKeyValue.getValue()); + } + } + }); + return newAlertsMap; + } + + /** + * Deletes rows in Alerts table for give rowKeys + * + * @param cdapAlertsTableName CDAP Alerts Table Name + * @param workerContext Worker Context + * @param rowKeys Row Key Set + * @param metrics CDAP metrics + */ + private void deleteAlertsByKey(final String cdapAlertsTableName, final WorkerContext workerContext, + final Set rowKeys, final Metrics metrics) { + LOG.debug("Deleting Published Alerts from alerts table with rowKeys: {}", Joiner.on(",").join(rowKeys)); + workerContext.execute(new TxRunnable() { + @Override + public void run(DatasetContext context) throws Exception { + final ObjectMappedTable alertsTable = context.getDataset(cdapAlertsTableName); + for (String rowKey : rowKeys) { + alertsTable.delete(rowKey); + metrics.count(CDAPMetricsConstants.TCA_PUBLISHER_DELETED_ALERTS_METRIC, 1); + } + } + }); + } +} diff --git a/dcae-analytics-tca/src/main/java/org/openecomp/dcae/apod/analytics/tca/worker/TCADMaaPMRSubscriberJob.java b/dcae-analytics-tca/src/main/java/org/openecomp/dcae/apod/analytics/tca/worker/TCADMaaPMRSubscriberJob.java new file mode 100644 index 0000000..d5c9c41 --- /dev/null +++ b/dcae-analytics-tca/src/main/java/org/openecomp/dcae/apod/analytics/tca/worker/TCADMaaPMRSubscriberJob.java @@ -0,0 +1,175 @@ +/* + * ============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.tca.worker; + +import co.cask.cdap.api.metrics.Metrics; +import co.cask.cdap.api.worker.WorkerContext; +import com.google.common.base.Optional; +import com.google.common.base.Stopwatch; +import org.openecomp.dcae.apod.analytics.common.AnalyticsConstants; +import org.openecomp.dcae.apod.analytics.common.CDAPMetricsConstants; +import org.openecomp.dcae.apod.analytics.common.exception.DCAEAnalyticsRuntimeException; +import org.openecomp.dcae.apod.analytics.common.utils.HTTPUtils; +import org.openecomp.dcae.apod.analytics.dmaap.domain.response.DMaaPMRSubscriberResponse; +import org.openecomp.dcae.apod.analytics.dmaap.service.subscriber.DMaaPMRSubscriber; +import org.quartz.DisallowConcurrentExecution; +import org.quartz.Job; +import org.quartz.JobDataMap; +import org.quartz.JobExecutionContext; +import org.quartz.JobExecutionException; +import org.quartz.PersistJobDataAfterExecution; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.IOException; +import java.util.List; + +import static java.lang.String.format; + +/** + * Quartz Job which polls DMaaP MR VES Collector Topic for messages and writes them to + * a given CDAP Stream + * + * @author Rajiv Singla. Creation Date: 10/24/2016. + */ +@DisallowConcurrentExecution +@PersistJobDataAfterExecution +public class TCADMaaPMRSubscriberJob implements Job { + + private static final Logger LOG = LoggerFactory.getLogger(TCADMaaPMRSubscriberJob.class); + + @Override + public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException { + + LOG.debug("Starting DMaaP MR Topic Subscriber fetch Job. Next firing time will be: {}", + jobExecutionContext.getNextFireTime()); + + // Get Job Data Map + final JobDataMap jobDataMap = jobExecutionContext.getMergedJobDataMap(); + + // Fetch all Job Params from Job Data Map + final String cdapStreamName = jobDataMap.getString(AnalyticsConstants.CDAP_STREAM_VARIABLE_NAME); + final WorkerContext workerContext = + (WorkerContext) jobDataMap.get(AnalyticsConstants.WORKER_CONTEXT_VARIABLE_NAME); + final DMaaPMRSubscriber subscriber = + (DMaaPMRSubscriber) jobDataMap.get(AnalyticsConstants.DMAAP_SUBSCRIBER_VARIABLE_NAME); + final Metrics metrics = (Metrics) jobDataMap.get(AnalyticsConstants.DMAAP_METRICS_VARIABLE_NAME); + + final Optional subscriberResponseOptional = + getSubscriberResponse(subscriber, metrics); + + // If response is not present, unable to proceed + if (!subscriberResponseOptional.isPresent()) { + return; + } + + final DMaaPMRSubscriberResponse subscriberResponse = subscriberResponseOptional.get(); + + // If response code return by the subscriber call is not successful, unable to do proceed + if (!HTTPUtils.isSuccessfulResponseCode(subscriberResponse.getResponseCode())) { + LOG.error("Subscriber was unable to fetch messages properly. Subscriber Response Code: {} " + + "Unable to proceed further....", subscriberResponse.getResponseCode()); + metrics.count(CDAPMetricsConstants.TCA_SUBSCRIBER_UNSUCCESSFUL_RESPONSES_METRIC, 1); + return; + } + + LOG.debug("Subscriber HTTP Response Status Code match successful: {}", subscriberResponse, + HTTPUtils.HTTP_SUCCESS_STATUS_CODE); + + final List actualMessages = subscriberResponse.getFetchedMessages(); + + // If there are no message returned during from Subscriber, nothing to write to CDAP Stream + if (actualMessages.isEmpty()) { + LOG.debug("Subscriber Response has no messages. Nothing to write to CDAP stream...."); + metrics.count(CDAPMetricsConstants.TCA_SUBSCRIBER_RESPONSES_WITH_NO_MESSAGES_METRIC, 1); + return; + } + + LOG.debug("DMaaP MR Subscriber found new messages in DMaaP Topic. Message count: {}", actualMessages.size()); + metrics.count(CDAPMetricsConstants.TCA_SUBSCRIBER_TOTAL_MESSAGES_PROCESSED_METRIC, actualMessages.size()); + + // Write message to CDAP Stream using Stream Batch Writer + LOG.debug("Writing message to CDAP Stream: {}, Message Count: {}", cdapStreamName, actualMessages.size()); + try { + + for (String message : actualMessages) { + workerContext.write(cdapStreamName, message); + } + + } catch (IOException e) { + metrics.count(CDAPMetricsConstants.TCA_SUBSCRIBER_FAILURE_TO_WRITE_TO_STREAM_METRIC, 1); + final String errorMessage = + format("Error while DMaaP message router subscriber attempting to write to CDAP Stream: %s, " + + "Exception: %s", cdapStreamName, e); + throw new DCAEAnalyticsRuntimeException(errorMessage, LOG, e); + } + + LOG.debug("DMaaP MR Subscriber successfully finished writing messages to CDAP Stream: {}, Message count: {}", + cdapStreamName, actualMessages.size()); + } + + + /** + * Get Subscriber response and records time taken to fetch messages. Returns Optional.None if Subscriber response + * is null or response status code is not present + * + * @param subscriber - DMaaP Subscriber + * @param metrics - CDAP Metrics collector + * + * @return - Optional of Subscriber Response + */ + private static Optional getSubscriberResponse(final DMaaPMRSubscriber subscriber, + final Metrics metrics) { + + // Check how long it took for subscriber to respond + final Stopwatch stopwatch = new Stopwatch(); + stopwatch.start(); + + DMaaPMRSubscriberResponse subscriberResponse = null; + // Fetch messages from DMaaP MR Topic + try { + subscriberResponse = subscriber.fetchMessages(); + } catch (DCAEAnalyticsRuntimeException e) { + LOG.error("Error while fetching messages for DMaaP MR Topic: {}", e); + } + + stopwatch.stop(); + final long subscriberResponseTimeMS = stopwatch.elapsedMillis(); + + // If response is null is null or response code is null, unable to proceed nothing to do + if (subscriberResponse == null || subscriberResponse.getResponseCode() == null) { + LOG.error("Subscriber Response is null or subscriber Response code is null. Unable to proceed further..."); + return Optional.absent(); + } + + LOG.debug("Subscriber Response:{}, Subscriber HTTP Response Status Code {}, Subscriber Response Time(ms): {}", + subscriberResponse, subscriberResponse.getResponseCode(), subscriberResponseTimeMS); + + // Record subscriber response time + metrics.gauge(CDAPMetricsConstants.TCA_SUBSCRIBER_RESPONSE_TIME_MS_METRIC, subscriberResponseTimeMS); + + // Record all response count from subscriber + metrics.count(CDAPMetricsConstants.TCA_SUBSCRIBER_ALL_RESPONSES_COUNT_METRIC, 1); + + return Optional.of(subscriberResponse); + } + +} diff --git a/dcae-analytics-tca/src/main/java/org/openecomp/dcae/apod/analytics/tca/worker/TCADMaaPMockSubscriberWorker.java b/dcae-analytics-tca/src/main/java/org/openecomp/dcae/apod/analytics/tca/worker/TCADMaaPMockSubscriberWorker.java new file mode 100644 index 0000000..0e3da5e --- /dev/null +++ b/dcae-analytics-tca/src/main/java/org/openecomp/dcae/apod/analytics/tca/worker/TCADMaaPMockSubscriberWorker.java @@ -0,0 +1,139 @@ +/* + * ============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.tca.worker; + +import co.cask.cdap.api.annotation.Property; +import co.cask.cdap.api.worker.AbstractWorker; +import co.cask.cdap.api.worker.WorkerContext; +import com.fasterxml.jackson.core.type.TypeReference; +import org.openecomp.dcae.apod.analytics.common.exception.DCAEAnalyticsRuntimeException; +import org.openecomp.dcae.apod.analytics.model.domain.cef.EventListener; +import org.openecomp.dcae.apod.analytics.tca.settings.TCAAppPreferences; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStream; +import java.util.List; + +import static org.openecomp.dcae.apod.analytics.tca.utils.TCAUtils.getValidatedTCAAppPreferences; +import static org.openecomp.dcae.apod.analytics.tca.utils.TCAUtils.readValue; +import static org.openecomp.dcae.apod.analytics.tca.utils.TCAUtils.writeValueAsString; + +/** + * CDAP Worker which mocks fetching VES Messages from DMaaP MR topic. + * The mock instead of making DMaaP MR calls will actually take messages + * from file and send them to stream at subscriber polling interval + * + * TODO: To be removed before going to production - only for testing purposes + * + * @author Rajiv Singla. Creation Date: 11/4/2016. + */ +public class TCADMaaPMockSubscriberWorker extends AbstractWorker { + + private static final Logger LOG = LoggerFactory.getLogger(TCADMaaPMockSubscriberWorker.class); + + // TODO: Remove this file before going to production - only for mocking purposes + private static final String MOCK_MESSAGE_FILE_LOCATION = "ves_mock_messages.json"; + private static final TypeReference> EVENT_LISTENER_TYPE_REFERENCE = + new TypeReference>() { + }; + + private TCAAppPreferences tcaAppPreferences; + private boolean stopSendingMessages; + @Property + private final String tcaSubscriberOutputStreamName; + + public TCADMaaPMockSubscriberWorker(final String tcaSubscriberOutputStreamName) { + this.tcaSubscriberOutputStreamName = tcaSubscriberOutputStreamName; + } + + @Override + public void configure() { + setName("MockTCASubscriberWorker"); + setDescription("Writes Mocked VES messages to CDAP Stream"); + LOG.info("Configuring Mock TCA MR DMaaP Subscriber worker with name: {}", "MockTCASubscriberWorker"); + } + + @Override + public void initialize(WorkerContext context) throws Exception { + super.initialize(context); + + final TCAAppPreferences tcaAppPreferences = getValidatedTCAAppPreferences(context); + LOG.info("Initializing Mock TCA MR DMaaP Subscriber worker with preferences: {}", tcaAppPreferences); + this.tcaAppPreferences = tcaAppPreferences; + this.stopSendingMessages = false; + } + + + @Override + public void run() { + final Integer subscriberPollingInterval = tcaAppPreferences.getSubscriberPollingInterval(); + LOG.debug("Mock TCA Subscriber Polling interval: {}", subscriberPollingInterval); + + final InputStream resourceAsStream = Thread.currentThread().getContextClassLoader().getResourceAsStream + (MOCK_MESSAGE_FILE_LOCATION); + + if (resourceAsStream == null) { + LOG.error("Unable to find file at location: {}", MOCK_MESSAGE_FILE_LOCATION); + throw new DCAEAnalyticsRuntimeException("Unable to find file", LOG, new FileNotFoundException()); + } + + + try { + List eventListeners = readValue(resourceAsStream, EVENT_LISTENER_TYPE_REFERENCE); + + final int totalMessageCount = eventListeners.size(); + LOG.debug("Mock message count to be written to cdap stream: ()", totalMessageCount); + + int i = 1; + for (EventListener eventListener : eventListeners) { + if (stopSendingMessages) { + LOG.debug("Stop sending messages......"); + break; + } + final String eventListenerString = writeValueAsString(eventListener); + LOG.debug("=======>> Writing message to cdap stream no: {} of {}", i, totalMessageCount); + getContext().write(tcaSubscriberOutputStreamName, eventListenerString); + i++; + try { + Thread.sleep(subscriberPollingInterval); + } catch (InterruptedException e) { + LOG.error("Error while sleeping"); + } + } + + LOG.debug("Finished writing mock messages to CDAP Stream"); + + } catch (IOException e) { + LOG.error("Error while parsing json file"); + throw new DCAEAnalyticsRuntimeException("Error while parsing mock json file", LOG, e); + } + + + } + + @Override + public void stop() { + stopSendingMessages = true; + } +} diff --git a/dcae-analytics-tca/src/main/java/org/openecomp/dcae/apod/analytics/tca/worker/TCADMaaPPublisherWorker.java b/dcae-analytics-tca/src/main/java/org/openecomp/dcae/apod/analytics/tca/worker/TCADMaaPPublisherWorker.java new file mode 100644 index 0000000..0286d43 --- /dev/null +++ b/dcae-analytics-tca/src/main/java/org/openecomp/dcae/apod/analytics/tca/worker/TCADMaaPPublisherWorker.java @@ -0,0 +1,230 @@ +/* + * ============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.tca.worker; + +import co.cask.cdap.api.annotation.Property; +import co.cask.cdap.api.metrics.Metrics; +import co.cask.cdap.api.worker.AbstractWorker; +import co.cask.cdap.api.worker.WorkerContext; +import org.openecomp.dcae.apod.analytics.common.AnalyticsConstants; +import org.openecomp.dcae.apod.analytics.common.CDAPComponentsConstants; +import org.openecomp.dcae.apod.analytics.common.exception.DCAEAnalyticsRuntimeException; +import org.openecomp.dcae.apod.analytics.dmaap.DMaaPMRFactory; +import org.openecomp.dcae.apod.analytics.dmaap.domain.config.DMaaPMRPublisherConfig; +import org.openecomp.dcae.apod.analytics.dmaap.service.publisher.DMaaPMRPublisher; +import org.openecomp.dcae.apod.analytics.model.util.AnalyticsModelIOUtils; +import org.openecomp.dcae.apod.analytics.tca.settings.TCAAppPreferences; +import org.openecomp.dcae.apod.analytics.tca.utils.AppPreferencesToPublisherConfigMapper; +import org.openecomp.dcae.apod.analytics.tca.utils.TCAUtils; +import org.quartz.JobBuilder; +import org.quartz.JobDataMap; +import org.quartz.JobDetail; +import org.quartz.Scheduler; +import org.quartz.SchedulerException; +import org.quartz.SimpleScheduleBuilder; +import org.quartz.SimpleTrigger; +import org.quartz.TriggerBuilder; +import org.quartz.impl.StdSchedulerFactory; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.Properties; +import java.util.concurrent.atomic.AtomicBoolean; + +import static java.lang.String.format; + +/** + * TCA DMaaP Publisher will monitor alerts table at regular intervals and publish any alerts to DMaaP MR Publishing + * Topic + *

+ * @author Rajiv Singla. Creation Date: 11/16/2016. + */ +public class TCADMaaPPublisherWorker extends AbstractWorker { + + private static final Logger LOG = LoggerFactory.getLogger(TCADMaaPPublisherWorker.class); + + /** + * DMaaP MR Publisher + */ + private DMaaPMRPublisher publisher; + /** + * Quartz Scheduler + */ + private Scheduler scheduler; + /** + * Determines if scheduler is shutdown + */ + private AtomicBoolean isSchedulerShutdown; + /** + * Store runtime metrics + */ + private Metrics metrics; + + @Property + private final String tcaVESAlertsTableName; + + public TCADMaaPPublisherWorker(final String tcaVESAlertsTableName) { + this.tcaVESAlertsTableName = tcaVESAlertsTableName; + } + + @Override + public void configure() { + // configure + setName(CDAPComponentsConstants.TCA_FIXED_DMAAP_PUBLISHER_WORKER); + setDescription(CDAPComponentsConstants.TCA_FIXED_DMAAP_PUBLISHER_DESCRIPTION_WORKER); + LOG.debug("Configuring TCA MR DMaaP Publisher worker with name: {}", + CDAPComponentsConstants.TCA_FIXED_DMAAP_PUBLISHER_WORKER); + } + + + @Override + public void initialize(WorkerContext context) throws Exception { + super.initialize(context); + + // Parse runtime arguments + final TCAAppPreferences tcaAppPreferences = TCAUtils.getValidatedTCAAppPreferences(context); + + LOG.info("Initializing TCA MR DMaaP Publisher worker with preferences: {}", tcaAppPreferences); + + // Map TCA App Preferences to DMaaP MR Publisher Config + final DMaaPMRPublisherConfig publisherConfig = AppPreferencesToPublisherConfigMapper.map(tcaAppPreferences); + + LOG.info("TCA DMaaP MR Publisher worker will be polling TCA Alerts Table Name: {}", tcaVESAlertsTableName); + + // Create an instance of DMaaP MR Publisher + LOG.debug("Creating an instance of DMaaP Publisher"); + publisher = DMaaPMRFactory.create().createPublisher(publisherConfig); + + // initialize a new Quartz scheduler + initializeScheduler(tcaAppPreferences); + // initialize scheduler state + isSchedulerShutdown = new AtomicBoolean(true); + } + + + @Override + public void run() { + // Start Publisher scheduler + try { + scheduler.start(); + isSchedulerShutdown.getAndSet(false); + + } catch (SchedulerException e) { + final String errorMessage = + format("Error while starting TCA DMaaP MR Publisher scheduler: %s", e.toString()); + throw new DCAEAnalyticsRuntimeException(errorMessage, LOG, e); + } + + LOG.info("TCA DMaaP MR Publisher Scheduler started successfully"); + + // indefinite loop which wakes up and confirms scheduler is indeed running + while (!isSchedulerShutdown.get()) { + try { + + Thread.sleep(AnalyticsConstants.TCA_DEFAULT_WORKER_SHUTDOWN_CHECK_INTERVAL_MS); + + } catch (InterruptedException e) { + + final String errorMessage = + format("Error while checking TCA DMaaP MR Publisher worker status: %s", e); + throw new DCAEAnalyticsRuntimeException(errorMessage, LOG, e); + } + } + + LOG.info("Finished execution of TCA DMaaP MR Publisher worker thread"); + + } + + @Override + public void stop() { + // Stop Publisher - which will flush any batch messages if present + try { + publisher.close(); + } catch (Exception e) { + + final String errorMessage = format("Error while shutting down DMaaP MR Publisher: %s", e); + throw new DCAEAnalyticsRuntimeException(errorMessage, LOG, e); + + } + // Stop Publisher scheduler + try { + + LOG.info("Shutting TCA DMaaP MR Publisher Scheduler"); + + scheduler.shutdown(); + isSchedulerShutdown.getAndSet(true); + + } catch (SchedulerException e) { + + final String errorMessage = + format("Error while shutting down TCA DMaaP MR Publisher scheduler: %s", e); + throw new DCAEAnalyticsRuntimeException(errorMessage, LOG, e); + } + } + + + /** + * Initializes a scheduler instance for DMaaP MR Publisher Job + * + * @throws SchedulerException SchedulerException + */ + private void initializeScheduler(TCAAppPreferences tcaAnalyticsAppConfig) throws SchedulerException { + + // Initialize a new Quartz Standard scheduler - settings settings are in quartz-publisher.properties file + final StdSchedulerFactory stdSchedulerFactory = new StdSchedulerFactory(); + final String quartzPublisherPropertiesFileName = AnalyticsConstants.TCA_QUARTZ_PUBLISHER_PROPERTIES_FILE_NAME; + LOG.debug("Configuring quartz scheduler for TCA DMaaP MR Publisher with properties file: {}", + quartzPublisherPropertiesFileName); + final Properties publisherProperties = + AnalyticsModelIOUtils.loadPropertiesFile(quartzPublisherPropertiesFileName); + stdSchedulerFactory.initialize(publisherProperties); + scheduler = stdSchedulerFactory.getScheduler(); + + // Create a new JobDataMap containing information required by TCA DMaaP Publisher Job + final JobDataMap jobDataMap = new JobDataMap(); + jobDataMap.put(AnalyticsConstants.CDAP_ALERTS_TABLE_VARIABLE_NAME, tcaVESAlertsTableName); + jobDataMap.put(AnalyticsConstants.WORKER_CONTEXT_VARIABLE_NAME, getContext()); + jobDataMap.put(AnalyticsConstants.DMAAP_PUBLISHER_VARIABLE_NAME, publisher); + jobDataMap.put(AnalyticsConstants.DMAAP_METRICS_VARIABLE_NAME, metrics); + + // Create a new job detail + final JobDetail jobDetail = JobBuilder.newJob(TCADMaaPMRPublisherJob.class) + .withIdentity(AnalyticsConstants.TCA_DMAAP_PUBLISHER_QUARTZ_JOB_NAME, + AnalyticsConstants.TCA_QUARTZ_GROUP_NAME) + .usingJobData(jobDataMap).build(); + + // Create a new scheduling builder + final Integer publisherPollingInterval = tcaAnalyticsAppConfig.getPublisherPollingInterval(); + final SimpleScheduleBuilder simpleScheduleBuilder = SimpleScheduleBuilder.simpleSchedule() + .withIntervalInMilliseconds(publisherPollingInterval) // job will use custom polling schedule + .repeatForever(); // repeats while worker is running + + // Create a trigger for the TCA Publisher Job + final SimpleTrigger simpleTrigger = TriggerBuilder.newTrigger() + .withIdentity(AnalyticsConstants.TCA_DMAAP_PUBLISHER_QUARTZ_TRIGGER_NAME, + AnalyticsConstants.TCA_QUARTZ_GROUP_NAME) + .startNow() // job starts right away + .withSchedule(simpleScheduleBuilder).build(); + + scheduler.scheduleJob(jobDetail, simpleTrigger); + LOG.info("Initialized TCA DMaaP MR Publisher Scheduler"); + } +} diff --git a/dcae-analytics-tca/src/main/java/org/openecomp/dcae/apod/analytics/tca/worker/TCADMaaPSubscriberWorker.java b/dcae-analytics-tca/src/main/java/org/openecomp/dcae/apod/analytics/tca/worker/TCADMaaPSubscriberWorker.java new file mode 100644 index 0000000..7b58dbc --- /dev/null +++ b/dcae-analytics-tca/src/main/java/org/openecomp/dcae/apod/analytics/tca/worker/TCADMaaPSubscriberWorker.java @@ -0,0 +1,226 @@ +/* + * ============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.tca.worker; + +import co.cask.cdap.api.annotation.Property; +import co.cask.cdap.api.metrics.Metrics; +import co.cask.cdap.api.worker.AbstractWorker; +import co.cask.cdap.api.worker.WorkerContext; +import org.openecomp.dcae.apod.analytics.common.AnalyticsConstants; +import org.openecomp.dcae.apod.analytics.common.CDAPComponentsConstants; +import org.openecomp.dcae.apod.analytics.common.exception.DCAEAnalyticsRuntimeException; +import org.openecomp.dcae.apod.analytics.dmaap.DMaaPMRFactory; +import org.openecomp.dcae.apod.analytics.dmaap.domain.config.DMaaPMRSubscriberConfig; +import org.openecomp.dcae.apod.analytics.dmaap.service.subscriber.DMaaPMRSubscriber; +import org.openecomp.dcae.apod.analytics.model.util.AnalyticsModelIOUtils; +import org.openecomp.dcae.apod.analytics.tca.settings.TCAAppPreferences; +import org.openecomp.dcae.apod.analytics.tca.utils.AppPreferencesToSubscriberConfigMapper; +import org.openecomp.dcae.apod.analytics.tca.utils.TCAUtils; +import org.quartz.JobBuilder; +import org.quartz.JobDataMap; +import org.quartz.JobDetail; +import org.quartz.Scheduler; +import org.quartz.SchedulerException; +import org.quartz.SimpleScheduleBuilder; +import org.quartz.SimpleTrigger; +import org.quartz.TriggerBuilder; +import org.quartz.impl.StdSchedulerFactory; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.Properties; +import java.util.concurrent.atomic.AtomicBoolean; + +import static java.lang.String.format; + +/** + * TCA DMaaP Subscriber will read messages and post them to cdap stream at regular intervals + *

+ * @author Rajiv Singla. Creation Date: 10/14/2016. + */ +public class TCADMaaPSubscriberWorker extends AbstractWorker { + + private static final Logger LOG = LoggerFactory.getLogger(TCADMaaPSubscriberWorker.class); + + /** + * DMaaP MR Subscriber + */ + private DMaaPMRSubscriber subscriber; + /** + * Quartz Scheduler + */ + private Scheduler scheduler; + /** + * Determines if scheduler is shutdown + */ + private AtomicBoolean isSchedulerShutdown; + /** + * Store runtime metrics + */ + private Metrics metrics; + + /** + * CDAP output stream name + */ + @Property + private final String tcaSubscriberOutputStreamName; + + public TCADMaaPSubscriberWorker(final String tcaSubscriberOutputStreamName) { + this.tcaSubscriberOutputStreamName = tcaSubscriberOutputStreamName; + } + + + @Override + public void configure() { + // configure + setName(CDAPComponentsConstants.TCA_FIXED_DMAAP_SUBSCRIBER_WORKER); + setDescription(CDAPComponentsConstants.TCA_FIXED_DMAAP_SUBSCRIBER_DESCRIPTION_WORKER); + LOG.debug("Configuring TCA MR DMaaP Subscriber worker with name: {}", + CDAPComponentsConstants.TCA_FIXED_DMAAP_SUBSCRIBER_WORKER); + } + + @Override + public void initialize(WorkerContext context) throws Exception { + super.initialize(context); + + // Parse runtime arguments + final TCAAppPreferences tcaAppPreferences = TCAUtils.getValidatedTCAAppPreferences(context); + + LOG.info("Initializing TCA MR DMaaP Subscriber worker with preferences: {}", tcaAppPreferences); + + // Map TCA App Preferences to DMaaP MR Subscriber Config + final DMaaPMRSubscriberConfig subscriberConfig = AppPreferencesToSubscriberConfigMapper.map(tcaAppPreferences); + + LOG.info("TCA DMaaP MR Subscriber worker will be writing to CDAP Stream: {}", tcaSubscriberOutputStreamName); + + // Create an instance of DMaaP MR Subscriber + LOG.debug("Creating an instance of DMaaP Subscriber"); + subscriber = DMaaPMRFactory.create().createSubscriber(subscriberConfig); + + // initialize a new Quartz scheduler + initializeScheduler(tcaAppPreferences); + + // initialize scheduler state + isSchedulerShutdown = new AtomicBoolean(true); + } + + + @Override + public void run() { + // Start scheduler + try { + scheduler.start(); + isSchedulerShutdown.getAndSet(false); + + } catch (SchedulerException e) { + final String errorMessage = + format("Error while starting TCA DMaaP MR Subscriber scheduler: %s", e.toString()); + throw new DCAEAnalyticsRuntimeException(errorMessage, LOG, e); + } + + LOG.info("TCA DMaaP MR Subscriber Scheduler started successfully"); + + // indefinite loop which wakes up and confirms scheduler is indeed running + while (!isSchedulerShutdown.get()) { + try { + + Thread.sleep(AnalyticsConstants.TCA_DEFAULT_WORKER_SHUTDOWN_CHECK_INTERVAL_MS); + + } catch (InterruptedException e) { + + final String errorMessage = + format("Error while checking TCA DMaaP MR Subscriber worker status: %s", e); + throw new DCAEAnalyticsRuntimeException(errorMessage, LOG, e); + } + } + + LOG.info("Finished execution of TCA DMaaP MR Subscriber worker thread"); + + } + + @Override + public void stop() { + + // Stop scheduler + try { + + LOG.info("Shutting TCA DMaaP MR Subscriber Scheduler"); + + scheduler.shutdown(); + isSchedulerShutdown.getAndSet(true); + + } catch (SchedulerException e) { + + final String errorMessage = + format("Error while shutting down TCA DMaaP MR Subscriber scheduler: %s", e); + throw new DCAEAnalyticsRuntimeException(errorMessage, LOG, e); + } + } + + /** + * Initializes a scheduler instance for DMaaP MR Subscriber Job + * + * @throws SchedulerException SchedulerException + */ + private void initializeScheduler(TCAAppPreferences tcaAppPreferences) throws SchedulerException { + + // Initialize a new Quartz Standard scheduler - settings settings are in quartz-subscriber.properties file + final StdSchedulerFactory stdSchedulerFactory = new StdSchedulerFactory(); + final String quartzSubscriberPropertiesFileName = AnalyticsConstants.TCA_QUARTZ_SUBSCRIBER_PROPERTIES_FILE_NAME; + LOG.debug("Configuring quartz scheduler for TCA DMaaP MR Subscriber with properties file: {}", + quartzSubscriberPropertiesFileName); + final Properties subscriberProperties = + AnalyticsModelIOUtils.loadPropertiesFile(quartzSubscriberPropertiesFileName); + stdSchedulerFactory.initialize(subscriberProperties); + scheduler = stdSchedulerFactory.getScheduler(); + + // Create a new JobDataMap containing information required by TCA DMaaP Subscriber Job + final JobDataMap jobDataMap = new JobDataMap(); + jobDataMap.put(AnalyticsConstants.CDAP_STREAM_VARIABLE_NAME, tcaSubscriberOutputStreamName); + jobDataMap.put(AnalyticsConstants.WORKER_CONTEXT_VARIABLE_NAME, getContext()); + jobDataMap.put(AnalyticsConstants.DMAAP_SUBSCRIBER_VARIABLE_NAME, subscriber); + jobDataMap.put(AnalyticsConstants.DMAAP_METRICS_VARIABLE_NAME, metrics); + + // Create a new job detail + final JobDetail jobDetail = JobBuilder.newJob(TCADMaaPMRSubscriberJob.class) + .withIdentity(AnalyticsConstants.TCA_DMAAP_SUBSCRIBER_QUARTZ_JOB_NAME, + AnalyticsConstants.TCA_QUARTZ_GROUP_NAME) + .usingJobData(jobDataMap).build(); + + // Create a new scheduling builder + final Integer subscriberPollingInterval = tcaAppPreferences.getSubscriberPollingInterval(); + final SimpleScheduleBuilder simpleScheduleBuilder = SimpleScheduleBuilder.simpleSchedule() + .withIntervalInMilliseconds(subscriberPollingInterval) // job will use custom polling schedule + .repeatForever(); // repeats while worker is running + + // Create a trigger for the TCA Subscriber Job + final SimpleTrigger simpleTrigger = TriggerBuilder.newTrigger() + .withIdentity(AnalyticsConstants.TCA_DMAAP_SUBSCRIBER_QUARTZ_TRIGGER_NAME, + AnalyticsConstants.TCA_QUARTZ_GROUP_NAME) + .startNow() // job starts right away + .withSchedule(simpleScheduleBuilder).build(); + + scheduler.scheduleJob(jobDetail, simpleTrigger); + LOG.info("Initialized TCA DMaaP MR Subscriber Scheduler"); + } + + +} diff --git a/dcae-analytics-tca/src/test/java/org/openecomp/dcae/analytics/tca/BaseAnalyticsTCAIT.java b/dcae-analytics-tca/src/test/java/org/openecomp/dcae/analytics/tca/BaseAnalyticsTCAIT.java deleted file mode 100644 index 4dedf98..0000000 --- a/dcae-analytics-tca/src/test/java/org/openecomp/dcae/analytics/tca/BaseAnalyticsTCAIT.java +++ /dev/null @@ -1,141 +0,0 @@ -/* - * ============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.analytics.tca; - -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.databind.SerializationFeature; -import com.google.common.base.Suppliers; -import org.junit.BeforeClass; -import org.openecomp.dcae.analytics.common.AnalyticsConstants; -import org.openecomp.dcae.analytics.model.util.AnalyticsModelIOUtils; -import org.openecomp.dcae.analytics.model.util.json.AnalyticsModelObjectMapperSupplier; -import org.openecomp.dcae.analytics.tca.settings.TCATestAppConfig; -import org.openecomp.dcae.analytics.tca.settings.TCATestAppPreferences; -import org.openecomp.dcae.analytics.test.BaseDCAEAnalyticsIT; - -import java.util.LinkedHashMap; -import java.util.Map; -import java.util.Properties; - -/** - * - * @author Rajiv Singla. Creation Date: 10/25/2016. - */ -public class BaseAnalyticsTCAIT extends BaseDCAEAnalyticsIT { - - protected static ObjectMapper objectMapper; - - @BeforeClass - public static void beforeClass() { - final AnalyticsModelObjectMapperSupplier analyticsModelObjectMapperSupplier = - new AnalyticsModelObjectMapperSupplier(); - objectMapper = Suppliers.memoize(analyticsModelObjectMapperSupplier).get(); - objectMapper.enable(SerializationFeature.INDENT_OUTPUT); - } - - protected static final String TCA_CONTROLLER_POLICY_FILE_LOCATION = - "data/properties/tca_controller_policy.properties"; - - // App Settings - protected static final String DCAE_ANALYTICS_TCA_TEST_APP_NAME = "dcae-tca"; - protected static final String DCAE_ANALYTICS_TCA_TEST_APP_DESC = - "DCAE Analytics Threshold Crossing Alert Application"; - - // Subscriber App Preferences - protected static final String SUBSCRIBER_HOST_NAME = "mrlocal-mtnjftle01.homer.com"; - protected static final Integer SUBSCRIBER_PORT_NUMBER = 3905; - protected static final String SUBSCRIBER_TOPIC_NAME = "com.dcae.dmaap.mtnje2.DcaeTestVESSub"; - protected static final String SUBSCRIBER_USERNAME = "m00502@tca.af.dcae.com"; - protected static final String SUBSCRIBER_PASSWORD = "Te5021abc"; - protected static final String SUBSCRIBER_HTTP_PROTOCOL = "https"; - protected static final String SUBSCRIBER_CONTENT_TYPE = "application/json"; - protected static final Integer SUBSCRIBER_POLLING_INTERVAL = 20000; - - protected static final String SUBSCRIBER_CONSUMER_ID = "c12"; - protected static final String SUBSCRIBER_CONSUMER_GROUP_NAME = AnalyticsConstants.DMAAP_GROUP_PREFIX + - SUBSCRIBER_CONSUMER_ID; - protected static final int SUBSCRIBER_TIMOUT_MS = -1; - protected static final int SUBSCRIBER_MESSAGE_LIMIT = -1; - - // Publisher App Preferences - protected static final String PUBLISHER_HOST_NAME = "mrlocal-mtnjftle01.homer.com"; - protected static final Integer PUBLISHER_PORT_NUMBER = 3905; - protected static final String PUBLISHER_TOPIC_NAME = "com.dcae.dmaap.mtnje2.DcaeTestVESPub"; - protected static final String PUBLISHER_USERNAME = "m00502@tca.af.dcae.com"; - protected static final String PUBLISHER_PASSWORD = "Te5021abc"; - protected static final String PUBLISHER_HTTP_PROTOCOL = "https"; - protected static final String PUBLISHER_CONTENT_TYPE = "application/json"; - protected static final Integer PUBLISHER_BATCH_QUEUE_SIZE = 10; - protected static final Integer PUBLISHER_RECOVERY_QUEUE_SIZE = 100000; - protected static final Integer PUBLISHER_POLLING_INTERVAL = 20000; - - protected static TCATestAppConfig getTCATestAppConfig() { - final TCATestAppConfig tcaTestAppConfig = new TCATestAppConfig(); - tcaTestAppConfig.setAppName(DCAE_ANALYTICS_TCA_TEST_APP_NAME); - tcaTestAppConfig.setAppDescription(DCAE_ANALYTICS_TCA_TEST_APP_DESC); - return tcaTestAppConfig; - } - - protected static TCATestAppPreferences getTCATestAppPreferences() { - final TCATestAppPreferences tcaTestAppPreferences = new TCATestAppPreferences(getTCAPolicyPreferences()); - tcaTestAppPreferences.setSubscriberHostName(SUBSCRIBER_HOST_NAME); - tcaTestAppPreferences.setSubscriberHostPortNumber(SUBSCRIBER_PORT_NUMBER); - tcaTestAppPreferences.setSubscriberTopicName(SUBSCRIBER_TOPIC_NAME); - tcaTestAppPreferences.setSubscriberUserName(SUBSCRIBER_USERNAME); - tcaTestAppPreferences.setSubscriberUserPassword(SUBSCRIBER_PASSWORD); - tcaTestAppPreferences.setSubscriberProtocol(SUBSCRIBER_HTTP_PROTOCOL); - tcaTestAppPreferences.setSubscriberContentType(SUBSCRIBER_CONTENT_TYPE); - tcaTestAppPreferences.setSubscriberConsumerId(SUBSCRIBER_CONSUMER_ID); - tcaTestAppPreferences.setSubscriberConsumerGroup(SUBSCRIBER_CONSUMER_GROUP_NAME); - tcaTestAppPreferences.setSubscriberTimeoutMS(SUBSCRIBER_TIMOUT_MS); - tcaTestAppPreferences.setSubscriberMessageLimit(SUBSCRIBER_MESSAGE_LIMIT); - tcaTestAppPreferences.setSubscriberPollingInterval(SUBSCRIBER_POLLING_INTERVAL); - - tcaTestAppPreferences.setPublisherHostName(PUBLISHER_HOST_NAME); - tcaTestAppPreferences.setPublisherHostPort(PUBLISHER_PORT_NUMBER); - tcaTestAppPreferences.setPublisherTopicName(PUBLISHER_TOPIC_NAME); - tcaTestAppPreferences.setPublisherUserName(PUBLISHER_USERNAME); - tcaTestAppPreferences.setPublisherUserPassword(PUBLISHER_PASSWORD); - tcaTestAppPreferences.setPublisherProtocol(PUBLISHER_HTTP_PROTOCOL); - tcaTestAppPreferences.setPublisherContentType(PUBLISHER_CONTENT_TYPE); - tcaTestAppPreferences.setPublisherMaxBatchSize(PUBLISHER_BATCH_QUEUE_SIZE); - tcaTestAppPreferences.setPublisherMaxRecoveryQueueSize(PUBLISHER_RECOVERY_QUEUE_SIZE); - tcaTestAppPreferences.setPublisherPollingInterval(PUBLISHER_POLLING_INTERVAL); - return tcaTestAppPreferences; - } - - - protected static Map getTCAPolicyPreferences() { - final Map policyPreferences = new LinkedHashMap<>(); - final Properties policyPreferencesProps = - AnalyticsModelIOUtils.loadPropertiesFile(TCA_CONTROLLER_POLICY_FILE_LOCATION); - for (Map.Entry propEntry : policyPreferencesProps.entrySet()) { - policyPreferences.put(propEntry.getKey().toString(), propEntry.getValue().toString()); - } - - return policyPreferences; - } - - protected static String serializeModelToJson(Object model) throws JsonProcessingException { - return objectMapper.writeValueAsString(model); - } -} diff --git a/dcae-analytics-tca/src/test/java/org/openecomp/dcae/analytics/tca/BaseAnalyticsTCAUnitTest.java b/dcae-analytics-tca/src/test/java/org/openecomp/dcae/analytics/tca/BaseAnalyticsTCAUnitTest.java deleted file mode 100644 index 38e4ce1..0000000 --- a/dcae-analytics-tca/src/test/java/org/openecomp/dcae/analytics/tca/BaseAnalyticsTCAUnitTest.java +++ /dev/null @@ -1,220 +0,0 @@ -/* - * ============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.analytics.tca; - -import com.fasterxml.jackson.core.type.TypeReference; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.google.common.base.Suppliers; -import org.junit.Assert; -import org.openecomp.dcae.analytics.model.domain.cef.EventListener; -import org.openecomp.dcae.analytics.model.domain.policy.tca.Direction; -import org.openecomp.dcae.analytics.model.domain.policy.tca.TCAPolicy; -import org.openecomp.dcae.analytics.model.domain.policy.tca.Threshold; -import org.openecomp.dcae.analytics.model.util.json.AnalyticsModelObjectMapperSupplier; -import org.openecomp.dcae.analytics.tca.settings.TCATestAppPreferences; -import org.openecomp.dcae.analytics.test.BaseDCAEAnalyticsUnitTest; - -import java.io.IOException; -import java.io.InputStream; -import java.util.Arrays; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -/** - * @author Rajiv Singla. Creation Date: 10/25/2016. - */ -public class BaseAnalyticsTCAUnitTest extends BaseDCAEAnalyticsUnitTest { - - /** - * Object mapper to be used for all TCA Json Parsing - */ - protected static final ObjectMapper ANALYTICS_MODEL_OBJECT_MAPPER = - Suppliers.memoize(new AnalyticsModelObjectMapperSupplier()).get(); - - protected static final String TCA_POLICY_JSON_FILE_LOCATION = "data/json/policy/tca_policy.json"; - protected static final String CEF_MESSAGES_JSON_FILE_LOCATION = "data/json/cef/cef_messages.json"; - protected static final String CEF_MESSAGE_JSON_FILE_LOCATION = "data/json/cef/cef_message.json"; - protected static final String CEF_MESSAGE_WITH_THRESHOLD_VIOLATION_JSON_FILE_LOCATION = - "data/json/cef/cef_message_with_threshold_violation.json"; - - protected static final String TCA_CONTROLLER_POLICY_FILE_LOCATION = - "data/properties/tca_controller_policy.properties"; - - - /** - * Provides TCA Policy that can be used for testing - * - * @return test TCA Policy Object - */ - protected TCAPolicy getSampleTCAPolicy() { - return deserializeJsonFileToModel(TCA_POLICY_JSON_FILE_LOCATION, TCAPolicy.class); - } - - /** - * Provides list containing 350 CEF messages - * - * @return CEF Test Message - * @throws Exception Exception - */ - protected List getCEFMessages() throws Exception { - final String cefMessageAsString = fromStream(CEF_MESSAGES_JSON_FILE_LOCATION); - final TypeReference> eventListenerListTypeReference = - new TypeReference>() { - }; - return ANALYTICS_MODEL_OBJECT_MAPPER.readValue(cefMessageAsString, eventListenerListTypeReference); - } - - /** - * Provides 1 valid CEF messages which does not violate Threshold as String - * - * @return CEF Test Message String - * @throws Exception Exception - */ - protected String getValidCEFMessage() throws Exception { - return fromStream(CEF_MESSAGE_JSON_FILE_LOCATION); - } - - - /** - * Provides single CEF Test Message - * - * @return CEF Test Message - * @throws Exception Exception - */ - protected EventListener getCEFEventListener() throws Exception { - final String cefMessageAsString = fromStream(CEF_MESSAGE_JSON_FILE_LOCATION); - return ANALYTICS_MODEL_OBJECT_MAPPER.readValue(cefMessageAsString, EventListener.class); - } - - /** - * Deserialize given Json file location to given model class and returns it back without any validation check - * - * @param jsonFileLocation Classpath location of the json file - * @param modelClass Model Class type - * @param Json Model Type - * @return Json model object - */ - public static T deserializeJsonFileToModel(String jsonFileLocation, Class modelClass) { - final InputStream jsonFileInputStream = - BaseDCAEAnalyticsUnitTest.class.getClassLoader().getResourceAsStream(jsonFileLocation); - Assert.assertNotNull("Json File Location must be valid", jsonFileInputStream); - try { - return ANALYTICS_MODEL_OBJECT_MAPPER.readValue(jsonFileInputStream, modelClass); - } catch (IOException ex) { - LOG.error("Error while doing assert Json for fileLocation: {}, modelClass: {}, Exception {}", - jsonFileLocation, modelClass, ex); - throw new RuntimeException(ex); - } finally { - try { - jsonFileInputStream.close(); - } catch (IOException e) { - LOG.error("Error while closing input stream at file location: {}", jsonFileLocation); - throw new RuntimeException(e); - } - } - } - - /** - * Provides a test application preference for unit testing - * - * @return tca app preferences - */ - protected static TCATestAppPreferences getTCATestAppPreferences() { - final TCATestAppPreferences tcaTestAppPreferences = new TCATestAppPreferences(); - tcaTestAppPreferences.setSubscriberHostName("SUBSCRIBER_HOST_NAME"); - tcaTestAppPreferences.setSubscriberHostPortNumber(10000); - tcaTestAppPreferences.setSubscriberTopicName("SUBSCRIBER_TOPIC_NAME"); - tcaTestAppPreferences.setSubscriberUserName("SUBSCRIBER_USERNAME"); - tcaTestAppPreferences.setSubscriberUserPassword("SUBSCRIBER_PASSWORD"); - tcaTestAppPreferences.setSubscriberProtocol("https"); - tcaTestAppPreferences.setSubscriberContentType("application/json"); - tcaTestAppPreferences.setSubscriberConsumerId("SUBSCRIBER_CONSUMER_ID"); - tcaTestAppPreferences.setSubscriberConsumerGroup("SUBSCRIBER_CONSUMER_GROUP_NAME"); - tcaTestAppPreferences.setSubscriberTimeoutMS(10); - tcaTestAppPreferences.setSubscriberMessageLimit(100); - tcaTestAppPreferences.setSubscriberPollingInterval(1000); - - tcaTestAppPreferences.setPublisherHostName("PUBLISHER_HOST_NAME"); - tcaTestAppPreferences.setPublisherHostPort(1234); - tcaTestAppPreferences.setPublisherTopicName("PUBLISHER_TOPIC_NAME"); - tcaTestAppPreferences.setPublisherUserName("PUBLISHER_USERNAME"); - tcaTestAppPreferences.setPublisherUserPassword("PUBLISHER_PASSWORD"); - tcaTestAppPreferences.setPublisherProtocol("https"); - tcaTestAppPreferences.setPublisherContentType("application/json"); - tcaTestAppPreferences.setPublisherMaxBatchSize(100); - tcaTestAppPreferences.setPublisherMaxRecoveryQueueSize(100); - tcaTestAppPreferences.setPublisherPollingInterval(6000); - return tcaTestAppPreferences; - } - - protected static Map getPreferenceMap() { - Map preference = new HashMap<>(); - preference.put("subscriberHostName", "mrlocal-mtnjftle01.homer.com"); - preference.put("subscriberHostPort", "3905"); - preference.put("subscriberTopicName", "com.dcae.dmaap.mtnje2.DcaeTestVESPub"); - preference.put("subscriberProtocol", "https"); - preference.put("subscriberUserName", "m00502@tca.af.dcae.com"); - preference.put("subscriberUserPassword", "Te5021abc"); - preference.put("subscriberContentType", "application/json"); - preference.put("subscriberConsumerId", "123"); - preference.put("subscriberConsumerGroup", "testTCAConsumerName-123"); - preference.put("subscriberTimeoutMS", "-1"); - preference.put("subscriberMessageLimit", "-1"); - preference.put("subscriberPollingInterval", "30000"); - - preference.put("publisherHostName", "publisherHostName"); - preference.put("publisherHostPort", "3905"); - preference.put("publisherTopicName", "publisherTopicName"); - preference.put("publisherProtocol", "https"); - preference.put("publisherUserName", "publisherUserName"); - preference.put("publisherContentType", "application/json"); - preference.put("publisherMaxBatchSize", "1000"); - preference.put("publisherMaxRecoveryQueueSize", "100"); - preference.put("publisherPollingInterval", "6000"); - return preference; - } - - protected static Threshold getCriticalThreshold() { - Threshold criticalThreshold = new Threshold(); - criticalThreshold.setClosedLoopControlName("CL-LBAL-LOW-TRAFFIC-SIG-FB480F95-A453-6F24-B767-FD703241AB1A"); - criticalThreshold.setThresholdValue(5000L); - criticalThreshold.setFieldPath("$.event.measurementsForVfScalingFields.vNicUsageArray[*].packetsIn"); - criticalThreshold.setDirection(Direction.GREATER_OR_EQUAL); - return criticalThreshold; - } - - protected static List getThresholds() { - Threshold majorThreshold = new Threshold(); - majorThreshold.setClosedLoopControlName("CL-LBAL-LOW-TRAFFIC-SIG-FB480F95-A453-6F24-B767-FD703241AB1A"); - majorThreshold.setFieldPath("$.event.measurementsForVfScalingFields.vNicUsageArray[*].packetsIn"); - majorThreshold.setVersion("Test Version"); - majorThreshold.setThresholdValue(500L); - majorThreshold.setDirection(Direction.LESS_OR_EQUAL); - - Threshold criticalThreshold = new Threshold(); - criticalThreshold.setClosedLoopControlName("CL-LBAL-LOW-TRAFFIC-SIG-FB480F95-A453-6F24-B767-FD703241AB1A"); - criticalThreshold.setThresholdValue(5000L); - criticalThreshold.setFieldPath("$.event.measurementsForVfScalingFields.vNicUsageArray[*].packetsIn"); - criticalThreshold.setDirection(Direction.GREATER_OR_EQUAL); - return Arrays.asList(majorThreshold, criticalThreshold); - } -} diff --git a/dcae-analytics-tca/src/test/java/org/openecomp/dcae/analytics/tca/it/TCAnalyticsAppConfigIT.java b/dcae-analytics-tca/src/test/java/org/openecomp/dcae/analytics/tca/it/TCAnalyticsAppConfigIT.java deleted file mode 100644 index 2ed5252..0000000 --- a/dcae-analytics-tca/src/test/java/org/openecomp/dcae/analytics/tca/it/TCAnalyticsAppConfigIT.java +++ /dev/null @@ -1,53 +0,0 @@ -/* - * ============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.analytics.tca.it; - -import org.junit.Test; -import org.openecomp.dcae.analytics.tca.BaseAnalyticsTCAIT; -import org.openecomp.dcae.analytics.tca.settings.TCATestAppConfig; -import org.openecomp.dcae.analytics.tca.settings.TCATestAppConfigHolder; -import org.openecomp.dcae.analytics.tca.settings.TCATestAppPreferences; - -/** - * - * @author Rajiv Singla. Creation Date: 10/25/2016. - */ -public class TCAnalyticsAppConfigIT extends BaseAnalyticsTCAIT { - - - @Test - public void createTestAppConfigJson() throws Exception { - final TCATestAppConfig tcaTestAppConfig = getTCATestAppConfig(); - final TCATestAppConfigHolder appConfigHolder = new TCATestAppConfigHolder(tcaTestAppConfig); - final String appConfigJson = serializeModelToJson(appConfigHolder); - LOG.info("AppConfigJson: \n{}", appConfigJson); - writeToOutputTextFile("appSettings/tca_app_config.json", appConfigJson, TCAnalyticsAppConfigIT.class); - } - - @Test - public void createTestAppPreferencesJson() throws Exception { - final TCATestAppPreferences tcaTestAppPreferences = getTCATestAppPreferences(); - final String appPreferencesJson = serializeModelToJson(tcaTestAppPreferences); - LOG.info("AppPreferences: \n{}", appPreferencesJson); - writeToOutputTextFile("appSettings/tca_app_preferences.json", - appPreferencesJson, TCAnalyticsAppConfigIT.class); - } -} diff --git a/dcae-analytics-tca/src/test/java/org/openecomp/dcae/analytics/tca/processor/TCACEFJsonProcessorTest.java b/dcae-analytics-tca/src/test/java/org/openecomp/dcae/analytics/tca/processor/TCACEFJsonProcessorTest.java deleted file mode 100644 index 05efcd6..0000000 --- a/dcae-analytics-tca/src/test/java/org/openecomp/dcae/analytics/tca/processor/TCACEFJsonProcessorTest.java +++ /dev/null @@ -1,117 +0,0 @@ -/* - * ============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.analytics.tca.processor; - -import org.junit.Test; -import org.openecomp.dcae.analytics.common.exception.MessageProcessingException; -import org.openecomp.dcae.analytics.model.domain.cef.EventListener; -import org.openecomp.dcae.analytics.tca.BaseAnalyticsTCAUnitTest; - -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; - -/** - * - * @author Rajiv Singla. Creation Date: 11/9/2016. - */ -public class TCACEFJsonProcessorTest extends BaseAnalyticsTCAUnitTest { - - - // A valid CEF Message - @Test - public void testCEFJsonProcessorWithValidCEFMessage() throws Exception { - - final String cefMessageString = fromStream(CEF_MESSAGE_JSON_FILE_LOCATION); - final TCACEFProcessorContext tcacefProcessorContext = - new TCACEFProcessorContext(cefMessageString, getSampleTCAPolicy()); - - TCACEFJsonProcessor tcacefJsonProcessor = new TCACEFJsonProcessor(); - final TCACEFProcessorContext finalProcessorContext = tcacefJsonProcessor.apply(tcacefProcessorContext); - - final EventListener cefEventListener = finalProcessorContext.getCEFEventListener(); - - assertNotNull("CEF Event Listener must be present", cefEventListener); - - } - - // Even if message is not a valid CEF format but still a Json - Json Processor will parse it - @Test - public void testCEFJsonProcessorWithValidJson() throws Exception { - - final TCACEFProcessorContext tcacefProcessorContext = new TCACEFProcessorContext( - " { \"key\" : \"value\" } ", getSampleTCAPolicy()); - - TCACEFJsonProcessor tcacefJsonProcessor = new TCACEFJsonProcessor(); - final TCACEFProcessorContext finalProcessorContext = tcacefJsonProcessor.apply(tcacefProcessorContext); - final EventListener cefEventListener = finalProcessorContext.getCEFEventListener(); - - assertNotNull("Even if message is not a valid CEF format but a valid Json.Json Processor must be able to " + - "parse it", - cefEventListener); - } - - @Test(expected = MessageProcessingException.class) - public void testCEFJsonProcessorWithCEFMessageAsNull() throws Exception { - - final TCACEFProcessorContext tcacefProcessorContext = new TCACEFProcessorContext(null, getSampleTCAPolicy()); - - TCACEFJsonProcessor tcacefJsonProcessor = new TCACEFJsonProcessor(); - tcacefJsonProcessor.apply(tcacefProcessorContext); - - } - - @Test - public void testCEFJsonProcessorWithCEFMessageIsBlank() throws Exception { - - final TCACEFProcessorContext tcacefProcessorContext = new TCACEFProcessorContext(" ", getSampleTCAPolicy()); - - TCACEFJsonProcessor tcacefJsonProcessor = new TCACEFJsonProcessor(); - final TCACEFProcessorContext finalProcessorContext = tcacefJsonProcessor.apply(tcacefProcessorContext); - assertFalse("Blank message must terminate processing of message chain", finalProcessorContext - .canProcessingContinue()); - } - - - @Test - public void testCEFJsonProcessorWithCEFMessageWhichIsNotValidMessage() throws Exception { - - final TCACEFProcessorContext tcacefProcessorContext = new TCACEFProcessorContext(" Invalid Message ", - getSampleTCAPolicy()); - - TCACEFJsonProcessor tcacefJsonProcessor = new TCACEFJsonProcessor(); - final TCACEFProcessorContext finalProcessorContext = tcacefJsonProcessor.apply(tcacefProcessorContext); - assertFalse("Invalid message must terminate processing of message chain", finalProcessorContext - .canProcessingContinue()); - } - - - @Test(expected = MessageProcessingException.class) - public void testCEFJsonProcessorWithCEFMessageWhichIsNotValidJson() throws Exception { - - final TCACEFProcessorContext tcacefProcessorContext = new TCACEFProcessorContext( - " { \"Invalid Event Listener Json\" } ", getSampleTCAPolicy()); - - TCACEFJsonProcessor tcacefJsonProcessor = new TCACEFJsonProcessor(); - tcacefJsonProcessor.apply(tcacefProcessorContext); - } - - -} diff --git a/dcae-analytics-tca/src/test/java/org/openecomp/dcae/analytics/tca/processor/TCACEFPolicyThresholdsProcessorTest.java b/dcae-analytics-tca/src/test/java/org/openecomp/dcae/analytics/tca/processor/TCACEFPolicyThresholdsProcessorTest.java deleted file mode 100644 index 8d83307..0000000 --- a/dcae-analytics-tca/src/test/java/org/openecomp/dcae/analytics/tca/processor/TCACEFPolicyThresholdsProcessorTest.java +++ /dev/null @@ -1,77 +0,0 @@ -/* - * ============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.analytics.tca.processor; - -import org.junit.Test; -import org.openecomp.dcae.analytics.common.service.processor.ProcessingState; -import org.openecomp.dcae.analytics.model.domain.cef.EventListener; -import org.openecomp.dcae.analytics.tca.BaseAnalyticsTCAUnitTest; - -import static org.hamcrest.CoreMatchers.is; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertThat; -import static org.junit.Assert.assertTrue; - -/** - * - * @author Rajiv Singla. Creation Date: 11/9/2016. - */ -public class TCACEFPolicyThresholdsProcessorTest extends BaseAnalyticsTCAUnitTest { - - @Test - public void testCEFPolicyThresholdProcessorWithNoThresholdViolation() throws Exception { - - final String cefMessageString = fromStream(CEF_MESSAGE_JSON_FILE_LOCATION); - final TCACEFProcessorContext tcacefProcessorContext = new TCACEFProcessorContext(cefMessageString, - getSampleTCAPolicy()); - tcacefProcessorContext.setCEFEventListener(getCEFEventListener()); - - AbstractTCAECEFPolicyProcessor policyThresholdsProcessor = new TCACEFPolicyThresholdsProcessor(); - final TCACEFProcessorContext finalProcessorContext = policyThresholdsProcessor.apply(tcacefProcessorContext); - - assertFalse("Process Context can Processing Continue flag should be false", finalProcessorContext - .canProcessingContinue()); - assertThat("Policy Threshold Processor State must be terminated early", - policyThresholdsProcessor.getProcessingState(), is(ProcessingState.PROCESSING_TERMINATED_EARLY)); - - } - - @Test - public void testCEFPolicyThresholdProcessorWithThresholdViolation() throws Exception { - - final String cefMessageString = fromStream(CEF_MESSAGE_WITH_THRESHOLD_VIOLATION_JSON_FILE_LOCATION); - final TCACEFProcessorContext tcacefProcessorContext = new TCACEFProcessorContext(cefMessageString, - getSampleTCAPolicy()); - - final EventListener eventListener = getCEFEventListener(); - tcacefProcessorContext.setCEFEventListener(eventListener); - - AbstractTCAECEFPolicyProcessor policyThresholdsProcessor = new TCACEFPolicyThresholdsProcessor(); - final TCACEFProcessorContext finalProcessorContext = policyThresholdsProcessor.apply(tcacefProcessorContext); - - assertTrue("Process Context can Processing Continue flag should be true", finalProcessorContext - .canProcessingContinue()); - assertThat("Policy Threshold Processor State must be successful", - policyThresholdsProcessor.getProcessingState(), is(ProcessingState.PROCESSING_FINISHED_SUCCESSFULLY)); - - } - -} diff --git a/dcae-analytics-tca/src/test/java/org/openecomp/dcae/analytics/tca/processor/TCACEFProcessorContextTest.java b/dcae-analytics-tca/src/test/java/org/openecomp/dcae/analytics/tca/processor/TCACEFProcessorContextTest.java deleted file mode 100644 index d955d81..0000000 --- a/dcae-analytics-tca/src/test/java/org/openecomp/dcae/analytics/tca/processor/TCACEFProcessorContextTest.java +++ /dev/null @@ -1,38 +0,0 @@ -/* - * ============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.analytics.tca.processor; - -import org.junit.Test; -import org.openecomp.dcae.analytics.tca.BaseAnalyticsTCAUnitTest; - -/** - * - * @author Rajiv Singla. Creation Date: 11/14/2016. - */ -public class TCACEFProcessorContextTest extends BaseAnalyticsTCAUnitTest { - - @Test - public void testProcessorContextSerialization() throws Exception { - TCACEFProcessorContext tcacefProcessorContext = new TCACEFProcessorContext(getValidCEFMessage(), - getSampleTCAPolicy()); - testSerialization(tcacefProcessorContext, TCACEFProcessorContextTest.class); - } -} diff --git a/dcae-analytics-tca/src/test/java/org/openecomp/dcae/analytics/tca/settings/TCATestAppConfig.java b/dcae-analytics-tca/src/test/java/org/openecomp/dcae/analytics/tca/settings/TCATestAppConfig.java deleted file mode 100644 index 4e057cc..0000000 --- a/dcae-analytics-tca/src/test/java/org/openecomp/dcae/analytics/tca/settings/TCATestAppConfig.java +++ /dev/null @@ -1,38 +0,0 @@ -/* - * ============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.analytics.tca.settings; - -/** - * TCA Test App Config is used for testing purposes only - * - * @author Rajiv Singla. Creation Date: 11/3/2016. - */ -public class TCATestAppConfig extends TCAAppConfig { - - public void setAppName(String appName) { - this.appName = appName; - } - - public void setAppDescription(String appDescription) { - this.appDescription = appDescription; - } - -} diff --git a/dcae-analytics-tca/src/test/java/org/openecomp/dcae/analytics/tca/settings/TCATestAppConfigHolder.java b/dcae-analytics-tca/src/test/java/org/openecomp/dcae/analytics/tca/settings/TCATestAppConfigHolder.java deleted file mode 100644 index e854cac..0000000 --- a/dcae-analytics-tca/src/test/java/org/openecomp/dcae/analytics/tca/settings/TCATestAppConfigHolder.java +++ /dev/null @@ -1,40 +0,0 @@ -/* - * ============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.analytics.tca.settings; - -/** - * Holder for TCA Test App Config - * - * @author Rajiv Singla. Creation Date: 11/3/2016. - */ -public class TCATestAppConfigHolder { - - private final TCATestAppConfig config; - - public TCATestAppConfigHolder(TCATestAppConfig config) { - this.config = config; - } - - public TCATestAppConfig getConfig() { - return config; - } - -} diff --git a/dcae-analytics-tca/src/test/java/org/openecomp/dcae/analytics/tca/settings/TCATestAppPreferences.java b/dcae-analytics-tca/src/test/java/org/openecomp/dcae/analytics/tca/settings/TCATestAppPreferences.java deleted file mode 100644 index f8a0474..0000000 --- a/dcae-analytics-tca/src/test/java/org/openecomp/dcae/analytics/tca/settings/TCATestAppPreferences.java +++ /dev/null @@ -1,136 +0,0 @@ -/* - * ============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.analytics.tca.settings; - - -import com.fasterxml.jackson.annotation.JsonAnyGetter; - -import java.util.Map; - -/** - * TCA Test App Preferences are used for testing purposes only - * - * @author Rajiv Singla. Creation Date: 11/3/2016. - */ -public class TCATestAppPreferences extends TCAAppPreferences { - - private Map policyPreferences; - - public TCATestAppPreferences() { - } - - public TCATestAppPreferences(final Map policyPreferences) { - this.policyPreferences = policyPreferences; - } - - @JsonAnyGetter - public Map getPolicyPreferences() { - return policyPreferences; - } - - public void setSubscriberHostName(String subscriberHostName) { - this.subscriberHostName = subscriberHostName; - } - - public void setSubscriberHostPortNumber(Integer subscriberHostPort) { - this.subscriberHostPort = subscriberHostPort; - } - - public void setSubscriberTopicName(String subscriberTopicName) { - this.subscriberTopicName = subscriberTopicName; - } - - public void setSubscriberProtocol(String subscriberProtocol) { - this.subscriberProtocol = subscriberProtocol; - } - - public void setSubscriberUserName(String subscriberUserName) { - this.subscriberUserName = subscriberUserName; - } - - public void setSubscriberUserPassword(String subscriberUserPassword) { - this.subscriberUserPassword = subscriberUserPassword; - } - - public void setSubscriberContentType(String subscriberContentType) { - this.subscriberContentType = subscriberContentType; - } - - public void setSubscriberConsumerId(String subscriberConsumerId) { - this.subscriberConsumerId = subscriberConsumerId; - } - - public void setSubscriberConsumerGroup(String subscriberConsumerGroup) { - this.subscriberConsumerGroup = subscriberConsumerGroup; - } - - public void setSubscriberTimeoutMS(Integer subscriberTimeoutMS) { - this.subscriberTimeoutMS = subscriberTimeoutMS; - } - - public void setSubscriberMessageLimit(Integer subscriberMessageLimit) { - this.subscriberMessageLimit = subscriberMessageLimit; - } - - public void setSubscriberPollingInterval(Integer subscriberPollingInterval) { - this.subscriberPollingInterval = subscriberPollingInterval; - } - - public void setPublisherHostName(String publisherHostName) { - this.publisherHostName = publisherHostName; - } - - public void setPublisherHostPort(Integer publisherHostPort) { - this.publisherHostPort = publisherHostPort; - } - - public void setPublisherTopicName(String publisherTopicName) { - this.publisherTopicName = publisherTopicName; - } - - public void setPublisherProtocol(String publisherProtocol) { - this.publisherProtocol = publisherProtocol; - } - - public void setPublisherUserName(String publisherUserName) { - this.publisherUserName = publisherUserName; - } - - public void setPublisherUserPassword(String publisherUserPassword) { - this.publisherUserPassword = publisherUserPassword; - } - - public void setPublisherContentType(String publisherContentType) { - this.publisherContentType = publisherContentType; - } - - public void setPublisherMaxBatchSize(Integer publisherMaxBatchSize) { - this.publisherMaxBatchSize = publisherMaxBatchSize; - } - - public void setPublisherMaxRecoveryQueueSize(Integer publisherMaxRecoveryQueueSize) { - this.publisherMaxRecoveryQueueSize = publisherMaxRecoveryQueueSize; - } - - public void setPublisherPollingInterval(Integer publisherPollingInterval) { - this.publisherPollingInterval = publisherPollingInterval; - } -} diff --git a/dcae-analytics-tca/src/test/java/org/openecomp/dcae/analytics/tca/utils/AppPreferencesToPublisherConfigMapperTest.java b/dcae-analytics-tca/src/test/java/org/openecomp/dcae/analytics/tca/utils/AppPreferencesToPublisherConfigMapperTest.java deleted file mode 100644 index d66ef52..0000000 --- a/dcae-analytics-tca/src/test/java/org/openecomp/dcae/analytics/tca/utils/AppPreferencesToPublisherConfigMapperTest.java +++ /dev/null @@ -1,58 +0,0 @@ -/* - * ============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.analytics.tca.utils; - -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.runners.MockitoJUnitRunner; -import org.openecomp.dcae.analytics.dmaap.domain.config.DMaaPMRPublisherConfig; -import org.openecomp.dcae.analytics.tca.BaseAnalyticsTCAUnitTest; -import org.openecomp.dcae.analytics.tca.settings.TCATestAppPreferences; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNull; - -/** - * @author Manjesh Gowda. Creation Date: 11/21/2016. - */ -@RunWith(MockitoJUnitRunner.class) -public class AppPreferencesToPublisherConfigMapperTest extends BaseAnalyticsTCAUnitTest { - @Test - public void testMapTCAConfigToSubscriberConfigFunctionGood() { - DMaaPMRPublisherConfig dMaaPMRPublisherConfig = - (new AppPreferencesToPublisherConfigMapper()).apply(getTCATestAppPreferences()); - assertEquals(dMaaPMRPublisherConfig.getHostName(), "PUBLISHER_HOST_NAME"); - } - - @Test - public void testMapTCAConfigToSubscriberConfigFunctionMap() { - DMaaPMRPublisherConfig dMaaPMRPublisherConfig = AppPreferencesToPublisherConfigMapper.map( - getTCATestAppPreferences()); - assertEquals(dMaaPMRPublisherConfig.getHostName(), "PUBLISHER_HOST_NAME"); - } - - @Test - public void testMapTCAConfigToSubscriberConfigFunctionAllNull() { - DMaaPMRPublisherConfig dMaaPMRPublisherConfig = - (new AppPreferencesToPublisherConfigMapper()).apply(new TCATestAppPreferences()); - assertNull(dMaaPMRPublisherConfig.getHostName()); - } -} diff --git a/dcae-analytics-tca/src/test/java/org/openecomp/dcae/analytics/tca/utils/AppPreferencesToSubscriberConfigMapperTest.java b/dcae-analytics-tca/src/test/java/org/openecomp/dcae/analytics/tca/utils/AppPreferencesToSubscriberConfigMapperTest.java deleted file mode 100644 index 6963201..0000000 --- a/dcae-analytics-tca/src/test/java/org/openecomp/dcae/analytics/tca/utils/AppPreferencesToSubscriberConfigMapperTest.java +++ /dev/null @@ -1,59 +0,0 @@ -/* - * ============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.analytics.tca.utils; - -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.runners.MockitoJUnitRunner; -import org.openecomp.dcae.analytics.dmaap.domain.config.DMaaPMRSubscriberConfig; -import org.openecomp.dcae.analytics.tca.BaseAnalyticsTCAUnitTest; -import org.openecomp.dcae.analytics.tca.settings.TCATestAppPreferences; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNull; - -/** - * @author Manjesh Gowda. Creation Date: 11/21/2016. - */ -@RunWith(MockitoJUnitRunner.class) -public class AppPreferencesToSubscriberConfigMapperTest extends BaseAnalyticsTCAUnitTest { - - @Test - public void testMapTCAConfigToSubscriberConfigFunctionGood() { - DMaaPMRSubscriberConfig dMaaPMRSubscriberConfig = - (new AppPreferencesToSubscriberConfigMapper()).apply(getTCATestAppPreferences()); - assertEquals(dMaaPMRSubscriberConfig.getHostName(), "SUBSCRIBER_HOST_NAME"); - } - - @Test - public void testMapTCAConfigToSubscriberConfigFunctionMap() { - DMaaPMRSubscriberConfig dMaaPMRSubscriberConfig = - AppPreferencesToSubscriberConfigMapper.map(getTCATestAppPreferences()); - assertEquals(dMaaPMRSubscriberConfig.getHostName(), "SUBSCRIBER_HOST_NAME"); - } - - @Test - public void testMapTCAConfigToSubscriberConfigFunctionAllNull() { - DMaaPMRSubscriberConfig dMaaPMRSubscriberConfig = - (new AppPreferencesToSubscriberConfigMapper()).apply(new TCATestAppPreferences()); - assertNull(dMaaPMRSubscriberConfig.getHostName()); - } -} diff --git a/dcae-analytics-tca/src/test/java/org/openecomp/dcae/analytics/tca/utils/TCAUtilsTest.java b/dcae-analytics-tca/src/test/java/org/openecomp/dcae/analytics/tca/utils/TCAUtilsTest.java deleted file mode 100644 index 19c80d2..0000000 --- a/dcae-analytics-tca/src/test/java/org/openecomp/dcae/analytics/tca/utils/TCAUtilsTest.java +++ /dev/null @@ -1,357 +0,0 @@ -/* - * ============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.analytics.tca.utils; - -import co.cask.cdap.api.RuntimeContext; -import com.google.common.base.Supplier; -import com.google.common.collect.ImmutableSet; -import com.google.common.collect.Table; -import org.apache.commons.lang3.tuple.Pair; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.ExpectedException; -import org.junit.runner.RunWith; -import org.mockito.Mockito; -import org.mockito.runners.MockitoJUnitRunner; -import org.openecomp.dcae.analytics.common.exception.MessageProcessingException; -import org.openecomp.dcae.analytics.model.domain.cef.CommonEventHeader; -import org.openecomp.dcae.analytics.model.domain.cef.Event; -import org.openecomp.dcae.analytics.model.domain.cef.EventListener; -import org.openecomp.dcae.analytics.model.domain.cef.EventSeverity; -import org.openecomp.dcae.analytics.model.domain.policy.tca.MetricsPerFunctionalRole; -import org.openecomp.dcae.analytics.model.domain.policy.tca.TCAPolicy; -import org.openecomp.dcae.analytics.model.domain.policy.tca.Threshold; -import org.openecomp.dcae.analytics.model.facade.tca.TCAVESResponse; -import org.openecomp.dcae.analytics.model.util.AnalyticsModelIOUtils; -import org.openecomp.dcae.analytics.tca.BaseAnalyticsTCAUnitTest; -import org.openecomp.dcae.analytics.tca.processor.TCACEFProcessorContext; -import org.openecomp.dcae.analytics.tca.settings.TCAAppPreferences; - -import java.util.HashMap; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; -import java.util.Properties; -import java.util.Set; - -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.CoreMatchers.isA; -import static org.hamcrest.Matchers.containsInAnyOrder; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertThat; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - -/** - * @author Rajiv Singla. Creation Date: 11/9/2016. - */ -@RunWith(MockitoJUnitRunner.class) -public class TCAUtilsTest extends BaseAnalyticsTCAUnitTest { - - @Test - public void testGetPolicyFunctionalRoles() throws Exception { - - final TCAPolicy sampleTCAPolicy = getSampleTCAPolicy(); - final List policyFunctionalRoles = TCAUtils.getPolicyFunctionalRoles(sampleTCAPolicy); - - assertThat("Policy Functional Roles must contain vFirewall and vLoadBalancer", policyFunctionalRoles, - containsInAnyOrder("vFirewall", "vLoadBalancer")); - } - - @Test - public void testGetPolicyFunctionalRoleSupplier() throws Exception { - final TCAPolicy sampleTCAPolicy = getSampleTCAPolicy(); - final Supplier> policyFunctionalRoleSupplier = TCAUtils.getPolicyFunctionalRoleSupplier - (sampleTCAPolicy); - final List policyFunctionalRoles = policyFunctionalRoleSupplier.get(); - assertThat("Policy Functional Roles must contain vFirewall and vLoadBalancer", policyFunctionalRoles, - containsInAnyOrder("vFirewall", "vLoadBalancer")); - } - - @Test - public void testProcessCEFMessage() throws Exception { - final String cefMessageString = fromStream(CEF_MESSAGE_JSON_FILE_LOCATION); - final TCACEFProcessorContext tcacefProcessorContext = TCAUtils.filterCEFMessage(cefMessageString, - getSampleTCAPolicy()); - assertThat("TCAECEFProcessor Processor Context can continue flag is true", tcacefProcessorContext - .canProcessingContinue(), is(true)); - } - - @Test - public void testGetPolicyFRThresholdsTableSupplier() throws Exception { - final Table> policyFRThresholdPathTable = TCAUtils - .getPolicyFRThresholdsTableSupplier(getSampleTCAPolicy()).get(); - - final Map> vFirewall = policyFRThresholdPathTable.row("vFirewall"); - final Map> vLoadBalancer = policyFRThresholdPathTable.row("vLoadBalancer"); - - final Set vFirewallThresholdPaths = vFirewall.keySet(); - final Set vLoadBalancerPaths = vLoadBalancer.keySet(); - - assertThat("vFirewall threshold field path size must be " + - "\"$.event.measurementsForVfScalingFields.vNicUsageArray[*].bytesIn\"", - vFirewallThresholdPaths.iterator().next(), - is("$.event.measurementsForVfScalingFields.vNicUsageArray[*].bytesIn")); - - assertThat("vLoadBalancer threshold field path size must be " + - "\"\"$.event.measurementsForVfScalingFields.vNicUsageArray[*].packetsIn\"", - vLoadBalancerPaths.iterator().next(), - is("$.event.measurementsForVfScalingFields.vNicUsageArray[*].packetsIn")); - - final List firewallThresholds = policyFRThresholdPathTable.get("vFirewall", - "$.event.measurementsForVfScalingFields.vNicUsageArray[*].bytesIn"); - final List vLoadBalancerThresholds = policyFRThresholdPathTable.get("vLoadBalancer", - "$.event.measurementsForVfScalingFields.vNicUsageArray[*].packetsIn"); - - assertThat("vFirewall Threshold size must be 2", firewallThresholds.size(), is(2)); - assertThat("vLoadBalancer Threshold size must be 2", vLoadBalancerThresholds.size(), is(2)); - } - - @Test - public void testGetJsonPathValueWithValidMessageAndPolicy() throws Exception { - final String cefMessageString = fromStream(CEF_MESSAGE_JSON_FILE_LOCATION); - final String jsonPath = "$.event.measurementsForVfScalingFields.vNicUsageArray[*].bytesIn"; - final ImmutableSet fieldPaths = ImmutableSet.of(jsonPath); - final Map> jsonPathValueMap = TCAUtils.getJsonPathValue(cefMessageString, fieldPaths); - assertThat("Json Path value must match", jsonPathValueMap.get(jsonPath).get(0), is(6086L)); - - } - - @Test - public void testGetJsonPathValueWithValidPath() throws Exception { - final String cefMessageString = fromStream(CEF_MESSAGE_JSON_FILE_LOCATION); - final String jsonPath = "$.event.measurementsForVfScalingFields.vNicUsageArray[*].invalid"; - final ImmutableSet fieldPaths = ImmutableSet.of(jsonPath); - final Map> jsonPathValueMap = TCAUtils.getJsonPathValue(cefMessageString, fieldPaths); - assertThat("Json path value must be empty", jsonPathValueMap.size(), is(0)); - - } - - @Test - public void testGetValidatedTCAAppPreferences() throws Exception { - RuntimeContext runtimeContext = mock(RuntimeContext.class); - when(runtimeContext.getRuntimeArguments()).thenReturn(getPreferenceMap()); - TCAAppPreferences validatedTCAAppPreferences = TCAUtils.getValidatedTCAAppPreferences(runtimeContext); - assertEquals(validatedTCAAppPreferences.getSubscriberHostName(), "mrlocal-mtnjftle01.homer.com"); - } - - @Test - public void testGetValidatedTCAAppPreferencesWhenDMaaPUrlArePresent() throws Exception { - RuntimeContext runtimeContext = mock(RuntimeContext.class); - final Map preferenceMap = getPreferenceMap(); - preferenceMap.put("dmaap.in.event-input.dmaapUrl", - "http://zldcmtd1njcoll00.research.com/unauthenticated.SEC_MEASUREMENT_OUTPUT"); - preferenceMap.put("dmaap.out.alert-output.dmaapUrl", - "http://zldcmtd1njcoll00.research.com/unauthenticated.TCA_EVENT_OUTPUT"); - - preferenceMap.put("dmaap.in.event-input.dmaapUserName", null); - preferenceMap.put("dmaap.in.event-input.dmaapPassword", null); - preferenceMap.put("dmaap.out.alert-output.dmaapUserName", null); - preferenceMap.put("dmaap.out.alert-output.dmaapPassword", null); - - when(runtimeContext.getRuntimeArguments()).thenReturn(preferenceMap); - TCAAppPreferences validatedTCAAppPreferences = TCAUtils.getValidatedTCAAppPreferences(runtimeContext); - - assertEquals(validatedTCAAppPreferences.getSubscriberProtocol(), "http"); - assertEquals(validatedTCAAppPreferences.getPublisherProtocol(), "http"); - - assertEquals(validatedTCAAppPreferences.getSubscriberHostName(), "zldcmtd1njcoll00.research.com"); - assertEquals(validatedTCAAppPreferences.getPublisherHostName(), "zldcmtd1njcoll00.research.com"); - - assertEquals(validatedTCAAppPreferences.getSubscriberHostPort(), new Integer(3904)); - assertEquals(validatedTCAAppPreferences.getPublisherHostPort(), new Integer(3904)); - - assertEquals(validatedTCAAppPreferences.getSubscriberTopicName(), "unauthenticated.SEC_MEASUREMENT_OUTPUT"); - assertEquals(validatedTCAAppPreferences.getPublisherTopicName(), "unauthenticated.TCA_EVENT_OUTPUT"); - - assertNull(validatedTCAAppPreferences.getSubscriberUserName()); - assertNull(validatedTCAAppPreferences.getSubscriberUserPassword()); - assertNull(validatedTCAAppPreferences.getPublisherUserName()); - assertNull(validatedTCAAppPreferences.getPublisherUserPassword()); - - } - - @Test - public void testCreateNewTCAVESResponse() throws Exception { - TCACEFProcessorContext tcacefProcessorContext = mock(TCACEFProcessorContext.class); - - MetricsPerFunctionalRole metricsPerFunctionalRole = mock(MetricsPerFunctionalRole.class); - when(metricsPerFunctionalRole.getThresholds()).thenReturn(getThresholds()); - when(metricsPerFunctionalRole.getPolicyScope()).thenReturn("Test Policy scope"); - when(tcacefProcessorContext.getMetricsPerFunctionalRole()).thenReturn(metricsPerFunctionalRole); - when(metricsPerFunctionalRole.getFunctionalRole()).thenReturn("vLoadBalancer"); - - when(tcacefProcessorContext.getCEFEventListener()).thenReturn(getCEFEventListener()); - TCAVESResponse tcaVESResponse = TCAUtils.createNewTCAVESResponse(tcacefProcessorContext, "TCA_APP_NAME"); - - //TODO : Add proper assertions, as the usage is not clearly understood - assertThat(tcaVESResponse.getClosedLoopControlName(), - is("CL-LBAL-LOW-TRAFFIC-SIG-FB480F95-A453-6F24-B767-FD703241AB1A")); - assertThat(tcaVESResponse.getVersion(), is("Test Version")); - assertThat(tcaVESResponse.getPolicyScope(), is("Test Policy scope")); - } - - @Rule - public ExpectedException expectedIllegalArgumentException = ExpectedException.none(); - - @Test - public void testCreateNewTCAVESResponseNullFunctionalRole() throws Exception { - expectedIllegalArgumentException.expect(MessageProcessingException.class); - expectedIllegalArgumentException.expectCause(isA(IllegalArgumentException.class)); - expectedIllegalArgumentException.expectMessage("No violations metrics. Unable to create VES Response"); - - TCACEFProcessorContext tcacefProcessorContext = mock(TCACEFProcessorContext.class); - TCAVESResponse tcaVESResponse = TCAUtils.createNewTCAVESResponse(tcacefProcessorContext, "TCA_APP_NAME"); - assertNotNull(tcaVESResponse.getClosedLoopControlName()); - } - - @Test - public void testPrioritizeThresholdViolations() throws Exception { - - Map thresholdMap = new HashMap<>(); - Threshold majorThreshold = mock(Threshold.class); - when(majorThreshold.getSeverity()).thenReturn(EventSeverity.MAJOR); - thresholdMap.put("MAJOR", majorThreshold); - - Threshold result1 = TCAUtils.prioritizeThresholdViolations(thresholdMap); - assertEquals(result1.getSeverity(), EventSeverity.MAJOR); - - Threshold criticalThreshold = mock(Threshold.class); - when(criticalThreshold.getSeverity()).thenReturn(EventSeverity.CRITICAL); - thresholdMap.put("CRITICAL", criticalThreshold); - - Threshold result2 = TCAUtils.prioritizeThresholdViolations(thresholdMap); - assertEquals(result2.getSeverity(), EventSeverity.CRITICAL); - } - - @Test - public void testCreateViolatedMetrics() throws Exception { - TCAPolicy tcaPolicy = getSampleTCAPolicy(); - Threshold violatedThreshold = getCriticalThreshold(); - String functionalRole = "vFirewall"; - MetricsPerFunctionalRole result = TCAUtils.createViolatedMetrics(tcaPolicy, violatedThreshold, functionalRole); - assertThat(result.getPolicyScope(), is("resource=vFirewall;type=configuration")); - assertThat(result.getPolicyName(), is("configuration.dcae.microservice.tca.xml")); - } - - @Test - public void testCreateViolatedMetricsWrongFunctionalRole() throws Exception { - expectedIllegalArgumentException.expect(MessageProcessingException.class); - expectedIllegalArgumentException.expectCause(isA(IllegalStateException.class)); - expectedIllegalArgumentException.expectMessage("TCA Policy must contain functional Role: badFunctionRoleName"); - - TCAPolicy tcaPolicy = getSampleTCAPolicy(); - Threshold violatedThreshold = getCriticalThreshold(); - String functionalRole = "badFunctionRoleName"; - MetricsPerFunctionalRole result = TCAUtils.createViolatedMetrics(tcaPolicy, violatedThreshold, functionalRole); - } - - @Test - public void testGetDomainAndFunctionalRole() { - TCACEFProcessorContext tcacefProcessorContext = mock(TCACEFProcessorContext.class); - EventListener eventListener = mock(EventListener.class); - Event event = mock(Event.class); - CommonEventHeader commonEventHeader = mock(CommonEventHeader.class); - - Pair result = TCAUtils.getDomainAndFunctionalRole(tcacefProcessorContext); - assertNull(result.getLeft()); - assertNull(result.getRight()); - - when(tcacefProcessorContext.getCEFEventListener()).thenReturn(eventListener); - result = TCAUtils.getDomainAndFunctionalRole(tcacefProcessorContext); - assertNull(result.getLeft()); - assertNull(result.getRight()); - - when(eventListener.getEvent()).thenReturn(event); - result = TCAUtils.getDomainAndFunctionalRole(tcacefProcessorContext); - assertNull(result.getLeft()); - assertNull(result.getRight()); - - when(event.getCommonEventHeader()).thenReturn(commonEventHeader); - result = TCAUtils.getDomainAndFunctionalRole(tcacefProcessorContext); - assertNull(result.getLeft()); - assertNull(result.getRight()); - - when(commonEventHeader.getDomain()).thenReturn("testDomain"); - when(commonEventHeader.getFunctionalRole()).thenReturn("functionalRole"); - - result = TCAUtils.getDomainAndFunctionalRole(tcacefProcessorContext); - assertEquals(result.getLeft(), "testDomain"); - assertEquals(result.getRight(), "functionalRole"); - - } - - @Test - public void testComputeThresholdViolationsNotPresent() throws Exception { - TCACEFProcessorContext tcacefProcessorContext = mock(TCACEFProcessorContext.class); - when(tcacefProcessorContext.canProcessingContinue()).thenReturn(true); - when(tcacefProcessorContext.getMessage()).thenReturn(getValidCEFMessage()); - - when(tcacefProcessorContext.getTCAPolicy()).thenReturn(getSampleTCAPolicy()); - when(tcacefProcessorContext.getCEFEventListener()).thenReturn(getCEFEventListener()); - - TCACEFProcessorContext result = TCAUtils.computeThresholdViolations(tcacefProcessorContext); - assertNotNull(result); - verify(result, times(0)).setMetricsPerFunctionalRole(Mockito.any(MetricsPerFunctionalRole.class)); - } - - @Test - public void testComputeThresholdViolationsPresent() throws Exception { - TCACEFProcessorContext tcacefProcessorContext = mock(TCACEFProcessorContext.class); - when(tcacefProcessorContext.canProcessingContinue()).thenReturn(true); - final String cefMessageString = fromStream(CEF_MESSAGE_WITH_THRESHOLD_VIOLATION_JSON_FILE_LOCATION); - when(tcacefProcessorContext.getMessage()).thenReturn(cefMessageString); - - when(tcacefProcessorContext.getTCAPolicy()).thenReturn(getSampleTCAPolicy()); - when(tcacefProcessorContext.getCEFEventListener()).thenReturn(getCEFEventListener()); - - TCACEFProcessorContext result = TCAUtils.computeThresholdViolations(tcacefProcessorContext); - verify(result, times(1)).setMetricsPerFunctionalRole(Mockito.any(MetricsPerFunctionalRole.class)); - } - - - @Test - public void testConvertRuntimeContextToTCAPolicy() throws Exception { - - final Properties controllerProperties = - AnalyticsModelIOUtils.loadPropertiesFile(TCA_CONTROLLER_POLICY_FILE_LOCATION); - - Map runtimeArgs = new LinkedHashMap<>(); - for (Map.Entry property : controllerProperties.entrySet()) { - runtimeArgs.put(property.getKey().toString(), property.getValue().toString()); - } - - RuntimeContext runtimeContext = mock(RuntimeContext.class); - when(runtimeContext.getRuntimeArguments()).thenReturn(runtimeArgs); - final TCAPolicy tcaPolicy = TCAUtils.getValidatedTCAPolicyPreferences(runtimeContext); - - assertThat("Policy Domain must be measurementsForVfScaling", - tcaPolicy.getDomain(), is("measurementsForVfScaling")); - - assertThat("Policy must have 2 metrics per functional roles", - tcaPolicy.getMetricsPerFunctionalRole().size(), is(2)); - - } -} diff --git a/dcae-analytics-tca/src/test/java/org/openecomp/dcae/analytics/tca/worker/TCADMaaPMRSubscriberJobTest.java b/dcae-analytics-tca/src/test/java/org/openecomp/dcae/analytics/tca/worker/TCADMaaPMRSubscriberJobTest.java deleted file mode 100644 index bc8a6f0..0000000 --- a/dcae-analytics-tca/src/test/java/org/openecomp/dcae/analytics/tca/worker/TCADMaaPMRSubscriberJobTest.java +++ /dev/null @@ -1,80 +0,0 @@ -/* - * ============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.analytics.tca.worker; - -import co.cask.cdap.api.metrics.Metrics; -import co.cask.cdap.api.worker.WorkerConfigurer; -import co.cask.cdap.api.worker.WorkerContext; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.Mockito; -import org.mockito.runners.MockitoJUnitRunner; -import org.openecomp.dcae.analytics.common.AnalyticsConstants; -import org.openecomp.dcae.analytics.dmaap.service.subscriber.DMaaPMRSubscriber; -import org.openecomp.dcae.analytics.tca.BaseAnalyticsTCAUnitTest; -import org.quartz.JobDataMap; -import org.quartz.JobExecutionContext; - -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - -/** - * @author Manjesh Gowda. Creation Date: 11/18/2016. - */ -@RunWith(MockitoJUnitRunner.class) -public class TCADMaaPMRSubscriberJobTest extends BaseAnalyticsTCAUnitTest { - - @Test - public void testExecute() throws Exception { - - DMaaPMRSubscriber mockDMaaPMRSubscriber = mock(DMaaPMRSubscriber.class); - Metrics mockMetrics = mock(Metrics.class); - - WorkerContext workerContext = mock(WorkerContext.class); - WorkerConfigurer workerConfigurer = mock(WorkerConfigurer.class); - //when(workerContext.getRuntimeArguments()).thenReturn(getPreferenceMap()); - - JobExecutionContext mockJobExecutionContext = mock(JobExecutionContext.class); - JobDataMap mockJobDataMap = mock(JobDataMap.class); - when(mockJobExecutionContext.getMergedJobDataMap()).thenReturn(mockJobDataMap); - - /*when(mockJobDataMap.getString(AnalyticsConstants.CDAP_STREAM_VARIABLE_NAME)) - .thenReturn(CDAPComponentsConstants.TCA_FIXED_SUBSCRIBER_OUTPUT_NAME_STREAM);*/ - - when(mockJobDataMap.get(AnalyticsConstants.WORKER_CONTEXT_VARIABLE_NAME)) - .thenReturn(workerContext); - - when(mockJobDataMap.get(AnalyticsConstants.DMAAP_SUBSCRIBER_VARIABLE_NAME)) - .thenReturn(mockDMaaPMRSubscriber); - - /*when(mockJobDataMap.get(AnalyticsConstants.DMAAP_SUBSCRIBER_METRICS_VARIABLE_NAME)) - .thenReturn(mockMetrics);*/ - - TCADMaaPMRSubscriberJob tcaDMaaPMRSubscriberJob = new TCADMaaPMRSubscriberJob(); - tcaDMaaPMRSubscriberJob.execute(mockJobExecutionContext); - - verify(mockJobDataMap, times(1)).getString(Mockito.anyString()); - verify(mockJobDataMap, times(3)).get(any()); - } -} diff --git a/dcae-analytics-tca/src/test/java/org/openecomp/dcae/apod/analytics/tca/BaseAnalyticsTCAIT.java b/dcae-analytics-tca/src/test/java/org/openecomp/dcae/apod/analytics/tca/BaseAnalyticsTCAIT.java new file mode 100644 index 0000000..e7fa380 --- /dev/null +++ b/dcae-analytics-tca/src/test/java/org/openecomp/dcae/apod/analytics/tca/BaseAnalyticsTCAIT.java @@ -0,0 +1,141 @@ +/* + * ============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.tca; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.SerializationFeature; +import com.google.common.base.Suppliers; +import org.junit.BeforeClass; +import org.openecomp.dcae.apod.analytics.common.AnalyticsConstants; +import org.openecomp.dcae.apod.analytics.model.util.AnalyticsModelIOUtils; +import org.openecomp.dcae.apod.analytics.model.util.json.AnalyticsModelObjectMapperSupplier; +import org.openecomp.dcae.apod.analytics.tca.settings.TCATestAppConfig; +import org.openecomp.dcae.apod.analytics.tca.settings.TCATestAppPreferences; +import org.openecomp.dcae.apod.analytics.test.BaseDCAEAnalyticsIT; + +import java.util.LinkedHashMap; +import java.util.Map; +import java.util.Properties; + +/** + * + * @author Rajiv Singla. Creation Date: 10/25/2016. + */ +public class BaseAnalyticsTCAIT extends BaseDCAEAnalyticsIT { + + protected static ObjectMapper objectMapper; + + @BeforeClass + public static void beforeClass() { + final AnalyticsModelObjectMapperSupplier analyticsModelObjectMapperSupplier = + new AnalyticsModelObjectMapperSupplier(); + objectMapper = Suppliers.memoize(analyticsModelObjectMapperSupplier).get(); + objectMapper.enable(SerializationFeature.INDENT_OUTPUT); + } + + protected static final String TCA_CONTROLLER_POLICY_FILE_LOCATION = + "data/properties/tca_controller_policy.properties"; + + // App Settings + protected static final String DCAE_ANALYTICS_TCA_TEST_APP_NAME = "dcae-tca"; + protected static final String DCAE_ANALYTICS_TCA_TEST_APP_DESC = + "DCAE Analytics Threshold Crossing Alert Application"; + + // Subscriber App Preferences + protected static final String SUBSCRIBER_HOST_NAME = "mrlocal-mtnjftle01.homer.com"; + protected static final Integer SUBSCRIBER_PORT_NUMBER = 3905; + protected static final String SUBSCRIBER_TOPIC_NAME = "com.dcae.dmaap.mtnje2.DcaeTestVESSub"; + protected static final String SUBSCRIBER_USERNAME = "m00502@tca.af.dcae.com"; + protected static final String SUBSCRIBER_PASSWORD = "Te5021abc"; + protected static final String SUBSCRIBER_HTTP_PROTOCOL = "https"; + protected static final String SUBSCRIBER_CONTENT_TYPE = "application/json"; + protected static final Integer SUBSCRIBER_POLLING_INTERVAL = 20000; + + protected static final String SUBSCRIBER_CONSUMER_ID = "c12"; + protected static final String SUBSCRIBER_CONSUMER_GROUP_NAME = AnalyticsConstants.DMAAP_GROUP_PREFIX + + SUBSCRIBER_CONSUMER_ID; + protected static final int SUBSCRIBER_TIMOUT_MS = -1; + protected static final int SUBSCRIBER_MESSAGE_LIMIT = -1; + + // Publisher App Preferences + protected static final String PUBLISHER_HOST_NAME = "mrlocal-mtnjftle01.homer.com"; + protected static final Integer PUBLISHER_PORT_NUMBER = 3905; + protected static final String PUBLISHER_TOPIC_NAME = "com.dcae.dmaap.mtnje2.DcaeTestVESPub"; + protected static final String PUBLISHER_USERNAME = "m00502@tca.af.dcae.com"; + protected static final String PUBLISHER_PASSWORD = "Te5021abc"; + protected static final String PUBLISHER_HTTP_PROTOCOL = "https"; + protected static final String PUBLISHER_CONTENT_TYPE = "application/json"; + protected static final Integer PUBLISHER_BATCH_QUEUE_SIZE = 10; + protected static final Integer PUBLISHER_RECOVERY_QUEUE_SIZE = 100000; + protected static final Integer PUBLISHER_POLLING_INTERVAL = 20000; + + protected static TCATestAppConfig getTCATestAppConfig() { + final TCATestAppConfig tcaTestAppConfig = new TCATestAppConfig(); + tcaTestAppConfig.setAppName(DCAE_ANALYTICS_TCA_TEST_APP_NAME); + tcaTestAppConfig.setAppDescription(DCAE_ANALYTICS_TCA_TEST_APP_DESC); + return tcaTestAppConfig; + } + + protected static TCATestAppPreferences getTCATestAppPreferences() { + final TCATestAppPreferences tcaTestAppPreferences = new TCATestAppPreferences(getTCAPolicyPreferences()); + tcaTestAppPreferences.setSubscriberHostName(SUBSCRIBER_HOST_NAME); + tcaTestAppPreferences.setSubscriberHostPortNumber(SUBSCRIBER_PORT_NUMBER); + tcaTestAppPreferences.setSubscriberTopicName(SUBSCRIBER_TOPIC_NAME); + tcaTestAppPreferences.setSubscriberUserName(SUBSCRIBER_USERNAME); + tcaTestAppPreferences.setSubscriberUserPassword(SUBSCRIBER_PASSWORD); + tcaTestAppPreferences.setSubscriberProtocol(SUBSCRIBER_HTTP_PROTOCOL); + tcaTestAppPreferences.setSubscriberContentType(SUBSCRIBER_CONTENT_TYPE); + tcaTestAppPreferences.setSubscriberConsumerId(SUBSCRIBER_CONSUMER_ID); + tcaTestAppPreferences.setSubscriberConsumerGroup(SUBSCRIBER_CONSUMER_GROUP_NAME); + tcaTestAppPreferences.setSubscriberTimeoutMS(SUBSCRIBER_TIMOUT_MS); + tcaTestAppPreferences.setSubscriberMessageLimit(SUBSCRIBER_MESSAGE_LIMIT); + tcaTestAppPreferences.setSubscriberPollingInterval(SUBSCRIBER_POLLING_INTERVAL); + + tcaTestAppPreferences.setPublisherHostName(PUBLISHER_HOST_NAME); + tcaTestAppPreferences.setPublisherHostPort(PUBLISHER_PORT_NUMBER); + tcaTestAppPreferences.setPublisherTopicName(PUBLISHER_TOPIC_NAME); + tcaTestAppPreferences.setPublisherUserName(PUBLISHER_USERNAME); + tcaTestAppPreferences.setPublisherUserPassword(PUBLISHER_PASSWORD); + tcaTestAppPreferences.setPublisherProtocol(PUBLISHER_HTTP_PROTOCOL); + tcaTestAppPreferences.setPublisherContentType(PUBLISHER_CONTENT_TYPE); + tcaTestAppPreferences.setPublisherMaxBatchSize(PUBLISHER_BATCH_QUEUE_SIZE); + tcaTestAppPreferences.setPublisherMaxRecoveryQueueSize(PUBLISHER_RECOVERY_QUEUE_SIZE); + tcaTestAppPreferences.setPublisherPollingInterval(PUBLISHER_POLLING_INTERVAL); + return tcaTestAppPreferences; + } + + + protected static Map getTCAPolicyPreferences() { + final Map policyPreferences = new LinkedHashMap<>(); + final Properties policyPreferencesProps = + AnalyticsModelIOUtils.loadPropertiesFile(TCA_CONTROLLER_POLICY_FILE_LOCATION); + for (Map.Entry propEntry : policyPreferencesProps.entrySet()) { + policyPreferences.put(propEntry.getKey().toString(), propEntry.getValue().toString()); + } + + return policyPreferences; + } + + protected static String serializeModelToJson(Object model) throws JsonProcessingException { + return objectMapper.writeValueAsString(model); + } +} diff --git a/dcae-analytics-tca/src/test/java/org/openecomp/dcae/apod/analytics/tca/BaseAnalyticsTCAUnitTest.java b/dcae-analytics-tca/src/test/java/org/openecomp/dcae/apod/analytics/tca/BaseAnalyticsTCAUnitTest.java new file mode 100644 index 0000000..002c427 --- /dev/null +++ b/dcae-analytics-tca/src/test/java/org/openecomp/dcae/apod/analytics/tca/BaseAnalyticsTCAUnitTest.java @@ -0,0 +1,220 @@ +/* + * ============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.tca; + +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.google.common.base.Suppliers; +import org.junit.Assert; +import org.openecomp.dcae.apod.analytics.model.domain.cef.EventListener; +import org.openecomp.dcae.apod.analytics.model.domain.policy.tca.Direction; +import org.openecomp.dcae.apod.analytics.model.domain.policy.tca.TCAPolicy; +import org.openecomp.dcae.apod.analytics.model.domain.policy.tca.Threshold; +import org.openecomp.dcae.apod.analytics.model.util.json.AnalyticsModelObjectMapperSupplier; +import org.openecomp.dcae.apod.analytics.tca.settings.TCATestAppPreferences; +import org.openecomp.dcae.apod.analytics.test.BaseDCAEAnalyticsUnitTest; + +import java.io.IOException; +import java.io.InputStream; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * @author Rajiv Singla. Creation Date: 10/25/2016. + */ +public class BaseAnalyticsTCAUnitTest extends BaseDCAEAnalyticsUnitTest { + + /** + * Object mapper to be used for all TCA Json Parsing + */ + protected static final ObjectMapper ANALYTICS_MODEL_OBJECT_MAPPER = + Suppliers.memoize(new AnalyticsModelObjectMapperSupplier()).get(); + + protected static final String TCA_POLICY_JSON_FILE_LOCATION = "data/json/policy/tca_policy.json"; + protected static final String CEF_MESSAGES_JSON_FILE_LOCATION = "data/json/cef/cef_messages.json"; + protected static final String CEF_MESSAGE_JSON_FILE_LOCATION = "data/json/cef/cef_message.json"; + protected static final String CEF_MESSAGE_WITH_THRESHOLD_VIOLATION_JSON_FILE_LOCATION = + "data/json/cef/cef_message_with_threshold_violation.json"; + + protected static final String TCA_CONTROLLER_POLICY_FILE_LOCATION = + "data/properties/tca_controller_policy.properties"; + + + /** + * Provides TCA Policy that can be used for testing + * + * @return test TCA Policy Object + */ + protected TCAPolicy getSampleTCAPolicy() { + return deserializeJsonFileToModel(TCA_POLICY_JSON_FILE_LOCATION, TCAPolicy.class); + } + + /** + * Provides list containing 350 CEF messages + * + * @return CEF Test Message + * @throws Exception Exception + */ + protected List getCEFMessages() throws Exception { + final String cefMessageAsString = fromStream(CEF_MESSAGES_JSON_FILE_LOCATION); + final TypeReference> eventListenerListTypeReference = + new TypeReference>() { + }; + return ANALYTICS_MODEL_OBJECT_MAPPER.readValue(cefMessageAsString, eventListenerListTypeReference); + } + + /** + * Provides 1 valid CEF messages which does not violate Threshold as String + * + * @return CEF Test Message String + * @throws Exception Exception + */ + protected String getValidCEFMessage() throws Exception { + return fromStream(CEF_MESSAGE_JSON_FILE_LOCATION); + } + + + /** + * Provides single CEF Test Message + * + * @return CEF Test Message + * @throws Exception Exception + */ + protected EventListener getCEFEventListener() throws Exception { + final String cefMessageAsString = fromStream(CEF_MESSAGE_JSON_FILE_LOCATION); + return ANALYTICS_MODEL_OBJECT_MAPPER.readValue(cefMessageAsString, EventListener.class); + } + + /** + * Deserialize given Json file location to given model class and returns it back without any validation check + * + * @param jsonFileLocation Classpath location of the json file + * @param modelClass Model Class type + * @param Json Model Type + * @return Json model object + */ + public static T deserializeJsonFileToModel(String jsonFileLocation, Class modelClass) { + final InputStream jsonFileInputStream = + BaseDCAEAnalyticsUnitTest.class.getClassLoader().getResourceAsStream(jsonFileLocation); + Assert.assertNotNull("Json File Location must be valid", jsonFileInputStream); + try { + return ANALYTICS_MODEL_OBJECT_MAPPER.readValue(jsonFileInputStream, modelClass); + } catch (IOException ex) { + LOG.error("Error while doing assert Json for fileLocation: {}, modelClass: {}, Exception {}", + jsonFileLocation, modelClass, ex); + throw new RuntimeException(ex); + } finally { + try { + jsonFileInputStream.close(); + } catch (IOException e) { + LOG.error("Error while closing input stream at file location: {}", jsonFileLocation); + throw new RuntimeException(e); + } + } + } + + /** + * Provides a test application preference for unit testing + * + * @return tca app preferences + */ + protected static TCATestAppPreferences getTCATestAppPreferences() { + final TCATestAppPreferences tcaTestAppPreferences = new TCATestAppPreferences(); + tcaTestAppPreferences.setSubscriberHostName("SUBSCRIBER_HOST_NAME"); + tcaTestAppPreferences.setSubscriberHostPortNumber(10000); + tcaTestAppPreferences.setSubscriberTopicName("SUBSCRIBER_TOPIC_NAME"); + tcaTestAppPreferences.setSubscriberUserName("SUBSCRIBER_USERNAME"); + tcaTestAppPreferences.setSubscriberUserPassword("SUBSCRIBER_PASSWORD"); + tcaTestAppPreferences.setSubscriberProtocol("https"); + tcaTestAppPreferences.setSubscriberContentType("application/json"); + tcaTestAppPreferences.setSubscriberConsumerId("SUBSCRIBER_CONSUMER_ID"); + tcaTestAppPreferences.setSubscriberConsumerGroup("SUBSCRIBER_CONSUMER_GROUP_NAME"); + tcaTestAppPreferences.setSubscriberTimeoutMS(10); + tcaTestAppPreferences.setSubscriberMessageLimit(100); + tcaTestAppPreferences.setSubscriberPollingInterval(1000); + + tcaTestAppPreferences.setPublisherHostName("PUBLISHER_HOST_NAME"); + tcaTestAppPreferences.setPublisherHostPort(1234); + tcaTestAppPreferences.setPublisherTopicName("PUBLISHER_TOPIC_NAME"); + tcaTestAppPreferences.setPublisherUserName("PUBLISHER_USERNAME"); + tcaTestAppPreferences.setPublisherUserPassword("PUBLISHER_PASSWORD"); + tcaTestAppPreferences.setPublisherProtocol("https"); + tcaTestAppPreferences.setPublisherContentType("application/json"); + tcaTestAppPreferences.setPublisherMaxBatchSize(100); + tcaTestAppPreferences.setPublisherMaxRecoveryQueueSize(100); + tcaTestAppPreferences.setPublisherPollingInterval(6000); + return tcaTestAppPreferences; + } + + protected static Map getPreferenceMap() { + Map preference = new HashMap<>(); + preference.put("subscriberHostName", "mrlocal-mtnjftle01.homer.com"); + preference.put("subscriberHostPort", "3905"); + preference.put("subscriberTopicName", "com.dcae.dmaap.mtnje2.DcaeTestVESPub"); + preference.put("subscriberProtocol", "https"); + preference.put("subscriberUserName", "m00502@tca.af.dcae.com"); + preference.put("subscriberUserPassword", "Te5021abc"); + preference.put("subscriberContentType", "application/json"); + preference.put("subscriberConsumerId", "123"); + preference.put("subscriberConsumerGroup", "testTCAConsumerName-123"); + preference.put("subscriberTimeoutMS", "-1"); + preference.put("subscriberMessageLimit", "-1"); + preference.put("subscriberPollingInterval", "30000"); + + preference.put("publisherHostName", "publisherHostName"); + preference.put("publisherHostPort", "3905"); + preference.put("publisherTopicName", "publisherTopicName"); + preference.put("publisherProtocol", "https"); + preference.put("publisherUserName", "publisherUserName"); + preference.put("publisherContentType", "application/json"); + preference.put("publisherMaxBatchSize", "1000"); + preference.put("publisherMaxRecoveryQueueSize", "100"); + preference.put("publisherPollingInterval", "6000"); + return preference; + } + + protected static Threshold getCriticalThreshold() { + Threshold criticalThreshold = new Threshold(); + criticalThreshold.setClosedLoopControlName("CL-LBAL-LOW-TRAFFIC-SIG-FB480F95-A453-6F24-B767-FD703241AB1A"); + criticalThreshold.setThresholdValue(5000L); + criticalThreshold.setFieldPath("$.event.measurementsForVfScalingFields.vNicUsageArray[*].packetsIn"); + criticalThreshold.setDirection(Direction.GREATER_OR_EQUAL); + return criticalThreshold; + } + + protected static List getThresholds() { + Threshold majorThreshold = new Threshold(); + majorThreshold.setClosedLoopControlName("CL-LBAL-LOW-TRAFFIC-SIG-FB480F95-A453-6F24-B767-FD703241AB1A"); + majorThreshold.setFieldPath("$.event.measurementsForVfScalingFields.vNicUsageArray[*].packetsIn"); + majorThreshold.setVersion("Test Version"); + majorThreshold.setThresholdValue(500L); + majorThreshold.setDirection(Direction.LESS_OR_EQUAL); + + Threshold criticalThreshold = new Threshold(); + criticalThreshold.setClosedLoopControlName("CL-LBAL-LOW-TRAFFIC-SIG-FB480F95-A453-6F24-B767-FD703241AB1A"); + criticalThreshold.setThresholdValue(5000L); + criticalThreshold.setFieldPath("$.event.measurementsForVfScalingFields.vNicUsageArray[*].packetsIn"); + criticalThreshold.setDirection(Direction.GREATER_OR_EQUAL); + return Arrays.asList(majorThreshold, criticalThreshold); + } +} diff --git a/dcae-analytics-tca/src/test/java/org/openecomp/dcae/apod/analytics/tca/it/TCAnalyticsAppConfigIT.java b/dcae-analytics-tca/src/test/java/org/openecomp/dcae/apod/analytics/tca/it/TCAnalyticsAppConfigIT.java new file mode 100644 index 0000000..59795fd --- /dev/null +++ b/dcae-analytics-tca/src/test/java/org/openecomp/dcae/apod/analytics/tca/it/TCAnalyticsAppConfigIT.java @@ -0,0 +1,53 @@ +/* + * ============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.tca.it; + +import org.junit.Test; +import org.openecomp.dcae.apod.analytics.tca.BaseAnalyticsTCAIT; +import org.openecomp.dcae.apod.analytics.tca.settings.TCATestAppConfig; +import org.openecomp.dcae.apod.analytics.tca.settings.TCATestAppConfigHolder; +import org.openecomp.dcae.apod.analytics.tca.settings.TCATestAppPreferences; + +/** + * + * @author Rajiv Singla. Creation Date: 10/25/2016. + */ +public class TCAnalyticsAppConfigIT extends BaseAnalyticsTCAIT { + + + @Test + public void createTestAppConfigJson() throws Exception { + final TCATestAppConfig tcaTestAppConfig = getTCATestAppConfig(); + final TCATestAppConfigHolder appConfigHolder = new TCATestAppConfigHolder(tcaTestAppConfig); + final String appConfigJson = serializeModelToJson(appConfigHolder); + LOG.info("AppConfigJson: \n{}", appConfigJson); + writeToOutputTextFile("appSettings/tca_app_config.json", appConfigJson, TCAnalyticsAppConfigIT.class); + } + + @Test + public void createTestAppPreferencesJson() throws Exception { + final TCATestAppPreferences tcaTestAppPreferences = getTCATestAppPreferences(); + final String appPreferencesJson = serializeModelToJson(tcaTestAppPreferences); + LOG.info("AppPreferences: \n{}", appPreferencesJson); + writeToOutputTextFile("appSettings/tca_app_preferences.json", + appPreferencesJson, TCAnalyticsAppConfigIT.class); + } +} diff --git a/dcae-analytics-tca/src/test/java/org/openecomp/dcae/apod/analytics/tca/processor/TCACEFJsonProcessorTest.java b/dcae-analytics-tca/src/test/java/org/openecomp/dcae/apod/analytics/tca/processor/TCACEFJsonProcessorTest.java new file mode 100644 index 0000000..dfb640c --- /dev/null +++ b/dcae-analytics-tca/src/test/java/org/openecomp/dcae/apod/analytics/tca/processor/TCACEFJsonProcessorTest.java @@ -0,0 +1,117 @@ +/* + * ============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.tca.processor; + +import org.junit.Test; +import org.openecomp.dcae.apod.analytics.common.exception.MessageProcessingException; +import org.openecomp.dcae.apod.analytics.model.domain.cef.EventListener; +import org.openecomp.dcae.apod.analytics.tca.BaseAnalyticsTCAUnitTest; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; + +/** + * + * @author Rajiv Singla. Creation Date: 11/9/2016. + */ +public class TCACEFJsonProcessorTest extends BaseAnalyticsTCAUnitTest { + + + // A valid CEF Message + @Test + public void testCEFJsonProcessorWithValidCEFMessage() throws Exception { + + final String cefMessageString = fromStream(CEF_MESSAGE_JSON_FILE_LOCATION); + final TCACEFProcessorContext tcacefProcessorContext = + new TCACEFProcessorContext(cefMessageString, getSampleTCAPolicy()); + + TCACEFJsonProcessor tcacefJsonProcessor = new TCACEFJsonProcessor(); + final TCACEFProcessorContext finalProcessorContext = tcacefJsonProcessor.apply(tcacefProcessorContext); + + final EventListener cefEventListener = finalProcessorContext.getCEFEventListener(); + + assertNotNull("CEF Event Listener must be present", cefEventListener); + + } + + // Even if message is not a valid CEF format but still a Json - Json Processor will parse it + @Test + public void testCEFJsonProcessorWithValidJson() throws Exception { + + final TCACEFProcessorContext tcacefProcessorContext = new TCACEFProcessorContext( + " { \"key\" : \"value\" } ", getSampleTCAPolicy()); + + TCACEFJsonProcessor tcacefJsonProcessor = new TCACEFJsonProcessor(); + final TCACEFProcessorContext finalProcessorContext = tcacefJsonProcessor.apply(tcacefProcessorContext); + final EventListener cefEventListener = finalProcessorContext.getCEFEventListener(); + + assertNotNull("Even if message is not a valid CEF format but a valid Json.Json Processor must be able to " + + "parse it", + cefEventListener); + } + + @Test(expected = MessageProcessingException.class) + public void testCEFJsonProcessorWithCEFMessageAsNull() throws Exception { + + final TCACEFProcessorContext tcacefProcessorContext = new TCACEFProcessorContext(null, getSampleTCAPolicy()); + + TCACEFJsonProcessor tcacefJsonProcessor = new TCACEFJsonProcessor(); + tcacefJsonProcessor.apply(tcacefProcessorContext); + + } + + @Test + public void testCEFJsonProcessorWithCEFMessageIsBlank() throws Exception { + + final TCACEFProcessorContext tcacefProcessorContext = new TCACEFProcessorContext(" ", getSampleTCAPolicy()); + + TCACEFJsonProcessor tcacefJsonProcessor = new TCACEFJsonProcessor(); + final TCACEFProcessorContext finalProcessorContext = tcacefJsonProcessor.apply(tcacefProcessorContext); + assertFalse("Blank message must terminate processing of message chain", finalProcessorContext + .canProcessingContinue()); + } + + + @Test + public void testCEFJsonProcessorWithCEFMessageWhichIsNotValidMessage() throws Exception { + + final TCACEFProcessorContext tcacefProcessorContext = new TCACEFProcessorContext(" Invalid Message ", + getSampleTCAPolicy()); + + TCACEFJsonProcessor tcacefJsonProcessor = new TCACEFJsonProcessor(); + final TCACEFProcessorContext finalProcessorContext = tcacefJsonProcessor.apply(tcacefProcessorContext); + assertFalse("Invalid message must terminate processing of message chain", finalProcessorContext + .canProcessingContinue()); + } + + + @Test(expected = MessageProcessingException.class) + public void testCEFJsonProcessorWithCEFMessageWhichIsNotValidJson() throws Exception { + + final TCACEFProcessorContext tcacefProcessorContext = new TCACEFProcessorContext( + " { \"Invalid Event Listener Json\" } ", getSampleTCAPolicy()); + + TCACEFJsonProcessor tcacefJsonProcessor = new TCACEFJsonProcessor(); + tcacefJsonProcessor.apply(tcacefProcessorContext); + } + + +} diff --git a/dcae-analytics-tca/src/test/java/org/openecomp/dcae/apod/analytics/tca/processor/TCACEFPolicyThresholdsProcessorTest.java b/dcae-analytics-tca/src/test/java/org/openecomp/dcae/apod/analytics/tca/processor/TCACEFPolicyThresholdsProcessorTest.java new file mode 100644 index 0000000..e03d81b --- /dev/null +++ b/dcae-analytics-tca/src/test/java/org/openecomp/dcae/apod/analytics/tca/processor/TCACEFPolicyThresholdsProcessorTest.java @@ -0,0 +1,77 @@ +/* + * ============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.tca.processor; + +import org.junit.Test; +import org.openecomp.dcae.apod.analytics.common.service.processor.ProcessingState; +import org.openecomp.dcae.apod.analytics.model.domain.cef.EventListener; +import org.openecomp.dcae.apod.analytics.tca.BaseAnalyticsTCAUnitTest; + +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; + +/** + * + * @author Rajiv Singla. Creation Date: 11/9/2016. + */ +public class TCACEFPolicyThresholdsProcessorTest extends BaseAnalyticsTCAUnitTest { + + @Test + public void testCEFPolicyThresholdProcessorWithNoThresholdViolation() throws Exception { + + final String cefMessageString = fromStream(CEF_MESSAGE_JSON_FILE_LOCATION); + final TCACEFProcessorContext tcacefProcessorContext = new TCACEFProcessorContext(cefMessageString, + getSampleTCAPolicy()); + tcacefProcessorContext.setCEFEventListener(getCEFEventListener()); + + AbstractTCAECEFPolicyProcessor policyThresholdsProcessor = new TCACEFPolicyThresholdsProcessor(); + final TCACEFProcessorContext finalProcessorContext = policyThresholdsProcessor.apply(tcacefProcessorContext); + + assertFalse("Process Context can Processing Continue flag should be false", finalProcessorContext + .canProcessingContinue()); + assertThat("Policy Threshold Processor State must be terminated early", + policyThresholdsProcessor.getProcessingState(), is(ProcessingState.PROCESSING_TERMINATED_EARLY)); + + } + + @Test + public void testCEFPolicyThresholdProcessorWithThresholdViolation() throws Exception { + + final String cefMessageString = fromStream(CEF_MESSAGE_WITH_THRESHOLD_VIOLATION_JSON_FILE_LOCATION); + final TCACEFProcessorContext tcacefProcessorContext = new TCACEFProcessorContext(cefMessageString, + getSampleTCAPolicy()); + + final EventListener eventListener = getCEFEventListener(); + tcacefProcessorContext.setCEFEventListener(eventListener); + + AbstractTCAECEFPolicyProcessor policyThresholdsProcessor = new TCACEFPolicyThresholdsProcessor(); + final TCACEFProcessorContext finalProcessorContext = policyThresholdsProcessor.apply(tcacefProcessorContext); + + assertTrue("Process Context can Processing Continue flag should be true", finalProcessorContext + .canProcessingContinue()); + assertThat("Policy Threshold Processor State must be successful", + policyThresholdsProcessor.getProcessingState(), is(ProcessingState.PROCESSING_FINISHED_SUCCESSFULLY)); + + } + +} diff --git a/dcae-analytics-tca/src/test/java/org/openecomp/dcae/apod/analytics/tca/processor/TCACEFProcessorContextTest.java b/dcae-analytics-tca/src/test/java/org/openecomp/dcae/apod/analytics/tca/processor/TCACEFProcessorContextTest.java new file mode 100644 index 0000000..ff044bc --- /dev/null +++ b/dcae-analytics-tca/src/test/java/org/openecomp/dcae/apod/analytics/tca/processor/TCACEFProcessorContextTest.java @@ -0,0 +1,38 @@ +/* + * ============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.tca.processor; + +import org.junit.Test; +import org.openecomp.dcae.apod.analytics.tca.BaseAnalyticsTCAUnitTest; + +/** + * + * @author Rajiv Singla. Creation Date: 11/14/2016. + */ +public class TCACEFProcessorContextTest extends BaseAnalyticsTCAUnitTest { + + @Test + public void testProcessorContextSerialization() throws Exception { + TCACEFProcessorContext tcacefProcessorContext = new TCACEFProcessorContext(getValidCEFMessage(), + getSampleTCAPolicy()); + testSerialization(tcacefProcessorContext, TCACEFProcessorContextTest.class); + } +} diff --git a/dcae-analytics-tca/src/test/java/org/openecomp/dcae/apod/analytics/tca/settings/TCATestAppConfig.java b/dcae-analytics-tca/src/test/java/org/openecomp/dcae/apod/analytics/tca/settings/TCATestAppConfig.java new file mode 100644 index 0000000..b8718c3 --- /dev/null +++ b/dcae-analytics-tca/src/test/java/org/openecomp/dcae/apod/analytics/tca/settings/TCATestAppConfig.java @@ -0,0 +1,38 @@ +/* + * ============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.tca.settings; + +/** + * TCA Test App Config is used for testing purposes only + * + * @author Rajiv Singla. Creation Date: 11/3/2016. + */ +public class TCATestAppConfig extends TCAAppConfig { + + public void setAppName(String appName) { + this.appName = appName; + } + + public void setAppDescription(String appDescription) { + this.appDescription = appDescription; + } + +} diff --git a/dcae-analytics-tca/src/test/java/org/openecomp/dcae/apod/analytics/tca/settings/TCATestAppConfigHolder.java b/dcae-analytics-tca/src/test/java/org/openecomp/dcae/apod/analytics/tca/settings/TCATestAppConfigHolder.java new file mode 100644 index 0000000..4007a7c --- /dev/null +++ b/dcae-analytics-tca/src/test/java/org/openecomp/dcae/apod/analytics/tca/settings/TCATestAppConfigHolder.java @@ -0,0 +1,40 @@ +/* + * ============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.tca.settings; + +/** + * Holder for TCA Test App Config + * + * @author Rajiv Singla. Creation Date: 11/3/2016. + */ +public class TCATestAppConfigHolder { + + private final TCATestAppConfig config; + + public TCATestAppConfigHolder(TCATestAppConfig config) { + this.config = config; + } + + public TCATestAppConfig getConfig() { + return config; + } + +} diff --git a/dcae-analytics-tca/src/test/java/org/openecomp/dcae/apod/analytics/tca/settings/TCATestAppPreferences.java b/dcae-analytics-tca/src/test/java/org/openecomp/dcae/apod/analytics/tca/settings/TCATestAppPreferences.java new file mode 100644 index 0000000..2218d63 --- /dev/null +++ b/dcae-analytics-tca/src/test/java/org/openecomp/dcae/apod/analytics/tca/settings/TCATestAppPreferences.java @@ -0,0 +1,136 @@ +/* + * ============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.tca.settings; + + +import com.fasterxml.jackson.annotation.JsonAnyGetter; + +import java.util.Map; + +/** + * TCA Test App Preferences are used for testing purposes only + * + * @author Rajiv Singla. Creation Date: 11/3/2016. + */ +public class TCATestAppPreferences extends TCAAppPreferences { + + private Map policyPreferences; + + public TCATestAppPreferences() { + } + + public TCATestAppPreferences(final Map policyPreferences) { + this.policyPreferences = policyPreferences; + } + + @JsonAnyGetter + public Map getPolicyPreferences() { + return policyPreferences; + } + + public void setSubscriberHostName(String subscriberHostName) { + this.subscriberHostName = subscriberHostName; + } + + public void setSubscriberHostPortNumber(Integer subscriberHostPort) { + this.subscriberHostPort = subscriberHostPort; + } + + public void setSubscriberTopicName(String subscriberTopicName) { + this.subscriberTopicName = subscriberTopicName; + } + + public void setSubscriberProtocol(String subscriberProtocol) { + this.subscriberProtocol = subscriberProtocol; + } + + public void setSubscriberUserName(String subscriberUserName) { + this.subscriberUserName = subscriberUserName; + } + + public void setSubscriberUserPassword(String subscriberUserPassword) { + this.subscriberUserPassword = subscriberUserPassword; + } + + public void setSubscriberContentType(String subscriberContentType) { + this.subscriberContentType = subscriberContentType; + } + + public void setSubscriberConsumerId(String subscriberConsumerId) { + this.subscriberConsumerId = subscriberConsumerId; + } + + public void setSubscriberConsumerGroup(String subscriberConsumerGroup) { + this.subscriberConsumerGroup = subscriberConsumerGroup; + } + + public void setSubscriberTimeoutMS(Integer subscriberTimeoutMS) { + this.subscriberTimeoutMS = subscriberTimeoutMS; + } + + public void setSubscriberMessageLimit(Integer subscriberMessageLimit) { + this.subscriberMessageLimit = subscriberMessageLimit; + } + + public void setSubscriberPollingInterval(Integer subscriberPollingInterval) { + this.subscriberPollingInterval = subscriberPollingInterval; + } + + public void setPublisherHostName(String publisherHostName) { + this.publisherHostName = publisherHostName; + } + + public void setPublisherHostPort(Integer publisherHostPort) { + this.publisherHostPort = publisherHostPort; + } + + public void setPublisherTopicName(String publisherTopicName) { + this.publisherTopicName = publisherTopicName; + } + + public void setPublisherProtocol(String publisherProtocol) { + this.publisherProtocol = publisherProtocol; + } + + public void setPublisherUserName(String publisherUserName) { + this.publisherUserName = publisherUserName; + } + + public void setPublisherUserPassword(String publisherUserPassword) { + this.publisherUserPassword = publisherUserPassword; + } + + public void setPublisherContentType(String publisherContentType) { + this.publisherContentType = publisherContentType; + } + + public void setPublisherMaxBatchSize(Integer publisherMaxBatchSize) { + this.publisherMaxBatchSize = publisherMaxBatchSize; + } + + public void setPublisherMaxRecoveryQueueSize(Integer publisherMaxRecoveryQueueSize) { + this.publisherMaxRecoveryQueueSize = publisherMaxRecoveryQueueSize; + } + + public void setPublisherPollingInterval(Integer publisherPollingInterval) { + this.publisherPollingInterval = publisherPollingInterval; + } +} diff --git a/dcae-analytics-tca/src/test/java/org/openecomp/dcae/apod/analytics/tca/utils/AppPreferencesToPublisherConfigMapperTest.java b/dcae-analytics-tca/src/test/java/org/openecomp/dcae/apod/analytics/tca/utils/AppPreferencesToPublisherConfigMapperTest.java new file mode 100644 index 0000000..7ae3cf8 --- /dev/null +++ b/dcae-analytics-tca/src/test/java/org/openecomp/dcae/apod/analytics/tca/utils/AppPreferencesToPublisherConfigMapperTest.java @@ -0,0 +1,58 @@ +/* + * ============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.tca.utils; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.runners.MockitoJUnitRunner; +import org.openecomp.dcae.apod.analytics.dmaap.domain.config.DMaaPMRPublisherConfig; +import org.openecomp.dcae.apod.analytics.tca.BaseAnalyticsTCAUnitTest; +import org.openecomp.dcae.apod.analytics.tca.settings.TCATestAppPreferences; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +/** + * @author Manjesh Gowda. Creation Date: 11/21/2016. + */ +@RunWith(MockitoJUnitRunner.class) +public class AppPreferencesToPublisherConfigMapperTest extends BaseAnalyticsTCAUnitTest { + @Test + public void testMapTCAConfigToSubscriberConfigFunctionGood() { + DMaaPMRPublisherConfig dMaaPMRPublisherConfig = + (new AppPreferencesToPublisherConfigMapper()).apply(getTCATestAppPreferences()); + assertEquals(dMaaPMRPublisherConfig.getHostName(), "PUBLISHER_HOST_NAME"); + } + + @Test + public void testMapTCAConfigToSubscriberConfigFunctionMap() { + DMaaPMRPublisherConfig dMaaPMRPublisherConfig = AppPreferencesToPublisherConfigMapper.map( + getTCATestAppPreferences()); + assertEquals(dMaaPMRPublisherConfig.getHostName(), "PUBLISHER_HOST_NAME"); + } + + @Test + public void testMapTCAConfigToSubscriberConfigFunctionAllNull() { + DMaaPMRPublisherConfig dMaaPMRPublisherConfig = + (new AppPreferencesToPublisherConfigMapper()).apply(new TCATestAppPreferences()); + assertNull(dMaaPMRPublisherConfig.getHostName()); + } +} diff --git a/dcae-analytics-tca/src/test/java/org/openecomp/dcae/apod/analytics/tca/utils/AppPreferencesToSubscriberConfigMapperTest.java b/dcae-analytics-tca/src/test/java/org/openecomp/dcae/apod/analytics/tca/utils/AppPreferencesToSubscriberConfigMapperTest.java new file mode 100644 index 0000000..7ff8d60 --- /dev/null +++ b/dcae-analytics-tca/src/test/java/org/openecomp/dcae/apod/analytics/tca/utils/AppPreferencesToSubscriberConfigMapperTest.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.tca.utils; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.runners.MockitoJUnitRunner; +import org.openecomp.dcae.apod.analytics.dmaap.domain.config.DMaaPMRSubscriberConfig; +import org.openecomp.dcae.apod.analytics.tca.BaseAnalyticsTCAUnitTest; +import org.openecomp.dcae.apod.analytics.tca.settings.TCATestAppPreferences; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +/** + * @author Manjesh Gowda. Creation Date: 11/21/2016. + */ +@RunWith(MockitoJUnitRunner.class) +public class AppPreferencesToSubscriberConfigMapperTest extends BaseAnalyticsTCAUnitTest { + + @Test + public void testMapTCAConfigToSubscriberConfigFunctionGood() { + DMaaPMRSubscriberConfig dMaaPMRSubscriberConfig = + (new AppPreferencesToSubscriberConfigMapper()).apply(getTCATestAppPreferences()); + assertEquals(dMaaPMRSubscriberConfig.getHostName(), "SUBSCRIBER_HOST_NAME"); + } + + @Test + public void testMapTCAConfigToSubscriberConfigFunctionMap() { + DMaaPMRSubscriberConfig dMaaPMRSubscriberConfig = + AppPreferencesToSubscriberConfigMapper.map(getTCATestAppPreferences()); + assertEquals(dMaaPMRSubscriberConfig.getHostName(), "SUBSCRIBER_HOST_NAME"); + } + + @Test + public void testMapTCAConfigToSubscriberConfigFunctionAllNull() { + DMaaPMRSubscriberConfig dMaaPMRSubscriberConfig = + (new AppPreferencesToSubscriberConfigMapper()).apply(new TCATestAppPreferences()); + assertNull(dMaaPMRSubscriberConfig.getHostName()); + } +} diff --git a/dcae-analytics-tca/src/test/java/org/openecomp/dcae/apod/analytics/tca/utils/TCAUtilsTest.java b/dcae-analytics-tca/src/test/java/org/openecomp/dcae/apod/analytics/tca/utils/TCAUtilsTest.java new file mode 100644 index 0000000..7fa961e --- /dev/null +++ b/dcae-analytics-tca/src/test/java/org/openecomp/dcae/apod/analytics/tca/utils/TCAUtilsTest.java @@ -0,0 +1,357 @@ +/* + * ============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.tca.utils; + +import co.cask.cdap.api.RuntimeContext; +import com.google.common.base.Supplier; +import com.google.common.collect.ImmutableSet; +import com.google.common.collect.Table; +import org.apache.commons.lang3.tuple.Pair; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.junit.runner.RunWith; +import org.mockito.Mockito; +import org.mockito.runners.MockitoJUnitRunner; +import org.openecomp.dcae.apod.analytics.common.exception.MessageProcessingException; +import org.openecomp.dcae.apod.analytics.model.domain.cef.CommonEventHeader; +import org.openecomp.dcae.apod.analytics.model.domain.cef.Event; +import org.openecomp.dcae.apod.analytics.model.domain.cef.EventListener; +import org.openecomp.dcae.apod.analytics.model.domain.cef.EventSeverity; +import org.openecomp.dcae.apod.analytics.model.domain.policy.tca.MetricsPerFunctionalRole; +import org.openecomp.dcae.apod.analytics.model.domain.policy.tca.TCAPolicy; +import org.openecomp.dcae.apod.analytics.model.domain.policy.tca.Threshold; +import org.openecomp.dcae.apod.analytics.model.facade.tca.TCAVESResponse; +import org.openecomp.dcae.apod.analytics.model.util.AnalyticsModelIOUtils; +import org.openecomp.dcae.apod.analytics.tca.BaseAnalyticsTCAUnitTest; +import org.openecomp.dcae.apod.analytics.tca.processor.TCACEFProcessorContext; +import org.openecomp.dcae.apod.analytics.tca.settings.TCAAppPreferences; + +import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.Properties; +import java.util.Set; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.CoreMatchers.isA; +import static org.hamcrest.Matchers.containsInAnyOrder; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +/** + * @author Rajiv Singla. Creation Date: 11/9/2016. + */ +@RunWith(MockitoJUnitRunner.class) +public class TCAUtilsTest extends BaseAnalyticsTCAUnitTest { + + @Test + public void testGetPolicyFunctionalRoles() throws Exception { + + final TCAPolicy sampleTCAPolicy = getSampleTCAPolicy(); + final List policyFunctionalRoles = TCAUtils.getPolicyFunctionalRoles(sampleTCAPolicy); + + assertThat("Policy Functional Roles must contain vFirewall and vLoadBalancer", policyFunctionalRoles, + containsInAnyOrder("vFirewall", "vLoadBalancer")); + } + + @Test + public void testGetPolicyFunctionalRoleSupplier() throws Exception { + final TCAPolicy sampleTCAPolicy = getSampleTCAPolicy(); + final Supplier> policyFunctionalRoleSupplier = TCAUtils.getPolicyFunctionalRoleSupplier + (sampleTCAPolicy); + final List policyFunctionalRoles = policyFunctionalRoleSupplier.get(); + assertThat("Policy Functional Roles must contain vFirewall and vLoadBalancer", policyFunctionalRoles, + containsInAnyOrder("vFirewall", "vLoadBalancer")); + } + + @Test + public void testProcessCEFMessage() throws Exception { + final String cefMessageString = fromStream(CEF_MESSAGE_JSON_FILE_LOCATION); + final TCACEFProcessorContext tcacefProcessorContext = TCAUtils.filterCEFMessage(cefMessageString, + getSampleTCAPolicy()); + assertThat("TCAECEFProcessor Processor Context can continue flag is true", tcacefProcessorContext + .canProcessingContinue(), is(true)); + } + + @Test + public void testGetPolicyFRThresholdsTableSupplier() throws Exception { + final Table> policyFRThresholdPathTable = TCAUtils + .getPolicyFRThresholdsTableSupplier(getSampleTCAPolicy()).get(); + + final Map> vFirewall = policyFRThresholdPathTable.row("vFirewall"); + final Map> vLoadBalancer = policyFRThresholdPathTable.row("vLoadBalancer"); + + final Set vFirewallThresholdPaths = vFirewall.keySet(); + final Set vLoadBalancerPaths = vLoadBalancer.keySet(); + + assertThat("vFirewall threshold field path size must be " + + "\"$.event.measurementsForVfScalingFields.vNicUsageArray[*].bytesIn\"", + vFirewallThresholdPaths.iterator().next(), + is("$.event.measurementsForVfScalingFields.vNicUsageArray[*].bytesIn")); + + assertThat("vLoadBalancer threshold field path size must be " + + "\"\"$.event.measurementsForVfScalingFields.vNicUsageArray[*].packetsIn\"", + vLoadBalancerPaths.iterator().next(), + is("$.event.measurementsForVfScalingFields.vNicUsageArray[*].packetsIn")); + + final List firewallThresholds = policyFRThresholdPathTable.get("vFirewall", + "$.event.measurementsForVfScalingFields.vNicUsageArray[*].bytesIn"); + final List vLoadBalancerThresholds = policyFRThresholdPathTable.get("vLoadBalancer", + "$.event.measurementsForVfScalingFields.vNicUsageArray[*].packetsIn"); + + assertThat("vFirewall Threshold size must be 2", firewallThresholds.size(), is(2)); + assertThat("vLoadBalancer Threshold size must be 2", vLoadBalancerThresholds.size(), is(2)); + } + + @Test + public void testGetJsonPathValueWithValidMessageAndPolicy() throws Exception { + final String cefMessageString = fromStream(CEF_MESSAGE_JSON_FILE_LOCATION); + final String jsonPath = "$.event.measurementsForVfScalingFields.vNicUsageArray[*].bytesIn"; + final ImmutableSet fieldPaths = ImmutableSet.of(jsonPath); + final Map> jsonPathValueMap = TCAUtils.getJsonPathValue(cefMessageString, fieldPaths); + assertThat("Json Path value must match", jsonPathValueMap.get(jsonPath).get(0), is(6086L)); + + } + + @Test + public void testGetJsonPathValueWithValidPath() throws Exception { + final String cefMessageString = fromStream(CEF_MESSAGE_JSON_FILE_LOCATION); + final String jsonPath = "$.event.measurementsForVfScalingFields.vNicUsageArray[*].invalid"; + final ImmutableSet fieldPaths = ImmutableSet.of(jsonPath); + final Map> jsonPathValueMap = TCAUtils.getJsonPathValue(cefMessageString, fieldPaths); + assertThat("Json path value must be empty", jsonPathValueMap.size(), is(0)); + + } + + @Test + public void testGetValidatedTCAAppPreferences() throws Exception { + RuntimeContext runtimeContext = mock(RuntimeContext.class); + when(runtimeContext.getRuntimeArguments()).thenReturn(getPreferenceMap()); + TCAAppPreferences validatedTCAAppPreferences = TCAUtils.getValidatedTCAAppPreferences(runtimeContext); + assertEquals(validatedTCAAppPreferences.getSubscriberHostName(), "mrlocal-mtnjftle01.homer.com"); + } + + @Test + public void testGetValidatedTCAAppPreferencesWhenDMaaPUrlArePresent() throws Exception { + RuntimeContext runtimeContext = mock(RuntimeContext.class); + final Map preferenceMap = getPreferenceMap(); + preferenceMap.put("dmaap.in.event-input.dmaapUrl", + "http://zldcmtd1njcoll00.research.com/unauthenticated.SEC_MEASUREMENT_OUTPUT"); + preferenceMap.put("dmaap.out.alert-output.dmaapUrl", + "http://zldcmtd1njcoll00.research.com/unauthenticated.TCA_EVENT_OUTPUT"); + + preferenceMap.put("dmaap.in.event-input.dmaapUserName", null); + preferenceMap.put("dmaap.in.event-input.dmaapPassword", null); + preferenceMap.put("dmaap.out.alert-output.dmaapUserName", null); + preferenceMap.put("dmaap.out.alert-output.dmaapPassword", null); + + when(runtimeContext.getRuntimeArguments()).thenReturn(preferenceMap); + TCAAppPreferences validatedTCAAppPreferences = TCAUtils.getValidatedTCAAppPreferences(runtimeContext); + + assertEquals(validatedTCAAppPreferences.getSubscriberProtocol(), "http"); + assertEquals(validatedTCAAppPreferences.getPublisherProtocol(), "http"); + + assertEquals(validatedTCAAppPreferences.getSubscriberHostName(), "zldcmtd1njcoll00.research.com"); + assertEquals(validatedTCAAppPreferences.getPublisherHostName(), "zldcmtd1njcoll00.research.com"); + + assertEquals(validatedTCAAppPreferences.getSubscriberHostPort(), new Integer(3904)); + assertEquals(validatedTCAAppPreferences.getPublisherHostPort(), new Integer(3904)); + + assertEquals(validatedTCAAppPreferences.getSubscriberTopicName(), "unauthenticated.SEC_MEASUREMENT_OUTPUT"); + assertEquals(validatedTCAAppPreferences.getPublisherTopicName(), "unauthenticated.TCA_EVENT_OUTPUT"); + + assertNull(validatedTCAAppPreferences.getSubscriberUserName()); + assertNull(validatedTCAAppPreferences.getSubscriberUserPassword()); + assertNull(validatedTCAAppPreferences.getPublisherUserName()); + assertNull(validatedTCAAppPreferences.getPublisherUserPassword()); + + } + + @Test + public void testCreateNewTCAVESResponse() throws Exception { + TCACEFProcessorContext tcacefProcessorContext = mock(TCACEFProcessorContext.class); + + MetricsPerFunctionalRole metricsPerFunctionalRole = mock(MetricsPerFunctionalRole.class); + when(metricsPerFunctionalRole.getThresholds()).thenReturn(getThresholds()); + when(metricsPerFunctionalRole.getPolicyScope()).thenReturn("Test Policy scope"); + when(tcacefProcessorContext.getMetricsPerFunctionalRole()).thenReturn(metricsPerFunctionalRole); + when(metricsPerFunctionalRole.getFunctionalRole()).thenReturn("vLoadBalancer"); + + when(tcacefProcessorContext.getCEFEventListener()).thenReturn(getCEFEventListener()); + TCAVESResponse tcaVESResponse = TCAUtils.createNewTCAVESResponse(tcacefProcessorContext, "TCA_APP_NAME"); + + //TODO : Add proper assertions, as the usage is not clearly understood + assertThat(tcaVESResponse.getClosedLoopControlName(), + is("CL-LBAL-LOW-TRAFFIC-SIG-FB480F95-A453-6F24-B767-FD703241AB1A")); + assertThat(tcaVESResponse.getVersion(), is("Test Version")); + assertThat(tcaVESResponse.getPolicyScope(), is("Test Policy scope")); + } + + @Rule + public ExpectedException expectedIllegalArgumentException = ExpectedException.none(); + + @Test + public void testCreateNewTCAVESResponseNullFunctionalRole() throws Exception { + expectedIllegalArgumentException.expect(MessageProcessingException.class); + expectedIllegalArgumentException.expectCause(isA(IllegalArgumentException.class)); + expectedIllegalArgumentException.expectMessage("No violations metrics. Unable to create VES Response"); + + TCACEFProcessorContext tcacefProcessorContext = mock(TCACEFProcessorContext.class); + TCAVESResponse tcaVESResponse = TCAUtils.createNewTCAVESResponse(tcacefProcessorContext, "TCA_APP_NAME"); + assertNotNull(tcaVESResponse.getClosedLoopControlName()); + } + + @Test + public void testPrioritizeThresholdViolations() throws Exception { + + Map thresholdMap = new HashMap<>(); + Threshold majorThreshold = mock(Threshold.class); + when(majorThreshold.getSeverity()).thenReturn(EventSeverity.MAJOR); + thresholdMap.put("MAJOR", majorThreshold); + + Threshold result1 = TCAUtils.prioritizeThresholdViolations(thresholdMap); + assertEquals(result1.getSeverity(), EventSeverity.MAJOR); + + Threshold criticalThreshold = mock(Threshold.class); + when(criticalThreshold.getSeverity()).thenReturn(EventSeverity.CRITICAL); + thresholdMap.put("CRITICAL", criticalThreshold); + + Threshold result2 = TCAUtils.prioritizeThresholdViolations(thresholdMap); + assertEquals(result2.getSeverity(), EventSeverity.CRITICAL); + } + + @Test + public void testCreateViolatedMetrics() throws Exception { + TCAPolicy tcaPolicy = getSampleTCAPolicy(); + Threshold violatedThreshold = getCriticalThreshold(); + String functionalRole = "vFirewall"; + MetricsPerFunctionalRole result = TCAUtils.createViolatedMetrics(tcaPolicy, violatedThreshold, functionalRole); + assertThat(result.getPolicyScope(), is("resource=vFirewall;type=configuration")); + assertThat(result.getPolicyName(), is("configuration.dcae.microservice.tca.xml")); + } + + @Test + public void testCreateViolatedMetricsWrongFunctionalRole() throws Exception { + expectedIllegalArgumentException.expect(MessageProcessingException.class); + expectedIllegalArgumentException.expectCause(isA(IllegalStateException.class)); + expectedIllegalArgumentException.expectMessage("TCA Policy must contain functional Role: badFunctionRoleName"); + + TCAPolicy tcaPolicy = getSampleTCAPolicy(); + Threshold violatedThreshold = getCriticalThreshold(); + String functionalRole = "badFunctionRoleName"; + MetricsPerFunctionalRole result = TCAUtils.createViolatedMetrics(tcaPolicy, violatedThreshold, functionalRole); + } + + @Test + public void testGetDomainAndFunctionalRole() { + TCACEFProcessorContext tcacefProcessorContext = mock(TCACEFProcessorContext.class); + EventListener eventListener = mock(EventListener.class); + Event event = mock(Event.class); + CommonEventHeader commonEventHeader = mock(CommonEventHeader.class); + + Pair result = TCAUtils.getDomainAndFunctionalRole(tcacefProcessorContext); + assertNull(result.getLeft()); + assertNull(result.getRight()); + + when(tcacefProcessorContext.getCEFEventListener()).thenReturn(eventListener); + result = TCAUtils.getDomainAndFunctionalRole(tcacefProcessorContext); + assertNull(result.getLeft()); + assertNull(result.getRight()); + + when(eventListener.getEvent()).thenReturn(event); + result = TCAUtils.getDomainAndFunctionalRole(tcacefProcessorContext); + assertNull(result.getLeft()); + assertNull(result.getRight()); + + when(event.getCommonEventHeader()).thenReturn(commonEventHeader); + result = TCAUtils.getDomainAndFunctionalRole(tcacefProcessorContext); + assertNull(result.getLeft()); + assertNull(result.getRight()); + + when(commonEventHeader.getDomain()).thenReturn("testDomain"); + when(commonEventHeader.getFunctionalRole()).thenReturn("functionalRole"); + + result = TCAUtils.getDomainAndFunctionalRole(tcacefProcessorContext); + assertEquals(result.getLeft(), "testDomain"); + assertEquals(result.getRight(), "functionalRole"); + + } + + @Test + public void testComputeThresholdViolationsNotPresent() throws Exception { + TCACEFProcessorContext tcacefProcessorContext = mock(TCACEFProcessorContext.class); + when(tcacefProcessorContext.canProcessingContinue()).thenReturn(true); + when(tcacefProcessorContext.getMessage()).thenReturn(getValidCEFMessage()); + + when(tcacefProcessorContext.getTCAPolicy()).thenReturn(getSampleTCAPolicy()); + when(tcacefProcessorContext.getCEFEventListener()).thenReturn(getCEFEventListener()); + + TCACEFProcessorContext result = TCAUtils.computeThresholdViolations(tcacefProcessorContext); + assertNotNull(result); + verify(result, times(0)).setMetricsPerFunctionalRole(Mockito.any(MetricsPerFunctionalRole.class)); + } + + @Test + public void testComputeThresholdViolationsPresent() throws Exception { + TCACEFProcessorContext tcacefProcessorContext = mock(TCACEFProcessorContext.class); + when(tcacefProcessorContext.canProcessingContinue()).thenReturn(true); + final String cefMessageString = fromStream(CEF_MESSAGE_WITH_THRESHOLD_VIOLATION_JSON_FILE_LOCATION); + when(tcacefProcessorContext.getMessage()).thenReturn(cefMessageString); + + when(tcacefProcessorContext.getTCAPolicy()).thenReturn(getSampleTCAPolicy()); + when(tcacefProcessorContext.getCEFEventListener()).thenReturn(getCEFEventListener()); + + TCACEFProcessorContext result = TCAUtils.computeThresholdViolations(tcacefProcessorContext); + verify(result, times(1)).setMetricsPerFunctionalRole(Mockito.any(MetricsPerFunctionalRole.class)); + } + + + @Test + public void testConvertRuntimeContextToTCAPolicy() throws Exception { + + final Properties controllerProperties = + AnalyticsModelIOUtils.loadPropertiesFile(TCA_CONTROLLER_POLICY_FILE_LOCATION); + + Map runtimeArgs = new LinkedHashMap<>(); + for (Map.Entry property : controllerProperties.entrySet()) { + runtimeArgs.put(property.getKey().toString(), property.getValue().toString()); + } + + RuntimeContext runtimeContext = mock(RuntimeContext.class); + when(runtimeContext.getRuntimeArguments()).thenReturn(runtimeArgs); + final TCAPolicy tcaPolicy = TCAUtils.getValidatedTCAPolicyPreferences(runtimeContext); + + assertThat("Policy Domain must be measurementsForVfScaling", + tcaPolicy.getDomain(), is("measurementsForVfScaling")); + + assertThat("Policy must have 2 metrics per functional roles", + tcaPolicy.getMetricsPerFunctionalRole().size(), is(2)); + + } +} diff --git a/dcae-analytics-tca/src/test/java/org/openecomp/dcae/apod/analytics/tca/worker/TCADMaaPMRSubscriberJobTest.java b/dcae-analytics-tca/src/test/java/org/openecomp/dcae/apod/analytics/tca/worker/TCADMaaPMRSubscriberJobTest.java new file mode 100644 index 0000000..453f195 --- /dev/null +++ b/dcae-analytics-tca/src/test/java/org/openecomp/dcae/apod/analytics/tca/worker/TCADMaaPMRSubscriberJobTest.java @@ -0,0 +1,80 @@ +/* + * ============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.tca.worker; + +import co.cask.cdap.api.metrics.Metrics; +import co.cask.cdap.api.worker.WorkerConfigurer; +import co.cask.cdap.api.worker.WorkerContext; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mockito; +import org.mockito.runners.MockitoJUnitRunner; +import org.openecomp.dcae.apod.analytics.common.AnalyticsConstants; +import org.openecomp.dcae.apod.analytics.dmaap.service.subscriber.DMaaPMRSubscriber; +import org.openecomp.dcae.apod.analytics.tca.BaseAnalyticsTCAUnitTest; +import org.quartz.JobDataMap; +import org.quartz.JobExecutionContext; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +/** + * @author Manjesh Gowda. Creation Date: 11/18/2016. + */ +@RunWith(MockitoJUnitRunner.class) +public class TCADMaaPMRSubscriberJobTest extends BaseAnalyticsTCAUnitTest { + + @Test + public void testExecute() throws Exception { + + DMaaPMRSubscriber mockDMaaPMRSubscriber = mock(DMaaPMRSubscriber.class); + Metrics mockMetrics = mock(Metrics.class); + + WorkerContext workerContext = mock(WorkerContext.class); + WorkerConfigurer workerConfigurer = mock(WorkerConfigurer.class); + //when(workerContext.getRuntimeArguments()).thenReturn(getPreferenceMap()); + + JobExecutionContext mockJobExecutionContext = mock(JobExecutionContext.class); + JobDataMap mockJobDataMap = mock(JobDataMap.class); + when(mockJobExecutionContext.getMergedJobDataMap()).thenReturn(mockJobDataMap); + + /*when(mockJobDataMap.getString(AnalyticsConstants.CDAP_STREAM_VARIABLE_NAME)) + .thenReturn(CDAPComponentsConstants.TCA_FIXED_SUBSCRIBER_OUTPUT_NAME_STREAM);*/ + + when(mockJobDataMap.get(AnalyticsConstants.WORKER_CONTEXT_VARIABLE_NAME)) + .thenReturn(workerContext); + + when(mockJobDataMap.get(AnalyticsConstants.DMAAP_SUBSCRIBER_VARIABLE_NAME)) + .thenReturn(mockDMaaPMRSubscriber); + + /*when(mockJobDataMap.get(AnalyticsConstants.DMAAP_SUBSCRIBER_METRICS_VARIABLE_NAME)) + .thenReturn(mockMetrics);*/ + + TCADMaaPMRSubscriberJob tcaDMaaPMRSubscriberJob = new TCADMaaPMRSubscriberJob(); + tcaDMaaPMRSubscriberJob.execute(mockJobExecutionContext); + + verify(mockJobDataMap, times(1)).getString(Mockito.anyString()); + verify(mockJobDataMap, times(3)).get(any()); + } +} -- cgit 1.2.3-korg