fc9ae5085488991b197f5f077aa12c601ff52bd3
[ccsdk/features.git] /
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
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
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
15  * the License.
16  * ============LICENSE_END==========================================================================
17  ******************************************************************************/
18 package org.onap.ccsdk.features.sdnr.wt.dataprovider.impl;
19
20 import java.net.MalformedURLException;
21 import java.net.URL;
22 import java.util.ArrayList;
23 import java.util.Arrays;
24 import java.util.List;
25
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;
34
35 public class EsConfig implements Configuration, IEsConfig {
36
37     private static final Logger LOG = LoggerFactory.getLogger(EsConfig.class);
38
39     public static final String SECTION_MARKER_ES = "es";
40
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";
48
49     
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
58     private final ConfigurationFileRepresentation configuration;
59
60     public EsConfig(ConfigurationFileRepresentation configuration) {
61
62         this.configuration = configuration;
63         this.configuration.addSection(SECTION_MARKER_ES);
64         defaults();
65     }
66
67     /*
68      * Setter
69      */
70
71     public void setNode(String nodeName) {
72         configuration.setProperty(SECTION_MARKER_ES, PROPERTY_KEY_NODE, nodeName);
73     }
74
75     /*
76      * Getter
77      */
78
79     public String getNode() {
80         return configuration.getProperty(SECTION_MARKER_ES, PROPERTY_KEY_NODE);
81     }
82
83     public HostInfo[] getHosts() {
84         String dbHosts = configuration.getProperty(SECTION_MARKER_ES, PROPERTY_KEY_DBHOSTS);
85         return parseHosts(dbHosts);
86     }
87     public void setHosts(HostInfo[] hosts) {
88         this.configuration.setProperty(SECTION_MARKER_ES, PROPERTY_KEY_DBHOSTS, printHosts(hosts));
89     }
90     @Override
91     public String getCluster() {
92         return configuration.getProperty(SECTION_MARKER_ES, PROPERTY_KEY_ARCHIVE_INTERVAL);
93     }
94
95     public void setCluster(String cluster) {
96         configuration.setProperty(SECTION_MARKER_ES, PROPERTY_KEY_CLUSTER, cluster);
97     }
98
99     public boolean hasBasicAuthCredentials() {
100         return this.getBasicAuthUsername()!=null && this.getBasicAuthPassword()!=null &&
101                         this.getBasicAuthUsername()!="" && this.getBasicAuthPassword()!="" ;
102     }
103     public String getBasicAuthUsername() {
104         return this.configuration.getProperty(SECTION_MARKER_ES, PROPERTY_KEY_AUTH_USERNAME);
105     }
106     public String getBasicAuthPassword() {
107         return this.configuration.getProperty(SECTION_MARKER_ES, PROPERTY_KEY_AUTH_PASSWORD);
108     }
109     @Override
110     public long getArchiveCheckIntervalSeconds() {
111         return configuration.getPropertyLong(SECTION_MARKER_ES, PROPERTY_KEY_ARCHIVE_INTERVAL).orElse(0L);
112     }
113
114     public void setArchiveCheckIntervalSeconds(long seconds) {
115         configuration.setProperty(SECTION_MARKER_ES, PROPERTY_KEY_ARCHIVE_INTERVAL, seconds);
116     }
117
118     @Override
119     public long getArchiveLifetimeSeconds() {
120         return configuration.getPropertyLong(SECTION_MARKER_ES, PROPERTY_KEY_ARCHIVE_LIMIT).orElse(0L);
121     }
122
123     public void setArchiveLimit(long seconds) {
124         configuration.setProperty(SECTION_MARKER_ES, PROPERTY_KEY_ARCHIVE_LIMIT, seconds);
125     }
126
127     @Override
128     public String getSectionName() {
129         return SECTION_MARKER_ES;
130     }
131
132     @Override
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, "");
144     }
145
146     @Override
147     public void unregisterConfigChangedListener(IConfigChangedListener archiveCleanService) {
148         configuration.unregisterConfigChangedListener(archiveCleanService);
149     }
150
151     @Override
152     public void registerConfigChangedListener(IConfigChangedListener archiveCleanService) {
153         configuration.registerConfigChangedListener(archiveCleanService);
154     }
155
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) {
162                 sb.append(",");
163             }
164         }
165         return sb.toString();
166     }
167
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) {
174                 try {
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());
179                 }
180             }
181         }
182         HostInfo[] a = new HostInfo[infos.size()];
183         return infos.toArray(a);
184     }
185
186     @Override
187     public String toString() {
188         return "EsConfig [getNode()=" + getNode() + ", getHosts()=" + Arrays.toString(getHosts()) + ", getCluster()="
189                 + getCluster() + ", getArchiveCheckIntervalSeconds()=" + getArchiveCheckIntervalSeconds()
190                 + ", getArchiveLifetimeSeconds()=" + getArchiveLifetimeSeconds() + ", getSectionName()="
191                 + getSectionName() + "]";
192     }
193
194 }