YANG Model update for A1 Adapter
[ccsdk/features.git] / sdnr / wt / devicemanager / provider / src / main / java / org / onap / ccsdk / features / sdnr / wt / devicemanager / config / impl / 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.devicemanager.config.impl;
19
20 import org.onap.ccsdk.features.sdnr.wt.devicemanager.base.internalTypes.Environment;
21 import org.onap.ccsdk.features.sdnr.wt.devicemanager.base.internalTypes.IniConfigurationFile;
22 import org.onap.ccsdk.features.sdnr.wt.devicemanager.base.internalTypes.IniConfigurationFile.ConfigurationException;
23 import org.onap.ccsdk.features.sdnr.wt.devicemanager.config.BaseSubConfig;
24 import org.onap.ccsdk.features.sdnr.wt.devicemanager.config.ISubConfigHandler;
25
26 public class EsConfig extends BaseSubConfig {
27
28     public static final String SECTION_MARKER_ES = "es";
29     public static final String ESDATATYPENAME = "database";
30     private static final String EMPTY = "empty";
31     private static final String PROPERTY_KEY_CLUSTER = "esCluster";
32     private static final String PROPERTY_KEY_ARCHIVE_INTERVAL = "esArchiveCheckIntervalSeconds";
33     private static final String PROPERTY_KEY_ARCHIVE_LIMIT = "esArchiveLifetimeSeconds";
34
35     private static final String DEFAULT_VALUE_CLUSTER = "";
36     /**
37      * check db data in this interval [in seconds]
38      * 0 deactivated
39      */
40     private static final long DEFAULT_ARCHIVE_INTERVAL_SEC = 0;
41     /**
42      * keep data for this time [in seconds]
43      * 30 days
44      */
45     private static final long DEFAULT_ARCHIVE_LIMIT_SEC = 60 * 60 * 24 * 30;
46
47     private static EsConfig esConfig;
48
49     private String cluster;
50     private String host;
51     private String node;
52     private String index;
53     private long archiveCheckIntervalSeconds;
54     private long archiveLifetimeSeconds;
55
56     private EsConfig() {
57         super();
58         this.host = EMPTY;
59         this.node = EMPTY;
60         this.index = EMPTY;
61         this.cluster = DEFAULT_VALUE_CLUSTER;
62         this.archiveCheckIntervalSeconds = DEFAULT_ARCHIVE_INTERVAL_SEC;
63         this.archiveLifetimeSeconds = DEFAULT_ARCHIVE_LIMIT_SEC;
64     }
65
66     public EsConfig cloneWithIndex(String _index) {
67         EsConfig c = new EsConfig();
68         c.index = _index;
69         c.host = this.host;
70         c.node = this.node;
71         c.cluster = this.cluster;
72         c.archiveCheckIntervalSeconds = this.archiveCheckIntervalSeconds;
73         c.archiveLifetimeSeconds = this.archiveLifetimeSeconds;
74         return c;
75     }
76
77     public static String getESDATATYPENAME() {
78         return ESDATATYPENAME;
79     }
80
81     public String getCluster() {
82         return cluster;
83     }
84
85     public void setCluster(String cluster) {
86         this.cluster = cluster;
87     }
88
89     public String getHost() {
90         return host;
91     }
92
93     public void setHost(String host) {
94         this.host = host;
95     }
96
97     public String getNode() {
98         return node;
99     }
100
101     public void setNode(String node) {
102         this.node = node;
103     }
104
105     public String getIndex() {
106         return index;
107     }
108
109     public void setIndex(String index) {
110         this.index = index;
111     }
112
113     public long getArchiveCheckIntervalSeconds() {
114         return this.archiveCheckIntervalSeconds;
115     }
116
117     public void setArchiveCheckIntervalSeconds(long x) {
118         this.archiveCheckIntervalSeconds = x;
119     }
120
121     public long getArchiveLifetimeSeconds() {
122         return this.archiveLifetimeSeconds;
123     }
124
125     public void setArchiveLimit(long x) {
126         this.archiveLifetimeSeconds = x;
127     }
128
129     @Override
130     public String toString() {
131         return "EsConfig [cluster=" + cluster + ", host=" + host + ", node=" + node + ", index=" + index + "]";
132     }
133
134     public EsConfig(IniConfigurationFile config, ISubConfigHandler configHandler) throws ConfigurationException {
135         this(config, configHandler, true);
136     }
137
138     public EsConfig(IniConfigurationFile config, ISubConfigHandler configHandler, boolean save)
139             throws ConfigurationException {
140
141         super(config, configHandler, SECTION_MARKER_ES);
142         String clustername = Environment.getVar("$HOSTNAME");
143
144         String c = this.getString(PROPERTY_KEY_CLUSTER, clustername);
145         if (c != null && c.startsWith("$")) {
146             c = Environment.getVar(c);
147         }
148         this.cluster = c;
149         this.node = String.format("%s%s", this.cluster, "n1");
150         this.host = "localhost";
151         this.archiveCheckIntervalSeconds = this.getLong(PROPERTY_KEY_ARCHIVE_INTERVAL, DEFAULT_ARCHIVE_INTERVAL_SEC);
152         this.archiveLifetimeSeconds = this.getLong(PROPERTY_KEY_ARCHIVE_LIMIT, DEFAULT_ARCHIVE_LIMIT_SEC);
153
154         if (save) {
155             config.setProperty(SECTION_MARKER_ES + "." + PROPERTY_KEY_CLUSTER, this.cluster);
156             config.setProperty(SECTION_MARKER_ES + "." + PROPERTY_KEY_ARCHIVE_INTERVAL, this.archiveCheckIntervalSeconds);
157             config.setProperty(SECTION_MARKER_ES + "." + PROPERTY_KEY_ARCHIVE_LIMIT, this.archiveLifetimeSeconds);
158             this.save();
159         }
160     }
161
162     @Override
163     public int hashCode() {
164         final int prime = 31;
165         int result = 1;
166         result = prime * result + (cluster == null ? 0 : cluster.hashCode());
167         result = prime * result + (host == null ? 0 : host.hashCode());
168         result = prime * result + (index == null ? 0 : index.hashCode());
169         result = prime * result + (node == null ? 0 : node.hashCode());
170         return result;
171     }
172
173     @Override
174     public boolean equals(Object obj) {
175         if (this == obj) {
176             return true;
177         }
178         if (obj == null) {
179             return false;
180         }
181         if (getClass() != obj.getClass()) {
182             return false;
183         }
184         EsConfig other = (EsConfig) obj;
185         if (cluster == null) {
186             if (other.cluster != null) {
187                 return false;
188             }
189         } else if (!cluster.equals(other.cluster)) {
190             return false;
191         }
192         if (host == null) {
193             if (other.host != null) {
194                 return false;
195             }
196         } else if (!host.equals(other.host)) {
197             return false;
198         }
199         if (index == null) {
200             if (other.index != null) {
201                 return false;
202             }
203         } else if (!index.equals(other.index)) {
204             return false;
205         }
206         if (node == null) {
207             if (other.node != null) {
208                 return false;
209             }
210         } else if (!node.equals(other.node)) {
211             return false;
212         }
213         if (archiveCheckIntervalSeconds != other.archiveCheckIntervalSeconds) {
214             return false;
215         }
216         if (archiveLifetimeSeconds != other.archiveLifetimeSeconds) {
217             return false;
218         }
219         return true;
220     }
221
222     @Override
223     public void save() {
224         this.getConfig().setProperty(SECTION_MARKER_ES + "." + PROPERTY_KEY_CLUSTER, this.cluster);
225         super.save();
226     }
227
228     public static boolean isInstantiated() {
229         return esConfig != null;
230     }
231
232     public static EsConfig getDefaultConfiguration() {
233         return new EsConfig();
234     }
235
236     public static EsConfig getEs(IniConfigurationFile config, ISubConfigHandler configHandler) {
237         if (esConfig == null) {
238             try {
239                 esConfig = new EsConfig(config, configHandler);
240             } catch (ConfigurationException e) {
241                 esConfig = EsConfig.getDefaultConfiguration();
242             }
243         }
244         return esConfig;
245     }
246
247     public static EsConfig reload() {
248         if (esConfig == null) {
249             return null;
250         }
251         EsConfig tmpConfig;
252         try {
253             tmpConfig = new EsConfig(esConfig.getConfig(), esConfig.getConfigHandler(), false);
254         } catch (ConfigurationException e) {
255             tmpConfig = EsConfig.getDefaultConfiguration();
256         }
257         esConfig = tmpConfig;
258         return esConfig;
259     }
260
261     public static void clear() {
262         esConfig = null;
263     }
264
265 }