1e1b9e7f8f85a32412176ab7773525ad8fdf636e
[appc.git] / services / appc-dmaap-service / appc-dmaap-event-service / src / main / java / org / onap / appc / services / dmaapService / PublishService.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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.
18  * 
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.appc.services.dmaapService;
23
24 import java.util.HashMap;
25 import java.util.HashSet;
26 import java.util.Map;
27 import java.util.Properties;
28
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;
36
37 @Service
38 public class PublishService {
39     
40     private Map<String,Producer> producers;
41     private MessageAdapterFactory factory;
42     Configuration configuration;
43     
44     public PublishService() {
45         this.factory = new DmaapMessageAdapterFactoryImpl();
46         producers = new HashMap<>();
47     }
48     
49     public PublishService(MessageAdapterFactory factory) {
50         this.factory = factory;
51         producers = new HashMap<>();
52     }
53     
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;
58         }
59         boolean success = producer.post(partition, message);
60         if(success) {
61             return "Success";
62         }
63         return "Failed. See dmaap service jar log.";
64     }
65     
66     private Producer getProducer(String key, String topic) {
67         String searchKey = key;
68         if(topic != null) {
69             searchKey += topic;
70         }
71         Producer producer = producers.get(searchKey);
72         if(producer != null) {
73             return producer;
74         }
75         producer =  newProducer(key, topic);
76         producers.put(searchKey,producer);
77         return producer;
78     }
79     
80     private Producer newProducer(String key, String topic) {
81         Configuration configuration;
82         if(this.configuration != null) {
83             configuration = this.configuration;
84         } else {
85             configuration = ConfigurationFactory.getConfiguration();
86         }
87         Properties props = configuration.getProperties();
88         HashSet<String> pool = new HashSet<>();
89         if (props != null) {
90             String writeTopic;
91             if(topic == null) {
92                 writeTopic = props.getProperty(key + ".topic.write");
93             } else {
94                 writeTopic = topic;
95             }
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(",")) {
101                     pool.add(name);
102                 }
103             }
104             return factory.createProducer(pool, writeTopic, apiKey, apiSecret);
105         }
106         return null;
107     }
108     
109 }