X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=policy-agent%2Fsrc%2Fmain%2Fjava%2Forg%2Foransc%2Fpolicyagent%2Ftasks%2FServiceSupervision.java;h=626a9b69314dc1684af53e837ef6e9b7fabb03db;hb=9013ed7ad46ce6927fbf69890487e8df61b7d7ee;hp=335aa9452beff645b610a06525f4b4393567ccbe;hpb=581b06ef3c0536416575de7fb7315ba95b849c92;p=nonrtric.git diff --git a/policy-agent/src/main/java/org/oransc/policyagent/tasks/ServiceSupervision.java b/policy-agent/src/main/java/org/oransc/policyagent/tasks/ServiceSupervision.java index 335aa945..626a9b69 100644 --- a/policy-agent/src/main/java/org/oransc/policyagent/tasks/ServiceSupervision.java +++ b/policy-agent/src/main/java/org/oransc/policyagent/tasks/ServiceSupervision.java @@ -20,7 +20,7 @@ package org.oransc.policyagent.tasks; -import org.oransc.policyagent.clients.A1Client; +import org.oransc.policyagent.clients.A1ClientFactory; import org.oransc.policyagent.repository.Policies; import org.oransc.policyagent.repository.Policy; import org.oransc.policyagent.repository.Service; @@ -35,33 +35,35 @@ import org.springframework.stereotype.Component; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; +/** + * Periodically checks that services with a keepAliveInterval set are alive. If a service is deemed not alive, + * all the service's policies are deleted, both in the repository and in the affected Rics, and the service is + * removed from the repository. This means that the service needs to register again after this. + */ @Component @EnableScheduling public class ServiceSupervision { private static final Logger logger = LoggerFactory.getLogger(ServiceSupervision.class); private final Services services; private final Policies policies; - private A1Client a1Client; + private A1ClientFactory a1ClientFactory; @Autowired - public ServiceSupervision(Services services, Policies policies, A1Client a1Client) { + public ServiceSupervision(Services services, Policies policies, A1ClientFactory a1ClientFactory) { this.services = services; this.policies = policies; - this.a1Client = a1Client; + this.a1ClientFactory = a1ClientFactory; } @Scheduled(fixedRate = 1000 * 60) public void checkAllServices() { logger.debug("Checking services starting"); - createTask().subscribe(this::onPolicyDeleted, this::onError, this::onComplete); + createTask().subscribe(this::onPolicyDeleted, null, this::onComplete); } + @SuppressWarnings("squid:S2629") private void onPolicyDeleted(Policy policy) { - logger.info("Policy deleted due to inactivity: " + policy.id() + ", service: " + policy.ownerServiceName()); - } - - private void onError(Throwable t) { - logger.error("Service supervision failed", t); + logger.debug("Policy deleted due to inactivity: {}, service: {}", policy.id(), policy.ownerServiceName()); } private void onComplete() { @@ -69,24 +71,31 @@ public class ServiceSupervision { } private Flux createTask() { - return Flux.fromIterable(services.getAll()) // - .filter(service -> service.isExpired()) // - .doOnNext(service -> logger.info("Service is expired:" + service.getName())) // - .flatMap(service -> getAllPolicies(service)) // - .doOnNext(policy -> this.policies.remove(policy)) // - .flatMap(policy -> deletePolicyInRic(policy)); + synchronized (services) { + return Flux.fromIterable(services.getAll()) // + .filter(Service::isExpired) // + .doOnNext(service -> logger.info("Service is expired: {}", service.getName())) // + .doOnNext(service -> services.remove(service.getName())) // + .flatMap(this::getAllPoliciesForService) // + .doOnNext(policies::remove) // + .flatMap(this::deletePolicyInRic); + } } - private Flux getAllPolicies(Service service) { - return Flux.fromIterable(policies.getForService(service.getName())); + private Flux getAllPoliciesForService(Service service) { + synchronized (policies) { + return Flux.fromIterable(policies.getForService(service.getName())); + } } private Mono deletePolicyInRic(Policy policy) { - return a1Client.deletePolicy(policy.ric().getConfig().baseUrl(), policy.id()) // - .onErrorResume(exception -> handleDeleteFromRicFailure(policy, exception)) // - .map((nothing) -> policy); + return a1ClientFactory.createA1Client(policy.ric()) // + .flatMap(client -> client.deletePolicy(policy) // + .onErrorResume(exception -> handleDeleteFromRicFailure(policy, exception)) // + .map(nothing -> policy)); } + @SuppressWarnings("squid:S2629") private Mono handleDeleteFromRicFailure(Policy policy, Throwable e) { logger.warn("Could not delete policy: {} from ric: {}", policy.id(), policy.ric().name(), e); return Mono.empty();