2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.appc.services.dmaapService;
24 import java.util.HashMap;
25 import java.util.HashSet;
27 import java.util.Properties;
29 import org.onap.appc.adapter.factory.DmaapMessageAdapterFactoryImpl;
30 import org.onap.appc.configuration.Configuration;
31 import org.onap.appc.configuration.ConfigurationFactory;
32 import org.springframework.stereotype.Service;
33 import org.onap.appc.adapter.message.MessageAdapterFactory;
34 import org.onap.appc.adapter.message.Producer;
35 import org.onap.appc.adapter.messaging.dmaap.http.HttpDmaapProducerImpl;
38 public class PublishService {
40 private Map<String,Producer> producers;
41 private MessageAdapterFactory factory;
42 Configuration configuration;
44 public PublishService() {
45 this.factory = new DmaapMessageAdapterFactoryImpl();
46 producers = new HashMap<>();
49 public PublishService(MessageAdapterFactory factory) {
50 this.factory = factory;
51 producers = new HashMap<>();
54 public String publishMessage(String key, String partition, String topic, String message) {
55 Producer producer = getProducer(key, topic);
56 if(producer == null) {
57 return "Could not find producer with property prefix: " + key;
59 boolean success = producer.post(partition, message);
63 return "Failed. See dmaap service jar log.";
66 private Producer getProducer(String key, String topic) {
67 String searchKey = key;
71 Producer producer = producers.get(searchKey);
72 if(producer != null) {
75 producer = newProducer(key, topic);
76 producers.put(searchKey,producer);
80 private Producer newProducer(String key, String topic) {
81 Configuration configuration;
82 if(this.configuration != null) {
83 configuration = this.configuration;
85 configuration = ConfigurationFactory.getConfiguration();
87 Properties props = configuration.getProperties();
88 HashSet<String> pool = new HashSet<>();
92 writeTopic = props.getProperty(key + ".topic.write");
96 String apiKey = props.getProperty(key + ".client.key");
97 String apiSecret = props.getProperty(key + ".client.secret");
98 String hostnames = props.getProperty(key + ".poolMembers");
99 if (hostnames != null && !hostnames.isEmpty()) {
100 for (String name : hostnames.split(",")) {
104 return factory.createProducer(pool, writeTopic, apiKey, apiSecret);