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=94f4684fbb186649782f751745c9fbbada500678;hb=c675b454a3434d690695a7336700f78a51579400;hp=7a1bf184a267107c894f084e9d04c749e65804dd;hpb=6c468636c3790e3420da97dab19057892988fa11;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 7a1bf184..94f4684f 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 @@ -34,6 +34,8 @@ import io.swagger.v3.oas.annotations.responses.ApiResponses; import io.swagger.v3.oas.annotations.tags.Tag; import java.lang.invoke.MethodHandles; +import java.net.URI; +import java.net.URISyntaxException; import java.util.ArrayList; import java.util.List; @@ -43,11 +45,11 @@ 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.EiJob; -import org.oransc.enrichment.repository.EiJobs; -import org.oransc.enrichment.repository.EiProducers; -import org.oransc.enrichment.repository.EiType; -import org.oransc.enrichment.repository.EiTypes; +import org.oransc.enrichment.repository.InfoJob; +import org.oransc.enrichment.repository.InfoJobs; +import org.oransc.enrichment.repository.InfoProducers; +import org.oransc.enrichment.repository.InfoType; +import org.oransc.enrichment.repository.InfoTypes; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; @@ -76,13 +78,13 @@ public class ConsumerController { ApplicationConfig applicationConfig; @Autowired - private EiJobs jobs; + private InfoJobs jobs; @Autowired - private EiTypes infoTypes; + private InfoTypes infoTypes; @Autowired - private EiProducers infoProducers; + private InfoProducers infoProducers; @Autowired ProducerCallbacks producerCallbacks; @@ -101,7 +103,7 @@ public class ConsumerController { public ResponseEntity getinfoTypeIdentifiers( // ) { List result = new ArrayList<>(); - for (EiType infoType : this.infoTypes.getAllInfoTypes()) { + for (InfoType infoType : this.infoTypes.getAllInfoTypes()) { result.add(infoType.getId()); } @@ -124,7 +126,7 @@ public class ConsumerController { public ResponseEntity getInfoType( // @PathVariable("infoTypeId") String infoTypeId) { try { - EiType type = this.infoTypes.getType(infoTypeId); + InfoType type = this.infoTypes.getType(infoTypeId); ConsumerInfoTypeInfo info = toInfoTypeInfo(type); return new ResponseEntity<>(gson.toJson(info), HttpStatus.OK); } catch (Exception e) { @@ -133,7 +135,7 @@ public class ConsumerController { } @GetMapping(path = "/info-jobs", produces = MediaType.APPLICATION_JSON_VALUE) - @Operation(summary = "EI job identifiers", description = "query for information job identifiers") + @Operation(summary = "Information Job identifiers", description = "query for information job identifiers") @ApiResponses( value = { // @ApiResponse( @@ -159,7 +161,7 @@ public class ConsumerController { try { List result = new ArrayList<>(); if (owner != null) { - for (EiJob job : this.jobs.getJobsForOwner(owner)) { + for (InfoJob job : this.jobs.getJobsForOwner(owner)) { if (infoTypeId == null || job.getTypeId().equals(infoTypeId)) { result.add(job.getId()); } @@ -193,7 +195,7 @@ public class ConsumerController { public ResponseEntity getIndividualEiJob( // @PathVariable("infoJobId") String infoJobId) { try { - EiJob job = this.jobs.getJob(infoJobId); + InfoJob job = this.jobs.getJob(infoJobId); return new ResponseEntity<>(gson.toJson(toInfoJobInfo(job)), HttpStatus.OK); } catch (Exception e) { return ErrorResponse.create(e, HttpStatus.NOT_FOUND); @@ -216,14 +218,14 @@ public class ConsumerController { public ResponseEntity getEiJobStatus( // @PathVariable("infoJobId") String jobId) { try { - EiJob job = this.jobs.getJob(jobId); + InfoJob job = this.jobs.getJob(jobId); return new ResponseEntity<>(gson.toJson(toInfoJobStatus(job)), HttpStatus.OK); } catch (Exception e) { return ErrorResponse.create(e, HttpStatus.NOT_FOUND); } } - private ConsumerJobStatus toInfoJobStatus(EiJob job) { + private ConsumerJobStatus toInfoJobStatus(InfoJob job) { return this.infoProducers.isJobEnabled(job) ? new ConsumerJobStatus(ConsumerJobStatus.InfoJobStatusValues.ENABLED) : new ConsumerJobStatus(ConsumerJobStatus.InfoJobStatusValues.DISABLED); @@ -241,7 +243,8 @@ public class ConsumerController { @ApiResponse( responseCode = "204", description = "Job deleted", // - content = @Content(schema = @Schema(implementation = VoidResponse.class))), // "Individual EI job" + content = @Content(schema = @Schema(implementation = VoidResponse.class))), // "Individual + // Information Job" @ApiResponse( responseCode = "404", description = "Information subscription job is not found", // @@ -250,7 +253,7 @@ public class ConsumerController { public ResponseEntity deleteIndividualEiJob( // @PathVariable("infoJobId") String jobId) { try { - EiJob job = this.jobs.getJob(jobId); + InfoJob job = this.jobs.getJob(jobId); this.jobs.remove(job, this.infoProducers); return new ResponseEntity<>(HttpStatus.NO_CONTENT); } catch (Exception e) { @@ -262,7 +265,7 @@ public class ConsumerController { path = "/info-jobs/{infoJobId}", // produces = MediaType.APPLICATION_JSON_VALUE, // consumes = MediaType.APPLICATION_JSON_VALUE) - @Operation(summary = ConsumerConsts.INDIVIDUAL_JOB) + @Operation(summary = ConsumerConsts.INDIVIDUAL_JOB, description = ConsumerConsts.PUT_INDIVIDUAL_JOB_DESCRIPTION) @ApiResponses( value = { // @ApiResponse( @@ -275,44 +278,65 @@ public class ConsumerController { content = @Content(schema = @Schema(implementation = VoidResponse.class))), // @ApiResponse( responseCode = "404", - description = "Enrichment Information type is not found", // + description = "Information type is not found", // content = @Content(schema = @Schema(implementation = ErrorResponse.ErrorInfo.class))) // }) public Mono> putIndividualInfoJob( // @PathVariable("infoJobId") String jobId, // + @Parameter( + name = ConsumerConsts.PERFORM_TYPE_CHECK_PARAM, + required = false, // + description = ConsumerConsts.PERFORM_TYPE_CHECK_PARAM_DESCRIPTION) // + @RequestParam( + name = ConsumerConsts.PERFORM_TYPE_CHECK_PARAM, + required = false, + defaultValue = "false") boolean performTypeCheck, @RequestBody ConsumerJobInfo informationJobObject) { final boolean isNewJob = this.jobs.get(jobId) == null; - return validatePutInfoJob(jobId, informationJobObject) // + return validatePutInfoJob(jobId, informationJobObject, performTypeCheck) // .flatMap(this::startInfoSubscriptionJob) // .doOnNext(newEiJob -> this.jobs.put(newEiJob)) // .flatMap(newEiJob -> Mono.just(new ResponseEntity<>(isNewJob ? HttpStatus.CREATED : HttpStatus.OK))) .onErrorResume(throwable -> Mono.just(ErrorResponse.create(throwable, HttpStatus.NOT_FOUND))); } - private Mono startInfoSubscriptionJob(EiJob newInfoJob) { + private Mono startInfoSubscriptionJob(InfoJob newInfoJob) { return this.producerCallbacks.startInfoSubscriptionJob(newInfoJob, infoProducers) // .doOnNext(noOfAcceptingProducers -> this.logger.debug("Started job {}, number of activated producers: {}", newInfoJob.getId(), noOfAcceptingProducers)) // .flatMap(noOfAcceptingProducers -> Mono.just(newInfoJob)); } - private Mono validatePutInfoJob(String jobId, ConsumerJobInfo jobInfo) { + private Mono validatePutInfoJob(String jobId, ConsumerJobInfo jobInfo, boolean performTypeCheck) { try { - EiType infoType = this.infoTypes.getType(jobInfo.infoTypeId); - validateJsonObjectAgainstSchema(infoType.getJobDataSchema(), jobInfo.jobDefinition); - EiJob existingEiJob = this.jobs.get(jobId); + if (performTypeCheck) { + InfoType infoType = this.infoTypes.getType(jobInfo.infoTypeId); + validateJsonObjectAgainstSchema(infoType.getJobDataSchema(), jobInfo.jobDefinition); + } + InfoJob existingEiJob = this.jobs.get(jobId); + validateUri(jobInfo.statusNotificationUri); + validateUri(jobInfo.jobResultUri); if (existingEiJob != null && !existingEiJob.getTypeId().equals(jobInfo.infoTypeId)) { throw new ServiceException("Not allowed to change type for existing job", HttpStatus.CONFLICT); } - return Mono.just(toEiJob(jobInfo, jobId, infoType)); + return Mono.just(toEiJob(jobInfo, jobId, jobInfo.infoTypeId)); } catch (Exception e) { return Mono.error(e); } } + private void validateUri(String url) throws URISyntaxException, ServiceException { + if (url != null && !url.isEmpty()) { + URI uri = new URI(url); + if (!uri.isAbsolute()) { + throw new ServiceException("URI: " + url + " is not absolute", HttpStatus.CONFLICT); + } + } + } + private void validateJsonObjectAgainstSchema(Object schemaObj, Object object) throws ServiceException { if (schemaObj != null) { // schema is optional for now try { @@ -331,10 +355,10 @@ public class ConsumerController { } } - private EiJob toEiJob(ConsumerJobInfo info, String id, EiType type) { - return EiJob.builder() // + private InfoJob toEiJob(ConsumerJobInfo info, String id, String typeId) { + return InfoJob.builder() // .id(id) // - .typeId(type.getId()) // + .typeId(typeId) // .owner(info.owner) // .jobData(info.jobDefinition) // .targetUrl(info.jobResultUri) // @@ -342,11 +366,11 @@ public class ConsumerController { .build(); } - private ConsumerInfoTypeInfo toInfoTypeInfo(EiType type) { + private ConsumerInfoTypeInfo toInfoTypeInfo(InfoType type) { return new ConsumerInfoTypeInfo(type.getJobDataSchema()); } - private ConsumerJobInfo toInfoJobInfo(EiJob s) { + private ConsumerJobInfo toInfoJobInfo(InfoJob s) { return new ConsumerJobInfo(s.getTypeId(), s.getJobData(), s.getOwner(), s.getTargetUrl(), s.getJobStatusUrl()); } }