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