ES to SDS conversion
[aai/sparky-be.git] / sparkybe-onap-service / src / main / java / org / onap / aai / sparky / search / filters / searchservice / FileBasedFilters.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017-2018 Amdocs
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *       http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21 package org.onap.aai.sparky.search.filters.searchservice;
22
23 import java.io.File;
24 import java.io.FileReader;
25
26 import org.onap.aai.cl.api.Logger;
27 import org.onap.aai.cl.eelf.LoggerFactory;
28 import org.onap.aai.sparky.logging.AaiUiMsgs;
29 import org.onap.aai.sparky.search.filters.config.FiltersConfig;
30
31 import com.google.gson.Gson;
32 import com.google.gson.JsonObject;
33 import com.google.gson.stream.JsonReader;
34
35 public class FileBasedFilters {
36
37   private static final Logger LOG = LoggerFactory.getInstance().getLogger(FileBasedFilters.class);
38   
39   private FiltersConfig filtersConfig;
40   private JsonObject filters;
41   private JsonObject views;
42   private Gson converter;
43
44   public FileBasedFilters(FiltersConfig filtersConfig) {
45     this.filtersConfig = filtersConfig;
46     this.converter = new Gson();
47     this.processFiltersAndViewsFromFile();
48   }
49
50   public JsonObject getFilters() {
51     return filters;
52   }
53
54   public JsonObject getViews() {
55     return views;
56   }
57   
58   public void setFiltersConfig(FiltersConfig filtersConfig) {
59     this.filtersConfig = filtersConfig;
60     processFiltersAndViewsFromFile();
61   }
62   
63   private void processFiltersAndViewsFromFile() {
64     String currentReadLocation = "_variable_not_set_check_filters_file_location_";
65     
66     try {
67       File filtersFile = filtersConfig.getFiltersFile();
68       if (filtersFile != null) {
69         currentReadLocation = filtersConfig.getFiltersFileName();
70         JsonReader filtersFileReader = new JsonReader(new FileReader(filtersFile));
71         this.filters = converter.fromJson(filtersFileReader, JsonObject.class);
72         filtersFileReader.close();
73       }
74       
75       File viewsFile = filtersConfig.getViewsFile();
76       if(viewsFile != null) {
77         currentReadLocation = filtersConfig.getViewsFileName();
78         JsonReader viewsFileReader = new JsonReader(new FileReader(viewsFile));
79         this.views = converter.fromJson(viewsFileReader, JsonObject.class);
80         viewsFileReader.close();
81       }
82       
83     } catch (Exception exc) {
84       String errorMessage = "Exception " + exc.getClass() + " was caught while reading file " + currentReadLocation;
85       LOG.error(AaiUiMsgs.ERROR_GENERIC, errorMessage);
86     }
87   }
88 }