2 * ============LICENSE_START=======================================================
3 * ONAP : ccsdk features
4 * ================================================================================
5 * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property.
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=========================================================
22 package org.onap.ccsdk.features.sdnr.wt.dataprovider.impl;
24 import java.net.MalformedURLException;
26 import java.util.ArrayList;
27 import java.util.Arrays;
28 import java.util.List;
30 import org.onap.ccsdk.features.sdnr.wt.common.configuration.Configuration;
31 import org.onap.ccsdk.features.sdnr.wt.common.configuration.ConfigurationFileRepresentation;
32 import org.onap.ccsdk.features.sdnr.wt.common.configuration.filechange.IConfigChangedListener;
33 import org.onap.ccsdk.features.sdnr.wt.common.database.config.HostInfo;
34 import org.onap.ccsdk.features.sdnr.wt.common.database.config.HostInfo.Protocol;
35 import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.IEsConfig;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
39 public class EsConfig implements Configuration, IEsConfig {
41 private static final Logger LOG = LoggerFactory.getLogger(EsConfig.class);
43 public static final String SECTION_MARKER_ES = "es";
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";
55 private static String defaultHostinfo = "${SDNRDBURL}";//printHosts(new HostInfo[] { new HostInfo("sdnrdb", 9200, Protocol.HTTP) });
56 private static final String DEFAULT_VALUE_CLUSTER = "";
57 /** check db data in this interval [in seconds] 0 deactivated */
58 private static final String DEFAULT_ARCHIVE_INTERVAL_SEC = "0";
59 /** keep data for this time [in seconds] 30 days */
60 private static final String DEFAULT_ARCHIVE_LIMIT_SEC = String.valueOf(60L * 60L * 24L * 30L);
61 //private static final String DEFAULT_KEY_NODE = "elasticsearchnode";
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}";
67 private final ConfigurationFileRepresentation configuration;
69 public EsConfig(ConfigurationFileRepresentation configuration) {
71 this.configuration = configuration;
72 this.configuration.addSection(SECTION_MARKER_ES);
80 public void setNode(String nodeName) {
81 configuration.setProperty(SECTION_MARKER_ES, PROPERTY_KEY_NODE, nodeName);
88 public String getNode() {
89 return configuration.getProperty(SECTION_MARKER_ES, PROPERTY_KEY_NODE);
92 public HostInfo[] getHosts() {
93 String dbHosts = configuration.getProperty(SECTION_MARKER_ES, PROPERTY_KEY_DBHOSTS);
94 return parseHosts(dbHosts);
96 public void setHosts(HostInfo[] hosts) {
97 this.configuration.setProperty(SECTION_MARKER_ES, PROPERTY_KEY_DBHOSTS, printHosts(hosts));
100 public String getCluster() {
101 return configuration.getProperty(SECTION_MARKER_ES, PROPERTY_KEY_ARCHIVE_INTERVAL);
104 public void setCluster(String cluster) {
105 configuration.setProperty(SECTION_MARKER_ES, PROPERTY_KEY_CLUSTER, cluster);
108 public boolean hasBasicAuthCredentials() {
109 return this.getBasicAuthUsername()!=null && this.getBasicAuthPassword()!=null &&
110 !this.getBasicAuthUsername().isEmpty() && !this.getBasicAuthPassword().isEmpty() ;
112 public String getBasicAuthUsername() {
113 return this.configuration.getProperty(SECTION_MARKER_ES, PROPERTY_KEY_AUTH_USERNAME);
115 public String getBasicAuthPassword() {
116 return this.configuration.getProperty(SECTION_MARKER_ES, PROPERTY_KEY_AUTH_PASSWORD);
119 public long getArchiveCheckIntervalSeconds() {
120 return configuration.getPropertyLong(SECTION_MARKER_ES, PROPERTY_KEY_ARCHIVE_INTERVAL).orElse(0L);
122 public boolean trustAllCerts() {
123 return configuration.getPropertyBoolean(SECTION_MARKER_ES, PROPERTY_KEY_TRUSTALLCERTS);
125 public void setArchiveCheckIntervalSeconds(long seconds) {
126 configuration.setProperty(SECTION_MARKER_ES, PROPERTY_KEY_ARCHIVE_INTERVAL, seconds);
130 public long getArchiveLifetimeSeconds() {
131 return configuration.getPropertyLong(SECTION_MARKER_ES, PROPERTY_KEY_ARCHIVE_LIMIT).orElse(0L);
134 public void setArchiveLimit(long seconds) {
135 configuration.setProperty(SECTION_MARKER_ES, PROPERTY_KEY_ARCHIVE_LIMIT, seconds);
139 public String getSectionName() {
140 return SECTION_MARKER_ES;
144 public synchronized void defaults() {
145 // Add default if not available
146 configuration.setPropertyIfNotAvailable(SECTION_MARKER_ES, PROPERTY_KEY_DBHOSTS, defaultHostinfo);
147 configuration.setPropertyIfNotAvailable(SECTION_MARKER_ES, PROPERTY_KEY_ARCHIVE_LIMIT,
148 DEFAULT_ARCHIVE_LIMIT_SEC);
149 configuration.setPropertyIfNotAvailable(SECTION_MARKER_ES, PROPERTY_KEY_CLUSTER, DEFAULT_VALUE_CLUSTER);
150 configuration.setPropertyIfNotAvailable(SECTION_MARKER_ES, PROPERTY_KEY_ARCHIVE_INTERVAL,
151 DEFAULT_ARCHIVE_INTERVAL_SEC);
152 configuration.setPropertyIfNotAvailable(SECTION_MARKER_ES, PROPERTY_KEY_NODE, DEFAULT_VALUE_NODE);
153 configuration.setPropertyIfNotAvailable(SECTION_MARKER_ES, PROPERTY_KEY_AUTH_USERNAME, DEFAULT_VALUE_DBUSERNAME);
154 configuration.setPropertyIfNotAvailable(SECTION_MARKER_ES, PROPERTY_KEY_AUTH_PASSWORD, DEFAULT_VALUE_DBPASSWORD);
155 configuration.setPropertyIfNotAvailable(SECTION_MARKER_ES, PROPERTY_KEY_TRUSTALLCERTS, DEFAULT_VALUE_TRUSTALLCERTS);
160 public void unregisterConfigChangedListener(IConfigChangedListener archiveCleanService) {
161 configuration.unregisterConfigChangedListener(archiveCleanService);
165 public void registerConfigChangedListener(IConfigChangedListener archiveCleanService) {
166 configuration.registerConfigChangedListener(archiveCleanService);
169 /** @TODO Shift to own class **/
170 private static String printHosts(HostInfo[] h) {
171 StringBuilder sb = new StringBuilder();
172 for (int i = 0; i < h.length; i++) {
173 sb.append(h[i].toUrl());
174 if (i != h.length - 1) {
178 return sb.toString();
181 /** @TODO Shift to own class **/
182 private static HostInfo[] parseHosts(String string) {
183 List<HostInfo> infos = new ArrayList<>();
184 String[] list = string.split(",");
185 if (list.length > 0) {
186 for (String item : list) {
188 URL url = new URL(item);
189 infos.add(new HostInfo(url.getHost(), url.getPort(), Protocol.getValueOf(url.getProtocol())));
190 } catch (MalformedURLException e) {
191 LOG.warn("problem parsing url {} : {}", item, e.getMessage());
195 HostInfo[] a = new HostInfo[infos.size()];
196 return infos.toArray(a);
200 public String toString() {
201 return "EsConfig [getNode()=" + getNode() + ", getHosts()=" + Arrays.toString(getHosts()) + ", getCluster()="
202 + getCluster() + ", getArchiveCheckIntervalSeconds()=" + getArchiveCheckIntervalSeconds()
203 + ", getArchiveLifetimeSeconds()=" + getArchiveLifetimeSeconds() + ", getSectionName()="
204 + getSectionName() + "]";