TCA: Support for VES/A&AI enrichment
[dcaegen2/analytics/tca.git] / dcae-analytics-model / src / main / java / org / openecomp / dcae / apod / analytics / model / util / AnalyticsModelIOUtils.java
index df1d46d..6fcd459 100644 (file)
-/*
- * ===============================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 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);
-    }
-}
+/*\r
+ * ===============================LICENSE_START======================================\r
+ *  dcae-analytics\r
+ * ================================================================================\r
+ *    Copyright © 2017 AT&T Intellectual Property. All rights reserved.\r
+ * ================================================================================\r
+ *  Licensed under the Apache License, Version 2.0 (the "License");\r
+ *  you may not use this file except in compliance with the License.\r
+ *   You may obtain a copy of the License at\r
+ *\r
+ *          http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ *  Unless required by applicable law or agreed to in writing, software\r
+ *  distributed under the License is distributed on an "AS IS" BASIS,\r
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ *  See the License for the specific language governing permissions and\r
+ *  limitations under the License.\r
+ *  ============================LICENSE_END===========================================\r
+ */\r
+\r
+package org.openecomp.dcae.apod.analytics.model.util;\r
+\r
+import com.fasterxml.jackson.core.JsonParseException;\r
+import com.fasterxml.jackson.databind.JsonMappingException;\r
+import java.io.FileNotFoundException;\r
+import java.io.IOException;\r
+import java.io.InputStream;\r
+import java.io.InputStreamReader;\r
+import java.nio.charset.Charset;\r
+import java.util.Properties;\r
+import org.slf4j.Logger;\r
+import org.slf4j.LoggerFactory;\r
+\r
+/**\r
+ * Utility class containing methods for IO related operations\r
+ * <p>\r
+ * @author Rajiv Singla . Creation Date: 10/17/2016.\r
+ */\r
+public abstract class AnalyticsModelIOUtils extends AnalyticsModelJsonUtils {\r
+\r
+    private static final Logger LOG = LoggerFactory.getLogger(AnalyticsModelIOUtils.class);\r
+\r
+    /**\r
+     * Parses given valid JSON file Location to object of given binding class type.\r
+     *\r
+     * @param fileLocation valid JSON File Location\r
+     * @param bindingClass class Type of Binding object\r
+     *\r
+     * @param <T>  binding Class Type\r
+     *\r
+     * @return binding Class Object which properties populated from JSON File Location\r
+     * @throws IOException when fails to do IO operations\r
+     */\r
+    public static final <T> T convertToJsonObject(String fileLocation, Class<T> bindingClass) throws IOException {\r
+\r
+        // Load Resource from give path\r
+        final InputStream resourceAsStream = loadResourceAsStream(fileLocation);\r
+\r
+        // If resource is null throw an exception\r
+        if (resourceAsStream == null) {\r
+            final String errorMessage = String.format("Invalid File location: %s", fileLocation);\r
+            throw new IOException(errorMessage, new FileNotFoundException(errorMessage));\r
+        }\r
+\r
+        // Parse input stream\r
+        try (InputStreamReader inputStreamReader = new InputStreamReader(resourceAsStream, Charset.forName("UTF-8"))) {\r
+\r
+            return ANALYTICS_MODEL_OBJECT_MAPPER.readValue(inputStreamReader, bindingClass);\r
+        } catch (JsonMappingException | JsonParseException e) {\r
+\r
+            // If parsing fails due to Invalid Json or Json IO Issues throw an exception\r
+            final String errorMessage = String.format("Json parsing error while parsing Json File location: %s",\r
+                    fileLocation);\r
+\r
+            LOG.error(errorMessage);\r
+            throw new IOException(errorMessage, e);\r
+        } catch (IOException e) {\r
+\r
+            // If parsing fails due to IO Issues throw an exception\r
+            final String errorMessage = String.format("IO Error while parsing Json File location: %s", fileLocation);\r
+            LOG.error(errorMessage);\r
+            throw new IOException(errorMessage, e);\r
+        }\r
+    }\r
+\r
+    /**\r
+     * Loads properties from a given file location. Throws {@link RuntimeException} if file location is invalid\r
+     * or there were exception when loading properties\r
+     *\r
+     * @param propertiesFileLocation path string for properties file\r
+     * @param properties properties object that needs to be populated with give file properties\r
+     *\r
+     * @return properties object with populated properties from properties file\r
+     *\r
+     */\r
+    public static Properties loadPropertiesFile(String propertiesFileLocation, final Properties properties) {\r
+\r
+        // Load Resource from give properties file path\r
+        final InputStream propertiesFileInputStream = loadResourceAsStream(propertiesFileLocation);\r
+\r
+        // If properties file is not present throw an exception\r
+        if (propertiesFileInputStream == null) {\r
+            final String errorMessage = String.format("Invalid Properties File at location: %s",\r
+                propertiesFileLocation);\r
+            //TODO: discuss and change this excpeiton as well.\r
+            throw new RuntimeException(errorMessage, new FileNotFoundException(errorMessage));\r
+        }\r
+\r
+        try {\r
+            properties.load(propertiesFileInputStream);\r
+        } catch (IOException e) {\r
+            final String errorMessage = String.format("IO Exception while reading Properties File at location: %s",\r
+                    propertiesFileLocation);\r
+            throw new RuntimeException(errorMessage, e);\r
+        }\r
+\r
+        return properties;\r
+\r
+    }\r
+\r
+    /**\r
+     * Loads Input file from the given classpath file location and returns file InputStream\r
+     *\r
+     * @param fileLocation classpath file location\r
+     *\r
+     * @return {@link InputStream} for classpath file\r
+     */\r
+    public static InputStream loadResourceAsStream(String fileLocation) {\r
+        // Load Resource from give path\r
+        return Thread.currentThread().getContextClassLoader().getResourceAsStream(fileLocation);\r
+    }\r
+}\r