2 * ================================================================================
3 * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 * ============LICENSE_END=========================================================
20 package org.onap.dcae.analytics.tca.core.util.function.calculation;
22 import java.util.Comparator;
23 import java.util.List;
24 import java.util.stream.Collectors;
25 import java.util.stream.Stream;
27 import org.onap.dcae.analytics.model.TcaModelConstants;
28 import org.onap.dcae.analytics.model.cef.EventListener;
29 import org.onap.dcae.analytics.tca.core.exception.TcaProcessingException;
30 import org.onap.dcae.analytics.tca.core.service.TcaAbatementContext;
31 import org.onap.dcae.analytics.tca.core.service.TcaAbatementEntity;
32 import org.onap.dcae.analytics.tca.core.service.TcaAbatementRepository;
33 import org.onap.dcae.analytics.tca.core.service.TcaExecutionContext;
34 import org.onap.dcae.analytics.tca.core.service.TcaProcessingContext;
35 import org.onap.dcae.analytics.tca.core.service.TcaResultContext;
36 import org.onap.dcae.analytics.tca.model.policy.ClosedLoopEventStatus;
37 import org.onap.dcae.analytics.tca.model.policy.MetricsPerEventName;
38 import org.onap.dcae.analytics.tca.model.policy.Threshold;
39 import org.onap.dcae.utils.eelf.logger.api.log.EELFLogFactory;
40 import org.onap.dcae.utils.eelf.logger.api.log.EELFLogger;
41 import org.onap.dcae.utils.eelf.logger.model.info.RequestIdLogInfoImpl;
42 import org.onap.dcae.utils.eelf.logger.model.spec.DebugLogSpecImpl;
45 * @author Rajiv Singla
47 public class TcaAbatementCalculator implements TcaCalculationFunction {
49 private static final EELFLogger logger = EELFLogFactory.getLogger(TcaAbatementCalculator.class);
52 public TcaExecutionContext calculate(final TcaExecutionContext tcaExecutionContext) {
54 final TcaAbatementContext abatementContext = tcaExecutionContext.getTcaAbatementContext();
55 final TcaAbatementRepository abatementPersistenceContext = abatementContext
56 .getTcaAbatementRepository();
57 final TcaResultContext resultContext = tcaExecutionContext.getTcaResultContext();
58 final TcaProcessingContext processingContext = tcaExecutionContext.getTcaProcessingContext();
61 // Skip Abatement processing - if it is not enabled
62 if (!abatementContext.isAbatementEnabled() ||
63 // Skip if no threshold violations are present
64 !resultContext.isThresholdViolationsPresent()) {
65 return tcaExecutionContext;
69 final EventListener eventListener = processingContext.getEventListener();
70 final MetricsPerEventName violatedMetricsPerEventName = resultContext.getViolatedMetricsPerEventName();
71 final Threshold violatedThreshold = violatedMetricsPerEventName.getThresholds().get(0);
72 final ClosedLoopEventStatus closedLoopEventStatus = violatedThreshold.getClosedLoopEventStatus();
73 final String requestId = tcaExecutionContext.getRequestId();
74 final String lookupKey = createLookupKey(eventListener, violatedMetricsPerEventName);
75 final RequestIdLogInfoImpl requestIdLogInfo = new RequestIdLogInfoImpl(requestId);
76 final DebugLogSpecImpl debugLogSpec = new DebugLogSpecImpl(requestIdLogInfo);
77 switch (closedLoopEventStatus) {
79 // ONSET - save alert info in database so that next abated event can fetch its request id for abated
83 final TcaAbatementEntity tcaAbatementEntity =
84 abatementContext.create(lookupKey, requestId, false);
85 logger.debugLog().debug("Request Id: {}. Alert ClosedLoop Status is ONSET. " +
86 "Saving abatement Entity to repository with lookupKey: {}",
87 debugLogSpec, requestId, tcaAbatementEntity.getLookupKey());
88 abatementPersistenceContext.save(tcaAbatementEntity);
89 return tcaExecutionContext;
91 // ABATED - look up previous saved request id from db
94 final List<TcaAbatementEntity> previousTcaAbatementEntities =
95 abatementPersistenceContext.findByLookupKey(lookupKey);
97 // if previous abatement are indeed present - sort them my last modified date and get latest entity
98 if (previousTcaAbatementEntities != null && !previousTcaAbatementEntities.isEmpty()) {
99 previousTcaAbatementEntities.sort(
100 Comparator.comparing(TcaAbatementEntity::getLastModificationDate));
101 final TcaAbatementEntity previousTcaAbatementEntity =
102 previousTcaAbatementEntities.get(previousTcaAbatementEntities.size() - 1);
104 logger.debugLog().debug("Request Id: {}. Found previous Abatement Entity with lookupKey: {}",
105 debugLogSpec, requestId, previousTcaAbatementEntity.getLookupKey());
107 // previous abatement entity was found - but it was already sent before - so ignore alert creation
108 if (previousTcaAbatementEntity.isAbatementAlertSent()) {
109 final String terminatingMessage = "Abatement alert was already sent before on: " +
110 previousTcaAbatementEntity.getLastModificationDate();
111 setTerminatingMessage(terminatingMessage, tcaExecutionContext, false);
112 return tcaExecutionContext;
115 // no previous abatement was sent
116 final String previousRequestId = previousTcaAbatementEntity.getRequestId();
117 // set abated alert request id to previous ONSET alert request id
118 logger.debugLog().debug("Request Id: {}. No previous abated alert was sent. Setting previous request id: {}",
119 debugLogSpec, requestId, previousRequestId);
120 resultContext.setPreviousRequestId(previousRequestId);
121 // save new entity with alert as sent
122 final TcaAbatementEntity newTcaAbatementEntity =
123 abatementContext.create(lookupKey, previousRequestId, true);
124 logger.debugLog().debug("Request Id: {}. Saving new entity with alert as sent with lookupKey: {}",
125 debugLogSpec, requestId, newTcaAbatementEntity.getLookupKey());
126 abatementPersistenceContext.save(newTcaAbatementEntity);
127 return tcaExecutionContext;
130 // no previous onset event found
131 final String terminatingMessage =
132 "Ignored orphan Abated Message. No previous ONSET event found for lookup key: " + lookupKey;
133 setTerminatingMessage(terminatingMessage, tcaExecutionContext, false);
134 return tcaExecutionContext;
137 // Only ONSET and ABATED closed loop status are supported
139 final String errorMessage = String.format(
140 "Request Id: %s. Unexpected ClosedLoopEventStatus: %s - Only ONSET and ABATED are supported.",
141 requestId, closedLoopEventStatus);
142 throw new TcaProcessingException(errorMessage, new IllegalStateException(errorMessage));
147 private static String createLookupKey(final EventListener eventListener,
148 final MetricsPerEventName violatedMetricsPerEventName) {
149 // no null check required as all are required fields
150 final String eventName = violatedMetricsPerEventName.getEventName();
151 final String sourceName = eventListener.getEvent().getCommonEventHeader().getSourceName();
152 final String reportingEntityName = eventListener.getEvent().getCommonEventHeader().getReportingEntityName();
153 // violated threshold will always be present
154 final Threshold violatedThreshold = violatedMetricsPerEventName.getThresholds().get(0);
155 final String closedLoopControlName = violatedThreshold.getClosedLoopControlName();
156 final String fieldPath = violatedThreshold.getFieldPath();
157 return Stream.of(eventName, sourceName, reportingEntityName, closedLoopControlName, fieldPath)
158 .collect(Collectors.joining(TcaModelConstants.TCA_ROW_KEY_DELIMITER));