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