1 /*******************************************************************************
2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2020-2021 Wipro Limited.
6 * Copyright (C) 2022 Huawei Technologies Co., Ltd.
7 * ==============================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
21 *******************************************************************************/
23 package org.onap.slice.analysis.ms.controller;
25 import com.google.gson.Gson;
26 import com.google.gson.JsonObject;
27 import com.google.gson.reflect.TypeToken;
29 import java.lang.reflect.Type;
30 import java.time.Duration;
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.CbsClientConfiguration;
37 import org.onap.dcaegen2.services.sdk.rest.services.model.logging.ImmutableRequestDiagnosticContext;
38 import org.onap.dcaegen2.services.sdk.rest.services.model.logging.RequestDiagnosticContext;
39 import org.onap.slice.analysis.ms.models.ConfigPolicy;
40 import org.onap.slice.analysis.ms.models.Configuration;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
44 import reactor.core.Disposable;
47 * This class provides method to fetch application Configuration
50 public class ConfigFetchFromCbs implements Runnable {
52 private static Logger log = LoggerFactory.getLogger(ConfigFetchFromCbs.class);
54 private Duration interval;
56 public ConfigFetchFromCbs() {
60 public ConfigFetchFromCbs(Duration interval) {
61 this.interval = interval;
65 * Gets app config from CBS.
67 private Disposable getAppConfig() {
69 // Generate RequestID and InvocationID which will be used when logging and in
71 log.info("getAppconfig start ..");
72 RequestDiagnosticContext diagnosticContext = RequestDiagnosticContext.create();
73 // Read necessary properties from the environment
74 final CbsClientConfiguration cbsClientConfiguration = CbsClientConfiguration.fromEnvironment();
76 log.debug("environments {}", cbsClientConfiguration);
77 ConfigPolicy configPolicy = ConfigPolicy.getInstance();
80 final Duration initialDelay = Duration.ofSeconds(5);
81 final Duration period = interval;
83 // Create the client and use it to get the configuration
84 final CbsRequest request = CbsRequests.getAll(diagnosticContext);
85 return CbsClientFactory.createCbsClient(cbsClientConfiguration)
86 .flatMapMany(cbsClient -> cbsClient.updates(request, initialDelay, period)).subscribe(jsonObject -> {
87 log.info("configuration and policy from CBS {}", jsonObject);
88 JsonObject config = jsonObject.getAsJsonObject("config");
89 Duration newPeriod = Duration.ofSeconds(config.get("cbsPollingInterval").getAsInt());
90 if (!newPeriod.equals(period)) {
96 Configuration.getInstance().updateConfigurationFromJsonObject(config);
98 Type mapType = new TypeToken<Map<String, Object>>() {
101 if (jsonObject.getAsJsonObject("policies") != null) {
102 if(jsonObject.getAsJsonObject("policies").getAsJsonArray("items").size() == 0) {
103 log.error("No policy in policy drool pdp engine, nothing to update.");
105 JsonObject policyJson = jsonObject.getAsJsonObject("policies").getAsJsonArray("items").get(0)
106 .getAsJsonObject().getAsJsonObject("config");
107 Map<String, Object> policy = new Gson().fromJson(policyJson, mapType);
108 configPolicy.setConfig(policy);
109 log.info("Config policy {}", configPolicy);
112 }, throwable -> log.warn("Ooops", throwable));
118 Boolean done = false;
121 Disposable disp = getAppConfig();
122 synchronized (this) {
125 log.info("Polling interval changed");
127 } catch (Exception e) {