2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
20 package org.onap.aai.dmaap;
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;
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;
46 public class JMSConsumer implements MessageListener {
48 private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(JMSConsumer.class);
50 private static final int HTTPS_PORT = 3905;
51 private static final Base64.Encoder base64Encoder = Base64.getEncoder();
53 private HttpHeaders httpHeaders;
54 private RestTemplate restTemplate;
56 private Environment environment;
57 private LoadBalancerClient loadBalancerClient;
59 public JMSConsumer() throws Exception {
60 this((LoadBalancerClient)SpringContextAware.getApplicationContext().getBean("loadBalancerClient"));
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();
69 String username = this.environment.getProperty("dmaap.ribbon.username");
70 String password = this.environment.getProperty("dmaap.ribbon.password");
72 if(username == null || password == null){
73 throw new Exception("Unable to retrive username/password from the application properties");
76 String auth = String.format("%s:%s", username, password);
77 String authString = "Basic " + base64Encoder.encodeToString(auth.getBytes());
78 httpHeaders.add("Authorization", authString);
80 restTemplate = new RestTemplate();
84 public void onMessage(Message message) {
86 String jsmMessageTxt = "";
88 String eventName = "";
90 String environment = System.getProperty("lrmRO");
91 if (environment == null) {
95 if (message instanceof TextMessage) {
97 jsmMessageTxt = ((TextMessage) message).getText();
98 JSONObject jo = new JSONObject(jsmMessageTxt);
100 if (jo.has("aaiEventPayload")) {
101 aaiEvent = jo.getJSONObject("aaiEventPayload").toString();
105 if (jo.getString("transId") != null) {
106 MDC.put("requestId", jo.getString("transId"));
108 if (jo.getString("fromAppId") != null) {
109 MDC.put("partnerName", jo.getString("fromAppId"));
111 MDC.put("targetEntity", "DMAAP");
112 if (jo.getString("event-topic") != null) {
113 eventName = jo.getString("event-topic");
114 MDC.put("targetServiceName", eventName);
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);
121 HttpEntity<String> httpEntity = new HttpEntity<>(aaiEvent, httpHeaders);
122 ServiceInstance serviceInstance = loadBalancerClient.choose("dmaap");
123 String url = serviceInstance.getHost() + ":" + serviceInstance.getPort();
125 if(serviceInstance.getPort() == HTTPS_PORT){
126 url = "https://" + url;
128 url = "http://" + url;
131 url += "/events/" + eventName;
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);
139 LOGGER.info(eventName + "|Event sent. " + msg);
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.");
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);