2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Copyright (C) 2017 Amdocs
8 * =============================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 * ============LICENSE_END=========================================================
25 package org.onap.appc.adapter.messaging.dmaap.impl;
27 import com.att.eelf.configuration.EELFLogger;
28 import com.att.eelf.configuration.EELFManager;
29 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
30 import org.onap.appc.adapter.message.EventSender;
31 import org.onap.appc.adapter.message.MessageDestination;
32 import org.onap.appc.adapter.message.Producer;
33 import org.onap.appc.adapter.message.event.EventHeader;
34 import org.onap.appc.adapter.message.event.EventMessage;
35 import org.onap.appc.adapter.message.event.EventStatus;
36 import org.onap.appc.configuration.Configuration;
37 import org.onap.appc.configuration.ConfigurationFactory;
38 import org.onap.appc.exceptions.APPCException;
39 import java.util.ArrayList;
40 import java.util.Collections;
41 import java.util.Date;
42 import java.util.List;
44 import java.util.Properties;
45 import java.util.concurrent.ConcurrentHashMap;
48 public class EventSenderDmaapImpl implements EventSender
50 private final EELFLogger LOG = EELFManager.getInstance().getLogger(EventSenderDmaapImpl.class);
51 public static final String EVENT_TOPIC_WRITE = "dmaap.event.topic.write";
52 public static final String DMAAP_USERNAME = "dmaap.appc.username";
53 public static final String DMAAP_PASSWORD = "dmaap.appc.password";
54 public static final String EVENT_POOL_MEMBERS = "dmaap.event.pool.members";
56 private static Configuration configuration = ConfigurationFactory.getConfiguration();
58 private Map<String,Producer> producerMap = new ConcurrentHashMap<>();
60 public Map<String, Producer> getProducerMap() {
64 public void setProducerMap(Map<String, Producer> producerMap) {
65 this.producerMap = producerMap;
68 public EventSenderDmaapImpl(){
72 public void initialize(){
73 Properties properties = configuration.getProperties();
77 final List<String> pool = new ArrayList<>();
79 for(MessageDestination destination: MessageDestination.values()){
80 writeTopic = properties.getProperty(destination + "." + EVENT_TOPIC_WRITE);
81 username = properties.getProperty(destination + "." + DMAAP_USERNAME);
82 password = properties.getProperty(destination + "." + DMAAP_PASSWORD);
83 String hostNames = properties.getProperty(destination + "." + EVENT_POOL_MEMBERS);
85 if (hostNames != null && !hostNames.isEmpty()) {
86 LOG.debug(String.format("hostNames = %s, taken from property: %s", hostNames, destination + "." + EVENT_POOL_MEMBERS));
87 Collections.addAll(pool, hostNames.split(","));
90 LOG.debug(String.format("pool = %s, taken from property: %s", pool, destination + "." + EVENT_POOL_MEMBERS));
91 LOG.debug(String.format("writeTopic = %s, taken from property: %s", writeTopic, destination + "." + EVENT_TOPIC_WRITE));
92 LOG.debug(String.format("username = %s, taken from property: %s", username, destination + "." + DMAAP_USERNAME));
93 Producer producer = new DmaapProducerImpl(pool, writeTopic,username, password);
95 for (String url : pool) {
96 if (url.contains("3905") || url.contains("https")) {
97 LOG.debug("Producer should use HTTPS");
98 producer.useHttps(true);
102 producerMap.put(destination.toString(),producer);
108 public boolean sendEvent(MessageDestination destination, EventMessage msg) {
109 String jsonStr = msg.toJson();
110 String id = msg.getEventHeader().getEventId();
111 LOG.info(String.format("Posting Message [%s - %s]", id, jsonStr));
112 Producer producer = producerMap.get(destination.toString());
113 return producer.post(id, jsonStr);
117 public boolean sendEvent(MessageDestination destination, EventMessage msg, String eventTopicName) {
118 String jsonStr = msg.toJson();
119 String id = msg.getEventHeader().getEventId();
120 LOG.info(String.format("Posting Message [%s - %s]", id, jsonStr));
121 Producer producer = createProducer(destination, eventTopicName);
122 return producer.post(id, jsonStr);
125 private Producer createProducer(MessageDestination destination, String eventTopicName) {
126 Properties properties = configuration.getProperties();
127 final List<String> pool = new ArrayList<>();
128 String username = properties.getProperty(destination + "." + DMAAP_USERNAME);
129 String password = properties.getProperty(destination + "." + DMAAP_PASSWORD);
130 String hostNames = properties.getProperty(destination + "." + EVENT_POOL_MEMBERS);
132 if (hostNames != null && !hostNames.isEmpty()) {
133 LOG.debug(String.format("hostNames = %s, taken from property: %s", hostNames, destination + "." + EVENT_POOL_MEMBERS));
134 Collections.addAll(pool, hostNames.split(","));
137 LOG.debug(String.format("pool = %s, taken from property: %s", pool, destination + "." + EVENT_POOL_MEMBERS));
138 LOG.debug(String.format("writeTopic = %s, taken from property: %s", eventTopicName, destination + "." + EVENT_TOPIC_WRITE));
139 LOG.debug(String.format("username = %s, taken from property: %s", username, destination + "." + DMAAP_USERNAME));
140 Producer producer = new DmaapProducerImpl(pool, eventTopicName,username, password);
142 for (String url : pool) {
143 if (url.contains("3905") || url.contains("https")) {
144 LOG.debug("Producer should use HTTPS");
145 producer.useHttps(true);
153 public boolean sendEvent(MessageDestination destination, Map<String, String> params, SvcLogicContext ctx) throws APPCException {
155 if (params == null) {
156 String message = "Parameters map is empty (null)";
158 throw new APPCException(message);
160 String eventTime = new Date(System.currentTimeMillis()).toString();
161 String apiVer = params.get("apiVer");
162 String eventId = params.get("eventId");
163 String reason = params.get("reason");
164 String entityId=params.get("entityId");
166 reason=reason+"("+entityId+")";
168 Integer code = Integer.getInteger(params.get("code"), 500);
170 if (eventTime == null || apiVer == null || eventId == null || reason == null) {
171 String message = String.format("Missing input parameters: %s", params);
173 throw new APPCException(message);
175 EventMessage eventMessage = new EventMessage(
176 new EventHeader(eventTime, apiVer, eventId),
177 new EventStatus(code, reason));
179 return sendEvent(destination,eventMessage);