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