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";
58 private final ConfigurationFileRepresentation configuration;
60 public EsConfig(ConfigurationFileRepresentation configuration) {
62 this.configuration = configuration;
63 this.configuration.addSection(SECTION_MARKER_ES);
71 public void setNode(String nodeName) {
72 configuration.setProperty(SECTION_MARKER_ES, PROPERTY_KEY_NODE, nodeName);
79 public String getNode() {
80 return configuration.getProperty(SECTION_MARKER_ES, PROPERTY_KEY_NODE);
83 public HostInfo[] getHosts() {
84 String dbHosts = configuration.getProperty(SECTION_MARKER_ES, PROPERTY_KEY_DBHOSTS);
85 return parseHosts(dbHosts);
87 public void setHosts(HostInfo[] hosts) {
88 this.configuration.setProperty(SECTION_MARKER_ES, PROPERTY_KEY_DBHOSTS, printHosts(hosts));
91 public String getCluster() {
92 return configuration.getProperty(SECTION_MARKER_ES, PROPERTY_KEY_ARCHIVE_INTERVAL);
95 public void setCluster(String cluster) {
96 configuration.setProperty(SECTION_MARKER_ES, PROPERTY_KEY_CLUSTER, cluster);
99 public boolean hasBasicAuthCredentials() {
100 return this.getBasicAuthUsername()!=null && this.getBasicAuthPassword()!=null &&
101 this.getBasicAuthUsername()!="" && this.getBasicAuthPassword()!="" ;
103 public String getBasicAuthUsername() {
104 return this.configuration.getProperty(SECTION_MARKER_ES, PROPERTY_KEY_AUTH_USERNAME);
106 public String getBasicAuthPassword() {
107 return this.configuration.getProperty(SECTION_MARKER_ES, PROPERTY_KEY_AUTH_PASSWORD);
110 public long getArchiveCheckIntervalSeconds() {
111 return configuration.getPropertyLong(SECTION_MARKER_ES, PROPERTY_KEY_ARCHIVE_INTERVAL).orElse(0L);
114 public void setArchiveCheckIntervalSeconds(long seconds) {
115 configuration.setProperty(SECTION_MARKER_ES, PROPERTY_KEY_ARCHIVE_INTERVAL, seconds);
119 public long getArchiveLifetimeSeconds() {
120 return configuration.getPropertyLong(SECTION_MARKER_ES, PROPERTY_KEY_ARCHIVE_LIMIT).orElse(0L);
123 public void setArchiveLimit(long seconds) {
124 configuration.setProperty(SECTION_MARKER_ES, PROPERTY_KEY_ARCHIVE_LIMIT, seconds);
128 public String getSectionName() {
129 return SECTION_MARKER_ES;
133 public void defaults() {
134 // Add default if not available
135 configuration.setPropertyIfNotAvailable(SECTION_MARKER_ES, PROPERTY_KEY_DBHOSTS, defaultHostinfo);
136 configuration.setPropertyIfNotAvailable(SECTION_MARKER_ES, PROPERTY_KEY_ARCHIVE_LIMIT,
137 DEFAULT_ARCHIVE_LIMIT_SEC);
138 configuration.setPropertyIfNotAvailable(SECTION_MARKER_ES, PROPERTY_KEY_CLUSTER, DEFAULT_VALUE_CLUSTER);
139 configuration.setPropertyIfNotAvailable(SECTION_MARKER_ES, PROPERTY_KEY_ARCHIVE_INTERVAL,
140 DEFAULT_ARCHIVE_INTERVAL_SEC);
141 configuration.setPropertyIfNotAvailable(SECTION_MARKER_ES, PROPERTY_KEY_NODE, DEFAULT_KEY_NODE);
142 configuration.setPropertyIfNotAvailable(SECTION_MARKER_ES, PROPERTY_KEY_AUTH_USERNAME, "");
143 configuration.setPropertyIfNotAvailable(SECTION_MARKER_ES, PROPERTY_KEY_AUTH_PASSWORD, "");
147 public void unregisterConfigChangedListener(IConfigChangedListener archiveCleanService) {
148 configuration.unregisterConfigChangedListener(archiveCleanService);
152 public void registerConfigChangedListener(IConfigChangedListener archiveCleanService) {
153 configuration.registerConfigChangedListener(archiveCleanService);
156 /** @TODO Shift to own class **/
157 private static String printHosts(HostInfo[] h) {
158 StringBuilder sb = new StringBuilder();
159 for (int i = 0; i < h.length; i++) {
160 sb.append(h[i].toUrl());
161 if (i != h.length - 1) {
165 return sb.toString();
168 /** @TODO Shift to own class **/
169 private static HostInfo[] parseHosts(String string) {
170 List<HostInfo> infos = new ArrayList<>();
171 String[] list = string.split(",");
172 if (list.length > 0) {
173 for (String item : list) {
175 URL url = new URL(item);
176 infos.add(new HostInfo(url.getHost(), url.getPort(), Protocol.getValueOf(url.getProtocol())));
177 } catch (MalformedURLException e) {
178 LOG.warn("problem parsing url {} : {}", item, e.getMessage());
182 HostInfo[] a = new HostInfo[infos.size()];
183 return infos.toArray(a);
187 public String toString() {
188 return "EsConfig [getNode()=" + getNode() + ", getHosts()=" + Arrays.toString(getHosts()) + ", getCluster()="
189 + getCluster() + ", getArchiveCheckIntervalSeconds()=" + getArchiveCheckIntervalSeconds()
190 + ", getArchiveLifetimeSeconds()=" + getArchiveLifetimeSeconds() + ", getSectionName()="
191 + getSectionName() + "]";