lower code smells
[appc.git] / services / appc-dmaap-service / appc-event-listener-bundle / src / main / java / org / onap / appc / listener / ListenerProperties.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017-2019 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  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.appc.listener;
25
26 import java.util.Properties;
27
28 /**
29  * A class for instantiating Listener objects. It is primarily used to hold properties that start with the given prefix.
30  * It also holds a class that implements {@see Listener} and will be used by the controller to spawn a new listener
31  * object.
32  *
33  * @since Apr 25, 2016
34  * @version $Id$
35  */
36 public class ListenerProperties {
37
38     private String prefix;
39
40     private Class<? extends Listener> listenerClass;
41
42     private Properties props;
43
44     /**
45      * Creates a new listener object with the given prefix and properties. Any property starting with the prefix is
46      * added to the internal properties object with the prefix removed. All other properties are ignored.
47      * ListenerProperties constructor
48      *
49      * @param prefix
50      *            The prefix of the properties to load
51      * @param allProps
52      *            The properties object to load from.
53      */
54     public ListenerProperties(String prefix, Properties allProps) {
55         this.prefix = prefix;
56         props = new Properties();
57
58         String dottedPrefix = String.format("%s.", prefix);
59         for (String key : allProps.stringPropertyNames()) {
60             if (key.startsWith(dottedPrefix) && key.length() > dottedPrefix.length()) {
61                 props.put(key.substring(dottedPrefix.length()), allProps.get(key));
62             }
63         }
64     }
65
66     /**
67      * @return The prefix of these properties
68      */
69     public String getPrefix() {
70         return prefix;
71     }
72
73     /**
74      * Sets the listener class. Will be used by {@see Controller} to instantiate the Listener thread for this object
75      *
76      * @param cls
77      *            The class to be created. Implements {@see Listener}
78      */
79     public void setListenerClass(Class<? extends Listener> cls) {
80         this.listenerClass = cls;
81     }
82
83     /**
84      * @return The class that will be used by {@see Controller} to instantiate the Listener thread for this object
85      */
86     public Class<? extends Listener> getListenerClass() {
87         return listenerClass;
88     }
89
90     /**
91      * Returns a property matching a given KEYS
92      *
93      * @param key
94      *            The KEYS object who's value to return.
95      * @return The value of the property or null if none exists
96      */
97     public String getProperty(KEYS key) {
98         return getProperty(key, null);
99     }
100
101     /**
102      * Returns a property matching a given string.
103      *
104      * @param key
105      *            The key who's value to return.
106      * @return The value of the property or null if none exists
107      */
108     public String getProperty(String key) {
109         return getProperty(key, null);
110     }
111
112     /**
113      * Returns a property matching a given KEYS
114      *
115      * @param key
116      *            The KEYS object who's value to return.
117      * @param defaultValue
118      *            The value to return if the property is not found
119      * @return The value of the property or null if none exists
120      */
121     public String getProperty(KEYS key, String defaultValue) {
122         return getProperty(key.getPropertySuffix(), defaultValue);
123     }
124
125     /**
126      * Returns a property matching a given string.
127      *
128      * @param key
129      *            The key who's value to return.
130      * @param defaultValue
131      *            The value to return if the property is not found
132      * @return The value of the property or null if none exists
133      */
134     public String getProperty(String key, String defaultValue) {
135         return props.getProperty(key, defaultValue);
136     }
137
138     /**
139      * @return The properties object containing all properties
140      */
141     public Properties getProperties() {
142         return props;
143     }
144
145     /**
146      * Reads the <i>prefix</i>.disabled property to determine if the listener is disabled and should not be run by the
147      * controller. Defaults to false if property not set or value cannot be parsed.
148      *
149      * @return true if the listener is disabled and should not be started. false if the listener should be start
150      *         normally (default).
151      */
152     public boolean isDisabled() {
153         return Boolean.valueOf(getProperty(KEYS.DISABLED, "false"));
154     }
155
156     @Override
157     public String toString() {
158         return String.format("%s", prefix);
159     }
160
161
162     /**
163      * Set of common properties that will be used by most systems. Primarily relating to DMaaP and ThreadPools
164      *
165      * @since Apr 25, 2016
166      * @version $Id$
167      */
168     public enum KEYS {
169         /**
170          * Property to determine if the listener should be disabled. If not set, defaults to false
171          */
172         DISABLED("disabled"),
173
174         /**
175          * Property for the message service type. Should be a lower case string. See MessageService.
176          */
177         MESSAGE_SERVICE("service"),
178
179         /**
180          * A hostname or comma separated list (no spaces) of hostnames of servers in a cluster. Can have ports included
181          * as well.<br>
182          * Examples:
183          * <ul>
184          * <li>server1.appc.onap.org</li>
185          * <li>server1.appc.onap.org:3904</li>
186          * <li>server1.appc.onap.org,server2.appc.onap.org</li>
187          * </ul>
188          */
189         HOSTS("poolMembers"),
190
191         /**
192          * The topic that will be used for DMaaP read operations. Can only support a single topic.
193          */
194         TOPIC_READ("topic.read"),
195
196         /**
197          * The topic or topics that will be used to write to. If multiple topics are provided, should be in a comma
198          * seperated list with no spaces.<br>
199          * Examples:
200          * <ul>
201          * <li>TOPIC-1</li>
202          * <li>TOPIC-1,TOPIC-2,ANOTHER-TOPIC</li>
203          * </ul>
204          */
205         TOPIC_WRITE("topic.write"),
206
207         /**
208          * The highland park filter to use on read requests. If you are reading and writing to the same topic this must
209          * be provided. Filter should be in JSON format (not url escaped).
210          */
211         TOPIC_READ_FILTER("topic.read.filter"),
212
213         /**
214          * The amount of time in seconds that the DMaaP polling connection should stay open for. Recommended to be set
215          * high (around 60 seconds) as most clients will return immediately and not wait until the timeout is up to
216          * return if they have data.
217          */
218         TOPIC_READ_TIMEOUT("topic.read.timeout"),
219
220         /**
221          *  Blacklist time for a server with response problem in seconds
222          */
223
224         PROBLEM_WITH_RESPONSE_BLACKLIST_TIME("topic.responseProblem.blacklistTime"),
225         /**
226          *  Blacklist time for a server with server problem in seconds
227          */
228
229         PROBLEM_SERVERSIDE_ERROR_BLACKLIST_TIME("topic.serverError.blacklistTime"),
230         /**
231          *  Blacklist time for a server with DNS problem in seconds
232          */
233
234         PROBLEM_DNS_BLACKLIST_TIME("topic.dnsIssue.blacklistTime"),
235         /**
236          *  Blacklist time for a server with IO Exception problem in seconds
237          */
238
239         PROBLEM_IO_EXCEPTION_BLACKLIST_TIME("topic.ioException.blacklistTime"),
240
241         /**
242          * The name of the client to use. Should be unique to the application.
243          */
244         CLIENT_NAME("client.name"),
245
246         /**
247          * The id of the client to use. Should be unique for each instance of the application in an environment.
248          */
249         CLIENT_ID("client.name.id"),
250
251         /**
252          * The User (DMaaP) to use for authentication. If a user is provided, you must include the
253          * domain name (e.g. example<b>@example.com</b>).
254          */
255         AUTH_USER_KEY("client.key"),
256
257         /**
258          * The password (DMaaP) to use for authentication.
259          */
260         AUTH_SECRET_KEY("client.secret"),
261
262         /**
263          * The minimum amount of size of the queue. A client should request new messages once the queue has dropped
264          * below this size.
265          */
266         THREADS_MIN_QUEUE("threads.queuesize.min"),
267
268         /**
269          * The maximum size of the queue. A client will request no new messages once this maximum size has been reached.
270          */
271         THREADS_MAX_QUEUE("threads.queuesize.max"),
272
273         /**
274          * The minimum size of the worker threads pool. This is the pool each listener will use to launch longer running
275          * operations.
276          */
277         THREADS_MIN_POOL("threads.poolsize.min"),
278
279         /**
280          * The maximum size of the worker threads pool. This is the pool each listener will use to launch longer running
281          * operations.
282          */
283         THREADS_MAX_POOL("threads.poolsize.max");
284
285         private String suffix;
286
287         private KEYS(String val) {
288             this.suffix = val;
289         }
290
291         /**
292          * @param prefix
293          *            The prefix to prepend
294          * @return a fully property name that corroponds to what is used in the properties file. Format is PREFIX.KEY
295          */
296         public String getFullProp(String prefix) {
297             return String.format("%s.%s", prefix, suffix);
298         }
299
300         public String getPropertySuffix() {
301             return suffix;
302         }
303     }
304
305 }