2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017 AT&T Intellectual Property.
6 * Copyright © 2017 Amdocs
8 * ================================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License ati
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * ============LICENSE_END=========================================================
22 * ECOMP and OpenECOMP are trademarks
23 * and service marks of AT&T Intellectual Property.
25 package org.openecomp.sa.rest;
27 import com.fasterxml.jackson.databind.ObjectMapper;
28 import org.openecomp.cl.api.Logger;
29 import org.openecomp.cl.eelf.LoggerFactory;
30 import org.openecomp.sa.rest.AnalyzerSchema;
31 import org.openecomp.sa.rest.FilterSchema;
32 import org.openecomp.sa.searchdbabstraction.logging.SearchDbMsgs;
33 import org.openecomp.sa.searchdbabstraction.util.SearchDbConstants;
36 import java.io.IOException;
37 import java.util.concurrent.atomic.AtomicBoolean;
40 * This class encapsulates the configuration of the predefined
41 * Analyzer and Filter behaviours that help to tell the document
42 * store how to index the documents that are provided to it.
44 public class AnalysisConfiguration {
47 * Contains all of the predefined indexing filters.
49 private FilterSchema[] customFilters;
52 * Contains all of the predefined indexing analyzers.
54 private AnalyzerSchema[] customAnalysers;
57 * Indicates whether or not we have imported the filter and
58 * analyzer configurations.
60 private AtomicBoolean configured = new AtomicBoolean(false);
63 * A json format string which is readable by Elastic Search and defines
64 * all of the custom filters and analyzers that we need Elastic Search
67 private static String esSettings = null;
69 private static Logger logger = LoggerFactory.getInstance()
70 .getLogger(AnalysisConfiguration.class.getName());
74 * Imports the filter and analyzer configuration files and
75 * builds an Elastic Search readable settings file from the
78 * @param filterConfigFile - Location of filter configuration json file
79 * @param analyzerConfigFile - Location of analyzer configuration json file
81 public void init(String filterConfigFile, String analyzerConfigFile) {
83 if (configured.compareAndSet(false, true)) {
84 ObjectMapper mapper = new ObjectMapper();
86 File filtersConfig = new File(filterConfigFile);
88 customFilters = mapper.readValue(filtersConfig, FilterSchema[].class);
89 } catch (IOException e) {
92 logger.warn(SearchDbMsgs.FILTERS_CONFIG_FAILURE, filterConfigFile, e.getMessage());
95 File analysersConfig = new File(analyzerConfigFile);
97 customAnalysers = mapper.readValue(analysersConfig, AnalyzerSchema[].class);
98 } catch (IOException e) {
101 logger.warn(SearchDbMsgs.ANALYSYS_CONFIG_FAILURE, analyzerConfigFile, e.getMessage());
104 esSettings = buildEsIndexSettings();
110 * Returns the set of pre-configured filters.
112 * @return - An array of filters.
114 public FilterSchema[] getFilters() {
115 return customFilters;
120 * Returns the set of pre-configured analyzers.
122 * @return - An array of analyzers.
124 public AnalyzerSchema[] getAnalyzers() {
125 init(SearchDbConstants.SDB_FILTER_CONFIG_FILE, SearchDbConstants.SDB_ANALYSIS_CONFIG_FILE);
126 return customAnalysers;
131 * Imports the filter and analyzer configurations and translates those
132 * into a settings string that will be parseable by Elastic Search.
134 * @return - Elastic Search formatted settings string.
136 public String getEsIndexSettings() {
138 // Generate the es-settings string from our filter and analyzer
139 // configurations if we have not already done so.
140 init(SearchDbConstants.SDB_FILTER_CONFIG_FILE, SearchDbConstants.SDB_ANALYSIS_CONFIG_FILE);
142 // Now, return the es-settings string.
148 * Constructs a settings string that is readable by Elastic Search based
149 * on the contents of the filter and analyzer configuration files.
151 * @return Elastic Search formatted settings string.
153 public String buildEsIndexSettings() {
155 StringBuilder sb = new StringBuilder();
158 sb.append("\"analysis\": {");
160 // Define the custom filters.
161 boolean atLeastOneFilter = false;
162 sb.append("\"filter\": {");
163 AtomicBoolean firstFilter = new AtomicBoolean(true);
164 for (FilterSchema filter : customFilters) {
166 // Append a comma before the next entry, unless it is the
168 if (!firstFilter.compareAndSet(true, false)) {
172 // Now, build the filter entry.
173 buildFilterEntry(filter, sb);
174 atLeastOneFilter = true;
176 sb.append((atLeastOneFilter) ? "}," : "}");
178 // Define the custom analyzers.
179 sb.append("\"analyzer\": {");
180 AtomicBoolean firstAnalyzer = new AtomicBoolean(true);
181 for (AnalyzerSchema analyzer : customAnalysers) {
183 // Prepend a comma before the entry, unless it is the
185 if (!firstAnalyzer.compareAndSet(true, false)) {
189 // Now, construct the entry for this analyzer.
190 buildAnalyzerEntry(analyzer, sb);
197 return sb.toString();
202 * Constructs an ElasticSearch friendly custom filter definition.
204 * @param filter - The filter to generate ElasticSearch json for.
205 * @param sb - The string builder to append the filter definition
208 private void buildFilterEntry(FilterSchema filter, StringBuilder sb) {
210 sb.append("\"" + filter.getName()).append("\": {");
212 sb.append(filter.getConfiguration());
219 * Constructs an ElasticSearch friendly custom analyzer definition.
221 * @param analyzer - The analyzer to generate ElasticSearch json for.
222 * @param sb - The string builder to append the analyzer definition
225 private void buildAnalyzerEntry(AnalyzerSchema analyzer, StringBuilder sb) {
227 sb.append("\"").append(analyzer.getName()).append("\": {");
228 sb.append("\"type\": \"custom\",");
229 sb.append("\"tokenizer\": ").append("\"").append(analyzer.getTokenizer()).append("\",");
230 sb.append("\"filter\": [");
231 boolean firstFilter = true;
232 for (String filter : analyzer.getFilters()) {
238 sb.append("\"").append(filter).append("\"");