Merge "Fixed some sonar warnings"
[nonrtric.git] / enrichment-coordinator-service / src / main / java / org / oransc / enrichment / controllers / producer / ProducerController.java
index b88e047..c9b6467 100644 (file)
@@ -81,7 +81,7 @@ public class ProducerController {
     ProducerCallbacks producerCallbacks;
 
     @GetMapping(path = ProducerConsts.API_ROOT + "/eitypes", produces = MediaType.APPLICATION_JSON_VALUE)
-    @ApiOperation(value = "Query EI type identifiers", notes = "DETAILS TBD")
+    @ApiOperation(value = "EI type identifiers", notes = "")
     @ApiResponses(
         value = { //
             @ApiResponse(
@@ -101,7 +101,7 @@ public class ProducerController {
     }
 
     @GetMapping(path = ProducerConsts.API_ROOT + "/eitypes/{eiTypeId}", produces = MediaType.APPLICATION_JSON_VALUE)
-    @ApiOperation(value = "Definitions for an individual EI Type", notes = "Query EI type")
+    @ApiOperation(value = "Individual EI Type", notes = "")
     @ApiResponses(
         value = { //
             @ApiResponse(code = 200, message = "EI type", response = ProducerEiTypeInfo.class), //
@@ -121,7 +121,7 @@ public class ProducerController {
     }
 
     @GetMapping(path = ProducerConsts.API_ROOT + "/eiproducers", produces = MediaType.APPLICATION_JSON_VALUE)
-    @ApiOperation(value = "Query EI producer identifiers", notes = "DETAILS TBD")
+    @ApiOperation(value = "EI producer identifiers", notes = "")
     @ApiResponses(
         value = { //
             @ApiResponse(
@@ -143,10 +143,10 @@ public class ProducerController {
     @GetMapping(
         path = ProducerConsts.API_ROOT + "/eiproducers/{eiProducerId}",
         produces = MediaType.APPLICATION_JSON_VALUE)
-    @ApiOperation(value = "Definition for an individual EI producer", notes = "Query EI jobs")
+    @ApiOperation(value = "Individual EI producer", notes = "")
     @ApiResponses(
         value = { //
-            @ApiResponse(code = 200, message = "EI Jobs", response = ProducerEiTypeInfo.class), //
+            @ApiResponse(code = 200, message = "EI Jobs", response = ProducerRegistrationInfo.class), //
             @ApiResponse(
                 code = 404,
                 message = "Enrichment Information producer is not found",
@@ -165,7 +165,7 @@ public class ProducerController {
     @GetMapping(
         path = ProducerConsts.API_ROOT + "/eiproducers/{eiProducerId}/eijobs",
         produces = MediaType.APPLICATION_JSON_VALUE)
-    @ApiOperation(value = "Job definitions for an individual EI producer", notes = "Query EI producer jobs")
+    @ApiOperation(value = "EI job definitions", notes = "EI job definitions for one EI producer")
     @ApiResponses(
         value = { //
             @ApiResponse(code = 200, message = "EI jobs", response = ProducerJobInfo.class, responseContainer = "List"), //
@@ -180,7 +180,7 @@ public class ProducerController {
             Collection<ProducerJobInfo> producerJobs = new ArrayList<>();
             for (EiType type : producer.eiTypes()) {
                 for (EiJob eiJob : this.eiJobs.getJobsForType(type)) {
-                    ProducerJobInfo request = new ProducerJobInfo(eiJob.jobData(), eiJob, eiJob.type());
+                    ProducerJobInfo request = new ProducerJobInfo(eiJob);
                     producerJobs.add(request);
                 }
             }
@@ -194,7 +194,7 @@ public class ProducerController {
     @PutMapping(
         path = ProducerConsts.API_ROOT + "/eiproducers/{eiProducerId}",
         produces = MediaType.APPLICATION_JSON_VALUE)
-    @ApiOperation(value = "Individual EI producer", notes = "Put EI producer")
+    @ApiOperation(value = "Individual EI producer", notes = "")
     @ApiResponses(
         value = { //
             @ApiResponse(code = 201, message = "Producer created", response = void.class), //
@@ -204,12 +204,17 @@ public class ProducerController {
         @PathVariable("eiProducerId") String eiProducerId, //
         @RequestBody ProducerRegistrationInfo registrationInfo) {
         try {
-            final EiProducer previousDefinition = this.eiProducers.get(eiProducerId);
+            EiProducer previousDefinition = this.eiProducers.get(eiProducerId);
             if (previousDefinition != null) {
-                deregisterProducer(previousDefinition, false);
+                for (EiType type : previousDefinition.eiTypes()) {
+                    type.removeProducer(previousDefinition);
+                }
             }
 
             registerProducer(eiProducerId, registrationInfo);
+            if (previousDefinition != null) {
+                purgeTypes(previousDefinition.eiTypes());
+            }
 
             return new ResponseEntity<>(previousDefinition == null ? HttpStatus.CREATED : HttpStatus.OK);
         } catch (Exception e) {
@@ -217,10 +222,18 @@ public class ProducerController {
         }
     }
 
+    private void purgeTypes(Collection<EiType> types) {
+        for (EiType type : types) {
+            if (type.getProducerIds().isEmpty()) {
+                this.deregisterType(type);
+            }
+        }
+    }
+
     @DeleteMapping(
         path = ProducerConsts.API_ROOT + "/eiproducers/{eiProducerId}",
         produces = MediaType.APPLICATION_JSON_VALUE)
-    @ApiOperation(value = "Individual EI Producer", notes = "Delete an EI Producer")
+    @ApiOperation(value = "Individual EI producer", notes = "")
     @ApiResponses(
         value = { //
             @ApiResponse(code = 200, message = "Not used", response = void.class),
@@ -229,7 +242,7 @@ public class ProducerController {
     public ResponseEntity<Object> deleteEiProducer(@PathVariable("eiProducerId") String eiProducerId) {
         try {
             final EiProducer producer = this.eiProducers.getProducer(eiProducerId);
-            deregisterProducer(producer, true);
+            deregisterProducer(producer);
             return new ResponseEntity<>(HttpStatus.NO_CONTENT);
         } catch (Exception e) {
             return ErrorResponse.create(e, HttpStatus.NOT_FOUND);
@@ -255,7 +268,7 @@ public class ProducerController {
             .build();
     }
 
-    private void registerProducer(String producerId, ProducerRegistrationInfo registrationInfo) {
+    private EiProducer registerProducer(String producerId, ProducerRegistrationInfo registrationInfo) {
         ArrayList<EiType> types = new ArrayList<>();
         for (ProducerEiTypeRegistrationInfo typeInfo : registrationInfo.types) {
             types.add(registerType(typeInfo));
@@ -269,21 +282,26 @@ public class ProducerController {
             }
             type.addProducer(producer);
         }
+        return producer;
+    }
+
+    private void deregisterType(EiType type) {
+        this.eiTypes.remove(type);
+        for (EiJob job : this.eiJobs.getJobsForType(type.getId())) {
+            this.eiJobs.remove(job);
+            this.logger.warn("Deleted job {} because no producers left", job.id());
+        }
     }
 
-    private void deregisterProducer(EiProducer producer, boolean deleteJobs) {
+    private void deregisterProducer(EiProducer producer) {
         this.eiProducers.remove(producer);
         for (EiType type : producer.eiTypes()) {
             boolean removed = type.removeProducer(producer) != null;
             if (!removed) {
                 this.logger.error("Bug, no producer found");
             }
-            if (type.getProducerIds().isEmpty() && deleteJobs) {
-                this.eiTypes.remove(type);
-                for (EiJob job : this.eiJobs.getJobsForType(type.getId())) {
-                    this.eiJobs.remove(job);
-                    this.logger.warn("Deleted job {} because no producers left", job.id());
-                }
+            if (type.getProducerIds().isEmpty()) {
+                deregisterType(type);
             }
         }
     }