e52cc3eddf92f567bb8a315bf91da2f53633a46a
[aai/aai-common.git] / aai-core / src / main / java / org / onap / aai / dmaap / AAIDmaapEventJMSConsumer.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21  */
22 package org.onap.aai.dmaap;
23
24 import com.att.eelf.configuration.EELFLogger;
25 import com.att.eelf.configuration.EELFManager;
26 import com.sun.jersey.api.client.Client;
27 import com.sun.jersey.api.client.ClientResponse;
28 import com.sun.jersey.api.client.WebResource;
29 import org.apache.log4j.MDC;
30 import org.json.JSONException;
31 import org.json.JSONObject;
32 import org.onap.aai.logging.ErrorLogHelper;
33 import org.onap.aai.util.AAIConstants;
34 import org.onap.aai.logging.LoggingContext;
35 import org.onap.aai.logging.LoggingContext.LoggingField;
36 import org.onap.aai.logging.LoggingContext.StatusCode;
37
38 import javax.jms.JMSException;
39 import javax.jms.Message;
40 import javax.jms.MessageListener;
41 import javax.jms.TextMessage;
42 import javax.ws.rs.core.MediaType;
43 import java.io.File;
44 import java.io.FileReader;
45 import java.io.IOException;
46 import java.util.Properties;
47
48 public class AAIDmaapEventJMSConsumer implements MessageListener {
49
50         private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(AAIDmaapEventJMSConsumer.class);
51
52         private Client httpClient;
53
54         private Properties aaiEventProps;
55         private String aaiEventUrl = "";
56
57         public AAIDmaapEventJMSConsumer() throws org.apache.commons.configuration.ConfigurationException {
58                 super();
59                 try(FileReader reader = new FileReader(new File(AAIConstants.AAI_EVENT_DMAAP_PROPS))) {
60
61                         if (this.httpClient == null) {
62                                 aaiEventProps = new Properties();
63                                 aaiEventProps.load(reader);
64
65                                 String host = aaiEventProps.getProperty("host");
66                                 String topic = aaiEventProps.getProperty("topic");
67                                 String protocol = aaiEventProps.getProperty("Protocol");
68
69                                 aaiEventUrl = protocol + "://" + host + "/events/" + topic;
70                                 httpClient = Client.create();
71                         }
72
73                 } catch (IOException e) {
74                         ErrorLogHelper.logError("AAI_4000", "Error updating dmaap config file for aai event.");
75                         LOGGER.error(e.getMessage(), e);
76                 }
77
78         }
79
80         @Override
81         public void onMessage(Message message) {
82
83                 String jsmMessageTxt = "";
84                 String aaiEvent = "";
85                 String eventName = "";
86
87                 if (message instanceof TextMessage) {
88                         try {
89                                 jsmMessageTxt = ((TextMessage) message).getText();
90                                 JSONObject jo = new JSONObject(jsmMessageTxt);
91
92                                 if (jo.has("aaiEventPayload")) {
93                                         aaiEvent = jo.getJSONObject("aaiEventPayload").toString();
94                                 } else {
95                                         return;
96                                 }
97                                 if (jo.getString("transId") != null) {
98                                         MDC.put("requestId", jo.getString("transId"));
99                                 }
100                                 if (jo.getString("fromAppId") != null) {
101                                         MDC.put("partnerName", jo.getString("fromAppId"));
102                                 }
103                                 if (jo.getString("event-topic") != null) {
104                                         eventName = jo.getString("event-topic");
105                                 }
106
107                                 MDC.put ("targetEntity", "DMAAP");
108                                 if (jo.getString("event-topic") != null) {
109                                         eventName = jo.getString("event-topic");
110                                         MDC.put ("targetServiceName", eventName);
111                                 }
112                                 MDC.put ("serviceName", "AAI");
113                                 MDC.put(LoggingField.STATUS_CODE.toString(), StatusCode.COMPLETE.toString());
114                                 MDC.put(LoggingField.RESPONSE_CODE.toString(), "0");
115                                 LOGGER.info(eventName + "|" + aaiEvent);
116
117                                 if ("AAI-EVENT".equals(eventName)) {
118                                         this.sentWithHttp(this.httpClient, this.aaiEventUrl, aaiEvent);
119                                 } else {
120                                         LoggingContext.statusCode(StatusCode.ERROR);
121                                         LOGGER.error(eventName + "|Event Topic invalid.");
122                                 }
123                         } catch (java.net.SocketException e) {
124                                 if (!e.getMessage().contains("Connection reset")) {
125                                         MDC.put(LoggingField.STATUS_CODE.toString(), StatusCode.ERROR.toString());
126                                         MDC.put(LoggingField.RESPONSE_CODE.toString(), "200");
127                                         LOGGER.error("AAI_7304 Error reaching DMaaP to send event. " + aaiEvent, e);
128                                 }
129                         } catch (IOException e) {
130                                 MDC.put(LoggingField.STATUS_CODE.toString(), StatusCode.ERROR.toString());
131                                 MDC.put(LoggingField.RESPONSE_CODE.toString(), "200");
132                                 LOGGER.error("AAI_7304 Error reaching DMaaP to send event. " + aaiEvent, e);
133                         } catch (JMSException | JSONException e) {
134                                 MDC.put(LoggingField.STATUS_CODE.toString(), StatusCode.ERROR.toString());
135                                 MDC.put(LoggingField.RESPONSE_CODE.toString(), "200");
136                                 LOGGER.error("AAI_7350 Error parsing aaievent jsm message for sending to dmaap. " + jsmMessageTxt, e);
137                         } catch (Exception e) {
138                                 MDC.put(LoggingField.STATUS_CODE.toString(), StatusCode.ERROR.toString());
139                                 MDC.put(LoggingField.RESPONSE_CODE.toString(), "200");
140                                 LOGGER.error("AAI_7350 Error sending message to dmaap. " + jsmMessageTxt, e);
141                         }
142                 }
143
144         }
145
146         private boolean sentWithHttp(Client client, String url, String aaiEvent) throws IOException {
147
148                 WebResource webResource = client.resource(url);
149
150                 ClientResponse response = webResource
151                                 .accept(MediaType.APPLICATION_JSON)
152                                 .type(MediaType.APPLICATION_JSON)
153                                 .post(ClientResponse.class, aaiEvent);
154
155                 if (response.getStatus() != 200) {
156                         LOGGER.info("Failed : HTTP error code : " + response.getStatus());
157                         return false;
158                 }
159                 return true;
160         }
161 }