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