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