2 * ========================LICENSE_START=================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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===================================
21 package org.onap.ccsdk.oran.a1policymanagementservice.tasks;
23 import static org.onap.ccsdk.oran.a1policymanagementservice.repository.Ric.RicState;
25 import org.onap.ccsdk.oran.a1policymanagementservice.clients.A1Client;
26 import org.onap.ccsdk.oran.a1policymanagementservice.clients.A1ClientFactory;
27 import org.onap.ccsdk.oran.a1policymanagementservice.clients.AsyncRestClient;
28 import org.onap.ccsdk.oran.a1policymanagementservice.clients.AsyncRestClientFactory;
29 import org.onap.ccsdk.oran.a1policymanagementservice.repository.ImmutablePolicyType;
30 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Lock.LockType;
31 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Policies;
32 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Policy;
33 import org.onap.ccsdk.oran.a1policymanagementservice.repository.PolicyType;
34 import org.onap.ccsdk.oran.a1policymanagementservice.repository.PolicyTypes;
35 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Ric;
36 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Service;
37 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Services;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
41 import reactor.core.publisher.BaseSubscriber;
42 import reactor.core.publisher.Flux;
43 import reactor.core.publisher.Mono;
44 import reactor.core.publisher.SignalType;
47 * Synchronizes the content of a Near-RT RIC with the content in the repository.
50 * load all policy types
52 * send all policy instances to the Near-RT RIC
54 * if that fails remove all policy instances
56 * Notify subscribing services
58 @SuppressWarnings("squid:S2629") // Invoke method(s) only conditionally
59 public class RicSynchronizationTask {
61 private static final Logger logger = LoggerFactory.getLogger(RicSynchronizationTask.class);
62 static final int CONCURRENCY_RIC = 1; // How many paralell requests that is sent to one NearRT RIC
64 private final A1ClientFactory a1ClientFactory;
65 private final PolicyTypes policyTypes;
66 private final Policies policies;
67 private final Services services;
68 private final AsyncRestClientFactory restClientFactory;
70 public RicSynchronizationTask(A1ClientFactory a1ClientFactory, PolicyTypes policyTypes, Policies policies,
71 Services services, AsyncRestClientFactory restClientFactory) {
72 this.a1ClientFactory = a1ClientFactory;
73 this.policyTypes = policyTypes;
74 this.policies = policies;
75 this.services = services;
76 this.restClientFactory = restClientFactory;
79 public void run(Ric ric) {
80 logger.debug("Handling ric: {}", ric.getConfig().ricId());
82 if (ric.getState() == RicState.SYNCHRONIZING) {
83 logger.debug("Ric: {} is already being synchronized", ric.getConfig().ricId());
87 ric.getLock().lock(LockType.EXCLUSIVE) //
88 .flatMap(notUsed -> setRicState(ric)) //
89 .flatMap(lock -> this.a1ClientFactory.createA1Client(ric)) //
90 .flatMapMany(client -> runSynchronization(ric, client)) //
91 .onErrorResume(throwable -> deleteAllPolicyInstances(ric, throwable))
92 .subscribe(new BaseSubscriber<Object>() {
94 protected void hookOnError(Throwable throwable) {
95 logger.warn("Synchronization failure for ric: {}, reason: {}", ric.id(),
96 throwable.getMessage());
97 ric.setState(RicState.UNAVAILABLE);
101 protected void hookOnComplete() {
102 onSynchronizationComplete(ric);
106 protected void hookFinally(SignalType type) {
107 ric.getLock().unlockBlocking();
112 @SuppressWarnings("squid:S2445") // Blocks should be synchronized on "private final" fields
113 private Mono<Ric> setRicState(Ric ric) {
115 if (ric.getState() == RicState.SYNCHRONIZING) {
116 logger.debug("Ric: {} is already being synchronized", ric.getConfig().ricId());
119 ric.setState(RicState.SYNCHRONIZING);
120 return Mono.just(ric);
124 private Flux<Object> runSynchronization(Ric ric, A1Client a1Client) {
125 Flux<PolicyType> synchronizedTypes = synchronizePolicyTypes(ric, a1Client);
126 Flux<?> policiesDeletedInRic = a1Client.deleteAllPolicies();
127 Flux<Policy> policiesRecreatedInRic = recreateAllPoliciesInRic(ric, a1Client);
129 return Flux.concat(synchronizedTypes, policiesDeletedInRic, policiesRecreatedInRic);
132 private void onSynchronizationComplete(Ric ric) {
133 logger.debug("Synchronization completed for: {}", ric.id());
134 ric.setState(RicState.AVAILABLE);
135 notifyAllServices("Synchronization completed for:" + ric.id());
138 private void notifyAllServices(String body) {
139 for (Service service : services.getAll()) {
140 String url = service.getCallbackUrl();
141 if (url.length() > 0) {
142 createNotificationClient(url) //
145 notUsed -> logger.debug("Service {} notified", service.getName()),
146 throwable -> logger.warn("Service notification failed for service: {}. Cause: {}",
147 service.getName(), throwable.getMessage()),
148 () -> logger.debug("All services notified"));
153 private Flux<Object> deleteAllPolicyInstances(Ric ric, Throwable t) {
154 logger.debug("Recreation of policies failed for ric: {}, reason: {}", ric.id(), t.getMessage());
155 deleteAllPoliciesInRepository(ric);
157 Flux<PolicyType> synchronizedTypes = this.a1ClientFactory.createA1Client(ric) //
158 .flatMapMany(a1Client -> synchronizePolicyTypes(ric, a1Client));
159 Flux<?> deletePoliciesInRic = this.a1ClientFactory.createA1Client(ric) //
160 .flatMapMany(A1Client::deleteAllPolicies) //
161 .doOnComplete(() -> deleteAllPoliciesInRepository(ric));
163 return Flux.concat(synchronizedTypes, deletePoliciesInRic);
166 AsyncRestClient createNotificationClient(final String url) {
167 return restClientFactory.createRestClient(url);
170 private Flux<PolicyType> synchronizePolicyTypes(Ric ric, A1Client a1Client) {
171 return a1Client.getPolicyTypeIdentities() //
172 .doOnNext(x -> ric.clearSupportedPolicyTypes()) //
173 .flatMapMany(Flux::fromIterable) //
174 .doOnNext(typeId -> logger.debug("For ric: {}, handling type: {}", ric.getConfig().ricId(), typeId)) //
175 .flatMap(policyTypeId -> getPolicyType(policyTypeId, a1Client), CONCURRENCY_RIC) //
176 .doOnNext(ric::addSupportedPolicyType); //
179 private Mono<PolicyType> getPolicyType(String policyTypeId, A1Client a1Client) {
180 if (policyTypes.contains(policyTypeId)) {
181 return Mono.just(policyTypes.get(policyTypeId));
183 return a1Client.getPolicyTypeSchema(policyTypeId) //
184 .flatMap(schema -> createPolicyType(policyTypeId, schema));
187 private Mono<PolicyType> createPolicyType(String policyTypeId, String schema) {
188 PolicyType pt = ImmutablePolicyType.builder().id(policyTypeId).schema(schema).build();
190 return Mono.just(pt);
193 private void deleteAllPoliciesInRepository(Ric ric) {
194 for (Policy policy : policies.getForRic(ric.id())) {
195 this.policies.remove(policy);
199 private Flux<Policy> putPolicy(Policy policy, Ric ric, A1Client a1Client) {
200 logger.debug("Recreating policy: {}, for ric: {}", policy.id(), ric.getConfig().ricId());
201 return a1Client.putPolicy(policy) //
202 .flatMapMany(notUsed -> Flux.just(policy));
205 private boolean checkTransient(Policy policy) {
206 if (policy.isTransient()) {
207 this.policies.remove(policy);
209 return policy.isTransient();
212 private Flux<Policy> recreateAllPoliciesInRic(Ric ric, A1Client a1Client) {
213 return Flux.fromIterable(policies.getForRic(ric.id())) //
214 .filter(policy -> !checkTransient(policy)) //
215 .flatMap(policy -> putPolicy(policy, ric, a1Client), CONCURRENCY_RIC);