Add multi-oxm using schemaIngest library
[aai/data-router.git] / src / main / java / org / onap / aai / datarouter / schema / SchemaIngestPropertiesReader.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017-2018 Amdocs
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *       http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21 package org.onap.aai.datarouter.schema;
22
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.net.URISyntaxException;
26 import java.nio.file.Files;
27 import java.nio.file.NoSuchFileException;
28 import java.nio.file.Path;
29 import java.nio.file.Paths;
30 import java.util.Properties;
31 import org.onap.aai.cl.eelf.LoggerFactory;
32 import org.onap.aai.datarouter.logging.DataRouterMsgs;
33
34 public class SchemaIngestPropertiesReader {
35
36     private static final String SCHEMA_INGEST_PROPERTIES_FILE = "schemaIngest.properties";
37     private static final String MSG_FAILED_LOAD_SCHEMA_INGEST_PROPERTIES = "Failed to load " + SCHEMA_INGEST_PROPERTIES_FILE;
38
39     private static final String SCHEMA_INGEST_PROPERTIES_LOCATION =
40             System.getProperty("CONFIG_HOME") + "/" + SCHEMA_INGEST_PROPERTIES_FILE;
41
42     private static org.onap.aai.cl.api.Logger logger =
43             LoggerFactory.getInstance().getLogger(SchemaIngestPropertiesReader.class.getName());
44
45     /**
46      * Gets the location of the OXM.
47      *
48      * @return the directory containing the OXM files
49      */
50     public String getNodeDir() {
51
52         Properties prop = new Properties();
53         try {
54             prop = loadFromFile(SCHEMA_INGEST_PROPERTIES_LOCATION);
55         } catch (NoSuchFileException e) {
56             try {
57                 prop = loadFromClasspath(SCHEMA_INGEST_PROPERTIES_FILE);
58             } catch (URISyntaxException | IOException ef) {
59                 logger.error(DataRouterMsgs.SCHEMA_INGEST_LOAD_ERROR, ef.getMessage());
60                 throw new RuntimeException(MSG_FAILED_LOAD_SCHEMA_INGEST_PROPERTIES);
61             }
62         } catch (IOException ec) {
63             logger.error(DataRouterMsgs.SCHEMA_INGEST_LOAD_ERROR, ec.getMessage());
64             throw new RuntimeException(MSG_FAILED_LOAD_SCHEMA_INGEST_PROPERTIES);
65         }
66         return prop.getProperty("nodeDir");
67     }
68
69     private Properties loadFromFile(String filename) throws IOException {
70         Path configLocation = Paths.get(filename);
71         try (InputStream stream = Files.newInputStream(configLocation)) {
72             return loadProperties(stream);
73         }
74     }
75
76     private Properties loadFromClasspath(String resourceName) throws URISyntaxException, IOException {
77         Path path = Paths.get(ClassLoader.getSystemResource(resourceName).toURI());
78         try (InputStream stream = Files.newInputStream(path)) {
79             return loadProperties(stream);
80         }
81     }
82
83     private Properties loadProperties(InputStream stream) throws IOException {
84         Properties config = new Properties();
85         config.load(stream);
86         return config;
87     }
88 }