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