2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
20 package org.onap.aai.dmaap;
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;
42 public class AAIDmaapEventJMSConsumer implements MessageListener {
44 private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(AAIDmaapEventJMSConsumer.class);
46 private RestTemplate restTemplate;
48 private HttpHeaders httpHeaders;
50 private Environment environment;
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;
62 public void onMessage(Message message) {
64 if(restTemplate == null){
68 String jsmMessageTxt = "";
70 String eventName = "";
72 if (message instanceof TextMessage) {
74 jsmMessageTxt = ((TextMessage) message).getText();
75 JSONObject jo = new JSONObject(jsmMessageTxt);
77 if (jo.has("aaiEventPayload")) {
78 aaiEvent = jo.getJSONObject("aaiEventPayload").toString();
82 if (jo.getString("transId") != null) {
83 MDC.put("requestId", jo.getString("transId"));
85 if (jo.getString("fromAppId") != null) {
86 MDC.put("partnerName", jo.getString("fromAppId"));
88 if (jo.getString("event-topic") != null) {
89 eventName = jo.getString("event-topic");
92 MDC.put ("targetEntity", "DMAAP");
93 if (jo.getString("event-topic") != null) {
94 eventName = jo.getString("event-topic");
95 MDC.put ("targetServiceName", eventName);
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);
103 String transportType = environment.getProperty("dmaap.ribbon.transportType", "http");
104 String baseUrl = transportType + "://" + environment.getProperty("dmaap.ribbon.listOfServers");
105 String endpoint = "/events/" + eventName;
107 if ("AAI-EVENT".equals(eventName)) {
108 restTemplate.exchange(baseUrl + endpoint, HttpMethod.POST, httpEntity, String.class);
110 LoggingContext.statusCode(StatusCode.ERROR);
111 LOGGER.error(eventName + "|Event Topic invalid.");
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));