Skip to content
Merged
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
7 changes: 7 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
<liquibase-hibernate-package>org.gridsuite.mapping.server</liquibase-hibernate-package>
<sonar.organization>gridsuite</sonar.organization>
<sonar.projectKey>org.gridsuite:dynamic-mapping-server</sonar.projectKey>
<!-- To remove after when using gridsuite dependencies release and ws-commons version containing merged PR: https://github.com/powsybl/powsybl-ws-commons/pull/167 -->
<powsybl-ws-commons.version>1.34.0</powsybl-ws-commons.version>
</properties>

<build>
Expand Down Expand Up @@ -68,6 +70,11 @@
<dependencyManagement>
<dependencies>
<!-- overrides of imports -->
<dependency>
<groupId>com.powsybl</groupId>
<artifactId>powsybl-ws-commons</artifactId>
<version>${powsybl-ws-commons.version}</version>
</dependency>

<!-- imports -->
<dependency>
Expand Down

This file was deleted.

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.mapping.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:dynamic-mapping-server}") String name) {
this.name = name;
}

@Override
public String serverName() {
return name;
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* 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/.
* SPDX-License-Identifier: MPL-2.0
*/
package org.gridsuite.mapping.server.error;

import com.powsybl.ws.commons.error.BusinessErrorCode;

/**
* @author Hugo Marcellin <hugo.marcelin at rte-france.com>
*/
public enum DynamicMappingErrorBusinessCode implements BusinessErrorCode {
MAPPING_NAME_NOT_PROVIDED("dynamicMapping.mappingNameNotProvided");

private final String code;

DynamicMappingErrorBusinessCode(String code) {
this.code = code;
}

public String value() {
return code;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* 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.mapping.server.error;

import com.powsybl.ws.commons.error.AbstractBusinessException;
import jakarta.validation.constraints.NotNull;
import lombok.Getter;

/**
* @author Thang PHAM <quyet-thang.pham at rte-france.com>
*/
@Getter
public class DynamicMappingException extends AbstractBusinessException {
private final DynamicMappingErrorBusinessCode errorCode;

public DynamicMappingException(DynamicMappingErrorBusinessCode errorCode, String message) {
super(message);
this.errorCode = errorCode;
}

@NotNull
@Override
public DynamicMappingErrorBusinessCode getBusinessErrorCode() {
return errorCode;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* 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.mapping.server.error;

import com.powsybl.ws.commons.error.AbstractBusinessExceptionHandler;
import com.powsybl.ws.commons.error.PowsyblWsProblemDetail;
import com.powsybl.ws.commons.error.ServerNameProvider;
import jakarta.servlet.http.HttpServletRequest;
import lombok.NonNull;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

/**
* @author Thang PHAM <quyet-thang.pham at rte-france.com>
*/
@ControllerAdvice
public class DynamicMappingExceptionHandler extends AbstractBusinessExceptionHandler<DynamicMappingException, DynamicMappingErrorBusinessCode> {
protected DynamicMappingExceptionHandler(ServerNameProvider serverNameProvider) {
super(serverNameProvider);
}

@Override
protected @NonNull DynamicMappingErrorBusinessCode getBusinessCode(DynamicMappingException e) {
return e.getBusinessErrorCode();
}

protected HttpStatus mapStatus(DynamicMappingErrorBusinessCode businessErrorCode) {
return switch (businessErrorCode) {
case MAPPING_NAME_NOT_PROVIDED -> HttpStatus.BAD_REQUEST;
};
}

@ExceptionHandler(DynamicMappingException.class)
protected ResponseEntity<PowsyblWsProblemDetail> handleDynamicMappingException(
DynamicMappingException exception, HttpServletRequest request) {
return super.handleDomainException(exception, request);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,16 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.collections4.CollectionUtils;
import org.gridsuite.filter.expertfilter.ExpertFilter;
import org.gridsuite.mapping.server.DynamicMappingException;
import org.gridsuite.mapping.server.service.client.AbstractRestClient;
import org.gridsuite.mapping.server.service.client.filter.FilterClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.*;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.web.client.HttpStatusCodeException;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;

Expand All @@ -26,8 +27,6 @@
import java.util.Map;
import java.util.UUID;

import static org.gridsuite.mapping.server.DynamicMappingException.Type.*;
import static org.gridsuite.mapping.server.service.client.utils.ExceptionUtils.handleHttpError;
import static org.gridsuite.mapping.server.service.client.utils.UrlUtils.buildEndPointUrl;

/**
Expand Down Expand Up @@ -57,20 +56,12 @@ public List<ExpertFilter> getFilters(List<UUID> filterUuids) {
uriComponentsBuilder.queryParam("ids", filterUuids);

// call filter server Rest API
try {
return getRestTemplate().exchange(
uriComponentsBuilder.build().toUriString(),
HttpMethod.GET,
null,
new ParameterizedTypeReference<List<ExpertFilter>>() { }).getBody();

} catch (HttpStatusCodeException e) {
if (HttpStatus.NOT_FOUND.equals(e.getStatusCode())) {
throw new DynamicMappingException(FILTER_NOT_FOUND, "Some filters have not been found");
} else {
throw handleHttpError(e, GET_FILTER_ERROR, getObjectMapper());
}
}
return getRestTemplate().exchange(
uriComponentsBuilder.build().toUriString(),
HttpMethod.GET,
null,
new ParameterizedTypeReference<List<ExpertFilter>>() {
}).getBody();
}

@Override
Expand All @@ -89,15 +80,13 @@ public List<ExpertFilter> createFilters(Map<UUID, ExpertFilter> filtersToCreateM
HttpEntity<Map<UUID, ExpertFilter>> httpEntity = new HttpEntity<>(filtersToCreateMap, headers);

// call filter server Rest API
try {
return getRestTemplate().exchange(
uriComponentsBuilder.build().toUriString(),
HttpMethod.POST,
httpEntity,
new ParameterizedTypeReference<List<ExpertFilter>>() { }).getBody();
} catch (HttpStatusCodeException e) {
throw handleHttpError(e, CREATE_FILTER_ERROR, getObjectMapper());
}
return getRestTemplate().exchange(
uriComponentsBuilder.build().toUriString(),
HttpMethod.POST,
httpEntity,
new ParameterizedTypeReference<List<ExpertFilter>>() {
}).getBody();

}

@Override
Expand All @@ -116,15 +105,13 @@ public List<ExpertFilter> updateFilters(Map<UUID, ExpertFilter> filtersToUpdateM
HttpEntity<Map<UUID, ExpertFilter>> httpEntity = new HttpEntity<>(filtersToUpdateMap, headers);

// call filter server Rest API
try {
return getRestTemplate().exchange(
uriComponentsBuilder.build().toUriString(),
HttpMethod.PUT,
httpEntity,
new ParameterizedTypeReference<List<ExpertFilter>>() { }).getBody();
} catch (HttpStatusCodeException e) {
throw handleHttpError(e, UPDATE_FILTER_ERROR, getObjectMapper());
}
return getRestTemplate().exchange(
uriComponentsBuilder.build().toUriString(),
HttpMethod.PUT,
httpEntity,
new ParameterizedTypeReference<List<ExpertFilter>>() {
}).getBody();

}

@Override
Expand All @@ -143,15 +130,13 @@ public Map<UUID, UUID> duplicateFilters(List<UUID> filterUuids) {
HttpEntity<List<UUID>> httpEntity = new HttpEntity<>(filterUuids, headers);

// call filter server Rest API
try {
return getRestTemplate().exchange(
uriComponentsBuilder.build().toUriString(),
HttpMethod.POST,
httpEntity,
new ParameterizedTypeReference<Map<UUID, UUID>>() { }).getBody();
} catch (HttpStatusCodeException e) {
throw handleHttpError(e, DUPLICATE_FILTER_ERROR, getObjectMapper());
}
return getRestTemplate().exchange(
uriComponentsBuilder.build().toUriString(),
HttpMethod.POST,
httpEntity,
new ParameterizedTypeReference<Map<UUID, UUID>>() {
}).getBody();

}

@Override
Expand All @@ -170,14 +155,11 @@ public void deleteFilters(List<UUID> filterUuids) {
HttpEntity<List<UUID>> httpEntity = new HttpEntity<>(filterUuids, headers);

// call filter server Rest API
try {
getRestTemplate().exchange(
uriComponentsBuilder.build().toUriString(),
HttpMethod.DELETE,
httpEntity,
Void.class);
} catch (HttpStatusCodeException e) {
throw handleHttpError(e, DELETE_FILTER_ERROR, getObjectMapper());
}
getRestTemplate().exchange(
uriComponentsBuilder.build().toUriString(),
HttpMethod.DELETE,
httpEntity,
Void.class);

}
}
Loading