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=842b9d220588fba7fc17df0cf9c094f91005118b;hp=1e7f2dc771d5bb4bae59b988a883a38c012ac694;hpb=f694dec2ff16069e6bb5c9de845278f44e8c9591;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 1e7f2dc7..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() { @@ -71,26 +73,29 @@ public class ServiceSupervision { private Flux createTask() { synchronized (services) { 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)); + .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) { + 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();