aboutsummaryrefslogtreecommitdiffstats
path: root/dcae-analytics-model/src/main/java/org/onap/dcae/apod/analytics/model/util/AnalyticsModelIOUtils.java
blob: a0f374c733043686bf3f777bb9aeb21fcc3af82c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
 * ===============================LICENSE_START======================================
 *  dcae-analytics
 * ================================================================================
 *    Copyright © 2017 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *          http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  ============================LICENSE_END===========================================
 */

package org.onap.dcae.apod.analytics.model.util;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.util.Properties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Utility class containing methods for IO related operations
 * <p>
 * @author Rajiv Singla . Creation Date: 10/17/2016.
 */
public abstract class AnalyticsModelIOUtils extends AnalyticsModelJsonUtils {

    private static final Logger LOG = LoggerFactory.getLogger(AnalyticsModelIOUtils.class);

    /**
     * Parses given valid JSON file Location to object of given binding class type.
     *
     * @param fileLocation valid JSON File Location
     * @param bindingClass class Type of Binding object
     *
     * @param <T>  binding Class Type
     *
     * @return binding Class Object which properties populated from JSON File Location
     * @throws IOException when fails to do IO operations
     */
    public static final <T> T convertToJsonObject(String fileLocation, Class<T> bindingClass) throws IOException {

        // Load Resource from give path
        final InputStream resourceAsStream = loadResourceAsStream(fileLocation);

        // If resource is null throw an exception
        if (resourceAsStream == null) {
            final String errorMessage = String.format("Invalid File location: %s", fileLocation);
            throw new IOException(errorMessage, new FileNotFoundException(errorMessage));
        }

        // Parse input stream
        try (InputStreamReader inputStreamReader = new InputStreamReader(resourceAsStream, Charset.forName("UTF-8"))) {

            return ANALYTICS_MODEL_OBJECT_MAPPER.readValue(inputStreamReader, bindingClass);
        } catch (JsonMappingException | JsonParseException e) {

            // If parsing fails due to Invalid Json or Json IO Issues throw an exception
            final String errorMessage = String.format("Json parsing error while parsing Json File location: %s",
                    fileLocation);

            LOG.error(errorMessage);
            throw new IOException(errorMessage, e);
        } catch (IOException e) {

            // If parsing fails due to IO Issues throw an exception
            final String errorMessage = String.format("IO Error while parsing Json File location: %s", fileLocation);
            LOG.error(errorMessage);
            throw new IOException(errorMessage, e);
        }
    }

    /**
     * Loads properties from a given file location. Throws {@link RuntimeException} if file location is invalid
     * or there were exception when loading properties
     *
     * @param propertiesFileLocation path string for properties file
     * @param properties properties object that needs to be populated with give file properties
     *
     * @return properties object with populated properties from properties file
     *
     */
    public static Properties loadPropertiesFile(String propertiesFileLocation, final Properties properties) {

        // Load Resource from give properties file path
        final InputStream propertiesFileInputStream = loadResourceAsStream(propertiesFileLocation);

        // If properties file is not present throw an exception
        if (propertiesFileInputStream == null) {
            final String errorMessage = String.format("Invalid Properties File at location: %s",
                propertiesFileLocation);
            //TODO: discuss and change this excpeiton as well.
            throw new RuntimeException(errorMessage, new FileNotFoundException(errorMessage));
        }

        try {
            properties.load(propertiesFileInputStream);
        } catch (IOException e) {
            final String errorMessage = String.format("IO Exception while reading Properties File at location: %s",
                    propertiesFileLocation);
            throw new RuntimeException(errorMessage, e);
        }

        return properties;

    }

    /**
     * Loads Input file from the given classpath file location and returns file InputStream
     *
     * @param fileLocation classpath file location
     *
     * @return {@link InputStream} for classpath file
     */
    public static InputStream loadResourceAsStream(String fileLocation) {
        // Load Resource from give path
        return Thread.currentThread().getContextClassLoader().getResourceAsStream(fileLocation);
    }
}