Skip to content

Commit

Permalink
Merge branch 'main' into dbraquart/use-study-config-server
Browse files Browse the repository at this point in the history
  • Loading branch information
dbraquart authored Dec 16, 2024
2 parents 4242435 + 2737564 commit 4a87bdf
Show file tree
Hide file tree
Showing 7 changed files with 206 additions and 42 deletions.
16 changes: 15 additions & 1 deletion src/main/java/org/gridsuite/study/server/StudyController.java
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,21 @@ public ResponseEntity<RootNetworkCreationRequestInfos> createRootNetwork(@PathVa
@RequestParam(value = CASE_FORMAT) String caseFormat,
@RequestBody(required = false) Map<String, Object> importParameters,
@RequestHeader(HEADER_USER_ID) String userId) {
return ResponseEntity.ok().body(studyService.createRootNetwork(studyUuid, caseUuid, caseFormat, importParameters, userId));
return ResponseEntity.ok().body(studyService.createRootNetworkRequest(studyUuid, caseUuid, caseFormat, importParameters, userId));
}

@PutMapping(value = "/studies/{studyUuid}/root-networks/{rootNetworkUuid}")
@Operation(summary = "update root network case")
@ApiResponses(value = {@ApiResponse(responseCode = "200", description = "The case is updated for a root network")})
public ResponseEntity<Void> updateRootNetworkCase(@PathVariable("studyUuid") UUID studyUuid,
@PathVariable("rootNetworkUuid") UUID rootNetworkUuid,
@RequestParam(value = CASE_UUID) UUID caseUuid,
@RequestParam(value = CASE_FORMAT) String caseFormat,
@RequestBody(required = false) Map<String, Object> importParameters,
@RequestHeader(HEADER_USER_ID) String userId) {
caseService.assertCaseExists(caseUuid);
studyService.updateNetworkRequest(studyUuid, rootNetworkUuid, caseUuid, caseFormat, importParameters, userId);
return ResponseEntity.ok().build();
}

