2 * ===============================LICENSE_START======================================
\r
4 * ================================================================================
\r
5 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
\r
6 * ================================================================================
\r
7 * Licensed under the Apache License, Version 2.0 (the "License");
\r
8 * you may not use this file except in compliance with the License.
\r
9 * You may obtain a copy of the License at
\r
11 * http://www.apache.org/licenses/LICENSE-2.0
\r
13 * Unless required by applicable law or agreed to in writing, software
\r
14 * distributed under the License is distributed on an "AS IS" BASIS,
\r
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
16 * See the License for the specific language governing permissions and
\r
17 * limitations under the License.
\r
18 * ============================LICENSE_END===========================================
\r
21 package org.openecomp.dcae.apod.analytics.tca.processor;
\r
23 import org.apache.commons.lang3.StringUtils;
\r
24 import org.openecomp.dcae.apod.analytics.common.exception.MessageProcessingException;
\r
25 import org.openecomp.dcae.apod.analytics.common.service.processor.AbstractMessageProcessor;
\r
26 import org.openecomp.dcae.apod.analytics.model.domain.cef.EventListener;
\r
27 import org.openecomp.dcae.apod.analytics.tca.utils.TCAUtils;
\r
28 import org.slf4j.Logger;
\r
29 import org.slf4j.LoggerFactory;
\r
31 import java.io.IOException;
\r
35 * Processor that converts incoming presumed JSON string CEF message to {@link EventListener} object
\r
37 * Pre Conditions: None
\r
40 * @author Rajiv Singla . Creation Date: 11/5/2016.
\r
42 public class TCACEFJsonProcessor extends AbstractMessageProcessor<TCACEFProcessorContext> {
\r
45 private static final long serialVersionUID = 1L;
\r
47 private static final Logger LOG = LoggerFactory.getLogger(TCACEFJsonProcessor.class);
\r
51 public String getProcessorDescription() {
\r
52 return "Converts incoming TCA CEF Message to Event Listener object";
\r
56 public TCACEFProcessorContext processMessage(TCACEFProcessorContext processorContext) {
\r
58 final String cefMessage = processorContext.getMessage();
\r
60 // If CEF Message is null then processor should stop processing
\r
61 if (cefMessage == null) {
\r
62 String errorMessage = "Null CEF message cannot be converted to CEF Event Listener Object";
\r
63 throw new MessageProcessingException(errorMessage, LOG, new IllegalArgumentException(errorMessage));
\r
66 // If CEF Message is blank then processor stop processing
\r
67 if (StringUtils.isBlank(cefMessage)) {
\r
68 setTerminatingProcessingMessage("Blank CEF message cannot be converted to CEF Event Listener Object",
\r
70 return processorContext;
\r
74 final String trimmedCEFMessage = cefMessage.trim();
\r
76 // if message does not start with curly brace and ends with curly brace, it is not a valid cef message
\r
77 // processor will stop processing
\r
78 if (!(trimmedCEFMessage.startsWith("{") && trimmedCEFMessage.endsWith("}"))) {
\r
79 setTerminatingProcessingMessage("CEF Message must start with curly brace and must end with curly brace",
\r
81 return processorContext;
\r
84 // try parsing the cef message
\r
86 final EventListener eventListener = TCAUtils.readValue(trimmedCEFMessage, EventListener.class);
\r
87 setFinishedProcessingMessage("CEF JSON to Event Listener Conversion Successful", processorContext);
\r
88 // set new Event Listener in the Processor Context
\r
89 processorContext.setCEFEventListener(eventListener);
\r
90 return processorContext;
\r
91 } catch (IOException e) {
\r
92 final String errorMessage = String.format("Parsing Failed for CEF Message: %s, Error: %s", cefMessage, e);
\r
93 // If parsing fails throw an exception
\r
94 throw new MessageProcessingException(errorMessage, LOG, e);
\r