ORAN A1 Adapter YANG Model Update
[ccsdk/features.git] / sdnr / wt / data-provider / database / src / main / java / org / onap / ccsdk / features / sdnr / wt / database / config / EsConfig.java
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.database.config;
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.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;
32
33 public class EsConfig implements Configuration {
34
35         private static final Logger LOG = LoggerFactory.getLogger(EsConfig.class);
36
37         public static final String SECTION_MARKER_ES = "es";
38
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";
44
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";
52
53         private final ConfigurationFileRepresentation configuration;
54
55         public EsConfig(ConfigurationFileRepresentation configuration) {
56
57                 this.configuration = configuration;
58                 this.configuration.addSection(SECTION_MARKER_ES);
59                 defaults();
60         }
61
62         /*
63          * Setter
64          */
65
66         public void setNode(String nodeName) {
67                 configuration.setProperty(SECTION_MARKER_ES, PROPERTY_KEY_NODE, nodeName);
68         }
69
70         /*
71          * Getter
72          */
73
74         public String getNode() {
75                 return configuration.getProperty(SECTION_MARKER_ES, PROPERTY_KEY_NODE);
76         }
77
78         public HostInfo[] getHosts() {
79                 String dbHosts = configuration.getProperty(SECTION_MARKER_ES, PROPERTY_KEY_DBHOSTS);
80                 return parseHosts(dbHosts);
81         }
82         public void setHosts(HostInfo[] hosts) {
83                 this.configuration.setProperty(SECTION_MARKER_ES, PROPERTY_KEY_DBHOSTS, printHosts(hosts));
84         }
85         public String getCluster() {
86                 return configuration.getProperty(SECTION_MARKER_ES, PROPERTY_KEY_ARCHIVE_INTERVAL);
87         }
88
89         public void setCluster(String cluster) {
90                 configuration.setProperty(SECTION_MARKER_ES, PROPERTY_KEY_CLUSTER, cluster);
91         }
92
93         public long getArchiveCheckIntervalSeconds() {
94                 return configuration.getPropertyLong(SECTION_MARKER_ES, PROPERTY_KEY_ARCHIVE_INTERVAL).orElse(0L);
95         }
96
97         public void setArchiveCheckIntervalSeconds(long seconds) {
98                 configuration.setProperty(SECTION_MARKER_ES, PROPERTY_KEY_ARCHIVE_INTERVAL, seconds);
99         }
100
101         public long getArchiveLifetimeSeconds() {
102                 return configuration.getPropertyLong(SECTION_MARKER_ES, PROPERTY_KEY_ARCHIVE_LIMIT).orElse(0L);
103         }
104
105         public void setArchiveLimit(long seconds) {
106                 configuration.setProperty(SECTION_MARKER_ES, PROPERTY_KEY_ARCHIVE_LIMIT, seconds);
107         }
108
109         @Override
110         public String getSectionName() {
111                 return SECTION_MARKER_ES;
112         }
113
114         @Override
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);
124         }
125
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) {
132                                 sb.append(",");
133                         }
134                 }
135                 return sb.toString();
136         }
137
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) {
144                                 try {
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());
149                                 }
150                         }
151                 }
152                 HostInfo[] a = new HostInfo[infos.size()];
153                 return infos.toArray(a);
154         }
155
156         @Override
157         public String toString() {
158                 return "EsConfig [getNode()=" + getNode() + ", getHosts()=" + Arrays.toString(getHosts()) + ", getCluster()="
159                                 + getCluster() + ", getArchiveCheckIntervalSeconds()=" + getArchiveCheckIntervalSeconds()
160                                 + ", getArchiveLifetimeSeconds()=" + getArchiveLifetimeSeconds() + ", getSectionName()="
161                                 + getSectionName() + "]";
162         }
163
164 }