aboutsummaryrefslogtreecommitdiffstats
path: root/dcae-analytics-model/src/main/java/org/openecomp/dcae/apod/analytics/model/util/AnalyticsModelIOUtils.java
diff options
context:
space:
mode:
Diffstat (limited to 'dcae-analytics-model/src/main/java/org/openecomp/dcae/apod/analytics/model/util/AnalyticsModelIOUtils.java')
-rw-r--r--dcae-analytics-model/src/main/java/org/openecomp/dcae/apod/analytics/model/util/AnalyticsModelIOUtils.java146
1 files changed, 146 insertions, 0 deletions
diff --git a/dcae-analytics-model/src/main/java/org/openecomp/dcae/apod/analytics/model/util/AnalyticsModelIOUtils.java b/dcae-analytics-model/src/main/java/org/openecomp/dcae/apod/analytics/model/util/AnalyticsModelIOUtils.java
new file mode 100644
index 0000000..9b97b26
--- /dev/null
+++ b/dcae-analytics-model/src/main/java/org/openecomp/dcae/apod/analytics/model/util/AnalyticsModelIOUtils.java
@@ -0,0 +1,146 @@
+/*
+ * ============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.openecomp.dcae.apod.analytics.model.util;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+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;
+
+/**
+ * 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
+ */
+ public static final <T> T convertToJsonObject(String fileLocation, Class<T> bindingClass) {
+
+ // 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);
+ logAndThrowFileNotFoundException(errorMessage);
+ }
+
+ // Parse input stream
+ try (InputStreamReader inputStreamReader = new InputStreamReader(resourceAsStream, Charset.forName("UTF-8"))) {
+
+ return ANALYTICS_MODEL_OBJECT_MAPPER.readValue(inputStreamReader, bindingClass);
+
+ } 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 RuntimeException(errorMessage, e);
+
+ } catch (Exception 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 RuntimeException(errorMessage, e);
+
+ }
+
+ }
+
+ public static Properties loadPropertiesFile(String propertiesFileLocation) {
+
+ // 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);
+ logAndThrowFileNotFoundException(errorMessage);
+ }
+
+ final Properties properties = new Properties();
+
+ try {
+ properties.load(propertiesFileInputStream);
+ } catch (IOException e) {
+ final String errorMessage = String.format("IO Exception while reading Properties File at location: %s",
+ propertiesFileLocation);
+ logAndThrowRuntimeException(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);
+ }
+
+
+ /**
+ * Throws a new Runtime exception for new {@link FileNotFoundException} and logs it as an error
+ *
+ * @param errorMessage
+ */
+ private static void logAndThrowFileNotFoundException(String errorMessage) {
+ logAndThrowRuntimeException(errorMessage, new FileNotFoundException(errorMessage));
+ }
+
+ /**
+ * Wraps checked exception into a runtime exception and log it as an error
+ *
+ * @param errorMessage
+ * @param actualException
+ */
+ private static void logAndThrowRuntimeException(String errorMessage, Exception actualException) {
+
+ LOG.error(errorMessage);
+ throw new RuntimeException(errorMessage, actualException);
+
+ }
+
+
+}