2  * ============LICENSE_START=======================================================
 
   3  *  Copyright (C) 2021 China Mobile.
 
   4  * ================================================================================
 
   5  * Licensed under the Apache License, Version 2.0 (the "License");
 
   6  * you may not use this file except in compliance with the License.
 
   7  * You may obtain a copy of the License at
 
   9  *      http://www.apache.org/licenses/LICENSE-2.0
 
  11  * Unless required by applicable law or agreed to in writing, software
 
  12  * distributed under the License is distributed on an "AS IS" BASIS,
 
  13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
  14  * See the License for the specific language governing permissions and
 
  15  * limitations under the License.
 
  17  * SPDX-License-Identifier: Apache-2.0
 
  18  * ============LICENSE_END=========================================================
 
  21 package org.onap.dcaegen2.kpi.controller;
 
  23 import java.lang.reflect.Type;
 
  24 import java.time.Duration;
 
  27 import org.onap.dcaegen2.kpi.models.ConfigPolicy;
 
  28 import org.onap.dcaegen2.kpi.models.Configuration;
 
  29 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api.CbsClientFactory;
 
  30 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api.CbsRequests;
 
  31 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.CbsClientConfiguration;
 
  32 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.CbsRequest;
 
  33 import org.onap.dcaegen2.services.sdk.rest.services.model.logging.RequestDiagnosticContext;
 
  34 import org.slf4j.Logger;
 
  35 import org.slf4j.LoggerFactory;
 
  37 import com.google.gson.Gson;
 
  38 import com.google.gson.JsonObject;
 
  39 import com.google.gson.reflect.TypeToken;
 
  41 import reactor.core.Disposable;
 
  44  * This class provides method to fetch application Configuration from CBS.
 
  46 public class ConfigFetchFromCbs implements Runnable {
 
  48     private static Logger log = LoggerFactory.getLogger(ConfigFetchFromCbs.class);
 
  50     private Duration interval;
 
  52     public ConfigFetchFromCbs() {
 
  56     public ConfigFetchFromCbs(Duration interval) {
 
  57         this.interval = interval;
 
  61      * Gets app config from CBS.
 
  63     private Disposable getAppConfig() {
 
  65         // Generate RequestID and InvocationID which will be used when logging and in
 
  67         log.info("getAppconfig start ..");
 
  68         RequestDiagnosticContext diagnosticContext = RequestDiagnosticContext.create();
 
  69         // Read necessary properties from the environment
 
  70         final var env = CbsClientConfiguration.fromEnvironment();
 
  72         log.debug("environments {}", env);
 
  73         ConfigPolicy configPolicy = ConfigPolicy.getInstance();
 
  76         final Duration initialDelay = Duration.ofSeconds(5);
 
  77         final Duration period = interval;
 
  79         // Create the client and use it to get the configuration
 
  80         final CbsRequest request = CbsRequests.getAll(diagnosticContext);
 
  81         return CbsClientFactory.createCbsClient(env)
 
  82                 .flatMapMany(cbsClient -> cbsClient.updates(request, initialDelay, period)).subscribe(jsonObject -> {
 
  83                     log.info("configuration and policy from CBS {}", jsonObject);
 
  84                     JsonObject config = jsonObject.getAsJsonObject("config");
 
  85                     Duration newPeriod = Duration.ofSeconds(config.get("cbsPollingInterval").getAsInt());
 
  86                     if (!newPeriod.equals(period)) {
 
  93                     Configuration.getInstance().updateConfigurationFromJsonObject(config);
 
  95                     Type mapType = new TypeToken<Map<String, Object>>() {
 
  97                     if (jsonObject.getAsJsonObject("policies") != null) {
 
  98                         JsonObject policyJson = jsonObject.getAsJsonObject("policies").getAsJsonArray("items").get(0)
 
  99                                 .getAsJsonObject().getAsJsonObject("config");
 
 100                         Map<String, Object> policy = new Gson().fromJson(policyJson, mapType);
 
 101                         configPolicy.setConfig(policy);
 
 102                         log.info("Config policy {}", configPolicy);
 
 104                 }, throwable -> log.warn("Get config from cbs error", throwable));
 
 109         Boolean done = false;
 
 112                 Disposable disp = getAppConfig();
 
 113                 synchronized (this) {
 
 116                 log.info("Polling interval changed");
 
 118             } catch (Exception e) {
 
 119                 log.info("The config won't be updated");