Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use notifications to manage parameter changes #614

Merged
merged 13 commits into from
Oct 8, 2024
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.gridsuite.study.server.dto.ComputationType;
import org.gridsuite.study.server.dto.StudyIndexationStatus;
import org.gridsuite.study.server.networkmodificationtree.dto.InsertMode;
import org.gridsuite.study.server.notification.dto.NetworkImpactsInfos;
Expand Down Expand Up @@ -38,6 +39,7 @@ public class NotificationService {
public static final String HEADER_NODES = "nodes";
public static final String HEADER_STUDY_UUID = "studyUuid";
public static final String HEADER_UPDATE_TYPE = "updateType";
public static final String HEADER_COMPUTATION_TYPE = "computationType";
public static final String HEADER_UPDATE_TYPE_SUBSTATIONS_IDS = "substationsIds";
public static final String HEADER_USER_ID = "userId";
public static final String HEADER_MODIFIED_BY = "modifiedBy";
Expand Down Expand Up @@ -80,6 +82,7 @@ public class NotificationService {
public static final String UPDATE_TYPE_STATE_ESTIMATION_FAILED = "stateEstimation_failed";
public static final String UPDATE_TYPE_STATE_ESTIMATION_RESULT = "stateEstimationResult";
public static final String UPDATE_TYPE_STATE_ESTIMATION_STATUS = "stateEstimation_status";
public static final String UPDATE_TYPE_COMPUTATION_PARAMETERS = "computationParametersUpdated";

public static final String MODIFICATIONS_CREATING_IN_PROGRESS = "creatingInProgress";
public static final String MODIFICATIONS_STASHING_IN_PROGRESS = "stashingInProgress";
Expand Down Expand Up @@ -166,6 +169,15 @@ public void emitStudyChanged(UUID studyUuid, UUID nodeUuid, String updateType) {
.build());
}

@PostCompletion
public void emitComputationParamsChanged(UUID studyUuid, ComputationType computationType) {
sendUpdateMessage(MessageBuilder.withPayload("")
.setHeader(HEADER_STUDY_UUID, studyUuid)
.setHeader(HEADER_UPDATE_TYPE, UPDATE_TYPE_COMPUTATION_PARAMETERS)
.setHeader(HEADER_COMPUTATION_TYPE, computationType.name())
.build());
}

public void emitStudyCreationError(UUID studyUuid, String userId, String errorMessage) {
sendUpdateMessage(MessageBuilder.withPayload("")
.setHeader(HEADER_STUDY_UUID, studyUuid)
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/org/gridsuite/study/server/service/StudyService.java
Original file line number Diff line number Diff line change
Expand Up @@ -822,6 +822,7 @@ public void setSecurityAnalysisParametersValues(UUID studyUuid, String parameter
createOrUpdateSecurityAnalysisParameters(studyUuid, studyEntity, parameters);
notificationService.emitStudyChanged(studyUuid, null, NotificationService.UPDATE_TYPE_SECURITY_ANALYSIS_STATUS);
notificationService.emitElementUpdated(studyUuid, userId);
notificationService.emitComputationParamsChanged(studyUuid, SECURITY_ANALYSIS);
}

public NonEvacuatedEnergyParametersInfos getNonEvacuatedEnergyParametersInfos(UUID studyUuid) {
Expand All @@ -847,6 +848,7 @@ public boolean setLoadFlowParameters(UUID studyUuid, String parameters, String u
notificationService.emitStudyChanged(studyUuid, null, NotificationService.UPDATE_TYPE_NON_EVACUATED_ENERGY_STATUS);
notificationService.emitStudyChanged(studyUuid, null, NotificationService.UPDATE_TYPE_DYNAMIC_SIMULATION_STATUS);
notificationService.emitElementUpdated(studyUuid, userId);
notificationService.emitComputationParamsChanged(studyUuid, LOAD_FLOW);
return userProfileIssue;
}

Expand Down Expand Up @@ -877,6 +879,8 @@ public void updateLoadFlowProvider(UUID studyUuid, String provider, String userI
loadflowService.updateLoadFlowProvider(studyEntity.getLoadFlowParametersUuid(), provider);
invalidateLoadFlowStatusOnAllNodes(studyUuid);
notificationService.emitStudyChanged(studyUuid, null, NotificationService.UPDATE_TYPE_LOADFLOW_STATUS);
notificationService.emitComputationParamsChanged(studyUuid, LOAD_FLOW);

});
}

Expand All @@ -889,6 +893,7 @@ public void updateSecurityAnalysisProvider(UUID studyUuid, String provider, Stri
securityAnalysisService.updateSecurityAnalysisProvider(studyEntity.getSecurityAnalysisParametersUuid(), provider);
invalidateSecurityAnalysisStatusOnAllNodes(studyUuid);
notificationService.emitStudyChanged(studyUuid, null, NotificationService.UPDATE_TYPE_SECURITY_ANALYSIS_STATUS);
notificationService.emitComputationParamsChanged(studyUuid, SECURITY_ANALYSIS);
});
}

