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