22563f68aa8742b0bd4f95d2e67277a4cd2a62a9
[appc.git] / appc-client / client-lib / src / main / java / org / openecomp / 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.openecomp.appc.client.impl.protocol;
26
27 import com.att.eelf.configuration.EELFLogger;
28 import com.att.eelf.configuration.EELFManager;
29
30 import java.io.IOException;
31 import java.security.GeneralSecurityException;
32 import java.util.*;
33
34 class UEBMessagingService implements MessagingService {
35
36     private Consumer consumer;
37     private Producer producer;
38
39     private final String DEFAULT_READ_TIMEOUT_MS = "60000";
40     private final String DEFAULT_READ_LIMIT = "1000";
41
42     private int readLimit;
43
44     private final EELFLogger LOG = EELFManager.getInstance().getLogger(UEBMessagingService.class);
45
46     @SuppressWarnings("Since15")
47     public void init(Properties props) throws IOException, GeneralSecurityException, NoSuchFieldException, IllegalAccessException {
48
49         if (props != null) {
50             String readTopic = props.getProperty(UEBPropertiesKeys.TOPIC_READ);
51             String writeTopic = props.getProperty(UEBPropertiesKeys.TOPIC_WRITE);
52             String apiKey = props.getProperty(UEBPropertiesKeys.AUTH_USER);
53             String apiSecret = props.getProperty(UEBPropertiesKeys.AUTH_SECRET);
54             String readTimeoutString = props.getProperty(UEBPropertiesKeys.TOPIC_READ_TIMEOUT, DEFAULT_READ_TIMEOUT_MS);
55             Integer readTimeout = Integer.parseInt(readTimeoutString);
56             String readLimitString = props.getProperty(UEBPropertiesKeys.READ_LIMIT, DEFAULT_READ_LIMIT);
57             readLimit = Integer.parseInt(readLimitString);
58             //get hosts pool
59             Collection<String> pool = new HashSet<String>();
60             String hostNames = props.getProperty(UEBPropertiesKeys.HOSTS);
61             if (hostNames != null && !hostNames.isEmpty()) {
62                 for (String name : hostNames.split(",")) {
63                     pool.add(name);
64                 }
65             }
66
67             //generate consumer id and group - same value for both
68             String consumerName = UUID.randomUUID().toString();
69             String consumerID = consumerName;
70
71             //create consumer and producer
72             consumer = new ConsumerImpl(pool, readTopic, consumerName, consumerID, readTimeout, apiKey, apiSecret);
73             producer = new ProducerImpl(pool, writeTopic, apiKey, apiSecret);
74
75             //initial consumer registration
76             try {
77                 consumer.registerForRead();
78             }catch(Exception e){
79                 LOG.error("Message consumer failed to register client "+consumerID);
80             }
81         }
82     }
83
84     public void send(String partition, String body) throws IOException {
85         producer.post(partition, body);
86     }
87
88     public List<String> fetch() throws IOException {
89         return consumer.fetch(readLimit);
90     }
91
92     public List<String> fetch(int limit) throws IOException {
93         return consumer.fetch(limit);
94     }
95
96     @Override
97     public void close() {
98         consumer.close();
99         producer.close();
100     }
101
102 }