Fix mongodb errors on application startup
[dcaegen2/analytics/tca-gen2.git] / dcae-analytics / dcae-analytics-tca-web / src / main / java / org / onap / dcae / analytics / tca / web / integration / TcaAlertTransformer.java
1 /*
2  * ================================================================================
3  * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
4  * Copyright (c) 2021 China Mobile Property. All rights reserved.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  * ============LICENSE_END=========================================================
18  *
19  */
20
21 package org.onap.dcae.analytics.tca.web.integration;
22
23 import java.util.Date;
24 import java.util.List;
25 import java.util.Objects;
26 import java.util.function.Predicate;
27 import java.util.stream.Collectors;
28
29 import org.onap.dcae.analytics.model.AnalyticsHttpConstants;
30 import org.onap.dcae.analytics.model.TcaModelConstants;
31 import org.onap.dcae.analytics.model.ecomplogger.AnalyticsErrorType;
32 import org.onap.dcae.analytics.tca.core.service.TcaExecutionContext;
33 import org.onap.dcae.analytics.tca.core.util.TcaUtils;
34 import org.onap.dcae.analytics.tca.model.facade.TcaAlert;
35 import org.onap.dcae.analytics.tca.web.TcaAppProperties;
36 import org.onap.dcae.analytics.web.util.AnalyticsHttpUtils;
37 import org.onap.dcae.utils.eelf.logger.api.info.ErrorLogInfo;
38 import org.onap.dcae.utils.eelf.logger.api.info.ResponseLogInfo;
39 import org.onap.dcae.utils.eelf.logger.api.info.TargetServiceLogInfo;
40 import org.onap.dcae.utils.eelf.logger.api.log.EELFLogFactory;
41 import org.onap.dcae.utils.eelf.logger.api.log.EELFLogger;
42 import org.onap.dcae.utils.eelf.logger.model.info.ErrorLogInfoImpl;
43 import org.onap.dcae.utils.eelf.logger.model.info.RequestIdLogInfoImpl;
44 import org.onap.dcae.utils.eelf.logger.model.info.RequestTimingLogInfoImpl;
45 import org.onap.dcae.utils.eelf.logger.model.info.ResponseLogInfoImpl;
46 import org.onap.dcae.utils.eelf.logger.model.info.TargetServiceLogInfoImpl;
47 import org.onap.dcae.utils.eelf.logger.model.spec.AuditLogSpecImpl;
48 import org.onap.dcae.utils.eelf.logger.model.spec.ErrorLogSpecImpl;
49 import org.springframework.integration.transformer.AbstractTransformer;
50 import org.springframework.messaging.Message;
51
52 /**
53  * @author Rajiv Singla
54  */
55 public class TcaAlertTransformer extends AbstractTransformer {
56
57     private static final EELFLogger logger = EELFLogFactory.getLogger(TcaAlertTransformer.class);
58
59     private static final Predicate<TcaExecutionContext> ERROR_EXECUTION_CONTEXT_PREDICATE =
60             executionContext -> executionContext.getTcaProcessingContext().getErrorMessage() != null;
61     private static final Predicate<TcaExecutionContext> EARLY_TERMINATION_CONTEXT_PREDICATE =
62             tcaExecutionContext -> tcaExecutionContext.getTcaProcessingContext().getEarlyTerminationMessage() != null;
63     private static final Predicate<TcaExecutionContext> ABATED_EXECUTION_CONTEXT_PREDICATE =
64             tcaExecutionContext -> tcaExecutionContext.getTcaResultContext().getPreviousRequestId() != null;
65
66     private final TcaAppProperties tcaAppProperties;
67
68     public TcaAlertTransformer(final TcaAppProperties tcaAppProperties) {
69         this.tcaAppProperties = tcaAppProperties;
70     }
71
72
73     @Override
74     @SuppressWarnings("unchecked")
75     protected Object doTransform(final Message<?> message) {
76
77         final Object messagePayload = message.getPayload();
78
79         final String requestId = AnalyticsHttpUtils.getRequestId(message.getHeaders());
80         final String transactionId = AnalyticsHttpUtils.getTransactionId(message.getHeaders());
81         final Date requestBeginTimestamp = AnalyticsHttpUtils.getTimestampFromHeaders(message.getHeaders(),
82                 AnalyticsHttpConstants.REQUEST_BEGIN_TS_HEADER_KEY);
83
84         if (messagePayload instanceof List) {
85             // get execution contexts with alerts
86             final List<TcaExecutionContext> tcaExecutionContexts = (List<TcaExecutionContext>) messagePayload;
87             final List<TcaAlert> tcaAlerts =
88                     tcaExecutionContexts.stream()
89                             .map(e -> e.getTcaResultContext().getTcaAlert())
90                             .filter(Objects::nonNull).collect(Collectors.toList());
91
92             // do ecomp logging
93             if (tcaAppProperties.getTca().getEnableEcompLogging()) {
94                 final List<TcaExecutionContext> errorExecutionContexts =
95                         tcaExecutionContexts.stream().filter(ERROR_EXECUTION_CONTEXT_PREDICATE)
96                                 .collect(Collectors.toList());
97                 createAuditLog(requestId, transactionId, requestBeginTimestamp, tcaExecutionContexts, tcaAlerts,
98                         errorExecutionContexts);
99                 createErrorLog(requestId, transactionId, errorExecutionContexts);
100             }
101
102             // if no alerts terminate further processing
103             return tcaAlerts.isEmpty() ? null : tcaAlerts;
104         } else {
105             return null;
106         }
107     }
108
109
110     private static void createErrorLog(final String requestId,
111                                        final String transactionId,
112                                        final List<TcaExecutionContext> errorExecutionContexts) {
113         // no error log generated - if there was no error during tca processing
114         if (!errorExecutionContexts.isEmpty()) {
115             for (TcaExecutionContext errorExecutionContext : errorExecutionContexts) {
116                 final RequestIdLogInfoImpl requestIdLogInfo =
117                         new RequestIdLogInfoImpl(errorExecutionContext.getRequestId());
118                 final TargetServiceLogInfo targetServiceLogInfo = new TargetServiceLogInfoImpl(
119                         "DCAE-TCA", TcaModelConstants.TCA_SERVICE_NAME, "");
120                 final ErrorLogInfo errorLogInfo =
121                         new ErrorLogInfoImpl(AnalyticsErrorType.SCHEMA_ERROR.getErrorCode(),
122                                 AnalyticsErrorType.SCHEMA_ERROR.getErrorDescription());
123                 final ErrorLogSpecImpl errorLogSpec = new ErrorLogSpecImpl(requestIdLogInfo,
124                         TcaUtils.TCA_SERVICE_LOG_INFO, targetServiceLogInfo, errorLogInfo);
125                 logger.errorLog().error("Request Id: {}, Transaction Id: {}, Error Message: {} ",
126                         errorLogSpec, requestId, transactionId, errorExecutionContext.getTcaProcessingContext()
127                                 .getErrorMessage());
128             }
129         }
130     }
131
132     private static void createAuditLog(final String requestId,
133                                        final String transactionId,
134                                        final Date requestBeginTimestamp,
135                                        final List<TcaExecutionContext> tcaExecutionContexts,
136                                        final List<TcaAlert> tcaAlerts,
137                                        final List<TcaExecutionContext> errorExecutionContexts) {
138         final List<TcaExecutionContext> earlyTerminationExecutionContexts =
139                 tcaExecutionContexts.stream().filter(EARLY_TERMINATION_CONTEXT_PREDICATE)
140                         .collect(Collectors.toList());
141         final List<TcaExecutionContext> abatedExecutionContexts =
142                 tcaExecutionContexts.stream().filter(ABATED_EXECUTION_CONTEXT_PREDICATE)
143                         .collect(Collectors.toList());
144         final RequestIdLogInfoImpl requestIdLogInfo = new RequestIdLogInfoImpl(requestId);
145         final Date endTimestamp = new Date();
146         final RequestTimingLogInfoImpl requestTimingLogInfo = new RequestTimingLogInfoImpl(requestBeginTimestamp,
147                 endTimestamp, endTimestamp.getTime() - requestBeginTimestamp.getTime());
148         final ResponseLogInfo responseLogInfo =
149                 new ResponseLogInfoImpl(AnalyticsErrorType.SUCCESSFUL.getErrorCode(),
150                         AnalyticsErrorType.SUCCESSFUL.getErrorDescription());
151         final AuditLogSpecImpl auditLogSpec = new AuditLogSpecImpl(requestIdLogInfo, TcaUtils.TCA_SERVICE_LOG_INFO,
152                 requestTimingLogInfo, responseLogInfo);
153         logger.auditLog().info("Request Id: {}, Transaction Id: {}, " +
154                         "Message counts - Received: {}, Errors: {}, Terminated Early: {}, Abated: {}, Alerts: {}, Alerts size: {}",
155                 auditLogSpec, requestId, transactionId,
156                 Integer.toString(tcaExecutionContexts.size()), Integer.toString(errorExecutionContexts.size()),
157                 Integer.toString(earlyTerminationExecutionContexts.size()),
158                 tcaAlerts.toString(),
159                 Integer.toString(abatedExecutionContexts.size()), Integer.toString(tcaAlerts.size()));
160     }
161 }
162