2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017 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 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 package org.onap.aai.dmaap;
24 import com.att.eelf.configuration.EELFLogger;
25 import com.att.eelf.configuration.EELFManager;
26 import com.sun.jersey.api.client.Client;
27 import com.sun.jersey.api.client.ClientResponse;
28 import com.sun.jersey.api.client.WebResource;
29 import org.apache.log4j.MDC;
30 import org.json.JSONException;
31 import org.json.JSONObject;
32 import org.onap.aai.logging.ErrorLogHelper;
33 import org.onap.aai.util.AAIConstants;
34 import org.onap.aai.logging.LoggingContext;
35 import org.onap.aai.logging.LoggingContext.LoggingField;
36 import org.onap.aai.logging.LoggingContext.StatusCode;
38 import javax.jms.JMSException;
39 import javax.jms.Message;
40 import javax.jms.MessageListener;
41 import javax.jms.TextMessage;
42 import javax.ws.rs.core.MediaType;
44 import java.io.FileReader;
45 import java.io.IOException;
46 import java.util.Properties;
48 public class AAIDmaapEventJMSConsumer implements MessageListener {
50 private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(AAIDmaapEventJMSConsumer.class);
52 private Client httpClient;
54 private Properties aaiEventProps;
55 private String aaiEventUrl = "";
57 public AAIDmaapEventJMSConsumer() throws org.apache.commons.configuration.ConfigurationException {
59 try(FileReader reader = new FileReader(new File(AAIConstants.AAI_EVENT_DMAAP_PROPS))) {
61 if (this.httpClient == null) {
62 aaiEventProps = new Properties();
63 aaiEventProps.load(reader);
65 String host = aaiEventProps.getProperty("host");
66 String topic = aaiEventProps.getProperty("topic");
67 String protocol = aaiEventProps.getProperty("Protocol");
69 aaiEventUrl = protocol + "://" + host + "/events/" + topic;
70 httpClient = Client.create();
73 } catch (IOException e) {
74 ErrorLogHelper.logError("AAI_4000", "Error updating dmaap config file for aai event.");
75 LOGGER.error(e.getMessage(), e);
81 public void onMessage(Message message) {
83 String jsmMessageTxt = "";
85 String eventName = "";
87 if (message instanceof TextMessage) {
89 jsmMessageTxt = ((TextMessage) message).getText();
90 JSONObject jo = new JSONObject(jsmMessageTxt);
92 if (jo.has("aaiEventPayload")) {
93 aaiEvent = jo.getJSONObject("aaiEventPayload").toString();
97 if (jo.getString("transId") != null) {
98 MDC.put("requestId", jo.getString("transId"));
100 if (jo.getString("fromAppId") != null) {
101 MDC.put("partnerName", jo.getString("fromAppId"));
103 if (jo.getString("event-topic") != null) {
104 eventName = jo.getString("event-topic");
107 MDC.put ("targetEntity", "DMAAP");
108 if (jo.getString("event-topic") != null) {
109 eventName = jo.getString("event-topic");
110 MDC.put ("targetServiceName", eventName);
112 MDC.put ("serviceName", "AAI");
113 MDC.put(LoggingField.STATUS_CODE.toString(), StatusCode.COMPLETE.toString());
114 MDC.put(LoggingField.RESPONSE_CODE.toString(), "0");
115 LOGGER.info(eventName + "|" + aaiEvent);
117 if ("AAI-EVENT".equals(eventName)) {
118 this.sentWithHttp(this.httpClient, this.aaiEventUrl, aaiEvent);
120 LoggingContext.statusCode(StatusCode.ERROR);
121 LOGGER.error(eventName + "|Event Topic invalid.");
123 } catch (java.net.SocketException e) {
124 if (!e.getMessage().contains("Connection reset")) {
125 MDC.put(LoggingField.STATUS_CODE.toString(), StatusCode.ERROR.toString());
126 MDC.put(LoggingField.RESPONSE_CODE.toString(), "200");
127 LOGGER.error("AAI_7304 Error reaching DMaaP to send event. " + aaiEvent, e);
129 } catch (IOException e) {
130 MDC.put(LoggingField.STATUS_CODE.toString(), StatusCode.ERROR.toString());
131 MDC.put(LoggingField.RESPONSE_CODE.toString(), "200");
132 LOGGER.error("AAI_7304 Error reaching DMaaP to send event. " + aaiEvent, e);
133 } catch (JMSException | JSONException e) {
134 MDC.put(LoggingField.STATUS_CODE.toString(), StatusCode.ERROR.toString());
135 MDC.put(LoggingField.RESPONSE_CODE.toString(), "200");
136 LOGGER.error("AAI_7350 Error parsing aaievent jsm message for sending to dmaap. " + jsmMessageTxt, e);
137 } catch (Exception e) {
138 MDC.put(LoggingField.STATUS_CODE.toString(), StatusCode.ERROR.toString());
139 MDC.put(LoggingField.RESPONSE_CODE.toString(), "200");
140 LOGGER.error("AAI_7350 Error sending message to dmaap. " + jsmMessageTxt, e);
146 private boolean sentWithHttp(Client client, String url, String aaiEvent) throws IOException {
148 WebResource webResource = client.resource(url);
150 ClientResponse response = webResource
151 .accept(MediaType.APPLICATION_JSON)
152 .type(MediaType.APPLICATION_JSON)
153 .post(ClientResponse.class, aaiEvent);
155 if (response.getStatus() != 200) {
156 LOGGER.info("Failed : HTTP error code : " + response.getStatus());