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;
34 import com.att.eelf.configuration.EELFLogger;
35 import com.att.eelf.configuration.EELFManager;
37 import org.onap.appc.adapter.message.MessageAdapterFactory;
38 import org.onap.appc.adapter.message.Producer;
41 public class PublishService {
43 private static final EELFLogger LOG = EELFManager.getInstance().getLogger(PublishService.class);
45 private Map<String,Producer> producers;
46 private MessageAdapterFactory factory;
47 Configuration configuration;
49 public PublishService() {
50 this.factory = new DmaapMessageAdapterFactoryImpl();
51 producers = new HashMap<>();
54 public PublishService(MessageAdapterFactory factory) {
55 this.factory = factory;
56 producers = new HashMap<>();
59 public String publishMessage(String key, String partition, String topic, String message) {
60 Producer producer = getProducer(key, topic);
61 if(producer == null) {
62 return "Could not find producer with property prefix: " + key;
64 boolean success = producer.post(partition, message);
68 return "Failed. See dmaap service jar log.";
71 private Producer getProducer(String key, String topic) {
72 String searchKey = key;
76 Producer producer = producers.get(searchKey);
77 if(producer != null) {
80 producer = newProducer(key, topic);
81 producers.put(searchKey,producer);
85 private Producer newProducer(String key, String topic) {
86 Configuration configuration;
87 if(this.configuration != null) {
88 configuration = this.configuration;
90 configuration = ConfigurationFactory.getConfiguration();
92 Properties props = configuration.getProperties();
93 HashSet<String> pool = new HashSet<>();
97 writeTopic = props.getProperty(key + ".topic.write");
101 String apiKey = props.getProperty(key + ".client.key");
102 String apiSecret = props.getProperty(key + ".client.secret");
103 String hostnames = props.getProperty(key + ".poolMembers");
104 if (hostnames != null && !hostnames.isEmpty()) {
105 for (String name : hostnames.split(",")) {
110 LOG.error("There are no dmaap server pools. Check the property " + key + ".poolMembers");
113 if(writeTopic == null || writeTopic.isEmpty()) {
114 LOG.error("There is no write topic defined in the message request or in the properties file");
117 return factory.createProducer(pool, writeTopic, apiKey, apiSecret);