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