Logging Updates to a few files in the common repo.
[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 import org.openecomp.aai.logging.LoggingContext;
34 import org.openecomp.aai.logging.LoggingContext.LoggingField;
35 import org.openecomp.aai.logging.LoggingContext.StatusCode;
36
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;
42 import java.io.File;
43 import java.io.FileReader;
44 import java.io.IOException;
45 import java.util.Properties;
46
47 public class AAIDmaapEventJMSConsumer implements MessageListener {
48
49         private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(AAIDmaapEventJMSConsumer.class);
50
51         private Client httpClient;
52
53         private Properties aaiEventProps;
54         private String aaiEventUrl = "";
55
56         public AAIDmaapEventJMSConsumer() throws org.apache.commons.configuration.ConfigurationException {
57                 super();
58                 try(FileReader reader = new FileReader(new File(AAIConstants.AAI_EVENT_DMAAP_PROPS))) {
59
60                         if (this.httpClient == null) {
61                                 aaiEventProps = new Properties();
62                                 aaiEventProps.load(reader);
63
64                                 String host = aaiEventProps.getProperty("host");
65                                 String topic = aaiEventProps.getProperty("topic");
66                                 String protocol = aaiEventProps.getProperty("Protocol");
67
68                                 aaiEventUrl = protocol + "://" + host + "/events/" + topic;
69                                 httpClient = Client.create();
70                         }
71
72                 } catch (IOException e) {
73                         ErrorLogHelper.logError("AAI_4000", "Error updating dmaap config file for aai event.");
74                         LOGGER.error(e.getMessage(), e);
75                 }
76
77         }
78
79         @Override
80         public void onMessage(Message message) {
81
82                 String jsmMessageTxt = "";
83                 String aaiEvent = "";
84                 String eventName = "";
85
86                 if (message instanceof TextMessage) {
87                         try {
88                                 jsmMessageTxt = ((TextMessage) message).getText();
89                                 JSONObject jo = new JSONObject(jsmMessageTxt);
90
91                                 if (jo.has("aaiEventPayload")) {
92                                         aaiEvent = jo.getJSONObject("aaiEventPayload").toString();
93                                 } else {
94                                         return;
95                                 }
96                                 if (jo.getString("transId") != null) {
97                                         MDC.put("requestId", jo.getString("transId"));
98                                 }
99                                 if (jo.getString("fromAppId") != null) {
100                                         MDC.put("partnerName", jo.getString("fromAppId"));
101                                 }
102                                 if (jo.getString("event-topic") != null) {
103                                         eventName = jo.getString("event-topic");
104                                 }
105
106                                 LOGGER.info(eventName + "|" + aaiEvent);
107                                 if ("AAI-EVENT".equals(eventName)) {
108                                         this.sentWithHttp(this.httpClient, this.aaiEventUrl, aaiEvent);
109                                 } else {
110                                         LoggingContext.statusCode(StatusCode.ERROR);
111                                         LOGGER.error(eventName + "|Event Topic invalid.");
112                                 }
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);
116                                 }
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);
123                         }
124                 }
125
126         }
127
128         private boolean sentWithHttp(Client client, String url, String aaiEvent) throws IOException {
129
130                 WebResource webResource = client.resource(url);
131
132                 ClientResponse response = webResource
133                                 .accept(MediaType.APPLICATION_JSON)
134                                 .type(MediaType.APPLICATION_JSON)
135                                 .post(ClientResponse.class, aaiEvent);
136
137                 if (response.getStatus() != 200) {
138                         LOGGER.info("Failed : HTTP error code : " + response.getStatus());
139                         return false;
140                 }
141                 return true;
142         }
143 }