|
| 1 | +/** |
| 2 | + * Copyright (c) 2024, RTE (http://www.rte-france.com) |
| 3 | + * This Source Code Form is subject to the terms of the Mozilla Public |
| 4 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 | + * file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 6 | + */ |
| 7 | +package org.gridsuite.dynamicsecurityanalysis.server.controller; |
| 8 | + |
| 9 | +import com.powsybl.ws.commons.computation.dto.ReportInfos; |
| 10 | +import io.swagger.v3.oas.annotations.Operation; |
| 11 | +import io.swagger.v3.oas.annotations.Parameter; |
| 12 | +import io.swagger.v3.oas.annotations.responses.ApiResponse; |
| 13 | +import io.swagger.v3.oas.annotations.responses.ApiResponses; |
| 14 | +import io.swagger.v3.oas.annotations.tags.Tag; |
| 15 | +import org.apache.commons.collections4.CollectionUtils; |
| 16 | +import org.gridsuite.dynamicsecurityanalysis.server.dto.DynamicSecurityAnalysisStatus; |
| 17 | +import org.gridsuite.dynamicsecurityanalysis.server.service.DynamicSecurityAnalysisResultService; |
| 18 | +import org.gridsuite.dynamicsecurityanalysis.server.service.DynamicSecurityAnalysisService; |
| 19 | +import org.gridsuite.dynamicsecurityanalysis.server.service.ParametersService; |
| 20 | +import org.gridsuite.dynamicsecurityanalysis.server.service.contexts.DynamicSecurityAnalysisRunContext; |
| 21 | +import org.springframework.http.MediaType; |
| 22 | +import org.springframework.http.ResponseEntity; |
| 23 | +import org.springframework.web.bind.annotation.*; |
| 24 | + |
| 25 | +import java.util.List; |
| 26 | +import java.util.UUID; |
| 27 | + |
| 28 | +import static com.powsybl.ws.commons.computation.service.AbstractResultContext.*; |
| 29 | +import static com.powsybl.ws.commons.computation.service.NotificationService.*; |
| 30 | +import static org.gridsuite.dynamicsecurityanalysis.server.DynamicSecurityAnalysisApi.API_VERSION; |
| 31 | +import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE; |
| 32 | +import static org.springframework.http.MediaType.TEXT_PLAIN_VALUE; |
| 33 | + |
| 34 | +/** |
| 35 | + * @author Thang PHAM <quyet-thang.pham at rte-france.com> |
| 36 | + */ |
| 37 | +@RestController |
| 38 | +@RequestMapping(value = "/" + API_VERSION) |
| 39 | +@Tag(name = "Dynamic simulation server") |
| 40 | +public class DynamicSecurityAnalysisController { |
| 41 | + |
| 42 | + private final DynamicSecurityAnalysisService dynamicSecurityAnalysisService; |
| 43 | + private final DynamicSecurityAnalysisResultService dynamicSecurityAnalysisResultService; |
| 44 | + private final ParametersService parametersService; |
| 45 | + |
| 46 | + public DynamicSecurityAnalysisController(DynamicSecurityAnalysisService dynamicSecurityAnalysisService, |
| 47 | + DynamicSecurityAnalysisResultService dynamicSecurityAnalysisResultService, |
| 48 | + ParametersService parametersService) { |
| 49 | + this.dynamicSecurityAnalysisService = dynamicSecurityAnalysisService; |
| 50 | + this.dynamicSecurityAnalysisResultService = dynamicSecurityAnalysisResultService; |
| 51 | + this.parametersService = parametersService; |
| 52 | + } |
| 53 | + |
| 54 | + @PostMapping(value = "/networks/{networkUuid}/run", produces = "application/json") |
| 55 | + @Operation(summary = "run the dynamic security analysis") |
| 56 | + @ApiResponses(value = {@ApiResponse(responseCode = "200", description = "Run dynamic simulation")}) |
| 57 | + public ResponseEntity<UUID> run(@PathVariable("networkUuid") UUID networkUuid, |
| 58 | + @RequestParam(name = VARIANT_ID_HEADER, required = false) String variantId, |
| 59 | + @RequestParam(name = HEADER_RECEIVER, required = false) String receiver, |
| 60 | + @RequestParam(name = "reportUuid", required = false) UUID reportId, |
| 61 | + @RequestParam(name = REPORTER_ID_HEADER, required = false) String reportName, |
| 62 | + @RequestParam(name = REPORT_TYPE_HEADER, required = false, defaultValue = "DynamicSecurityAnalysis") String reportType, |
| 63 | + @RequestParam(name = HEADER_PROVIDER, required = false) String provider, |
| 64 | + @RequestParam(name = "contingencyListName") List<String> contingencyListNames, |
| 65 | + @RequestParam(name = "dynamicSimulationResultUuid") UUID dynamicSimulationResultUuid, |
| 66 | + @RequestParam(name = "parametersUuid") UUID parametersUuid, |
| 67 | + @RequestHeader(HEADER_USER_ID) String userId) { |
| 68 | + |
| 69 | + DynamicSecurityAnalysisRunContext dynamicSecurityAnalysisRunContext = parametersService.createRunContext( |
| 70 | + networkUuid, |
| 71 | + variantId, |
| 72 | + receiver, |
| 73 | + provider, |
| 74 | + ReportInfos.builder().reportUuid(reportId).reporterId(reportName).computationType(reportType).build(), |
| 75 | + userId, |
| 76 | + contingencyListNames, |
| 77 | + dynamicSimulationResultUuid, |
| 78 | + parametersUuid); |
| 79 | + |
| 80 | + UUID resultUuid = dynamicSecurityAnalysisService.runAndSaveResult(dynamicSecurityAnalysisRunContext); |
| 81 | + return ResponseEntity.ok().contentType(MediaType.APPLICATION_JSON).body(resultUuid); |
| 82 | + } |
| 83 | + |
| 84 | + @GetMapping(value = "/results/{resultUuid}/status", produces = "application/json") |
| 85 | + @Operation(summary = "Get the dynamic security analysis status from the database") |
| 86 | + @ApiResponses(value = {@ApiResponse(responseCode = "200", description = "The dynamic simulation status"), |
| 87 | + @ApiResponse(responseCode = "204", description = "Dynamic security analysis status is empty"), |
| 88 | + @ApiResponse(responseCode = "404", description = "Dynamic security analysis result uuid has not been found")}) |
| 89 | + public ResponseEntity<DynamicSecurityAnalysisStatus> getStatus(@Parameter(description = "Result UUID") @PathVariable("resultUuid") UUID resultUuid) { |
| 90 | + DynamicSecurityAnalysisStatus result = dynamicSecurityAnalysisResultService.findStatus(resultUuid); |
| 91 | + return result != null ? ResponseEntity.ok().contentType(MediaType.APPLICATION_JSON).body(result) : |
| 92 | + ResponseEntity.noContent().build(); |
| 93 | + } |
| 94 | + |
| 95 | + @PutMapping(value = "/results/invalidate-status", produces = "application/json") |
| 96 | + @Operation(summary = "Invalidate the dynamic security analysis status from the database") |
| 97 | + @ApiResponses(value = {@ApiResponse(responseCode = "200", description = "The dynamic security analysis result uuids have been invalidated"), |
| 98 | + @ApiResponse(responseCode = "404", description = "Dynamic security analysis result has not been found")}) |
| 99 | + public ResponseEntity<List<UUID>> invalidateStatus(@Parameter(description = "Result UUIDs") @RequestParam("resultUuid") List<UUID> resultUuids) { |
| 100 | + List<UUID> result = dynamicSecurityAnalysisResultService.updateStatus(resultUuids, DynamicSecurityAnalysisStatus.NOT_DONE); |
| 101 | + return CollectionUtils.isEmpty(result) ? ResponseEntity.notFound().build() : |
| 102 | + ResponseEntity.ok().contentType(MediaType.APPLICATION_JSON).body(result); |
| 103 | + } |
| 104 | + |
| 105 | + @DeleteMapping(value = "/results/{resultUuid}") |
| 106 | + @Operation(summary = "Delete a dynamic security analysis result from the database") |
| 107 | + @ApiResponses(value = {@ApiResponse(responseCode = "200", description = "The dynamic simulation result has been deleted")}) |
| 108 | + public ResponseEntity<Void> deleteResult(@Parameter(description = "Result UUID") @PathVariable("resultUuid") UUID resultUuid) { |
| 109 | + dynamicSecurityAnalysisResultService.delete(resultUuid); |
| 110 | + return ResponseEntity.ok().build(); |
| 111 | + } |
| 112 | + |
| 113 | + @DeleteMapping(value = "/results", produces = APPLICATION_JSON_VALUE) |
| 114 | + @Operation(summary = "Delete all dynamic security analysis results from the database") |
| 115 | + @ApiResponses(value = {@ApiResponse(responseCode = "200", description = "All dynamic security analysis results have been deleted")}) |
| 116 | + public ResponseEntity<Void> deleteResults() { |
| 117 | + dynamicSecurityAnalysisResultService.deleteAll(); |
| 118 | + return ResponseEntity.ok().build(); |
| 119 | + } |
| 120 | + |
| 121 | + @PutMapping(value = "/results/{resultUuid}/stop", produces = APPLICATION_JSON_VALUE) |
| 122 | + @Operation(summary = "Stop a dynamic security analysis computation") |
| 123 | + @ApiResponses(value = {@ApiResponse(responseCode = "200", description = "The dynamic security analysis has been stopped")}) |
| 124 | + public ResponseEntity<Void> stop(@Parameter(description = "Result UUID") @PathVariable("resultUuid") UUID resultUuid, |
| 125 | + @Parameter(description = "Result receiver") @RequestParam(name = "receiver", required = false, defaultValue = "") String receiver) { |
| 126 | + dynamicSecurityAnalysisService.stop(resultUuid, receiver); |
| 127 | + return ResponseEntity.ok().build(); |
| 128 | + } |
| 129 | + |
| 130 | + @GetMapping(value = "/providers", produces = APPLICATION_JSON_VALUE) |
| 131 | + @Operation(summary = "Get all security analysis simulation providers") |
| 132 | + @ApiResponses(value = {@ApiResponse(responseCode = "200", description = "Dynamic security analysis providers have been found")}) |
| 133 | + public ResponseEntity<List<String>> getProviders() { |
| 134 | + return ResponseEntity.ok().contentType(MediaType.APPLICATION_JSON) |
| 135 | + .body(dynamicSecurityAnalysisService.getProviders()); |
| 136 | + } |
| 137 | + |
| 138 | + @GetMapping(value = "/default-provider", produces = TEXT_PLAIN_VALUE) |
| 139 | + @Operation(summary = "Get dynamic security analysis default provider") |
| 140 | + @ApiResponses(@ApiResponse(responseCode = "200", description = "The dynamic security analysis default provider has been found")) |
| 141 | + public ResponseEntity<String> getDefaultProvider() { |
| 142 | + return ResponseEntity.ok().body(dynamicSecurityAnalysisService.getDefaultProvider()); |
| 143 | + } |
| 144 | +} |
0 commit comments