2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 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 * ============LICENSE_END=========================================================
24 package org.onap.appc.adapter.messaging.dmaap.impl;
26 import com.att.eelf.configuration.EELFLogger;
27 import com.att.eelf.configuration.EELFManager;
28 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
29 import org.onap.appc.adapter.message.EventSender;
30 import org.onap.appc.adapter.message.MessageDestination;
31 import org.onap.appc.adapter.message.Producer;
32 import org.onap.appc.adapter.message.event.EventHeader;
33 import org.onap.appc.adapter.message.event.EventMessage;
34 import org.onap.appc.adapter.message.event.EventStatus;
35 import org.onap.appc.configuration.Configuration;
36 import org.onap.appc.configuration.ConfigurationFactory;
37 import org.onap.appc.exceptions.APPCException;
38 import java.util.ArrayList;
39 import java.util.Collections;
40 import java.util.Date;
41 import java.util.List;
43 import java.util.Properties;
44 import java.util.concurrent.ConcurrentHashMap;
47 public class EventSenderDmaapImpl implements EventSender
49 private final EELFLogger LOG = EELFManager.getInstance().getLogger(EventSenderDmaapImpl.class);
50 public static final String EVENT_TOPIC_WRITE = "dmaap.event.topic.write";
51 public static final String DMAAP_USERNAME = "dmaap.appc.username";
52 public static final String DMAAP_PASSWORD = "dmaap.appc.password";
53 public static final String EVENT_POOL_MEMBERS = "dmaap.event.pool.members";
55 private static Configuration configuration = ConfigurationFactory.getConfiguration();
57 private Map<String,Producer> producerMap = new ConcurrentHashMap<>();
59 public Map<String, Producer> getProducerMap() {
63 public void setProducerMap(Map<String, Producer> producerMap) {
64 this.producerMap = producerMap;
67 public EventSenderDmaapImpl(){
71 public void initialize(){
72 Properties properties = configuration.getProperties();
76 final List<String> pool = new ArrayList<>();
78 for(MessageDestination destination: MessageDestination.values()){
79 writeTopic = properties.getProperty(destination + "." + EVENT_TOPIC_WRITE);
80 username = properties.getProperty(destination + "." + DMAAP_USERNAME);
81 password = properties.getProperty(destination + "." + DMAAP_PASSWORD);
82 String hostNames = properties.getProperty(destination + "." + EVENT_POOL_MEMBERS);
84 if (hostNames != null && !hostNames.isEmpty()) {
85 LOG.debug(String.format("hostNames = %s, taken from property: %s", hostNames, destination + "." + EVENT_POOL_MEMBERS));
86 Collections.addAll(pool, hostNames.split(","));
89 LOG.debug(String.format("pool = %s, taken from property: %s", pool, destination + "." + EVENT_POOL_MEMBERS));
90 LOG.debug(String.format("writeTopic = %s, taken from property: %s", writeTopic, destination + "." + EVENT_TOPIC_WRITE));
91 LOG.debug(String.format("username = %s, taken from property: %s", username, destination + "." + DMAAP_USERNAME));
92 Producer producer = new DmaapProducerImpl(pool, writeTopic,username, password);
94 for (String url : pool) {
95 if (url.contains("3905") || url.contains("https")) {
96 LOG.debug("Producer should use HTTPS");
97 producer.useHttps(true);
101 producerMap.put(destination.toString(),producer);
107 public boolean sendEvent(MessageDestination destination, EventMessage msg) {
108 String jsonStr = msg.toJson();
109 String id = msg.getEventHeader().getEventId();
110 LOG.info(String.format("Posting Message [%s - %s]", id, jsonStr));
111 Producer producer = producerMap.get(destination.toString());
112 return producer.post(id, jsonStr);
116 public boolean sendEvent(MessageDestination destination, EventMessage msg, String eventTopicName) {
117 String jsonStr = msg.toJson();
118 String id = msg.getEventHeader().getEventId();
119 LOG.info(String.format("Posting Message [%s - %s]", id, jsonStr));
120 Producer producer = createProducer(destination, eventTopicName);
121 return producer.post(id, jsonStr);
124 private Producer createProducer(MessageDestination destination, String eventTopicName) {
125 Properties properties = configuration.getProperties();
126 final List<String> pool = new ArrayList<>();
127 String username = properties.getProperty(destination + "." + DMAAP_USERNAME);
128 String password = properties.getProperty(destination + "." + DMAAP_PASSWORD);
129 String hostNames = properties.getProperty(destination + "." + EVENT_POOL_MEMBERS);
131 if (hostNames != null && !hostNames.isEmpty()) {
132 LOG.debug(String.format("hostNames = %s, taken from property: %s", hostNames, destination + "." + EVENT_POOL_MEMBERS));
133 Collections.addAll(pool, hostNames.split(","));
136 LOG.debug(String.format("pool = %s, taken from property: %s", pool, destination + "." + EVENT_POOL_MEMBERS));
137 LOG.debug(String.format("writeTopic = %s, taken from property: %s", eventTopicName, destination + "." + EVENT_TOPIC_WRITE));
138 LOG.debug(String.format("username = %s, taken from property: %s", username, destination + "." + DMAAP_USERNAME));
139 Producer producer = new DmaapProducerImpl(pool, eventTopicName,username, password);
141 for (String url : pool) {
142 if (url.contains("3905") || url.contains("https")) {
143 LOG.debug("Producer should use HTTPS");
144 producer.useHttps(true);
152 public boolean sendEvent(MessageDestination destination, Map<String, String> params, SvcLogicContext ctx) throws APPCException {
154 if (params == null) {
155 String message = "Parameters map is empty (null)";
157 throw new APPCException(message);
159 String eventTime = new Date(System.currentTimeMillis()).toString();
160 String apiVer = params.get("apiVer");
161 String eventId = params.get("eventId");
162 String reason = params.get("reason");
163 String entityId=params.get("entityId");
165 reason=reason+"("+entityId+")";
167 Integer code = Integer.getInteger(params.get("code"), 500);
169 if (eventTime == null || apiVer == null || eventId == null || reason == null) {
170 String message = String.format("Missing input parameters: %s", params);
172 throw new APPCException(message);
174 EventMessage eventMessage = new EventMessage(
175 new EventHeader(eventTime, apiVer, eventId),
176 new EventStatus(code, reason));
178 return sendEvent(destination,eventMessage);