TCA: Support for VES/A&AI enrichment
[dcaegen2/analytics/tca.git] / dcae-analytics-cdap-tca / src / main / java / org / openecomp / dcae / apod / analytics / cdap / tca / flowlet / TCAVESThresholdViolationCalculatorFlowlet.java
index a3a35cf..f895d70 100644 (file)
-/*
- * ===============================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.cdap.tca.flowlet;
-
-import co.cask.cdap.api.annotation.HashPartition;
-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.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.cdap.common.CDAPComponentsConstants;
-import org.openecomp.dcae.apod.analytics.cdap.common.CDAPMetricsConstants;
-import org.openecomp.dcae.apod.analytics.cdap.common.domain.tca.ThresholdCalculatorOutput;
-import org.openecomp.dcae.apod.analytics.cdap.common.persistance.tca.TCACalculatorMessageType;
-import org.openecomp.dcae.apod.analytics.cdap.common.persistance.tca.TCAMessageStatusEntity;
-import org.openecomp.dcae.apod.analytics.cdap.tca.settings.TCAAppPreferences;
-import org.openecomp.dcae.apod.analytics.cdap.tca.utils.CDAPTCAUtils;
-import org.openecomp.dcae.apod.analytics.common.AnalyticsConstants;
-import org.openecomp.dcae.apod.analytics.model.domain.policy.tca.TCAPolicy;
-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.cdap.common.persistance.tca.TCAMessageStatusPersister.persist;
-
-/**
- * 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)
-    protected OutputEmitter<ThresholdCalculatorOutput> tcaAlertOutputEmitter;
-    protected Metrics metrics;
-    private ObjectMappedTable<TCAMessageStatusEntity> vesMessageStatusTable;
-
-    @Property
-    private final String messageStatusTableName;
-    private Boolean enableAlertCEFFormat;
-
-    private TCAPolicy tcaPolicy;
-
-    /**
-     * Creates an instance of TCA VES Threshold violation calculator flowlet with give message status table name
-     *
-     * @param messageStatusTableName message status table name
-     */
-    public TCAVESThresholdViolationCalculatorFlowlet(String messageStatusTableName) {
-        this.messageStatusTableName = messageStatusTableName;
-    }
-
-    @Override
-    public void configure() {
-        setName(CDAPComponentsConstants.TCA_FIXED_VES_THRESHOLD_VIOLATION_CALCULATOR_NAME_FLOWLET);
-        setDescription(CDAPComponentsConstants.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 = CDAPTCAUtils.getValidatedTCAPolicyPreferences(flowletContext);
-        // Parse runtime arguments
-        final TCAAppPreferences tcaAppPreferences = CDAPTCAUtils.getValidatedTCAAppPreferences(flowletContext);
-        enableAlertCEFFormat = tcaAppPreferences.getEnableAlertCEFFormat();
-        vesMessageStatusTable = getContext().getDataset(messageStatusTableName);
-
-    }
-
-    /**
-     * Filters VES Messages that violates TCA Policy
-     *
-     * @param vesMessage VES Message
-     * @throws JsonProcessingException if alert message cannot be parsed into JSON object
-     */
-    @ProcessInput(CDAPComponentsConstants.TCA_FIXED_VES_MESSAGE_ROUTER_OUTPUT)
-    @HashPartition(AnalyticsConstants.TCA_VES_MESSAGE_ROUTER_PARTITION_KEY)
-    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();
-                alertMessage =
-                        TCAUtils.createTCAAlertString(processorContextWithViolations, tcaAppName, enableAlertCEFFormat);
-                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
-                final ThresholdCalculatorOutput thresholdCalculatorOutput =
-                        new ThresholdCalculatorOutput(processorContext.getMessage(),
-                                TCAUtils.writeValueAsString(processorContext.getTCAPolicy()),
-                                TCAUtils.writeValueAsString(processorContextWithViolations.getMetricsPerEventName()),
-                                alertMessage);
-                tcaAlertOutputEmitter.emit(thresholdCalculatorOutput);
-
-            } 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
-        final int instanceId = getContext().getInstanceId();
-        persist(processorContext, instanceId, calculatorMessageType, vesMessageStatusTable, alertMessage);
-    }
-
-
-}
+/*\r
+ * ===============================LICENSE_START======================================\r
+ *  dcae-analytics\r
+ * ================================================================================\r
+ *    Copyright © 2017 AT&T Intellectual Property. All rights reserved.\r
+ * ================================================================================\r
+ *  Licensed under the Apache License, Version 2.0 (the "License");\r
+ *  you may not use this file except in compliance with the License.\r
+ *   You may obtain a copy of the License at\r
+ *\r
+ *          http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ *  Unless required by applicable law or agreed to in writing, software\r
+ *  distributed under the License is distributed on an "AS IS" BASIS,\r
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ *  See the License for the specific language governing permissions and\r
+ *  limitations under the License.\r
+ *  ============================LICENSE_END===========================================\r
+ */\r
+\r
+package org.openecomp.dcae.apod.analytics.cdap.tca.flowlet;\r
+\r
+import co.cask.cdap.api.annotation.HashPartition;\r
+import co.cask.cdap.api.annotation.Output;\r
+import co.cask.cdap.api.annotation.ProcessInput;\r
+import co.cask.cdap.api.annotation.Property;\r
+import co.cask.cdap.api.dataset.lib.ObjectMappedTable;\r
+import co.cask.cdap.api.flow.flowlet.AbstractFlowlet;\r
+import co.cask.cdap.api.flow.flowlet.FlowletContext;\r
+import co.cask.cdap.api.flow.flowlet.OutputEmitter;\r
+import co.cask.cdap.api.metrics.Metrics;\r
+import com.fasterxml.jackson.core.JsonProcessingException;\r
+import org.openecomp.dcae.apod.analytics.cdap.common.CDAPComponentsConstants;\r
+import org.openecomp.dcae.apod.analytics.cdap.common.CDAPMetricsConstants;\r
+import org.openecomp.dcae.apod.analytics.cdap.common.domain.tca.ThresholdCalculatorOutput;\r
+import org.openecomp.dcae.apod.analytics.cdap.common.persistance.tca.TCACalculatorMessageType;\r
+import org.openecomp.dcae.apod.analytics.cdap.common.persistance.tca.TCAMessageStatusEntity;\r
+import org.openecomp.dcae.apod.analytics.cdap.tca.settings.TCAAppPreferences;\r
+import org.openecomp.dcae.apod.analytics.cdap.tca.utils.CDAPTCAUtils;\r
+import org.openecomp.dcae.apod.analytics.common.AnalyticsConstants;\r
+import org.openecomp.dcae.apod.analytics.model.domain.policy.tca.TCAPolicy;\r
+import org.openecomp.dcae.apod.analytics.tca.processor.TCACEFProcessorContext;\r
+import org.openecomp.dcae.apod.analytics.tca.utils.TCAUtils;\r
+import org.slf4j.Logger;\r
+import org.slf4j.LoggerFactory;\r
+\r
+import static org.openecomp.dcae.apod.analytics.cdap.common.persistance.tca.TCAMessageStatusPersister.persist;\r
+\r
+/**\r
+ * TCA VES Message Filter filters out messages which are not applicable for TCA as per TCA Policy\r
+ *\r
+ * @author Rajiv Singla . Creation Date: 11/3/2016.\r
+ */\r
+public class TCAVESThresholdViolationCalculatorFlowlet extends AbstractFlowlet {\r
+\r
+    private static final Logger LOG = LoggerFactory.getLogger(TCAVESThresholdViolationCalculatorFlowlet.class);\r
+\r
+    @Output(CDAPComponentsConstants.TCA_FIXED_VES_TCA_CALCULATOR_NAME_OUTPUT)\r
+    protected OutputEmitter<ThresholdCalculatorOutput> tcaAlertOutputEmitter;\r
+    protected Metrics metrics;\r
+    private ObjectMappedTable<TCAMessageStatusEntity> vesMessageStatusTable;\r
+\r
+    @Property\r
+    private final String messageStatusTableName;\r
+    private Boolean enableAlertCEFFormat;\r
+\r
+    private TCAPolicy tcaPolicy;\r
+\r
+    /**\r
+     * Creates an instance of TCA VES Threshold violation calculator flowlet with give message status table name\r
+     *\r
+     * @param messageStatusTableName message status table name\r
+     */\r
+    public TCAVESThresholdViolationCalculatorFlowlet(String messageStatusTableName) {\r
+        this.messageStatusTableName = messageStatusTableName;\r
+    }\r
+\r
+    @Override\r
+    public void configure() {\r
+        setName(CDAPComponentsConstants.TCA_FIXED_VES_THRESHOLD_VIOLATION_CALCULATOR_NAME_FLOWLET);\r
+        setDescription(CDAPComponentsConstants.TCA_FIXED_VES_THRESHOLD_VIOLATION_CALCULATOR_DESCRIPTION_FLOWLET);\r
+    }\r
+\r
+\r
+    @Override\r
+    public void initialize(FlowletContext flowletContext) throws Exception {\r
+        super.initialize(flowletContext);\r
+\r
+        // parse Runtime Arguments to tca policy preferences\r
+        tcaPolicy = CDAPTCAUtils.getValidatedTCAPolicyPreferences(flowletContext);\r
+        // Parse runtime arguments\r
+        final TCAAppPreferences tcaAppPreferences = CDAPTCAUtils.getValidatedTCAAppPreferences(flowletContext);\r
+        enableAlertCEFFormat = tcaAppPreferences.getEnableAlertCEFFormat();\r
+        vesMessageStatusTable = getContext().getDataset(messageStatusTableName);\r
+\r
+    }\r
+\r
+    /**\r
+     * Filters VES Messages that violates TCA Policy\r
+     *\r
+     * @param vesMessage VES Message\r
+     * @throws JsonProcessingException if alert message cannot be parsed into JSON object\r
+     */\r
+    @ProcessInput(CDAPComponentsConstants.TCA_FIXED_VES_MESSAGE_ROUTER_OUTPUT)\r
+    @HashPartition(AnalyticsConstants.TCA_VES_MESSAGE_ROUTER_PARTITION_KEY)\r
+    public void filterVESMessages(String vesMessage) throws JsonProcessingException {\r
+\r
+        TCACalculatorMessageType calculatorMessageType = TCACalculatorMessageType.INAPPLICABLE;\r
+        String alertMessage = null;\r
+\r
+        // Step 1: Filter incoming messages\r
+        final TCACEFProcessorContext processorContext = TCAUtils.filterCEFMessage(vesMessage, tcaPolicy);\r
+\r
+        if (processorContext.canProcessingContinue()) {\r
+\r
+            // Step 2: Check if CEF Message violate any thresholds\r
+            final TCACEFProcessorContext processorContextWithViolations =\r
+                    TCAUtils.computeThresholdViolations(processorContext);\r
+\r
+            if (processorContextWithViolations.canProcessingContinue()) {\r
+\r
+                // Step 3: Create Alert Message\r
+                final String tcaAppName = getContext().getApplicationSpecification().getName();\r
+                alertMessage =\r
+                        TCAUtils.createTCAAlertString(processorContextWithViolations, tcaAppName, enableAlertCEFFormat);\r
+                calculatorMessageType = TCACalculatorMessageType.NON_COMPLIANT;\r
+                LOG.debug("VES Threshold Violation Detected. An alert message is be generated. {}", alertMessage);\r
+\r
+                metrics.count(CDAPMetricsConstants.TCA_VES_NON_COMPLIANT_MESSAGES_METRIC, 1);\r
+\r
+                // Step 4: Emit message to Alert Sink Flowlet\r
+                final ThresholdCalculatorOutput thresholdCalculatorOutput =\r
+                        new ThresholdCalculatorOutput(processorContext.getMessage(),\r
+                                TCAUtils.writeValueAsString(processorContext.getTCAPolicy()),\r
+                                TCAUtils.writeValueAsString(processorContextWithViolations.getMetricsPerEventName()),\r
+                                alertMessage);\r
+                tcaAlertOutputEmitter.emit(thresholdCalculatorOutput);\r
+\r
+            } else {\r
+\r
+                calculatorMessageType = TCACalculatorMessageType.COMPLIANT;\r
+                metrics.count(CDAPMetricsConstants.TCA_VES_COMPLIANT_MESSAGES_METRIC, 1);\r
+            }\r
+\r
+        } else {\r
+\r
+            metrics.count(CDAPMetricsConstants.TCA_VES_INAPPLICABLE_MESSAGES_METRIC, 1);\r
+        }\r
+\r
+        // save message to message status table\r
+        final int instanceId = getContext().getInstanceId();\r
+        persist(processorContext, instanceId, calculatorMessageType, vesMessageStatusTable, alertMessage);\r
+    }\r
+\r
+\r
+}\r