NONRTRIC - Enrichment Coordinator Service, Changed error codes
[nonrtric.git] / enrichment-coordinator-service / src / main / java / org / oransc / enrichment / controllers / a1e / A1eController.java
index 34d9725..9609e27 100644 (file)
@@ -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 A1eController {
     ApplicationConfig applicationConfig;
 
     @Autowired
-    private EiJobs eiJobs;
+    private InfoJobs eiJobs;
 
     @Autowired
-    private EiTypes eiTypes;
+    private InfoTypes eiTypes;
 
     @Autowired
-    private EiProducers eiProducers;
+    private InfoProducers infoProducers;
 
     @Autowired
     ProducerCallbacks producerCallbacks;
@@ -101,7 +103,7 @@ public class A1eController {
     public ResponseEntity<Object> getEiTypeIdentifiers( //
     ) {
         List<String> result = new ArrayList<>();
-        for (EiType eiType : this.eiTypes.getAllInfoTypes()) {
+        for (InfoType eiType : this.eiTypes.getAllInfoTypes()) {
             result.add(eiType.getId());
         }
 
@@ -159,7 +161,7 @@ public class A1eController {
         try {
             List<String> result = new ArrayList<>();
             if (owner != null) {
-                for (EiJob job : this.eiJobs.getJobsForOwner(owner)) {
+                for (InfoJob job : this.eiJobs.getJobsForOwner(owner)) {
                     if (eiTypeId == null || job.getTypeId().equals(eiTypeId)) {
                         result.add(job.getId());
                     }
@@ -193,7 +195,7 @@ public class A1eController {
     public ResponseEntity<Object> getIndividualEiJob( //
         @PathVariable("eiJobId") String eiJobId) {
         try {
-            EiJob job = this.eiJobs.getJob(eiJobId);
+            InfoJob job = this.eiJobs.getJob(eiJobId);
             return new ResponseEntity<>(gson.toJson(toEiJobInfo(job)), HttpStatus.OK);
         } catch (Exception e) {
             return ErrorResponse.create(e, HttpStatus.NOT_FOUND);
@@ -216,15 +218,15 @@ public class A1eController {
     public ResponseEntity<Object> getEiJobStatus( //
         @PathVariable("eiJobId") String eiJobId) {
         try {
-            EiJob job = this.eiJobs.getJob(eiJobId);
+            InfoJob job = this.eiJobs.getJob(eiJobId);
             return new ResponseEntity<>(gson.toJson(toEiJobStatus(job)), HttpStatus.OK);
         } catch (Exception e) {
             return ErrorResponse.create(e, HttpStatus.NOT_FOUND);
         }
     }
 
-    private A1eEiJobStatus toEiJobStatus(EiJob job) {
-        return this.eiProducers.isJobEnabled(job) ? new A1eEiJobStatus(A1eEiJobStatus.EiJobStatusValues.ENABLED)
+    private A1eEiJobStatus toEiJobStatus(InfoJob job) {
+        return this.infoProducers.isJobEnabled(job) ? new A1eEiJobStatus(A1eEiJobStatus.EiJobStatusValues.ENABLED)
             : new A1eEiJobStatus(A1eEiJobStatus.EiJobStatusValues.DISABLED);
 
     }
@@ -249,8 +251,8 @@ public class A1eController {
     public ResponseEntity<Object> deleteIndividualEiJob( //
         @PathVariable("eiJobId") String eiJobId) {
         try {
-            EiJob job = this.eiJobs.getJob(eiJobId);
-            this.eiJobs.remove(job, this.eiProducers);
+            InfoJob job = this.eiJobs.getJob(eiJobId);
+            this.eiJobs.remove(job, this.infoProducers);
             return new ResponseEntity<>(HttpStatus.NO_CONTENT);
         } catch (Exception e) {
             return ErrorResponse.create(e, HttpStatus.NOT_FOUND);
@@ -275,8 +277,18 @@ public class A1eController {
             @ApiResponse(
                 responseCode = "404",
                 description = "Enrichment Information type is not found", //
-                content = @Content(schema = @Schema(implementation = ErrorResponse.ErrorInfo.class))) //
+                content = @Content(schema = @Schema(implementation = ErrorResponse.ErrorInfo.class))),
+            @ApiResponse(
+                responseCode = "400",
+                description = "Input validation failed", //
+                content = @Content(schema = @Schema(implementation = ErrorResponse.ErrorInfo.class))), //
+            @ApiResponse(
+                responseCode = "409",
+                description = "Cannot modify job type", //
+                content = @Content(schema = @Schema(implementation = ErrorResponse.ErrorInfo.class)))
+
         })
+
     public Mono<ResponseEntity<Object>> putIndividualEiJob( //
         @PathVariable("eiJobId") String eiJobId, //
         @RequestBody A1eEiJobInfo eiJobObject) {
@@ -287,21 +299,23 @@ public class A1eController {
             .flatMap(this::startEiJob) //
             .doOnNext(newEiJob -> this.eiJobs.put(newEiJob)) //
             .flatMap(newEiJob -> Mono.just(new ResponseEntity<>(isNewJob ? HttpStatus.CREATED : HttpStatus.OK)))
-            .onErrorResume(throwable -> Mono.just(ErrorResponse.create(throwable, HttpStatus.NOT_FOUND)));
+            .onErrorResume(throwable -> Mono.just(ErrorResponse.create(throwable, HttpStatus.INTERNAL_SERVER_ERROR)));
     }
 
-    private Mono<EiJob> startEiJob(EiJob newEiJob) {
-        return this.producerCallbacks.startInfoSubscriptionJob(newEiJob, eiProducers) //
+    private Mono<InfoJob> startEiJob(InfoJob newEiJob) {
+        return this.producerCallbacks.startInfoSubscriptionJob(newEiJob, infoProducers) //
             .doOnNext(noOfAcceptingProducers -> this.logger.debug(
                 "Started EI job {}, number of activated producers: {}", newEiJob.getId(), noOfAcceptingProducers)) //
             .flatMap(noOfAcceptingProducers -> Mono.just(newEiJob));
     }
 
-    private Mono<EiJob> validatePutEiJob(String eiJobId, A1eEiJobInfo eiJobInfo) {
+    private Mono<InfoJob> validatePutEiJob(String eiJobId, A1eEiJobInfo eiJobInfo) {
         try {
-            EiType eiType = this.eiTypes.getType(eiJobInfo.eiTypeId);
+            InfoType eiType = this.eiTypes.getType(eiJobInfo.eiTypeId);
             validateJsonObjectAgainstSchema(eiType.getJobDataSchema(), eiJobInfo.jobDefinition);
-            EiJob existingEiJob = this.eiJobs.get(eiJobId);
+            InfoJob existingEiJob = this.eiJobs.get(eiJobId);
+            validateUri(eiJobInfo.jobResultUri);
+            validateUri(eiJobInfo.statusNotificationUri);
 
             if (existingEiJob != null && !existingEiJob.getTypeId().equals(eiJobInfo.eiTypeId)) {
                 throw new ServiceException("Not allowed to change type for existing EI job", HttpStatus.CONFLICT);
@@ -312,6 +326,15 @@ public class A1eController {
         }
     }
 
+    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.BAD_REQUEST);
+            }
+        }
+    }
+
     private void validateJsonObjectAgainstSchema(Object schemaObj, Object object) throws ServiceException {
         if (schemaObj != null) { // schema is optional for now
             try {
@@ -319,19 +342,19 @@ public class A1eController {
 
                 String schemaAsString = mapper.writeValueAsString(schemaObj);
                 JSONObject schemaJSON = new JSONObject(schemaAsString);
-                org.everit.json.schema.Schema schema = org.everit.json.schema.loader.SchemaLoader.load(schemaJSON);
+                var schema = org.everit.json.schema.loader.SchemaLoader.load(schemaJSON);
 
                 String objectAsString = mapper.writeValueAsString(object);
                 JSONObject json = new JSONObject(objectAsString);
                 schema.validate(json);
             } catch (Exception e) {
-                throw new ServiceException("Json validation failure " + e.toString(), HttpStatus.CONFLICT);
+                throw new ServiceException("Json validation failure " + e.toString(), HttpStatus.BAD_REQUEST);
             }
         }
     }
 
-    private EiJob toEiJob(A1eEiJobInfo info, String id, EiType type) {
-        return EiJob.builder() //
+    private InfoJob toEiJob(A1eEiJobInfo info, String id, InfoType type) {
+        return InfoJob.builder() //
             .id(id) //
             .typeId(type.getId()) //
             .owner(info.owner) //
@@ -345,7 +368,7 @@ public class A1eController {
         return new A1eEiTypeInfo();
     }
 
-    private A1eEiJobInfo toEiJobInfo(EiJob s) {
+    private A1eEiJobInfo toEiJobInfo(InfoJob s) {
         return new A1eEiJobInfo(s.getTypeId(), s.getJobData(), s.getOwner(), s.getTargetUrl(), s.getJobStatusUrl());
     }
 }