X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=enrichment-coordinator-service%2Fsrc%2Fmain%2Fjava%2Forg%2Foransc%2Fenrichment%2Fcontrollers%2Fproducer%2FProducerCallbacks.java;h=9b489cdfed9963a8c7c16f88d52cf042e5751d6f;hb=2dbde318f013212c81c4a1f477d7638ec3367aa5;hp=5a47b58f60f61841a204258584f1b8924b965669;hpb=08d483a877c24c3668f77dc8404ee54cd1e041ab;p=nonrtric.git diff --git a/enrichment-coordinator-service/src/main/java/org/oransc/enrichment/controllers/producer/ProducerCallbacks.java b/enrichment-coordinator-service/src/main/java/org/oransc/enrichment/controllers/producer/ProducerCallbacks.java index 5a47b58f..9b489cdf 100644 --- a/enrichment-coordinator-service/src/main/java/org/oransc/enrichment/controllers/producer/ProducerCallbacks.java +++ b/enrichment-coordinator-service/src/main/java/org/oransc/enrichment/controllers/producer/ProducerCallbacks.java @@ -24,27 +24,26 @@ import com.google.gson.Gson; import com.google.gson.GsonBuilder; import java.lang.invoke.MethodHandles; +import java.time.Duration; import java.util.Collection; -import java.util.Vector; import org.oransc.enrichment.clients.AsyncRestClient; import org.oransc.enrichment.clients.AsyncRestClientFactory; import org.oransc.enrichment.configuration.ApplicationConfig; import org.oransc.enrichment.repository.EiJob; +import org.oransc.enrichment.repository.EiJobs; import org.oransc.enrichment.repository.EiProducer; -import org.oransc.enrichment.repository.EiTypes; +import org.oransc.enrichment.repository.EiProducers; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Component; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; +import reactor.util.retry.Retry; /** * Callbacks to the EiProducer */ -@Component @SuppressWarnings("java:S3457") // No need to call "toString()" method as formatting and string .. public class ProducerCallbacks { @@ -52,20 +51,22 @@ public class ProducerCallbacks { private static Gson gson = new GsonBuilder().create(); private final AsyncRestClient restClient; - private final EiTypes eiTypes; - @Autowired - public ProducerCallbacks(ApplicationConfig config, EiTypes eiTypes) { + public ProducerCallbacks(ApplicationConfig config) { AsyncRestClientFactory restClientFactory = new AsyncRestClientFactory(config.getWebClientConfig()); - this.restClient = restClientFactory.createRestClient(""); - this.eiTypes = eiTypes; + this.restClient = restClientFactory.createRestClientNoHttpProxy(""); } - public void notifyProducersJobDeleted(EiJob eiJob) { - for (EiProducer producer : getProducers(eiJob)) { - String url = producer.getJobCallbackUrl() + "/" + eiJob.id(); + public Mono healthCheck(EiProducer producer) { + return restClient.get(producer.getProducerSupervisionCallbackUrl()); + } + + public void stopEiJob(EiJob eiJob, EiProducers eiProducers) { + for (EiProducer producer : getProducersForJob(eiJob, eiProducers)) { + String url = producer.getJobCallbackUrl() + "/" + eiJob.getId(); + producer.setJobDisabled(eiJob); restClient.delete(url) // - .subscribe(notUsed -> logger.debug("Producer job deleted OK {}", producer.getId()), // + .subscribe(response -> logger.debug("Producer job deleted OK {}", producer.getId()), // throwable -> logger.warn("Producer job delete failed {} {}", producer.getId(), throwable.getMessage()), null); @@ -73,43 +74,51 @@ public class ProducerCallbacks { } /** - * Calls all producers for an EiJob activation. + * Start a job in all producers that suports the job type * * @param eiJob an EI job * @return the number of producers that returned OK */ - public Mono notifyProducersJobStarted(EiJob eiJob) { - return Flux.fromIterable(getProducers(eiJob)) // - .flatMap(eiProducer -> notifyProducerJobStarted(eiProducer, eiJob)) // + public Mono startEiJob(EiJob eiJob, EiProducers eiProducers) { + Retry retrySpec = Retry.fixedDelay(1, Duration.ofSeconds(1)); + return Flux.fromIterable(getProducersForJob(eiJob, eiProducers)) // + .flatMap(eiProducer -> startEiJob(eiProducer, eiJob, retrySpec)) // .collectList() // .flatMap(okResponses -> Mono.just(Integer.valueOf(okResponses.size()))); // } /** - * Calls one producer for an EiJob activation. + * Start all jobs for one producer * - * @param producer a producer - * @param eiJob an EI job - * @return the body of the response from the REST call + * @param producer + * @param eiJobs */ - public Mono notifyProducerJobStarted(EiProducer producer, EiJob eiJob) { + public Flux startEiJobs(EiProducer producer, EiJobs eiJobs) { + final int maxNoOfParalellRequests = 10; + Retry retrySpec = Retry.backoff(3, Duration.ofSeconds(1)); + + return Flux.fromIterable(producer.getEiTypes()) // + .flatMap(type -> Flux.fromIterable(eiJobs.getJobsForType(type))) // + .flatMap(job -> startEiJob(producer, job, retrySpec), maxNoOfParalellRequests); + } + + public Mono startEiJob(EiProducer producer, EiJob eiJob, Retry retrySpec) { ProducerJobInfo request = new ProducerJobInfo(eiJob); String body = gson.toJson(request); - return restClient.post(producer.getJobCallbackUrl(), body) - .doOnNext(resp -> logger.debug("Job subscription started OK {}", producer.getId())) + return restClient.post(producer.getJobCallbackUrl(), body) // + .retryWhen(retrySpec) // + .doOnNext(resp -> logger.debug("Job subscription {} started OK {}", eiJob.getId(), producer.getId())) // .onErrorResume(throwable -> { + producer.setJobDisabled(eiJob); logger.warn("Job subscription failed {}", producer.getId(), throwable.toString()); return Mono.empty(); - }); + }) // + .doOnNext(resp -> producer.setJobEnabled(eiJob)); } - private Collection getProducers(EiJob eiJob) { - try { - return this.eiTypes.getType(eiJob.typeId()).getProducers(); - } catch (Exception e) { - return new Vector<>(); - } + private Collection getProducersForJob(EiJob eiJob, EiProducers eiProducers) { + return eiProducers.getProducersForType(eiJob.getTypeId()); } }