Adding UI extensibility
[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.sync.config.NetworkStatisticsConfig;
31 import org.onap.aai.sparky.viewandinspect.config.TierSupportUiConstants;
32
33 import com.fasterxml.jackson.databind.ObjectMapper;
34
35 public class FiltersConfig {
36
37   private static final Logger LOG = LoggerFactory.getInstance().getLogger(FiltersConfig.class);
38
39   private static FiltersConfig instance;
40
41   private String filtersFileName;
42
43   private String filterMappingsFileName;
44
45   private FiltersForViewsConfig viewsConfig;
46
47   private FiltersDetailsConfig filtersConfig;
48
49   private NetworkStatisticsConfig processorConfig;
50
51   public NetworkStatisticsConfig getProcessorConfig() {
52     return processorConfig;
53   }
54
55   public void setProcessorConfig(NetworkStatisticsConfig processorConfig) {
56     this.processorConfig = processorConfig;
57   }
58
59   public static FiltersConfig getInstance() {
60     if (instance == null) {
61       instance = new FiltersConfig();
62       instance.initializeFilters();
63     }
64
65     return instance;
66   }
67
68   public static void setConfig(FiltersConfig config) {
69     FiltersConfig.instance = config;
70   }
71
72   /**
73    * Instantiates a new UiViewFilterConfig.
74    */
75   private FiltersConfig() {}
76
77   /**
78    * Initialize config.
79    */
80   private void initializeFilters() {
81     filtersFileName = TierSupportUiConstants.FILTER_LIST_FILE_DEFAULT;
82     filterMappingsFileName = TierSupportUiConstants.FILTER_MAPPING_FILE_DEFAULT;
83
84     viewsConfig = this.readUiViewsConfig();
85     filtersConfig = this.readUiFiltersConfig();
86   }
87
88   public String getFilterMappingsFileName() {
89     return filterMappingsFileName;
90   }
91
92   public void setFilterMappingsFileName(String filterMappingsFileName) {
93     this.filterMappingsFileName = filterMappingsFileName;
94   }
95
96   public String getFiltersFileName() {
97     return filtersFileName;
98   }
99
100   public void setFiltersFileName(String filtersFileName) {
101     this.filtersFileName = filtersFileName;
102   }
103
104   public FiltersForViewsConfig getViewsConfig() {
105     return viewsConfig;
106   }
107
108   public void setViewsConfig(FiltersForViewsConfig filtersMapEntity) {
109     this.viewsConfig = filtersMapEntity;
110   }
111
112   public FiltersDetailsConfig getFiltersConfig() {
113     return filtersConfig;
114   }
115
116   public UiFilterConfig getFilterById(String filterId) {
117     for (UiFilterConfig filter : filtersConfig.getFilters()) {
118       if (filter.getFilterId().equals(filterId)) {
119         return filter;
120       }
121     }
122
123     return null;
124   }
125
126   public void setFiltersConfig(FiltersDetailsConfig filtersConfig) {
127     this.filtersConfig = filtersConfig;
128   }
129
130   public FiltersDetailsConfig readUiFiltersConfig() {
131     ObjectMapper mapper = new ObjectMapper();
132     FiltersDetailsConfig filtersConfig = null;
133     try {
134       filtersConfig =
135           mapper.readValue(new File(this.getFiltersFileName()), FiltersDetailsConfig.class);
136       System.out.println(String.valueOf(filtersConfig));
137     } catch (Exception e) {
138       LOG.error(AaiUiMsgs.ERROR_READING_JSON_SCHEMA,
139           TierSupportUiConstants.getConfigPath(this.getFiltersFileName()));
140     }
141
142     return filtersConfig;
143   }
144
145   public FiltersForViewsConfig readUiViewsConfig() {
146     ObjectMapper mapper = new ObjectMapper();
147     FiltersForViewsConfig viewsConfig = null;
148
149     try {
150       viewsConfig =
151           mapper.readValue(new File(this.getFilterMappingsFileName()), FiltersForViewsConfig.class);
152       System.out.println(String.valueOf(viewsConfig));
153     } catch (Exception e) {
154       LOG.error(AaiUiMsgs.ERROR_READING_JSON_SCHEMA,
155           TierSupportUiConstants.getConfigPath(this.getFilterMappingsFileName()));
156     }
157
158     return viewsConfig;
159   }
160 }
161