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