b7638876bd408ab645fa3ae3e07ebe9af2f7c1b7
[aai/aai-common.git] / aai-core / src / main / java / org / onap / aai / dmaap / JMSConsumer.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 org.apache.log4j.MDC;
27 import org.json.JSONException;
28 import org.json.JSONObject;
29 import org.onap.aai.config.SpringContextAware;
30 import org.onap.aai.logging.LoggingContext.LoggingField;
31 import org.onap.aai.logging.LoggingContext.StatusCode;
32 import org.springframework.cloud.client.ServiceInstance;
33 import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
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.http.MediaType;
39 import org.springframework.web.client.RestTemplate;
40
41 import javax.jms.JMSException;
42 import javax.jms.Message;
43 import javax.jms.MessageListener;
44 import javax.jms.TextMessage;
45 import java.util.Base64;
46 import java.util.Collections;
47
48 public class JMSConsumer implements MessageListener {
49
50     private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(JMSConsumer.class);
51
52     private static final int HTTPS_PORT = 3905;
53     private static final Base64.Encoder base64Encoder = Base64.getEncoder();
54
55     private HttpHeaders httpHeaders;
56     private RestTemplate restTemplate;
57
58     private Environment environment;
59     private LoadBalancerClient loadBalancerClient;
60
61     public JMSConsumer() throws Exception {
62         this((LoadBalancerClient)SpringContextAware.getApplicationContext().getBean("loadBalancerClient"));
63     }
64
65     public JMSConsumer(LoadBalancerClient loadBalancerClient) throws Exception {
66         this.loadBalancerClient = loadBalancerClient;
67         this.httpHeaders  = new HttpHeaders();
68         this.httpHeaders.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
69         this.environment  = SpringContextAware.getApplicationContext().getEnvironment();
70
71         String username = this.environment.getProperty("dmaap.ribbon.username");
72         String password = this.environment.getProperty("dmaap.ribbon.password");
73
74         if(username == null || password == null){
75             throw new Exception("Unable to retrive username/password from the application properties");
76         }
77
78         String auth = String.format("%s:%s", username, password);
79         String authString = "Basic " + base64Encoder.encodeToString(auth.getBytes());
80         httpHeaders.add("Authorization", authString);
81
82         restTemplate = new RestTemplate();
83     }
84
85     @Override
86     public void onMessage(Message message) {
87
88         String jsmMessageTxt = "";
89         String aaiEvent = "";
90         String eventName = "";
91
92         String environment = System.getProperty("lrmRO");
93         if (environment == null) {
94             environment = "";
95         }
96
97         if (message instanceof TextMessage) {
98             try {
99                 jsmMessageTxt = ((TextMessage) message).getText();
100                 JSONObject jo = new JSONObject(jsmMessageTxt);
101
102                 if (jo.has("aaiEventPayload")) {
103                     aaiEvent = jo.getJSONObject("aaiEventPayload").toString();
104                 } else {
105                     return;
106                 }
107                 if (jo.getString("transId") != null) {
108                     MDC.put("requestId", jo.getString("transId"));
109                 }
110                 if (jo.getString("fromAppId") != null) {
111                     MDC.put("partnerName", jo.getString("fromAppId"));
112                 }
113                 MDC.put("targetEntity", "DMAAP");
114                 if (jo.getString("event-topic") != null) {
115                     eventName = jo.getString("event-topic");
116                     MDC.put("targetServiceName", eventName);
117                 }
118                 MDC.put("serviceName", "AAI");
119                 MDC.put(LoggingField.STATUS_CODE.toString(), StatusCode.COMPLETE.toString());
120                 MDC.put(LoggingField.RESPONSE_CODE.toString(), "0");
121                 LOGGER.info(eventName + "|" + aaiEvent);
122
123                 HttpEntity<String> httpEntity = new HttpEntity<>(aaiEvent, httpHeaders);
124                 ServiceInstance serviceInstance = loadBalancerClient.choose("dmaap");
125                 String url = serviceInstance.getHost() + ":" + serviceInstance.getPort();
126
127                 if(serviceInstance.getPort() == HTTPS_PORT){
128                     url = "https://" + url;
129                 } else {
130                     url = "http://" + url;
131                 }
132
133                 url += "/events/" + eventName;
134
135                 if ("AAI-EVENT".equals(eventName)) {
136                     restTemplate.exchange(url, HttpMethod.POST, httpEntity, String.class);
137                     LOGGER.info(eventName + "|Event sent.");
138                 } else if ("AAI-VCE-INTERFACE-DATA".equals(eventName)) {
139                     restTemplate.exchange(url, HttpMethod.POST, httpEntity, String.class);
140                     String msg = "";
141                     LOGGER.info(eventName + "|Event sent. " + msg);
142                 } else {
143                     MDC.put(LoggingField.STATUS_CODE.toString(), StatusCode.ERROR.toString());
144                     MDC.put(LoggingField.RESPONSE_CODE.toString(), "900");
145                     LOGGER.error(eventName + "|Event Topic invalid.");
146                 }
147             } catch (JMSException | JSONException e) {
148                 MDC.put(LoggingField.STATUS_CODE.toString(), StatusCode.ERROR.toString());
149                 MDC.put(LoggingField.RESPONSE_CODE.toString(), "200");
150                 LOGGER.error("AAI_7350 Error parsing aaievent jms message for sending to dmaap. " + jsmMessageTxt, e);
151             } catch (Exception e) {
152                 MDC.put(LoggingField.STATUS_CODE.toString(), StatusCode.ERROR.toString());
153                 MDC.put(LoggingField.RESPONSE_CODE.toString(), "200");
154                 LOGGER.error("AAI_7350 Error sending message to dmaap. " + jsmMessageTxt, e);
155             }
156         }
157
158     }
159
160 }