8acb954e01daa77b7163e295072892146b3b04a4
[aai/aai-common.git] / aai-core / src / main / java / org / openecomp / aai / dmaap / AAIDmaapEventJMSConsumer.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * org.openecomp.aai
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.openecomp.aai.dmaap;
22
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
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;
39 import java.io.File;
40 import java.io.FileReader;
41 import java.io.IOException;
42 import java.util.Properties;
43
44 public class AAIDmaapEventJMSConsumer implements MessageListener {
45
46         private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(AAIDmaapEventJMSConsumer.class);
47
48         private Client httpClient;
49
50         private Properties aaiEventProps;
51         private String aaiEventUrl = "";
52
53         public AAIDmaapEventJMSConsumer() throws org.apache.commons.configuration.ConfigurationException {
54                 super();
55                 try(FileReader reader = new FileReader(new File(AAIConstants.AAI_EVENT_DMAAP_PROPS))) {
56
57                         if (this.httpClient == null) {
58                                 aaiEventProps = new Properties();
59                                 aaiEventProps.load(reader);
60
61                                 String host = aaiEventProps.getProperty("host");
62                                 String topic = aaiEventProps.getProperty("topic");
63                                 String protocol = aaiEventProps.getProperty("Protocol");
64
65                                 aaiEventUrl = protocol + "://" + host + "/events/" + topic;
66                                 httpClient = Client.create();
67                         }
68
69                 } catch (IOException e) {
70                         ErrorLogHelper.logError("AAI_4000", "Error updating dmaap config file for aai event.");
71                         LOGGER.error(e.getMessage(), e);
72                 }
73
74         }
75
76         @Override
77         public void onMessage(Message message) {
78
79                 String jsmMessageTxt = "";
80                 String aaiEvent = "";
81                 String eventName = "";
82
83                 if (message instanceof TextMessage) {
84                         try {
85                                 jsmMessageTxt = ((TextMessage) message).getText();
86                                 JSONObject jo = new JSONObject(jsmMessageTxt);
87
88                                 if (jo.has("aaiEventPayload")) {
89                                         aaiEvent = jo.getJSONObject("aaiEventPayload").toString();
90                                 } else {
91                                         return;
92                                 }
93                                 if (jo.getString("transId") != null) {
94                                         MDC.put("requestId", jo.getString("transId"));
95                                 }
96                                 if (jo.getString("fromAppId") != null) {
97                                         MDC.put("partnerName", jo.getString("fromAppId"));
98                                 }
99                                 if (jo.getString("event-topic") != null) {
100                                         eventName = jo.getString("event-topic");
101                                 }
102
103                                 LOGGER.info(eventName + "|" + aaiEvent);
104                                 if ("AAI-EVENT".equals(eventName)) {
105                                         this.sentWithHttp(this.httpClient, this.aaiEventUrl, aaiEvent);
106                                 }
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);
110                                 }
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);
117                         }
118                 }
119
120         }
121
122         private boolean sentWithHttp(Client client, String url, String aaiEvent) throws IOException {
123
124                 WebResource webResource = client.resource(url);
125
126                 ClientResponse response = webResource
127                                 .accept(MediaType.APPLICATION_JSON)
128                                 .type(MediaType.APPLICATION_JSON)
129                                 .post(ClientResponse.class, aaiEvent);
130
131                 if (response.getStatus() != 200) {
132                         LOGGER.info("Failed : HTTP error code : " + response.getStatus());
133                         return false;
134                 }
135                 return true;
136         }
137 }