cc20539f4823ec80bcdb5adde78d8a75b2d6519c
[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
34 import com.att.eelf.configuration.EELFLogger;
35 import com.att.eelf.configuration.EELFManager;
36
37 import org.onap.appc.adapter.message.MessageAdapterFactory;
38 import org.onap.appc.adapter.message.Producer;
39 import org.onap.appc.adapter.messaging.dmaap.http.HttpDmaapProducerImpl;
40
41 @Service
42 public class PublishService {
43     
44     private static final EELFLogger LOG = EELFManager.getInstance().getLogger(PublishService.class);
45     
46     private Map<String,Producer> producers;
47     private MessageAdapterFactory factory;
48     Configuration configuration;
49     
50     public PublishService() {
51         this.factory = new DmaapMessageAdapterFactoryImpl();
52         producers = new HashMap<>();
53     }
54     
55     public PublishService(MessageAdapterFactory factory) {
56         this.factory = factory;
57         producers = new HashMap<>();
58     }
59     
60     public String publishMessage(String key, String partition, String topic, String message) {
61         Producer producer = getProducer(key, topic);
62         if(producer == null) {
63             return "Could not find producer with property prefix: " + key;
64         }
65         boolean success = producer.post(partition, message);
66         if(success) {
67             return "Success";
68         }
69         return "Failed. See dmaap service jar log.";
70     }
71     
72     private Producer getProducer(String key, String topic) {
73         String searchKey = key;
74         if(topic != null) {
75             searchKey += topic;
76         }
77         Producer producer = producers.get(searchKey);
78         if(producer != null) {
79             return producer;
80         }
81         producer =  newProducer(key, topic);
82         producers.put(searchKey,producer);
83         return producer;
84     }
85     
86     private Producer newProducer(String key, String topic) {
87         Configuration configuration;
88         if(this.configuration != null) {
89             configuration = this.configuration;
90         } else {
91             configuration = ConfigurationFactory.getConfiguration();
92         }
93         Properties props = configuration.getProperties();
94         HashSet<String> pool = new HashSet<>();
95         if (props != null) {
96             String writeTopic;
97             if(topic == null) {
98                 writeTopic = props.getProperty(key + ".topic.write");
99             } else {
100                 writeTopic = topic;
101             }
102             String apiKey = props.getProperty(key + ".client.key");
103             String apiSecret = props.getProperty(key + ".client.secret");
104             String hostnames = props.getProperty(key + ".poolMembers");
105             if (hostnames != null && !hostnames.isEmpty()) {
106                 for (String name : hostnames.split(",")) {
107                     pool.add(name);
108                 }
109             }
110             if(pool.isEmpty()) {
111                 LOG.error("There are no dmaap server pools. Check the property " + key + ".poolMembers");
112                 return null;
113             }
114             if(writeTopic == null || writeTopic.isEmpty()) {
115                 LOG.error("There is no write topic defined in the message request or in the properties file");
116                 return null;
117             }
118             return factory.createProducer(pool, writeTopic, apiKey, apiSecret);
119         }
120         return null;
121     }
122     
123 }