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