1 /** ============LICENSE_START=======================================================
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
20 package org.onap.aai.setup;
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;
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;
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.
44 public abstract class ConfigTranslator extends Translator {
45 private static final Logger LOGGER = LoggerFactory.getLogger(ConfigTranslator.class);
47 protected SchemaLocationsBean schemaLocationsBean;
50 public ConfigTranslator(SchemaLocationsBean schemaLocationsBean, SchemaConfigVersions schemaConfigVersions) {
51 super(schemaConfigVersions);
52 this.schemaLocationsBean = schemaLocationsBean;
57 * Translates the contents of the schema config file
58 * into the input for the NodeIngestor
60 * @return Map of Version to the list of (string) filenames to be
61 * ingested for that version
63 public abstract Map<SchemaVersion, List<String>> getNodeFiles();
65 public List<InputStream> getVersionNodeStream(SchemaVersion version) {
67 Map<SchemaVersion, List<String>> filesToIngest = getNodeFiles();
68 List<InputStream> streams = new ArrayList<>();
70 if (!filesToIngest.containsKey(version)) {
73 List<String> versionFiles = filesToIngest.get(version);
75 for (String name : versionFiles) {
77 InputStream stream = new FileInputStream(new File(name));
78 String value = IOUtils.toString(stream, Charset.defaultCharset());
79 InputStream bis = (IOUtils.toInputStream(value, Charset.defaultCharset()));
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());
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)) {
98 List<String> versionFiles = filesToIngest.get(version);
99 JsonIngestor ji = new JsonIngestor();
100 for (String rulesFilename : versionFiles) {
101 jsonPayloads.add(ji.readInJsonFile(rulesFilename));
109 * Translates the contents of the schema config file
110 * into the input for the EdgeIngestor
112 * @return Map of Version to the List of (String) filenames to be
113 * ingested for that version
115 public abstract Map<SchemaVersion, List<String>> getEdgeFiles();