Search service configurable index settings
[aai/search-data-service.git] / src / main / java / org / onap / aai / sa / rest / SettingConfiguration.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2019 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2019 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 package org.onap.aai.sa.rest;
22
23 import java.io.IOException;
24 import java.nio.file.Files;
25 import java.nio.file.Path;
26 import java.nio.file.Paths;
27 import java.util.concurrent.atomic.AtomicBoolean;
28 import org.onap.aai.sa.searchdbabstraction.util.SearchDbConstants;
29
30
31 public class SettingConfiguration {
32
33     /**
34      * Indicates whether or not we have imported the filter and analyzer configurations.
35      */
36     private AtomicBoolean configured = new AtomicBoolean(false);
37
38     /**
39      * A json format string which is readable by Elastic Search and defines all of the custom filters and analyzers that
40      * we need Elastic Search to know about.
41      */
42     private String settings;
43
44     public void init(String settingConfigFile) {
45
46         if (configured.compareAndSet(false, true)) {
47             try {
48                 Path path = Paths.get(settingConfigFile);
49                 settings = new String(Files.readAllBytes(path));
50                 
51                 // Remove the enclosing brackets from the json blob.
52                 settings = settings.replaceFirst("\\{", "");
53                 settings = settings.substring(0, settings.lastIndexOf("}"));        
54             } catch (IOException e) {
55                 // It is valid not to have a settings file.
56                 settings = "";
57             }
58         }
59     }
60
61
62     /**
63      * Returns the set of pre-configured settings.
64      *
65      * @return - settings.
66      */
67     public String getSettings() {
68         init(SearchDbConstants.SDB_SETTINGS_CONFIG_FILE);
69         return settings;
70     }
71     
72     public String getSettingsWithAnalysis(AnalysisConfiguration analysisConfig) {
73         String ac = analysisConfig.getEsIndexSettings();
74         StringBuilder sb = new StringBuilder();
75         sb.append(ac.substring(0, ac.lastIndexOf("}")));
76         
77         if (!getSettings().trim().isEmpty()) {
78             sb.append(", " + getSettings());
79         }
80         
81         sb.append(" }");
82         return sb.toString();
83     }
84 }