faef863e54b34c8a144da4d4019f115b62aca509
[ccsdk/oran.git] /
1 /*-
2  * ========================LICENSE_START=================================
3  * ONAP : ccsdk oran
4  * ======================================================================
5  * Copyright (C) 2019-2020 Nordix Foundation. All rights reserved.
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 package org.onap.ccsdk.oran.a1policymanagementservice.tasks;
22
23 import com.google.gson.JsonObject;
24
25 import java.time.Duration;
26 import java.util.Optional;
27 import java.util.Properties;
28
29 import lombok.AccessLevel;
30 import lombok.Getter;
31
32 import org.onap.ccsdk.oran.a1policymanagementservice.clients.A1ClientFactory;
33 import org.onap.ccsdk.oran.a1policymanagementservice.clients.AsyncRestClientFactory;
34 import org.onap.ccsdk.oran.a1policymanagementservice.configuration.ApplicationConfig;
35 import org.onap.ccsdk.oran.a1policymanagementservice.configuration.ApplicationConfig.RicConfigUpdate;
36 import org.onap.ccsdk.oran.a1policymanagementservice.configuration.ApplicationConfigParser;
37 import org.onap.ccsdk.oran.a1policymanagementservice.configuration.ConfigurationFile;
38 import org.onap.ccsdk.oran.a1policymanagementservice.controllers.ServiceCallbacks;
39 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Policies;
40 import org.onap.ccsdk.oran.a1policymanagementservice.repository.PolicyTypes;
41 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Ric;
42 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Ric.RicState;
43 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Rics;
44 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Services;
45 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api.CbsClient;
46 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api.CbsClientFactory;
47 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api.CbsRequests;
48 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.CbsRequest;
49 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.EnvProperties;
50 import org.onap.dcaegen2.services.sdk.rest.services.model.logging.RequestDiagnosticContext;
51 import org.slf4j.Logger;
52 import org.slf4j.LoggerFactory;
53 import org.springframework.beans.factory.annotation.Autowired;
54 import org.springframework.beans.factory.annotation.Value;
55 import org.springframework.stereotype.Component;
56
57 import reactor.core.Disposable;
58 import reactor.core.publisher.Flux;
59 import reactor.core.publisher.Mono;
60 import reactor.util.annotation.Nullable;
61
62 /**
63  * Regularly refreshes the configuration from Consul or from a local
64  * configuration file.
65  */
66 @Component
67 @SuppressWarnings("squid:S2629") // Invoke method(s) only conditionally
68 public class RefreshConfigTask {
69
70     private static final Logger logger = LoggerFactory.getLogger(RefreshConfigTask.class);
71
72     @Value("#{systemEnvironment}")
73     public Properties systemEnvironment;
74
75     /**
76      * The time between refreshes of the configuration. Not final so tests can
77      * modify it.
78      */
79     private static Duration configRefreshInterval = Duration.ofMinutes(1);
80
81     final ConfigurationFile configurationFile;
82     final ApplicationConfig appConfig;
83     @Getter(AccessLevel.PROTECTED)
84     private Disposable refreshTask = null;
85     private boolean isConsulUsed = false;
86
87     private final Rics rics;
88     private final A1ClientFactory a1ClientFactory;
89     private final Policies policies;
90     private final Services services;
91     private final PolicyTypes policyTypes;
92     private final AsyncRestClientFactory restClientFactory;
93
94     @Autowired
95     public RefreshConfigTask(ConfigurationFile configurationFile, ApplicationConfig appConfig, Rics rics,
96             Policies policies, Services services, PolicyTypes policyTypes, A1ClientFactory a1ClientFactory) {
97         this.configurationFile = configurationFile;
98         this.appConfig = appConfig;
99         this.rics = rics;
100         this.policies = policies;
101         this.services = services;
102         this.policyTypes = policyTypes;
103         this.a1ClientFactory = a1ClientFactory;
104         this.restClientFactory = new AsyncRestClientFactory(appConfig.getWebClientConfig());
105     }
106
107     public void start() {
108         logger.debug("Starting refreshConfigTask");
109         stop();
110         refreshTask = createRefreshTask() //
111                 .subscribe(
112                         notUsed -> logger.debug("Refreshed configuration data"), throwable -> logger
113                                 .error("Configuration refresh terminated due to exception {}", throwable.toString()),
114                         () -> logger.error("Configuration refresh terminated"));
115     }
116
117     public void stop() {
118         if (refreshTask != null) {
119             refreshTask.dispose();
120         }
121     }
122
123     Flux<RicConfigUpdate.Type> createRefreshTask() {
124         Flux<JsonObject> loadFromFile = regularInterval() //
125                 .filter(notUsed -> !this.isConsulUsed) //
126                 .flatMap(notUsed -> loadConfigurationFromFile()) //
127                 .onErrorResume(this::ignoreErrorFlux) //
128                 .doOnNext(json -> logger.debug("loadFromFile succeeded")) //
129                 .doOnTerminate(() -> logger.error("loadFromFile Terminate"));
130
131         Flux<JsonObject> loadFromConsul = regularInterval() //
132                 .flatMap(i -> getEnvironment(systemEnvironment)) //
133                 .flatMap(this::createCbsClient) //
134                 .flatMap(this::getFromCbs) //
135                 .onErrorResume(this::ignoreErrorMono) //
136                 .doOnNext(json -> logger.debug("loadFromConsul succeeded")) //
137                 .doOnNext(json -> this.isConsulUsed = true) //
138                 .doOnTerminate(() -> logger.error("loadFromConsul Terminated"));
139
140         final int CONCURRENCY = 50; // Number of RIC synched in paralell
141
142         return Flux.merge(loadFromFile, loadFromConsul) //
143                 .flatMap(this::parseConfiguration) //
144                 .flatMap(this::updateConfig, CONCURRENCY) //
145                 .flatMap(this::handleUpdatedRicConfig) //
146                 .doOnTerminate(() -> logger.error("Configuration refresh task is terminated"));
147     }
148
149     private Flux<Long> regularInterval() {
150         return Flux.interval(Duration.ZERO, configRefreshInterval) //
151                 .onBackpressureDrop() //
152                 .limitRate(1); // Limit so that only one event is emitted at a time
153     }
154
155     Mono<EnvProperties> getEnvironment(Properties systemEnvironment) {
156         return EnvironmentProcessor.readEnvironmentVariables(systemEnvironment) //
157                 .onErrorResume(t -> Mono.empty());
158     }
159
160     Mono<CbsClient> createCbsClient(EnvProperties env) {
161         return CbsClientFactory.createCbsClient(env) //
162                 .onErrorResume(this::ignoreErrorMono);
163     }
164
165     private Mono<JsonObject> getFromCbs(CbsClient cbsClient) {
166         try {
167             final CbsRequest getConfigRequest = CbsRequests.getAll(RequestDiagnosticContext.create());
168             return cbsClient.get(getConfigRequest) //
169                     .onErrorResume(this::ignoreErrorMono);
170         } catch (Exception e) {
171             return ignoreErrorMono(e);
172         }
173     }
174
175     private <R> Flux<R> ignoreErrorFlux(Throwable throwable) {
176         String errMsg = throwable.toString();
177         logger.warn("Could not refresh application configuration. {}", errMsg);
178         return Flux.empty();
179     }
180
181     private <R> Mono<R> ignoreErrorMono(Throwable throwable) {
182         String errMsg = throwable.toString();
183         logger.warn("Could not refresh application configuration. {}", errMsg);
184         return Mono.empty();
185     }
186
187     private Mono<ApplicationConfigParser.ConfigParserResult> parseConfiguration(JsonObject jsonObject) {
188         try {
189             ApplicationConfigParser parser = new ApplicationConfigParser();
190             return Mono.just(parser.parse(jsonObject));
191         } catch (Exception e) {
192             String str = e.toString();
193             logger.error("Could not parse configuration {}", str);
194             return Mono.empty();
195         }
196     }
197
198     private Flux<RicConfigUpdate> updateConfig(ApplicationConfigParser.ConfigParserResult config) {
199         return this.appConfig.setConfiguration(config);
200     }
201
202     private void removePoliciciesInRic(@Nullable Ric ric) {
203         if (ric != null) {
204             synchronizationTask().run(ric);
205         }
206     }
207
208     private RicSynchronizationTask synchronizationTask() {
209         return new RicSynchronizationTask(a1ClientFactory, policyTypes, policies, services, restClientFactory, rics);
210     }
211
212     /**
213      * for an added RIC after a restart it is nesessary to get the suypported policy
214      * types from the RIC unless a full synchronization is wanted.
215      * 
216      * @param ric the ric to get supprted types from
217      * @return the same ric
218      */
219     private Mono<Ric> trySyncronizeSupportedTypes(Ric ric) {
220         logger.debug("Synchronizing policy types for new RIC: {}", ric.id());
221         // Synchronize the policy types
222         return this.a1ClientFactory.createA1Client(ric) //
223                 .flatMapMany(client -> synchronizationTask().synchronizePolicyTypes(ric, client)) //
224                 .collectList() //
225                 .flatMap(list -> Mono.just(ric)) //
226                 .doOnError(t -> logger.warn("Failed to synchronize types in new RIC: {}, reason: {}", ric.id(),
227                         t.getMessage())) //
228                 .onErrorResume(t -> Mono.just(ric));
229     }
230
231     public Mono<RicConfigUpdate.Type> handleUpdatedRicConfig(RicConfigUpdate updatedInfo) {
232         synchronized (this.rics) {
233             String ricId = updatedInfo.getRicConfig().ricId();
234             RicConfigUpdate.Type event = updatedInfo.getType();
235             if (event == RicConfigUpdate.Type.ADDED) {
236                 logger.debug("RIC added {}", ricId);
237
238                 return trySyncronizeSupportedTypes(new Ric(updatedInfo.getRicConfig())) //
239                         .flatMap(this::addRic) //
240                         .flatMap(this::notifyServicesRicAvailable) //
241                         .doOnNext(ric -> ric.setState(RicState.AVAILABLE)) //
242                         .flatMap(notUsed -> Mono.just(event));
243             } else if (event == RicConfigUpdate.Type.REMOVED) {
244                 logger.debug("RIC removed {}", ricId);
245                 Ric ric = rics.remove(ricId);
246                 this.policies.removePoliciesForRic(ricId);
247                 removePoliciciesInRic(ric);
248             } else if (event == RicConfigUpdate.Type.CHANGED) {
249                 logger.debug("RIC config updated {}", ricId);
250                 Ric ric = this.rics.get(ricId);
251                 if (ric == null) {
252                     logger.error("An non existing RIC config is changed, should not happen (just for robustness)");
253                     addRic(new Ric(updatedInfo.getRicConfig())).block();
254                 } else {
255                     ric.setRicConfig(updatedInfo.getRicConfig());
256                 }
257             }
258             return Mono.just(event);
259         }
260     }
261
262     Mono<Ric> addRic(Ric ric) {
263         this.rics.put(ric);
264         if (this.appConfig.getVardataDirectory() != null) {
265             this.policies.restoreFromDatabase(ric, this.policyTypes);
266         }
267         logger.debug("Added RIC: {}", ric.id());
268
269         return Mono.just(ric);
270     }
271
272     private Mono<Ric> notifyServicesRicAvailable(Ric ric) {
273         ServiceCallbacks callbacks = new ServiceCallbacks(this.restClientFactory);
274         return callbacks.notifyServicesRicAvailable(ric, services) //
275                 .collectList() //
276                 .flatMap(list -> Mono.just(ric));
277     }
278
279     /**
280      * Reads the configuration from file.
281      */
282     Flux<JsonObject> loadConfigurationFromFile() {
283         Optional<JsonObject> readJson = configurationFile.readFile();
284         if (readJson.isPresent()) {
285             return Flux.just(readJson.get());
286         }
287         return Flux.empty();
288     }
289 }