Cleanup sparky-be pom
[aai/sparky-be.git] / sparkybe-onap-service / src / main / java / org / onap / aai / sparky / util / ConfigHelper.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017-2018 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  */
21 package org.onap.aai.sparky.util;
22
23 import java.io.BufferedReader;
24 import java.io.File;
25 import java.io.FileInputStream;
26 import java.io.FileNotFoundException;
27 import java.io.FileReader;
28 import java.io.IOException;
29 import java.io.InputStream;
30 import java.util.Properties;
31 import java.util.Set;
32
33 import org.onap.aai.cl.api.Logger;
34 import org.onap.aai.cl.eelf.LoggerFactory;
35 import org.onap.aai.sparky.logging.AaiUiMsgs;
36
37 /**
38  * The Class ConfigHelper.
39  */
40 public class ConfigHelper {
41
42   private static final Logger LOG = LoggerFactory.getInstance().getLogger(ConfigHelper.class);
43
44   /**
45    * Gets the config with prefix.
46    *
47    * @param configPrefix the config prefix
48    * @param properties the properties
49    * @return the config with prefix
50    */
51   public static Properties getConfigWithPrefix(String configPrefix, Properties properties) {
52
53     /*
54      * The idea here is collect properties groups prefixed with the same origin
55      */
56
57     Set<Object> set = properties.keySet();
58     Properties newProps = new Properties();
59
60     for (Object k : set) {
61       String ks = (String) k;
62       if (ks.startsWith(configPrefix)) {
63
64         String temp = ks.replaceFirst(configPrefix + ".", "");
65         newProps.setProperty(temp, properties.getProperty(ks));
66       }
67     }
68
69     return newProps;
70   }
71
72   /**
73    * Load config.
74    *
75    * @param fileName the file name
76    * @return the properties
77    * @throws Exception the exception
78    */
79   public static Properties loadConfig(String fileName) throws Exception {
80     String basePath = System.getProperty("user.dir");
81     try (InputStream fileInputStream = new FileInputStream(basePath + "//" + fileName)) {
82       Properties props = new Properties();
83       props.load(fileInputStream);
84       return props;
85     }
86   }
87
88   /**
89    * Load config from explicit path.
90    *
91    * @param fileName the file name
92    * @return the properties
93    */
94   public static Properties loadConfigFromExplicitPath(String fileName) {
95
96     Properties props = new Properties();
97     try (InputStream fileInputStream = new FileInputStream(fileName)) {
98       props.load(fileInputStream);
99     } catch (Exception exc) {
100       LOG.warn(AaiUiMsgs.CONFIG_NOT_FOUND_VERBOSE, fileName, exc.getLocalizedMessage());
101     }
102     return props;
103   }
104
105   /**
106    * Property fetch.
107    *
108    * @param config the config
109    * @param propName the prop name
110    * @param defaultValue the default value
111    * @return the string
112    */
113   public static String propertyFetch(Properties config, String propName, String defaultValue) {
114     return config.getProperty(propName, defaultValue);
115   }
116
117   public static boolean isEssDevModeEnabled() {
118     return Boolean.parseBoolean(System.getProperty("isEssDevMode", "false"));
119   }
120
121   /**
122    * Gets the filepath.
123    *
124    * @param fileName the file name
125    * @param isRelativePath the is relative path
126    * @return the filepath
127    */
128   public static String getFilepath(String fileName, boolean isRelativePath) {
129
130     String filepath = null;
131
132     if (isRelativePath) {
133       filepath = System.getProperty("user.dir") + "/" + fileName;
134
135     } else {
136       filepath = fileName;
137     }
138
139     return filepath;
140
141   }
142
143   /**
144    * Gets the file contents.
145    *
146    * @param fileName the file name
147    * @return the file contents
148    * @throws IOException Signals that an I/O exception has occurred.
149    */
150   public static String getFileContents(String fileName) throws IOException {
151
152     LOG.debug(AaiUiMsgs.FILE_READ_IN_PROGRESS, fileName);
153
154     File file = new File(fileName);
155
156     if (!file.exists()) {
157       throw new FileNotFoundException("Failed to load file = " + fileName);
158     }
159
160     if (file.exists() && !file.isDirectory()) {
161       BufferedReader br = new BufferedReader(new FileReader(file));
162       try {
163         StringBuilder sb = new StringBuilder();
164         String line = br.readLine();
165
166         while (line != null) {
167           sb.append(line);
168           sb.append(System.lineSeparator());
169           line = br.readLine();
170         }
171
172         return sb.toString();
173       } finally {
174         br.close();
175       }
176     } else {
177       LOG.warn(AaiUiMsgs.FILE_NOT_FOUND, fileName);
178     }
179
180     return null;
181
182   }
183
184 }