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