aboutsummaryrefslogtreecommitdiffstats
path: root/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/tasks/RefreshConfigTask.java
blob: e3d489b5dd39b99bb1cde78e037eb22e4ae933fb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/*-
 * ========================LICENSE_START=================================
 * ONAP : ccsdk oran
 * ======================================================================
 * Copyright (C) 2019-2020 Nordix Foundation. All rights reserved.
 * ======================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ========================LICENSE_END===================================
 */

package org.onap.ccsdk.oran.a1policymanagementservice.tasks;

import com.google.gson.JsonObject;

import java.time.Duration;
import java.util.Optional;
import java.util.Properties;

import lombok.AccessLevel;
import lombok.Getter;

import org.onap.ccsdk.oran.a1policymanagementservice.clients.A1ClientFactory;
import org.onap.ccsdk.oran.a1policymanagementservice.clients.AsyncRestClientFactory;
import org.onap.ccsdk.oran.a1policymanagementservice.clients.SecurityContext;
import org.onap.ccsdk.oran.a1policymanagementservice.configuration.ApplicationConfig;
import org.onap.ccsdk.oran.a1policymanagementservice.configuration.ApplicationConfig.RicConfigUpdate;
import org.onap.ccsdk.oran.a1policymanagementservice.configuration.ApplicationConfigParser;
import org.onap.ccsdk.oran.a1policymanagementservice.configuration.ConfigurationFile;
import org.onap.ccsdk.oran.a1policymanagementservice.controllers.ServiceCallbacks;
import org.onap.ccsdk.oran.a1policymanagementservice.repository.Policies;
import org.onap.ccsdk.oran.a1policymanagementservice.repository.PolicyTypes;
import org.onap.ccsdk.oran.a1policymanagementservice.repository.Ric;
import org.onap.ccsdk.oran.a1policymanagementservice.repository.Ric.RicState;
import org.onap.ccsdk.oran.a1policymanagementservice.repository.Rics;
import org.onap.ccsdk.oran.a1policymanagementservice.repository.Services;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import reactor.core.Disposable;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.util.annotation.Nullable;

/**
 * Regularly refreshes the component configuration from a configuration file.
 */
@Component
@SuppressWarnings("squid:S2629") // Invoke method(s) only conditionally
public class RefreshConfigTask {

    private static final Logger logger = LoggerFactory.getLogger(RefreshConfigTask.class);

    @Value("#{systemEnvironment}")
    public Properties systemEnvironment;

    /**
     * The time between refreshes of the configuration. Not final so tests can
     * modify it.
     */
    private static Duration configRefreshInterval = Duration.ofMinutes(1);

    final ConfigurationFile configurationFile;
    final ApplicationConfig appConfig;

    @Getter(AccessLevel.PROTECTED)
    private Disposable refreshTask = null;

    private final Rics rics;
    private final A1ClientFactory a1ClientFactory;
    private final Policies policies;
    private final Services services;
    private final PolicyTypes policyTypes;
    private final AsyncRestClientFactory restClientFactory;

    private long fileLastModified = 0;

    @Autowired
    public RefreshConfigTask(ConfigurationFile configurationFile, ApplicationConfig appConfig, Rics rics,
            Policies policies, Services services, PolicyTypes policyTypes, A1ClientFactory a1ClientFactory,
            SecurityContext securityContext) {
        this.configurationFile = configurationFile;
        this.appConfig = appConfig;
        this.rics = rics;
        this.policies = policies;
        this.services = services;
        this.policyTypes = policyTypes;
        this.a1ClientFactory = a1ClientFactory;
        this.restClientFactory = new AsyncRestClientFactory(appConfig.getWebClientConfig(), securityContext);
    }

    public void start() {
        logger.debug("Starting refreshConfigTask");
        stop();
        refreshTask = createRefreshTask() //
                .subscribe(
                        notUsed -> logger.debug("Refreshed configuration data"), throwable -> logger
                                .error("Configuration refresh terminated due to exception {}", throwable.toString()),
                        () -> logger.error("Configuration refresh terminated"));
    }

    public void stop() {
        if (refreshTask != null) {
            refreshTask.dispose();
        }
    }

    Flux<RicConfigUpdate.Type> createRefreshTask() {
        Flux<JsonObject> loadFromFile = regularInterval() //
                .flatMap(notUsed -> loadConfigurationFromFile()) //
                .onErrorResume(this::ignoreErrorFlux) //
                .doOnNext(json -> logger.debug("loadFromFile succeeded")) //
                .doOnTerminate(() -> logger.error("loadFromFile Terminate"));

        final int CONCURRENCY = 50; // Number of RIC synched in paralell

        return loadFromFile //
                .flatMap(this::parseConfiguration) //
                .flatMap(this::updateConfig, CONCURRENCY) //
                .flatMap(this::handleUpdatedRicConfig) //
                .doOnTerminate(() -> logger.error("Configuration refresh task is terminated"));
    }

