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