@DeleteMapping(value = "/studies/{studyUuid}/root-networks/{rootNetworkUuid}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
public enum CaseImportAction {
STUDY_CREATION("Study creation"),
ROOT_NETWORK_CREATION("Root network creation"),
NETWORK_RECREATION("Network recreation");
NETWORK_RECREATION("Network recreation"),
ROOT_NETWORK_MODIFICATION("Root network modification");

@Getter
public final String label;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,12 @@ public Consumer<Message<NetworkModificationResult>> consumeBuildResult() {
try {
NetworkModificationResult networkModificationResult = message.getPayload();
receiverObj = objectMapper.readValue(URLDecoder.decode(receiver, StandardCharsets.UTF_8),
NodeReceiver.class);
NodeReceiver.class);

LOGGER.info("Build completed for node '{}'", receiverObj.getNodeUuid());

networkModificationTreeService.updateNodeBuildStatus(receiverObj.getNodeUuid(), receiverObj.getRootNetworkUuid(),
NodeBuildStatus.from(networkModificationResult.getLastGroupApplicationStatus(), networkModificationResult.getApplicationStatus()));
NodeBuildStatus.from(networkModificationResult.getLastGroupApplicationStatus(), networkModificationResult.getApplicationStatus()));

UUID studyUuid = networkModificationTreeService.getStudyUuidForNodeId(receiverObj.getNodeUuid());
notificationService.emitStudyChanged(studyUuid, receiverObj.getNodeUuid(), NotificationService.UPDATE_TYPE_BUILD_COMPLETED, networkModificationResult.getImpactedSubstationsIds());
Expand All @@ -132,7 +132,7 @@ public Consumer<Message<String>> consumeBuildStopped() {
NodeReceiver receiverObj;
try {
receiverObj = objectMapper.readValue(URLDecoder.decode(receiver, StandardCharsets.UTF_8),
NodeReceiver.class);
NodeReceiver.class);

LOGGER.info("Build stopped for node '{}'", receiverObj.getNodeUuid());

Expand All @@ -156,7 +156,7 @@ public Consumer<Message<String>> consumeBuildFailed() {
NodeReceiver receiverObj;
try {
receiverObj = objectMapper.readValue(URLDecoder.decode(receiver, StandardCharsets.UTF_8),
NodeReceiver.class);
NodeReceiver.class);

LOGGER.info("Build failed for node '{}'", receiverObj.getNodeUuid());

Expand Down Expand Up @@ -220,7 +220,15 @@ public Consumer<Message<String>> consumeCaseImportSucceeded() {
.importParameters(importParameters)
.build());
case NETWORK_RECREATION ->
studyService.updateNetwork(studyUuid, rootNetworkUuid, userId, networkInfos);
studyService.updateNetwork(studyUuid, rootNetworkUuid, networkInfos, userId);
case ROOT_NETWORK_MODIFICATION ->
studyService.updateNetwork(studyUuid, rootNetworkUuid, RootNetworkInfos.builder()
.id(rootNetworkUuid)
.networkInfos(networkInfos)
.caseInfos(caseInfos)
.importParameters(importParameters)
.reportUuid(importReportUuid)
.build());
}
caseService.disableCaseExpiration(caseUuid);
} catch (Exception e) {
Expand All @@ -230,8 +238,8 @@ public Consumer<Message<String>> consumeCaseImportSucceeded() {
if (caseImportAction == CaseImportAction.STUDY_CREATION) {
studyService.deleteStudyIfNotCreationInProgress(studyUuid, userId);
}
LOGGER.trace(caseImportAction.getLabel() + " for study uuid '{}' : {} seconds", studyUuid,
TimeUnit.NANOSECONDS.toSeconds(System.nanoTime() - startTime));
LOGGER.trace("{} for study uuid '{}' : {} seconds", caseImportAction.getLabel(), studyUuid,
TimeUnit.NANOSECONDS.toSeconds(System.nanoTime() - startTime));
}
}
};
Expand Down Expand Up @@ -383,7 +391,7 @@ public Consumer<Message<String>> consumeCaseImportFailed() {
CaseImportReceiver receiver;
try {
receiver = objectMapper.readValue(URLDecoder.decode(receiverString, StandardCharsets.UTF_8),
CaseImportReceiver.class);
CaseImportReceiver.class);
UUID studyUuid = receiver.getStudyUuid();
String userId = receiver.getUserId();

Expand Down Expand Up @@ -425,11 +433,11 @@ public void consumeCalculationFailed(Message<String> msg, ComputationType comput
UUID studyUuid = networkModificationTreeService.getStudyUuidForNodeId(receiverObj.getNodeUuid());
// send notification for failed computation
notificationService.emitStudyError(
studyUuid,
receiverObj.getNodeUuid(),
computationType.getUpdateFailedType(),
errorMessage,
userId);
studyUuid,
receiverObj.getNodeUuid(),
computationType.getUpdateFailedType(),
errorMessage,
userId);
} catch (JsonProcessingException e) {
LOGGER.error(e.toString());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.util.stream.Collectors;

import static org.gridsuite.study.server.StudyException.Type.DELETE_ROOT_NETWORK_FAILED;
import static org.gridsuite.study.server.StudyException.Type.ROOT_NETWORK_NOT_FOUND;

/**
* @author Le Saulnier Kevin <lesaulnier.kevin at rte-france.com>
Expand Down Expand Up @@ -81,6 +82,38 @@ public boolean exists(UUID rootNetworkUuid) {
return rootNetworkRepository.existsById(rootNetworkUuid);
}

public void updateNetwork(@NonNull RootNetworkEntity rootNetworkEntity, @NonNull NetworkInfos networkInfos) {
updateNetworkInfos(rootNetworkEntity, networkInfos);
}

public void updateNetwork(@NonNull RootNetworkInfos rootNetworkInfos) {
RootNetworkEntity rootNetworkEntity = getRootNetwork(rootNetworkInfos.getId()).orElseThrow(() -> new StudyException(ROOT_NETWORK_NOT_FOUND));
UUID oldCaseUuid = rootNetworkEntity.getCaseUuid();
updateNetwork(rootNetworkEntity, rootNetworkInfos);

//delete old case
caseService.deleteCase(oldCaseUuid);
}

private void updateNetwork(RootNetworkEntity rootNetworkEntity, RootNetworkInfos rootNetworkInfos) {
updateCaseInfos(rootNetworkEntity, rootNetworkInfos.getCaseInfos());
updateNetworkInfos(rootNetworkEntity, rootNetworkInfos.getNetworkInfos());

rootNetworkEntity.setImportParameters(rootNetworkInfos.getImportParameters());
rootNetworkEntity.setReportUuid(rootNetworkInfos.getReportUuid());
}

private void updateCaseInfos(@NonNull RootNetworkEntity rootNetworkEntity, @NonNull CaseInfos caseInfos) {
rootNetworkEntity.setCaseUuid(caseInfos.getCaseUuid());
rootNetworkEntity.setCaseFormat(caseInfos.getCaseFormat());
rootNetworkEntity.setCaseName(caseInfos.getCaseName());
}

private void updateNetworkInfos(@NonNull RootNetworkEntity rootNetworkEntity, @NonNull NetworkInfos networkInfos) {
rootNetworkEntity.setNetworkId(networkInfos.getNetworkId());
rootNetworkEntity.setNetworkUuid(networkInfos.getNetworkUuid());
}

@Transactional
public RootNetworkEntity createRootNetwork(@NonNull StudyEntity studyEntity, @NonNull RootNetworkInfos rootNetworkInfos) {
RootNetworkEntity rootNetworkEntity = rootNetworkRepository.save(rootNetworkInfos.toEntity());
Expand All @@ -100,11 +133,11 @@ public Optional<RootNetworkEntity> getRootNetwork(UUID rootNetworkUuid) {
}

public String getCaseName(UUID rootNetworkUuid) {
return getRootNetwork(rootNetworkUuid).map(RootNetworkEntity::getCaseName).orElseThrow(() -> new StudyException(StudyException.Type.ROOT_NETWORK_NOT_FOUND));
return getRootNetwork(rootNetworkUuid).map(RootNetworkEntity::getCaseName).orElseThrow(() -> new StudyException(ROOT_NETWORK_NOT_FOUND));
}

public Map<String, String> getImportParameters(UUID rootNetworkUuid) {
return rootNetworkRepository.findWithImportParametersById(rootNetworkUuid).map(RootNetworkEntity::getImportParameters).orElseThrow(() -> new StudyException(StudyException.Type.ROOT_NETWORK_NOT_FOUND));
return rootNetworkRepository.findWithImportParametersById(rootNetworkUuid).map(RootNetworkEntity::getImportParameters).orElseThrow(() -> new StudyException(ROOT_NETWORK_NOT_FOUND));
}

public List<RootNetworkEntity> getStudyRootNetworks(UUID studyUuid) {
Expand All @@ -115,16 +148,6 @@ public List<RootNetworkInfos> getStudyRootNetworkInfosWithRootNetworkNodeInfos(U
return rootNetworkRepository.findAllWithInfosByStudyId(studyUuid).stream().map(RootNetworkEntity::toDto).toList();
}

@Transactional
public void updateNetwork(RootNetworkEntity rootNetworkEntity, NetworkInfos networkInfos) {
if (networkInfos != null) {
rootNetworkEntity.setNetworkId(networkInfos.getNetworkId());
rootNetworkEntity.setNetworkUuid(networkInfos.getNetworkUuid());

rootNetworkRepository.save(rootNetworkEntity);
}
}

@Transactional
public void duplicateStudyRootNetworks(StudyEntity newStudyEntity, UUID sourceStudyUuid) {
List<RootNetworkEntity> rootNetworkEntities = rootNetworkRepository.findAllWithInfosByStudyId(sourceStudyUuid);
Expand Down Expand Up @@ -158,7 +181,7 @@ public RootNetworkCreationRequestEntity insertCreationRequest(UUID rootNetworkIn

public void assertIsRootNetworkInStudy(UUID rootNetworkUuid, UUID studyUuid) {
if (!rootNetworkRepository.existsByIdAndStudyId(rootNetworkUuid, studyUuid)) {
throw new StudyException(StudyException.Type.ROOT_NETWORK_NOT_FOUND);
throw new StudyException(ROOT_NETWORK_NOT_FOUND);
}
}

Expand All @@ -168,7 +191,7 @@ public void assertIsRootNetworkInStudy(UUID rootNetworkUuid, UUID studyUuid) {
* @param rootNetworkUuid
*/
public void delete(UUID rootNetworkUuid) {
delete(rootNetworkRepository.findWithRootNetworkNodeInfosById(rootNetworkUuid).orElseThrow(() -> new StudyException(StudyException.Type.ROOT_NETWORK_NOT_FOUND)).toDto());
delete(rootNetworkRepository.findWithRootNetworkNodeInfosById(rootNetworkUuid).orElseThrow(() -> new StudyException(ROOT_NETWORK_NOT_FOUND)).toDto());
}

public void delete(RootNetworkInfos rootNetworkInfos) {
Expand Down
20 changes: 18 additions & 2 deletions src/main/java/org/gridsuite/study/server/service/StudyService.java
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ public void deleteRootNetwork(UUID studyUuid, UUID rootNetworkUuid, String userI
}

@Transactional
public RootNetworkCreationRequestInfos createRootNetwork(UUID studyUuid, UUID caseUuid, String caseFormat, Map<String, Object> importParameters, String userId) {
public RootNetworkCreationRequestInfos createRootNetworkRequest(UUID studyUuid, UUID caseUuid, String caseFormat, Map<String, Object> importParameters, String userId) {
StudyEntity studyEntity = studyRepository.findById(studyUuid).orElseThrow(() -> new StudyException(STUDY_NOT_FOUND));

UUID importReportUuid = UUID.randomUUID();
Expand Down Expand Up @@ -319,6 +319,22 @@ public void createRootNetwork(@NonNull UUID studyUuid, @NonNull RootNetworkInfos
}
}

@Transactional(readOnly = true)
public void updateNetworkRequest(UUID studyUuid, UUID rootNetworkUuid, UUID caseUuid, String caseFormat, Map<String, Object> importParameters, String userId) {
UUID importReportUuid = UUID.randomUUID();
persistNetwork(caseUuid, studyUuid, rootNetworkUuid, null, userId, importReportUuid, caseFormat, importParameters, CaseImportAction.ROOT_NETWORK_MODIFICATION);
}

@Transactional
public void updateNetwork(UUID studyUuid, UUID rootNetworkUuid, RootNetworkInfos rootNetworkInfos) {
// Update case for a given root network
rootNetworkService.updateNetwork(rootNetworkInfos);

// Invalidate nodes of the updated root network
UUID rootNodeUuid = networkModificationTreeService.getStudyRootNodeUuid(studyUuid);
invalidateBuild(studyUuid, rootNodeUuid, rootNetworkUuid, false, false, true);
}

/**
* Recreates study network from <caseUuid> and <importParameters>
* @param caseUuid
Expand Down Expand Up @@ -510,7 +526,7 @@ public CreatedStudyBasicInfos insertStudy(UUID studyUuid, String userId, Network
}

@Transactional
public CreatedStudyBasicInfos updateNetwork(UUID studyUuid, UUID rootNetworkUuid, String userId, NetworkInfos networkInfos) {
public CreatedStudyBasicInfos updateNetwork(UUID studyUuid, UUID rootNetworkUuid, NetworkInfos networkInfos, String userId) {
StudyEntity studyEntity = studyRepository.findById(studyUuid).orElseThrow(() -> new StudyException(STUDY_NOT_FOUND));
RootNetworkEntity rootNetworkEntity = rootNetworkService.getRootNetwork(rootNetworkUuid).orElseThrow(() -> new StudyException(StudyException.Type.ROOT_NETWORK_NOT_FOUND));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import org.gridsuite.study.server.repository.rootnetwork.RootNetworkRepository;
import org.gridsuite.study.server.repository.voltageinit.StudyVoltageInitParametersEntity;
import org.gridsuite.study.server.service.NetworkService;
import org.gridsuite.study.server.service.RootNetworkNodeInfoService;
import org.gridsuite.study.server.utils.elasticsearch.DisableElasticsearch;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
Expand Down Expand Up @@ -96,8 +95,6 @@ class NetworkModificationUnitTest {
private RootNetworkNodeInfoRepository rootNetworkNodeInfoRepository;
@Autowired
private RootNetworkRepository rootNetworkRepository;
@Autowired
private RootNetworkNodeInfoService rootNetworkNodeInfoService;

@BeforeEach
void setup() {
Expand Down
Loading

0 comments on commit 4a87bdf

Please sign in to comment.