Update microservice to run as standalone MS
[dcaegen2/services/son-handler.git] / src / main / java / org / onap / dcaegen2 / services / sonhms / Configuration.java
1 /*******************************************************************************
2  *  ============LICENSE_START=======================================================
3  *  son-handler
4  *  ================================================================================
5  *   Copyright (C) 2019-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.dcaegen2.services.sonhms;
23
24 import com.google.gson.Gson;
25 import com.google.gson.JsonArray;
26 import com.google.gson.JsonObject;
27 import com.google.gson.reflect.TypeToken;
28
29 import java.lang.reflect.Type;
30 import java.util.List;
31 import java.util.Map;
32
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36
37 public class Configuration {
38
39     private static Logger log = LoggerFactory.getLogger(Configuration.class);
40
41     private static Configuration instance = null;
42     private String pgHost;
43     private int pgPort;
44     private String pgUsername;
45     private String pgPassword;
46     private List<String> dmaapServers;
47     private String configDbService;
48     private String oofService;
49     private String oofEndpoint;
50     private String cg;
51     private String cid;
52     private int pollingInterval;
53     private int pollingTimeout;
54     private int minCollision;
55     private int minConfusion;
56     private String sourceId;
57     private String callbackUrl;
58     private String pciOptimizer;
59     private String pciAnrOptimizer;
60     private int numSolutions;
61     private int bufferTime;
62     private int maximumClusters;
63     private String aafUsername;
64     private String aafPassword;
65     private Map<String, Object> streamsSubscribes;
66     private Map<String, Object> streamsPublishes;
67     private int badThreshold;
68     private int poorThreshold;
69     private int poorCountThreshold;
70     private int badCountThreshold;
71     private int oofTriggerCountTimer;
72     private int oofTriggerCountThreshold;
73     private int policyRespTimer;
74
75
76     public int getPoorCountThreshold() {
77         return poorCountThreshold;
78     }
79
80     public void setPoorCountThreshold(int poorCountThreshold) {
81         this.poorCountThreshold = poorCountThreshold;
82     }
83
84     public int getBadCountThreshold() {
85         return badCountThreshold;
86     }
87
88     public void setBadCountThreshold(int badCountThreshold) {
89         this.badCountThreshold = badCountThreshold;
90     }
91
92     public int getOofTriggerCountTimer() {
93         return oofTriggerCountTimer;
94     }
95
96     public void setOofTriggerCountTimer(int oofTriggerCountTimer) {
97         this.oofTriggerCountTimer = oofTriggerCountTimer;
98     }
99
100     public int getOofTriggerCountThreshold() {
101         return oofTriggerCountThreshold;
102     }
103
104     public void setOofTriggerCountThreshold(int oofTriggerCountThreshold) {
105         this.oofTriggerCountThreshold = oofTriggerCountThreshold;
106     }
107
108     public int getPolicyRespTimer() {
109         return policyRespTimer;
110     }
111
112     public void setPolicyRespTimer(int policyRespTimer) {
113         this.policyRespTimer = policyRespTimer;
114     }
115
116     public int getBadThreshold() {
117         return badThreshold;
118     }
119
120     public void setBadThreshold(int badThreshold) {
121         this.badThreshold = badThreshold;
122     }
123
124     public int getPoorThreshold() {
125         return poorThreshold;
126     }
127
128     public void setPoorThreshold(int poorThreshold) {
129         this.poorThreshold = poorThreshold;
130     }
131
132     /**
133      * Check if topic is secure.
134      */
135     public boolean isSecured() {
136         return (aafUsername != null);
137
138     }
139
140     public String getAafUsername() {
141         return aafUsername;
142     }
143
144     public void setAafUsername(String aafUsername) {
145         this.aafUsername = aafUsername;
146     }
147
148     public String getAafPassword() {
149         return aafPassword;
150     }
151
152     public void setAafPassword(String aafPassword) {
153         this.aafPassword = aafPassword;
154     }
155
156     public Map<String, Object> getStreamsSubscribes() {
157         return streamsSubscribes;
158     }
159
160     public void setStreamsSubscribes(Map<String, Object> streamsSubscribes) {
161         this.streamsSubscribes = streamsSubscribes;
162     }
163
164     public Map<String, Object> getStreamsPublishes() {
165         return streamsPublishes;
166     }
167
168     public void setStreamsPublishes(Map<String, Object> streamsPublishes) {
169         this.streamsPublishes = streamsPublishes;
170     }
171
172     public int getMaximumClusters() {
173         return maximumClusters;
174     }
175
176     public void setMaximumClusters(int maximumClusters) {
177         this.maximumClusters = maximumClusters;
178     }
179
180     protected Configuration() {
181
182     }
183
184     /**
185      * Get instance of class.
186      */
187     public static Configuration getInstance() {
188         if (instance == null) {
189             instance = new Configuration();
190         }
191         return instance;
192     }
193
194     public String getCg() {
195         return cg;
196     }
197
198     public void setCg(String cg) {
199         this.cg = cg;
200     }
201
202     public String getCid() {
203         return cid;
204     }
205
206     public void setCid(String cid) {
207         this.cid = cid;
208     }
209
210     public int getPollingInterval() {
211         return pollingInterval;
212     }
213
214     public void setPollingInterval(int pollingInterval) {
215         this.pollingInterval = pollingInterval;
216     }
217
218     public int getPollingTimeout() {
219         return pollingTimeout;
220     }
221
222     public void setPollingTimeout(int pollingTimeout) {
223         this.pollingTimeout = pollingTimeout;
224     }
225
226     public int getMinCollision() {
227         return minCollision;
228     }
229
230     public void setMinCollision(int minCollision) {
231         this.minCollision = minCollision;
232     }
233
234     public int getMinConfusion() {
235         return minConfusion;
236     }
237
238     public void setMinConfusion(int minConfusion) {
239         this.minConfusion = minConfusion;
240     }
241
242     public String getOofService() {
243         return oofService;
244     }
245
246     public void setOofService(String oofService) {
247         this.oofService = oofService;
248     }
249
250     public String getSourceId() {
251         return sourceId;
252     }
253
254     public void setSourceId(String sourceId) {
255         this.sourceId = sourceId;
256     }
257
258     public String getCallbackUrl() {
259         return callbackUrl;
260     }
261
262     public void setCallbackUrl(String callbackUrl) {
263         this.callbackUrl = callbackUrl;
264     }
265
266     public int getNumSolutions() {
267         return numSolutions;
268     }
269
270     public void setNumSolutions(int numSolutions) {
271         this.numSolutions = numSolutions;
272     }
273
274     public int getBufferTime() {
275         return bufferTime;
276     }
277
278     public void setBufferTime(int bufferTime) {
279         this.bufferTime = bufferTime;
280     }
281
282     public String getPgHost() {
283         return pgHost;
284     }
285
286     public void setPgHost(String pgHost) {
287         this.pgHost = pgHost;
288     }
289
290     public int getPgPort() {
291         return pgPort;
292     }
293
294     public void setPgPort(int pgPort) {
295         this.pgPort = pgPort;
296     }
297
298     public String getPgUsername() {
299         return pgUsername;
300     }
301
302     public void setPgUsername(String pgUsername) {
303         this.pgUsername = pgUsername;
304     }
305
306     public String getPgPassword() {
307         return pgPassword;
308     }
309
310     public void setPgPassword(String pgPassword) {
311         this.pgPassword = pgPassword;
312     }
313
314     public List<String> getDmaapServers() {
315         return dmaapServers;
316     }
317
318     public void setDmaapServers(List<String> dmaapServers) {
319         this.dmaapServers = dmaapServers;
320     }
321
322     public String getConfigDbService() {
323         return configDbService;
324     }
325
326     public void setConfigDbService(String configDbService) {
327         this.configDbService = configDbService;
328     }
329
330     public String getPciOptimizer() {
331         return pciOptimizer;
332     }
333
334     public void setPciOptimizer(String pciOptimizer) {
335         this.pciOptimizer = pciOptimizer;
336     }
337
338     public String getPciAnrOptimizer() {
339         return pciAnrOptimizer;
340     }
341
342     public void setPciAnrOptimizer(String pciAnrOptimizer) {
343         this.pciAnrOptimizer = pciAnrOptimizer;
344     }
345
346     public String getOofEndpoint() {
347         return oofEndpoint;
348     }
349
350     public void setOofEndpoint(String oofEndpoint) {
351         this.oofEndpoint = oofEndpoint;
352     }
353
354     @Override
355     public String toString() {
356         return "Configuration [pgHost=" + pgHost + ", pgPort=" + pgPort + ", pgUsername=" + pgUsername + ", pgPassword="
357                 + pgPassword + ", dmaapServers=" + dmaapServers + ", configDbService=" + configDbService
358                 + ", oofService=" + oofService + ", oofEndpoint=" + oofEndpoint + ", cg=" + cg + ", cid=" + cid
359                 + ", pollingInterval=" + pollingInterval + ", pollingTimeout=" + pollingTimeout + ", minCollision="
360                 + minCollision + ", minConfusion=" + minConfusion + ", sourceId=" + sourceId + ", callbackUrl="
361                 + callbackUrl + ", pciOptimizer=" + pciOptimizer + ", pciAnrOptimizer=" + pciAnrOptimizer
362                 + ", numSolutions=" + numSolutions + ", bufferTime=" + bufferTime + ", maximumClusters="
363                 + maximumClusters + ", aafUsername=" + aafUsername + ", aafPassword=" + aafPassword
364                 + ", streamsSubscribes=" + streamsSubscribes + ", streamsPublishes=" + streamsPublishes
365                 + ", badThreshold=" + badThreshold + ", poorThreshold=" + poorThreshold + ", poorCountThreshold="
366                 + poorCountThreshold + ", badCountThreshold=" + badCountThreshold + ", oofTriggerCountTimer="
367                 + oofTriggerCountTimer + ", oofTriggerCountThreshold=" + oofTriggerCountThreshold + ", policyRespTimer="
368                 + policyRespTimer + "]";
369     }
370
371     /**
372      * updates application configuration.
373      */
374     public void updateConfigurationFromJsonObject(JsonObject jsonObject) {
375
376         log.info("Updating configuration from CBS");
377
378         Type mapType = new TypeToken<Map<String, Object>>() {
379         }.getType();
380
381         JsonObject subscribes = jsonObject.getAsJsonObject("streams_subscribes");
382         streamsSubscribes = new Gson().fromJson(subscribes, mapType);
383
384         JsonObject publishes = jsonObject.getAsJsonObject("streams_publishes");
385         streamsPublishes = new Gson().fromJson(publishes, mapType);
386
387         pgPort = jsonObject.get("postgres.port").getAsInt();
388         pollingInterval = jsonObject.get("sonhandler.pollingInterval").getAsInt();
389         pgPassword = jsonObject.get("postgres.password").getAsString();
390         numSolutions = jsonObject.get("sonhandler.numSolutions").getAsInt();
391         minConfusion = jsonObject.get("sonhandler.minConfusion").getAsInt();
392         maximumClusters = jsonObject.get("sonhandler.maximumClusters").getAsInt();
393         minCollision = jsonObject.get("sonhandler.minCollision").getAsInt();
394         sourceId = jsonObject.get("sonhandler.sourceId").getAsString();
395         pgUsername = jsonObject.get("postgres.username").getAsString();
396         pgHost = jsonObject.get("postgres.host").getAsString();
397
398         JsonArray servers = jsonObject.getAsJsonArray("sonhandler.dmaap.server");
399         Type listType = new TypeToken<List<String>>() {
400         }.getType();
401         dmaapServers = new Gson().fromJson(servers, listType);
402
403         cg = jsonObject.get("sonhandler.cg").getAsString();
404         bufferTime = jsonObject.get("sonhandler.bufferTime").getAsInt();
405         cid = jsonObject.get("sonhandler.cid").getAsString();
406         configDbService = jsonObject.get("sonhandler.configDb.service").getAsString();
407         String namespace = jsonObject.get("sonhandler.namespace").getAsString();
408         callbackUrl = "http://" + System.getenv("HOSTNAME") + "." + namespace + ":8080/callbackUrl";
409
410         pciOptimizer = jsonObject.get("sonhandler.pciOptimizer").getAsString();
411         pciAnrOptimizer = jsonObject.get("sonhandler.pciAnrOptimizer").getAsString();
412
413         oofService = jsonObject.get("sonhandler.oof.service").getAsString();
414         oofEndpoint = jsonObject.get("sonhandler.oof.endpoint").getAsString();
415         pollingTimeout = jsonObject.get("sonhandler.pollingTimeout").getAsInt();
416
417         badThreshold = jsonObject.get("sonhandler.badThreshold").getAsInt();
418         poorThreshold = jsonObject.get("sonhandler.poorThreshold").getAsInt();
419
420         poorCountThreshold = jsonObject.get("sonhandler.poorCountThreshold").getAsInt();
421         badCountThreshold = jsonObject.get("sonhandler.badCountThreshold").getAsInt();
422         oofTriggerCountTimer = jsonObject.get("sonhandler.oofTriggerCountTimer").getAsInt();
423         oofTriggerCountThreshold = jsonObject.get("sonhandler.oofTriggerCountThreshold").getAsInt();
424         policyRespTimer = jsonObject.get("sonhandler.policyRespTimer").getAsInt();
425
426
427         log.info("configuration from CBS {}", this);
428
429     }
430
431
432
433 }