NONRTRIC - ECS updates of the NBI
[nonrtric.git] / enrichment-coordinator-service / src / main / java / org / oransc / enrichment / tasks / ProducerSupervision.java
index 3b62fa7..db7c29b 100644 (file)
@@ -1,9 +1,9 @@
 /*-
  * ========================LICENSE_START=================================
- * ONAP : ccsdk oran
- * ======================================================================
- * Copyright (C) 2020 Nordix Foundation. All rights reserved.
- * ======================================================================
+ * O-RAN-SC
+ * %%
+ * Copyright (C) 2020 Nordix Foundation
+ * %%
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
 
 package org.oransc.enrichment.tasks;
 
-import org.oransc.enrichment.clients.AsyncRestClient;
 import org.oransc.enrichment.configuration.ApplicationConfig;
-import org.oransc.enrichment.repository.EiJobs;
-import org.oransc.enrichment.repository.EiProducer;
-import org.oransc.enrichment.repository.EiProducers;
-import org.oransc.enrichment.repository.EiTypes;
+import org.oransc.enrichment.controllers.a1e.A1eCallbacks;
+import org.oransc.enrichment.controllers.r1producer.ProducerCallbacks;
+import org.oransc.enrichment.repository.InfoJob;
+import org.oransc.enrichment.repository.InfoJobs;
+import org.oransc.enrichment.repository.InfoProducer;
+import org.oransc.enrichment.repository.InfoProducers;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -35,9 +36,10 @@ import org.springframework.stereotype.Component;
 
 import reactor.core.publisher.Flux;
 import reactor.core.publisher.Mono;
+import reactor.util.retry.Retry;
 
 /**
- * Regularly checks the availability of the EI Producers
+ * Regularly checks the availability of the Info Producers
  */
 @Component
 @EnableScheduling
@@ -45,17 +47,19 @@ import reactor.core.publisher.Mono;
 public class ProducerSupervision {
     private static final Logger logger = LoggerFactory.getLogger(ProducerSupervision.class);
 
-    @Autowired
-    ApplicationConfig applicationConfig;
-
-    @Autowired
-    EiProducers eiProducers;
+    private final InfoProducers infoProducers;
+    private final InfoJobs infoJobs;
+    private final ProducerCallbacks producerCallbacks;
+    private final A1eCallbacks consumerCallbacks;
 
     @Autowired
-    EiJobs eiJobs;
-
-    @Autowired
-    EiTypes eiTypes;
+    public ProducerSupervision(ApplicationConfig applicationConfig, InfoProducers infoProducers, InfoJobs infoJobs,
+        ProducerCallbacks producerCallbacks, A1eCallbacks consumerCallbacks) {
+        this.infoProducers = infoProducers;
+        this.infoJobs = infoJobs;
+        this.producerCallbacks = producerCallbacks;
+        this.consumerCallbacks = consumerCallbacks;
+    }
 
     @Scheduled(fixedRate = 1000 * 60 * 5)
     public void checkAllProducers() {
@@ -63,36 +67,48 @@ public class ProducerSupervision {
         createTask().subscribe(null, null, () -> logger.debug("Checking all Producers completed"));
     }
 
-    public Flux<EiProducer> createTask() {
-        return Flux.fromIterable(eiProducers.getAllProducers()) //
+    public Flux<InfoProducer> createTask() {
+        return Flux.fromIterable(infoProducers.getAllProducers()) //
             .flatMap(this::checkOneProducer);
     }
 
-    private Mono<EiProducer> checkOneProducer(EiProducer producer) {
-        return restClient().get(producer.getProducerSupervisionCallbackUrl()) //
+    private Mono<InfoProducer> checkOneProducer(InfoProducer producer) {
+        return this.producerCallbacks.healthCheck(producer) //
             .onErrorResume(throwable -> {
                 handleNonRespondingProducer(throwable, producer);
                 return Mono.empty();
             })//
             .doOnNext(response -> handleRespondingProducer(response, producer))
-            .flatMap(response -> Mono.just(producer));
+            .flatMap(response -> checkProducerJobs(producer)) //
+            .flatMap(responses -> Mono.just(producer));
     }
 
-    private void handleNonRespondingProducer(Throwable throwable, EiProducer producer) {
+    private Mono<?> checkProducerJobs(InfoProducer producer) {
+        final int MAX_CONCURRENCY = 10;
+        return getEiJobs(producer) //
+            .filter(infoJob -> !producer.isJobEnabled(infoJob)) //
+            .flatMap(infoJob -> producerCallbacks.startInfoJob(producer, infoJob, Retry.max(1)), MAX_CONCURRENCY) //
+            .collectList() //
+            .flatMapMany(startedJobs -> consumerCallbacks.notifyJobStatus(producer.getInfoTypes())) //
+            .collectList();
+    }
+
+    private Flux<InfoJob> getEiJobs(InfoProducer producer) {
+        return Flux.fromIterable(producer.getInfoTypes()) //
+            .flatMap(infoType -> Flux.fromIterable(infoJobs.getJobsForType(infoType)));
+    }
+
+    private void handleNonRespondingProducer(Throwable throwable, InfoProducer producer) {
         logger.warn("Unresponsive producer: {} exception: {}", producer.getId(), throwable.getMessage());
         producer.setAliveStatus(false);
         if (producer.isDead()) {
-            this.eiProducers.deregisterProducer(producer, this.eiTypes, this.eiJobs);
+            this.infoProducers.deregisterProducer(producer);
         }
     }
 
-    private void handleRespondingProducer(String response, EiProducer producer) {
+    private void handleRespondingProducer(String response, InfoProducer producer) {
         logger.debug("{}", response);
         producer.setAliveStatus(true);
     }
 
-    private AsyncRestClient restClient() {
-        return new AsyncRestClient("", this.applicationConfig.getWebClientConfig());
-    }
-
 }