TCA: Replace any openecomp reference by onap
[dcaegen2/analytics/tca.git] / dcae-analytics-tca / src / main / java / org / onap / dcae / apod / analytics / tca / processor / TCACEFJsonProcessor.java
1 /*
2  * ===============================LICENSE_START======================================
3  *  dcae-analytics
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
10  *
11  *          http://www.apache.org/licenses/LICENSE-2.0
12  *
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===========================================
19  */
20
21 package org.onap.dcae.apod.analytics.tca.processor;
22
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;
30
31 import java.io.IOException;
32
33 /**
34  *<p>
35  *    Processor that converts incoming presumed JSON string CEF message to {@link EventListener} object
36  *    <br>
37  *    Pre Conditions: None
38  *</p>
39  *
40  *  @author Rajiv Singla . Creation Date: 11/5/2016.
41  */
42 public class TCACEFJsonProcessor extends AbstractMessageProcessor<TCACEFProcessorContext> {
43
44
45     private static final long serialVersionUID = 1L;
46
47     private static final Logger LOG = LoggerFactory.getLogger(TCACEFJsonProcessor.class);
48
49
50     @Override
51     public String getProcessorDescription() {
52         return "Converts incoming TCA CEF Message to Event Listener object";
53     }
54
55     @Override
56     public TCACEFProcessorContext processMessage(TCACEFProcessorContext processorContext) {
57
58         final String cefMessage = processorContext.getMessage();
59
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));
64         }
65
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",
69                     processorContext);
70             return processorContext;
71         }
72
73         // trim cef message
74         final String trimmedCEFMessage = cefMessage.trim();
75
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",
80                     processorContext);
81             return processorContext;
82         }
83
84         // try parsing the cef message
85         try {
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);
95         }
96
97     }
98 }