Expand Down Expand Up @@ -916,6 +921,7 @@ public void updateDynamicSimulationProvider(UUID studyUuid, String provider, Str
studyEntity.setDynamicSimulationProvider(provider != null ? provider : defaultDynamicSimulationProvider);
invalidateDynamicSimulationStatusOnAllNodes(studyUuid);
notificationService.emitStudyChanged(studyUuid, null, NotificationService.UPDATE_TYPE_DYNAMIC_SIMULATION_STATUS);
notificationService.emitComputationParamsChanged(studyUuid, DYNAMIC_SIMULATION);
});
}

Expand Down Expand Up @@ -947,6 +953,8 @@ public void setShortCircuitParameters(UUID studyUuid, @Nullable String shortCirc
studyRepository.save(studyEntity);
}
notificationService.emitElementUpdated(studyUuid, userId);
notificationService.emitComputationParamsChanged(studyUuid, SHORT_CIRCUIT);

}

@Transactional
Expand Down Expand Up @@ -1841,6 +1849,8 @@ public void setVoltageInitParameters(UUID studyUuid, StudyVoltageInitParameters
createOrUpdateVoltageInitParameters(studyEntity, parameters.getComputationParameters());
notificationService.emitStudyChanged(studyUuid, null, NotificationService.UPDATE_TYPE_VOLTAGE_INIT_STATUS);
notificationService.emitElementUpdated(studyUuid, userId);
notificationService.emitComputationParamsChanged(studyUuid, VOLTAGE_INITIALIZATION);

}

