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