ba8e33ea10f811ccc50d2e9f0330ebbe3267fc99
[dcaegen2/services.git] /
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.dcaegen2.kpi.controller;
22
23 import java.lang.reflect.Type;
24 import java.time.Duration;
25 import java.util.Map;
26
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;
36
37 import com.google.gson.Gson;
38 import com.google.gson.JsonObject;
39 import com.google.gson.reflect.TypeToken;
40
41 import reactor.core.Disposable;
42
43 /**
44  * This class provides method to fetch application Configuration from CBS.
45  */
46 public class ConfigFetchFromCbs implements Runnable {
47
48     private static Logger log = LoggerFactory.getLogger(ConfigFetchFromCbs.class);
49
50     private Duration interval;
51
52     public ConfigFetchFromCbs() {
53
54     }
55
56     public ConfigFetchFromCbs(Duration interval) {
57         this.interval = interval;
58     }
59
60     /**
61      * Gets app config from CBS.
62      */
63     private Disposable getAppConfig() {
64
65         // Generate RequestID and InvocationID which will be used when logging and in
66         // HTTP requests
67         log.info("getAppconfig start ..");
68         RequestDiagnosticContext diagnosticContext = RequestDiagnosticContext.create();
69         // Read necessary properties from the environment
70         final var env = CbsClientConfiguration.fromEnvironment();
71
72         log.debug("environments {}", env);
73         ConfigPolicy configPolicy = ConfigPolicy.getInstance();
74
75         // Polling properties
76         final Duration initialDelay = Duration.ofSeconds(5);
77         final Duration period = interval;
78
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)) {
87                         interval = newPeriod;
88                         synchronized (this) {
89                             this.notifyAll();
90                         }
91
92                     }
93                     Configuration.getInstance().updateConfigurationFromJsonObject(config);
94
95                     Type mapType = new TypeToken<Map<String, Object>>() {
96                     }.getType();
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);
103                     }
104                 }, throwable -> log.warn("Get config from cbs error", throwable));
105     }
106
107     @Override
108     public void run() {
109         Boolean done = false;
110         while (!done) {
111             try {
112                 Disposable disp = getAppConfig();
113                 synchronized (this) {
114                     this.wait();
115                 }
116                 log.info("Polling interval changed");
117                 disp.dispose();
118             } catch (Exception e) {
119                 log.info("The config won't be updated");
120                 done = true;
121             }
122         }
123     }
124
125 }
126
127