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.database.config;
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.database.config.HostInfo;
29 import org.onap.ccsdk.features.sdnr.wt.common.database.config.HostInfo.Protocol;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
33 public class EsConfig implements Configuration {
35 private static final Logger LOG = LoggerFactory.getLogger(EsConfig.class);
37 public static final String SECTION_MARKER_ES = "es";
39 private static final String PROPERTY_KEY_DBHOSTS = "esHosts";
40 private static final String PROPERTY_KEY_ARCHIVE_LIMIT = "esArchiveLifetimeSeconds";
41 private static final String PROPERTY_KEY_CLUSTER = "esCluster";
42 private static final String PROPERTY_KEY_ARCHIVE_INTERVAL = "esArchiveCheckIntervalSeconds";
43 private static final String PROPERTY_KEY_NODE = "esNode";
45 private static String defaultHostinfo = printHosts(new HostInfo[] { new HostInfo("sdnrdb", 9200, Protocol.HTTP) });
46 private static final String DEFAULT_VALUE_CLUSTER = "";
47 /** check db data in this interval [in seconds] 0 deactivated */
48 private static final String DEFAULT_ARCHIVE_INTERVAL_SEC = "0";
49 /** keep data for this time [in seconds] 30 days */
50 private static final String DEFAULT_ARCHIVE_LIMIT_SEC = String.valueOf(60L * 60L * 24L * 30L);
51 private static final String DEFAULT_KEY_NODE = "elasticsearchnode";
53 private final ConfigurationFileRepresentation configuration;
55 public EsConfig(ConfigurationFileRepresentation configuration) {
57 this.configuration = configuration;
58 this.configuration.addSection(SECTION_MARKER_ES);
66 public void setNode(String nodeName) {
67 configuration.setProperty(SECTION_MARKER_ES, PROPERTY_KEY_NODE, nodeName);
74 public String getNode() {
75 return configuration.getProperty(SECTION_MARKER_ES, PROPERTY_KEY_NODE);
78 public HostInfo[] getHosts() {
79 String dbHosts = configuration.getProperty(SECTION_MARKER_ES, PROPERTY_KEY_DBHOSTS);
80 return parseHosts(dbHosts);
82 public void setHosts(HostInfo[] hosts) {
83 this.configuration.setProperty(SECTION_MARKER_ES, PROPERTY_KEY_DBHOSTS, printHosts(hosts));
85 public String getCluster() {
86 return configuration.getProperty(SECTION_MARKER_ES, PROPERTY_KEY_ARCHIVE_INTERVAL);
89 public void setCluster(String cluster) {
90 configuration.setProperty(SECTION_MARKER_ES, PROPERTY_KEY_CLUSTER, cluster);
93 public long getArchiveCheckIntervalSeconds() {
94 return configuration.getPropertyLong(SECTION_MARKER_ES, PROPERTY_KEY_ARCHIVE_INTERVAL).orElse(0L);
97 public void setArchiveCheckIntervalSeconds(long seconds) {
98 configuration.setProperty(SECTION_MARKER_ES, PROPERTY_KEY_ARCHIVE_INTERVAL, seconds);
101 public long getArchiveLifetimeSeconds() {
102 return configuration.getPropertyLong(SECTION_MARKER_ES, PROPERTY_KEY_ARCHIVE_LIMIT).orElse(0L);
105 public void setArchiveLimit(long seconds) {
106 configuration.setProperty(SECTION_MARKER_ES, PROPERTY_KEY_ARCHIVE_LIMIT, seconds);
110 public String getSectionName() {
111 return SECTION_MARKER_ES;
115 public void defaults() {
116 // Add default if not available
117 configuration.setPropertyIfNotAvailable(SECTION_MARKER_ES, PROPERTY_KEY_DBHOSTS, defaultHostinfo);
118 configuration.setPropertyIfNotAvailable(SECTION_MARKER_ES, PROPERTY_KEY_ARCHIVE_LIMIT,
119 DEFAULT_ARCHIVE_LIMIT_SEC);
120 configuration.setPropertyIfNotAvailable(SECTION_MARKER_ES, PROPERTY_KEY_CLUSTER, DEFAULT_VALUE_CLUSTER);
121 configuration.setPropertyIfNotAvailable(SECTION_MARKER_ES, PROPERTY_KEY_ARCHIVE_INTERVAL,
122 DEFAULT_ARCHIVE_INTERVAL_SEC);
123 configuration.setPropertyIfNotAvailable(SECTION_MARKER_ES, PROPERTY_KEY_NODE, DEFAULT_KEY_NODE);
126 /** @TODO Shift to own class **/
127 private static String printHosts(HostInfo[] h) {
128 StringBuilder sb = new StringBuilder();
129 for (int i = 0; i < h.length; i++) {
130 sb.append(h[i].toUrl());
131 if (i != h.length - 1) {
135 return sb.toString();
138 /** @TODO Shift to own class **/
139 private static HostInfo[] parseHosts(String string) {
140 List<HostInfo> infos = new ArrayList<HostInfo>();
141 String[] list = string.split(",");
142 if (list.length > 0) {
143 for (String item : list) {
145 URL url = new URL(item);
146 infos.add(new HostInfo(url.getHost(), url.getPort(), Protocol.getValueOf(url.getProtocol())));
147 } catch (MalformedURLException e) {
148 LOG.warn("problem parsing url {} : {}", item, e.getMessage());
152 HostInfo[] a = new HostInfo[infos.size()];
153 return infos.toArray(a);
157 public String toString() {
158 return "EsConfig [getNode()=" + getNode() + ", getHosts()=" + Arrays.toString(getHosts()) + ", getCluster()="
159 + getCluster() + ", getArchiveCheckIntervalSeconds()=" + getArchiveCheckIntervalSeconds()
160 + ", getArchiveLifetimeSeconds()=" + getArchiveLifetimeSeconds() + ", getSectionName()="
161 + getSectionName() + "]";