Integrate aai-schema-ingest library into aai-core
[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 java.io.BufferedReader;
24 import java.io.FileReader;
25 import java.io.IOException;
26 import java.util.*;
27 import java.util.Map.Entry;
28
29 import org.onap.aai.setup.SchemaVersion;
30
31 import com.jayway.jsonpath.DocumentContext;
32 import com.jayway.jsonpath.JsonPath;
33
34 /**
35  * JsonIngestor produces DocumentContexts from json files
36  */
37 public class JsonIngestor {
38         
39         /**
40          * Reads in given json files to queryable DocumentContexts.
41          * 
42          * @param filesToIngest - map of filenames to ingest
43          *                      per Version
44          * @return Map<SchemaVersion, List<DocumentContext>> - map of DocumentContexts per Version
45          */
46         public Map<SchemaVersion, List<DocumentContext>> ingest(Map<SchemaVersion, List<String>> filesToIngest) {
47                 Map<SchemaVersion, List<DocumentContext>> result = new HashMap<>();
48                 
49                 for (Entry<SchemaVersion, List<String>> verFiles : filesToIngest.entrySet()) {
50                         SchemaVersion v = verFiles.getKey();
51                         List<String> files = verFiles.getValue();
52                         
53                         List<DocumentContext> docs = new ArrayList<>();
54                         
55                         for (String rulesFilename : files) {
56                                 String fileContents = readInJsonFile(rulesFilename);
57                                 docs.add(JsonPath.parse(fileContents));
58                         }
59                         result.put(v, docs);
60                 }
61                 
62                 return result;
63         }
64         
65         /**
66          * Reads the json file at the given filename into an in-memory String.
67          * 
68          * @param rulesFilename - json file to be read (must include path to the file)
69          * @return String json contents of the given file
70          */
71         private String readInJsonFile(String rulesFilename) {
72                 StringBuilder sb = new StringBuilder();
73                 try(BufferedReader br = new BufferedReader(new FileReader(rulesFilename))) {
74                         String line;
75                         while ((line = br.readLine()) != null) {
76                                 sb.append(line);
77                         }
78                 } catch (IOException e) {
79                         throw new ExceptionInInitializerError(e);
80                 }
81                 return sb.toString();
82         }
83 }