Sonar Critical Fix
[dcaegen2/analytics/tca.git] / dcae-analytics-model / src / main / java / org / openecomp / dcae / apod / analytics / model / util / AnalyticsModelIOUtils.java
1 /*\r
2  * ===============================LICENSE_START======================================\r
3  *  dcae-analytics\r
4  * ================================================================================\r
5  *    Copyright © 2017 AT&T Intellectual Property. All rights reserved.\r
6  * ================================================================================\r
7  *  Licensed under the Apache License, Version 2.0 (the "License");\r
8  *  you may not use this file except in compliance with the License.\r
9  *   You may obtain a copy of the License at\r
10  *\r
11  *          http://www.apache.org/licenses/LICENSE-2.0\r
12  *\r
13  *  Unless required by applicable law or agreed to in writing, software\r
14  *  distributed under the License is distributed on an "AS IS" BASIS,\r
15  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16  *  See the License for the specific language governing permissions and\r
17  *  limitations under the License.\r
18  *  ============================LICENSE_END===========================================\r
19  */\r
20 \r
21 package org.openecomp.dcae.apod.analytics.model.util;\r
22 \r
23 import com.fasterxml.jackson.core.JsonParseException;\r
24 import com.fasterxml.jackson.databind.JsonMappingException;\r
25 import java.io.FileNotFoundException;\r
26 import java.io.IOException;\r
27 import java.io.InputStream;\r
28 import java.io.InputStreamReader;\r
29 import java.nio.charset.Charset;\r
30 import java.util.Properties;\r
31 import org.slf4j.Logger;\r
32 import org.slf4j.LoggerFactory;\r
33 \r
34 /**\r
35  * Utility class containing methods for IO related operations\r
36  * <p>\r
37  * @author Rajiv Singla . Creation Date: 10/17/2016.\r
38  */\r
39 public abstract class AnalyticsModelIOUtils extends AnalyticsModelJsonUtils {\r
40 \r
41     private static final Logger LOG = LoggerFactory.getLogger(AnalyticsModelIOUtils.class);\r
42 \r
43     /**\r
44      * Parses given valid JSON file Location to object of given binding class type.\r
45      *\r
46      * @param fileLocation valid JSON File Location\r
47      * @param bindingClass class Type of Binding object\r
48      *\r
49      * @param <T>  binding Class Type\r
50      *\r
51      * @return binding Class Object which properties populated from JSON File Location\r
52      * @throws IOException when fails to do IO operations\r
53      */\r
54     public static final <T> T convertToJsonObject(String fileLocation, Class<T> bindingClass) throws IOException {\r
55 \r
56         // Load Resource from give path\r
57         final InputStream resourceAsStream = loadResourceAsStream(fileLocation);\r
58 \r
59         // If resource is null throw an exception\r
60         if (resourceAsStream == null) {\r
61             final String errorMessage = String.format("Invalid File location: %s", fileLocation);\r
62             throw new IOException(errorMessage, new FileNotFoundException(errorMessage));\r
63         }\r
64 \r
65         // Parse input stream\r
66         try (InputStreamReader inputStreamReader = new InputStreamReader(resourceAsStream, Charset.forName("UTF-8"))) {\r
67 \r
68             return ANALYTICS_MODEL_OBJECT_MAPPER.readValue(inputStreamReader, bindingClass);\r
69         } catch (JsonMappingException | JsonParseException e) {\r
70 \r
71             // If parsing fails due to Invalid Json or Json IO Issues throw an exception\r
72             final String errorMessage = String.format("Json parsing error while parsing Json File location: %s",\r
73                     fileLocation);\r
74 \r
75             LOG.error(errorMessage);\r
76             throw new IOException(errorMessage, e);\r
77         } catch (IOException e) {\r
78 \r
79             // If parsing fails due to IO Issues throw an exception\r
80             final String errorMessage = String.format("IO Error while parsing Json File location: %s", fileLocation);\r
81             LOG.error(errorMessage);\r
82             throw new IOException(errorMessage, e);\r
83         }\r
84     }\r
85 \r
86     /**\r
87      * Loads properties from a given file location. Throws {@link RuntimeException} if file location is invalid\r
88      * or there were exception when loading properties\r
89      *\r
90      * @param propertiesFileLocation path string for properties file\r
91      * @param properties properties object that needs to be populated with give file properties\r
92      *\r
93      * @return properties object with populated properties from properties file\r
94      *\r
95      */\r
96     public static Properties loadPropertiesFile(String propertiesFileLocation, final Properties properties) {\r
97 \r
98         // Load Resource from give properties file path\r
99         final InputStream propertiesFileInputStream = loadResourceAsStream(propertiesFileLocation);\r
100 \r
101         // If properties file is not present throw an exception\r
102         if (propertiesFileInputStream == null) {\r
103             final String errorMessage = String.format("Invalid Properties File at location: %s",\r
104                 propertiesFileLocation);\r
105             //TODO: discuss and change this excpeiton as well.\r
106             throw new RuntimeException(errorMessage, new FileNotFoundException(errorMessage));\r
107         }\r
108 \r
109         try {\r
110             properties.load(propertiesFileInputStream);\r
111         } catch (IOException e) {\r
112             final String errorMessage = String.format("IO Exception while reading Properties File at location: %s",\r
113                     propertiesFileLocation);\r
114             throw new RuntimeException(errorMessage, e);\r
115         }\r
116 \r
117         return properties;\r
118 \r
119     }\r
120 \r
121     /**\r
122      * Loads Input file from the given classpath file location and returns file InputStream\r
123      *\r
124      * @param fileLocation classpath file location\r
125      *\r
126      * @return {@link InputStream} for classpath file\r
127      */\r
128     public static InputStream loadResourceAsStream(String fileLocation) {\r
129         // Load Resource from give path\r
130         return Thread.currentThread().getContextClassLoader().getResourceAsStream(fileLocation);\r
131     }\r
132 }\r