    private Flux<Long> regularInterval() {
        return Flux.interval(Duration.ZERO, configRefreshInterval) //
                .onBackpressureDrop() //
                .limitRate(1); // Limit so that only one event is emitted at a time
    }

    private <R> Flux<R> ignoreErrorFlux(Throwable throwable) {
        String errMsg = throwable.toString();
        logger.warn("Could not refresh application configuration. {}", errMsg);
        return Flux.empty();
    }

    private Mono<ApplicationConfigParser.ConfigParserResult> parseConfiguration(JsonObject jsonObject) {
        try {
            ApplicationConfigParser parser = new ApplicationConfigParser(this.appConfig);
            return Mono.just(parser.parse(jsonObject));
        } catch (Exception e) {
            String str = e.toString();
            logger.error("Could not parse configuration {}", str);
            return Mono.empty();
        }
    }

    private Flux<RicConfigUpdate> updateConfig(ApplicationConfigParser.ConfigParserResult config) {
        return this.appConfig.setConfiguration(config);
    }

    private void removePoliciciesInRic(@Nullable Ric ric) {
        if (ric != null) {
            synchronizationTask().run(ric);
        }
    }

    private RicSynchronizationTask synchronizationTask() {
        return new RicSynchronizationTask(a1ClientFactory, policyTypes, policies, services, restClientFactory, rics);
    }

    /**
     * for an added RIC after a restart it is nesessary to get the suypported policy
     * types from the RIC unless a full synchronization is wanted.
     *
     * @param ric the ric to get supprted types from
     * @return the same ric
     */
    private Mono<Ric> trySyncronizeSupportedTypes(Ric ric) {
        logger.debug("Synchronizing policy types for new RIC: {}", ric.id());
        // Synchronize the policy types
        ric.setState(RicState.SYNCHRONIZING);
        return this.a1ClientFactory.createA1Client(ric) //
                .flatMapMany(client -> synchronizationTask().synchronizePolicyTypes(ric, client)) //
                .collectList() //
                .map(list -> ric) //
                .doOnNext(notUsed -> ric.setState(RicState.AVAILABLE)) //
                .doOnError(t -> {
                    logger.warn("Failed to synchronize types in new RIC: {}, reason: {}", ric.id(), t.getMessage());
                    ric.setState(RicState.UNAVAILABLE); //
                }) //
                .onErrorResume(t -> Mono.just(ric));
    }

    public Mono<RicConfigUpdate.Type> handleUpdatedRicConfig(RicConfigUpdate updatedInfo) {
        synchronized (this.rics) {
            String ricId = updatedInfo.getRicConfig().ricId();
            RicConfigUpdate.Type event = updatedInfo.getType();
            if (event == RicConfigUpdate.Type.ADDED) {
                logger.debug("RIC added {}", ricId);

                return trySyncronizeSupportedTypes(new Ric(updatedInfo.getRicConfig())) //
                        .doOnNext(this::addRic) //
                        .flatMap(this::notifyServicesRicAvailable) //
                        .flatMap(notUsed -> Mono.just(event));
            } else if (event == RicConfigUpdate.Type.REMOVED) {
                logger.debug("RIC removed {}", ricId);
                Ric ric = rics.remove(ricId);
                this.policies.removePoliciesForRic(ricId);
                removePoliciciesInRic(ric);
            } else if (event == RicConfigUpdate.Type.CHANGED) {
                logger.debug("RIC config updated {}", ricId);
                Ric ric = this.rics.get(ricId);
                if (ric == null) {
                    logger.error("An non existing RIC config is changed, should not happen (just for robustness)");
                    addRic(new Ric(updatedInfo.getRicConfig()));
                } else {
                    ric.setRicConfig(updatedInfo.getRicConfig());
                }
            }
            return Mono.just(event);
        }
    }

    void addRic(Ric ric) {
        this.rics.put(ric);
        if (this.appConfig.getVardataDirectory() != null) {
            this.policies.restoreFromDatabase(ric, this.policyTypes);
        }
        logger.debug("Added RIC: {}", ric.id());
    }

    private Mono<Ric> notifyServicesRicAvailable(Ric ric) {
        if (ric.getState() == RicState.AVAILABLE) {
            ServiceCallbacks callbacks = new ServiceCallbacks(this.restClientFactory);
            return callbacks.notifyServicesRicAvailable(ric, services) //
                    .collectList() //
                    .map(list -> ric);
        } else {
            return Mono.just(ric);
        }
    }

    /**
     * Reads the configuration from file.
     */
    Flux<JsonObject> loadConfigurationFromFile() {
        if (configurationFile.getLastModified() == fileLastModified) {
            return Flux.empty();
        }
        fileLastModified = configurationFile.getLastModified();
        Optional<JsonObject> readJson = configurationFile.readFile();
        if (readJson.isPresent()) {
            return Flux.just(readJson.get());
        }
        return Flux.empty();
    }
}