d3addebb280055b5233a63f46b3dae1140823214
[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 java.util.Map;
26 import java.util.Objects;
27
28 import javax.jms.JMSException;
29 import javax.jms.Message;
30 import javax.jms.MessageListener;
31 import javax.jms.TextMessage;
32
33 import org.json.JSONException;
34 import org.json.JSONObject;
35 import org.onap.aai.aailog.logs.AaiDmaapMetricLog;
36 import org.onap.aai.exceptions.AAIException;
37 import org.onap.aai.logging.AaiElsErrorCode;
38 import org.onap.aai.logging.ErrorLogHelper;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41 import org.slf4j.MDC;
42 import org.springframework.core.env.Environment;
43 import org.springframework.http.HttpEntity;
44 import org.springframework.http.HttpHeaders;
45 import org.springframework.http.HttpMethod;
46 import org.springframework.web.client.RestTemplate;
47
48 public class AAIDmaapEventJMSConsumer implements MessageListener {
49
50     private static final String EVENT_TOPIC = "event-topic";
51
52     private static final Logger LOGGER = LoggerFactory.getLogger(AAIDmaapEventJMSConsumer.class);
53
54     private RestTemplate restTemplate;
55
56     private HttpHeaders httpHeaders;
57
58     private Environment environment;
59     private Map<String, String> mdcCopy;
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                     } catch (JSONException jexc) {
117                         // ignore, this is just used for logging
118                     }
119                 }
120                 metricLog.pre(eventName, aaiEvent, transactionId, serviceName);
121
122                 HttpEntity<String> httpEntity = new HttpEntity<String>(aaiEvent, httpHeaders);
123
124                 String transportType = environment.getProperty("dmaap.ribbon.transportType", "http");
125                 String baseUrl = transportType + "://" + environment.getProperty("dmaap.ribbon.listOfServers");
126                 String endpoint = "/events/" + eventName;
127
128                 if ("AAI-EVENT".equals(eventName)) {
129                     restTemplate.exchange(baseUrl + endpoint, HttpMethod.POST, httpEntity, String.class);
130                 } else {
131                     LOGGER.error(String.format("%s|Event Topic invalid.", eventName));
132                 }
133             } catch (JMSException | JSONException e) {
134                 aaiElsErrorCode = AaiElsErrorCode.DATA_ERROR;
135                 errorDescription = e.getMessage();
136                 ErrorLogHelper.logException(new AAIException("AAI_7350"));
137             } catch (Exception e) {
138                 aaiElsErrorCode = AaiElsErrorCode.AVAILABILITY_TIMEOUT_ERROR;
139                 errorDescription = e.getMessage();
140                 ErrorLogHelper.logException(new AAIException("AAI_7304", jsmMessageTxt));
141             } finally {
142                 metricLog.post(aaiElsErrorCode, errorDescription);
143             }
144         }
145     }
146 }