aboutsummaryrefslogtreecommitdiffstats
path: root/appc-adapters/appc-dmaap-adapter/appc-dmaap-adapter-bundle/src/main/java/org/openecomp/appc/adapter/messaging/dmaap/impl/EventSenderDmaapImpl.java
blob: 37ca7ce3ea420abaf1183e3c4d1ae863fcd3c94c (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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*-
 * ============LICENSE_START=======================================================
 * APPC
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 * Copyright (C) 2017 Amdocs
 * ================================================================================
 * 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=========================================================
 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
 */

package org.openecomp.appc.adapter.messaging.dmaap.impl;

import com.att.eelf.configuration.EELFLogger;
import com.att.eelf.configuration.EELFManager;
import org.openecomp.sdnc.sli.SvcLogicContext;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

import org.openecomp.appc.adapter.message.EventSender;
import org.openecomp.appc.adapter.message.MessageDestination;
import org.openecomp.appc.adapter.message.Producer;
import org.openecomp.appc.adapter.message.event.EventHeader;
import org.openecomp.appc.adapter.message.event.EventMessage;
import org.openecomp.appc.adapter.message.event.EventStatus;
import org.openecomp.appc.adapter.messaging.dmaap.impl.DmaapProducerImpl;
import org.openecomp.appc.configuration.Configuration;
import org.openecomp.appc.configuration.ConfigurationFactory;
import org.openecomp.appc.exceptions.APPCException;

public class EventSenderDmaapImpl implements EventSender
{
    private static final EELFLogger LOG = EELFManager.getInstance().getLogger(EventSenderDmaapImpl.class);
    public static final String EVENT_TOPIC_WRITE = "dmaap.event.topic.write";
    public static final String DMAAP_USERNAME = "dmaap.appc.username";
    public static final String DMAAP_PASSWORD = "dmaap.appc.password";
    public static final String EVENT_POOL_MEMBERS = "dmaap.event.pool.members";

    private static Configuration configuration = ConfigurationFactory.getConfiguration();

    private Map<String,Producer> producerMap = new ConcurrentHashMap<>();

    public Map<String, Producer> getProducerMap() {
        return producerMap;
    }

    public void setProducerMap(Map<String, Producer> producerMap) {
        this.producerMap = producerMap;
    }

    public EventSenderDmaapImpl(){

    }

    public void initialize(){
        Properties properties = configuration.getProperties();
        String writeTopic;
        String username;
        String password;
        final List<String> pool = new ArrayList<>();

        for(MessageDestination destination: MessageDestination.values()){
            writeTopic = properties.getProperty(destination + "." +  EVENT_TOPIC_WRITE);
            username = properties.getProperty(destination + "." + DMAAP_USERNAME);
            password = properties.getProperty(destination + "." + DMAAP_PASSWORD);
            String hostNames = properties.getProperty(destination + "." + EVENT_POOL_MEMBERS);

            if (hostNames != null && !hostNames.isEmpty()) {
                LOG.debug(String.format("hostNames = %s, taken from property: %s", hostNames, destination + "." + EVENT_POOL_MEMBERS));
                Collections.addAll(pool, hostNames.split(","));
            }

            LOG.debug(String.format("pool = %s, taken from property: %s", pool, destination + "." + EVENT_POOL_MEMBERS));
            LOG.debug(String.format("writeTopic = %s, taken from property: %s", writeTopic, destination + "." + EVENT_TOPIC_WRITE));
            LOG.debug(String.format("username = %s, taken from property: %s", username, destination + "." + DMAAP_USERNAME));
            Producer producer = new DmaapProducerImpl(pool, writeTopic,username, password);

            for (String url : pool) {
                if (url.contains("3905") || url.contains("https")) {
                    LOG.debug("Producer should use HTTPS");
                    producer.useHttps(true);
                    break;
                }
            }
            producerMap.put(destination.toString(),producer);
        }

    }

    @Override
    public boolean sendEvent(MessageDestination destination, EventMessage msg) {
        String jsonStr = msg.toJson();
        String id = msg.getEventHeader().getEventId();
        LOG.info(String.format("Posting Message [%s - %s]", id, jsonStr));
        Producer producer = producerMap.get(destination.toString());
        return producer.post(id, jsonStr);
    }

    @Override
    public boolean sendEvent(MessageDestination destination, EventMessage msg, String eventTopicName) {
        String jsonStr = msg.toJson();
        String id = msg.getEventHeader().getEventId();
        LOG.info(String.format("Posting Message [%s - %s]", id, jsonStr));
        Producer producer = createProducer(destination, eventTopicName);
        return producer.post(id, jsonStr);
    }
    
    private Producer createProducer(MessageDestination destination, String eventTopicName) {
        Properties properties = configuration.getProperties();
        final List<String> pool = new ArrayList<>();
        String username = properties.getProperty(destination + "." + DMAAP_USERNAME);
        String password = properties.getProperty(destination + "." + DMAAP_PASSWORD);
        String hostNames = properties.getProperty(destination + "." + EVENT_POOL_MEMBERS);

        if (hostNames != null && !hostNames.isEmpty()) {
            LOG.debug(String.format("hostNames = %s, taken from property: %s", hostNames, destination + "." + EVENT_POOL_MEMBERS));
            Collections.addAll(pool, hostNames.split(","));
        }

        LOG.debug(String.format("pool = %s, taken from property: %s", pool, destination + "." + EVENT_POOL_MEMBERS));
        LOG.debug(String.format("writeTopic = %s, taken from property: %s", eventTopicName, destination + "." + EVENT_TOPIC_WRITE));
        LOG.debug(String.format("username = %s, taken from property: %s", username, destination + "." + DMAAP_USERNAME));
        Producer producer = new DmaapProducerImpl(pool, eventTopicName,username, password);

        for (String url : pool) {
            if (url.contains("3905") || url.contains("https")) {
                LOG.debug("Producer should use HTTPS");
                producer.useHttps(true);
                break;
            }
        }
        return producer;
    }

    @Override
    public boolean sendEvent(MessageDestination destination, Map<String, String> params, SvcLogicContext ctx) throws APPCException {

        if (params == null) {
            String message = "Parameters map is empty (null)";
            LOG.error(message);
            throw new APPCException(message);
        }
        String eventTime = new Date(System.currentTimeMillis()).toString();
        String apiVer = params.get("apiVer");
        String eventId = params.get("eventId");
        String reason = params.get("reason");
        String entityId=params.get("entityId");
        if(entityId!=null){
            reason=reason+"("+entityId+")";
        }
        Integer code = Integer.getInteger(params.get("code"), 500);

        if (eventTime == null || apiVer == null || eventId == null || reason == null) {
            String message = String.format("Missing input parameters: %s", params);
            LOG.error(message);
            throw new APPCException(message);
        }
        EventMessage eventMessage = new EventMessage(
                        new EventHeader(eventTime, apiVer, eventId),
                        new EventStatus(code, reason));

        return sendEvent(destination,eventMessage);
    }
}