0965c3dd67f9e7ce21d0a17405b09030332447a4
[ccsdk/apps.git] / sdnr / wireless-transport / code-Carbon-SR1 / apps / devicemanager / impl / src / main / java / org / opendaylight / mwtn / config / impl / EsConfig.java
1 package org.opendaylight.mwtn.config.impl;
2
3 import org.opendaylight.mwtn.base.internalTypes.Environment;
4 import org.opendaylight.mwtn.base.internalTypes.IniConfigurationFile;
5 import org.opendaylight.mwtn.base.internalTypes.IniConfigurationFile.ConfigurationException;
6 import org.opendaylight.mwtn.config.impl.HtDevicemanagerConfiguration.ISubConfigHandler;
7
8 public class EsConfig extends BaseSubConfig {
9
10         public static final String SECTION_MARKER_ES = "es";
11
12         public static final String ESDATATYPENAME = "database";
13
14         private static final String EMPTY = "empty";
15
16         private static final String PROPERTY_KEY_CLUSTER = "esCluster";
17
18         private static final String DEFAULT_VALUE_CLUSTER = "";
19
20         private static EsConfig esConfig;
21
22         private String cluster;
23         private String host;
24         private String node;
25         private String index;
26
27         public static String getESDATATYPENAME() {
28                 return ESDATATYPENAME;
29         }
30
31         public String getCluster() {
32                 return cluster;
33         }
34
35         public void setCluster(String cluster) {
36                 this.cluster = cluster;
37         }
38
39         public String getHost() {
40                 return host;
41         }
42
43         public void setHost(String host) {
44                 this.host = host;
45         }
46
47         public String getNode() {
48                 return node;
49         }
50
51         public void setNode(String node) {
52                 this.node = node;
53         }
54
55         public String getIndex() {
56                 return index;
57         }
58
59         public void setIndex(String index) {
60                 this.index = index;
61         }
62
63         @Override
64         public String toString() {
65                 return "EsConfig [cluster=" + cluster + ", host=" + host + ", node=" + node + ", index=" + index + "]";
66         }
67
68         public EsConfig(IniConfigurationFile config, ISubConfigHandler configHandler) throws ConfigurationException {
69                 this(config, configHandler, true);
70         }
71
72         public EsConfig(IniConfigurationFile config, ISubConfigHandler configHandler, boolean save)
73                         throws ConfigurationException {
74
75                 super(config, configHandler, SECTION_MARKER_ES);
76                 String clustername = Environment.getVar("$HOSTNAME");
77
78                 String c = this.getString(PROPERTY_KEY_CLUSTER, clustername);
79                 if (c != null && c.startsWith("$"))
80                         c = Environment.getVar(c);
81                 this.cluster = c;
82                 this.node = String.format("%s%s", this.cluster, "n1");
83                 this.host = "localhost";
84
85                 if (save) {
86                         config.setProperty(SECTION_MARKER_ES + "." + PROPERTY_KEY_CLUSTER, this.cluster);
87                         this.save();
88                 }
89         }
90
91         private EsConfig() {
92                 super();
93                 this.host = EMPTY;
94                 this.node = EMPTY;
95                 this.index = EMPTY;
96                 this.cluster = DEFAULT_VALUE_CLUSTER;
97         }
98
99         @Override
100         public boolean equals(Object obj) {
101                 if (obj instanceof EsConfig) {
102                         EsConfig cobj = (EsConfig) obj;
103                         if (!((cobj.cluster == null && this.cluster == null) || cobj.cluster.equals(this.cluster)))
104                                 return false;
105                         if (!((cobj.host == null && this.host == null) || cobj.host.equals(this.host)))
106                                 return false;
107                         if (!((cobj.node == null && this.node == null) || cobj.node.equals(this.node)))
108                                 return false;
109                         if (!((cobj.index == null && this.index == null) || cobj.index.equals(this.index)))
110                                 return false;
111                         return true;
112                 }
113                 return super.equals(obj);
114         }
115
116         public EsConfig cloneWithIndex(String index) {
117                 EsConfig c = new EsConfig();
118                 c.index = index;
119                 c.host = this.host;
120                 c.node = this.node;
121                 c.cluster = this.cluster;
122                 return c;
123         }
124
125         public static boolean isInstantiated() {
126                 return esConfig != null;
127         }
128
129         public static EsConfig getDefaultConfiguration() {
130                 return new EsConfig();
131         }
132
133         public static EsConfig getEs(IniConfigurationFile config, ISubConfigHandler configHandler) {
134                 if (esConfig == null)
135                         try {
136                                 esConfig = new EsConfig(config, configHandler);
137                         } catch (ConfigurationException e) {
138                                 esConfig = EsConfig.getDefaultConfiguration();
139                         }
140                 return esConfig;
141         }
142
143         public static EsConfig reload() {
144                 if (esConfig == null)
145                         return null;
146                 EsConfig tmpConfig;
147                 try {
148                         tmpConfig = new EsConfig(esConfig.getConfig(), esConfig.getConfigHandler(), false);
149                 } catch (ConfigurationException e) {
150                         tmpConfig = EsConfig.getDefaultConfiguration();
151                 }
152                 esConfig = tmpConfig;
153                 return esConfig;
154         }
155
156 }