summaryrefslogtreecommitdiffstats
path: root/appc-event-listener/appc-event-listener-bundle/src/main/java/org/openecomp/appc/listener/ListenerProperties.java
blob: c6c374eb0bedc958ca7cc27168fa03f0b8860b7e (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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/*-
 * ============LICENSE_START=======================================================
 * openECOMP : APP-C
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights
 * 						reserved.
 * ================================================================================
 * 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.openecomp.appc.listener;

import java.util.Properties;

/**
 * A class for instantiating Listener objects. It is primarily used to hold properties that start with the given prefix.
 * It also holds a class that implements {@see Listener} and will be used by the controller to spawn a new listener
 * object.
 *
 * @since Apr 25, 2016
 * @version $Id$
 */
public class ListenerProperties {

    private String prefix;

    private Class<? extends Listener> listenerClass;

    private Properties props;

    /**
     * Creates a new listener object with the given prefix and properties. Any property starting with the prefix is
     * added to the internal properties object with the prefix removed. All other properties are ignored.
     * ListenerProperties constructor
     *
     * @param prefix
     *            The prefix of the properties to load
     * @param allProps
     *            The properties object to load from.
     */
    public ListenerProperties(String prefix, Properties allProps) {
        this.prefix = prefix;
        props = new Properties();

        String dottedPrefix = String.format("%s.", prefix);
        for (String key : allProps.stringPropertyNames()) {
            if (key.startsWith(dottedPrefix) && key.length() > dottedPrefix.length()) {
                props.put(key.substring(dottedPrefix.length()), allProps.get(key));
            }
        }
    }

    /**
     * @return The prefix of these properties
     */
    public String getPrefix() {
        return prefix;
    }

    /**
     * Sets the listener class. Will be used by {@see Controller} to instantiate the Listener thread for this object
     *
     * @param cls
     *            The class to be created. Implements {@see Listener}
     */
    public void setListenerClass(Class<? extends Listener> cls) {
        this.listenerClass = cls;
    }

    /**
     * @return The class that will be used by {@see Controller} to instantiate the Listener thread for this object
     */
    public Class<? extends Listener> getListenerClass() {
        return listenerClass;
    }

    /**
     * Returns a property matching a given KEYS
     * 
     * @param key
     *            The KEYS object who's value to return.
     * @return The value of the property or null if none exists
     */
    public String getProperty(KEYS key) {
        return getProperty(key, null);
    }

    /**
     * Returns a property matching a given string.
     * 
     * @param key
     *            The key who's value to return.
     * @return The value of the property or null if none exists
     */
    public String getProperty(String key) {
        return getProperty(key, null);
    }

    /**
     * Returns a property matching a given KEYS
     * 
     * @param key
     *            The KEYS object who's value to return.
     * @param defaultValue
     *            The value to return if the property is not found
     * @return The value of the property or null if none exists
     */
    public String getProperty(KEYS key, String defaultValue) {
        return getProperty(key.getPropertySuffix(), defaultValue);
    }

    /**
     * Returns a property matching a given string.
     * 
     * @param key
     *            The key who's value to return.
     * @param defaultValue
     *            The value to return if the property is not found
     * @return The value of the property or null if none exists
     */
    public String getProperty(String key, String defaultValue) {
        return props.getProperty(key, defaultValue);
    }

    /**
     * @return The properties object containing all properties
     */
    public Properties getProperties() {
        return props;
    }

    /**
     * Reads the <i>prefix</i>.disabled property to determine if the listener is disabled and should not be run by the
     * controller. Defaults to false if property not set or value cannot be parsed.
     *
     * @return true if the listener is disabled and should not be started. false if the listener should be start
     *         normally (default).
     */
    public boolean isDisabled() {
        return Boolean.valueOf(getProperty(KEYS.DISABLED, "false"));
    }

    @Override
    public String toString() {
        return String.format("%s", prefix);
    }

    /**
     * The message service types that are available. Choices are DMaaP and DMaaP.
     *
     * @since Apr 25, 2016
     * @version $Id$
     */
    public enum MessageService {
        DMaaP("dmaap");

        private String val;

        private MessageService(String val) {
            this.val = val;
        }

        public String getValue() {
            return val;
        }

        /**
         * Tries to match a string to a MessageService. If no match is found, returns the default (DMaaP)
         * 
         * @param input
         *            the string to try and match
         * @return A MessasgeService
         */
        public static MessageService parse(String input) {
            if (input != null) {
                for (MessageService ms : MessageService.values()) {
                    if (ms.getValue().equals(input.toLowerCase())) {
                        return ms;
                    }
                }
            }
            return MessageService.DMaaP; // Default
        }
    }

    /**
     * Set of common properties that will be used by most systems. Primarily relating to DMaaP and ThreadPools
     *
     * @since Apr 25, 2016
     * @version $Id$
     */
    public enum KEYS {
        /**
         * Property to determine if the listener should be disabled. If not set, defaults to false
         */
        DISABLED("disabled"),

        /**
         * Property for the message service type. Should be a lower case string. See MessageService.
         */
        MESSAGE_SERVICE("service"),

        /**
         * A hostname or comma separated list (no spaces) of hostnames of servers in a cluster. Can have ports included
         * as well.<br>
         * Examples:
         * <ul>
         * <li>server1.appc.openecomp.org</li>
         * <li>server1.appc.openecomp.org:3904</li>
         * <li>server1.appc.openecomp.org,server2.appc.openecomp.org</li>
         * </ul>
         */
        HOSTS("poolMembers"),

        /**
         * The topic that will be used for DMaaP read operations. Can only support a single topic.
         */
        TOPIC_READ("topic.read"),

        /**
         * The topic or topics that will be used to write to. If multiple topics are provided, should be in a comma
         * seperated list with no spaces.<br>
         * Examples:
         * <ul>
         * <li>TOPIC-1</li>
         * <li>TOPIC-1,TOPIC-2,ANOTHER-TOPIC</li>
         * </ul>
         */
        TOPIC_WRITE("topic.write"),

        /**
         * The highland park filter to use on read requests. If you are reading and writing to the same topic this must
         * be provided. Filter should be in JSON format (not url escaped).
         */
        TOPIC_READ_FILTER("topic.read.filter"),

        /**
         * The amount of time in seconds that the DMaaP polling connection should stay open for. Recommended to be set
         * high (around 60 seconds) as most clients will return immediately and not wait until the timeout is up to
         * return if they have data.
         */
        TOPIC_READ_TIMEOUT("topic.read.timeout"),

        /**
         * The name of the client to use. Should be unique to the application.
         */
        CLIENT_NAME("client.name"),

        /**
         * The id of the client to use. Should be unique for each instance of the application in an environment.
         */
        CLIENT_ID("client.name.id"),

        /**
         * The User (DMaaP) to use for authentication. If a user is provided, you must include the
         * domain name (e.g. example<b>@example.com</b>).
         */
        AUTH_USER_KEY("client.key"),

        /**
         * The password (DMaaP) to use for authentication.
         */
        AUTH_SECRET_KEY("client.secret"),

        /**
         * The minimum amount of size of the queue. A client should request new messages once the queue has dropped
         * below this size.
         */
        THREADS_MIN_QUEUE("threads.queuesize.min"),

        /**
         * The maximum size of the queue. A client will request no new messages once this maximum size has been reached.
         */
        THREADS_MAX_QUEUE("threads.queuesize.max"),

        /**
         * The minimum size of the worker threads pool. This is the pool each listener will use to launch longer running
         * operations.
         */
        THREADS_MIN_POOL("threads.poolsize.min"),

        /**
         * The maximum size of the worker threads pool. This is the pool each listener will use to launch longer running
         * operations.
         */
        THREADS_MAX_POOL("threads.poolsize.max");

        private String suffix;

        private KEYS(String val) {
            this.suffix = val;
        }

        /**
         * @param prefix
         *            The prefix to prepend
         * @return a fully property name that corroponds to what is used in the properties file. Format is PREFIX.KEY
         */
        public String getFullProp(String prefix) {
            return String.format("%s.%s", prefix, suffix);
        }

        public String getPropertySuffix() {
            return suffix;
        }
    }

}