2 * ============LICENSE_START=======================================================
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
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 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 package org.onap.aai.dmaap;
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;
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;
48 public class JMSConsumer implements MessageListener {
50 private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(JMSConsumer.class);
52 private static final int HTTPS_PORT = 3905;
53 private static final Base64.Encoder base64Encoder = Base64.getEncoder();
55 private HttpHeaders httpHeaders;
56 private RestTemplate restTemplate;
58 private Environment environment;
59 private LoadBalancerClient loadBalancerClient;
61 public JMSConsumer() throws Exception {
62 this((LoadBalancerClient)SpringContextAware.getApplicationContext().getBean("loadBalancerClient"));
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();
71 String username = this.environment.getProperty("dmaap.ribbon.username");
72 String password = this.environment.getProperty("dmaap.ribbon.password");
74 if(username == null || password == null){
75 throw new Exception("Unable to retrive username/password from the application properties");
78 String auth = String.format("%s:%s", username, password);
79 String authString = "Basic " + base64Encoder.encodeToString(auth.getBytes());
80 httpHeaders.add("Authorization", authString);
82 restTemplate = new RestTemplate();
86 public void onMessage(Message message) {
88 String jsmMessageTxt = "";
90 String eventName = "";
92 String environment = System.getProperty("lrmRO");
93 if (environment == null) {
97 if (message instanceof TextMessage) {
99 jsmMessageTxt = ((TextMessage) message).getText();
100 JSONObject jo = new JSONObject(jsmMessageTxt);
102 if (jo.has("aaiEventPayload")) {
103 aaiEvent = jo.getJSONObject("aaiEventPayload").toString();
107 if (jo.getString("transId") != null) {
108 MDC.put("requestId", jo.getString("transId"));
110 if (jo.getString("fromAppId") != null) {
111 MDC.put("partnerName", jo.getString("fromAppId"));
113 MDC.put("targetEntity", "DMAAP");
114 if (jo.getString("event-topic") != null) {
115 eventName = jo.getString("event-topic");
116 MDC.put("targetServiceName", eventName);
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);
123 HttpEntity<String> httpEntity = new HttpEntity<>(aaiEvent, httpHeaders);
124 ServiceInstance serviceInstance = loadBalancerClient.choose("dmaap");
125 String url = serviceInstance.getHost() + ":" + serviceInstance.getPort();
127 if(serviceInstance.getPort() == HTTPS_PORT){
128 url = "https://" + url;
130 url = "http://" + url;
133 url += "/events/" + eventName;
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);
141 LOGGER.info(eventName + "|Event sent. " + msg);
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.");
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);