Update microservice to run as standalone MS
[dcaegen2/services/son-handler.git] / src / main / java / org / onap / dcaegen2 / services / sonhms / controller / ConfigFetchFromCbs.java
1 /*******************************************************************************
2  *  ============LICENSE_START=======================================================
3  *  son-handler
4  *  ================================================================================
5  *   Copyright (C) 2019-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
10  *
11  *          http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  *
20  *******************************************************************************/
21
22 package org.onap.dcaegen2.services.sonhms.controller;
23
24 import com.google.gson.Gson;
25 import com.google.gson.JsonObject;
26 import com.google.gson.reflect.TypeToken;
27
28 import java.lang.reflect.Type;
29 import java.time.Duration;
30 import java.util.Map;
31
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.dcaegen2.services.sonhms.ConfigPolicy;
38 import org.onap.dcaegen2.services.sonhms.Configuration;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41
42 import reactor.core.Disposable;
43
44 public class ConfigFetchFromCbs implements Runnable {
45
46     private static Logger log = LoggerFactory.getLogger(ConfigFetchFromCbs.class);
47
48     private Duration interval;
49
50     public ConfigFetchFromCbs() {
51
52     }
53
54     public ConfigFetchFromCbs(Duration interval) {
55         this.interval = interval;
56     }
57
58     /**
59      * Gets app config from CBS.
60      */
61     private Disposable getAppConfig() {
62
63         // Generate RequestID and InvocationID which will be used when logging and in
64         // HTTP requests
65         log.info("getAppconfig start ..");
66         RequestDiagnosticContext diagnosticContext = RequestDiagnosticContext.create();
67         // Read necessary properties from the environment
68         final EnvProperties env = EnvProperties.fromEnvironment();
69         log.debug("environments {}", env);
70         ConfigPolicy configPolicy = ConfigPolicy.getInstance();
71
72         // Polling properties
73         final Duration initialDelay = Duration.ofSeconds(5);
74         final Duration period = interval;
75
76         // Create the client and use it to get the configuration
77         final CbsRequest request = CbsRequests.getAll(diagnosticContext);
78         return CbsClientFactory.createCbsClient(env)
79                 .flatMapMany(cbsClient -> cbsClient.updates(request, initialDelay, period)).subscribe(jsonObject -> {
80                     log.info("configuration and policy from CBS {}", jsonObject);
81                     JsonObject config = jsonObject.getAsJsonObject("config");
82                     Duration newPeriod = Duration.ofSeconds(config.get("cbsPollingInterval").getAsInt());
83                     if (!newPeriod.equals(period)) {
84                         interval = newPeriod;
85                         synchronized (this) {
86                             this.notifyAll();
87                         }
88
89                     }
90                     Configuration.getInstance().updateConfigurationFromJsonObject(config);
91
92                     Type mapType = new TypeToken<Map<String, Object>>() {
93                     }.getType();
94                     if (jsonObject.getAsJsonObject("policies") != null) {
95                         JsonObject policyJson = jsonObject.getAsJsonObject("policies").getAsJsonArray("items").get(0)
96                                 .getAsJsonObject().getAsJsonObject("config");
97                         Map<String, Object> policy = new Gson().fromJson(policyJson, mapType);
98                         configPolicy.setConfig(policy);
99                         log.info("Config policy {}", configPolicy);
100                     }
101                 }, throwable -> log.warn("Ooops", throwable));
102     }
103
104
105
106     @Override
107     public void run() {
108         Boolean done = false;
109         while (!done) {
110             try {
111                 Disposable disp = getAppConfig();
112                 synchronized (this) {
113                     this.wait();
114                 }
115                 log.info("Polling interval changed");
116                 disp.dispose();
117             } catch (Exception e) {
118                 done = true;
119             }
120         }
121     }
122
123 }