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.controller;
 
  24 import com.google.gson.Gson;
 
  25 import com.google.gson.JsonObject;
 
  26 import com.google.gson.reflect.TypeToken;
 
  28 import java.lang.reflect.Type;
 
  29 import java.time.Duration;
 
  32 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api.CbsClientFactory;
 
  33 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api.CbsRequests;
 
  34 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.CbsRequest;
 
  35 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.EnvProperties;
 
  36 import org.onap.dcaegen2.services.sdk.rest.services.model.logging.RequestDiagnosticContext;
 
  37 import org.onap.slice.analysis.ms.models.ConfigPolicy;
 
  38 import org.onap.slice.analysis.ms.models.Configuration;
 
  39 import org.slf4j.Logger;
 
  40 import org.slf4j.LoggerFactory;
 
  42 import reactor.core.Disposable;
 
  45  * This class provides method to fetch application Configuration
 
  48 public class ConfigFetchFromCbs implements Runnable {
 
  50     private static Logger log = LoggerFactory.getLogger(ConfigFetchFromCbs.class);
 
  52     private Duration interval;
 
  54     public ConfigFetchFromCbs() {
 
  58     public ConfigFetchFromCbs(Duration interval) {
 
  59         this.interval = interval;
 
  63      * Gets app config from CBS.
 
  65     private Disposable getAppConfig() {
 
  67         // Generate RequestID and InvocationID which will be used when logging and in
 
  69         log.info("getAppconfig start ..");
 
  70         RequestDiagnosticContext diagnosticContext = RequestDiagnosticContext.create();
 
  71         // Read necessary properties from the environment
 
  72         final EnvProperties env = EnvProperties.fromEnvironment();
 
  73         log.debug("environments {}", env);
 
  74         ConfigPolicy configPolicy = ConfigPolicy.getInstance();
 
  77         final Duration initialDelay = Duration.ofSeconds(5);
 
  78         final Duration period = interval;
 
  80         // Create the client and use it to get the configuration
 
  81         final CbsRequest request = CbsRequests.getAll(diagnosticContext);
 
  82         return CbsClientFactory.createCbsClient(env)
 
  83                 .flatMapMany(cbsClient -> cbsClient.updates(request, initialDelay, period)).subscribe(jsonObject -> {
 
  84                     log.info("configuration and policy from CBS {}", jsonObject);
 
  85                     JsonObject config = jsonObject.getAsJsonObject("config");
 
  86                     Duration newPeriod = Duration.ofSeconds(config.get("cbsPollingInterval").getAsInt());
 
  87                     if (!newPeriod.equals(period)) {
 
  94                     Configuration.getInstance().updateConfigurationFromJsonObject(config);
 
  96                     Type mapType = new TypeToken<Map<String, Object>>() {
 
  98                     if (jsonObject.getAsJsonObject("policies") != null) {
 
  99                         JsonObject policyJson = jsonObject.getAsJsonObject("policies").getAsJsonArray("items").get(0)
 
 100                                 .getAsJsonObject().getAsJsonObject("config");
 
 101                         Map<String, Object> policy = new Gson().fromJson(policyJson, mapType);
 
 102                         configPolicy.setConfig(policy);
 
 103                         log.info("Config policy {}", configPolicy);
 
 105                 }, throwable -> log.warn("Ooops", throwable));
 
 112         Boolean done = false;
 
 115                 Disposable disp = getAppConfig();
 
 116                 synchronized (this) {
 
 119                 log.info("Polling interval changed");
 
 121             } catch (Exception e) {