2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.aai.edges;
23 import java.io.BufferedReader;
24 import java.io.FileReader;
25 import java.io.IOException;
27 import java.util.Map.Entry;
29 import org.onap.aai.setup.SchemaVersion;
31 import com.jayway.jsonpath.DocumentContext;
32 import com.jayway.jsonpath.JsonPath;
35 * JsonIngestor produces DocumentContexts from json files
37 public class JsonIngestor {
40 * Reads in given json files to queryable DocumentContexts.
42 * @param filesToIngest - map of filenames to ingest
44 * @return Map<SchemaVersion, List<DocumentContext>> - map of DocumentContexts per Version
46 public Map<SchemaVersion, List<DocumentContext>> ingest(Map<SchemaVersion, List<String>> filesToIngest) {
47 Map<SchemaVersion, List<DocumentContext>> result = new HashMap<>();
49 for (Entry<SchemaVersion, List<String>> verFiles : filesToIngest.entrySet()) {
50 SchemaVersion v = verFiles.getKey();
51 List<String> files = verFiles.getValue();
53 List<DocumentContext> docs = new ArrayList<>();
55 for (String rulesFilename : files) {
56 String fileContents = readInJsonFile(rulesFilename);
57 docs.add(JsonPath.parse(fileContents));
66 * Reads the json file at the given filename into an in-memory String.
68 * @param rulesFilename - json file to be read (must include path to the file)
69 * @return String json contents of the given file
71 private String readInJsonFile(String rulesFilename) {
72 StringBuilder sb = new StringBuilder();
73 try(BufferedReader br = new BufferedReader(new FileReader(rulesFilename))) {
75 while ((line = br.readLine()) != null) {
78 } catch (IOException e) {
79 throw new ExceptionInInitializerError(e);