Only load SchemaService related beans in aai-common when schema.translator.list=schem...
[aai/aai-common.git] / aai-schema-ingest / src / main / java / org / onap / aai / setup / ConfigTranslator.java
1 /**  ============LICENSE_START=======================================================
2  * org.onap.aai
3  * ================================================================================
4  * Copyright © 2017-18 AT&T Intellectual Property. All rights reserved.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *   http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  * ============LICENSE_END=========================================================
18 */
19
20 package org.onap.aai.setup;
21
22 import java.io.File;
23 import java.io.FileInputStream;
24 import java.io.FileNotFoundException;
25 import java.io.IOException;
26 import java.io.InputStream;
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.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36 import org.springframework.beans.factory.annotation.Autowired;
37
38 /**
39  * Converts the contents of the schema config file
40  * (which lists which schema files to be loaded) to
41  * the format the Ingestors can work with.
42  * 
43  */
44 public abstract class ConfigTranslator extends Translator {
45     private static final Logger LOGGER = LoggerFactory.getLogger(ConfigTranslator.class);
46
47     protected SchemaLocationsBean schemaLocationsBean;
48
49     @Autowired
50     public ConfigTranslator(SchemaLocationsBean schemaLocationsBean, SchemaConfigVersions schemaConfigVersions) {
51         super(schemaConfigVersions);
52         this.schemaLocationsBean = schemaLocationsBean;
53
54     }
55
56     /**
57      * Translates the contents of the schema config file
58      * into the input for the NodeIngestor
59      * 
60      * @return Map of Version to the list of (string) filenames to be
61      *         ingested for that version
62      */
63     public abstract Map<SchemaVersion, List<String>> getNodeFiles();
64
65     public List<InputStream> getVersionNodeStream(SchemaVersion version) {
66
67         Map<SchemaVersion, List<String>> filesToIngest = getNodeFiles();
68         List<InputStream> streams = new ArrayList<>();
69
70         if (!filesToIngest.containsKey(version)) {
71             return streams;
72         }
73         List<String> versionFiles = filesToIngest.get(version);
74
75         for (String name : versionFiles) {
76             try {
77                 InputStream stream = new FileInputStream(new File(name));
78                 String value = IOUtils.toString(stream, Charset.defaultCharset());
79                 InputStream bis = (IOUtils.toInputStream(value, Charset.defaultCharset()));
80                 streams.add(bis);
81             } catch (FileNotFoundException e) {
82                 // TODO This may have to be cascaded
83                 LOGGER.warn("File Not Found" + e.getMessage());
84             } catch (IOException e) {
85                 LOGGER.warn("IOException while reading files" + e.getMessage());
86             }
87         }
88         return streams;
89     }
90
91     @Override
92     public List<String> getJsonPayload(SchemaVersion version) {
93         Map<SchemaVersion, List<String>> filesToIngest = getEdgeFiles();
94         List<String> jsonPayloads = new ArrayList<>();
95         if (!filesToIngest.containsKey(version)) {
96             return jsonPayloads;
97         }
98         List<String> versionFiles = filesToIngest.get(version);
99         JsonIngestor ji = new JsonIngestor();
100         for (String rulesFilename : versionFiles) {
101             jsonPayloads.add(ji.readInJsonFile(rulesFilename));
102
103         }
104
105         return jsonPayloads;
106     }
107
108     /**
109      * Translates the contents of the schema config file
110      * into the input for the EdgeIngestor
111      * 
112      * @return Map of Version to the List of (String) filenames to be
113      *         ingested for that version
114      */
115     public abstract Map<SchemaVersion, List<String>> getEdgeFiles();
116
117 }