renaming/constant instead of literal/unused items
[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  *  Modifications Copyright © 2018 IBM.
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *    http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22 package org.onap.aai.dmaap;
23
24 import com.att.eelf.configuration.EELFLogger;
25 import com.att.eelf.configuration.EELFManager;
26 import java.util.Objects;
27 import javax.jms.JMSException;
28 import javax.jms.Message;
29 import javax.jms.MessageListener;
30 import javax.jms.TextMessage;
31 import org.apache.log4j.MDC;
32 import org.json.JSONException;
33 import org.json.JSONObject;
34 import org.onap.aai.logging.LogFormatTools;
35 import org.onap.aai.logging.LoggingContext;
36 import org.onap.aai.logging.LoggingContext.LoggingField;
37 import org.onap.aai.logging.LoggingContext.StatusCode;
38 import org.springframework.core.env.Environment;
39 import org.springframework.http.HttpEntity;
40 import org.springframework.http.HttpHeaders;
41 import org.springframework.http.HttpMethod;
42 import org.springframework.web.client.RestTemplate;
43
44 public class AAIDmaapEventJMSConsumer implements MessageListener {
45
46     private static final String EVENT_TOPIC = "event-topic";
47
48     private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(AAIDmaapEventJMSConsumer.class);
49
50     private RestTemplate restTemplate;
51
52     private HttpHeaders httpHeaders;
53
54     private Environment environment;
55
56     public AAIDmaapEventJMSConsumer(Environment environment, RestTemplate restTemplate, HttpHeaders httpHeaders) {
57         Objects.nonNull(environment);
58         Objects.nonNull(restTemplate);
59         Objects.nonNull(httpHeaders);
60         this.environment = environment;
61         this.restTemplate = restTemplate;
62         this.httpHeaders = httpHeaders;
63     }
64
65     @Override
66     public void onMessage(Message message) {
67
68         if(restTemplate == null){
69             return;
70         }
71
72         String jsmMessageTxt = "";
73         String aaiEvent = "";
74         String eventName = "";
75
76         if (message instanceof TextMessage) {
77             try {
78                 jsmMessageTxt = ((TextMessage) message).getText();
79                 JSONObject jo = new JSONObject(jsmMessageTxt);
80
81                 if (jo.has("aaiEventPayload")) {
82                     aaiEvent = jo.getJSONObject("aaiEventPayload").toString();
83                 } else {
84                     return;
85                 }
86                 if (jo.getString("transId") != null) {
87                     MDC.put("requestId", jo.getString("transId"));
88                 }
89                 if (jo.getString("fromAppId") != null) {
90                     MDC.put("partnerName", jo.getString("fromAppId"));
91                 }
92                 if (jo.getString(EVENT_TOPIC) != null) {
93                     eventName = jo.getString(EVENT_TOPIC);
94                 }
95
96                 MDC.put ("targetEntity", "DMAAP");
97                 if (jo.getString(EVENT_TOPIC) != null) {
98                     eventName = jo.getString(EVENT_TOPIC);
99                     MDC.put ("targetServiceName", eventName);
100                 }
101                 MDC.put ("serviceName", "AAI");
102                 MDC.put(LoggingField.STATUS_CODE.toString(), StatusCode.COMPLETE.toString());
103                 MDC.put(LoggingField.RESPONSE_CODE.toString(), "0");
104                 LOGGER.info(eventName + "|" + aaiEvent);
105                 HttpEntity httpEntity = new HttpEntity(aaiEvent, httpHeaders);
106
107                 String transportType = environment.getProperty("dmaap.ribbon.transportType", "http");
108                 String baseUrl  = transportType + "://" + environment.getProperty("dmaap.ribbon.listOfServers");
109                 String endpoint = "/events/" + eventName;
110
111                 if ("AAI-EVENT".equals(eventName)) {
112                     restTemplate.exchange(baseUrl + endpoint, HttpMethod.POST, httpEntity, String.class);
113                 } else {
114                     LoggingContext.statusCode(StatusCode.ERROR);
115                     LOGGER.error(eventName + "|Event Topic invalid.");
116                 }
117             } catch (JMSException | JSONException 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 parsing aaievent jsm message for sending to dmaap. {} {}", jsmMessageTxt, LogFormatTools.getStackTop(e));
121             } catch (Exception e) {
122                 MDC.put(LoggingField.STATUS_CODE.toString(), StatusCode.ERROR.toString());
123                 MDC.put(LoggingField.RESPONSE_CODE.toString(), "200");
124                 LOGGER.error("AAI_7350 Error sending message to dmaap. {} {}" , jsmMessageTxt, LogFormatTools.getStackTop(e));
125             }
126         }
127
128     }
129 }