Increasing test coverage
[aai/sparky-be.git] / src / main / java / org / onap / aai / sparky / search / filters / config / FiltersConfig.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017 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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  */
23 package org.onap.aai.sparky.search.filters.config;
24
25 import java.io.File;
26
27 import org.onap.aai.cl.api.Logger;
28 import org.onap.aai.cl.eelf.LoggerFactory;
29 import org.onap.aai.sparky.logging.AaiUiMsgs;
30 import org.onap.aai.sparky.viewandinspect.config.SparkyConstants;
31
32 import com.fasterxml.jackson.databind.ObjectMapper;
33
34 public class FiltersConfig {
35
36   private static final Logger LOG = LoggerFactory.getInstance().getLogger(FiltersConfig.class);
37   
38   private String filtersFileName;
39   
40   private String filterMappingsFileName;
41   
42   private FiltersForViewsConfig viewsConfig;
43   
44   private FiltersDetailsConfig filtersConfig;
45
46   /**
47    * Instantiates a new UiViewFilterConfig.
48    */
49   public FiltersConfig() {
50     initializeFilters();
51   }
52   
53   public FiltersConfig(String filtersFileName, String filterMappingsFileName) {
54     this.filtersFileName = filtersFileName;
55     this.filterMappingsFileName = filterMappingsFileName;
56     
57     viewsConfig = this.readUiViewsConfig();
58     filtersConfig = this.readUiFiltersConfig();
59   }
60
61   /**
62    * Initialize config.
63    */
64   private void initializeFilters() {
65     filtersFileName = SparkyConstants.FILTER_LIST_FILE_DEFAULT;
66     filterMappingsFileName = SparkyConstants.FILTER_MAPPING_FILE_DEFAULT;
67
68     viewsConfig = this.readUiViewsConfig();
69     filtersConfig = this.readUiFiltersConfig();
70   }
71
72   public String getFilterMappingsFileName() {
73     return filterMappingsFileName;
74   }
75
76   public void setFilterMappingsFileName(String filterMappingsFileName) {
77     this.filterMappingsFileName = filterMappingsFileName;
78   }
79
80   public String getFiltersFileName() {
81     return filtersFileName;
82   }
83
84   public void setFiltersFileName(String filtersFileName) {
85     this.filtersFileName = filtersFileName;
86   }
87
88   public FiltersForViewsConfig getViewsConfig() {
89     return viewsConfig;
90   }
91
92   public void setViewsConfig(FiltersForViewsConfig filtersMapEntity) {
93     this.viewsConfig = filtersMapEntity;
94   }
95
96   public FiltersDetailsConfig getFiltersConfig() {
97     return filtersConfig;
98   }
99   
100   public UiFilterConfig getFilterById(String filterId) {
101     for ( UiFilterConfig filter : filtersConfig.getFilters()) {
102       if ( filter.getFilterId().equals(filterId)) {
103         return filter;
104       }
105     }
106     
107     return null;
108   }
109   
110   public void setFiltersConfig(FiltersDetailsConfig filtersConfig) {
111     this.filtersConfig = filtersConfig;
112   }
113
114   public FiltersDetailsConfig readUiFiltersConfig() {
115     ObjectMapper mapper = new ObjectMapper();
116     FiltersDetailsConfig filtersConfig = null;
117     try{
118       filtersConfig = mapper.readValue(new File(this.getFiltersFileName()), FiltersDetailsConfig.class);
119     } catch (Exception e){
120       LOG.error(AaiUiMsgs.ERROR_READING_JSON_SCHEMA, SparkyConstants.getConfigPath(this.getFiltersFileName()));
121     }
122
123     return filtersConfig;
124   }
125
126   public FiltersForViewsConfig readUiViewsConfig() {
127     ObjectMapper mapper = new ObjectMapper();
128     FiltersForViewsConfig viewsConfig = null;
129     
130     try {
131       viewsConfig = mapper.readValue(new File(this.getFilterMappingsFileName()), FiltersForViewsConfig.class);
132     } catch (Exception e){
133       LOG.error(AaiUiMsgs.ERROR_READING_JSON_SCHEMA, SparkyConstants.getConfigPath(this.getFilterMappingsFileName()));
134     }
135
136     return viewsConfig;
137   }
138 }
139