ccfbe98fd60994a3528cd290b5f8c19141bfe667
[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         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}";
60
61     private final ConfigurationFileRepresentation configuration;
62
63     public EsConfig(ConfigurationFileRepresentation configuration) {
64
65         this.configuration = configuration;
66         this.configuration.addSection(SECTION_MARKER_ES);
67         defaults();
68     }
69
70     /*
71      * Setter
72      */
73
74     public void setNode(String nodeName) {
75         configuration.setProperty(SECTION_MARKER_ES, PROPERTY_KEY_NODE, nodeName);
76     }
77
78     /*
79      * Getter
80      */
81
82     public String getNode() {
83         return configuration.getProperty(SECTION_MARKER_ES, PROPERTY_KEY_NODE);
84     }
85
86     public HostInfo[] getHosts() {
87         String dbHosts = configuration.getProperty(SECTION_MARKER_ES, PROPERTY_KEY_DBHOSTS);
88         return parseHosts(dbHosts);
89     }
90     public void setHosts(HostInfo[] hosts) {
91         this.configuration.setProperty(SECTION_MARKER_ES, PROPERTY_KEY_DBHOSTS, printHosts(hosts));
92     }
93     @Override
94     public String getCluster() {
95         return configuration.getProperty(SECTION_MARKER_ES, PROPERTY_KEY_ARCHIVE_INTERVAL);
96     }
97
98     public void setCluster(String cluster) {
99         configuration.setProperty(SECTION_MARKER_ES, PROPERTY_KEY_CLUSTER, cluster);
100     }
101
102     public boolean hasBasicAuthCredentials() {
103         return this.getBasicAuthUsername()!=null && this.getBasicAuthPassword()!=null &&
104                         !this.getBasicAuthUsername().isEmpty() && !this.getBasicAuthPassword().isEmpty() ;
105     }
106     public String getBasicAuthUsername() {
107         return this.configuration.getProperty(SECTION_MARKER_ES, PROPERTY_KEY_AUTH_USERNAME);
108     }
109     public String getBasicAuthPassword() {
110         return this.configuration.getProperty(SECTION_MARKER_ES, PROPERTY_KEY_AUTH_PASSWORD);
111     }
112     @Override
113     public long getArchiveCheckIntervalSeconds() {
114         return configuration.getPropertyLong(SECTION_MARKER_ES, PROPERTY_KEY_ARCHIVE_INTERVAL).orElse(0L);
115     }
116
117     public void setArchiveCheckIntervalSeconds(long seconds) {
118         configuration.setProperty(SECTION_MARKER_ES, PROPERTY_KEY_ARCHIVE_INTERVAL, seconds);
119     }
120
121     @Override
122     public long getArchiveLifetimeSeconds() {
123         return configuration.getPropertyLong(SECTION_MARKER_ES, PROPERTY_KEY_ARCHIVE_LIMIT).orElse(0L);
124     }
125
126     public void setArchiveLimit(long seconds) {
127         configuration.setProperty(SECTION_MARKER_ES, PROPERTY_KEY_ARCHIVE_LIMIT, seconds);
128     }
129
130     @Override
131     public String getSectionName() {
132         return SECTION_MARKER_ES;
133     }
134
135     @Override
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);
147     }
148
149     @Override
150     public void unregisterConfigChangedListener(IConfigChangedListener archiveCleanService) {
151         configuration.unregisterConfigChangedListener(archiveCleanService);
152     }
153
154     @Override
155     public void registerConfigChangedListener(IConfigChangedListener archiveCleanService) {
156         configuration.registerConfigChangedListener(archiveCleanService);
157     }
158
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) {
165                 sb.append(",");
166             }
167         }
168         return sb.toString();
169     }
170
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) {
177                 try {
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());
182                 }
183             }
184         }
185         HostInfo[] a = new HostInfo[infos.size()];
186         return infos.toArray(a);
187     }
188
189     @Override
190     public String toString() {
191         return "EsConfig [getNode()=" + getNode() + ", getHosts()=" + Arrays.toString(getHosts()) + ", getCluster()="
192                 + getCluster() + ", getArchiveCheckIntervalSeconds()=" + getArchiveCheckIntervalSeconds()
193                 + ", getArchiveLifetimeSeconds()=" + getArchiveLifetimeSeconds() + ", getSectionName()="
194                 + getSectionName() + "]";
195     }
196
197 }