2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.openecomp.appc.adapter.messaging.dmaap.impl;
24 import com.att.eelf.configuration.EELFLogger;
25 import com.att.eelf.configuration.EELFManager;
26 import org.openecomp.sdnc.sli.SvcLogicContext;
29 import java.util.concurrent.ConcurrentHashMap;
31 import org.openecomp.appc.adapter.message.EventSender;
32 import org.openecomp.appc.adapter.message.MessageDestination;
33 import org.openecomp.appc.adapter.message.Producer;
34 import org.openecomp.appc.adapter.message.event.EventHeader;
35 import org.openecomp.appc.adapter.message.event.EventMessage;
36 import org.openecomp.appc.adapter.message.event.EventStatus;
37 import org.openecomp.appc.adapter.messaging.dmaap.impl.DmaapProducerImpl;
38 import org.openecomp.appc.configuration.Configuration;
39 import org.openecomp.appc.configuration.ConfigurationFactory;
40 import org.openecomp.appc.exceptions.APPCException;
42 public class EventSenderDmaapImpl implements EventSender
44 private static final EELFLogger LOG = EELFManager.getInstance().getLogger(EventSenderDmaapImpl.class);
45 public static final String EVENT_TOPIC_WRITE = "dmaap.event.topic.write";
46 public static final String DMAAP_USERNAME = "dmaap.appc.username";
47 public static final String DMAAP_PASSWORD = "dmaap.appc.password";
48 public static final String EVENT_POOL_MEMBERS = "dmaap.event.pool.members";
50 private static Configuration configuration = ConfigurationFactory.getConfiguration();
52 private Map<String,Producer> producerMap = new ConcurrentHashMap<>();
54 public Map<String, Producer> getProducerMap() {
58 public void setProducerMap(Map<String, Producer> producerMap) {
59 this.producerMap = producerMap;
62 public EventSenderDmaapImpl(){
66 public void initialize(){
67 Properties properties = configuration.getProperties();
71 final List<String> pool = new ArrayList<>();
73 for(MessageDestination destination: MessageDestination.values()){
74 writeTopic = properties.getProperty(destination + "." + EVENT_TOPIC_WRITE);
75 username = properties.getProperty(destination + "." + DMAAP_USERNAME);
76 password = properties.getProperty(destination + "." + DMAAP_PASSWORD);
77 String hostNames = properties.getProperty(destination + "." + EVENT_POOL_MEMBERS);
79 if (hostNames != null && !hostNames.isEmpty()) {
80 LOG.debug(String.format("hostNames = %s, taken from property: %s", hostNames, destination + "." + EVENT_POOL_MEMBERS));
81 Collections.addAll(pool, hostNames.split(","));
84 LOG.debug(String.format("pool = %s, taken from property: %s", pool, destination + "." + EVENT_POOL_MEMBERS));
85 LOG.debug(String.format("writeTopic = %s, taken from property: %s", writeTopic, destination + "." + EVENT_TOPIC_WRITE));
86 LOG.debug(String.format("username = %s, taken from property: %s", username, destination + "." + DMAAP_USERNAME));
87 Producer producer = new DmaapProducerImpl(pool, writeTopic,username, password);
89 for (String url : pool) {
90 if (url.contains("3905") || url.contains("https")) {
91 LOG.debug("Producer should use HTTPS");
92 producer.useHttps(true);
96 producerMap.put(destination.toString(),producer);
102 public boolean sendEvent(MessageDestination destination, EventMessage msg) {
103 String jsonStr = msg.toJson();
104 String id = msg.getEventHeader().getEventId();
105 LOG.info(String.format("Posting Message [%s - %s]", id, jsonStr));
106 Producer producer = producerMap.get(destination.toString());
107 return producer.post(id, jsonStr);
111 public boolean sendEvent(MessageDestination destination, EventMessage msg, String eventTopicName) {
112 String jsonStr = msg.toJson();
113 String id = msg.getEventHeader().getEventId();
114 LOG.info(String.format("Posting Message [%s - %s]", id, jsonStr));
115 Producer producer = createProducer(destination, eventTopicName);
116 return producer.post(id, jsonStr);
119 private Producer createProducer(MessageDestination destination, String eventTopicName) {
120 Properties properties = configuration.getProperties();
121 final List<String> pool = new ArrayList<>();
122 String username = properties.getProperty(destination + "." + DMAAP_USERNAME);
123 String password = properties.getProperty(destination + "." + DMAAP_PASSWORD);
124 String hostNames = properties.getProperty(destination + "." + EVENT_POOL_MEMBERS);
126 if (hostNames != null && !hostNames.isEmpty()) {
127 LOG.debug(String.format("hostNames = %s, taken from property: %s", hostNames, destination + "." + EVENT_POOL_MEMBERS));
128 Collections.addAll(pool, hostNames.split(","));
131 LOG.debug(String.format("pool = %s, taken from property: %s", pool, destination + "." + EVENT_POOL_MEMBERS));
132 LOG.debug(String.format("writeTopic = %s, taken from property: %s", eventTopicName, destination + "." + EVENT_TOPIC_WRITE));
133 LOG.debug(String.format("username = %s, taken from property: %s", username, destination + "." + DMAAP_USERNAME));
134 Producer producer = new DmaapProducerImpl(pool, eventTopicName,username, password);
136 for (String url : pool) {
137 if (url.contains("3905") || url.contains("https")) {
138 LOG.debug("Producer should use HTTPS");
139 producer.useHttps(true);
147 public boolean sendEvent(MessageDestination destination, Map<String, String> params, SvcLogicContext ctx) throws APPCException {
149 if (params == null) {
150 String message = "Parameters map is empty (null)";
152 throw new APPCException(message);
154 String eventTime = new Date(System.currentTimeMillis()).toString();
155 String apiVer = params.get("apiVer");
156 String eventId = params.get("eventId");
157 String reason = params.get("reason");
158 String entityId=params.get("entityId");
160 reason=reason+"("+entityId+")";
162 Integer code = Integer.getInteger(params.get("code"), 500);
164 if (eventTime == null || apiVer == null || eventId == null || reason == null) {
165 String message = String.format("Missing input parameters: %s", params);
167 throw new APPCException(message);
169 EventMessage eventMessage = new EventMessage(
170 new EventHeader(eventTime, apiVer, eventId),
171 new EventStatus(code, reason));
173 return sendEvent(destination,eventMessage);