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