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.openecomp.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;
32 import java.util.concurrent.ConcurrentHashMap;
34 import org.openecomp.appc.adapter.message.EventSender;
35 import org.openecomp.appc.adapter.message.MessageDestination;
36 import org.openecomp.appc.adapter.message.Producer;
37 import org.openecomp.appc.adapter.message.event.EventHeader;
38 import org.openecomp.appc.adapter.message.event.EventMessage;
39 import org.openecomp.appc.adapter.message.event.EventStatus;
40 import org.openecomp.appc.adapter.messaging.dmaap.impl.DmaapProducerImpl;
41 import org.openecomp.appc.configuration.Configuration;
42 import org.openecomp.appc.configuration.ConfigurationFactory;
43 import org.openecomp.appc.exceptions.APPCException;
45 public class EventSenderDmaapImpl implements EventSender
47 private static final EELFLogger LOG = EELFManager.getInstance().getLogger(EventSenderDmaapImpl.class);
48 public static final String EVENT_TOPIC_WRITE = "dmaap.event.topic.write";
49 public static final String DMAAP_USERNAME = "dmaap.appc.username";
50 public static final String DMAAP_PASSWORD = "dmaap.appc.password";
51 public static final String EVENT_POOL_MEMBERS = "dmaap.event.pool.members";
53 private static Configuration configuration = ConfigurationFactory.getConfiguration();
55 private Map<String,Producer> producerMap = new ConcurrentHashMap<>();
57 public Map<String, Producer> getProducerMap() {
61 public void setProducerMap(Map<String, Producer> producerMap) {
62 this.producerMap = producerMap;
65 public EventSenderDmaapImpl(){
69 public void initialize(){
70 Properties properties = configuration.getProperties();
74 final List<String> pool = new ArrayList<>();
76 for(MessageDestination destination: MessageDestination.values()){
77 writeTopic = properties.getProperty(destination + "." + EVENT_TOPIC_WRITE);
78 username = properties.getProperty(destination + "." + DMAAP_USERNAME);
79 password = properties.getProperty(destination + "." + DMAAP_PASSWORD);
80 String hostNames = properties.getProperty(destination + "." + EVENT_POOL_MEMBERS);
82 if (hostNames != null && !hostNames.isEmpty()) {
83 LOG.debug(String.format("hostNames = %s, taken from property: %s", hostNames, destination + "." + EVENT_POOL_MEMBERS));
84 Collections.addAll(pool, hostNames.split(","));
87 LOG.debug(String.format("pool = %s, taken from property: %s", pool, destination + "." + EVENT_POOL_MEMBERS));
88 LOG.debug(String.format("writeTopic = %s, taken from property: %s", writeTopic, destination + "." + EVENT_TOPIC_WRITE));
89 LOG.debug(String.format("username = %s, taken from property: %s", username, destination + "." + DMAAP_USERNAME));
90 Producer producer = new DmaapProducerImpl(pool, writeTopic,username, password);
92 for (String url : pool) {
93 if (url.contains("3905") || url.contains("https")) {
94 LOG.debug("Producer should use HTTPS");
95 producer.useHttps(true);
99 producerMap.put(destination.toString(),producer);
105 public boolean sendEvent(MessageDestination destination, EventMessage msg) {
106 String jsonStr = msg.toJson();
107 String id = msg.getEventHeader().getEventId();
108 LOG.info(String.format("Posting Message [%s - %s]", id, jsonStr));
109 Producer producer = producerMap.get(destination.toString());
110 return producer.post(id, jsonStr);
114 public boolean sendEvent(MessageDestination destination, EventMessage msg, String eventTopicName) {
115 String jsonStr = msg.toJson();
116 String id = msg.getEventHeader().getEventId();
117 LOG.info(String.format("Posting Message [%s - %s]", id, jsonStr));
118 Producer producer = createProducer(destination, eventTopicName);
119 return producer.post(id, jsonStr);
122 private Producer createProducer(MessageDestination destination, String eventTopicName) {
123 Properties properties = configuration.getProperties();
124 final List<String> pool = new ArrayList<>();
125 String username = properties.getProperty(destination + "." + DMAAP_USERNAME);
126 String password = properties.getProperty(destination + "." + DMAAP_PASSWORD);
127 String hostNames = properties.getProperty(destination + "." + EVENT_POOL_MEMBERS);
129 if (hostNames != null && !hostNames.isEmpty()) {
130 LOG.debug(String.format("hostNames = %s, taken from property: %s", hostNames, destination + "." + EVENT_POOL_MEMBERS));
131 Collections.addAll(pool, hostNames.split(","));
134 LOG.debug(String.format("pool = %s, taken from property: %s", pool, destination + "." + EVENT_POOL_MEMBERS));
135 LOG.debug(String.format("writeTopic = %s, taken from property: %s", eventTopicName, destination + "." + EVENT_TOPIC_WRITE));
136 LOG.debug(String.format("username = %s, taken from property: %s", username, destination + "." + DMAAP_USERNAME));
137 Producer producer = new DmaapProducerImpl(pool, eventTopicName,username, password);
139 for (String url : pool) {
140 if (url.contains("3905") || url.contains("https")) {
141 LOG.debug("Producer should use HTTPS");
142 producer.useHttps(true);
150 public boolean sendEvent(MessageDestination destination, Map<String, String> params, SvcLogicContext ctx) throws APPCException {
152 if (params == null) {
153 String message = "Parameters map is empty (null)";
155 throw new APPCException(message);
157 String eventTime = new Date(System.currentTimeMillis()).toString();
158 String apiVer = params.get("apiVer");
159 String eventId = params.get("eventId");
160 String reason = params.get("reason");
161 String entityId=params.get("entityId");
163 reason=reason+"("+entityId+")";
165 Integer code = Integer.getInteger(params.get("code"), 500);
167 if (eventTime == null || apiVer == null || eventId == null || reason == null) {
168 String message = String.format("Missing input parameters: %s", params);
170 throw new APPCException(message);
172 EventMessage eventMessage = new EventMessage(
173 new EventHeader(eventTime, apiVer, eventId),
174 new EventStatus(code, reason));
176 return sendEvent(destination,eventMessage);