public StudyVoltageInitParameters getVoltageInitParameters(UUID studyUuid) {
Expand Down Expand Up @@ -1879,6 +1889,8 @@ public List<ModelInfos> getDynamicSimulationModels(UUID studyUuid, UUID nodeUuid
public void setDynamicSimulationParameters(UUID studyUuid, DynamicSimulationParametersInfos dsParameter, String userId) {
updateDynamicSimulationParameters(studyUuid, DynamicSimulationService.toEntity(dsParameter != null ? dsParameter : DynamicSimulationService.getDefaultDynamicSimulationParameters(), objectMapper));
notificationService.emitElementUpdated(studyUuid, userId);
notificationService.emitComputationParamsChanged(studyUuid, DYNAMIC_SIMULATION);

}

public DynamicSimulationParametersInfos getDynamicSimulationParameters(UUID studyUuid) {
Expand Down Expand Up @@ -2056,6 +2068,7 @@ public void setSensitivityAnalysisParameters(UUID studyUuid, String parameters,
createOrUpdateSensitivityAnalysisParameters(studyUuid, parameters);
notificationService.emitStudyChanged(studyUuid, null, NotificationService.UPDATE_TYPE_SENSITIVITY_ANALYSIS_STATUS);
notificationService.emitElementUpdated(studyUuid, userId);
notificationService.emitComputationParamsChanged(studyUuid, SENSITIVITY_ANALYSIS);
}

@Transactional
Expand All @@ -2064,6 +2077,8 @@ public void setNonEvacuatedEnergyParametersInfos(UUID studyUuid, NonEvacuatedEne
NonEvacuatedEnergyService.toEntity(parameters != null ? parameters :
NonEvacuatedEnergyService.getDefaultNonEvacuatedEnergyParametersInfos()));
notificationService.emitElementUpdated(studyUuid, userId);
notificationService.emitComputationParamsChanged(studyUuid, NON_EVACUATED_ENERGY_ANALYSIS);

}

public void createOrUpdateSensitivityAnalysisParameters(UUID studyUuid, String parameters) {
Expand Down Expand Up @@ -2159,6 +2174,7 @@ public void updateNonEvacuatedEnergyProvider(UUID studyUuid, String provider, St
studyEntity.setNonEvacuatedEnergyProvider(provider != null ? provider : defaultNonEvacuatedEnergyProvider);
invalidateNonEvacuatedEnergyAnalysisStatusOnAllNodes(studyUuid);
notificationService.emitStudyChanged(studyUuid, null, NotificationService.UPDATE_TYPE_NON_EVACUATED_ENERGY_STATUS);
notificationService.emitComputationParamsChanged(studyUuid, NON_EVACUATED_ENERGY_ANALYSIS);
});
}

Expand Down
4 changes: 4 additions & 0 deletions src/test/java/org/gridsuite/study/server/LoadFlowTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
import static org.gridsuite.study.server.StudyConstants.HEADER_USER_ID;
import static org.gridsuite.study.server.dto.ComputationType.LOAD_FLOW;
import static org.gridsuite.study.server.notification.NotificationService.HEADER_UPDATE_TYPE;
import static org.gridsuite.study.server.notification.NotificationService.UPDATE_TYPE_COMPUTATION_PARAMETERS;
import static org.junit.Assert.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doAnswer;
Expand Down Expand Up @@ -647,6 +648,9 @@ private void createOrUpdateParametersAndDoChecks(UUID studyNameUserIdUuid, Strin

message = output.receive(TIMEOUT, elementUpdateDestination);
assertEquals(studyNameUserIdUuid, message.getHeaders().get(NotificationService.HEADER_ELEMENT_UUID));
message = output.receive(TIMEOUT, studyUpdateDestination);
assertEquals(UPDATE_TYPE_COMPUTATION_PARAMETERS, message.getHeaders().get(NotificationService.HEADER_UPDATE_TYPE));

Copy link
Contributor

Choose a reason for hiding this comment

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

could assert that it's certainly LOADFLOW in HEADER_COMPUTATION_TYPE here as well

}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
import static org.gridsuite.study.server.StudyConstants.HEADER_RECEIVER;
import static org.gridsuite.study.server.StudyConstants.HEADER_USER_ID;
import static org.gridsuite.study.server.notification.NotificationService.HEADER_UPDATE_TYPE;
import static org.gridsuite.study.server.notification.NotificationService.UPDATE_TYPE_COMPUTATION_PARAMETERS;
import static org.junit.Assert.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doAnswer;
Expand Down Expand Up @@ -658,6 +659,8 @@ public void testNonEvacuatedEnergyParameters() throws Exception {
.contentType(MediaType.APPLICATION_JSON)
.content(myBodyJson)).andExpect(
status().isOk());
Message<byte[]> message = output.receive(TIMEOUT, studyUpdateDestination);
assertEquals(UPDATE_TYPE_COMPUTATION_PARAMETERS, message.getHeaders().get(NotificationService.HEADER_UPDATE_TYPE));

Copy link
Contributor

Choose a reason for hiding this comment

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

same, you don't test HEADER_COMPUTATION_TYPE here and elsewhere

mockMvc.perform(get("/v1/studies/{studyUuid}/non-evacuated-energy/parameters", studyNameUserIdUuid)).andExpectAll(
status().isOk(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -655,6 +655,7 @@ public void testSecurityAnalysisParameters() throws Exception {
assertTrue(TestUtils.getRequestsDone(1, server).stream().anyMatch(r -> r.matches("/v1/parameters/" + SECURITY_ANALYSIS_PARAMETERS_UUID)));
assertEquals(SECURITY_ANALYSIS_PARAMETERS_UUID, studyRepository.findById(studyNameUserIdUuid).orElseThrow().getSecurityAnalysisParametersUuid());
assertEquals(NotificationService.UPDATE_TYPE_SECURITY_ANALYSIS_STATUS, output.receive(TIMEOUT, studyUpdateDestination).getHeaders().get(NotificationService.HEADER_UPDATE_TYPE));
assertEquals(NotificationService.UPDATE_TYPE_COMPUTATION_PARAMETERS, output.receive(TIMEOUT, studyUpdateDestination).getHeaders().get(NotificationService.HEADER_UPDATE_TYPE));
//get security analysis parameters but with an already registered securityAnalysisParametersUuid
mockMvc.perform(get("/v1/studies/{studyUuid}/security-analysis/parameters", studyNameUserIdUuid)).andExpectAll(
status().isOk(),
Expand All @@ -669,6 +670,7 @@ public void testSecurityAnalysisParameters() throws Exception {
.content(mnBodyJson)).andExpect(
status().isOk());
assertEquals(NotificationService.UPDATE_TYPE_SECURITY_ANALYSIS_STATUS, output.receive(TIMEOUT, studyUpdateDestination).getHeaders().get(NotificationService.HEADER_UPDATE_TYPE));
assertEquals(NotificationService.UPDATE_TYPE_COMPUTATION_PARAMETERS, output.receive(TIMEOUT, studyUpdateDestination).getHeaders().get(NotificationService.HEADER_UPDATE_TYPE));
assertTrue(TestUtils.getRequestsDone(1, server).stream().anyMatch(r -> r.matches("/v1/parameters/" + SECURITY_ANALYSIS_PARAMETERS_UUID)));
assertEquals(SECURITY_ANALYSIS_PARAMETERS_UUID, studyRepository.findById(studyNameUserIdUuid).orElseThrow().getSecurityAnalysisParametersUuid());
}
Expand All @@ -690,6 +692,7 @@ public void testCreateSecurityAnalysisParameters() throws Exception {

assertEquals(SECURITY_ANALYSIS_PARAMETERS_UUID, studyRepository.findById(studyUuid).orElseThrow().getSecurityAnalysisParametersUuid());
assertEquals(NotificationService.UPDATE_TYPE_SECURITY_ANALYSIS_STATUS, output.receive(TIMEOUT, studyUpdateDestination).getHeaders().get(NotificationService.HEADER_UPDATE_TYPE));
assertEquals(NotificationService.UPDATE_TYPE_COMPUTATION_PARAMETERS, output.receive(TIMEOUT, studyUpdateDestination).getHeaders().get(NotificationService.HEADER_UPDATE_TYPE));
Set<String> requests = TestUtils.getRequestsDone(1, server);
assertTrue(requests.stream().anyMatch(r -> r.matches("/v1/parameters")));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
import static org.gridsuite.study.server.StudyConstants.HEADER_USER_ID;
import static org.gridsuite.study.server.dto.ComputationType.SENSITIVITY_ANALYSIS;
import static org.gridsuite.study.server.notification.NotificationService.HEADER_UPDATE_TYPE;
import static org.gridsuite.study.server.notification.NotificationService.UPDATE_TYPE_COMPUTATION_PARAMETERS;
import static org.gridsuite.study.server.utils.TestUtils.getBinaryAsBuffer;
import static org.junit.Assert.*;
import static org.mockito.ArgumentMatchers.any;
Expand Down Expand Up @@ -719,9 +720,13 @@ private void setParametersAndDoChecks(UUID studyNameUserIdUuid, String parameter
.content(parameters)).andExpect(
status().isOk());

Message<byte[]> sensiStatusMessage = output.receive(TIMEOUT, studyUpdateDestination);
assertEquals(studyNameUserIdUuid, sensiStatusMessage.getHeaders().get(NotificationService.HEADER_STUDY_UUID));
assertEquals(NotificationService.UPDATE_TYPE_SENSITIVITY_ANALYSIS_STATUS, sensiStatusMessage.getHeaders().get(NotificationService.HEADER_UPDATE_TYPE));
Message<byte[]> message = output.receive(TIMEOUT, studyUpdateDestination);
assertEquals(studyNameUserIdUuid, message.getHeaders().get(NotificationService.HEADER_STUDY_UUID));
assertEquals(NotificationService.UPDATE_TYPE_SENSITIVITY_ANALYSIS_STATUS, message.getHeaders().get(NotificationService.HEADER_UPDATE_TYPE));

message = output.receive(TIMEOUT, studyUpdateDestination);
assertEquals(studyNameUserIdUuid, message.getHeaders().get(NotificationService.HEADER_STUDY_UUID));
assertEquals(UPDATE_TYPE_COMPUTATION_PARAMETERS, message.getHeaders().get(NotificationService.HEADER_UPDATE_TYPE));

Message<byte[]> elementUpdateMessage = output.receive(TIMEOUT, elementUpdateDestination);
assertEquals(studyNameUserIdUuid, elementUpdateMessage.getHeaders().get(NotificationService.HEADER_ELEMENT_UUID));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
import static org.gridsuite.study.server.StudyConstants.HEADER_RECEIVER;
import static org.gridsuite.study.server.StudyConstants.HEADER_USER_ID;
import static org.gridsuite.study.server.notification.NotificationService.HEADER_UPDATE_TYPE;
import static org.gridsuite.study.server.notification.NotificationService.UPDATE_TYPE_COMPUTATION_PARAMETERS;
import static org.gridsuite.study.server.utils.TestUtils.getBinaryAsBuffer;
import static org.junit.Assert.*;
import static org.mockito.ArgumentMatchers.any;
Expand Down Expand Up @@ -282,6 +283,7 @@ public void testShortCircuitAnalysisParameters() throws Exception {
.header(HEADER_USER_ID, "testUserId")
.content("{\"dumb\": \"json\"}").contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk());
assertEquals(UPDATE_TYPE_COMPUTATION_PARAMETERS, output.receive(TIMEOUT, studyUpdateDestination).getHeaders().get(NotificationService.HEADER_UPDATE_TYPE));
assertTrue(TestUtils.getRequestsDone(1, server).stream().anyMatch(r -> r.equals("/v1/parameters/" + SHORT_CIRCUIT_ANALYSIS_PARAMETERS_UUID)));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
import java.util.*;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertEquals;
import static org.mockito.BDDMockito.any;
import static org.mockito.BDDMockito.eq;
import static org.mockito.Mockito.when;
Expand Down Expand Up @@ -725,11 +726,13 @@ public void testGetDynamicSimulationModelsGivenEmptyMapping() throws Exception {
}

private void checkNotificationsAfterInjectingDynamicSimulationParameters(UUID studyUuid) {
// must have message UPDATE_TYPE_DYNAMIC_SIMULATION_STATUS from channel : studyUpdateDestination
// must have message UPDATE_TYPE_DYNAMIC_SIMULATION_STATUS and UPDATE_TYPE_COMPUTATION_PARAMETERS from channel : studyUpdateDestination
Message<byte[]> studyUpdateMessage = output.receive(TIMEOUT, studyUpdateDestination);
assertThat(studyUpdateMessage.getHeaders())
.containsEntry(NotificationService.HEADER_STUDY_UUID, studyUuid)
.containsEntry(NotificationService.HEADER_UPDATE_TYPE, NotificationService.UPDATE_TYPE_DYNAMIC_SIMULATION_STATUS);
studyUpdateMessage = output.receive(TIMEOUT, studyUpdateDestination);
assertEquals(NotificationService.UPDATE_TYPE_COMPUTATION_PARAMETERS, studyUpdateMessage.getHeaders().get(NotificationService.HEADER_UPDATE_TYPE));

// must have message HEADER_USER_ID_VALUE from channel : elementUpdateDestination
Message<byte[]> elementUpdateMessage = output.receive(TIMEOUT, elementUpdateDestination);
Expand Down
15 changes: 15 additions & 0 deletions src/test/java/org/gridsuite/study/server/StudyTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@
import static org.gridsuite.study.server.StudyConstants.HEADER_USER_ID;
import static org.gridsuite.study.server.StudyException.Type.STUDY_NOT_FOUND;
import static org.gridsuite.study.server.notification.NotificationService.DEFAULT_ERROR_MESSAGE;
import static org.gridsuite.study.server.notification.NotificationService.UPDATE_TYPE_COMPUTATION_PARAMETERS;
import static org.gridsuite.study.server.utils.MatcherBasicStudyInfos.createMatcherStudyBasicInfos;
import static org.gridsuite.study.server.utils.MatcherCreatedStudyBasicInfos.createMatcherCreatedStudyBasicInfos;
import static org.gridsuite.study.server.utils.MatcherStudyInfos.createMatcherStudyInfos;
Expand Down Expand Up @@ -2584,6 +2585,11 @@ public void providerTest() throws Exception {
Message<byte[]> message = output.receive(TIMEOUT, studyUpdateDestination);
assertNotNull(message);
assertEquals(NotificationService.UPDATE_TYPE_LOADFLOW_STATUS, message.getHeaders().get(HEADER_UPDATE_TYPE));
message = output.receive(TIMEOUT, studyUpdateDestination);
assertNotNull(message);

assertEquals(UPDATE_TYPE_COMPUTATION_PARAMETERS, message.getHeaders().get(NotificationService.HEADER_UPDATE_TYPE));

assertNotNull(output.receive(TIMEOUT, elementUpdateDestination));

mockMvc.perform(post("/v1/studies/{studyUuid}/security-analysis/provider", studyUuid)
Expand All @@ -2594,6 +2600,11 @@ public void providerTest() throws Exception {
message = output.receive(TIMEOUT, studyUpdateDestination);
assertNotNull(message);
assertEquals(NotificationService.UPDATE_TYPE_SECURITY_ANALYSIS_STATUS, message.getHeaders().get(HEADER_UPDATE_TYPE));

message = output.receive(TIMEOUT, studyUpdateDestination);
assertNotNull(message);
assertEquals(UPDATE_TYPE_COMPUTATION_PARAMETERS, message.getHeaders().get(NotificationService.HEADER_UPDATE_TYPE));

assertNotNull(output.receive(TIMEOUT, elementUpdateDestination));

mockMvc.perform(post("/v1/studies/{studyUuid}/non-evacuated-energy/provider", studyUuid)
Expand All @@ -2604,6 +2615,10 @@ public void providerTest() throws Exception {
message = output.receive(TIMEOUT, studyUpdateDestination);
assertNotNull(message);
assertEquals(NotificationService.UPDATE_TYPE_NON_EVACUATED_ENERGY_STATUS, message.getHeaders().get(HEADER_UPDATE_TYPE));
message = output.receive(TIMEOUT, studyUpdateDestination);
assertNotNull(message);
assertEquals(UPDATE_TYPE_COMPUTATION_PARAMETERS, message.getHeaders().get(NotificationService.HEADER_UPDATE_TYPE));

assertNotNull(output.receive(TIMEOUT, elementUpdateDestination));

mockMvc.perform(get("/v1/studies/{studyUuid}/non-evacuated-energy/provider", studyUuid))
Expand Down
Loading
Loading