[AAI-171 Amsterdam] Move rest.db to aai-common
[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 {
56
57                         if (this.httpClient == null) {
58                                 FileReader reader = new FileReader(new File(AAIConstants.AAI_EVENT_DMAAP_PROPS));
59                                 aaiEventProps = new Properties();
60                                 aaiEventProps.load(reader);
61                                 reader.close();
62
63                                 String host = aaiEventProps.getProperty("host");
64                                 String topic = aaiEventProps.getProperty("topic");
65                                 String protocol = aaiEventProps.getProperty("Protocol");
66
67                                 aaiEventUrl = protocol + "://" + host + "/events/" + topic;
68                                 httpClient = Client.create();
69                         }
70
71                 } catch (IOException e) {
72                         ErrorLogHelper.logError("AAI_4000", "Error updating dmaap config file for aai event.");
73                 }
74
75         }
76
77         @Override
78         public void onMessage(Message message) {
79
80                 String jsmMessageTxt = "";
81                 String aaiEvent = "";
82                 String eventName = "";
83
84                 if (message instanceof TextMessage) {
85                         try {
86                                 jsmMessageTxt = ((TextMessage) message).getText();
87                                 JSONObject jo = new JSONObject(jsmMessageTxt);
88
89                                 if (jo.has("aaiEventPayload")) {
90                                         aaiEvent = jo.getJSONObject("aaiEventPayload").toString();
91                                 } else {
92                                         return;
93                                 }
94                                 if (jo.getString("transId") != null) {
95                                         MDC.put("requestId", jo.getString("transId"));
96                                 }
97                                 if (jo.getString("fromAppId") != null) {
98                                         MDC.put("partnerName", jo.getString("fromAppId"));
99                                 }
100                                 if (jo.getString("event-topic") != null) {
101                                         eventName = jo.getString("event-topic");
102                                 }
103
104                                 LOGGER.info(eventName + "|" + aaiEvent);
105                                 if ("AAI-EVENT".equals(eventName)) {
106                                         this.sentWithHttp(this.httpClient, this.aaiEventUrl, aaiEvent);
107                                 }
108                         } catch (java.net.SocketException e) {
109                                 if (!e.getMessage().contains("Connection reset")) {
110                                         LOGGER.error("AAI_7304 Error reaching DMaaP to send event. " + aaiEvent, e);
111                                 }
112                         } catch (IOException e) {
113                                 LOGGER.error("AAI_7304 Error reaching DMaaP to send event. " + aaiEvent, e);
114                         } catch (JMSException | JSONException e) {
115                                 LOGGER.error("AAI_7350 Error parsing aaievent jsm message for sending to dmaap. " + jsmMessageTxt, e);
116                         } catch (Exception e) {
117                                 LOGGER.error("AAI_7350 Error sending message to dmaap. " + jsmMessageTxt, e);
118                         }
119                 }
120
121         }
122
123         private boolean sentWithHttp(Client client, String url, String aaiEvent) throws IOException {
124
125                 WebResource webResource = client.resource(url);
126
127                 ClientResponse response = webResource
128                                 .accept(MediaType.APPLICATION_JSON)
129                                 .type(MediaType.APPLICATION_JSON)
130                                 .post(ClientResponse.class, aaiEvent);
131
132                 if (response.getStatus() != 200) {
133                         LOGGER.info("Failed : HTTP error code : " + response.getStatus());
134                         return false;
135                 }
136                 return true;
137         }
138 }