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