2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 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=========================================================
21 package org.openecomp.aai.dmaap;
23 import com.att.eelf.configuration.EELFLogger;
24 import com.att.eelf.configuration.EELFManager;
25 import com.sun.jersey.api.client.Client;
26 import com.sun.jersey.api.client.ClientResponse;
27 import com.sun.jersey.api.client.WebResource;
28 import org.apache.log4j.MDC;
29 import org.json.JSONException;
30 import org.json.JSONObject;
31 import org.openecomp.aai.logging.ErrorLogHelper;
32 import org.openecomp.aai.util.AAIConstants;
33 import org.openecomp.aai.logging.LoggingContext;
34 import org.openecomp.aai.logging.LoggingContext.LoggingField;
35 import org.openecomp.aai.logging.LoggingContext.StatusCode;
37 import javax.jms.JMSException;
38 import javax.jms.Message;
39 import javax.jms.MessageListener;
40 import javax.jms.TextMessage;
41 import javax.ws.rs.core.MediaType;
43 import java.io.FileReader;
44 import java.io.IOException;
45 import java.util.Properties;
47 public class AAIDmaapEventJMSConsumer implements MessageListener {
49 private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(AAIDmaapEventJMSConsumer.class);
51 private Client httpClient;
53 private Properties aaiEventProps;
54 private String aaiEventUrl = "";
56 public AAIDmaapEventJMSConsumer() throws org.apache.commons.configuration.ConfigurationException {
58 try(FileReader reader = new FileReader(new File(AAIConstants.AAI_EVENT_DMAAP_PROPS))) {
60 if (this.httpClient == null) {
61 aaiEventProps = new Properties();
62 aaiEventProps.load(reader);
64 String host = aaiEventProps.getProperty("host");
65 String topic = aaiEventProps.getProperty("topic");
66 String protocol = aaiEventProps.getProperty("Protocol");
68 aaiEventUrl = protocol + "://" + host + "/events/" + topic;
69 httpClient = Client.create();
72 } catch (IOException e) {
73 ErrorLogHelper.logError("AAI_4000", "Error updating dmaap config file for aai event.");
74 LOGGER.error(e.getMessage(), e);
80 public void onMessage(Message message) {
82 String jsmMessageTxt = "";
84 String eventName = "";
86 if (message instanceof TextMessage) {
88 jsmMessageTxt = ((TextMessage) message).getText();
89 JSONObject jo = new JSONObject(jsmMessageTxt);
91 if (jo.has("aaiEventPayload")) {
92 aaiEvent = jo.getJSONObject("aaiEventPayload").toString();
96 if (jo.getString("transId") != null) {
97 MDC.put("requestId", jo.getString("transId"));
99 if (jo.getString("fromAppId") != null) {
100 MDC.put("partnerName", jo.getString("fromAppId"));
102 if (jo.getString("event-topic") != null) {
103 eventName = jo.getString("event-topic");
106 LOGGER.info(eventName + "|" + aaiEvent);
107 if ("AAI-EVENT".equals(eventName)) {
108 this.sentWithHttp(this.httpClient, this.aaiEventUrl, aaiEvent);
110 LoggingContext.statusCode(StatusCode.ERROR);
111 LOGGER.error(eventName + "|Event Topic invalid.");
113 } catch (java.net.SocketException e) {
114 if (!e.getMessage().contains("Connection reset")) {
115 LOGGER.error("AAI_7304 Error reaching DMaaP to send event. " + aaiEvent, e);
117 } catch (IOException e) {
118 LOGGER.error("AAI_7304 Error reaching DMaaP to send event. " + aaiEvent, e);
119 } catch (JMSException | JSONException e) {
120 LOGGER.error("AAI_7350 Error parsing aaievent jsm message for sending to dmaap. " + jsmMessageTxt, e);
121 } catch (Exception e) {
122 LOGGER.error("AAI_7350 Error sending message to dmaap. " + jsmMessageTxt, e);
128 private boolean sentWithHttp(Client client, String url, String aaiEvent) throws IOException {
130 WebResource webResource = client.resource(url);
132 ClientResponse response = webResource
133 .accept(MediaType.APPLICATION_JSON)
134 .type(MediaType.APPLICATION_JSON)
135 .post(ClientResponse.class, aaiEvent);
137 if (response.getStatus() != 200) {
138 LOGGER.info("Failed : HTTP error code : " + response.getStatus());