Client to use controller type for DMaaP partition
[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
45     private Consumer consumer;
46     private Producer producer;
47     private int readLimit;
48
49     @Override
50     @SuppressWarnings("Since15")
51     public void init(Properties props)
52             throws IOException, GeneralSecurityException, NoSuchFieldException, IllegalAccessException {
53
54         if (props != null) {
55             String readTopic = props.getProperty(UEBPropertiesKeys.TOPIC_READ);
56             String writeTopic = props.getProperty(UEBPropertiesKeys.TOPIC_WRITE);
57             String cType = props.getProperty(UEBPropertiesKeys.CONTROLLER_TYPE);
58             if (cType != null && cType.length()!= 0 && (!cType.equals("APPC")))
59             {
60                 readTopic = cType + "-" + readTopic;
61                 writeTopic = cType + "-" + writeTopic;
62             }
63             String apiKey = props.getProperty(UEBPropertiesKeys.AUTH_USER);
64             String apiSecret = props.getProperty(UEBPropertiesKeys.AUTH_SECRET);
65             String readTimeoutString = props.getProperty(UEBPropertiesKeys.TOPIC_READ_TIMEOUT, DEFAULT_READ_TIMEOUT_MS);
66             Integer readTimeout = Integer.parseInt(readTimeoutString);
67             String readLimitString = props.getProperty(UEBPropertiesKeys.READ_LIMIT, DEFAULT_READ_LIMIT);
68             readLimit = Integer.parseInt(readLimitString);
69             //get hosts pool
70             Collection<String> pool = new HashSet<>();
71             String hostNames = props.getProperty(UEBPropertiesKeys.HOSTS);
72             if (hostNames != null && !hostNames.isEmpty()) {
73                 pool.addAll(Arrays.asList(hostNames.split(",")));
74             }
75             //generate consumer id and group - same value for both
76             String consumerName = UUID.randomUUID().toString();
77
78             //create consumer and producer
79             consumer = new ConsumerImpl(pool, readTopic, consumerName, consumerName, readTimeout, apiKey, apiSecret);
80             producer = new ProducerImpl(pool, writeTopic, apiKey, apiSecret);
81
82             //initial consumer registration
83             try {
84                 consumer.registerForRead();
85             } catch (Exception e) {
86                 logger.error("Message consumer failed to register client " + consumerName, e);
87             }
88         }
89     }
90
91     @Override
92     public void send(String partition, String body) throws IOException {
93         producer.post(partition, body);
94     }
95
96     @Override
97     public List<String> fetch() throws IOException {
98         return consumer.fetch(readLimit);
99     }
100
101     @Override
102     public List<String> fetch(int limit) throws IOException {
103         return consumer.fetch(limit);
104     }
105
106     @Override
107     public void close() {
108         consumer.close();
109         producer.close();
110     }
111
112 }