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.core.util.function.calculation;
22 import java.util.Optional;
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;
29 * Tca Event Listener Calculation Function converts incoming CEF string message
30 * to Java Object of type Event Listener
32 * @author Rajiv Singla
34 public class TcaEventListenerExtractor implements TcaCalculationFunction {
37 public TcaExecutionContext calculate(final TcaExecutionContext tcaExecutionContext) {
39 final String cefMessage = tcaExecutionContext.getCefMessage();
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;
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: " +
55 setTerminatingMessage(errorMessage, tcaExecutionContext, true);
56 return tcaExecutionContext;
59 // Parse CEF message as JSON Object
60 final Optional<EventListener> eventListenerOptional =
61 AnalyticsModelJsonConversion.EVENT_LISTENER_JSON_FUNCTION.apply(cefMessage);
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;
71 // JSON parsing completed successfully
72 tcaExecutionContext.getTcaProcessingContext().setEventListener(eventListenerOptional.get());
74 return tcaExecutionContext;