AAI-1523 Batch reformat aai-schema-ingest
[aai/aai-common.git] / aai-schema-ingest / src / main / java / org / onap / aai / edges / JsonIngestor.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.edges;
22
23 import com.att.eelf.configuration.EELFLogger;
24 import com.att.eelf.configuration.EELFManager;
25 import com.jayway.jsonpath.DocumentContext;
26 import com.jayway.jsonpath.JsonPath;
27
28 import java.io.BufferedReader;
29 import java.io.FileReader;
30 import java.io.IOException;
31 import java.util.ArrayList;
32 import java.util.HashMap;
33 import java.util.List;
34 import java.util.Map;
35 import java.util.Map.Entry;
36
37 import org.onap.aai.setup.SchemaVersion;
38
39 /**
40  * JsonIngestor produces DocumentContexts from json files
41  */
42 public class JsonIngestor {
43     private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(JsonIngestor.class);
44
45     /**
46      * Reads in given json files to queryable DocumentContexts.
47      *
48      * @param filesToIngest - map of filenames to ingest
49      *        per Version
50      * @return Map<SchemaVersion, List<DocumentContext>> - map of DocumentContexts per Version
51      */
52     public Map<SchemaVersion, List<DocumentContext>> ingest(Map<SchemaVersion, List<String>> filesToIngest) {
53         Map<SchemaVersion, List<DocumentContext>> result = new HashMap<>();
54
55         for (Entry<SchemaVersion, List<String>> verFiles : filesToIngest.entrySet()) {
56             SchemaVersion v = verFiles.getKey();
57             List<String> files = verFiles.getValue();
58
59             List<DocumentContext> docs = new ArrayList<>();
60             for (String rulesFilename : files) {
61                 String fileContents = readInJsonFile(rulesFilename);
62                 docs.add(JsonPath.parse(fileContents));
63             }
64             result.put(v, docs);
65         }
66
67         return result;
68     }
69
70     public Map<SchemaVersion, List<DocumentContext>> ingestContent(Map<SchemaVersion, List<String>> filesToIngest) {
71         Map<SchemaVersion, List<DocumentContext>> result = new HashMap<>();
72
73         for (Entry<SchemaVersion, List<String>> verFiles : filesToIngest.entrySet()) {
74             SchemaVersion v = verFiles.getKey();
75             List<String> files = verFiles.getValue();
76
77             List<DocumentContext> docs = new ArrayList<>();
78             for (String jsonPayload : files) {
79                 docs.add(JsonPath.parse(jsonPayload));
80             }
81             result.put(v, docs);
82         }
83         return result;
84     }
85
86     /**
87      * Reads the json file at the given filename into an in-memory String.
88      *
89      * @param rulesFilename - json file to be read (must include path to the file)
90      * @return String json contents of the given file
91      */
92     public String readInJsonFile(String rulesFilename) {
93         StringBuilder sb = new StringBuilder();
94         try (BufferedReader br = new BufferedReader(new FileReader(rulesFilename))) {
95             String line;
96             while ((line = br.readLine()) != null) {
97                 sb.append(line);
98             }
99         } catch (IOException e) {
100             LOGGER.warn("Exception in file" + e.getMessage());
101             throw new ExceptionInInitializerError(e);
102         }
103         return sb.toString();
104     }
105 }