Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -60,21 +60,19 @@ public ResponseEntity<UUID> run(@PathVariable("networkUuid") UUID networkUuid,
@RequestParam(name = "reportUuid", required = false) UUID reportId,
@RequestParam(name = REPORTER_ID_HEADER, required = false) String reportName,
@RequestParam(name = REPORT_TYPE_HEADER, required = false, defaultValue = "DynamicMarginCalculation") String reportType,
@RequestParam(name = HEADER_PROVIDER, required = false) String provider,
@RequestParam(name = HEADER_DEBUG, required = false, defaultValue = "false") boolean debug,
@RequestParam(name = "dynamicSimulationParametersUuid") UUID dynamicSimulationParametersUuid,
@RequestParam(name = "dynamicSecurityAnalysisParametersUuid") UUID dynamicSecurityAnalysisParametersUuid,
@RequestParam(name = "parametersUuid") UUID parametersUuid,
@RequestBody String dynamicSimulationParametersJson,
@RequestHeader(HEADER_USER_ID) String userId) {

DynamicMarginCalculationRunContext dynamicMarginCalculationRunContext = parametersService.createRunContext(
networkUuid,
variantId,
receiver,
provider,
ReportInfos.builder().reportUuid(reportId).reporterId(reportName).computationType(reportType).build(),
userId,
dynamicSimulationParametersJson,
dynamicSimulationParametersUuid,
dynamicSecurityAnalysisParametersUuid,
parametersUuid,
debug);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public void preRun(DynamicMarginCalculationRunContext runContext) {

// get evaluated parameters values from the dynamic simulation server
DynamicSimulationParametersValues dynamicSimulationParametersValues =
dynamicSimulationClient.getParametersValues(runContext.getDynamicSimulationParametersJson(),
dynamicSimulationClient.getParametersValues(runContext.getDynamicSimulationParametersUuid(),
runContext.getNetworkUuid(), runContext.getVariantId());

// get dynamic model list from dynamic simulation server
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,8 @@ public ParametersService(@Value("${dynamic-margin-calculation.default-provider}"

@Transactional(readOnly = true)
public DynamicMarginCalculationRunContext createRunContext(UUID networkUuid, String variantId, String receiver,
String provider, ReportInfos reportInfos, String userId,
// should be UUID dynamicSimulationParametersUuid after moving dynamic simulation parameters to its server,
String dynamicSimulationParametersJson,
ReportInfos reportInfos, String userId,
UUID dynamicSimulationParametersUuid,
UUID dynamicSecurityAnalysisParametersUuid,
UUID dynamicMarginCalculationParametersUuid,
boolean debug) {
Expand All @@ -95,15 +94,11 @@ public DynamicMarginCalculationRunContext createRunContext(UUID networkUuid, Str
.parameters(dynamicMarginCalculationParametersInfos)
.debug(debug)
.build();
runContext.setDynamicSimulationParametersJson(dynamicSimulationParametersJson);
runContext.setDynamicSimulationParametersUuid(dynamicSimulationParametersUuid);
runContext.setDynamicSecurityAnalysisParametersUuid(dynamicSecurityAnalysisParametersUuid);

// set provider for run context
String providerToUse = provider;
if (providerToUse == null) {
providerToUse = Optional.ofNullable(runContext.getParameters().getProvider()).orElse(defaultProvider);
}

String providerToUse = Optional.ofNullable(runContext.getParameters().getProvider()).orElse(defaultProvider);
runContext.setProvider(providerToUse);

// check provider
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import org.gridsuite.dynamicmargincalculation.server.dto.parameters.DynamicSimulationParametersValues;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponents;
Expand Down Expand Up @@ -39,26 +38,19 @@ public DynamicSimulationClient(@Value("${gridsuite.services.dynamic-simulation-s
super(baseUri, restTemplate, objectMapper);
}

public DynamicSimulationParametersValues getParametersValues(String dynamicSimulationParametersJson, UUID networkUuid, String variant) {
public DynamicSimulationParametersValues getParametersValues(UUID dynamicSimulationParametersUuid, UUID networkUuid, String variant) {
String endPointUrl = buildEndPointUrl(getBaseUri(), API_VERSION, DYNAMIC_SIMULATION_END_POINT_PARAMETERS);

// TODO should use GET instead of POST after moving dynamic simulation parameters to its server
UriComponents uriComponents = UriComponentsBuilder.fromUriString(endPointUrl + "/values")
UriComponents uriComponents = UriComponentsBuilder.fromUriString(endPointUrl + "/" + dynamicSimulationParametersUuid + "/values")
.queryParam("networkUuid", networkUuid)
.queryParam(VARIANT_ID_HEADER, variant)
.build();

// call dynamic simulation REST API
String url = uriComponents.toUriString();

HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);

HttpEntity<String> requestEntity = new HttpEntity<>(dynamicSimulationParametersJson, headers);

ResponseEntity<DynamicSimulationParametersValues> result = getRestTemplate().exchange(url, HttpMethod.POST, requestEntity, DynamicSimulationParametersValues.class);
DynamicSimulationParametersValues result = getRestTemplate().getForObject(url, DynamicSimulationParametersValues.class);

logger.debug(DYNAMIC_SIMULATION_REST_API_CALLED_SUCCESSFULLY_MESSAGE, url);
return result.getBody();
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import org.gridsuite.computation.dto.ReportInfos;
import org.gridsuite.computation.service.AbstractResultContext;
import org.gridsuite.dynamicmargincalculation.server.dto.parameters.DynamicMarginCalculationParametersInfos;
import org.gridsuite.dynamicmargincalculation.server.utils.GZipUtils;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHeaders;

Expand All @@ -29,7 +28,7 @@
*/
public class DynamicMarginCalculationResultContext extends AbstractResultContext<DynamicMarginCalculationRunContext> {

private static final String HEADER_DYNAMIC_SIMULATION_PARAMETERS_JSON_UUID = "dynamicSimulationParametersJson";
private static final String HEADER_DYNAMIC_SIMULATION_PARAMETERS_UUID = "dynamicSimulationParametersUuid";
private static final String HEADER_DYNAMIC_SECURITY_ANALYSIS_PARAMETERS_UUID = "dynamicSecurityAnalysisParametersUuid";

public DynamicMarginCalculationResultContext(UUID resultUuid, DynamicMarginCalculationRunContext runContext) {
Expand Down Expand Up @@ -74,18 +73,15 @@ public static DynamicMarginCalculationResultContext fromMessage(Message<String>
// specific headers
UUID dynamicSecurityAnalysisParametersUuid = UUID.fromString(getNonNullHeader(headers, HEADER_DYNAMIC_SECURITY_ANALYSIS_PARAMETERS_UUID));
runContext.setDynamicSecurityAnalysisParametersUuid(dynamicSecurityAnalysisParametersUuid);
// TODO : using directly uuid after moving dynamic simulation parameters to its server
String compressedJson = headers.get(HEADER_DYNAMIC_SIMULATION_PARAMETERS_JSON_UUID).toString();
runContext.setDynamicSimulationParametersJson(GZipUtils.decompress(compressedJson));
UUID dynamicSimulationParametersUuid = UUID.fromString(getNonNullHeader(headers, HEADER_DYNAMIC_SIMULATION_PARAMETERS_UUID));
runContext.setDynamicSimulationParametersUuid(dynamicSimulationParametersUuid);
Comment on lines 73 to +77
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

Keep a transition path for already-enqueued run messages.

Lines 76-77 assume every queued message now carries dynamicSimulationParametersUuid. Messages published by the previous version only carried the JSON payload, so after rollout those in-flight computations will fail before the worker even reaches preRun. Please either keep a temporary legacy read path or make draining the run queue a hard deploy prerequisite.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In
`@src/main/java/org/gridsuite/dynamicmargincalculation/server/service/contexts/DynamicMarginCalculationResultContext.java`
around lines 73 - 77, The code unconditionally reads
HEADER_DYNAMIC_SIMULATION_PARAMETERS_UUID and sets
runContext.setDynamicSimulationParametersUuid, which will break already-enqueued
messages that lack that header; add a backward-compatible fallback in
DynamicMarginCalculationResultContext: when getNonNullHeader(headers,
HEADER_DYNAMIC_SIMULATION_PARAMETERS_UUID) is missing or empty, attempt to
extract dynamicSimulationParametersUuid from the message payload (e.g., parse
the JSON body used by preRun for a "dynamicSimulationParametersUuid" field) and
only set runContext.setDynamicSimulationParametersUuid when found, otherwise
leave it null and log a warning for telemetry so older messages can still be
processed; keep getNonNullHeader and existing path as primary, but implement
this safe legacy-read path around UUID.fromString to avoid throwing for missing
headers.


return new DynamicMarginCalculationResultContext(resultUuid, runContext);
}

@Override
public Map<String, String> getSpecificMsgHeaders(ObjectMapper objectMapper) {
// TODO : using directly uuid after moving dynamic simulation parameters to its server
String compressedJson = GZipUtils.compress(getRunContext().getDynamicSimulationParametersJson());
return Map.of(HEADER_DYNAMIC_SECURITY_ANALYSIS_PARAMETERS_UUID, getRunContext().getDynamicSecurityAnalysisParametersUuid().toString(),
HEADER_DYNAMIC_SIMULATION_PARAMETERS_JSON_UUID, compressedJson);
HEADER_DYNAMIC_SIMULATION_PARAMETERS_UUID, getRunContext().getDynamicSimulationParametersUuid().toString());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
@Setter
public class DynamicMarginCalculationRunContext extends AbstractComputationRunContext<DynamicMarginCalculationParametersInfos> {

private String dynamicSimulationParametersJson;
private UUID dynamicSimulationParametersUuid;
private UUID dynamicSecurityAnalysisParametersUuid;

// --- Fields which are enriched in worker service --- //
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@
import static org.gridsuite.computation.service.NotificationService.HEADER_RESULT_UUID;
import static org.gridsuite.computation.service.NotificationService.HEADER_USER_ID;
import static org.gridsuite.dynamicmargincalculation.server.controller.utils.TestUtils.RESOURCE_PATH_DELIMITER;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.when;
import static org.springframework.http.MediaType.APPLICATION_JSON;
Expand Down Expand Up @@ -78,6 +79,7 @@ public class DynamicMarginCalculationControllerIEEE14Test extends AbstractDynami
private static final UUID NETWORK_UUID = UUID.fromString("43b0f54e-e8fc-4607-8f53-e1cab1075ab5");
private static final String VARIANT_1_ID = "variant_1";

private static final UUID DS_PARAMETERS_UUID = UUID.fromString("ca76b46c-b013-4453-b2a2-f64107dd023b");
private static final UUID DSA_PARAMETERS_UUID = UUID.fromString("2745666a-0abe-4c3a-8b91-a719c1d1f753");
private static final UUID PARAMETERS_UUID = UUID.fromString("e786c4ca-64e7-4f44-b6a2-8f23b8b4334a");
private static final UUID FILTER_UUID = UUID.fromString("b234ce92-23f2-422c-b239-ec69abc399bd");
Expand Down Expand Up @@ -163,7 +165,7 @@ private void initDynamicSimulationClientsMock() {
DynamicSimulationParameters dynamicSimulationParameters = objectMapper.readValue(dynamicSimulationParametersIS, DynamicSimulationParameters.class);

// Mock for dynamic simulation server
when(dynamicSimulationClient.getParametersValues(anyString(), eq(NETWORK_UUID), any()))
when(dynamicSimulationClient.getParametersValues(eq(DS_PARAMETERS_UUID), eq(NETWORK_UUID), any()))
.thenReturn(DynamicSimulationParametersValues.builder()
.dynamicModel(dynamicModel)
.dynawoParameters(dynamicSimulationParameters.getExtension(DynawoSimulationParameters.class))
Expand All @@ -183,17 +185,15 @@ private void initDynamicSecurityAnalysisClientMock() {

@Test
void test01IEEE14() throws Exception {
// The controller requires a request body string: dynamicSimulationParametersJson.
String dynamicSimulationParametersJson = "{}";

// run dynamic margin calculation on a specific variant
MvcResult result = mockMvc.perform(
post("/v1/networks/{networkUuid}/run", NETWORK_UUID.toString())
.param(VARIANT_ID_HEADER, VARIANT_1_ID)
.param("dynamicSimulationParametersUuid", DS_PARAMETERS_UUID.toString())
.param("dynamicSecurityAnalysisParametersUuid", DSA_PARAMETERS_UUID.toString())
.param("parametersUuid", PARAMETERS_UUID.toString())
.contentType(APPLICATION_JSON)
.content(dynamicSimulationParametersJson)
.header(HEADER_USER_ID, "testUserId")
)
.andExpect(status().isOk())
Expand Down
Loading
Loading