2 * ===============================LICENSE_START======================================
4 * ================================================================================
5 * Copyright © 2017 AT&T 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
11 * http://www.apache.org/licenses/LICENSE-2.0
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===========================================
21 package org.onap.dcae.apod.analytics.tca.processor;
23 import org.apache.commons.lang3.StringUtils;
24 import org.onap.dcae.apod.analytics.common.exception.MessageProcessingException;
25 import org.onap.dcae.apod.analytics.common.service.processor.AbstractMessageProcessor;
26 import org.onap.dcae.apod.analytics.model.domain.cef.EventListener;
27 import org.onap.dcae.apod.analytics.tca.utils.TCAUtils;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
31 import java.io.IOException;
35 * Processor that converts incoming presumed JSON string CEF message to {@link EventListener} object
37 * Pre Conditions: None
40 * @author Rajiv Singla . Creation Date: 11/5/2016.
42 public class TCACEFJsonProcessor extends AbstractMessageProcessor<TCACEFProcessorContext> {
45 private static final long serialVersionUID = 1L;
47 private static final Logger LOG = LoggerFactory.getLogger(TCACEFJsonProcessor.class);
51 public String getProcessorDescription() {
52 return "Converts incoming TCA CEF Message to Event Listener object";
56 public TCACEFProcessorContext processMessage(TCACEFProcessorContext processorContext) {
58 final String cefMessage = processorContext.getMessage();
60 // If CEF Message is null then processor should stop processing
61 if (cefMessage == null) {
62 String errorMessage = "Null CEF message cannot be converted to CEF Event Listener Object";
63 throw new MessageProcessingException(errorMessage, LOG, new IllegalArgumentException(errorMessage));
66 // If CEF Message is blank then processor stop processing
67 if (StringUtils.isBlank(cefMessage)) {
68 setTerminatingProcessingMessage("Blank CEF message cannot be converted to CEF Event Listener Object",
70 return processorContext;
74 final String trimmedCEFMessage = cefMessage.trim();
76 // if message does not start with curly brace and ends with curly brace, it is not a valid cef message
77 // processor will stop processing
78 if (!(trimmedCEFMessage.startsWith("{") && trimmedCEFMessage.endsWith("}"))) {
79 setTerminatingProcessingMessage("CEF Message must start with curly brace and must end with curly brace",
81 return processorContext;
84 // try parsing the cef message
86 final EventListener eventListener = TCAUtils.readValue(trimmedCEFMessage, EventListener.class);
87 setFinishedProcessingMessage("CEF JSON to Event Listener Conversion Successful", processorContext);
88 // set new Event Listener in the Processor Context
89 processorContext.setCEFEventListener(eventListener);
90 return processorContext;
91 } catch (IOException e) {
92 final String errorMessage = String.format("Parsing Failed for CEF Message: %s, Error: %s", cefMessage, e);
93 // If parsing fails throw an exception
94 throw new MessageProcessingException(errorMessage, LOG, e);