Standalone TCA with EELF Logger
[dcaegen2/analytics/tca-gen2.git] / dcae-analytics / dcae-analytics-tca-core / src / main / java / org / onap / dcae / analytics / tca / core / util / function / calculation / TcaEventListenerExtractor.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.core.util.function.calculation;
21
22 import java.util.Optional;
23
24 import org.onap.dcae.analytics.model.cef.EventListener;
25 import org.onap.dcae.analytics.model.util.json.AnalyticsModelJsonConversion;
26 import org.onap.dcae.analytics.tca.core.service.TcaExecutionContext;
27
28 /**
29  * Tca Event Listener Calculation Function converts incoming CEF string message
30  * to Java Object of type Event Listener
31  *
32  * @author Rajiv Singla
33  */
34 public class TcaEventListenerExtractor implements TcaCalculationFunction {
35
36     @Override
37     public TcaExecutionContext calculate(final TcaExecutionContext tcaExecutionContext) {
38
39         final String cefMessage = tcaExecutionContext.getCefMessage();
40
41         // Check blank message
42         if (cefMessage == null || cefMessage.isEmpty() || cefMessage.trim().isEmpty()) {
43             final String errorMessage = "Blank CEF message cannot be converted to CEF Event Listener Object";
44             setTerminatingMessage(errorMessage, tcaExecutionContext, true);
45             return tcaExecutionContext;
46         }
47
48
49         // Check CEF message is a valid JSON Object
50         final String trimmedMessage = cefMessage.trim();
51         if (!(trimmedMessage.startsWith("{") && trimmedMessage.endsWith("}"))) {
52             final String errorMessage =
53                     "CEF Message must be a JSON object starting and ending with curly braces. Invalid JSON String: " +
54                             cefMessage;
55             setTerminatingMessage(errorMessage, tcaExecutionContext, true);
56             return tcaExecutionContext;
57         }
58
59         // Parse CEF message as JSON Object
60         final Optional<EventListener> eventListenerOptional =
61                 AnalyticsModelJsonConversion.EVENT_LISTENER_JSON_FUNCTION.apply(cefMessage);
62
63         if (!eventListenerOptional.isPresent()) {
64             final String errorMessage =
65                     "Unable to parse given CEF Message as JSON Object. CEF Message String: " + cefMessage;
66             setTerminatingMessage(errorMessage, tcaExecutionContext, true);
67             return tcaExecutionContext;
68         }
69
70
71         // JSON parsing completed successfully
72         tcaExecutionContext.getTcaProcessingContext().setEventListener(eventListenerOptional.get());
73
74         return tcaExecutionContext;
75     }
76 }