Merge "Fixes in DataGrooming"
[aai/aai-common.git] / aai-core / src / main / java / org / onap / aai / dmaap / AAIDmaapEventJMSConsumer.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 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 package org.onap.aai.dmaap;
21
22 import com.att.eelf.configuration.EELFLogger;
23 import com.att.eelf.configuration.EELFManager;
24 import org.apache.log4j.MDC;
25 import org.json.JSONException;
26 import org.json.JSONObject;
27 import org.onap.aai.config.SpringContextAware;
28 import org.onap.aai.logging.LogFormatTools;
29 import org.onap.aai.logging.LoggingContext;
30 import org.onap.aai.logging.LoggingContext.LoggingField;
31 import org.onap.aai.logging.LoggingContext.StatusCode;
32 import org.springframework.context.ApplicationContext;
33 import org.springframework.core.env.Environment;
34 import org.springframework.http.HttpEntity;
35 import org.springframework.http.HttpHeaders;
36 import org.springframework.http.HttpMethod;
37 import org.springframework.web.client.RestTemplate;
38
39 import javax.jms.JMSException;
40 import javax.jms.Message;
41 import javax.jms.MessageListener;
42 import javax.jms.TextMessage;
43
44 public class AAIDmaapEventJMSConsumer implements MessageListener {
45
46         private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(AAIDmaapEventJMSConsumer.class);
47
48         private RestTemplate restTemplate;
49
50         private HttpHeaders httpHeaders;
51
52         private Environment environment;
53
54         public AAIDmaapEventJMSConsumer() {
55         ApplicationContext applicationContext  = SpringContextAware.getApplicationContext();
56
57         if(applicationContext != null){
58                         restTemplate = (RestTemplate) applicationContext.getBean("dmaapRestTemplate");
59                         httpHeaders  = (HttpHeaders) applicationContext.getBean("dmaapHeaders");
60                         environment  = applicationContext.getEnvironment();
61                 }
62         }
63
64         @Override
65         public void onMessage(Message message) {
66
67             if(restTemplate == null){
68                 return;
69                 }
70
71                 String jsmMessageTxt = "";
72                 String aaiEvent = "";
73                 String eventName = "";
74
75                 if (message instanceof TextMessage) {
76                         try {
77                                 jsmMessageTxt = ((TextMessage) message).getText();
78                                 JSONObject jo = new JSONObject(jsmMessageTxt);
79
80                                 if (jo.has("aaiEventPayload")) {
81                                         aaiEvent = jo.getJSONObject("aaiEventPayload").toString();
82                                 } else {
83                                         return;
84                                 }
85                                 if (jo.getString("transId") != null) {
86                                         MDC.put("requestId", jo.getString("transId"));
87                                 }
88                                 if (jo.getString("fromAppId") != null) {
89                                         MDC.put("partnerName", jo.getString("fromAppId"));
90                                 }
91                                 if (jo.getString("event-topic") != null) {
92                                         eventName = jo.getString("event-topic");
93                                 }
94
95                                 MDC.put ("targetEntity", "DMAAP");
96                                 if (jo.getString("event-topic") != null) {
97                                         eventName = jo.getString("event-topic");
98                                         MDC.put ("targetServiceName", eventName);
99                                 }
100                                 MDC.put ("serviceName", "AAI");
101                                 MDC.put(LoggingField.STATUS_CODE.toString(), StatusCode.COMPLETE.toString());
102                                 MDC.put(LoggingField.RESPONSE_CODE.toString(), "0");
103                                 LOGGER.info(eventName + "|" + aaiEvent);
104                                 HttpEntity httpEntity = new HttpEntity(aaiEvent, httpHeaders);
105
106                                 String transportType = environment.getProperty("dmaap.ribbon.transportType", "http");
107                                 String baseUrl  = transportType + "://" + environment.getProperty("dmaap.ribbon.listOfServers");
108                                 String endpoint = "/events/" + eventName;
109
110                                 if ("AAI-EVENT".equals(eventName)) {
111                     restTemplate.exchange(baseUrl + endpoint, HttpMethod.POST, httpEntity, String.class);
112                                 } else {
113                                         LoggingContext.statusCode(StatusCode.ERROR);
114                                         LOGGER.error(eventName + "|Event Topic invalid.");
115                                 }
116                         } catch (JMSException | JSONException e) {
117                                 MDC.put(LoggingField.STATUS_CODE.toString(), StatusCode.ERROR.toString());
118                                 MDC.put(LoggingField.RESPONSE_CODE.toString(), "200");
119                                 LOGGER.error("AAI_7350 Error parsing aaievent jsm message for sending to dmaap. {} {}", jsmMessageTxt, LogFormatTools.getStackTop(e));
120                         } catch (Exception e) {
121                                 MDC.put(LoggingField.STATUS_CODE.toString(), StatusCode.ERROR.toString());
122                                 MDC.put(LoggingField.RESPONSE_CODE.toString(), "200");
123                                 LOGGER.error("AAI_7350 Error sending message to dmaap. {} {}" , jsmMessageTxt, LogFormatTools.getStackTop(e));
124                         }
125                 }
126
127         }
128 }