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