a62dd17b0232c2986aa2d8fe759928d9824de549
[aai/aai-common.git] / aai-schema-ingest / src / main / java / org / onap / aai / setup / ConfigTranslator.java
1 /** 
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-18 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.onap.aai.setup;
22
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 import java.io.*;
27 import java.nio.charset.Charset;
28 import java.util.ArrayList;
29 import java.util.List;
30 import java.util.Map;
31
32 import org.apache.commons.io.IOUtils;
33 import org.onap.aai.edges.JsonIngestor;
34 import org.springframework.beans.factory.annotation.Autowired;
35
36 /**
37  * Converts the contents of the schema config file
38  * (which lists which schema files to be loaded) to
39  * the format the Ingestors can work with.
40  * 
41  */
42 public abstract class ConfigTranslator extends Translator {
43     private static final Logger LOGGER = LoggerFactory.getLogger(ConfigTranslator.class);
44
45     protected SchemaLocationsBean bean;
46
47     @Autowired
48     public ConfigTranslator(SchemaLocationsBean schemaLocationbean, SchemaConfigVersions schemaVersions) {
49         super(schemaVersions);
50         this.bean = schemaLocationbean;
51
52     }
53
54     /**
55      * Translates the contents of the schema config file
56      * into the input for the NodeIngestor
57      * 
58      * @return Map of Version to the list of (string) filenames to be
59      *         ingested for that version
60      */
61     public abstract Map<SchemaVersion, List<String>> getNodeFiles();
62
63     public List<InputStream> getVersionNodeStream(SchemaVersion version) {
64
65         Map<SchemaVersion, List<String>> filesToIngest = getNodeFiles();
66         List<InputStream> streams = new ArrayList<>();
67
68         if (!filesToIngest.containsKey(version)) {
69             return streams;
70         }
71         List<String> versionFiles = filesToIngest.get(version);
72
73         for (String name : versionFiles) {
74             try {
75                 InputStream stream = new FileInputStream(new File(name));
76                 String value = IOUtils.toString(stream, Charset.defaultCharset());
77                 InputStream bis = (IOUtils.toInputStream(value, Charset.defaultCharset()));
78                 streams.add(bis);
79             } catch (FileNotFoundException e) {
80                 // TODO This may have to be cascaded
81                 LOGGER.warn("File Not Found" + e.getMessage());
82             } catch (IOException e) {
83                 LOGGER.warn("IOException while reading files" + e.getMessage());
84             }
85         }
86         return streams;
87     }
88
89     @Override
90     public List<String> getJsonPayload(SchemaVersion version) {
91         Map<SchemaVersion, List<String>> filesToIngest = getEdgeFiles();
92         List<String> jsonPayloads = new ArrayList<>();
93         if (!filesToIngest.containsKey(version)) {
94             return jsonPayloads;
95         }
96         List<String> versionFiles = filesToIngest.get(version);
97         JsonIngestor ji = new JsonIngestor();
98         for (String rulesFilename : versionFiles) {
99             jsonPayloads.add(ji.readInJsonFile(rulesFilename));
100
101         }
102
103         return jsonPayloads;
104     }
105
106     /**
107      * Translates the contents of the schema config file
108      * into the input for the EdgeIngestor
109      * 
110      * @return Map of Version to the List of (String) filenames to be
111      *         ingested for that version
112      */
113     public abstract Map<SchemaVersion, List<String>> getEdgeFiles();
114
115 }