Standalone TCA with EELF Logger
[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  * ================================================================================
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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=========================================================
17  *
18  */
19
20 package org.onap.dcae.analytics.tca.web.config;
21
22 import static org.onap.dcae.analytics.model.AnalyticsHttpConstants.REQUEST_ID_HEADER_KEY;
23 import static org.onap.dcae.analytics.model.AnalyticsHttpConstants.REQUEST_TRANSACTION_ID_HEADER_KEY;
24
25 import java.util.List;
26 import java.util.Map;
27
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.messaging.MessageHeaders;
48
49 /**
50  * @author Rajiv Singla
51  */
52 @Profile({AnalyticsProfile.DMAAP_PROFILE_NAME})
53 @Configuration
54 public class TcaMrConfig {
55
56     @Bean
57     public MrSubscriberPreferences mrSubscriberPreferences(final TcaAppProperties tcaAppProperties) {
58         return new TcaAppPropsToMrSubscriberPrefsFunction().apply(tcaAppProperties);
59     }
60
61     @Bean
62     public MrPublisherPreferences mrPublisherPreferences(final TcaAppProperties tcaAppProperties) {
63         return new TcaAppPropsToMrPublisherPrefsFunction().apply(tcaAppProperties);
64     }
65
66     @Bean
67     public Integer processingBatchSize(final TcaAppProperties tcaAppProperties) {
68         return tcaAppProperties.getTca().getProcessingBatchSize();
69     }
70
71     @Bean
72     public TcaAlertTransformer tcaAlertTransformer(final TcaAppProperties tcaAppProperties) {
73         return new TcaAlertTransformer(tcaAppProperties);
74     }
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((List<String> cefMessages, Map<String, Object> headers) ->
87                         tcaProcessingService.getTcaExecutionResults(
88                                 headers.getOrDefault(REQUEST_ID_HEADER_KEY, headers.get(MessageHeaders.ID)).toString(),
89                                 headers.getOrDefault(REQUEST_TRANSACTION_ID_HEADER_KEY, "").toString(),
90                                 tcaPolicyWrapper.getTcaPolicy(), cefMessages))
91                 // transform tca execution results to alerts - if not alerts are detected terminate further processing
92                 .transform(tcaAlertTransformer, c -> c.requiresReply(false))
93                 // post messages to dmaap publisher input channel
94                 .channel(mrPublisherInputChannel)
95                 .get();
96     }
97
98
99     @Bean
100     public TcaPublisherResponseHandler tcaPublisherResponseHandler(final TcaAppProperties tcaAppProperties) {
101         return new TcaPublisherResponseHandler(tcaAppProperties);
102     }
103
104     @Bean
105     public IntegrationFlow tcaPublisherResponseFlow(final TcaPublisherResponseHandler tcaPublisherResponseHandler) {
106         return IntegrationFlows.from(DmaapMrConstants.DMAAP_MR_PUBLISHER_OUTPUT_CHANNEL)
107                 // log response from dmaap publisher output channel
108                 .handle(tcaPublisherResponseHandler)
109                 // finish processing
110                 .channel(new NullChannel())
111                 .get();
112     }
113
114 }