diff --git a/pom.xml b/pom.xml index 8e5da7df..b7e5d998 100644 --- a/pom.xml +++ b/pom.xml @@ -59,6 +59,10 @@ 4.10.1 gridsuite org.gridsuite:loadflow-server + + 1.7.0 + 1.34.0 + 1.15.0 @@ -93,6 +97,24 @@ + + + com.powsybl + powsybl-ws-commons + ${powsybl-ws-commons.version} + + + + org.gridsuite + gridsuite-computation + ${gridsuite-computation.version} + + + + org.gridsuite + gridsuite-filter + ${gridsuite-filter.version} + org.antlr diff --git a/src/main/java/org/gridsuite/loadflow/server/LoadFlowApplication.java b/src/main/java/org/gridsuite/loadflow/server/LoadFlowApplication.java index 16d13962..f1541f57 100644 --- a/src/main/java/org/gridsuite/loadflow/server/LoadFlowApplication.java +++ b/src/main/java/org/gridsuite/loadflow/server/LoadFlowApplication.java @@ -7,6 +7,7 @@ package org.gridsuite.loadflow.server; import com.powsybl.network.store.client.NetworkStoreService; +import org.gridsuite.computation.error.ComputationExceptionHandler; import org.gridsuite.computation.service.NotificationService; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @@ -15,7 +16,7 @@ * @author Franck Lecuyer */ @SuppressWarnings("checkstyle:HideUtilityClassConstructor") -@SpringBootApplication(scanBasePackageClasses = { LoadFlowApplication.class, NetworkStoreService.class, NotificationService.class }) +@SpringBootApplication(scanBasePackageClasses = {LoadFlowApplication.class, NetworkStoreService.class, NotificationService.class, ComputationExceptionHandler.class}) public class LoadFlowApplication { public static void main(String[] args) { SpringApplication.run(LoadFlowApplication.class, args); diff --git a/src/main/java/org/gridsuite/loadflow/server/PropertyServerNameProvider.java b/src/main/java/org/gridsuite/loadflow/server/PropertyServerNameProvider.java new file mode 100644 index 00000000..25f566fb --- /dev/null +++ b/src/main/java/org/gridsuite/loadflow/server/PropertyServerNameProvider.java @@ -0,0 +1,28 @@ +/** + * Copyright (c) 2025, 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.loadflow.server; + +import com.powsybl.ws.commons.error.ServerNameProvider; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Component; + +/** + * @author Hugo Marcellin + */ +@Component +public class PropertyServerNameProvider implements ServerNameProvider { + private final String name; + + public PropertyServerNameProvider(@Value("${spring.application.name:loadflow-server}") String name) { + this.name = name; + } + + @Override + public String serverName() { + return name; + } +} diff --git a/src/main/java/org/gridsuite/loadflow/server/service/FilterService.java b/src/main/java/org/gridsuite/loadflow/server/service/FilterService.java index ca189f29..142323af 100644 --- a/src/main/java/org/gridsuite/loadflow/server/service/FilterService.java +++ b/src/main/java/org/gridsuite/loadflow/server/service/FilterService.java @@ -15,6 +15,7 @@ import org.gridsuite.filter.utils.EquipmentType; import org.gridsuite.loadflow.server.dto.Column; import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.web.client.RestTemplateBuilder; import org.springframework.stereotype.Service; import java.util.List; @@ -28,10 +29,10 @@ @Service public class FilterService extends AbstractFilterService { - public FilterService( - NetworkStoreService networkStoreService, - @Value("${gridsuite.services.filter-server.base-uri:http://filter-server/}") String filterServerBaseUri) { - super(networkStoreService, filterServerBaseUri); + public FilterService(RestTemplateBuilder restTemplateBuilder, + NetworkStoreService networkStoreService, + @Value("${gridsuite.services.filter-server.base-uri:http://filter-server/}") String filterServerBaseUri) { + super(restTemplateBuilder, networkStoreService, filterServerBaseUri); } public Optional getResourceFilter(@NonNull UUID networkUuid, @NonNull String variantId, @NonNull GlobalFilter globalFilter) { diff --git a/src/main/java/org/gridsuite/loadflow/server/service/LimitReductionService.java b/src/main/java/org/gridsuite/loadflow/server/service/LimitReductionService.java index f7cb9976..b3492a0d 100644 --- a/src/main/java/org/gridsuite/loadflow/server/service/LimitReductionService.java +++ b/src/main/java/org/gridsuite/loadflow/server/service/LimitReductionService.java @@ -9,7 +9,8 @@ import lombok.Getter; import lombok.Setter; import org.apache.commons.lang3.Range; -import org.gridsuite.computation.ComputationException; +import org.gridsuite.computation.error.ComputationBusinessErrorCode; +import org.gridsuite.computation.error.ComputationException; import org.gridsuite.loadflow.server.dto.parameters.LimitReductionsByVoltageLevel; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Service; @@ -67,41 +68,41 @@ private List getLimitReductionsByD private void assertValidConfig(List> values) { if (voltageLevels.isEmpty()) { - throw new ComputationException(ComputationException.Type.LIMIT_REDUCTION_CONFIG_ERROR, "No configuration for voltage levels"); + throw new ComputationException(ComputationBusinessErrorCode.LIMIT_REDUCTION_CONFIG_ERROR, "No configuration for voltage levels"); } if (limitDurations.isEmpty()) { - throw new ComputationException(ComputationException.Type.LIMIT_REDUCTION_CONFIG_ERROR, "No configuration for limit durations"); + throw new ComputationException(ComputationBusinessErrorCode.LIMIT_REDUCTION_CONFIG_ERROR, "No configuration for limit durations"); } if (values.isEmpty() || values.get(0).isEmpty()) { - throw new ComputationException(ComputationException.Type.LIMIT_REDUCTION_CONFIG_ERROR, "No values provided"); + throw new ComputationException(ComputationBusinessErrorCode.LIMIT_REDUCTION_CONFIG_ERROR, "No values provided"); } int nbValuesByVl = values.get(0).size(); if (values.stream().anyMatch(valuesByVl -> valuesByVl.size() != nbValuesByVl)) { - throw new ComputationException(ComputationException.Type.LIMIT_REDUCTION_CONFIG_ERROR, "Number of values for a voltage level is incorrect"); + throw new ComputationException(ComputationBusinessErrorCode.LIMIT_REDUCTION_CONFIG_ERROR, "Number of values for a voltage level is incorrect"); } if (voltageLevels.size() < values.size()) { - throw new ComputationException(ComputationException.Type.LIMIT_REDUCTION_CONFIG_ERROR, "Too many values provided for voltage levels"); + throw new ComputationException(ComputationBusinessErrorCode.LIMIT_REDUCTION_CONFIG_ERROR, "Too many values provided for voltage levels"); } if (voltageLevels.size() > values.size()) { - throw new ComputationException(ComputationException.Type.LIMIT_REDUCTION_CONFIG_ERROR, "Not enough values provided for voltage levels"); + throw new ComputationException(ComputationBusinessErrorCode.LIMIT_REDUCTION_CONFIG_ERROR, "Not enough values provided for voltage levels"); } if (limitDurations.size() < nbValuesByVl - 1) { - throw new ComputationException(ComputationException.Type.LIMIT_REDUCTION_CONFIG_ERROR, "Too many values provided for limit durations"); + throw new ComputationException(ComputationBusinessErrorCode.LIMIT_REDUCTION_CONFIG_ERROR, "Too many values provided for limit durations"); } if (limitDurations.size() > nbValuesByVl - 1) { - throw new ComputationException(ComputationException.Type.LIMIT_REDUCTION_CONFIG_ERROR, "Not enough values provided for limit durations"); + throw new ComputationException(ComputationBusinessErrorCode.LIMIT_REDUCTION_CONFIG_ERROR, "Not enough values provided for limit durations"); } values.forEach(valuesByVl -> { if (valuesByVl.stream().anyMatch(v -> !Range.of(0.0, 1.0).contains(v))) { - throw new ComputationException(ComputationException.Type.LIMIT_REDUCTION_CONFIG_ERROR, "Value not between 0 and 1"); + throw new ComputationException(ComputationBusinessErrorCode.LIMIT_REDUCTION_CONFIG_ERROR, "Value not between 0 and 1"); } }); } diff --git a/src/main/java/org/gridsuite/loadflow/server/service/LoadFlowResultService.java b/src/main/java/org/gridsuite/loadflow/server/service/LoadFlowResultService.java index d08ef767..b07fe0f0 100644 --- a/src/main/java/org/gridsuite/loadflow/server/service/LoadFlowResultService.java +++ b/src/main/java/org/gridsuite/loadflow/server/service/LoadFlowResultService.java @@ -12,7 +12,6 @@ import com.powsybl.loadflow.LoadFlowResult; import com.powsybl.security.LimitViolationType; import lombok.AllArgsConstructor; -import org.gridsuite.computation.ComputationException; import org.gridsuite.computation.dto.GlobalFilter; import org.gridsuite.computation.dto.ResourceFilterDTO; import org.gridsuite.computation.service.AbstractComputationResultService; @@ -37,6 +36,7 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import java.io.UncheckedIOException; import java.time.Instant; import java.util.*; import java.util.concurrent.TimeUnit; @@ -290,7 +290,7 @@ private LoadFlowModificationInfos loadFlowModificationsToDTO(String jsonString) try { return objectMapper.readValue(jsonString, LoadFlowModificationInfos.class); } catch (JsonProcessingException e) { - throw new ComputationException("Invalid json string for modifications !"); + throw new UncheckedIOException("Invalid json string for modifications !", e); } } @@ -301,7 +301,7 @@ private String modificationsToJsonString(LoadFlowModificationInfos loadFlowModif try { return objectMapper.writeValueAsString(loadFlowModificationInfos); } catch (JsonProcessingException e) { - throw new ComputationException("Invalid modifications for json string !"); + throw new UncheckedIOException("Invalid modifications for json string !", e); } } } diff --git a/src/test/java/org/gridsuite/loadflow/server/LoadFlowParametersTest.java b/src/test/java/org/gridsuite/loadflow/server/LoadFlowParametersTest.java index c7afd87b..9851e220 100644 --- a/src/test/java/org/gridsuite/loadflow/server/LoadFlowParametersTest.java +++ b/src/test/java/org/gridsuite/loadflow/server/LoadFlowParametersTest.java @@ -14,7 +14,7 @@ import java.util.Map; import java.util.UUID; -import org.gridsuite.computation.ComputationException; +import org.gridsuite.computation.error.ComputationException; import org.gridsuite.loadflow.server.dto.parameters.LimitReductionsByVoltageLevel; import org.gridsuite.loadflow.server.dto.parameters.LoadFlowParametersInfos; import org.gridsuite.loadflow.server.dto.parameters.LoadFlowParametersValues; diff --git a/src/test/java/org/gridsuite/loadflow/server/PropertyServerNameProviderTest.java b/src/test/java/org/gridsuite/loadflow/server/PropertyServerNameProviderTest.java new file mode 100644 index 00000000..f91f71bf --- /dev/null +++ b/src/test/java/org/gridsuite/loadflow/server/PropertyServerNameProviderTest.java @@ -0,0 +1,22 @@ +/** + * Copyright (c) 2025, 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.loadflow.server; + +import org.junit.jupiter.api.Test; +import static org.assertj.core.api.Assertions.assertThat; + +/** + * @author Mohamed Ben-rejeb {@literal } + */ +class PropertyServerNameProviderTest { + + @Test + void returnsProvidedName() { + PropertyServerNameProvider provider = new PropertyServerNameProvider("custom-server"); + assertThat(provider.serverName()).isEqualTo("custom-server"); + } +}