Merge "Refactor datamodel"
[nonrtric.git] / enrichment-coordinator-service / src / main / java / org / oransc / enrichment / controllers / consumer / ConsumerCallbacks.java
index cded953..eb85d37 100644 (file)
@@ -31,8 +31,8 @@ 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.EiProducers;
 import org.oransc.enrichment.repository.EiType;
-import org.oransc.enrichment.repository.EiTypes;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -49,39 +49,53 @@ public class ConsumerCallbacks {
     private static Gson gson = new GsonBuilder().create();
 
     private final AsyncRestClient restClient;
-    private final EiTypes eiTypes;
     private final EiJobs eiJobs;
+    private final EiProducers eiProducers;
 
     @Autowired
-    public ConsumerCallbacks(ApplicationConfig config, EiTypes eiTypes, EiJobs eiJobs) {
+    public ConsumerCallbacks(ApplicationConfig config, EiJobs eiJobs, EiProducers eiProducers) {
         AsyncRestClientFactory restClientFactory = new AsyncRestClientFactory(config.getWebClientConfig());
-        this.restClient = restClientFactory.createRestClient("");
-        this.eiTypes = eiTypes;
+        this.restClient = restClientFactory.createRestClientUseHttpProxy("");
         this.eiJobs = eiJobs;
+        this.eiProducers = eiProducers;
     }
 
     public void notifyConsumersProducerDeleted(EiProducer eiProducer) {
         for (EiType type : eiProducer.getEiTypes()) {
-            if (this.eiTypes.get(type.getId()) == null) {
+            if (this.eiProducers.getProducersForType(type).isEmpty()) {
+                // No producers left for the type
                 for (EiJob job : this.eiJobs.getJobsForType(type)) {
-                    noifyJobOwner(job, new ConsumerEiJobStatus(ConsumerEiJobStatus.EiJobStatusValues.DISABLED));
+                    if (job.isLastStatusReportedEnabled()) {
+                        noifyJobOwner(job, new ConsumerEiJobStatus(ConsumerEiJobStatus.EiJobStatusValues.DISABLED));
+                        job.setLastReportedStatus(false);
+                    }
                 }
             }
         }
     }
 
+    public void notifyConsumersProducerAdded(EiProducer eiProducer) {
+        for (EiType type : eiProducer.getEiTypes()) {
+            notifyConsumersTypeAdded(type);
+        }
+    }
+
     public void notifyConsumersTypeAdded(EiType eiType) {
         for (EiJob job : this.eiJobs.getJobsForType(eiType)) {
-            noifyJobOwner(job, new ConsumerEiJobStatus(ConsumerEiJobStatus.EiJobStatusValues.ENABLED));
+            if (!job.isLastStatusReportedEnabled()) {
+                noifyJobOwner(job, new ConsumerEiJobStatus(ConsumerEiJobStatus.EiJobStatusValues.ENABLED));
+                job.setLastReportedStatus(true);
+            }
         }
     }
 
     private void noifyJobOwner(EiJob job, ConsumerEiJobStatus status) {
-        if (!job.jobStatusUrl().isEmpty()) {
+        if (!job.getJobStatusUrl().isEmpty()) {
             String body = gson.toJson(status);
-            this.restClient.post(job.jobStatusUrl(), body) //
-                .subscribe(notUsed -> logger.debug("Consumer notified OK {}", job.id()), //
-                    throwable -> logger.warn("Consumer notify failed {} {}", job.jobStatusUrl(), throwable.toString()), //
+            this.restClient.post(job.getJobStatusUrl(), body) //
+                .subscribe(notUsed -> logger.debug("Consumer notified OK {}", job.getId()), //
+                    throwable -> logger.warn("Consumer notify failed {} {}", job.getJobStatusUrl(),
+                        throwable.toString()), //
                     null);
         }
     }