aboutsummaryrefslogtreecommitdiffstats
path: root/message-router/consumer/provider/src/main/java/org/onap/ccsdk/sli/adaptors/messagerouter/consumer/provider/impl/ConsumerFactory.java
blob: 1aa02c70a12f5d0a98c37cc03213c98305bf3c60 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*-
 * ============LICENSE_START=======================================================
 * openECOMP : SDN-C
 * ================================================================================
 * Copyright (C) 2017 - 2019 AT&T Intellectual Property. All rights
 *                      reserved.
 *                      
 * Modifications Copyright (C) 2019 IBM.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */
package org.onap.ccsdk.sli.adaptors.messagerouter.consumer.provider.impl;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ConsumerFactory {
    private static final Logger LOG = LoggerFactory.getLogger(ConsumerFactory.class);

    // Default values to minimize required configuration
    private static final int DEFAULT_FETCH_PAUSE = 5000;
    private static final int DEFAULT_CONNECT_TIMEOUT = 30000;
    private static final int DEFAULT_READ_TIMEOUT = 180000;
    private static final int DEFAULT_LIMIT = 5; // Limits the number of messages pulled in a single GET request
    private static final int DEFAULT_TIMEOUT_QUERY_PARAM_VALUE = 15000;
    private static final String DEFAULT_AUTH_METHOD = "basic";

    // Required properties
    protected final String username;
    protected final String password;
    protected final String host;
    private final String group;
    private final String id;

    // Optional properties
    protected Integer connectTimeout;
    protected Integer readTimeout;
    private Integer fetchPause;
    private Integer limit;
    private Integer timeoutQueryParamValue;
    private String filter;
    protected String auth;
    
    public ConsumerFactory(Properties properties) {
    	// Required properties
    	username = properties.getProperty("username");
    	password = properties.getProperty("password");
    	host = properties.getProperty("host");
    	auth = properties.getProperty("auth");
    	group = properties.getProperty("group");
    	id = properties.getProperty("id");

    	// Optional properties
    	connectTimeout = readOptionalInteger(properties, "connectTimeoutSeconds");
    	readTimeout = readOptionalInteger(properties, "readTimeoutMinutes");
    	fetchPause = readOptionalInteger(properties, "fetchPause");
    	limit = readOptionalInteger(properties, "limit");
    	timeoutQueryParamValue = readOptionalInteger(properties, "timeout");
    	processFilter(properties.getProperty("filter"));

    	setDefaults();
        }
    
    public ConsumerFactory(String username, String password, String host, String group, String id, Integer connectTimeout, Integer readTimeout) {
    	this.username = username;
    	this.password = password;
    	this.host = host;
    	this.group = group;
    	this.id = id;
    	setDefaults();
        }

    
    public String getAuth() {
	return auth;
    }

    public void setAuth(String auth) {
	this.auth = auth;
    }

    public Integer getConnectTimeout() {
	return connectTimeout;
    }

    public void setConnectTimeout(Integer connectTimeout) {
	this.connectTimeout = connectTimeout;
    }

    public Integer getReadTimeout() {
	return readTimeout;
    }

    public void setReadTimeout(Integer readTimeout) {
	this.readTimeout = readTimeout;
    }

    public Integer getFetchPause() {
	return fetchPause;
    }

    public void setFetchPause(Integer fetchPause) {
	this.fetchPause = fetchPause;
    }

    public Integer getLimit() {
	return limit;
    }

    public void setLimit(Integer limit) {
	this.limit = limit;
    }

    public Integer getTimeoutQueryParamValue() {
	return timeoutQueryParamValue;
    }

    public void setTimeoutQueryParamValue(Integer timeoutQueryParamValue) {
	this.timeoutQueryParamValue = timeoutQueryParamValue;
    }

    public String getFilter() {
	return filter;
    }

    public void setFilter(String filter) {
	processFilter(filter);
    }

    private Integer readOptionalInteger(Properties properties, String propertyName) {
	String stringValue = properties.getProperty(propertyName);
	if (stringValue != null && stringValue.length() > 0) {
	    try {
		return Integer.valueOf(stringValue);
	    } catch (NumberFormatException e) {
		LOG.error("property " + propertyName + " had the value " + stringValue + " that could not be converted to an Integer", e);
	    }
	}
	return null;
    }

    public PollingConsumerImpl createPollingClient() {
	return new PollingConsumerImpl(username, password, host, auth, connectTimeout, readTimeout, fetchPause, group, id, filter, limit, timeoutQueryParamValue);
    }

    public PullingConsumerImpl createPullingClient() {
	return new PullingConsumerImpl(username, password, host, auth, connectTimeout, readTimeout, group, id, filter, limit, timeoutQueryParamValue);
    }

    private void processFilter(String filterString) {
	if (filterString != null) {
	    if (filterString.length() > 0) {
		try {
		    filter = URLEncoder.encode(filterString, StandardCharsets.UTF_8.name());
		} catch (UnsupportedEncodingException e) {
		    LOG.warn("Couldn't encode filter string. Filter will be ignored.", e);
		    filter = null;
		}
	    } else {
		filter = null;
	    }
	}
    }

    private void setDefaults() {
	if (connectTimeout == null) {
	    connectTimeout = DEFAULT_CONNECT_TIMEOUT;
	}
	if (readTimeout == null) {
	    readTimeout = DEFAULT_READ_TIMEOUT;
	}
	if (fetchPause == null) {
	    fetchPause = DEFAULT_FETCH_PAUSE;
	}
	if (limit == null) {
	    limit = DEFAULT_LIMIT;
	}
	if (timeoutQueryParamValue == null) {
	    timeoutQueryParamValue = DEFAULT_TIMEOUT_QUERY_PARAM_VALUE;
	}
	if (auth == null) {
	    auth = DEFAULT_AUTH_METHOD;
	}
    }

}