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