2bdb0506aa2d6870b92355a12fea5faef1a29135
[dcaegen2/services.git] /
1 /*******************************************************************************
2  *  ============LICENSE_START=======================================================
3  *  slice-analysis-ms
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
11  *
12  *          http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  *
21  *******************************************************************************/
22
23 package org.onap.slice.analysis.ms.controller;
24
25 import com.google.gson.Gson;
26 import com.google.gson.JsonObject;
27 import com.google.gson.reflect.TypeToken;
28
29 import java.lang.reflect.Type;
30 import java.time.Duration;
31 import java.util.Map;
32
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;
43
44 import reactor.core.Disposable;
45
46 /**
47  * This class provides method to fetch application Configuration
48  * from CBS
49  */
50 public class ConfigFetchFromCbs implements Runnable {
51
52     private static Logger log = LoggerFactory.getLogger(ConfigFetchFromCbs.class);
53
54     private Duration interval;
55
56     public ConfigFetchFromCbs() {
57
58     }
59
60     public ConfigFetchFromCbs(Duration interval) {
61         this.interval = interval;
62     }
63
64     /**
65      * Gets app config from CBS.
66      */
67     private Disposable getAppConfig() {
68
69         // Generate RequestID and InvocationID which will be used when logging and in
70         // HTTP requests
71         log.info("getAppconfig start ..");
72         RequestDiagnosticContext diagnosticContext = RequestDiagnosticContext.create();
73         // Read necessary properties from the environment
74         final CbsClientConfiguration cbsClientConfiguration = CbsClientConfiguration.fromEnvironment();
75
76         log.debug("environments {}", cbsClientConfiguration);
77         ConfigPolicy configPolicy = ConfigPolicy.getInstance();
78
79         // Polling properties
80         final Duration initialDelay = Duration.ofSeconds(5);
81         final Duration period = interval;
82
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)) {
91                     interval = newPeriod;
92                     synchronized (this) {
93                         this.notifyAll();
94                     }
95                 }
96                 Configuration.getInstance().updateConfigurationFromJsonObject(config);
97
98                 Type mapType = new TypeToken<Map<String, Object>>() {
99                 }.getType();
100
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.");
104                     } else {
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);
110                     }
111                 }
112             }, throwable -> log.warn("Ooops", throwable));
113     }
114
115
116     @Override
117     public void run() {
118         Boolean done = false;
119         while (!done) {
120             try {
121                 Disposable disp = getAppConfig();
122                 synchronized (this) {
123                     this.wait();
124                 }
125                 log.info("Polling interval changed");
126                 disp.dispose();
127             } catch (Exception e) {
128                 done = true;
129             }
130         }
131     }
132
133 }