Update license and poms
[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
81     String basePath = System.getProperty("user.dir");
82     InputStream fileInputStream = new FileInputStream(basePath + "//" + fileName);
83
84     Properties props = new Properties();
85     props.load(fileInputStream);
86
87     return props;
88   }
89
90   /**
91    * Load config from explicit path.
92    *
93    * @param fileName the file name
94    * @return the properties
95    */
96   public static Properties loadConfigFromExplicitPath(String fileName) {
97
98     Properties props = new Properties();
99
100     try {
101       InputStream fileInputStream = new FileInputStream(fileName);
102       props.load(fileInputStream);
103     } catch (Exception exc) {
104       LOG.warn(AaiUiMsgs.CONFIG_NOT_FOUND_VERBOSE, fileName, exc.getLocalizedMessage());
105     }
106
107     return props;
108   }
109
110   /**
111    * Property fetch.
112    *
113    * @param config the config
114    * @param propName the prop name
115    * @param defaultValue the default value
116    * @return the string
117    */
118   public static String propertyFetch(Properties config, String propName, String defaultValue) {
119     return config.getProperty(propName, defaultValue);
120   }
121
122   public static boolean isEssDevModeEnabled() {
123     return Boolean.parseBoolean(System.getProperty("isEssDevMode", "false"));
124   }
125
126   /**
127    * Gets the filepath.
128    *
129    * @param fileName the file name
130    * @param isRelativePath the is relative path
131    * @return the filepath
132    */
133   public static String getFilepath(String fileName, boolean isRelativePath) {
134
135     String filepath = null;
136
137     if (isRelativePath) {
138       filepath = System.getProperty("user.dir") + "/" + fileName;
139
140     } else {
141       filepath = fileName;
142     }
143
144     return filepath;
145
146   }
147
148   /**
149    * Gets the file contents.
150    *
151    * @param fileName the file name
152    * @return the file contents
153    * @throws IOException Signals that an I/O exception has occurred.
154    */
155   public static String getFileContents(String fileName) throws IOException {
156
157     LOG.debug(AaiUiMsgs.FILE_READ_IN_PROGRESS, fileName);
158
159     File file = new File(fileName);
160
161     if (!file.exists()) {
162       throw new FileNotFoundException("Failed to load file = " + fileName);
163     }
164
165     if (file.exists() && !file.isDirectory()) {
166       BufferedReader br = new BufferedReader(new FileReader(file));
167       try {
168         StringBuilder sb = new StringBuilder();
169         String line = br.readLine();
170
171         while (line != null) {
172           sb.append(line);
173           sb.append(System.lineSeparator());
174           line = br.readLine();
175         }
176
177         return sb.toString();
178       } finally {
179         br.close();
180       }
181     } else {
182       LOG.warn(AaiUiMsgs.FILE_NOT_FOUND, fileName);
183     }
184
185     return null;
186
187   }
188
189 }