Merge "Fix Issues - Fix certificate issue for authenticated topic - Fix consumer...
[dcaegen2/services.git] / components / slice-analysis-ms / src / main / java / org / onap / slice / analysis / ms / models / Configuration.java
1 /*******************************************************************************
2  *  ============LICENSE_START=======================================================
3  *  slice-analysis-ms
4  *  ================================================================================
5  *   Copyright (C) 2020 Wipro Limited.
6  *   ==============================================================================
7  *     Licensed under the Apache License, Version 2.0 (the "License");
8  *     you may not use this file except in compliance with the License.
9  *     You may obtain a copy of the License at
10  *
11  *          http://www.apache.org/licenses/LICENSE-2.0
12  *
13  *     Unless required by applicable law or agreed to in writing, software
14  *     distributed under the License is distributed on an "AS IS" BASIS,
15  *     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  *     See the License for the specific language governing permissions and
17  *     limitations under the License.
18  *     ============LICENSE_END=========================================================
19  *
20  *******************************************************************************/
21
22 package org.onap.slice.analysis.ms.models;
23
24 import java.lang.reflect.Type;
25 import java.util.List;
26 import java.util.Map;
27
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 import com.google.gson.Gson;
32 import com.google.gson.JsonArray;
33 import com.google.gson.JsonObject;
34 import com.google.gson.reflect.TypeToken;
35
36 /** 
37  * Model class for the application Configuration
38  */
39 public class Configuration {
40         private static Logger log = LoggerFactory.getLogger(Configuration.class);
41
42         private static Configuration instance = null;
43         private String pgHost;
44         private int pgPort;
45         private String pgUsername;
46         private String pgPassword;
47         private List<String> dmaapServers;
48         private String configDbService;
49         private String cg;
50         private String cid;
51         private int pollingInterval;
52         private int pollingTimeout;
53         private String aafUsername;
54         private String aafPassword;
55         private Map<String, Object> streamsSubscribes;
56         private Map<String, Object> streamsPublishes;
57         private int samples;
58         private int minPercentageChange;
59         private long initialDelaySeconds;
60         /**
61          * Check if topic is secure.
62          */
63          public boolean isSecured() {
64                  return (aafUsername != null);
65
66          }
67
68          public String getAafUsername() {
69                  return aafUsername;
70          }
71
72          public void setAafUsername(String aafUsername) {
73                  this.aafUsername = aafUsername;
74          }
75
76          public String getAafPassword() {
77                  return aafPassword;
78          }
79
80          public void setAafPassword(String aafPassword) {
81                  this.aafPassword = aafPassword;
82          }
83
84          public Map<String, Object> getStreamsSubscribes() {
85                  return streamsSubscribes;
86          }
87
88          public void setStreamsSubscribes(Map<String, Object> streamsSubscribes) {
89                  this.streamsSubscribes = streamsSubscribes;
90          }
91
92          public Map<String, Object> getStreamsPublishes() {
93                  return streamsPublishes;
94          }
95
96          public void setStreamsPublishes(Map<String, Object> streamsPublishes) {
97                  this.streamsPublishes = streamsPublishes;
98          }
99
100          protected Configuration() {
101
102          }
103
104          /**
105           * Get instance of class.
106           */
107          public static Configuration getInstance() {
108                  if (instance == null) {
109                          instance = new Configuration();
110                  }
111                  return instance;
112          }
113
114          public String getCg() {
115                  return cg;
116          }
117
118          public void setCg(String cg) {
119                  this.cg = cg;
120          }
121
122          public String getCid() {
123                  return cid;
124          }
125
126          public void setCid(String cid) {
127                  this.cid = cid;
128          }
129
130          public int getPollingInterval() {
131                  return pollingInterval;
132          }
133
134          public void setPollingInterval(int pollingInterval) {
135                  this.pollingInterval = pollingInterval;
136          }
137
138          public int getPollingTimeout() {
139                  return pollingTimeout;
140          }
141
142          public void setPollingTimeout(int pollingTimeout) {
143                  this.pollingTimeout = pollingTimeout;
144          }
145
146          public String getPgHost() {
147                  return pgHost;
148          }
149
150          public void setPgHost(String pgHost) {
151                  this.pgHost = pgHost;
152          }
153
154          public int getPgPort() {
155                  return pgPort;
156          }
157
158          public void setPgPort(int pgPort) {
159                  this.pgPort = pgPort;
160          }
161
162          public String getPgUsername() {
163                  return pgUsername;
164          }
165
166          public void setPgUsername(String pgUsername) {
167                  this.pgUsername = pgUsername;
168          }
169
170          public String getPgPassword() {
171                  return pgPassword;
172          }
173
174          public void setPgPassword(String pgPassword) {
175                  this.pgPassword = pgPassword;
176          }
177
178          public List<String> getDmaapServers() {
179                  return dmaapServers;
180          }
181
182          public void setDmaapServers(List<String> dmaapServers) {
183                  this.dmaapServers = dmaapServers;
184          }
185
186          public String getConfigDbService() {
187                  return configDbService;
188          }
189
190          public void setConfigDbService(String configDbService) {
191                  this.configDbService = configDbService;
192          }
193
194          public int getSamples() {
195                  return samples;
196          }
197
198          public void setSamples(int samples) {
199                  this.samples = samples;
200          }
201
202          public int getMinPercentageChange() {
203                  return minPercentageChange;
204          }
205
206          public void setMinPercentageChange(int minPercentageChange) {
207                  this.minPercentageChange = minPercentageChange;
208          }
209
210          public long getInitialDelaySeconds() {
211                  return initialDelaySeconds;
212          }
213
214          public void setInitialDelaySeconds(long initialDelaySeconds) {
215                  this.initialDelaySeconds = initialDelaySeconds;
216          }
217
218          @Override
219          public String toString() {
220                  return "Configuration [pgHost=" + pgHost + ", pgPort=" + pgPort + ", pgUsername=" + pgUsername + ", pgPassword="
221                                  + pgPassword + ", dmaapServers=" + dmaapServers + ", configDbService=" + configDbService + ", cg=" + cg
222                                  + ", cid=" + cid + ", pollingInterval=" + pollingInterval + ", pollingTimeout=" + pollingTimeout
223                                  + ", aafUsername=" + aafUsername + ", aafPassword=" + aafPassword + ", streamsSubscribes="
224                                  + streamsSubscribes + ", streamsPublishes=" + streamsPublishes + ", samples=" + samples
225                                  + ", minPercentageChange=" + minPercentageChange + ", initialDelaySeconds=" + initialDelaySeconds + "]";
226          }
227
228          /**
229           * updates application configuration.
230           */
231          public void updateConfigurationFromJsonObject(JsonObject jsonObject) {
232
233                  log.info("Updating configuration from CBS");
234
235                  Type mapType = new TypeToken<Map<String, Object>>() {
236                  }.getType();
237
238                  JsonObject subscribes = jsonObject.getAsJsonObject("streams_subscribes");
239                  streamsSubscribes = new Gson().fromJson(subscribes, mapType);
240
241                  JsonObject publishes = jsonObject.getAsJsonObject("streams_publishes");
242                  streamsPublishes = new Gson().fromJson(publishes, mapType);
243
244                  pgPort = jsonObject.get("postgres.port").getAsInt();
245                  pollingInterval = jsonObject.get("sliceanalysisms.pollingInterval").getAsInt();
246                  pgPassword = jsonObject.get("postgres.password").getAsString();
247                  pgUsername = jsonObject.get("postgres.username").getAsString();
248                  pgHost = jsonObject.get("postgres.host").getAsString();
249
250                  JsonArray servers = jsonObject.getAsJsonArray("sliceanalysisms.dmaap.server");
251                  Type listType = new TypeToken<List<String>>() {}.getType();
252                  dmaapServers = new Gson().fromJson(servers, listType);
253
254                  cg = jsonObject.get("sliceanalysisms.cg").getAsString();
255                  cid = jsonObject.get("sliceanalysisms.cid").getAsString();
256                  configDbService = jsonObject.get("sliceanalysisms.configDb.service").getAsString();
257
258                  pollingTimeout = jsonObject.get("sliceanalysisms.pollingTimeout").getAsInt();
259                  samples = jsonObject.get("sliceanalysisms.samples").getAsInt();
260                  minPercentageChange = jsonObject.get("sliceanalysisms.minPercentageChange").getAsInt();
261                  initialDelaySeconds = jsonObject.get("sliceanalysisms.initialDelaySeconds").getAsLong();
262
263                  aafUsername = jsonObject.get("aafUsername").getAsString();
264                  aafPassword = jsonObject.get("aafPassword").getAsString();
265                  
266                  log.info("configuration from CBS {}", this);
267          }
268 }