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.onap.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.dataset.lib.ObjectMappedTable;
27 import co.cask.cdap.api.flow.flowlet.AbstractFlowlet;
28 import co.cask.cdap.api.flow.flowlet.FlowletContext;
29 import co.cask.cdap.api.flow.flowlet.OutputEmitter;
30 import org.apache.commons.lang3.StringUtils;
31 import org.onap.dcae.apod.analytics.cdap.common.CDAPComponentsConstants;
32 import org.onap.dcae.apod.analytics.cdap.common.domain.tca.ThresholdCalculatorOutput;
33 import org.onap.dcae.apod.analytics.cdap.common.exception.CDAPSettingsException;
34 import org.onap.dcae.apod.analytics.cdap.common.persistance.tca.TCAAlertsAbatementEntity;
35 import org.onap.dcae.apod.analytics.cdap.common.persistance.tca.TCAAlertsAbatementPersister;
36 import org.onap.dcae.apod.analytics.model.domain.cef.EventListener;
37 import org.onap.dcae.apod.analytics.model.domain.policy.tca.ClosedLoopEventStatus;
38 import org.onap.dcae.apod.analytics.model.domain.policy.tca.MetricsPerEventName;
39 import org.onap.dcae.apod.analytics.model.domain.policy.tca.Threshold;
40 import org.onap.dcae.apod.analytics.model.facade.tca.TCAVESResponse;
41 import org.onap.dcae.apod.analytics.tca.utils.TCAUtils;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
45 import java.io.IOException;
46 import java.util.Date;
49 * Flowlet responsible to sending out abatement alerts
51 * @author Rajiv Singla . Creation Date: 9/11/2017.
53 public class TCAVESAlertsAbatementFlowlet extends AbstractFlowlet {
55 private static final Logger LOG = LoggerFactory.getLogger(TCAVESAlertsAbatementFlowlet.class);
58 private final String tcaAlertsAbatementTableName;
60 @Output(CDAPComponentsConstants.TCA_FIXED_VES_ALERTS_ABATEMENT_NAME_OUTPUT)
61 protected OutputEmitter<String> alertsAbatementOutputEmitter;
63 private ObjectMappedTable<TCAAlertsAbatementEntity> tcaAlertsAbatementTable;
65 public TCAVESAlertsAbatementFlowlet(final String tcaAlertsAbatementTableName) {
66 this.tcaAlertsAbatementTableName = tcaAlertsAbatementTableName;
70 public void configure() {
71 setName(CDAPComponentsConstants.TCA_FIXED_VES_ALERTS_ABATEMENT_NAME_FLOWLET);
72 setDescription(CDAPComponentsConstants.TCA_FIXED_VES_ALERTS_ABATEMENT_DESCRIPTION_FLOWLET);
76 public void initialize(FlowletContext flowletContext) throws Exception {
77 super.initialize(flowletContext);
78 tcaAlertsAbatementTable = getContext().getDataset(tcaAlertsAbatementTableName);
81 @ProcessInput(CDAPComponentsConstants.TCA_FIXED_VES_TCA_CALCULATOR_NAME_OUTPUT)
82 public void determineAbatementAlerts(final ThresholdCalculatorOutput thresholdCalculatorOutput) throws IOException {
84 final String cefMessage = thresholdCalculatorOutput.getCefMessage();
85 final String alertMessageString = thresholdCalculatorOutput.getAlertMessage();
86 final String violatedMetricsPerEventNameString = thresholdCalculatorOutput.getViolatedMetricsPerEventName();
88 // alerts must have violated metrics per event name present
89 if (StringUtils.isBlank(violatedMetricsPerEventNameString)) {
90 final String errorMessage = String.format(
91 "No violated metricsPerEventName found for VES Message: %s." +
92 "Ignored alert message: %s", cefMessage, alertMessageString);
93 throw new CDAPSettingsException(errorMessage, LOG, new IllegalStateException(errorMessage));
96 final MetricsPerEventName violatedMetricsPerEventName =
97 TCAUtils.readValue(violatedMetricsPerEventNameString, MetricsPerEventName.class);
98 final EventListener eventListener = TCAUtils.readValue(cefMessage, EventListener.class);
99 final TCAVESResponse tcavesResponse = TCAUtils.readValue(alertMessageString, TCAVESResponse.class);
100 final Threshold violatedThreshold = violatedMetricsPerEventName.getThresholds().get(0);
101 final ClosedLoopEventStatus closedLoopEventStatus = violatedThreshold.getClosedLoopEventStatus();
103 switch (closedLoopEventStatus) {
107 LOG.debug("Saving information for ONSET event for cefMessage: {}", cefMessage);
108 TCAAlertsAbatementPersister.persist(eventListener, violatedMetricsPerEventName, tcavesResponse,
109 null, tcaAlertsAbatementTable);
110 LOG.debug("Emitting ONSET alert: {}", alertMessageString);
111 alertsAbatementOutputEmitter.emit(alertMessageString);
116 LOG.debug("Looking up previous sent alert for abated threshold: {}", violatedThreshold);
117 final TCAAlertsAbatementEntity previousAlertsAbatementEntry =
118 TCAAlertsAbatementPersister.lookUpByKey(eventListener, violatedMetricsPerEventName,
119 tcaAlertsAbatementTable);
121 if (previousAlertsAbatementEntry != null) {
123 LOG.debug("Found previous AlertsAbatementEntity: {}", previousAlertsAbatementEntry);
125 final String abatementSentTS = previousAlertsAbatementEntry.getAbatementSentTS();
126 if (abatementSentTS != null) {
127 LOG.debug("Abatement alert was already sent at timestamp: {}. " +
128 "Skip resending this abatement alert again", abatementSentTS);
131 final long newAbatementSentTS = new Date().getTime();
133 "No abatement alert was sent before." +
134 "Sending abatement alert:{} for the first time at:{}",
135 alertMessageString, newAbatementSentTS);
137 // save new Abatement alert sent timestamp in table
138 TCAAlertsAbatementPersister.persist(eventListener, violatedMetricsPerEventName, tcavesResponse,
139 Long.toString(newAbatementSentTS), tcaAlertsAbatementTable);
141 // Set request id to be same as previous ONSET event request ID
142 tcavesResponse.setRequestID(previousAlertsAbatementEntry.getRequestId());
143 final String abatedAlertString = TCAUtils.writeValueAsString(tcavesResponse);
145 LOG.info("Emitting ABATED alert: {}", abatedAlertString);
146 alertsAbatementOutputEmitter.emit(abatedAlertString);
151 LOG.info("No previous ONSET alert was found for this ABATED alert: {}.Skip sending abated alert.",
159 final String errorMessage = String.format(
160 "Unexpected ClosedLoopEventStatus: %s. Only ONSET and ABATED are supported." +
161 "Ignoring alert: %s", closedLoopEventStatus, alertMessageString);
162 throw new CDAPSettingsException(errorMessage, LOG, new IllegalStateException(errorMessage));