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.web.integration;
22 import java.util.Date;
23 import java.util.List;
24 import java.util.Objects;
25 import java.util.function.Predicate;
26 import java.util.stream.Collectors;
28 import org.onap.dcae.analytics.model.AnalyticsHttpConstants;
29 import org.onap.dcae.analytics.model.TcaModelConstants;
30 import org.onap.dcae.analytics.model.ecomplogger.AnalyticsErrorType;
31 import org.onap.dcae.analytics.tca.core.service.TcaExecutionContext;
32 import org.onap.dcae.analytics.tca.model.facade.TcaAlert;
33 import org.onap.dcae.analytics.tca.web.TcaAppProperties;
34 import org.onap.dcae.analytics.tca.web.util.TcaUtils;
35 import org.onap.dcae.analytics.web.util.AnalyticsHttpUtils;
36 import org.onap.dcae.utils.eelf.logger.api.info.ErrorLogInfo;
37 import org.onap.dcae.utils.eelf.logger.api.info.ResponseLogInfo;
38 import org.onap.dcae.utils.eelf.logger.api.info.TargetServiceLogInfo;
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.ErrorLogInfoImpl;
42 import org.onap.dcae.utils.eelf.logger.model.info.RequestIdLogInfoImpl;
43 import org.onap.dcae.utils.eelf.logger.model.info.RequestTimingLogInfoImpl;
44 import org.onap.dcae.utils.eelf.logger.model.info.ResponseLogInfoImpl;
45 import org.onap.dcae.utils.eelf.logger.model.info.TargetServiceLogInfoImpl;
46 import org.onap.dcae.utils.eelf.logger.model.spec.AuditLogSpecImpl;
47 import org.onap.dcae.utils.eelf.logger.model.spec.ErrorLogSpecImpl;
48 import org.springframework.integration.transformer.AbstractTransformer;
49 import org.springframework.messaging.Message;
52 * @author Rajiv Singla
54 public class TcaAlertTransformer extends AbstractTransformer {
56 private static final EELFLogger logger = EELFLogFactory.getLogger(TcaAlertTransformer.class);
58 private static final Predicate<TcaExecutionContext> ERROR_EXECUTION_CONTEXT_PREDICATE =
59 executionContext -> executionContext.getTcaProcessingContext().getErrorMessage() != null;
60 private static final Predicate<TcaExecutionContext> EARLY_TERMINATION_CONTEXT_PREDICATE =
61 tcaExecutionContext -> tcaExecutionContext.getTcaProcessingContext().getEarlyTerminationMessage() != null;
62 private static final Predicate<TcaExecutionContext> ABATED_EXECUTION_CONTEXT_PREDICATE =
63 tcaExecutionContext -> tcaExecutionContext.getTcaResultContext().getPreviousRequestId() != null;
65 private final TcaAppProperties tcaAppProperties;
67 public TcaAlertTransformer(final TcaAppProperties tcaAppProperties) {
68 this.tcaAppProperties = tcaAppProperties;
73 @SuppressWarnings("unchecked")
74 protected Object doTransform(final Message<?> message) throws Exception {
76 final Object messagePayload = message.getPayload();
78 final String requestId = AnalyticsHttpUtils.getRequestId(message.getHeaders());
79 final String transactionId = AnalyticsHttpUtils.getTransactionId(message.getHeaders());
80 final Date requestBeginTimestamp = AnalyticsHttpUtils.getTimestampFromHeaders(message.getHeaders(),
81 AnalyticsHttpConstants.REQUEST_BEGIN_TS_HEADER_KEY);
83 if (messagePayload instanceof List) {
84 // get execution contexts with alerts
85 final List<TcaExecutionContext> tcaExecutionContexts = (List<TcaExecutionContext>) messagePayload;
86 final List<TcaAlert> tcaAlerts =
87 tcaExecutionContexts.stream()
88 .map(e -> e.getTcaResultContext().getTcaAlert())
89 .filter(Objects::nonNull).collect(Collectors.toList());
92 if (tcaAppProperties.getTca().getEnableEcompLogging()) {
93 final List<TcaExecutionContext> errorExecutionContexts =
94 tcaExecutionContexts.stream().filter(ERROR_EXECUTION_CONTEXT_PREDICATE)
95 .collect(Collectors.toList());
96 createAuditLog(requestId, transactionId, requestBeginTimestamp, tcaExecutionContexts, tcaAlerts,
97 errorExecutionContexts);
98 createErrorLog(requestId, transactionId, errorExecutionContexts);
101 // if no alerts terminate further processing
102 return tcaAlerts.isEmpty() ? null : tcaAlerts;
109 private static void createErrorLog(final String requestId,
110 final String transactionId,
111 final List<TcaExecutionContext> errorExecutionContexts) {
112 // no error log generated - if there was no error during tca processing
113 if (!errorExecutionContexts.isEmpty()) {
114 for (TcaExecutionContext errorExecutionContext : errorExecutionContexts) {
115 final RequestIdLogInfoImpl requestIdLogInfo =
116 new RequestIdLogInfoImpl(errorExecutionContext.getRequestId());
117 final TargetServiceLogInfo targetServiceLogInfo = new TargetServiceLogInfoImpl(
118 "DCAE-TCA", TcaModelConstants.TCA_SERVICE_NAME, "");
119 final ErrorLogInfo errorLogInfo =
120 new ErrorLogInfoImpl(AnalyticsErrorType.SCHEMA_ERROR.getErrorCode(),
121 AnalyticsErrorType.SCHEMA_ERROR.getErrorDescription());
122 final ErrorLogSpecImpl errorLogSpec = new ErrorLogSpecImpl(requestIdLogInfo,
123 TcaUtils.TCA_SERVICE_LOG_INFO, targetServiceLogInfo, errorLogInfo);
124 logger.errorLog().error("Request Id: {}, Transaction Id: {}, Error Message: {} ",
125 errorLogSpec, requestId, transactionId, errorExecutionContext.getTcaProcessingContext()
131 private static void createAuditLog(final String requestId,
132 final String transactionId,
133 final Date requestBeginTimestamp,
134 final List<TcaExecutionContext> tcaExecutionContexts,
135 final List<TcaAlert> tcaAlerts,
136 final List<TcaExecutionContext> errorExecutionContexts) {
137 final List<TcaExecutionContext> earlyTerminationExecutionContexts =
138 tcaExecutionContexts.stream().filter(EARLY_TERMINATION_CONTEXT_PREDICATE)
139 .collect(Collectors.toList());
140 final List<TcaExecutionContext> abatedExecutionContexts =
141 tcaExecutionContexts.stream().filter(ABATED_EXECUTION_CONTEXT_PREDICATE)
142 .collect(Collectors.toList());
143 final RequestIdLogInfoImpl requestIdLogInfo = new RequestIdLogInfoImpl(requestId);
144 final Date endTimestamp = new Date();
145 final RequestTimingLogInfoImpl requestTimingLogInfo = new RequestTimingLogInfoImpl(requestBeginTimestamp,
146 endTimestamp, endTimestamp.getTime() - requestBeginTimestamp.getTime());
147 final ResponseLogInfo responseLogInfo =
148 new ResponseLogInfoImpl(AnalyticsErrorType.SUCCESSFUL.getErrorCode(),
149 AnalyticsErrorType.SUCCESSFUL.getErrorDescription());
150 final AuditLogSpecImpl auditLogSpec = new AuditLogSpecImpl(requestIdLogInfo, TcaUtils.TCA_SERVICE_LOG_INFO,
151 requestTimingLogInfo, responseLogInfo);
152 logger.auditLog().info("Request Id: {}, Transaction Id: {}, " +
153 "Message counts - Received: {}, Errors: {}, Terminated Early: {}, Abated: {}, Alerts: {}",
154 auditLogSpec, requestId, transactionId,
155 Integer.toString(tcaExecutionContexts.size()), Integer.toString(errorExecutionContexts.size()),
156 Integer.toString(earlyTerminationExecutionContexts.size()),
157 Integer.toString(abatedExecutionContexts.size()), Integer.toString(tcaAlerts.size()));