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