added schema validation tools
[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 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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21  */
22
23 package org.onap.aai.edges;
24
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;
31 import java.util.Map;
32 import java.util.Map.Entry;
33
34 import org.onap.aai.setup.Version;
35
36 import com.jayway.jsonpath.DocumentContext;
37 import com.jayway.jsonpath.JsonPath;
38
39 /**
40  * JsonIngestor produces DocumentContexts from json files
41  */
42 public class JsonIngestor {
43         
44         /**
45          * Reads in given json files to queryable DocumentContexts.
46          * 
47          * @param Map<Version, List<String>> filesToIngest - map of filenames to ingest
48          *                      per Version
49          * @return Map<Version, List<DocumentContext>> - map of DocumentContexts per Version
50          */
51         public Map<Version, List<DocumentContext>> ingest(Map<Version, List<String>> filesToIngest) {
52                 Map<Version, List<DocumentContext>> result = new EnumMap<>(Version.class);
53                 
54                 for (Entry<Version, List<String>> verFiles : filesToIngest.entrySet()) {
55                         Version v = verFiles.getKey();
56                         List<String> files = verFiles.getValue();
57                         
58                         List<DocumentContext> docs = new ArrayList<>();
59                         
60                         for (String rulesFilename : files) {
61                                 String fileContents = readInJsonFile(rulesFilename);
62                                 docs.add(JsonPath.parse(fileContents));
63                         }
64                         result.put(v, docs);
65                 }
66                 
67                 return result;
68         }
69         
70         /**
71          * Reads the json file at the given filename into an in-memory String.
72          * 
73          * @param rulesFilename - json file to be read (must include path to the file)
74          * @return String json contents of the given file
75          */
76         private String readInJsonFile(String rulesFilename) {
77                 StringBuilder sb = new StringBuilder();
78                 try(BufferedReader br = new BufferedReader(new FileReader(rulesFilename))) {
79                         String line;
80                         while ((line = br.readLine()) != null) {
81                                 sb.append(line);
82                         }
83                 } catch (IOException e) {
84                         throw new ExceptionInInitializerError(e);
85                 }
86                 return sb.toString();
87         }
88 }