958a0dfd34bf5fccafbecfdf31caf06f53b7fb0c
[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.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 DEFAULT_VALUE_CLUSTER = "";
33     private static EsConfig esConfig;
34
35     private String cluster;
36     private String host;
37     private String node;
38     private String index;
39
40     private EsConfig() {
41         super();
42         this.host = EMPTY;
43         this.node = EMPTY;
44         this.index = EMPTY;
45         this.cluster = DEFAULT_VALUE_CLUSTER;
46     }
47
48     public EsConfig cloneWithIndex(String _index) {
49         EsConfig c = new EsConfig();
50         c.index = _index;
51         c.host = this.host;
52         c.node = this.node;
53         c.cluster = this.cluster;
54         return c;
55     }
56
57     public static String getESDATATYPENAME() {
58         return ESDATATYPENAME;
59     }
60
61     public String getCluster() {
62         return cluster;
63     }
64
65     public void setCluster(String cluster) {
66         this.cluster = cluster;
67     }
68
69     public String getHost() {
70         return host;
71     }
72
73     public void setHost(String host) {
74         this.host = host;
75     }
76
77     public String getNode() {
78         return node;
79     }
80
81     public void setNode(String node) {
82         this.node = node;
83     }
84
85     public String getIndex() {
86         return index;
87     }
88
89     public void setIndex(String index) {
90         this.index = index;
91     }
92
93     @Override
94     public String toString() {
95         return "EsConfig [cluster=" + cluster + ", host=" + host + ", node=" + node + ", index=" + index + "]";
96     }
97
98     public EsConfig(IniConfigurationFile config, ISubConfigHandler configHandler) throws ConfigurationException {
99         this(config, configHandler, true);
100     }
101
102     public EsConfig(IniConfigurationFile config, ISubConfigHandler configHandler, boolean save)
103             throws ConfigurationException {
104
105         super(config, configHandler, SECTION_MARKER_ES);
106         String clustername = Environment.getVar("$HOSTNAME");
107
108         String c = this.getString(PROPERTY_KEY_CLUSTER, clustername);
109         if (c != null && c.startsWith("$")) {
110             c = Environment.getVar(c);
111         }
112         this.cluster = c;
113         this.node = String.format("%s%s", this.cluster, "n1");
114         this.host = "localhost";
115
116         if (save) {
117             config.setProperty(SECTION_MARKER_ES + "." + PROPERTY_KEY_CLUSTER, this.cluster);
118             this.save();
119         }
120     }
121
122     @Override
123     public int hashCode() {
124         final int prime = 31;
125         int result = 1;
126         result = prime * result + (cluster == null ? 0 : cluster.hashCode());
127         result = prime * result + (host == null ? 0 : host.hashCode());
128         result = prime * result + (index == null ? 0 : index.hashCode());
129         result = prime * result + (node == null ? 0 : node.hashCode());
130         return result;
131     }
132
133     @Override
134     public boolean equals(Object obj) {
135         if (this == obj) {
136             return true;
137         }
138         if (obj == null) {
139             return false;
140         }
141         if (getClass() != obj.getClass()) {
142             return false;
143         }
144         EsConfig other = (EsConfig) obj;
145         if (cluster == null) {
146             if (other.cluster != null) {
147                 return false;
148             }
149         } else if (!cluster.equals(other.cluster)) {
150             return false;
151         }
152         if (host == null) {
153             if (other.host != null) {
154                 return false;
155             }
156         } else if (!host.equals(other.host)) {
157             return false;
158         }
159         if (index == null) {
160             if (other.index != null) {
161                 return false;
162             }
163         } else if (!index.equals(other.index)) {
164             return false;
165         }
166         if (node == null) {
167             if (other.node != null) {
168                 return false;
169             }
170         } else if (!node.equals(other.node)) {
171             return false;
172         }
173         return true;
174     }
175
176     @Override
177     public void save()
178     {
179         this.getConfig().setProperty(SECTION_MARKER_ES + "." + PROPERTY_KEY_CLUSTER, this.cluster);
180         super.save();
181     }
182     public static boolean isInstantiated() {
183         return esConfig != null;
184     }
185
186     public static EsConfig getDefaultConfiguration() {
187         return new EsConfig();
188     }
189
190     public static EsConfig getEs(IniConfigurationFile config, ISubConfigHandler configHandler) {
191         if (esConfig == null) {
192             try {
193                 esConfig = new EsConfig(config, configHandler);
194             } catch (ConfigurationException e) {
195                 esConfig = EsConfig.getDefaultConfiguration();
196             }
197         }
198         return esConfig;
199     }
200
201     public static EsConfig reload() {
202         if (esConfig == null) {
203             return null;
204         }
205         EsConfig tmpConfig;
206         try {
207             tmpConfig = new EsConfig(esConfig.getConfig(), esConfig.getConfigHandler(), false);
208         } catch (ConfigurationException e) {
209             tmpConfig = EsConfig.getDefaultConfiguration();
210         }
211         esConfig = tmpConfig;
212         return esConfig;
213     }
214
215     public static void clear() {
216         esConfig=null;
217     }
218
219 }