Refactor properties used for topic names
[appc.git] / appc-client / client-lib / src / main / java / org / onap / appc / client / impl / protocol / UEBMessagingService.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Copyright (C) 2017 Amdocs
8  * =============================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.onap.appc.client.impl.protocol;
26
27 import com.att.eelf.configuration.EELFLogger;
28 import com.att.eelf.configuration.EELFManager;
29 import java.io.IOException;
30 import java.security.GeneralSecurityException;
31 import java.util.Arrays;
32 import java.util.Collection;
33 import java.util.HashSet;
34 import java.util.List;
35 import java.util.Properties;
36 import java.util.UUID;
37
38 class UEBMessagingService implements MessagingService {
39
40     private final EELFLogger logger = EELFManager.getInstance().getLogger(UEBMessagingService.class);
41
42     private static final String DEFAULT_READ_TIMEOUT_MS = "60000";
43     private static final String DEFAULT_READ_LIMIT = "1000";
44     private static final String DEFAULT_READ_TOPIC = "client-read";
45     private static final String DEFAULT_WRITE_TOPIC = "client-write";
46
47     private Consumer consumer;
48     private Producer producer;
49     private int readLimit;
50
51     @Override
52     @SuppressWarnings("Since15")
53     public void init(Properties props)
54             throws IOException, GeneralSecurityException, NoSuchFieldException, IllegalAccessException {
55
56         if (props != null) {
57             String readTopic = null;
58             String writeTopic = null;
59             String cType = props.getProperty(UEBPropertiesKeys.CONTROLLER_TYPE); //CONTROLLER_TYPE = "controllerType"
60             
61             if (cType != null && cType.length()!= 0 && (!cType.equals("APPC")))
62             {
63                 logger.debug("Using controller type " + cType + " for topic properties");
64                 
65                 readTopic = props.getProperty(cType + "-" + UEBPropertiesKeys.TOPIC_READ);
66                 if(readTopic == null) {
67                     logger.error("Error reading property '"+ cType + "-" + UEBPropertiesKeys.TOPIC_READ + "' defaulting to " + DEFAULT_READ_TOPIC);
68                     readTopic = DEFAULT_READ_TOPIC;
69                 }
70                 writeTopic = props.getProperty(cType + "-" + UEBPropertiesKeys.TOPIC_WRITE);
71                 if(writeTopic == null) {
72                     logger.error("Error reading property '"+ cType + "-" + UEBPropertiesKeys.TOPIC_READ + "' defaulting to " + DEFAULT_WRITE_TOPIC);
73                     writeTopic = DEFAULT_WRITE_TOPIC;
74                 }
75             }
76             else {
77                 readTopic = props.getProperty(UEBPropertiesKeys.TOPIC_READ); //TOPIC_READ = "topic.read"
78                 if(readTopic == null) {
79                     logger.error("Error reading property '"+ UEBPropertiesKeys.TOPIC_READ + "' defaulting to " + DEFAULT_READ_TOPIC);
80                     readTopic = DEFAULT_READ_TOPIC;
81                 }
82                 writeTopic = props.getProperty(UEBPropertiesKeys.TOPIC_WRITE); //TOPIC_WRITE = "topic.write"
83                 if(writeTopic == null) {
84                     logger.error("Error reading property '" + UEBPropertiesKeys.TOPIC_READ + "' defaulting to " + DEFAULT_WRITE_TOPIC);
85                     writeTopic = DEFAULT_WRITE_TOPIC;
86                 }
87             }
88             
89             logger.debug("Using topics: Read = '" + readTopic + "' Write = '" + writeTopic + "'");
90             
91             String apiKey = props.getProperty(UEBPropertiesKeys.AUTH_USER);
92             String apiSecret = props.getProperty(UEBPropertiesKeys.AUTH_SECRET);
93             String readTimeoutString = props.getProperty(UEBPropertiesKeys.TOPIC_READ_TIMEOUT, DEFAULT_READ_TIMEOUT_MS);
94             Integer readTimeout = Integer.parseInt(readTimeoutString);
95             String readLimitString = props.getProperty(UEBPropertiesKeys.READ_LIMIT, DEFAULT_READ_LIMIT);
96             readLimit = Integer.parseInt(readLimitString);
97             //get hosts pool
98             Collection<String> pool = new HashSet<>();
99             String hostNames = props.getProperty(UEBPropertiesKeys.HOSTS);
100             if (hostNames != null && !hostNames.isEmpty()) {
101                 pool.addAll(Arrays.asList(hostNames.split(",")));
102             }
103             //generate consumer id and group - same value for both
104             String consumerName = UUID.randomUUID().toString();
105
106             //create consumer and producer
107             consumer = new ConsumerImpl(pool, readTopic, consumerName, consumerName, readTimeout, apiKey, apiSecret);
108             producer = new ProducerImpl(pool, writeTopic, apiKey, apiSecret);
109
110             //initial consumer registration
111             try {
112                 consumer.registerForRead();
113             } catch (Exception e) {
114                 logger.error("Message consumer failed to register client " + consumerName, e);
115             }
116         }
117     }
118
119     @Override
120     public void send(String partition, String body) throws IOException {
121         producer.post(partition, body);
122     }
123
124     @Override
125     public List<String> fetch() throws IOException {
126         return consumer.fetch(readLimit);
127     }
128
129     @Override
130     public List<String> fetch(int limit) throws IOException {
131         return consumer.fetch(limit);
132     }
133
134     @Override
135     public void close() {
136         consumer.close();
137         producer.close();
138     }
139
140 }