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