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 com.sun.jersey.api.client.Client;
25 import com.sun.jersey.api.client.ClientResponse;
26 import com.sun.jersey.api.client.WebResource;
27 import org.apache.log4j.MDC;
28 import org.json.JSONException;
29 import org.json.JSONObject;
30 import org.onap.aai.logging.ErrorLogHelper;
31 import org.onap.aai.util.AAIConstants;
32 import org.onap.aai.logging.LoggingContext;
33 import org.onap.aai.logging.LoggingContext.LoggingField;
34 import org.onap.aai.logging.LoggingContext.StatusCode;
36 import javax.jms.JMSException;
37 import javax.jms.Message;
38 import javax.jms.MessageListener;
39 import javax.jms.TextMessage;
40 import javax.ws.rs.core.MediaType;
42 import java.io.FileReader;
43 import java.io.IOException;
44 import java.util.Properties;
46 public class AAIDmaapEventJMSConsumer implements MessageListener {
48 private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(AAIDmaapEventJMSConsumer.class);
50 private Client httpClient;
52 private Properties aaiEventProps;
53 private String aaiEventUrl = "";
55 public AAIDmaapEventJMSConsumer() throws org.apache.commons.configuration.ConfigurationException {
57 try(FileReader reader = new FileReader(new File(AAIConstants.AAI_EVENT_DMAAP_PROPS))) {
59 if (this.httpClient == null) {
60 aaiEventProps = new Properties();
61 aaiEventProps.load(reader);
63 String host = aaiEventProps.getProperty("host");
64 String topic = aaiEventProps.getProperty("topic");
65 String protocol = aaiEventProps.getProperty("Protocol");
67 aaiEventUrl = protocol + "://" + host + "/events/" + topic;
68 httpClient = Client.create();
71 } catch (IOException e) {
72 ErrorLogHelper.logError("AAI_4000", "Error updating dmaap config file for aai event.");
73 LOGGER.error(e.getMessage(), e);
79 public void onMessage(Message message) {
81 String jsmMessageTxt = "";
83 String eventName = "";
85 if (message instanceof TextMessage) {
87 jsmMessageTxt = ((TextMessage) message).getText();
88 JSONObject jo = new JSONObject(jsmMessageTxt);
90 if (jo.has("aaiEventPayload")) {
91 aaiEvent = jo.getJSONObject("aaiEventPayload").toString();
95 if (jo.getString("transId") != null) {
96 MDC.put("requestId", jo.getString("transId"));
98 if (jo.getString("fromAppId") != null) {
99 MDC.put("partnerName", jo.getString("fromAppId"));
101 if (jo.getString("event-topic") != null) {
102 eventName = jo.getString("event-topic");
105 MDC.put ("targetEntity", "DMAAP");
106 if (jo.getString("event-topic") != null) {
107 eventName = jo.getString("event-topic");
108 MDC.put ("targetServiceName", eventName);
110 MDC.put ("serviceName", "AAI");
111 MDC.put(LoggingField.STATUS_CODE.toString(), StatusCode.COMPLETE.toString());
112 MDC.put(LoggingField.RESPONSE_CODE.toString(), "0");
113 LOGGER.info(eventName + "|" + aaiEvent);
115 if ("AAI-EVENT".equals(eventName)) {
116 this.sentWithHttp(this.httpClient, this.aaiEventUrl, aaiEvent);
118 LoggingContext.statusCode(StatusCode.ERROR);
119 LOGGER.error(eventName + "|Event Topic invalid.");
121 } catch (java.net.SocketException e) {
122 if (!e.getMessage().contains("Connection reset")) {
123 MDC.put(LoggingField.STATUS_CODE.toString(), StatusCode.ERROR.toString());
124 MDC.put(LoggingField.RESPONSE_CODE.toString(), "200");
125 LOGGER.error("AAI_7304 Error reaching DMaaP to send event. " + aaiEvent, e);
127 } catch (IOException e) {
128 MDC.put(LoggingField.STATUS_CODE.toString(), StatusCode.ERROR.toString());
129 MDC.put(LoggingField.RESPONSE_CODE.toString(), "200");
130 LOGGER.error("AAI_7304 Error reaching DMaaP to send event. " + aaiEvent, e);
131 } catch (JMSException | JSONException e) {
132 MDC.put(LoggingField.STATUS_CODE.toString(), StatusCode.ERROR.toString());
133 MDC.put(LoggingField.RESPONSE_CODE.toString(), "200");
134 LOGGER.error("AAI_7350 Error parsing aaievent jsm message for sending to dmaap. " + jsmMessageTxt, e);
135 } catch (Exception e) {
136 MDC.put(LoggingField.STATUS_CODE.toString(), StatusCode.ERROR.toString());
137 MDC.put(LoggingField.RESPONSE_CODE.toString(), "200");
138 LOGGER.error("AAI_7350 Error sending message to dmaap. " + jsmMessageTxt, e);
144 private boolean sentWithHttp(Client client, String url, String aaiEvent) throws IOException {
146 WebResource webResource = client.resource(url);
148 ClientResponse response = webResource
149 .accept(MediaType.APPLICATION_JSON)
150 .type(MediaType.APPLICATION_JSON)
151 .post(ClientResponse.class, aaiEvent);
153 if (response.getStatus() != 200) {
154 LOGGER.info("Failed : HTTP error code : " + response.getStatus());