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%2Fr1consumer%2FConsumerController.java;h=9de57d5bc920e8ecc655a7e496e27d4faab219eb;hb=b0612ab177e14ffa133aae2538aa504d5cc10e99;hp=9f16728b6692f65785c184fa6b8a8f8b596e3eb8;hpb=238144df7479c49c0cc1255395c8ce54d76471a3;p=nonrtric.git diff --git a/enrichment-coordinator-service/src/main/java/org/oransc/enrichment/controllers/r1consumer/ConsumerController.java b/enrichment-coordinator-service/src/main/java/org/oransc/enrichment/controllers/r1consumer/ConsumerController.java index 9f16728b..9de57d5b 100644 --- a/enrichment-coordinator-service/src/main/java/org/oransc/enrichment/controllers/r1consumer/ConsumerController.java +++ b/enrichment-coordinator-service/src/main/java/org/oransc/enrichment/controllers/r1consumer/ConsumerController.java @@ -41,15 +41,16 @@ import java.util.Collection; import java.util.List; import org.json.JSONObject; -import org.oransc.enrichment.configuration.ApplicationConfig; import org.oransc.enrichment.controllers.ErrorResponse; import org.oransc.enrichment.controllers.VoidResponse; import org.oransc.enrichment.controllers.r1producer.ProducerCallbacks; import org.oransc.enrichment.exceptions.ServiceException; 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.oransc.enrichment.repository.InfoType; +import org.oransc.enrichment.repository.InfoTypeSubscriptions; import org.oransc.enrichment.repository.InfoTypes; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -68,30 +69,29 @@ import org.springframework.web.bind.annotation.RestController; import reactor.core.publisher.Mono; @SuppressWarnings("java:S3457") // No need to call "toString()" method as formatting and string .. -@RestController("Consumer registry") +@RestController("Consumer API") @Tag(name = ConsumerConsts.CONSUMER_API_NAME) @RequestMapping(path = ConsumerConsts.API_ROOT, produces = MediaType.APPLICATION_JSON_VALUE) public class ConsumerController { private final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); - - @Autowired - ApplicationConfig applicationConfig; - - @Autowired - private InfoJobs jobs; - - @Autowired - private InfoTypes infoTypes; - - @Autowired - private InfoProducers infoProducers; - - @Autowired - ProducerCallbacks producerCallbacks; - + private final InfoJobs infoJobs; + private final InfoTypes infoTypes; + private final InfoProducers infoProducers; + private final ProducerCallbacks producerCallbacks; + private final InfoTypeSubscriptions infoTypeSubscriptions; private static Gson gson = new GsonBuilder().create(); + public ConsumerController(@Autowired InfoJobs jobs, @Autowired InfoTypes infoTypes, + @Autowired InfoProducers infoProducers, @Autowired ProducerCallbacks producerCallbacks, + @Autowired InfoTypeSubscriptions infoTypeSubscriptions) { + this.infoProducers = infoProducers; + this.infoJobs = jobs; + this.infoTypeSubscriptions = infoTypeSubscriptions; + this.infoTypes = infoTypes; + this.producerCallbacks = producerCallbacks; + } + @GetMapping(path = "/info-types", produces = MediaType.APPLICATION_JSON_VALUE) @Operation(summary = "Information type identifiers", description = "") @ApiResponses( @@ -162,15 +162,15 @@ public class ConsumerController { try { List result = new ArrayList<>(); if (owner != null) { - for (InfoJob job : this.jobs.getJobsForOwner(owner)) { + for (InfoJob job : this.infoJobs.getJobsForOwner(owner)) { if (infoTypeId == null || job.getTypeId().equals(infoTypeId)) { result.add(job.getId()); } } } else if (infoTypeId != null) { - this.jobs.getJobsForType(infoTypeId).forEach(job -> result.add(job.getId())); + this.infoJobs.getJobsForType(infoTypeId).forEach(job -> result.add(job.getId())); } else { - this.jobs.getJobs().forEach(job -> result.add(job.getId())); + this.infoJobs.getJobs().forEach(job -> result.add(job.getId())); } return new ResponseEntity<>(gson.toJson(result), HttpStatus.OK); } catch ( @@ -196,7 +196,7 @@ public class ConsumerController { public ResponseEntity getIndividualEiJob( // @PathVariable("infoJobId") String infoJobId) { try { - InfoJob job = this.jobs.getJob(infoJobId); + InfoJob job = this.infoJobs.getJob(infoJobId); return new ResponseEntity<>(gson.toJson(toInfoJobInfo(job)), HttpStatus.OK); } catch (Exception e) { return ErrorResponse.create(e, HttpStatus.NOT_FOUND); @@ -219,7 +219,7 @@ public class ConsumerController { public ResponseEntity getEiJobStatus( // @PathVariable("infoJobId") String jobId) { try { - InfoJob job = this.jobs.getJob(jobId); + InfoJob job = this.infoJobs.getJob(jobId); return new ResponseEntity<>(gson.toJson(toInfoJobStatus(job)), HttpStatus.OK); } catch (Exception e) { return ErrorResponse.create(e, HttpStatus.NOT_FOUND); @@ -256,8 +256,8 @@ public class ConsumerController { public ResponseEntity deleteIndividualEiJob( // @PathVariable("infoJobId") String jobId) { try { - InfoJob job = this.jobs.getJob(jobId); - this.jobs.remove(job, this.infoProducers); + InfoJob job = this.infoJobs.getJob(jobId); + this.infoJobs.remove(job, this.infoProducers); return new ResponseEntity<>(HttpStatus.NO_CONTENT); } catch (Exception e) { return ErrorResponse.create(e, HttpStatus.NOT_FOUND); @@ -296,15 +296,140 @@ public class ConsumerController { defaultValue = "false") boolean performTypeCheck, @RequestBody ConsumerJobInfo informationJobObject) { - final boolean isNewJob = this.jobs.get(jobId) == null; + final boolean isNewJob = this.infoJobs.get(jobId) == null; return validatePutInfoJob(jobId, informationJobObject, performTypeCheck) // .flatMap(this::startInfoSubscriptionJob) // - .doOnNext(newEiJob -> this.jobs.put(newEiJob)) // + .doOnNext(this.infoJobs::put) // .flatMap(newEiJob -> Mono.just(new ResponseEntity<>(isNewJob ? HttpStatus.CREATED : HttpStatus.OK))) .onErrorResume(throwable -> Mono.just(ErrorResponse.create(throwable, HttpStatus.NOT_FOUND))); } + @GetMapping(path = "/info-type-subscription", produces = MediaType.APPLICATION_JSON_VALUE) + @Operation( + summary = "Information type subscription identifiers", + description = "query for information type subscription identifiers") + @ApiResponses( + value = { // + @ApiResponse( + responseCode = "200", + description = "Information type subscription identifiers", // + content = @Content(array = @ArraySchema(schema = @Schema(implementation = String.class)))),}) + public ResponseEntity getInfoTypeSubscriptions( // + + @Parameter( + name = ConsumerConsts.OWNER_PARAM, + required = false, // + description = ConsumerConsts.OWNER_PARAM_DESCRIPTION) // + @RequestParam(name = ConsumerConsts.OWNER_PARAM, required = false) String owner) { + try { + List result = new ArrayList<>(); + if (owner != null) { + this.infoTypeSubscriptions.getSubscriptionsForOwner(owner) + .forEach(subscription -> result.add(subscription.getId())); + } else { + this.infoTypeSubscriptions.getAllSubscriptions() + .forEach(subscription -> result.add(subscription.getId())); + } + return new ResponseEntity<>(gson.toJson(result), HttpStatus.OK); + } catch (Exception e) { + return ErrorResponse.create(e, HttpStatus.NOT_FOUND); + } + } + + @GetMapping(path = "/info-type-subscription/{subscriptionId}", produces = MediaType.APPLICATION_JSON_VALUE) // + @Operation(summary = ConsumerConsts.INDIVIDUAL_TYPE_SUBSCRIPTION, description = "") // + @ApiResponses( + value = { // + @ApiResponse( + responseCode = "200", + description = "Type subscription", // + content = @Content(schema = @Schema(implementation = ConsumerTypeSubscriptionInfo.class))), // + @ApiResponse( + responseCode = "404", + description = "Subscription is not found", // + content = @Content(schema = @Schema(implementation = ErrorResponse.ErrorInfo.class))) // + }) + public ResponseEntity getIndividualTypeSubscription( // + @PathVariable("subscriptionId") String subscriptionId) { + try { + InfoTypeSubscriptions.SubscriptionInfo subscription = + this.infoTypeSubscriptions.getSubscription(subscriptionId); + return new ResponseEntity<>(gson.toJson(toTypeSuscriptionInfo(subscription)), HttpStatus.OK); + } catch (Exception e) { + return ErrorResponse.create(e, HttpStatus.NOT_FOUND); + } + } + + @PutMapping( + path = "/info-type-subscription/{subscriptionId}", // + produces = MediaType.APPLICATION_JSON_VALUE, // + consumes = MediaType.APPLICATION_JSON_VALUE) + @Operation( + summary = ConsumerConsts.INDIVIDUAL_TYPE_SUBSCRIPTION, + description = ConsumerConsts.TYPE_SUBSCRIPTION_DESCRIPTION) + @ApiResponses( + value = { // + @ApiResponse( + responseCode = "201", + description = "Subscription created", // + content = @Content(schema = @Schema(implementation = VoidResponse.class))), // + @ApiResponse( + responseCode = "200", + description = "Subscription updated", // + content = @Content(schema = @Schema(implementation = VoidResponse.class))) // + }) + public Mono> putIndividualTypeSubscription( // + @PathVariable("subscriptionId") String subscriptionId, // + @RequestBody ConsumerTypeSubscriptionInfo subscription) { + + final boolean isNewSubscription = this.infoTypeSubscriptions.get(subscriptionId) == null; + this.infoTypeSubscriptions.put(toTypeSuscriptionInfo(subscription, subscriptionId)); + return Mono.just(new ResponseEntity<>(isNewSubscription ? HttpStatus.CREATED : HttpStatus.OK)); + } + + @DeleteMapping(path = "/info-type-subscription/{subscriptionId}", produces = MediaType.APPLICATION_JSON_VALUE) + @Operation(summary = ConsumerConsts.INDIVIDUAL_TYPE_SUBSCRIPTION, description = "") + @ApiResponses( + value = { // + @ApiResponse( + responseCode = "200", + description = "Not used", // + content = @Content(schema = @Schema(implementation = VoidResponse.class))), + @ApiResponse( + responseCode = "204", + description = "Subscription deleted", // + content = @Content(schema = @Schema(implementation = VoidResponse.class))), + @ApiResponse( + responseCode = "404", + description = "Subscription is not found", // + content = @Content(schema = @Schema(implementation = ErrorResponse.ErrorInfo.class))) // + }) + public ResponseEntity deleteIndividualTypeSubscription( // + @PathVariable("subscriptionId") String subscriptionId) { + try { + InfoTypeSubscriptions.SubscriptionInfo subscription = + this.infoTypeSubscriptions.getSubscription(subscriptionId); + this.infoTypeSubscriptions.remove(subscription); + return new ResponseEntity<>(HttpStatus.NO_CONTENT); + } catch (Exception e) { + return ErrorResponse.create(e, HttpStatus.NOT_FOUND); + } + } + + private ConsumerTypeSubscriptionInfo toTypeSuscriptionInfo(InfoTypeSubscriptions.SubscriptionInfo s) { + return new ConsumerTypeSubscriptionInfo(s.getCallbackUrl(), s.getOwner()); + } + + private InfoTypeSubscriptions.SubscriptionInfo toTypeSuscriptionInfo(ConsumerTypeSubscriptionInfo s, + String subscriptionId) { + return InfoTypeSubscriptions.SubscriptionInfo.builder() // + .apiVersion(ConsumerCallbacks.API_VERSION) // + .owner(s.owner) // + .id(subscriptionId) // + .callbackUrl(s.statusResultUri).build(); + } + private Mono startInfoSubscriptionJob(InfoJob newInfoJob) { return this.producerCallbacks.startInfoSubscriptionJob(newInfoJob, infoProducers) // .doOnNext(noOfAcceptingProducers -> this.logger.debug("Started job {}, number of activated producers: {}", @@ -318,7 +443,7 @@ public class ConsumerController { InfoType infoType = this.infoTypes.getType(jobInfo.infoTypeId); validateJsonObjectAgainstSchema(infoType.getJobDataSchema(), jobInfo.jobDefinition); } - InfoJob existingEiJob = this.jobs.get(jobId); + InfoJob existingEiJob = this.infoJobs.get(jobId); validateUri(jobInfo.statusNotificationUri); validateUri(jobInfo.jobResultUri); @@ -370,7 +495,17 @@ public class ConsumerController { } private ConsumerInfoTypeInfo toInfoTypeInfo(InfoType type) { - return new ConsumerInfoTypeInfo(type.getJobDataSchema()); + return new ConsumerInfoTypeInfo(type.getJobDataSchema(), typeStatus(type), + this.infoProducers.getProducerIdsForType(type.getId()).size()); + } + + private ConsumerInfoTypeInfo.ConsumerTypeStatusValues typeStatus(InfoType type) { + for (InfoProducer producer : this.infoProducers.getProducersForType(type)) { + if (producer.isAvailable()) { + return ConsumerInfoTypeInfo.ConsumerTypeStatusValues.ENABLED; + } + } + return ConsumerInfoTypeInfo.ConsumerTypeStatusValues.DISABLED; } private ConsumerJobInfo toInfoJobInfo(InfoJob s) {