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