2 * ============LICENSE_START=======================================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.aai.sparky.search.filters.searchservice;
24 import java.io.FileReader;
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;
31 import com.google.gson.Gson;
32 import com.google.gson.JsonObject;
33 import com.google.gson.stream.JsonReader;
35 public class FileBasedFilters {
37 private static final Logger LOG = LoggerFactory.getInstance().getLogger(FileBasedFilters.class);
39 private FiltersConfig filtersConfig;
40 private JsonObject filters;
41 private JsonObject views;
42 private Gson converter;
44 public FileBasedFilters(FiltersConfig filtersConfig) {
45 this.filtersConfig = filtersConfig;
46 this.converter = new Gson();
47 this.processFiltersAndViewsFromFile();
50 public JsonObject getFilters() {
54 public JsonObject getViews() {
58 public void setFiltersConfig(FiltersConfig filtersConfig) {
59 this.filtersConfig = filtersConfig;
60 processFiltersAndViewsFromFile();
63 private void processFiltersAndViewsFromFile() {
64 String currentReadLocation = "_variable_not_set_check_filters_file_location_";
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();
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();
83 } catch (Exception exc) {
84 String errorMessage = "Exception " + exc.getClass() + " was caught while reading file " + currentReadLocation;
85 LOG.error(AaiUiMsgs.ERROR_GENERIC, errorMessage);