X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=policy-agent%2Fsrc%2Fmain%2Fjava%2Forg%2Foransc%2Fpolicyagent%2Ftasks%2FRicSynchronizationTask.java;h=3879fd6870e755ea39ad70dd0a9ed78e3dad0202;hb=refs%2Fchanges%2F57%2F3057%2F3;hp=bcfda484dc124061a400790e8f2ec4b968966961;hpb=842b9d220588fba7fc17df0cf9c094f91005118b;p=nonrtric.git diff --git a/policy-agent/src/main/java/org/oransc/policyagent/tasks/RicSynchronizationTask.java b/policy-agent/src/main/java/org/oransc/policyagent/tasks/RicSynchronizationTask.java index bcfda484..3879fd68 100644 --- a/policy-agent/src/main/java/org/oransc/policyagent/tasks/RicSynchronizationTask.java +++ b/policy-agent/src/main/java/org/oransc/policyagent/tasks/RicSynchronizationTask.java @@ -22,13 +22,16 @@ package org.oransc.policyagent.tasks; import static org.oransc.policyagent.repository.Ric.RicState; -import java.util.Collection; +import java.util.ArrayList; +import java.util.List; import java.util.Vector; import org.oransc.policyagent.clients.A1Client; import org.oransc.policyagent.clients.A1ClientFactory; import org.oransc.policyagent.clients.AsyncRestClient; import org.oransc.policyagent.repository.ImmutablePolicyType; +import org.oransc.policyagent.repository.Lock; +import org.oransc.policyagent.repository.Lock.LockType; import org.oransc.policyagent.repository.Policies; import org.oransc.policyagent.repository.Policy; import org.oransc.policyagent.repository.PolicyType; @@ -39,17 +42,23 @@ import org.oransc.policyagent.repository.Services; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import reactor.core.publisher.BaseSubscriber; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; /** - * Synchronizes the content of a RIC with the content in the repository. - * This means: - * - load all policy types - * - send all policy instances to the RIC - * --- if that fails remove all policy instances - * - Notify subscribing services + * Synchronizes the content of a RIC with the content in the repository. This + * means: + *

+ * load all policy types + *

+ * send all policy instances to the RIC + *

+ * if that fails remove all policy instances + *

+ * Notify subscribing services */ +@SuppressWarnings("squid:S2629") // Invoke method(s) only conditionally public class RicSynchronizationTask { private static final Logger logger = LoggerFactory.getLogger(RicSynchronizationTask.class); @@ -67,7 +76,7 @@ public class RicSynchronizationTask { this.services = services; } - @SuppressWarnings("squid:S2629") + @SuppressWarnings("squid:S2445") // Blocks should be synchronized on "private final" fields public void run(Ric ric) { logger.debug("Handling ric: {}", ric.getConfig().name()); @@ -78,28 +87,34 @@ public class RicSynchronizationTask { } ric.setState(RicState.SYNCHRONIZING); } - this.a1ClientFactory.createA1Client(ric)// + + ric.getLock().lock(LockType.EXCLUSIVE) // Make sure no NBI updates are running + .flatMap(Lock::unlock) // + .flatMap(lock -> this.a1ClientFactory.createA1Client(ric)) // .flatMapMany(client -> startSynchronization(ric, client)) // - .subscribe(x -> logger.debug("Synchronize: {}", x), // - throwable -> onSynchronizationError(ric, throwable), // - () -> onSynchronizationComplete(ric)); + .subscribe(new BaseSubscriber() { + @Override + protected void hookOnError(Throwable throwable) { + startDeleteAllPolicyInstances(ric, throwable); + } + + @Override + protected void hookOnComplete() { + onSynchronizationComplete(ric); + } + }); } private Flux startSynchronization(Ric ric, A1Client a1Client) { - Flux recoverTypes = synchronizePolicyTypes(ric, a1Client); - Collection policiesForRic = policies.getForRic(ric.name()); - Flux policiesDeletedInRic = Flux.empty(); - Flux policiesRecreatedInRic = Flux.empty(); - if (!policiesForRic.isEmpty()) { - policiesDeletedInRic = a1Client.deleteAllPolicies(); - policiesRecreatedInRic = recreateAllPoliciesInRic(ric, a1Client); - } - return Flux.concat(recoverTypes, policiesDeletedInRic, policiesRecreatedInRic); + Flux synchronizedTypes = synchronizePolicyTypes(ric, a1Client); + Flux policiesDeletedInRic = a1Client.deleteAllPolicies(); + Flux policiesRecreatedInRic = recreateAllPoliciesInRic(ric, a1Client); + + return Flux.concat(synchronizedTypes, policiesDeletedInRic, policiesRecreatedInRic); } - @SuppressWarnings("squid:S2629") private void onSynchronizationComplete(Ric ric) { - logger.debug("Synchronization completed for: {}", ric.name()); + logger.info("Synchronization completed for: {}", ric.name()); ric.setState(RicState.IDLE); notifyAllServices("Synchronization completed for:" + ric.name()); } @@ -111,7 +126,7 @@ public class RicSynchronizationTask { if (service.getCallbackUrl().length() > 0) { createNotificationClient(url) // .put("", body) // - .subscribe( + .subscribe( // notUsed -> logger.debug("Service {} notified", service.getName()), throwable -> logger .warn("Service notification failed for service: {}", service.getName(), throwable), () -> logger.debug("All services notified")); @@ -120,26 +135,24 @@ public class RicSynchronizationTask { } } - @SuppressWarnings("squid:S2629") - private void onSynchronizationError(Ric ric, Throwable t) { + private void startDeleteAllPolicyInstances(Ric ric, Throwable t) { logger.warn("Synchronization failed for ric: {}, reason: {}", ric.name(), t.getMessage()); + // If synchronization fails, try to remove all instances deleteAllPoliciesInRepository(ric); - Flux typesRecoveredForRic = this.a1ClientFactory.createA1Client(ric) // + Flux synchronizedTypes = this.a1ClientFactory.createA1Client(ric) // .flatMapMany(a1Client -> synchronizePolicyTypes(ric, a1Client)); + Flux deletePoliciesInRic = this.a1ClientFactory.createA1Client(ric) // + .flatMapMany(A1Client::deleteAllPolicies) // + .doOnComplete(() -> deleteAllPoliciesInRepository(ric)); - // If recovery fails, try to remove all instances - Flux policiesDeletedInRic = this.a1ClientFactory.createA1Client(ric) // - .flatMapMany(A1Client::deleteAllPolicies); - - Flux.merge(typesRecoveredForRic, policiesDeletedInRic) // - .subscribe(x -> logger.debug("Brute recover: {}", x), // - throwable -> onRecoveryError(ric, throwable), // + Flux.concat(synchronizedTypes, deletePoliciesInRic) // + .subscribe(x -> logger.debug("Brute recovery of failed synchronization: {}", x), // + throwable -> onDeleteAllPolicyInstancesError(ric, throwable), // () -> onSynchronizationComplete(ric)); } - @SuppressWarnings("squid:S2629") - private void onRecoveryError(Ric ric, Throwable t) { + private void onDeleteAllPolicyInstancesError(Ric ric, Throwable t) { logger.warn("Synchronization failure recovery failed for ric: {}, reason: {}", ric.name(), t.getMessage()); ric.setState(RicState.UNDEFINED); } @@ -149,8 +162,8 @@ public class RicSynchronizationTask { } private Flux synchronizePolicyTypes(Ric ric, A1Client a1Client) { - ric.clearSupportedPolicyTypes(); return a1Client.getPolicyTypeIdentities() // + .doOnNext(x -> ric.clearSupportedPolicyTypes()) // .flatMapMany(Flux::fromIterable) // .doOnNext(typeId -> logger.debug("For ric: {}, handling type: {}", ric.getConfig().name(), typeId)) // .flatMap(policyTypeId -> getPolicyType(policyTypeId, a1Client)) // @@ -173,18 +186,23 @@ public class RicSynchronizationTask { private void deleteAllPoliciesInRepository(Ric ric) { synchronized (policies) { - for (Policy policy : policies.getForRic(ric.name())) { + List ricPolicies = new ArrayList<>(policies.getForRic(ric.name())); + for (Policy policy : ricPolicies) { this.policies.remove(policy); } } } - private Flux recreateAllPoliciesInRic(Ric ric, A1Client a1Client) { + private Flux putPolicy(Policy policy, Ric ric, A1Client a1Client) { + logger.debug("Recreating policy: {}, for ric: {}", policy.id(), ric.getConfig().name()); + return a1Client.putPolicy(policy) // + .flatMapMany(notUsed -> Flux.just(policy)); + } + + private Flux recreateAllPoliciesInRic(Ric ric, A1Client a1Client) { synchronized (policies) { return Flux.fromIterable(new Vector<>(policies.getForRic(ric.name()))) // - .doOnNext( - policy -> logger.debug("Recreating policy: {}, for ric: {}", policy.id(), ric.getConfig().name())) - .flatMap(a1Client::putPolicy); + .flatMap(policy -> putPolicy(policy, ric, a1Client)); } }