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 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;
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;
36 import java.util.Map.Entry;
39 * JsonIngestor produces DocumentContexts from json files
41 public class JsonIngestor {
42 private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(JsonIngestor.class);
45 * Reads in given json files to queryable DocumentContexts.
47 * @param filesToIngest - map of filenames to ingest
49 * @return Map<SchemaVersion, List<DocumentContext>> - map of DocumentContexts per Version
51 public Map<SchemaVersion, List<DocumentContext>> ingest(Map<SchemaVersion, List<String>> filesToIngest) {
52 Map<SchemaVersion, List<DocumentContext>> result = new HashMap<>();
54 for (Entry<SchemaVersion, List<String>> verFiles : filesToIngest.entrySet()) {
55 SchemaVersion v = verFiles.getKey();
56 List<String> files = verFiles.getValue();
58 List<DocumentContext> docs = new ArrayList<>();
59 for (String rulesFilename : files) {
60 String fileContents = readInJsonFile(rulesFilename);
61 docs.add(JsonPath.parse(fileContents));
69 public Map<SchemaVersion, List<DocumentContext>> ingestContent(Map<SchemaVersion, List<String>> filesToIngest) {
70 Map<SchemaVersion, List<DocumentContext>> result = new HashMap<>();
72 for (Entry<SchemaVersion, List<String>> verFiles : filesToIngest.entrySet()) {
73 SchemaVersion v = verFiles.getKey();
74 List<String> files = verFiles.getValue();
76 List<DocumentContext> docs = new ArrayList<>();
77 for (String jsonPayload : files) {
78 docs.add(JsonPath.parse(jsonPayload));
86 * Reads the json file at the given filename into an in-memory String.
88 * @param rulesFilename - json file to be read (must include path to the file)
89 * @return String json contents of the given file
91 public String readInJsonFile(String rulesFilename) {
92 StringBuilder sb = new StringBuilder();
93 try(BufferedReader br = new BufferedReader(new FileReader(rulesFilename))) {
95 while ((line = br.readLine()) != null) {
98 } catch (IOException e) {
99 LOGGER.warn("Exception in file"+e.getMessage());
100 throw new ExceptionInInitializerError(e);
102 return sb.toString();