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;
34 import javax.jms.JMSException;
35 import javax.jms.Message;
36 import javax.jms.MessageListener;
37 import javax.jms.TextMessage;
38 import javax.ws.rs.core.MediaType;
40 import java.io.FileReader;
41 import java.io.IOException;
42 import java.util.Properties;
44 public class AAIDmaapEventJMSConsumer implements MessageListener {
46 private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(AAIDmaapEventJMSConsumer.class);
48 private Client httpClient;
50 private Properties aaiEventProps;
51 private String aaiEventUrl = "";
53 public AAIDmaapEventJMSConsumer() throws org.apache.commons.configuration.ConfigurationException {
55 try(FileReader reader = new FileReader(new File(AAIConstants.AAI_EVENT_DMAAP_PROPS))) {
57 if (this.httpClient == null) {
58 aaiEventProps = new Properties();
59 aaiEventProps.load(reader);
61 String host = aaiEventProps.getProperty("host");
62 String topic = aaiEventProps.getProperty("topic");
63 String protocol = aaiEventProps.getProperty("Protocol");
65 aaiEventUrl = protocol + "://" + host + "/events/" + topic;
66 httpClient = Client.create();
69 } catch (IOException e) {
70 ErrorLogHelper.logError("AAI_4000", "Error updating dmaap config file for aai event.");
71 LOGGER.error(e.getMessage(), e);
77 public void onMessage(Message message) {
79 String jsmMessageTxt = "";
81 String eventName = "";
83 if (message instanceof TextMessage) {
85 jsmMessageTxt = ((TextMessage) message).getText();
86 JSONObject jo = new JSONObject(jsmMessageTxt);
88 if (jo.has("aaiEventPayload")) {
89 aaiEvent = jo.getJSONObject("aaiEventPayload").toString();
93 if (jo.getString("transId") != null) {
94 MDC.put("requestId", jo.getString("transId"));
96 if (jo.getString("fromAppId") != null) {
97 MDC.put("partnerName", jo.getString("fromAppId"));
99 if (jo.getString("event-topic") != null) {
100 eventName = jo.getString("event-topic");
103 LOGGER.info(eventName + "|" + aaiEvent);
104 if ("AAI-EVENT".equals(eventName)) {
105 this.sentWithHttp(this.httpClient, this.aaiEventUrl, aaiEvent);
107 } catch (java.net.SocketException e) {
108 if (!e.getMessage().contains("Connection reset")) {
109 LOGGER.error("AAI_7304 Error reaching DMaaP to send event. " + aaiEvent, e);
111 } catch (IOException e) {
112 LOGGER.error("AAI_7304 Error reaching DMaaP to send event. " + aaiEvent, e);
113 } catch (JMSException | JSONException e) {
114 LOGGER.error("AAI_7350 Error parsing aaievent jsm message for sending to dmaap. " + jsmMessageTxt, e);
115 } catch (Exception e) {
116 LOGGER.error("AAI_7350 Error sending message to dmaap. " + jsmMessageTxt, e);
122 private boolean sentWithHttp(Client client, String url, String aaiEvent) throws IOException {
124 WebResource webResource = client.resource(url);
126 ClientResponse response = webResource
127 .accept(MediaType.APPLICATION_JSON)
128 .type(MediaType.APPLICATION_JSON)
129 .post(ClientResponse.class, aaiEvent);
131 if (response.getStatus() != 200) {
132 LOGGER.info("Failed : HTTP error code : " + response.getStatus());