summaryrefslogtreecommitdiffstats
path: root/aai-core/src/main/java/org/onap/aai/dmaap/JMSConsumer.java
blob: 160928657ceadb2e517c1c7c2ea4a89fcc15828f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/**
 * ============LICENSE_START=======================================================
 * org.onap.aai
 * ================================================================================
 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */
package org.onap.aai.dmaap;

import com.att.eelf.configuration.EELFLogger;
import com.att.eelf.configuration.EELFManager;
import org.apache.log4j.MDC;
import org.json.JSONException;
import org.json.JSONObject;
import org.onap.aai.config.SpringContextAware;
import org.onap.aai.logging.LoggingContext.LoggingField;
import org.onap.aai.logging.LoggingContext.StatusCode;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.core.env.Environment;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.web.client.RestTemplate;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
import java.util.Base64;
import java.util.Collections;

public class JMSConsumer implements MessageListener {

    private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(JMSConsumer.class);

    private static final int HTTPS_PORT = 3905;
    private static final Base64.Encoder base64Encoder = Base64.getEncoder();

    private HttpHeaders httpHeaders;
    private RestTemplate restTemplate;

    private Environment environment;
    private LoadBalancerClient loadBalancerClient;

    public JMSConsumer() throws Exception {
        this((LoadBalancerClient)SpringContextAware.getApplicationContext().getBean("loadBalancerClient"));
    }

    public JMSConsumer(LoadBalancerClient loadBalancerClient) throws Exception {
        this.loadBalancerClient = loadBalancerClient;
        this.httpHeaders  = new HttpHeaders();
        this.httpHeaders.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
        this.environment  = SpringContextAware.getApplicationContext().getEnvironment();

        String username = this.environment.getProperty("dmaap.ribbon.username");
        String password = this.environment.getProperty("dmaap.ribbon.password");

        if(username == null || password == null){
            throw new Exception("Unable to retrive username/password from the application properties");
        }

        String auth = String.format("%s:%s", username, password);
        String authString = "Basic " + base64Encoder.encodeToString(auth.getBytes());
        httpHeaders.add("Authorization", authString);

        restTemplate = new RestTemplate();
    }

    @Override
    public void onMessage(Message message) {

        String jsmMessageTxt = "";
        String aaiEvent = "";
        String eventName = "";

        String environment = System.getProperty("lrmRO");
        if (environment == null) {
            environment = "";
        }

        if (message instanceof TextMessage) {
            try {
                jsmMessageTxt = ((TextMessage) message).getText();
                JSONObject jo = new JSONObject(jsmMessageTxt);

                if (jo.has("aaiEventPayload")) {
                    aaiEvent = jo.getJSONObject("aaiEventPayload").toString();
                } else {
                    return;
                }
                if (jo.getString("transId") != null) {
                    MDC.put("requestId", jo.getString("transId"));
                }
                if (jo.getString("fromAppId") != null) {
                    MDC.put("partnerName", jo.getString("fromAppId"));
                }
                MDC.put("targetEntity", "DMAAP");
                if (jo.getString("event-topic") != null) {
                    eventName = jo.getString("event-topic");
                    MDC.put("targetServiceName", eventName);
                }
                MDC.put("serviceName", "AAI");
                MDC.put(LoggingField.STATUS_CODE.toString(), StatusCode.COMPLETE.toString());
                MDC.put(LoggingField.RESPONSE_CODE.toString(), "0");
                LOGGER.info(eventName + "|" + aaiEvent);

                HttpEntity<String> httpEntity = new HttpEntity<>(aaiEvent, httpHeaders);
                ServiceInstance serviceInstance = loadBalancerClient.choose("dmaap");
                String url = serviceInstance.getHost() + ":" + serviceInstance.getPort();

                if(serviceInstance.getPort() == HTTPS_PORT){
                    url = "https://" + url;
                } else {
                    url = "http://" + url;
                }

                url += "/events/" + eventName;

                if ("AAI-EVENT".equals(eventName)) {
                    restTemplate.exchange(url, HttpMethod.POST, httpEntity, String.class);
                    LOGGER.info(eventName + "|Event sent.");
                } else if ("AAI-VCE-INTERFACE-DATA".equals(eventName)) {
                    restTemplate.exchange(url, HttpMethod.POST, httpEntity, String.class);
                    String msg = "";
                    LOGGER.info(eventName + "|Event sent. " + msg);
                } else {
                    MDC.put(LoggingField.STATUS_CODE.toString(), StatusCode.ERROR.toString());
                    MDC.put(LoggingField.RESPONSE_CODE.toString(), "900");
                    LOGGER.error(eventName + "|Event Topic invalid.");
                }
            } catch (JMSException | JSONException e) {
                MDC.put(LoggingField.STATUS_CODE.toString(), StatusCode.ERROR.toString());
                MDC.put(LoggingField.RESPONSE_CODE.toString(), "200");
                LOGGER.error("AAI_7350 Error parsing aaievent jms message for sending to dmaap. " + jsmMessageTxt, e);
            } catch (Exception e) {
                MDC.put(LoggingField.STATUS_CODE.toString(), StatusCode.ERROR.toString());
                MDC.put(LoggingField.RESPONSE_CODE.toString(), "200");
                LOGGER.error("AAI_7350 Error sending message to dmaap. " + jsmMessageTxt, e);
            }
        }

    }

}