Skip to content

Commit

Permalink
Merge branch 'root-network-creation' of https://github.com/powsybl/po…
Browse files Browse the repository at this point in the history
…wsybl-study-server into root-network-modification-case
  • Loading branch information
Slimane AMAR committed Dec 12, 2024
2 parents 964f90e + 9edc642 commit 8c3209e
Show file tree
Hide file tree
Showing 19 changed files with 200 additions and 162 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

<groupId>org.gridsuite</groupId>
<artifactId>gridsuite-study-server</artifactId>
<version>2.10.0-SNAPSHOT</version>
<version>2.11.0-SNAPSHOT</version>

<packaging>jar</packaging>
<name>Study Server</name>
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/org/gridsuite/study/server/StudyController.java
Original file line number Diff line number Diff line change
Expand Up @@ -270,12 +270,12 @@ public ResponseEntity<Void> checkNetworkExistence(@PathVariable("studyUuid") UUI
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "Study network recreation has started"),
@ApiResponse(responseCode = "424", description = "The case doesn't exist")})
public ResponseEntity<BasicStudyInfos> recreateStudyNetworkFromCase(@PathVariable("studyUuid") UUID studyUuid,
public ResponseEntity<BasicStudyInfos> recreateNetworkFromCase(@PathVariable("studyUuid") UUID studyUuid,
@RequestBody(required = false) Map<String, Object> importParameters,
@RequestParam(value = "caseUuid") UUID caseUuid,
@Parameter(description = "case format") @RequestParam(name = "caseFormat", required = false) String caseFormat,
@RequestHeader(HEADER_USER_ID) String userId) {
studyService.recreateStudyRootNetwork(caseUuid, userId, studyUuid, studyService.getStudyFirstRootNetworkUuid(studyUuid), caseFormat, importParameters);
studyService.recreateNetwork(caseUuid, userId, studyUuid, studyService.getStudyFirstRootNetworkUuid(studyUuid), caseFormat, importParameters);
return ResponseEntity.ok().build();
}

Expand All @@ -288,7 +288,7 @@ public ResponseEntity<BasicStudyInfos> recreateStudyNetwork(@PathVariable("study
@RequestHeader(HEADER_USER_ID) String userId,
@Parameter(description = "case format") @RequestParam(name = "caseFormat", required = false) String caseFormat
) {
studyService.recreateStudyRootNetwork(userId, studyUuid, studyService.getStudyFirstRootNetworkUuid(studyUuid), caseFormat);
studyService.recreateNetwork(userId, studyUuid, studyService.getStudyFirstRootNetworkUuid(studyUuid), caseFormat);
return ResponseEntity.ok().build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
@Builder
public class InfoTypeParameters {
public static final String QUERY_PARAM_DC_POWERFACTOR = "dcPowerFactor";
public static final String QUERY_PARAM_OPERATION = "operation";
private String infoType;
private Map<String, String> optionalParameters;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
/**
* Copyright (c) 2024, RTE (http://www.rte-france.com)
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
package org.gridsuite.study.server.dto;

import jakarta.persistence.Id;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,23 @@
/**
* Copyright (c) 2024, RTE (http://www.rte-france.com)
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
package org.gridsuite.study.server.dto.caseimport;

import lombok.Getter;

public enum CaseImportAction {
STUDY_CREATION,
ROOT_NETWORK_CREATION,
NETWORK_RECREATION,
ROOT_NETWORK_MODIFICATION,
STUDY_CREATION("Study creation"),
ROOT_NETWORK_CREATION("Root network creation"),
NETWORK_RECREATION("Network recreation"),
ROOT_NETWORK_MODIFICATION("Root network modification");

@Getter
public final String label;

CaseImportAction(String label) {
this.label = label;
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
/**
* Copyright (c) 2024, RTE (http://www.rte-france.com)
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
package org.gridsuite.study.server.repository.rootnetwork;

import jakarta.persistence.*;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
/**
* Copyright (c) 2024, RTE (http://www.rte-france.com)
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
package org.gridsuite.study.server.repository.rootnetwork;

import org.springframework.data.jpa.repository.JpaRepository;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.util.Strings;
import org.gridsuite.study.server.StudyException;
import org.gridsuite.study.server.dto.*;
import org.gridsuite.study.server.dto.caseimport.CaseImportAction;
import org.gridsuite.study.server.dto.caseimport.CaseImportReceiver;
Expand All @@ -20,9 +19,6 @@
import org.gridsuite.study.server.networkmodificationtree.dto.BuildStatus;
import org.gridsuite.study.server.networkmodificationtree.dto.NodeBuildStatus;
import org.gridsuite.study.server.notification.NotificationService;
import org.gridsuite.study.server.repository.StudyEntity;
import org.gridsuite.study.server.repository.StudyRepository;
import org.gridsuite.study.server.repository.rootnetwork.RootNetworkEntity;
import org.gridsuite.study.server.service.dynamicsimulation.DynamicSimulationService;
import org.gridsuite.study.server.service.shortcircuit.ShortCircuitService;
import org.slf4j.Logger;
Expand Down Expand Up @@ -69,10 +65,8 @@ public class ConsumerService {
private final LoadFlowService loadFlowService;
private final UserAdminService userAdminService;
private final NetworkModificationTreeService networkModificationTreeService;
private final StudyRepository studyRepository;
private final ShortCircuitService shortCircuitService;
private final RootNetworkNodeInfoService rootNetworkNodeInfoService;
private final RootNetworkService rootNetworkService;

@Autowired
public ConsumerService(ObjectMapper objectMapper,
Expand All @@ -85,9 +79,7 @@ public ConsumerService(ObjectMapper objectMapper,
UserAdminService userAdminService,
NetworkModificationTreeService networkModificationTreeService,
SensitivityAnalysisService sensitivityAnalysisService,
StudyRepository studyRepository,
RootNetworkNodeInfoService rootNetworkNodeInfoService,
RootNetworkService rootNetworkservice) {
RootNetworkNodeInfoService rootNetworkNodeInfoService) {
this.objectMapper = objectMapper;
this.notificationService = notificationService;
this.studyService = studyService;
Expand All @@ -97,10 +89,8 @@ public ConsumerService(ObjectMapper objectMapper,
this.userAdminService = userAdminService;
this.networkModificationTreeService = networkModificationTreeService;
this.sensitivityAnalysisService = sensitivityAnalysisService;
this.studyRepository = studyRepository;
this.shortCircuitService = shortCircuitService;
this.rootNetworkNodeInfoService = rootNetworkNodeInfoService;
this.rootNetworkService = rootNetworkservice;
}

@Bean
Expand Down Expand Up @@ -211,35 +201,33 @@ public Consumer<Message<String>> consumeCaseImportSucceeded() {

CaseInfos caseInfos = new CaseInfos(caseUuid, caseName, caseFormat);
NetworkInfos networkInfos = new NetworkInfos(networkUuid, networkId);
StudyEntity studyEntity = studyRepository.findWithRootNetworksById(studyUuid).orElse(null);
try {
switch (caseImportAction) {
case STUDY_CREATION ->
insertStudy(studyUuid, userId, networkInfos, caseInfos, importParameters, importReportUuid);
case ROOT_NETWORK_CREATION ->
rootNetworkService.createRootNetworkFromRequest(studyEntity, RootNetworkInfos.builder()
studyService.createRootNetwork(studyUuid, RootNetworkInfos.builder()
.id(rootNetworkUuid)
.caseInfos(caseInfos)
.reportUuid(importReportUuid)
.networkInfos(networkInfos)
.importParameters(importParameters)
.build());
case NETWORK_RECREATION ->
recreateNetworkOfRootNetwork(studyEntity, rootNetworkUuid, userId, networkInfos);
studyService.updateNetwork(studyUuid, rootNetworkUuid, userId, networkInfos);
case ROOT_NETWORK_MODIFICATION ->
updateRootNetworkCase(studyEntity.getId(), rootNetworkUuid, networkInfos, caseInfos,
studyService.updateRootNetworkCase(studyUuid, rootNetworkUuid, networkInfos, caseInfos,
importParameters, importReportUuid);

}
caseService.disableCaseExpiration(caseUuid);
} catch (Exception e) {
LOGGER.error("Error while importing case", e);
} finally {
// if studyEntity is already existing, we don't delete anything in the end of the process
if (studyEntity == null) {
if (caseImportAction == CaseImportAction.STUDY_CREATION) {
studyService.deleteStudyIfNotCreationInProgress(studyUuid, userId);
}
LOGGER.trace("Create study '{}' : {} seconds", studyUuid,
LOGGER.trace(caseImportAction.getLabel() + " for study uuid '{}' : {} seconds", studyUuid,
TimeUnit.NANOSECONDS.toSeconds(System.nanoTime() - startTime));
}
}
Expand All @@ -256,27 +244,6 @@ private void insertStudy(UUID studyUuid, String userId, NetworkInfos networkInfo
studyService.insertStudy(studyUuid, userId, networkInfos, caseInfos, loadFlowParametersUuid, shortCircuitParametersUuid, DynamicSimulationService.toEntity(dynamicSimulationParameters, objectMapper), null, securityAnalysisParametersUuid, sensitivityAnalysisParametersUuid, importParameters, importReportUuid);
}

private void recreateNetworkOfRootNetwork(StudyEntity studyEntity, UUID rootNetworkUuid, String userId, NetworkInfos networkInfos) {
// TODO: what to do here ? throwing exception will provoke retried and won't notify frontend
RootNetworkEntity rootNetworkEntity = rootNetworkService.getRootNetwork(rootNetworkUuid).orElseThrow(() -> new StudyException(StudyException.Type.ROOTNETWORK_NOT_FOUND));
studyService.updateStudyNetwork(studyEntity, rootNetworkEntity, userId, networkInfos);
}

private void updateRootNetworkCase(UUID studyUuid, UUID rootNetworkUuid, NetworkInfos networkInfos, CaseInfos caseInfos,
Map<String, String> importParameters, UUID importReportUuid) {
// Update case for a given root network
rootNetworkService.updateRootNetworkCase(rootNetworkUuid, RootNetworkInfos.builder()
.id(rootNetworkUuid)
.caseInfos(caseInfos)
.reportUuid(importReportUuid)
.networkInfos(networkInfos)
.importParameters(importParameters)
.build());
// Invalidate nodes of the updated root network
UUID rootNodeUuid = networkModificationTreeService.getStudyRootNodeUuid(studyUuid);
studyService.invalidateBuild(studyUuid, rootNodeUuid, rootNetworkUuid, false, false, true);
}

private UserProfileInfos getUserProfile(String userId) {
try {
return userAdminService.getUserProfile(userId).orElse(null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@
@Service
public class NetworkConversionService {

private static final String FIRST_VARIANT_ID = "first_variant_id";

private final RestTemplate restTemplate;

private String networkConversionServerBaseUri;
Expand All @@ -55,7 +53,12 @@ public NetworkConversionService(@Value("${powsybl.services.network-conversion-se
this.restTemplate = restTemplate;
}

public void persistentStore(UUID caseUuid, UUID studyUuid, UUID rootNetworkUuid, String variantId, String userId, UUID importReportUuid, String caseFormat, Map<String, Object> importParameters, CaseImportAction caseImportAction) {
/**
* if *variantId* is not null, 2 variant will be created from network-conversion-server
* - one variant for root node - INITIAL_VARIANT
* - one variant cloned from the previous one for the 1st node - *variantId*
*/
public void persistNetwork(UUID caseUuid, UUID studyUuid, UUID rootNetworkUuid, String variantId, String userId, UUID importReportUuid, String caseFormat, Map<String, Object> importParameters, CaseImportAction caseImportAction) {
String receiver;
try {
receiver = URLEncoder.encode(objectMapper.writeValueAsString(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,20 @@
import org.gridsuite.study.server.dto.InfoTypeParameters;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.HttpEntity;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.web.client.HttpStatusCodeException;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;

import java.util.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import static org.gridsuite.study.server.StudyConstants.*;
import static org.gridsuite.study.server.StudyException.Type.*;
import static org.gridsuite.study.server.dto.InfoTypeParameters.QUERY_PARAM_DC_POWERFACTOR;
import static org.gridsuite.study.server.dto.InfoTypeParameters.QUERY_PARAM_OPERATION;
import static org.gridsuite.study.server.dto.InfoTypeParameters.*;
import static org.gridsuite.study.server.utils.StudyUtils.handleHttpError;

@Service
Expand Down Expand Up @@ -78,7 +76,8 @@ public String getElementsInfos(UUID networkUuid, String variantId, List<String>
return restTemplate.postForObject(networkMapServerBaseUri + url, httpEntity, String.class);
}

public String getElementInfos(UUID networkUuid, String variantId, String elementType, String infoType, String operation, double dcPowerFactor, String elementId) {
public String getElementInfos(UUID networkUuid, String variantId, String elementType, String infoType,
double dcPowerFactor, String elementId) {
String path = DELIMITER + NETWORK_MAP_API_VERSION + "/networks/{networkUuid}/elements/{elementId}";
UriComponentsBuilder builder = UriComponentsBuilder.fromPath(path);
if (!StringUtils.isBlank(variantId)) {
Expand All @@ -87,10 +86,6 @@ public String getElementInfos(UUID networkUuid, String variantId, String element
builder = builder.queryParam(QUERY_PARAM_ELEMENT_TYPE, elementType);
builder = builder.queryParam(QUERY_PARAM_INFO_TYPE, infoType);
Map<String, String> optionalParams = new HashMap<>();
if (operation != null) {
optionalParams.put(QUERY_PARAM_OPERATION, operation);
}

optionalParams.put(QUERY_PARAM_DC_POWERFACTOR, String.valueOf(dcPowerFactor));
InfoTypeParameters infoTypeParameters = InfoTypeParameters.builder()
.optionalParameters(optionalParams)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public class NetworkModificationTreeService {

public static final String ROOT_NODE_NAME = "Root";

private static final String FIRST_VARIANT_ID = "first_variant_id";
public static final String FIRST_VARIANT_ID = "first_variant_id";

private final EnumMap<NodeType, AbstractNodeRepositoryProxy<?, ?, ?>> repositories = new EnumMap<>(NodeType.class);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,16 @@ public List<ReportLog> getReportLogs(@NonNull UUID id, String messageFilter, Set
return restTemplate.exchange(this.getReportsServerURI() + path, HttpMethod.GET, new HttpEntity<>(headers), new ParameterizedTypeReference<List<ReportLog>>() {
}).getBody();
}

public UUID duplicateReport(@NonNull UUID id) {
var path = UriComponentsBuilder.fromPath("{id}/duplicate").buildAndExpand(id).toUriString();
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
try {
return restTemplate.exchange(this.getReportsServerURI() + path, HttpMethod.POST, new HttpEntity<>(headers), UUID.class).getBody();
} catch (Exception e) {
LOGGER.error("Error while duplicating report : {}", e.getMessage());
return UUID.randomUUID();
}
}
}
Loading

0 comments on commit 8c3209e

Please sign in to comment.