a16a157530b696c859159c66d451cbbd60968c2b
[ccsdk/features.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP : ccsdk features
4  * ================================================================================
5  * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property.
6  * All rights reserved.
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  */
22 package org.onap.ccsdk.features.sdnr.wt.dataprovider.database.elasticsearch;
23
24 import java.net.MalformedURLException;
25 import java.net.URL;
26 import java.util.ArrayList;
27 import java.util.Arrays;
28 import java.util.List;
29 import org.onap.ccsdk.features.sdnr.wt.common.configuration.Configuration;
30 import org.onap.ccsdk.features.sdnr.wt.common.configuration.ConfigurationFileRepresentation;
31 import org.onap.ccsdk.features.sdnr.wt.common.configuration.filechange.IConfigChangedListener;
32 import org.onap.ccsdk.features.sdnr.wt.common.database.config.HostInfo;
33 import org.onap.ccsdk.features.sdnr.wt.common.database.config.HostInfo.Protocol;
34 import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.IEsConfig;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38 @Deprecated
39 public class EsConfig implements Configuration, IEsConfig {
40
41     private static final Logger LOG = LoggerFactory.getLogger(EsConfig.class);
42
43     public static final String SECTION_MARKER_ES = "es";
44
45     private static final String PROPERTY_KEY_DBHOSTS = "esHosts";
46     private static final String PROPERTY_KEY_TRUSTALLCERTS = "esTrustAllCerts";
47     private static final String PROPERTY_KEY_ARCHIVE_LIMIT = "esArchiveLifetimeSeconds";
48     private static final String PROPERTY_KEY_CLUSTER = "esCluster";
49     private static final String PROPERTY_KEY_ARCHIVE_INTERVAL = "esArchiveCheckIntervalSeconds";
50     private static final String PROPERTY_KEY_NODE = "esNode";
51     private static final String PROPERTY_KEY_AUTH_USERNAME = "esAuthUsername";
52     private static final String PROPERTY_KEY_AUTH_PASSWORD = "esAuthPassword";
53     private static final String PROPERTY_KEY_FULLSIZE = "esFullsize";
54
55
56     private static String defaultHostinfo = "${SDNRDBURL}";
57     private static final String DEFAULT_VALUE_CLUSTER = "";
58     /** check db data in this interval [in seconds] 0 deactivated */
59     private static final String DEFAULT_ARCHIVE_INTERVAL_SEC = "0";
60     /** keep data for this time [in seconds] 30 days */
61     private static final String DEFAULT_ARCHIVE_LIMIT_SEC = String.valueOf(60L * 60L * 24L * 30L);
62     private static final String DEFAULT_VALUE_NODE = "elasticsearchnode";
63     private static final String DEFAULT_VALUE_DBUSERNAME = "${SDNRDBUSERNAME}";
64     private static final String DEFAULT_VALUE_DBPASSWORD = "${SDNRDBPASSWORD}";
65     private static final String DEFAULT_VALUE_TRUSTALLCERTS = "${SDNRDBTRUSTALLCERTS}";
66     private static final String DEFAULT_VALUE_FULLSIZE = "${SDNRDBFULLSIZEREQUESTS}";
67
68     private final ConfigurationFileRepresentation configuration;
69
70     public EsConfig(ConfigurationFileRepresentation configuration) {
71
72         this.configuration = configuration;
73         this.configuration.addSection(SECTION_MARKER_ES);
74         defaults();
75     }
76
77     /*
78      * Setter
79      */
80
81     public void setNode(String nodeName) {
82         configuration.setProperty(SECTION_MARKER_ES, PROPERTY_KEY_NODE, nodeName);
83     }
84
85     /*
86      * Getter
87      */
88
89     public String getNode() {
90         return configuration.getProperty(SECTION_MARKER_ES, PROPERTY_KEY_NODE);
91     }
92
93     public HostInfo[] getHosts() {
94         String dbHosts = configuration.getProperty(SECTION_MARKER_ES, PROPERTY_KEY_DBHOSTS);
95         return parseHosts(dbHosts);
96     }
97
98     public void setHosts(HostInfo[] hosts) {
99         this.configuration.setProperty(SECTION_MARKER_ES, PROPERTY_KEY_DBHOSTS, printHosts(hosts));
100     }
101
102     @Override
103     public String getCluster() {
104         return configuration.getProperty(SECTION_MARKER_ES, PROPERTY_KEY_ARCHIVE_INTERVAL);
105     }
106
107     public void setCluster(String cluster) {
108         configuration.setProperty(SECTION_MARKER_ES, PROPERTY_KEY_CLUSTER, cluster);
109     }
110
111     public boolean hasBasicAuthCredentials() {
112         return this.getBasicAuthUsername() != null && this.getBasicAuthPassword() != null
113                 && !this.getBasicAuthUsername().isEmpty() && !this.getBasicAuthPassword().isEmpty();
114     }
115
116     public String getBasicAuthUsername() {
117         return this.configuration.getProperty(SECTION_MARKER_ES, PROPERTY_KEY_AUTH_USERNAME);
118     }
119
120     public String getBasicAuthPassword() {
121         return this.configuration.getProperty(SECTION_MARKER_ES, PROPERTY_KEY_AUTH_PASSWORD);
122     }
123
124     @Override
125     public long getArchiveCheckIntervalSeconds() {
126         return configuration.getPropertyLong(SECTION_MARKER_ES, PROPERTY_KEY_ARCHIVE_INTERVAL).orElse(0L);
127     }
128
129     public boolean trustAllCerts() {
130         return configuration.getPropertyBoolean(SECTION_MARKER_ES, PROPERTY_KEY_TRUSTALLCERTS);
131     }
132
133     public void setArchiveCheckIntervalSeconds(long seconds) {
134         configuration.setProperty(SECTION_MARKER_ES, PROPERTY_KEY_ARCHIVE_INTERVAL, seconds);
135     }
136
137     @Override
138     public long getArchiveLifetimeSeconds() {
139         return configuration.getPropertyLong(SECTION_MARKER_ES, PROPERTY_KEY_ARCHIVE_LIMIT).orElse(0L);
140     }
141
142     public void setArchiveLimit(long seconds) {
143         configuration.setProperty(SECTION_MARKER_ES, PROPERTY_KEY_ARCHIVE_LIMIT, seconds);
144     }
145
146     @Override
147     public String getSectionName() {
148         return SECTION_MARKER_ES;
149     }
150
151     @Override
152     public synchronized void defaults() {
153         // Add default if not available
154         configuration.setPropertyIfNotAvailable(SECTION_MARKER_ES, PROPERTY_KEY_DBHOSTS, defaultHostinfo);
155         configuration.setPropertyIfNotAvailable(SECTION_MARKER_ES, PROPERTY_KEY_ARCHIVE_LIMIT,
156                 DEFAULT_ARCHIVE_LIMIT_SEC);
157         configuration.setPropertyIfNotAvailable(SECTION_MARKER_ES, PROPERTY_KEY_CLUSTER, DEFAULT_VALUE_CLUSTER);
158         configuration.setPropertyIfNotAvailable(SECTION_MARKER_ES, PROPERTY_KEY_ARCHIVE_INTERVAL,
159                 DEFAULT_ARCHIVE_INTERVAL_SEC);
160         configuration.setPropertyIfNotAvailable(SECTION_MARKER_ES, PROPERTY_KEY_NODE, DEFAULT_VALUE_NODE);
161         configuration.setPropertyIfNotAvailable(SECTION_MARKER_ES, PROPERTY_KEY_AUTH_USERNAME,
162                 DEFAULT_VALUE_DBUSERNAME);
163         configuration.setPropertyIfNotAvailable(SECTION_MARKER_ES, PROPERTY_KEY_AUTH_PASSWORD,
164                 DEFAULT_VALUE_DBPASSWORD);
165         configuration.setPropertyIfNotAvailable(SECTION_MARKER_ES, PROPERTY_KEY_TRUSTALLCERTS,
166                 DEFAULT_VALUE_TRUSTALLCERTS);
167         configuration.setPropertyIfNotAvailable(SECTION_MARKER_ES, PROPERTY_KEY_FULLSIZE,
168                 DEFAULT_VALUE_FULLSIZE);
169
170     }
171
172     @Override
173     public void unregisterConfigChangedListener(IConfigChangedListener archiveCleanService) {
174         configuration.unregisterConfigChangedListener(archiveCleanService);
175     }
176
177     @Override
178     public void registerConfigChangedListener(IConfigChangedListener archiveCleanService) {
179         configuration.registerConfigChangedListener(archiveCleanService);
180     }
181
182     /** @TODO Shift to own class **/
183     private static String printHosts(HostInfo[] h) {
184         StringBuilder sb = new StringBuilder();
185         for (int i = 0; i < h.length; i++) {
186             sb.append(h[i].toUrl());
187             if (i != h.length - 1) {
188                 sb.append(",");
189             }
190         }
191         return sb.toString();
192     }
193
194     /** @TODO Shift to own class **/
195     private static HostInfo[] parseHosts(String string) {
196         List<HostInfo> infos = new ArrayList<>();
197         String[] list = string.split(",");
198         if (list.length > 0) {
199             for (String item : list) {
200                 try {
201                     URL url = new URL(item);
202                     infos.add(new HostInfo(url.getHost(), url.getPort(), Protocol.getValueOf(url.getProtocol())));
203                 } catch (MalformedURLException e) {
204                     LOG.warn("problem parsing url {} : {}", item, e.getMessage());
205                 }
206             }
207         }
208         HostInfo[] a = new HostInfo[infos.size()];
209         return infos.toArray(a);
210     }
211
212     @Override
213     public String toString() {
214         return "EsConfig [getNode()=" + getNode() + ", getHosts()=" + Arrays.toString(getHosts()) + ", getCluster()="
215                 + getCluster() + ", getArchiveCheckIntervalSeconds()=" + getArchiveCheckIntervalSeconds()
216                 + ", getArchiveLifetimeSeconds()=" + getArchiveLifetimeSeconds() + ", getSectionName()="
217                 + getSectionName() + "]";
218     }
219
220     @Override
221     public boolean doFullsizeRequests() {
222         return configuration.getPropertyBoolean(SECTION_MARKER_ES, PROPERTY_KEY_FULLSIZE);
223     }
224
225 }