1 /*******************************************************************************
2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
20 *******************************************************************************/
22 package org.onap.slice.analysis.ms.models;
24 import java.lang.reflect.Type;
25 import java.util.List;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
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;
37 * Model class for the application Configuration
39 public class Configuration {
40 private static Logger log = LoggerFactory.getLogger(Configuration.class);
42 private static Configuration instance = null;
43 private String pgHost;
45 private String pgUsername;
46 private String pgPassword;
47 private List<String> dmaapServers;
48 private String configDbService;
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;
58 private int minPercentageChange;
59 private long initialDelaySeconds;
61 * Check if topic is secure.
63 public boolean isSecured() {
64 return (aafUsername != null);
68 public String getAafUsername() {
72 public void setAafUsername(String aafUsername) {
73 this.aafUsername = aafUsername;
76 public String getAafPassword() {
80 public void setAafPassword(String aafPassword) {
81 this.aafPassword = aafPassword;
84 public Map<String, Object> getStreamsSubscribes() {
85 return streamsSubscribes;
88 public void setStreamsSubscribes(Map<String, Object> streamsSubscribes) {
89 this.streamsSubscribes = streamsSubscribes;
92 public Map<String, Object> getStreamsPublishes() {
93 return streamsPublishes;
96 public void setStreamsPublishes(Map<String, Object> streamsPublishes) {
97 this.streamsPublishes = streamsPublishes;
100 protected Configuration() {
105 * Get instance of class.
107 public static Configuration getInstance() {
108 if (instance == null) {
109 instance = new Configuration();
114 public String getCg() {
118 public void setCg(String cg) {
122 public String getCid() {
126 public void setCid(String cid) {
130 public int getPollingInterval() {
131 return pollingInterval;
134 public void setPollingInterval(int pollingInterval) {
135 this.pollingInterval = pollingInterval;
138 public int getPollingTimeout() {
139 return pollingTimeout;
142 public void setPollingTimeout(int pollingTimeout) {
143 this.pollingTimeout = pollingTimeout;
146 public String getPgHost() {
150 public void setPgHost(String pgHost) {
151 this.pgHost = pgHost;
154 public int getPgPort() {
158 public void setPgPort(int pgPort) {
159 this.pgPort = pgPort;
162 public String getPgUsername() {
166 public void setPgUsername(String pgUsername) {
167 this.pgUsername = pgUsername;
170 public String getPgPassword() {
174 public void setPgPassword(String pgPassword) {
175 this.pgPassword = pgPassword;
178 public List<String> getDmaapServers() {
182 public void setDmaapServers(List<String> dmaapServers) {
183 this.dmaapServers = dmaapServers;
186 public String getConfigDbService() {
187 return configDbService;
190 public void setConfigDbService(String configDbService) {
191 this.configDbService = configDbService;
194 public int getSamples() {
198 public void setSamples(int samples) {
199 this.samples = samples;
202 public int getMinPercentageChange() {
203 return minPercentageChange;
206 public void setMinPercentageChange(int minPercentageChange) {
207 this.minPercentageChange = minPercentageChange;
210 public long getInitialDelaySeconds() {
211 return initialDelaySeconds;
214 public void setInitialDelaySeconds(long initialDelaySeconds) {
215 this.initialDelaySeconds = initialDelaySeconds;
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 + "]";
229 * updates application configuration.
231 public void updateConfigurationFromJsonObject(JsonObject jsonObject) {
233 log.info("Updating configuration from CBS");
235 Type mapType = new TypeToken<Map<String, Object>>() {
238 JsonObject subscribes = jsonObject.getAsJsonObject("streams_subscribes");
239 streamsSubscribes = new Gson().fromJson(subscribes, mapType);
241 JsonObject publishes = jsonObject.getAsJsonObject("streams_publishes");
242 streamsPublishes = new Gson().fromJson(publishes, mapType);
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();
250 JsonArray servers = jsonObject.getAsJsonArray("sliceanalysisms.dmaap.server");
251 Type listType = new TypeToken<List<String>>() {}.getType();
252 dmaapServers = new Gson().fromJson(servers, listType);
254 cg = jsonObject.get("sliceanalysisms.cg").getAsString();
255 cid = jsonObject.get("sliceanalysisms.cid").getAsString();
256 configDbService = jsonObject.get("sliceanalysisms.configDb.service").getAsString();
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();
263 aafUsername = jsonObject.get("aafUsername").getAsString();
264 aafPassword = jsonObject.get("aafPassword").getAsString();
266 log.info("configuration from CBS {}", this);