3c5629f11be330249598f8811c033d7bb49ea3a9
[appc.git] / appc-oam / appc-oam-bundle / src / main / java / org / openecomp / appc / oam / util / ConfigurationHelper.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.onap.appc.oam.util;
26
27 import com.att.eelf.configuration.EELFLogger;
28 import org.apache.commons.lang3.ArrayUtils;
29 import org.onap.appc.Constants;
30 import org.onap.appc.configuration.Configuration;
31 import org.onap.appc.configuration.ConfigurationFactory;
32
33 import java.util.concurrent.TimeUnit;
34
35 /**
36  * Utility class provides general configuration helps
37  */
38 public class ConfigurationHelper {
39     final static String PROP_KEY_APPC_NAME = Constants.PROPERTY_APPLICATION_NAME;
40     final static String PROP_KEY_METRIC_STATE = "metric.enabled";
41     private final String OAM_OPERATION_TIMEOUT_SECOND = "appc.OAM.api.timeout";
42     /** Default operation timeout set to 1 minute */
43     private final int DEFAULT_OAM_OPERATION_TIMEOUT = 60;
44
45     private final EELFLogger logger;
46     private Configuration configuration = ConfigurationFactory.getConfiguration();
47
48     public ConfigurationHelper(EELFLogger eelfLogger) {
49         logger = eelfLogger;
50     }
51
52     public String getAppcName() {
53         return configuration.getProperty(PROP_KEY_APPC_NAME);
54     }
55
56     public boolean isMetricEnabled() {
57         return configuration.getBooleanProperty(PROP_KEY_METRIC_STATE, false);
58     }
59
60     public Configuration getConfig() {
61         return configuration;
62     }
63
64     /**
65      * Read property value of a specified property key
66      *
67      * @param propertyKey string of the property key
68      * @return String[] of the property values associated with the propertyKey
69      */
70     String[] readProperty(String propertyKey) {
71         String propertyValue = configuration.getProperty(propertyKey);
72         if (propertyValue == null) {
73             return ArrayUtils.EMPTY_STRING_ARRAY;
74         }
75
76         if (logger.isDebugEnabled()) {
77             logger.debug(String.format("Property[%s] has value (%s).", propertyKey, propertyValue));
78         }
79
80         if (propertyValue.contains(",")) {
81             return propertyValue.split("\\s*,\\s*");
82         }
83         return new String[]{propertyValue};
84     }
85
86
87
88
89
90     /**
91      * This method returns timeout in milliseconds.  The source is chosen in the following order:
92      * The overrideTimeoutSeconds argument
93      * or {@link #OAM_OPERATION_TIMEOUT_SECOND} found in the configuration file
94      * or the {@link #DEFAULT_OAM_OPERATION_TIMEOUT}
95      * @param overrideTimeoutSeconds  or null to us the other sources
96      * @return timeout in milliseconds
97      */
98     public long getOAMOperationTimeoutValue(Integer overrideTimeoutSeconds) {
99         return TimeUnit.SECONDS.toMillis(
100                 overrideTimeoutSeconds == null ?
101             getConfig().getIntegerProperty(OAM_OPERATION_TIMEOUT_SECOND, DEFAULT_OAM_OPERATION_TIMEOUT)
102             :
103             overrideTimeoutSeconds
104         );
105     }
106 }