Enhancements for the aai-common library
[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  *  Modifications Copyright © 2018 IBM.
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *    http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.aai.dmaap;
24
25 import org.json.JSONException;
26 import org.json.JSONObject;
27 import org.onap.aai.aailog.logs.AaiDmaapMetricLog;
28 import org.onap.aai.exceptions.AAIException;
29 import org.onap.aai.logging.AaiElsErrorCode;
30 import org.onap.aai.logging.ErrorLogHelper;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33 import org.slf4j.MDC;
34 import org.springframework.core.env.Environment;
35 import org.springframework.http.HttpEntity;
36 import org.springframework.http.HttpHeaders;
37 import org.springframework.http.HttpMethod;
38 import org.springframework.web.client.RestTemplate;
39
40 import javax.jms.JMSException;
41 import javax.jms.Message;
42 import javax.jms.MessageListener;
43 import javax.jms.TextMessage;
44 import java.util.Map;
45 import java.util.Objects;
46
47 public class AAIDmaapEventJMSConsumer implements MessageListener {
48
49     private static final String EVENT_TOPIC = "event-topic";
50
51     private static final Logger LOGGER = LoggerFactory.getLogger(AAIDmaapEventJMSConsumer.class);
52
53     private RestTemplate restTemplate;
54
55     private HttpHeaders httpHeaders;
56
57     private Environment environment;
58     private Map<String, String> mdcCopy;
59
60
61     public AAIDmaapEventJMSConsumer(Environment environment, RestTemplate restTemplate, HttpHeaders httpHeaders) {
62         super();
63         mdcCopy = MDC.getCopyOfContextMap();
64         Objects.nonNull(environment);
65         Objects.nonNull(restTemplate);
66         Objects.nonNull(httpHeaders);
67         this.environment = environment;
68         this.restTemplate = restTemplate;
69         this.httpHeaders = httpHeaders;
70     }
71
72     @Override
73     public void onMessage(Message message) {
74
75         if (restTemplate == null) {
76             return;
77         }
78
79         String jsmMessageTxt = "";
80         String aaiEvent = "";
81         JSONObject aaiEventHeader;
82         JSONObject joPayload;
83         String transactionId = "";
84         String serviceName = "";
85         String eventName = "";
86         String aaiElsErrorCode = AaiElsErrorCode.SUCCESS;
87         String errorDescription = "";
88
89         if ( mdcCopy != null ) {
90             MDC.setContextMap(mdcCopy);
91         }
92
93         if (message instanceof TextMessage) {
94             AaiDmaapMetricLog metricLog = new AaiDmaapMetricLog();
95             try {
96                 jsmMessageTxt = ((TextMessage) message).getText();
97                 JSONObject jo = new JSONObject(jsmMessageTxt);
98                 if (jo.has("aaiEventPayload")) {
99                     joPayload = jo.getJSONObject("aaiEventPayload");
100                     aaiEvent = joPayload.toString();
101                 } else {
102                     return;
103                 }
104                 if (jo.getString("event-topic") != null) {
105                     eventName = jo.getString("event-topic");
106                 }
107                 if (joPayload.has("event-header")) {
108                     try {
109                         aaiEventHeader = joPayload.getJSONObject("event-header");
110                         if (aaiEventHeader.has("id")) {
111                             transactionId = aaiEventHeader.get("id").toString();
112                         }
113                         if (aaiEventHeader.has("entity-link")) {
114                             serviceName = aaiEventHeader.get("entity-link").toString();
115                         }
116                     }
117                     catch (JSONException jexc) {
118                         // ignore, this is just used for logging
119                     }
120                 }
121                 metricLog.pre(eventName, aaiEvent, transactionId, serviceName);
122
123                 HttpEntity httpEntity = new HttpEntity(aaiEvent, httpHeaders);
124
125                 String transportType = environment.getProperty("dmaap.ribbon.transportType", "http");
126                 String baseUrl = transportType + "://" + environment.getProperty("dmaap.ribbon.listOfServers");
127                 String endpoint = "/events/" + eventName;
128
129                 if ("AAI-EVENT".equals(eventName)) {
130                     restTemplate.exchange(baseUrl + endpoint, HttpMethod.POST, httpEntity, String.class);
131                 } else {
132                     LOGGER.error(eventName + "|Event Topic invalid.");
133                 }
134             } catch (JMSException | JSONException e) {
135                 aaiElsErrorCode = AaiElsErrorCode.DATA_ERROR;
136                 errorDescription = e.getMessage();
137                 ErrorLogHelper.logException(new AAIException("AAI_7350"));
138             } catch (Exception e) {
139                 aaiElsErrorCode = AaiElsErrorCode.AVAILABILITY_TIMEOUT_ERROR;
140                 errorDescription = e.getMessage();
141                 ErrorLogHelper.logException(new AAIException("AAI_7304", jsmMessageTxt));
142             }
143             finally {
144                 metricLog.post(aaiElsErrorCode, errorDescription);
145             }
146         }
147     }
148 }