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;
28 import java.io.BufferedReader;
29 import java.io.FileReader;
30 import java.io.IOException;
31 import java.util.ArrayList;
32 import java.util.HashMap;
33 import java.util.List;
35 import java.util.Map.Entry;
37 import org.onap.aai.setup.SchemaVersion;
40 * JsonIngestor produces DocumentContexts from json files
42 public class JsonIngestor {
43 private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(JsonIngestor.class);
46 * Reads in given json files to queryable DocumentContexts.
48 * @param filesToIngest - map of filenames to ingest
50 * @return Map<SchemaVersion, List<DocumentContext>> - map of DocumentContexts per Version
52 public Map<SchemaVersion, List<DocumentContext>> ingest(Map<SchemaVersion, List<String>> filesToIngest) {
53 Map<SchemaVersion, List<DocumentContext>> result = new HashMap<>();
55 for (Entry<SchemaVersion, List<String>> verFiles : filesToIngest.entrySet()) {
56 SchemaVersion v = verFiles.getKey();
57 List<String> files = verFiles.getValue();
59 List<DocumentContext> docs = new ArrayList<>();
60 for (String rulesFilename : files) {
61 String fileContents = readInJsonFile(rulesFilename);
62 docs.add(JsonPath.parse(fileContents));
70 public Map<SchemaVersion, List<DocumentContext>> ingestContent(Map<SchemaVersion, List<String>> filesToIngest) {
71 Map<SchemaVersion, List<DocumentContext>> result = new HashMap<>();
73 for (Entry<SchemaVersion, List<String>> verFiles : filesToIngest.entrySet()) {
74 SchemaVersion v = verFiles.getKey();
75 List<String> files = verFiles.getValue();
77 List<DocumentContext> docs = new ArrayList<>();
78 for (String jsonPayload : files) {
79 docs.add(JsonPath.parse(jsonPayload));
87 * Reads the json file at the given filename into an in-memory String.
89 * @param rulesFilename - json file to be read (must include path to the file)
90 * @return String json contents of the given file
92 public String readInJsonFile(String rulesFilename) {
93 StringBuilder sb = new StringBuilder();
94 try (BufferedReader br = new BufferedReader(new FileReader(rulesFilename))) {
96 while ((line = br.readLine()) != null) {
99 } catch (IOException e) {
100 LOGGER.warn("Exception in file" + e.getMessage());
101 throw new ExceptionInInitializerError(e);
103 return sb.toString();