1 /*******************************************************************************
2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 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.dcaegen2.services.sonhms.controller;
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;
29 import java.lang.reflect.Type;
30 import java.util.List;
33 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api.CbsClientFactory;
34 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api.CbsRequests;
35 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.CbsRequest;
36 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.EnvProperties;
37 import org.onap.dcaegen2.services.sdk.rest.services.model.logging.RequestDiagnosticContext;
38 import org.onap.dcaegen2.services.sonhms.ConfigPolicy;
39 import org.onap.dcaegen2.services.sonhms.Configuration;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
43 public class ConfigFetchFromCbs {
45 private static Logger log = LoggerFactory.getLogger(ConfigFetchFromCbs.class);
48 * Gets app config from CBS.
50 public void getAppConfig() {
52 // Generate RequestID and InvocationID which will be used when logging and in
54 log.info("getAppconfig start ..");
55 RequestDiagnosticContext diagnosticContext = RequestDiagnosticContext.create();
56 // Read necessary properties from the environment
57 final EnvProperties env = EnvProperties.fromEnvironment();
58 log.debug("environments {}", env);
59 ConfigPolicy configPolicy = ConfigPolicy.getInstance();
61 // Create the client and use it to get the configuration
62 final CbsRequest request = CbsRequests.getAll(diagnosticContext);
63 CbsClientFactory.createCbsClient(env).flatMap(cbsClient -> cbsClient.get(request)).subscribe(jsonObject -> {
64 log.info("configuration and policy from CBS {}", jsonObject);
65 JsonObject config = jsonObject.getAsJsonObject("config");
67 updateConfigurationFromJsonObject(config);
69 Type mapType = new TypeToken<Map<String, Object>>() {
71 JsonObject policyJson = jsonObject.getAsJsonObject("policies").getAsJsonArray("items").get(0)
72 .getAsJsonObject().getAsJsonObject("config");
73 Map<String, Object> policy = new Gson().fromJson(policyJson, mapType);
74 configPolicy.setConfig(policy);
75 log.info("Config policy {}", configPolicy);
76 }, throwable -> log.warn("Ooops", throwable));
80 private void updateConfigurationFromJsonObject(JsonObject jsonObject) {
82 log.info("Updating configuration from CBS");
83 Configuration configuration = Configuration.getInstance();
85 Type mapType = new TypeToken<Map<String, Object>>() {
88 JsonObject subscribes = jsonObject.getAsJsonObject("streams_subscribes");
89 Map<String, Object> streamsSubscribes = new Gson().fromJson(subscribes, mapType);
91 JsonObject publishes = jsonObject.getAsJsonObject("streams_publishes");
92 Map<String, Object> streamsPublishes = new Gson().fromJson(publishes, mapType);
94 int pgPort = jsonObject.get("postgres.port").getAsInt();
95 int pollingInterval = jsonObject.get("sonhandler.pollingInterval").getAsInt();
96 String pgPassword = jsonObject.get("postgres.password").getAsString();
97 int numSolutions = jsonObject.get("sonhandler.numSolutions").getAsInt();
98 int minConfusion = jsonObject.get("sonhandler.minConfusion").getAsInt();
99 int maximumClusters = jsonObject.get("sonhandler.maximumClusters").getAsInt();
100 int minCollision = jsonObject.get("sonhandler.minCollision").getAsInt();
101 String sourceId = jsonObject.get("sonhandler.sourceId").getAsString();
102 String pgUsername = jsonObject.get("postgres.username").getAsString();
103 String pgHost = jsonObject.get("postgres.host").getAsString();
105 JsonArray servers = jsonObject.getAsJsonArray("sonhandler.dmaap.server");
106 Type listType = new TypeToken<List<String>>() {
108 List<String> dmaapServers = new Gson().fromJson(servers, listType);
110 String cg = jsonObject.get("sonhandler.cg").getAsString();
111 int bufferTime = jsonObject.get("sonhandler.bufferTime").getAsInt();
112 String cid = jsonObject.get("sonhandler.cid").getAsString();
113 String configDbService = jsonObject.get("sonhandler.configDb.service").getAsString();
114 String namespace = jsonObject.get("sonhandler.namespace").getAsString();
115 String callbackUrl = "http://" + System.getenv("HOSTNAME") + "." + namespace + ":8080/callbackUrl";
117 String pciOptimizer = jsonObject.get("sonhandler.pciOptimizer").getAsString();
118 String pciAnrOptimizer = jsonObject.get("sonhandler.pciAnrOptimizer").getAsString();
120 String oofService = jsonObject.get("sonhandler.oof.service").getAsString();
121 int pollingTimeout = jsonObject.get("sonhandler.pollingTimeout").getAsInt();
123 int badThreshold = jsonObject.get("sonhandler.badThreshold").getAsInt();
124 int poorThreshold = jsonObject.get("sonhandler.poorThreshold").getAsInt();
126 int poorCountThreshold = jsonObject.get("sonhandler.poorCountThreshold").getAsInt();
127 int badCountThreshold = jsonObject.get("sonhandler.badCountThreshold").getAsInt();
128 int oofTriggerCountTimer = jsonObject.get("sonhandler.oofTriggerCountTimer").getAsInt();
129 int oofTriggerCountThreshold = jsonObject.get("sonhandler.oofTriggerCountThreshold").getAsInt();
130 int policyRespTimer = jsonObject.get("sonhandler.policyRespTimer").getAsInt();
132 configuration.setStreamsSubscribes(streamsSubscribes);
133 configuration.setStreamsPublishes(streamsPublishes);
134 configuration.setPgPassword(pgPassword);
135 configuration.setPgPort(pgPort);
136 configuration.setPollingInterval(pollingInterval);
137 configuration.setNumSolutions(numSolutions);
138 configuration.setMinCollision(minCollision);
139 configuration.setMinConfusion(minConfusion);
140 configuration.setMaximumClusters(maximumClusters);
141 configuration.setPgHost(pgHost);
142 configuration.setPgUsername(pgUsername);
143 configuration.setSourceId(sourceId);
144 configuration.setDmaapServers(dmaapServers);
145 configuration.setCg(cg);
146 configuration.setCid(cid);
147 configuration.setBufferTime(bufferTime);
148 configuration.setConfigDbService(configDbService);
149 configuration.setCallbackUrl(callbackUrl);
150 configuration.setPciOptimizer(pciOptimizer);
151 configuration.setPciAnrOptimizer(pciAnrOptimizer);
152 configuration.setOofService(oofService);
153 configuration.setPollingTimeout(pollingTimeout);
154 configuration.setBadThreshold(badThreshold);
155 configuration.setPoorThreshold(poorThreshold);
156 configuration.setPoorCountThreshold(poorCountThreshold);
157 configuration.setBadCountThreshold(badCountThreshold);
158 configuration.setOofTriggerCountTimer(oofTriggerCountTimer);
159 configuration.setOofTriggerCountThreshold(oofTriggerCountThreshold);
160 configuration.setPolicyRespTimer(policyRespTimer);
162 log.info("configuration from CBS {}", configuration.toString());