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 / config / TcaMrConfig.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  * Copyright (c) 2021 Nokia 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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  *
20  */
21
22 package org.onap.dcae.analytics.tca.web.config;
23
24 import static org.onap.dcae.analytics.model.AnalyticsHttpConstants.REQUEST_ID_HEADER_KEY;
25 import static org.onap.dcae.analytics.model.AnalyticsHttpConstants.REQUEST_TRANSACTION_ID_HEADER_KEY;
26
27 import java.util.List;
28 import org.onap.dcae.analytics.model.AnalyticsProfile;
29 import org.onap.dcae.analytics.model.DmaapMrConstants;
30 import org.onap.dcae.analytics.tca.web.TcaAppProperties;
31 import org.onap.dcae.analytics.tca.web.domain.TcaPolicyWrapper;
32 import org.onap.dcae.analytics.tca.web.integration.TcaAlertTransformer;
33 import org.onap.dcae.analytics.tca.web.integration.TcaPublisherResponseHandler;
34 import org.onap.dcae.analytics.tca.web.service.TcaProcessingService;
35 import org.onap.dcae.analytics.tca.web.util.function.TcaAppPropsToMrPublisherPrefsFunction;
36 import org.onap.dcae.analytics.tca.web.util.function.TcaAppPropsToMrSubscriberPrefsFunction;
37 import org.onap.dcae.analytics.web.dmaap.MrPublisherPreferences;
38 import org.onap.dcae.analytics.web.dmaap.MrSubscriberPreferences;
39 import org.springframework.context.annotation.Bean;
40 import org.springframework.context.annotation.Configuration;
41 import org.springframework.context.annotation.Profile;
42 import org.springframework.integration.channel.DirectChannel;
43 import org.springframework.integration.channel.NullChannel;
44 import org.springframework.integration.channel.QueueChannel;
45 import org.springframework.integration.dsl.IntegrationFlow;
46 import org.springframework.integration.dsl.IntegrationFlows;
47 import org.springframework.integration.handler.GenericHandler;
48 import org.springframework.messaging.MessageHeaders;
49
50 /**
51  * @author Rajiv Singla
52  */
53 @Profile({AnalyticsProfile.DMAAP_PROFILE_NAME})
54 @Configuration
55 public class TcaMrConfig {
56
57     @Bean
58     public MrSubscriberPreferences mrSubscriberPreferences(final TcaAppProperties tcaAppProperties) {
59         return new TcaAppPropsToMrSubscriberPrefsFunction().apply(tcaAppProperties);
60     }
61
62     @Bean
63     public MrPublisherPreferences mrPublisherPreferences(final TcaAppProperties tcaAppProperties) {
64         return new TcaAppPropsToMrPublisherPrefsFunction().apply(tcaAppProperties);
65     }
66
67     @Bean
68     public Integer processingBatchSize(final TcaAppProperties tcaAppProperties) {
69         return tcaAppProperties.getTca().getProcessingBatchSize();
70     }
71
72     @Bean
73     public TcaAlertTransformer tcaAlertTransformer(final TcaAppProperties tcaAppProperties) {
74         return new TcaAlertTransformer(tcaAppProperties);
75     }
76
77     @Bean
78     public IntegrationFlow tcaMrFlow(final TcaPolicyWrapper tcaPolicyWrapper,
79                                      final QueueChannel mrSubscriberOutputChannel,
80                                      final DirectChannel mrPublisherInputChannel,
81                                      final TcaProcessingService tcaProcessingService,
82                                      final TcaAlertTransformer tcaAlertTransformer) {
83         // get messages from dmaap subscriber channel
84         return IntegrationFlows.from(mrSubscriberOutputChannel)
85                 // handle incoming message from dmaap
86                 .handle(new GenericHandler<List<String>>() {
87                     @Override
88                     public Object handle(List<String> cefMessages, MessageHeaders headers) {
89                         return tcaProcessingService.getTcaExecutionResults(
90                                 headers.getOrDefault(REQUEST_ID_HEADER_KEY, headers.get(MessageHeaders.ID)).toString(),
91                                 headers.getOrDefault(REQUEST_TRANSACTION_ID_HEADER_KEY, "").toString(),
92                                 tcaPolicyWrapper, cefMessages);
93                     }
94                 })
95                 // transform tca execution results to alerts - if not alerts are detected terminate further processing
96                 .transform(tcaAlertTransformer, c -> c.requiresReply(false))
97                 // post messages to dmaap publisher input channel
98                 .channel(mrPublisherInputChannel)
99                 .get();
100     }
101
102
103     @Bean
104     public TcaPublisherResponseHandler tcaPublisherResponseHandler(final TcaAppProperties tcaAppProperties) {
105         return new TcaPublisherResponseHandler(tcaAppProperties);
106     }
107
108     @Bean
109     public IntegrationFlow tcaPublisherResponseFlow(final TcaPublisherResponseHandler tcaPublisherResponseHandler) {
110         return IntegrationFlows.from(DmaapMrConstants.DMAAP_MR_PUBLISHER_OUTPUT_CHANNEL)
111                 // log response from dmaap publisher output channel
112                 .handle(tcaPublisherResponseHandler)
113                 // finish processing
114                 .channel(new NullChannel())
115                 .get();
116     }
117
118 }