2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017 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=========================================================
20 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
23 package org.onap.aai.edges;
25 import java.io.BufferedReader;
26 import java.io.FileReader;
27 import java.io.IOException;
28 import java.util.ArrayList;
29 import java.util.EnumMap;
30 import java.util.List;
32 import java.util.Map.Entry;
34 import org.onap.aai.setup.Version;
36 import com.jayway.jsonpath.DocumentContext;
37 import com.jayway.jsonpath.JsonPath;
40 * JsonIngestor produces DocumentContexts from json files
42 public class JsonIngestor {
45 * Reads in given json files to queryable DocumentContexts.
47 * @param Map<Version, List<String>> filesToIngest - map of filenames to ingest
49 * @return Map<Version, List<DocumentContext>> - map of DocumentContexts per Version
51 public Map<Version, List<DocumentContext>> ingest(Map<Version, List<String>> filesToIngest) {
52 Map<Version, List<DocumentContext>> result = new EnumMap<>(Version.class);
54 for (Entry<Version, List<String>> verFiles : filesToIngest.entrySet()) {
55 Version v = verFiles.getKey();
56 List<String> files = verFiles.getValue();
58 List<DocumentContext> docs = new ArrayList<>();
60 for (String rulesFilename : files) {
61 String fileContents = readInJsonFile(rulesFilename);
62 docs.add(JsonPath.parse(fileContents));
71 * Reads the json file at the given filename into an in-memory String.
73 * @param rulesFilename - json file to be read (must include path to the file)
74 * @return String json contents of the given file
76 private String readInJsonFile(String rulesFilename) {
77 StringBuilder sb = new StringBuilder();
78 try(BufferedReader br = new BufferedReader(new FileReader(rulesFilename))) {
80 while ((line = br.readLine()) != null) {
83 } catch (IOException e) {
84 throw new ExceptionInInitializerError(e);