2 * ========================LICENSE_START=================================
5 * Copyright (C) 2019 Nordix Foundation
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.oransc.policyagent.tasks;
23 import static org.oransc.policyagent.repository.Ric.RicState;
25 import java.util.Vector;
27 import org.oransc.policyagent.clients.A1Client;
28 import org.oransc.policyagent.clients.A1ClientFactory;
29 import org.oransc.policyagent.clients.AsyncRestClient;
30 import org.oransc.policyagent.repository.ImmutablePolicyType;
31 import org.oransc.policyagent.repository.Lock.LockType;
32 import org.oransc.policyagent.repository.Policies;
33 import org.oransc.policyagent.repository.Policy;
34 import org.oransc.policyagent.repository.PolicyType;
35 import org.oransc.policyagent.repository.PolicyTypes;
36 import org.oransc.policyagent.repository.Ric;
37 import org.oransc.policyagent.repository.Service;
38 import org.oransc.policyagent.repository.Services;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
42 import reactor.core.publisher.Flux;
43 import reactor.core.publisher.Mono;
46 * Synchronizes the content of a RIC with the content in the repository. This
49 * load all policy types
51 * send all policy instances to the RIC
53 * if that fails remove all policy instances
55 * Notify subscribing services
57 public class RicSynchronizationTask {
59 private static final Logger logger = LoggerFactory.getLogger(RicSynchronizationTask.class);
61 private final A1ClientFactory a1ClientFactory;
62 private final PolicyTypes policyTypes;
63 private final Policies policies;
64 private final Services services;
66 public RicSynchronizationTask(A1ClientFactory a1ClientFactory, PolicyTypes policyTypes, Policies policies,
68 this.a1ClientFactory = a1ClientFactory;
69 this.policyTypes = policyTypes;
70 this.policies = policies;
71 this.services = services;
74 @SuppressWarnings("squid:S2629")
75 public void run(Ric ric) {
76 logger.debug("Handling ric: {}", ric.getConfig().name());
79 if (ric.getState() == RicState.SYNCHRONIZING) {
80 logger.debug("Ric: {} is already being synchronized", ric.getConfig().name());
83 ric.setState(RicState.SYNCHRONIZING);
86 ric.getLock().lock(LockType.EXCLUSIVE) // Make sure no NBI updates are running
87 .flatMap(lock -> lock.unlock()) //
88 .flatMap(lock -> this.a1ClientFactory.createA1Client(ric)) //
89 .flatMapMany(client -> startSynchronization(ric, client)) //
90 .subscribe(x -> logger.debug("Synchronize: {}", x), //
91 throwable -> onSynchronizationError(ric, throwable), //
92 () -> onSynchronizationComplete(ric));
95 private Flux<Object> startSynchronization(Ric ric, A1Client a1Client) {
96 Flux<PolicyType> recoverTypes = synchronizePolicyTypes(ric, a1Client);
97 Flux<?> policiesDeletedInRic = a1Client.deleteAllPolicies();
98 Flux<?> policiesRecreatedInRic = recreateAllPoliciesInRic(ric, a1Client);
100 return Flux.concat(recoverTypes, policiesDeletedInRic, policiesRecreatedInRic);
103 @SuppressWarnings("squid:S2629")
104 private void onSynchronizationComplete(Ric ric) {
105 logger.debug("Synchronization completed for: {}", ric.name());
106 ric.setState(RicState.IDLE);
107 notifyAllServices("Synchronization completed for:" + ric.name());
110 private void notifyAllServices(String body) {
111 synchronized (services) {
112 for (Service service : services.getAll()) {
113 String url = service.getCallbackUrl();
114 if (service.getCallbackUrl().length() > 0) {
115 createNotificationClient(url) //
118 notUsed -> logger.debug("Service {} notified", service.getName()), throwable -> logger
119 .warn("Service notification failed for service: {}", service.getName(), throwable),
120 () -> logger.debug("All services notified"));
126 @SuppressWarnings("squid:S2629")
127 private void onSynchronizationError(Ric ric, Throwable t) {
128 logger.warn("Synchronization failed for ric: {}, reason: {}", ric.name(), t.getMessage());
129 // If recovery fails, try to remove all instances
130 deleteAllPoliciesInRepository(ric);
132 Flux<PolicyType> recoverTypes = this.a1ClientFactory.createA1Client(ric) //
133 .flatMapMany(a1Client -> synchronizePolicyTypes(ric, a1Client));
134 Flux<?> deletePoliciesInRic = this.a1ClientFactory.createA1Client(ric) //
135 .flatMapMany(a1Client -> a1Client.deleteAllPolicies()) //
136 .doOnComplete(() -> deleteAllPoliciesInRepository(ric));
138 Flux.concat(recoverTypes, deletePoliciesInRic) //
139 .subscribe(x -> logger.debug("Brute recover: " + x), //
140 throwable -> onRecoveryError(ric, throwable), //
141 () -> onSynchronizationComplete(ric));
144 @SuppressWarnings("squid:S2629")
145 private void onRecoveryError(Ric ric, Throwable t) {
146 logger.warn("Synchronization failure recovery failed for ric: {}, reason: {}", ric.name(), t.getMessage());
147 ric.setState(RicState.UNDEFINED);
150 AsyncRestClient createNotificationClient(final String url) {
151 return new AsyncRestClient(url);
154 private Flux<PolicyType> synchronizePolicyTypes(Ric ric, A1Client a1Client) {
155 return a1Client.getPolicyTypeIdentities() //
156 .doOnNext(x -> ric.clearSupportedPolicyTypes()) //
157 .flatMapMany(Flux::fromIterable) //
158 .doOnNext(typeId -> logger.debug("For ric: {}, handling type: {}", ric.getConfig().name(), typeId)) //
159 .flatMap(policyTypeId -> getPolicyType(policyTypeId, a1Client)) //
160 .doOnNext(ric::addSupportedPolicyType); //
163 private Mono<PolicyType> getPolicyType(String policyTypeId, A1Client a1Client) {
164 if (policyTypes.contains(policyTypeId)) {
165 return Mono.just(policyTypes.get(policyTypeId));
167 return a1Client.getPolicyTypeSchema(policyTypeId) //
168 .flatMap(schema -> createPolicyType(policyTypeId, schema));
171 private Mono<PolicyType> createPolicyType(String policyTypeId, String schema) {
172 PolicyType pt = ImmutablePolicyType.builder().name(policyTypeId).schema(schema).build();
174 return Mono.just(pt);
177 private void deleteAllPoliciesInRepository(Ric ric) {
178 synchronized (policies) {
179 for (Policy policy : policies.getForRic(ric.name())) {
180 this.policies.remove(policy);
185 private Flux<String> recreateAllPoliciesInRic(Ric ric, A1Client a1Client) {
186 synchronized (policies) {
187 return Flux.fromIterable(new Vector<>(policies.getForRic(ric.name()))) //
189 policy -> logger.debug("Recreating policy: {}, for ric: {}", policy.id(), ric.getConfig().name()))
190 .flatMap(a1Client::putPolicy);