2 * ===============================LICENSE_START======================================
4 * ================================================================================
5 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============================LICENSE_END===========================================
21 package org.openecomp.dcae.apod.analytics.cdap.tca.flowlet;
23 import co.cask.cdap.api.annotation.Output;
24 import co.cask.cdap.api.annotation.ProcessInput;
25 import co.cask.cdap.api.annotation.Property;
26 import co.cask.cdap.api.annotation.RoundRobin;
27 import co.cask.cdap.api.dataset.lib.ObjectMappedTable;
28 import co.cask.cdap.api.flow.flowlet.AbstractFlowlet;
29 import co.cask.cdap.api.flow.flowlet.FlowletContext;
30 import co.cask.cdap.api.flow.flowlet.OutputEmitter;
31 import co.cask.cdap.api.metrics.Metrics;
32 import com.fasterxml.jackson.core.JsonProcessingException;
33 import org.openecomp.dcae.apod.analytics.cdap.common.CDAPComponentsConstants;
34 import org.openecomp.dcae.apod.analytics.cdap.common.CDAPMetricsConstants;
35 import org.openecomp.dcae.apod.analytics.cdap.common.persistance.tca.TCACalculatorMessageType;
36 import org.openecomp.dcae.apod.analytics.cdap.common.persistance.tca.TCAMessageStatusEntity;
37 import org.openecomp.dcae.apod.analytics.cdap.tca.settings.TCAAppPreferences;
38 import org.openecomp.dcae.apod.analytics.cdap.tca.utils.CDAPTCAUtils;
39 import org.openecomp.dcae.apod.analytics.model.domain.policy.tca.TCAPolicy;
40 import org.openecomp.dcae.apod.analytics.tca.processor.TCACEFProcessorContext;
41 import org.openecomp.dcae.apod.analytics.tca.utils.TCAUtils;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
45 import static org.openecomp.dcae.apod.analytics.cdap.common.persistance.tca.TCAMessageStatusPersister.persist;
48 * TCA VES Message Filter filters out messages which are not applicable for TCA as per TCA Policy
50 * @author Rajiv Singla . Creation Date: 11/3/2016.
52 public class TCAVESThresholdViolationCalculatorFlowlet extends AbstractFlowlet {
54 private static final Logger LOG = LoggerFactory.getLogger(TCAVESThresholdViolationCalculatorFlowlet.class);
56 @Output(CDAPComponentsConstants.TCA_FIXED_VES_TCA_CALCULATOR_NAME_OUTPUT)
57 protected OutputEmitter<String> tcaAlertOutputEmitter;
58 protected Metrics metrics;
59 private ObjectMappedTable<TCAMessageStatusEntity> vesMessageStatusTable;
62 private final String messageStatusTableName;
63 private Boolean enableAlertCEFFormat;
65 private TCAPolicy tcaPolicy;
68 * Creates an instance of TCA VES Threshold violation calculator flowlet with give message status table name
70 * @param messageStatusTableName message status table name
72 public TCAVESThresholdViolationCalculatorFlowlet(String messageStatusTableName) {
73 this.messageStatusTableName = messageStatusTableName;
77 public void configure() {
78 setName(CDAPComponentsConstants.TCA_FIXED_VES_THRESHOLD_VIOLATION_CALCULATOR_NAME_FLOWLET);
79 setDescription(CDAPComponentsConstants.TCA_FIXED_VES_THRESHOLD_VIOLATION_CALCULATOR_DESCRIPTION_FLOWLET);
84 public void initialize(FlowletContext flowletContext) throws Exception {
85 super.initialize(flowletContext);
87 // parse Runtime Arguments to tca policy preferences
88 tcaPolicy = CDAPTCAUtils.getValidatedTCAPolicyPreferences(flowletContext);
89 // Parse runtime arguments
90 final TCAAppPreferences tcaAppPreferences = CDAPTCAUtils.getValidatedTCAAppPreferences(flowletContext);
91 enableAlertCEFFormat = tcaAppPreferences.getEnableAlertCEFFormat();
92 vesMessageStatusTable = getContext().getDataset(messageStatusTableName);
97 * Filters VES Messages that violates TCA Policy
99 * @param vesMessage VES Message
100 * @throws JsonProcessingException if alert message cannot be parsed into JSON object
102 @ProcessInput(CDAPComponentsConstants.TCA_FIXED_VES_MESSAGE_ROUTER_OUTPUT)
104 public void filterVESMessages(String vesMessage) throws JsonProcessingException {
106 TCACalculatorMessageType calculatorMessageType = TCACalculatorMessageType.INAPPLICABLE;
107 String alertMessage = null;
109 // Step 1: Filter incoming messages
110 final TCACEFProcessorContext processorContext = TCAUtils.filterCEFMessage(vesMessage, tcaPolicy);
112 if (processorContext.canProcessingContinue()) {
114 // Step 2: Check if CEF Message violate any thresholds
115 final TCACEFProcessorContext processorContextWithViolations =
116 TCAUtils.computeThresholdViolations(processorContext);
118 if (processorContextWithViolations.canProcessingContinue()) {
120 // Step 3: Create Alert Message
121 final String tcaAppName = getContext().getApplicationSpecification().getName();
123 TCAUtils.createTCAAlertString(processorContextWithViolations, tcaAppName, enableAlertCEFFormat);
124 calculatorMessageType = TCACalculatorMessageType.NON_COMPLIANT;
125 LOG.debug("VES Threshold Violation Detected. An alert message is be generated. {}", alertMessage);
127 metrics.count(CDAPMetricsConstants.TCA_VES_NON_COMPLIANT_MESSAGES_METRIC, 1);
129 // Step 4: Emit message to Alert Sink Flowlet
130 tcaAlertOutputEmitter.emit(alertMessage);
134 calculatorMessageType = TCACalculatorMessageType.COMPLIANT;
135 metrics.count(CDAPMetricsConstants.TCA_VES_COMPLIANT_MESSAGES_METRIC, 1);
140 metrics.count(CDAPMetricsConstants.TCA_VES_INAPPLICABLE_MESSAGES_METRIC, 1);
143 // save message to message status table
144 final int instanceId = getContext().getInstanceId();
145 persist(processorContext, instanceId, calculatorMessageType, vesMessageStatusTable, alertMessage);