1 /*******************************************************************************
2 * ============LICENSE_START========================================================================
3 * ONAP : ccsdk feature sdnr wt
4 * =================================================================================================
5 * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property. All rights reserved.
6 * =================================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
8 * in compliance with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software distributed under the License
13 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
14 * or implied. See the License for the specific language governing permissions and limitations under
16 * ============LICENSE_END==========================================================================
17 ******************************************************************************/
18 package org.onap.ccsdk.features.sdnr.wt.dataprovider.impl;
20 import java.net.MalformedURLException;
22 import java.util.ArrayList;
23 import java.util.Arrays;
24 import java.util.List;
26 import org.onap.ccsdk.features.sdnr.wt.common.configuration.Configuration;
27 import org.onap.ccsdk.features.sdnr.wt.common.configuration.ConfigurationFileRepresentation;
28 import org.onap.ccsdk.features.sdnr.wt.common.configuration.filechange.IConfigChangedListener;
29 import org.onap.ccsdk.features.sdnr.wt.common.database.config.HostInfo;
30 import org.onap.ccsdk.features.sdnr.wt.common.database.config.HostInfo.Protocol;
31 import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.IEsConfig;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
35 public class EsConfig implements Configuration, IEsConfig {
37 private static final Logger LOG = LoggerFactory.getLogger(EsConfig.class);
39 public static final String SECTION_MARKER_ES = "es";
41 private static final String PROPERTY_KEY_DBHOSTS = "esHosts";
42 private static final String PROPERTY_KEY_ARCHIVE_LIMIT = "esArchiveLifetimeSeconds";
43 private static final String PROPERTY_KEY_CLUSTER = "esCluster";
44 private static final String PROPERTY_KEY_ARCHIVE_INTERVAL = "esArchiveCheckIntervalSeconds";
45 private static final String PROPERTY_KEY_NODE = "esNode";
46 private static final String PROPERTY_KEY_AUTH_USERNAME = "esAuthUsername";
47 private static final String PROPERTY_KEY_AUTH_PASSWORD = "esAuthPassword";
50 private static String defaultHostinfo = printHosts(new HostInfo[] { new HostInfo("sdnrdb", 9200, Protocol.HTTP) });
51 private static final String DEFAULT_VALUE_CLUSTER = "";
52 /** check db data in this interval [in seconds] 0 deactivated */
53 private static final String DEFAULT_ARCHIVE_INTERVAL_SEC = "0";
54 /** keep data for this time [in seconds] 30 days */
55 private static final String DEFAULT_ARCHIVE_LIMIT_SEC = String.valueOf(60L * 60L * 24L * 30L);
56 //private static final String DEFAULT_KEY_NODE = "elasticsearchnode";
57 private static final String DEFAULT_VALUE_NODE = "elasticsearchnode";
58 private static final String DEFAULT_VALUE_DBUSERNAME = "${SDNRDBUSERNAME}";
59 private static final String DEFAULT_VALUE_DBPASSWORD = "${SDNRDBPASSWORD}";
61 private final ConfigurationFileRepresentation configuration;
63 public EsConfig(ConfigurationFileRepresentation configuration) {
65 this.configuration = configuration;
66 this.configuration.addSection(SECTION_MARKER_ES);
74 public void setNode(String nodeName) {
75 configuration.setProperty(SECTION_MARKER_ES, PROPERTY_KEY_NODE, nodeName);
82 public String getNode() {
83 return configuration.getProperty(SECTION_MARKER_ES, PROPERTY_KEY_NODE);
86 public HostInfo[] getHosts() {
87 String dbHosts = configuration.getProperty(SECTION_MARKER_ES, PROPERTY_KEY_DBHOSTS);
88 return parseHosts(dbHosts);
90 public void setHosts(HostInfo[] hosts) {
91 this.configuration.setProperty(SECTION_MARKER_ES, PROPERTY_KEY_DBHOSTS, printHosts(hosts));
94 public String getCluster() {
95 return configuration.getProperty(SECTION_MARKER_ES, PROPERTY_KEY_ARCHIVE_INTERVAL);
98 public void setCluster(String cluster) {
99 configuration.setProperty(SECTION_MARKER_ES, PROPERTY_KEY_CLUSTER, cluster);
102 public boolean hasBasicAuthCredentials() {
103 return this.getBasicAuthUsername()!=null && this.getBasicAuthPassword()!=null &&
104 !this.getBasicAuthUsername().isEmpty() && !this.getBasicAuthPassword().isEmpty() ;
106 public String getBasicAuthUsername() {
107 return this.configuration.getProperty(SECTION_MARKER_ES, PROPERTY_KEY_AUTH_USERNAME);
109 public String getBasicAuthPassword() {
110 return this.configuration.getProperty(SECTION_MARKER_ES, PROPERTY_KEY_AUTH_PASSWORD);
113 public long getArchiveCheckIntervalSeconds() {
114 return configuration.getPropertyLong(SECTION_MARKER_ES, PROPERTY_KEY_ARCHIVE_INTERVAL).orElse(0L);
117 public void setArchiveCheckIntervalSeconds(long seconds) {
118 configuration.setProperty(SECTION_MARKER_ES, PROPERTY_KEY_ARCHIVE_INTERVAL, seconds);
122 public long getArchiveLifetimeSeconds() {
123 return configuration.getPropertyLong(SECTION_MARKER_ES, PROPERTY_KEY_ARCHIVE_LIMIT).orElse(0L);
126 public void setArchiveLimit(long seconds) {
127 configuration.setProperty(SECTION_MARKER_ES, PROPERTY_KEY_ARCHIVE_LIMIT, seconds);
131 public String getSectionName() {
132 return SECTION_MARKER_ES;
136 public void defaults() {
137 // Add default if not available
138 configuration.setPropertyIfNotAvailable(SECTION_MARKER_ES, PROPERTY_KEY_DBHOSTS, defaultHostinfo);
139 configuration.setPropertyIfNotAvailable(SECTION_MARKER_ES, PROPERTY_KEY_ARCHIVE_LIMIT,
140 DEFAULT_ARCHIVE_LIMIT_SEC);
141 configuration.setPropertyIfNotAvailable(SECTION_MARKER_ES, PROPERTY_KEY_CLUSTER, DEFAULT_VALUE_CLUSTER);
142 configuration.setPropertyIfNotAvailable(SECTION_MARKER_ES, PROPERTY_KEY_ARCHIVE_INTERVAL,
143 DEFAULT_ARCHIVE_INTERVAL_SEC);
144 configuration.setPropertyIfNotAvailable(SECTION_MARKER_ES, PROPERTY_KEY_NODE, DEFAULT_VALUE_NODE);
145 configuration.setPropertyIfNotAvailable(SECTION_MARKER_ES, PROPERTY_KEY_AUTH_USERNAME, DEFAULT_VALUE_DBUSERNAME);
146 configuration.setPropertyIfNotAvailable(SECTION_MARKER_ES, PROPERTY_KEY_AUTH_PASSWORD, DEFAULT_VALUE_DBPASSWORD);
150 public void unregisterConfigChangedListener(IConfigChangedListener archiveCleanService) {
151 configuration.unregisterConfigChangedListener(archiveCleanService);
155 public void registerConfigChangedListener(IConfigChangedListener archiveCleanService) {
156 configuration.registerConfigChangedListener(archiveCleanService);
159 /** @TODO Shift to own class **/
160 private static String printHosts(HostInfo[] h) {
161 StringBuilder sb = new StringBuilder();
162 for (int i = 0; i < h.length; i++) {
163 sb.append(h[i].toUrl());
164 if (i != h.length - 1) {
168 return sb.toString();
171 /** @TODO Shift to own class **/
172 private static HostInfo[] parseHosts(String string) {
173 List<HostInfo> infos = new ArrayList<>();
174 String[] list = string.split(",");
175 if (list.length > 0) {
176 for (String item : list) {
178 URL url = new URL(item);
179 infos.add(new HostInfo(url.getHost(), url.getPort(), Protocol.getValueOf(url.getProtocol())));
180 } catch (MalformedURLException e) {
181 LOG.warn("problem parsing url {} : {}", item, e.getMessage());
185 HostInfo[] a = new HostInfo[infos.size()];
186 return infos.toArray(a);
190 public String toString() {
191 return "EsConfig [getNode()=" + getNode() + ", getHosts()=" + Arrays.toString(getHosts()) + ", getCluster()="
192 + getCluster() + ", getArchiveCheckIntervalSeconds()=" + getArchiveCheckIntervalSeconds()
193 + ", getArchiveLifetimeSeconds()=" + getArchiveLifetimeSeconds() + ", getSectionName()="
194 + getSectionName() + "]";