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