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
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@
<dependency>
<groupId>org.gridsuite</groupId>
<artifactId>gridsuite-computation</artifactId>
<version>1.6.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.powsybl</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -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 <hugo.marcelin at rte-france.com>
*/
@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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* 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.AbstractBaseRestExceptionHandler;
import com.powsybl.ws.commons.error.ServerNameProvider;
import lombok.NonNull;
import org.gridsuite.computation.ComputationBusinessErrorCode;
import org.gridsuite.computation.ComputationException;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;

/**
* @author Hugo Marcellin <hugo.marcelin at rte-france.com>
*/
@ControllerAdvice
public class RestResponseEntityExceptionHandler extends AbstractBaseRestExceptionHandler<ComputationException, ComputationBusinessErrorCode> {

protected RestResponseEntityExceptionHandler(ServerNameProvider serverNameProvider) {
super(serverNameProvider);
}

@Override
protected @NonNull ComputationBusinessErrorCode getBusinessCode(ComputationException e) {
return e.getBusinessErrorCode();
}

@Override
protected HttpStatus mapStatus(ComputationBusinessErrorCode businessErrorCode) {
return switch (businessErrorCode) {
case RESULT_NOT_FOUND, NETWORK_NOT_FOUND, PARAMETERS_NOT_FOUND -> HttpStatus.NOT_FOUND;
case INVALID_FILTER_FORMAT,
INVALID_SORT_FORMAT,
INVALID_FILTER -> HttpStatus.BAD_REQUEST;
default -> HttpStatus.INTERNAL_SERVER_ERROR;
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.lang3.Range;
import org.gridsuite.computation.ComputationBusinessErrorCode;
import org.gridsuite.computation.ComputationException;
import org.gridsuite.loadflow.server.dto.parameters.LimitReductionsByVoltageLevel;
import org.springframework.boot.context.properties.ConfigurationProperties;
Expand Down Expand Up @@ -67,41 +68,41 @@ private List<LimitReductionsByVoltageLevel.LimitReduction> getLimitReductionsByD

private void assertValidConfig(List<List<Double>> 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");
}
});
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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 <mohamed.ben-rejeb at rte-france.com>}
*/
class PropertyServerNameProviderTest {

@Test
void returnsProvidedName() {
PropertyServerNameProvider provider = new PropertyServerNameProvider("custom-server");
assertThat(provider.serverName()).isEqualTo("custom-server");
}
}
Loading