Skip to content

Commit

Permalink
Merge pull request #129 from powsybl/add-case-format-param-to-import-…
Browse files Browse the repository at this point in the history
…case

Refactor case import methods  to avoid making a request to get caseFormat
  • Loading branch information
jamal-khey authored Feb 16, 2024
2 parents 05ef43a + 40828b9 commit 8d17c44
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public class NetworkConversionController {
@PostMapping(value = "/networks")
@Operation(summary = "Get a case file from its name and import it into the store")
public ResponseEntity<NetworkInfos> importCase(@Parameter(description = "Case UUID") @RequestParam("caseUuid") UUID caseUuid,
@Parameter(description = "Case format") @RequestParam(name = "caseFormat") String caseFormat,
@Parameter(description = "Variant ID") @RequestParam(name = "variantId", required = false) String variantId,
@Parameter(description = "Report UUID") @RequestParam(value = "reportUuid", required = false) UUID reportUuid,
@Parameter(description = "Import parameters") @RequestBody(required = false) Map<String, Object> importParameters,
Expand All @@ -62,11 +63,11 @@ public ResponseEntity<NetworkInfos> importCase(@Parameter(description = "Case UU
LOGGER.debug("Importing case {} {}...", caseUuid, isAsyncRun ? "asynchronously" : "synchronously");
Map<String, Object> nonNullImportParameters = importParameters == null ? new HashMap<>() : importParameters;
if (!isAsyncRun) {
NetworkInfos networkInfos = networkConversionService.importCase(caseUuid, variantId, reportUuid, nonNullImportParameters);
NetworkInfos networkInfos = networkConversionService.importCase(caseUuid, variantId, reportUuid, caseFormat, nonNullImportParameters);
return ResponseEntity.ok().body(networkInfos);
}

networkConversionService.importCaseAsynchronously(caseUuid, variantId, reportUuid, nonNullImportParameters, receiver);
networkConversionService.importCaseAsynchronously(caseUuid, variantId, reportUuid, caseFormat, nonNullImportParameters, receiver);
return ResponseEntity.ok().build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,11 @@ static EquipmentInfos toEquipmentInfos(Identifiable<?> i, UUID networkUuid, Stri
.build();
}

void importCaseAsynchronously(UUID caseUuid, String variantId, UUID reportUuid, Map<String, Object> importParameters, String receiver) {
notificationService.emitCaseImportStart(caseUuid, variantId, reportUuid, importParameters, receiver);
void importCaseAsynchronously(UUID caseUuid, String variantId, UUID reportUuid, String caseFormat, Map<String, Object> importParameters, String receiver) {
notificationService.emitCaseImportStart(caseUuid, variantId, reportUuid, caseFormat, importParameters, receiver);
}

Map<String, String> getDefaultImportParameters(UUID caseUuid) {
CaseInfos caseInfos = getCaseInfos(caseUuid);
Map<String, String> getDefaultImportParameters(CaseInfos caseInfos) {
Importer importer = Importer.find(caseInfos.getFormat());
Map<String, String> defaultValues = new HashMap<>();
importer.getParameters()
Expand All @@ -165,22 +164,22 @@ Consumer<Message<UUID>> consumeCaseImportStart() {
rawParameters.forEach((key, value) -> changedImportParameters.put(key, value.toString()));
}

CaseInfos caseInfos = getCaseInfos(caseUuid);
Map<String, String> allImportParameters = new HashMap<>();
changedImportParameters.forEach((k, v) -> allImportParameters.put(k, v.toString()));
getDefaultImportParameters(caseUuid).forEach(allImportParameters::putIfAbsent);
CaseInfos caseInfos = getCaseInfos(caseUuid);
getDefaultImportParameters(caseInfos).forEach(allImportParameters::putIfAbsent);

try {
NetworkInfos networkInfos = importCase(caseUuid, variantId, reportUuid, changedImportParameters);
notificationService.emitCaseImportSucceeded(networkInfos, caseInfos, receiver, allImportParameters);
NetworkInfos networkInfos = importCase(caseUuid, variantId, reportUuid, caseInfos.getFormat(), changedImportParameters);
notificationService.emitCaseImportSucceeded(networkInfos, caseInfos.getName(), caseInfos.getFormat(), receiver, allImportParameters);
} catch (Exception e) {
LOGGER.error(e.getMessage(), e);
notificationService.emitCaseImportFailed(receiver, e.getMessage());
}
};
}

NetworkInfos importCase(UUID caseUuid, String variantId, UUID reportUuid, Map<String, Object> importParameters) {
NetworkInfos importCase(UUID caseUuid, String variantId, UUID reportUuid, String caseFormat, Map<String, Object> importParameters) {
CaseDataSourceClient dataSource = new CaseDataSourceClient(caseServerRest, caseUuid);

Reporter rootReporter = Reporter.NO_OP;
Expand All @@ -193,16 +192,14 @@ NetworkInfos importCase(UUID caseUuid, String variantId, UUID reportUuid, Map<St
}

AtomicReference<Long> startTime = new AtomicReference<>(System.nanoTime());
CaseInfos caseInfos = getCaseInfos(caseUuid);
String format = caseInfos.getFormat();
Network network;
Reporter finalReporter = reporter;
if (!importParameters.isEmpty()) {
Properties importProperties = new Properties();
importProperties.putAll(importParameters);
network = networkConversionObserver.observeImport(format, () -> networkStoreService.importNetwork(dataSource, finalReporter, importProperties, false));
network = networkConversionObserver.observeImport(caseFormat, () -> networkStoreService.importNetwork(dataSource, finalReporter, importProperties, false));
} else {
network = networkConversionObserver.observeImport(format, () -> networkStoreService.importNetwork(dataSource, finalReporter, false));
network = networkConversionObserver.observeImport(caseFormat, () -> networkStoreService.importNetwork(dataSource, finalReporter, false));
}
UUID networkUuid = networkStoreService.getNetworkUuid(network);
LOGGER.trace("Import network '{}' : {} seconds", networkUuid, TimeUnit.NANOSECONDS.toSeconds(System.nanoTime() - startTime.get()));
Expand Down Expand Up @@ -397,11 +394,12 @@ private static CgmesExportContext createContext(Network network) {
}

NetworkInfos importCgmesCase(UUID caseUuid, List<BoundaryInfos> boundaries) {
String caseFormat = "CGMES";
if (CollectionUtils.isEmpty(boundaries)) { // no boundaries given, standard import
return importCase(caseUuid, null, UUID.randomUUID(), new HashMap<>());
return importCase(caseUuid, null, UUID.randomUUID(), caseFormat, new HashMap<>());
} else { // import using the given boundaries
CaseDataSourceClient dataSource = new CgmesCaseDataSourceClient(caseServerRest, caseUuid, boundaries);
Network network = networkConversionObserver.observeImport("CGMES", () -> networkStoreService.importNetwork(dataSource));
Network network = networkConversionObserver.observeImport(caseFormat, () -> networkStoreService.importNetwork(dataSource));
UUID networkUuid = networkStoreService.getNetworkUuid(network);
return new NetworkInfos(networkUuid, network.getId());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
* @author Kevin Le Saulnier <kevin.lesaulnier at rte-france.com>
*/

import com.powsybl.network.conversion.server.dto.CaseInfos;
import com.powsybl.network.conversion.server.dto.NetworkInfos;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -56,21 +55,22 @@ private void sendCaseImportFailedMessage(Message<String> message) {
networkConversionPublisher.send("publishCaseImportFailed-out-0", message);
}

public void emitCaseImportStart(UUID caseUuid, String variantId, UUID reportUuid, Map<String, Object> importParameters, String receiver) {
public void emitCaseImportStart(UUID caseUuid, String variantId, UUID reportUuid, String caseFormat, Map<String, Object> importParameters, String receiver) {
sendCaseImportStartMessage(MessageBuilder.withPayload(caseUuid)
.setHeader(HEADER_VARIANT_ID, variantId)
.setHeader(HEADER_REPORT_UUID, reportUuid != null ? reportUuid.toString() : null)
.setHeader(HEADER_IMPORT_PARAMETERS, importParameters)
.setHeader(HEADER_RECEIVER, receiver)
.setHeader(HEADER_CASE_FORMAT, caseFormat)
.build());
}

public void emitCaseImportSucceeded(NetworkInfos networkInfos, CaseInfos caseInfos, String receiver, Map<String, String> importParameters) {
public void emitCaseImportSucceeded(NetworkInfos networkInfos, String caseNameStr, String caseFormatStr, String receiver, Map<String, String> importParameters) {
sendCaseImportSucceededMessage(MessageBuilder.withPayload("")
.setHeader(HEADER_NETWORK_ID, networkInfos.getNetworkId())
.setHeader(HEADER_NETWORK_UUID, networkInfos.getNetworkUuid().toString())
.setHeader(HEADER_CASE_FORMAT, caseInfos.getFormat())
.setHeader(HEADER_CASE_NAME, caseInfos.getName())
.setHeader(HEADER_CASE_FORMAT, caseFormatStr)
.setHeader(HEADER_CASE_NAME, caseNameStr)
.setHeader(HEADER_RECEIVER, receiver)
.setHeader(HEADER_IMPORT_PARAMETERS, importParameters)
.build());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,8 @@ public void test() throws Exception {
MvcResult mvcResult = mvc.perform(post("/v1/networks")
.param("caseUuid", caseUuid)
.param("reportUuid", UUID.randomUUID().toString())
.param("isAsyncRun", "false"))
.param("isAsyncRun", "false")
.param("caseFormat", "XIIDM"))
.andExpect(status().isOk())
.andReturn();

Expand All @@ -149,13 +150,15 @@ public void test() throws Exception {
.param("caseUuid", caseUuid)
.param("variantId", "first_variant_id")
.param("isAsyncRun", "false")
.param("reportUuid", UUID.randomUUID().toString()))
.param("reportUuid", UUID.randomUUID().toString())
.param("caseFormat", "XIIDM"))
.andExpect(status().isOk());
mvc.perform(post("/v1/networks")
.param("caseUuid", caseUuid)
.param("variantId", "second_variant_id")
.param("isAsyncRun", "false")
.param("reportUuid", UUID.randomUUID().toString()))
.param("reportUuid", UUID.randomUUID().toString())
.param("caseFormat", "XIIDM"))
.andExpect(status().isOk());

verify(networkStoreClient).cloneVariant(randomUuid, VariantManagerConstants.INITIAL_VARIANT_ID, "first_variant_id");
Expand Down Expand Up @@ -257,21 +260,24 @@ public void test() throws Exception {
.param("caseUuid", caseUuid)
.param("variantId", "import_params_variant_id")
.param("reportUuid", UUID.randomUUID().toString())
.param("isAsyncRun", "false"))
.param("isAsyncRun", "false")
.param("caseFormat", "XIIDM"))
.andExpect(status().isOk());

// test without report
mvc.perform(post("/v1/networks")
.param("caseUuid", caseUuid)
.param("isAsyncRun", "false"))
.param("isAsyncRun", "false")
.param("caseFormat", "XIIDM"))
.andExpect(status().isOk());

// test without report and with an error at flush
doThrow(NetworkConversionException.createFailedNetworkSaving(networkUuid, NetworkConversionException.createEquipmentTypeUnknown("?")))
.when(networkStoreClient).flush(network);
mvc.perform(post("/v1/networks")
.param("caseUuid", caseUuid)
.param("isAsyncRun", "false"))
.param("isAsyncRun", "false")
.param("caseFormat", "XIIDM"))
.andExpect(status().isInternalServerError());
}
}
Expand Down Expand Up @@ -302,7 +308,8 @@ public void testAsyncImport() throws Exception {
.param("caseUuid", caseUuid)
.param("variantId", "async_variant_id")
.param("reportUuid", UUID.randomUUID().toString())
.param("receiver", receiver))
.param("receiver", receiver)
.param("caseFormat", "XIIDM"))
.andExpect(status().isOk());

Message<byte[]> message = output.receive(1000, "case.import.succeeded");
Expand Down Expand Up @@ -337,7 +344,8 @@ public void testFailedAsyncImport() throws Exception {
.param("caseUuid", caseUuid)
.param("variantId", "async_failure_variant_id")
.param("reportUuid", UUID.randomUUID().toString())
.param("receiver", receiver))
.param("receiver", receiver)
.param("caseFormat", "XIIDM"))
.andExpect(status().isOk());

Message<byte[]> message = output.receive(1000, "case.import.failed");
Expand Down Expand Up @@ -471,7 +479,8 @@ public void testSendReport() throws Exception {
MvcResult mvcResult = mvc.perform(post("/v1/networks")
.param("caseUuid", caseUuid.toString())
.param("reportUuid", reportUuid.toString())
.param("isAsyncRun", "false"))
.param("isAsyncRun", "false")
.param("caseFormat", "XIIDM"))
.andExpect(status().isOk())
.andReturn();

Expand Down Expand Up @@ -499,7 +508,7 @@ public void testImportWithError() {
.willReturn(new ResponseEntity<>(HttpStatus.OK));
given(caseServerRest.getForEntity(eq("/v1/cases/" + caseUuid + "/infos"), any())).willReturn(ResponseEntity.ok(new CaseInfos(UUID.fromString(caseUuid.toString()), "testCase", "XIIDM")));

String message = assertThrows(NetworkConversionException.class, () -> networkConversionService.importCase(caseUuid, null, reportUuid, EMPTY_PARAMETERS)).getMessage();
String message = assertThrows(NetworkConversionException.class, () -> networkConversionService.importCase(caseUuid, null, reportUuid, "XIIDM", EMPTY_PARAMETERS)).getMessage();
assertTrue(message.contains(String.format("The save of network '%s' has failed", networkUuid)));
}

Expand All @@ -526,7 +535,7 @@ public void testFlushNetworkWithError() {
.willReturn(ResponseEntity.ok("testCase"));
given(caseServerRest.getForEntity(eq("/v1/cases/" + caseUuid + "/infos"), any())).willReturn(ResponseEntity.ok(new CaseInfos(UUID.fromString(caseUuid.toString()), "testCase", "XIIDM")));

String message = assertThrows(NetworkConversionException.class, () -> networkConversionService.importCase(caseUuid, null, reportUuid, EMPTY_PARAMETERS)).getMessage();
String message = assertThrows(NetworkConversionException.class, () -> networkConversionService.importCase(caseUuid, null, reportUuid, "XIIDM", EMPTY_PARAMETERS)).getMessage();
assertTrue(message.contains(String.format("The save of network '%s' has failed", networkUuid)));
}

Expand Down

0 comments on commit 8d17c44

Please sign in to comment.