d6d5dc2cb2632f8d6923632c81d245a7fdc74dde
[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 org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 import java.io.FileNotFoundException;
29 import java.io.IOException;
30 import java.io.InputStream;
31 import java.io.InputStreamReader;
32 import java.nio.charset.Charset;
33 import java.util.Properties;
34
35 /**
36  * Utility class containing methods for IO related operations
37  * <p>
38  * @author Rajiv Singla . Creation Date: 10/17/2016.
39  */
40 public abstract class AnalyticsModelIOUtils extends AnalyticsModelJsonUtils {
41
42     private static final Logger LOG = LoggerFactory.getLogger(AnalyticsModelIOUtils.class);
43
44     /**
45      * Parses given valid JSON file Location to object of given binding class type.
46      *
47      * @param fileLocation valid JSON File Location
48      * @param bindingClass class Type of Binding object
49      *
50      * @param <T>  binding Class Type
51      *
52      * @return binding Class Object which properties populated from JSON File Location
53      */
54     public static final <T> T convertToJsonObject(String fileLocation, Class<T> bindingClass) {
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 RuntimeException(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
70         } catch (JsonMappingException | JsonParseException e) {
71
72             // If parsing fails due to Invalid Json or Json IO Issues throw an exception
73             final String errorMessage = String.format("Json parsing error while parsing Json File location: %s",
74                     fileLocation);
75
76             LOG.error(errorMessage);
77             throw new RuntimeException(errorMessage, e);
78
79         } catch (IOException e) {
80
81             // If parsing fails due to IO Issues throw an exception
82             final String errorMessage = String.format("IO Error while parsing Json File location: %s", fileLocation);
83             LOG.error(errorMessage);
84             throw new RuntimeException(errorMessage, e);
85
86         }
87
88     }
89
90     /**
91      * Loads properties from a given file location. Throws {@link RuntimeException} if file location is invalid
92      * or there were exception when loading properties
93      *
94      * @param propertiesFileLocation path string for properties file
95      * @param properties properties object that needs to be populated with give file properties
96      *
97      * @return properties object with populated properties from properties file
98      *
99      */
100     public static Properties loadPropertiesFile(String propertiesFileLocation, final Properties properties) {
101
102         // Load Resource from give properties file path
103         final InputStream propertiesFileInputStream = loadResourceAsStream(propertiesFileLocation);
104
105         // If properties file is not present throw an exception
106         if (propertiesFileInputStream == null) {
107             final String errorMessage = String.format("Invalid Properties File at location: %s",
108                     propertiesFileLocation);
109             throw new RuntimeException(errorMessage, new FileNotFoundException(errorMessage));
110         }
111
112         try {
113             properties.load(propertiesFileInputStream);
114         } catch (IOException e) {
115             final String errorMessage = String.format("IO Exception while reading Properties File at location: %s",
116                     propertiesFileLocation);
117             throw new RuntimeException(errorMessage, e);
118         }
119
120         return properties;
121
122     }
123
124     /**
125      * Loads Input file from the given classpath file location and returns file InputStream
126      *
127      * @param fileLocation classpath file location
128      *
129      * @return {@link InputStream} for classpath file
130      */
131     public static InputStream loadResourceAsStream(String fileLocation) {
132         // Load Resource from give path
133         return Thread.currentThread().getContextClassLoader().getResourceAsStream(fileLocation);
134     }
135
136
137 }