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 org.apache.log4j.MDC;
25 import org.json.JSONException;
26 import org.json.JSONObject;
27 import org.onap.aai.config.SpringContextAware;
28 import org.onap.aai.logging.LogFormatTools;
29 import org.onap.aai.logging.LoggingContext;
30 import org.onap.aai.logging.LoggingContext.LoggingField;
31 import org.onap.aai.logging.LoggingContext.StatusCode;
32 import org.springframework.context.ApplicationContext;
33 import org.springframework.core.env.Environment;
34 import org.springframework.http.HttpEntity;
35 import org.springframework.http.HttpHeaders;
36 import org.springframework.http.HttpMethod;
37 import org.springframework.web.client.RestTemplate;
39 import javax.jms.JMSException;
40 import javax.jms.Message;
41 import javax.jms.MessageListener;
42 import javax.jms.TextMessage;
44 public class AAIDmaapEventJMSConsumer implements MessageListener {
46 private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(AAIDmaapEventJMSConsumer.class);
48 private RestTemplate restTemplate;
50 private HttpHeaders httpHeaders;
52 private Environment environment;
54 public AAIDmaapEventJMSConsumer() {
55 ApplicationContext applicationContext = SpringContextAware.getApplicationContext();
57 if(applicationContext != null){
58 restTemplate = (RestTemplate) applicationContext.getBean("dmaapRestTemplate");
59 httpHeaders = (HttpHeaders) applicationContext.getBean("dmaapHeaders");
60 environment = applicationContext.getEnvironment();
65 public void onMessage(Message message) {
67 if(restTemplate == null){
71 String jsmMessageTxt = "";
73 String eventName = "";
75 if (message instanceof TextMessage) {
77 jsmMessageTxt = ((TextMessage) message).getText();
78 JSONObject jo = new JSONObject(jsmMessageTxt);
80 if (jo.has("aaiEventPayload")) {
81 aaiEvent = jo.getJSONObject("aaiEventPayload").toString();
85 if (jo.getString("transId") != null) {
86 MDC.put("requestId", jo.getString("transId"));
88 if (jo.getString("fromAppId") != null) {
89 MDC.put("partnerName", jo.getString("fromAppId"));
91 if (jo.getString("event-topic") != null) {
92 eventName = jo.getString("event-topic");
95 MDC.put ("targetEntity", "DMAAP");
96 if (jo.getString("event-topic") != null) {
97 eventName = jo.getString("event-topic");
98 MDC.put ("targetServiceName", eventName);
100 MDC.put ("serviceName", "AAI");
101 MDC.put(LoggingField.STATUS_CODE.toString(), StatusCode.COMPLETE.toString());
102 MDC.put(LoggingField.RESPONSE_CODE.toString(), "0");
103 LOGGER.info(eventName + "|" + aaiEvent);
104 HttpEntity httpEntity = new HttpEntity(aaiEvent, httpHeaders);
106 String transportType = environment.getProperty("dmaap.ribbon.transportType", "http");
107 String baseUrl = transportType + "://" + environment.getProperty("dmaap.ribbon.listOfServers");
108 String endpoint = "/events/" + eventName;
110 if ("AAI-EVENT".equals(eventName)) {
111 restTemplate.exchange(baseUrl + endpoint, HttpMethod.POST, httpEntity, String.class);
113 LoggingContext.statusCode(StatusCode.ERROR);
114 LOGGER.error(eventName + "|Event Topic invalid.");
116 } catch (JMSException | JSONException e) {
117 MDC.put(LoggingField.STATUS_CODE.toString(), StatusCode.ERROR.toString());
118 MDC.put(LoggingField.RESPONSE_CODE.toString(), "200");
119 LOGGER.error("AAI_7350 Error parsing aaievent jsm message for sending to dmaap. {} {}", jsmMessageTxt, LogFormatTools.getStackTop(e));
120 } catch (Exception e) {
121 MDC.put(LoggingField.STATUS_CODE.toString(), StatusCode.ERROR.toString());
122 MDC.put(LoggingField.RESPONSE_CODE.toString(), "200");
123 LOGGER.error("AAI_7350 Error sending message to dmaap. {} {}" , jsmMessageTxt, LogFormatTools.getStackTop(e));