2 * ============LICENSE_START=======================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.onap.aai.dmaap;
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;
44 public class AAIDmaapEventJMSConsumer implements MessageListener {
46 private static final String EVENT_TOPIC = "event-topic";
48 private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(AAIDmaapEventJMSConsumer.class);
50 private RestTemplate restTemplate;
52 private HttpHeaders httpHeaders;
54 private Environment environment;
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;
66 public void onMessage(Message message) {
68 if(restTemplate == null){
72 String jsmMessageTxt = "";
74 String eventName = "";
76 if (message instanceof TextMessage) {
78 jsmMessageTxt = ((TextMessage) message).getText();
79 JSONObject jo = new JSONObject(jsmMessageTxt);
81 if (jo.has("aaiEventPayload")) {
82 aaiEvent = jo.getJSONObject("aaiEventPayload").toString();
86 if (jo.getString("transId") != null) {
87 MDC.put("requestId", jo.getString("transId"));
89 if (jo.getString("fromAppId") != null) {
90 MDC.put("partnerName", jo.getString("fromAppId"));
92 if (jo.getString(EVENT_TOPIC) != null) {
93 eventName = jo.getString(EVENT_TOPIC);
96 MDC.put ("targetEntity", "DMAAP");
97 if (jo.getString(EVENT_TOPIC) != null) {
98 eventName = jo.getString(EVENT_TOPIC);
99 MDC.put ("targetServiceName", eventName);
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);
107 String transportType = environment.getProperty("dmaap.ribbon.transportType", "http");
108 String baseUrl = transportType + "://" + environment.getProperty("dmaap.ribbon.listOfServers");
109 String endpoint = "/events/" + eventName;
111 if ("AAI-EVENT".equals(eventName)) {
112 restTemplate.exchange(baseUrl + endpoint, HttpMethod.POST, httpEntity, String.class);
114 LoggingContext.statusCode(StatusCode.ERROR);
115 LOGGER.error(eventName + "|Event Topic invalid.");
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));