2 * ============LICENSE_START=======================================================
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
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 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
23 package org.openecomp.sa.rest;
25 import com.fasterxml.jackson.databind.ObjectMapper;
26 import org.openecomp.cl.api.Logger;
27 import org.openecomp.cl.eelf.LoggerFactory;
28 import org.openecomp.sa.rest.AnalyzerSchema;
29 import org.openecomp.sa.rest.FilterSchema;
30 import org.openecomp.sa.searchdbabstraction.logging.SearchDbMsgs;
31 import org.openecomp.sa.searchdbabstraction.util.SearchDbConstants;
34 import java.io.IOException;
35 import java.util.concurrent.atomic.AtomicBoolean;
38 * This class encapsulates the configuration of the predefined
39 * Analyzer and Filter behaviours that help to tell the document
40 * store how to index the documents that are provided to it.
42 public class AnalysisConfiguration {
45 * Contains all of the predefined indexing filters.
47 private FilterSchema[] customFilters;
50 * Contains all of the predefined indexing analyzers.
52 private AnalyzerSchema[] customAnalysers;
55 * Indicates whether or not we have imported the filter and
56 * analyzer configurations.
58 private AtomicBoolean configured = new AtomicBoolean(false);
61 * A json format string which is readable by Elastic Search and defines
62 * all of the custom filters and analyzers that we need Elastic Search
65 private static String esSettings = null;
67 private static Logger logger = LoggerFactory.getInstance()
68 .getLogger(AnalysisConfiguration.class.getName());
72 * Imports the filter and analyzer configuration files and
73 * builds an Elastic Search readable settings file from the
76 * @param filterConfigFile - Location of filter configuration json file
77 * @param analyzerConfigFile - Location of analyzer configuration json file
79 public void init(String filterConfigFile, String analyzerConfigFile) {
81 if (configured.compareAndSet(false, true)) {
82 ObjectMapper mapper = new ObjectMapper();
84 File filtersConfig = new File(filterConfigFile);
86 customFilters = mapper.readValue(filtersConfig, FilterSchema[].class);
87 } catch (IOException e) {
90 logger.warn(SearchDbMsgs.FILTERS_CONFIG_FAILURE, filterConfigFile, e.getMessage());
93 File analysersConfig = new File(analyzerConfigFile);
95 customAnalysers = mapper.readValue(analysersConfig, AnalyzerSchema[].class);
96 } catch (IOException e) {
99 logger.warn(SearchDbMsgs.ANALYSYS_CONFIG_FAILURE, analyzerConfigFile, e.getMessage());
102 esSettings = buildEsIndexSettings();
108 * Returns the set of pre-configured filters.
110 * @return - An array of filters.
112 public FilterSchema[] getFilters() {
113 return customFilters;
118 * Returns the set of pre-configured analyzers.
120 * @return - An array of analyzers.
122 public AnalyzerSchema[] getAnalyzers() {
123 init(SearchDbConstants.SDB_FILTER_CONFIG_FILE, SearchDbConstants.SDB_ANALYSIS_CONFIG_FILE);
124 return customAnalysers;
129 * Imports the filter and analyzer configurations and translates those
130 * into a settings string that will be parseable by Elastic Search.
132 * @return - Elastic Search formatted settings string.
134 public String getEsIndexSettings() {
136 // Generate the es-settings string from our filter and analyzer
137 // configurations if we have not already done so.
138 init(SearchDbConstants.SDB_FILTER_CONFIG_FILE, SearchDbConstants.SDB_ANALYSIS_CONFIG_FILE);
140 // Now, return the es-settings string.
146 * Constructs a settings string that is readable by Elastic Search based
147 * on the contents of the filter and analyzer configuration files.
149 * @return Elastic Search formatted settings string.
151 public String buildEsIndexSettings() {
153 StringBuilder sb = new StringBuilder();
156 sb.append("\"analysis\": {");
158 // Define the custom filters.
159 boolean atLeastOneFilter = false;
160 sb.append("\"filter\": {");
161 AtomicBoolean firstFilter = new AtomicBoolean(true);
162 for (FilterSchema filter : customFilters) {
164 // Append a comma before the next entry, unless it is the
166 if (!firstFilter.compareAndSet(true, false)) {
170 // Now, build the filter entry.
171 buildFilterEntry(filter, sb);
172 atLeastOneFilter = true;
174 sb.append((atLeastOneFilter) ? "}," : "}");
176 // Define the custom analyzers.
177 sb.append("\"analyzer\": {");
178 AtomicBoolean firstAnalyzer = new AtomicBoolean(true);
179 for (AnalyzerSchema analyzer : customAnalysers) {
181 // Prepend a comma before the entry, unless it is the
183 if (!firstAnalyzer.compareAndSet(true, false)) {
187 // Now, construct the entry for this analyzer.
188 buildAnalyzerEntry(analyzer, sb);
195 return sb.toString();
200 * Constructs an ElasticSearch friendly custom filter definition.
202 * @param filter - The filter to generate ElasticSearch json for.
203 * @param sb - The string builder to append the filter definition
206 private void buildFilterEntry(FilterSchema filter, StringBuilder sb) {
208 sb.append("\"" + filter.getName()).append("\": {");
210 sb.append(filter.getConfiguration());
217 * Constructs an ElasticSearch friendly custom analyzer definition.
219 * @param analyzer - The analyzer to generate ElasticSearch json for.
220 * @param sb - The string builder to append the analyzer definition
223 private void buildAnalyzerEntry(AnalyzerSchema analyzer, StringBuilder sb) {
225 sb.append("\"").append(analyzer.getName()).append("\": {");
226 sb.append("\"type\": \"custom\",");
227 sb.append("\"tokenizer\": ").append("\"").append(analyzer.getTokenizer()).append("\",");
228 sb.append("\"filter\": [");
229 boolean firstFilter = true;
230 for (String filter : analyzer.getFilters()) {
236 sb.append("\"").append(filter).append("\"");