Skip to content
Closed
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 @@ -24,6 +24,7 @@
import org.gridsuite.actions.server.entities.*;
import org.gridsuite.actions.server.repositories.FilterBasedContingencyListRepository;
import org.gridsuite.actions.server.repositories.IdBasedContingencyListRepository;
import org.gridsuite.actions.server.service.DirectoryService;
import org.gridsuite.actions.server.service.FilterService;
import org.gridsuite.actions.server.utils.ContingencyListType;
import org.gridsuite.actions.server.utils.ContingencyListUtils;
Expand Down Expand Up @@ -58,16 +59,20 @@ public class ContingencyListService {

private final FilterService filterService;

private final DirectoryService directoryService;

public ContingencyListService(IdBasedContingencyListRepository idBasedContingencyListRepository,
FilterBasedContingencyListRepository filterBasedContingencyListRepository,
NetworkStoreService networkStoreService,
NotificationService notificationService,
FilterService filterService) {
FilterService filterService,
DirectoryService directoryService) {
this.idBasedContingencyListRepository = idBasedContingencyListRepository;
this.filterBasedContingencyListRepository = filterBasedContingencyListRepository;
this.networkStoreService = networkStoreService;
this.notificationService = notificationService;
this.filterService = filterService;
this.directoryService = directoryService;
}

ContingencyListMetadata fromContingencyListEntity(AbstractContingencyEntity entity, ContingencyListType type) {
Expand Down Expand Up @@ -200,7 +205,19 @@ private List<Contingency> getContingencies(PersistentContingencyList persistentC
@Transactional(readOnly = true)
public List<ContingencyInfos> exportContingencyInfosList(List<UUID> ids, UUID networkUuid, String variantId) {
Network network = getNetworkFromUuid(networkUuid, variantId);
return ids.stream().map(id -> evaluateContingencyList(findContingencyList(id, network), network, networkUuid, variantId)).flatMap(Collection::stream).toList();
return ids.stream().map(id -> {
try {
return evaluateContingencyList(findContingencyList(id, network), network, networkUuid, variantId);
} catch (PowsyblException powsyblEx) {
String contingencyListName;
try {
contingencyListName = directoryService.getElementName(id);
} catch (Exception e) {
contingencyListName = id.toString();
}
throw new PowsyblException("Error in contingency list: '" + contingencyListName + "': " + powsyblEx.getMessage());
}
}).flatMap(Collection::stream).toList();
}

private PersistentContingencyList findContingencyList(UUID id, Network network) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/**
* Copyright (c) 2026, 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.actions.server.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;

import java.net.URI;
import java.util.Map;
import java.util.Objects;
import java.util.UUID;

/**
* @author Caroline Jeandat {@literal <caroline.jeandat at rte-france.com>}
*/
@Service
public class DirectoryService {
static final String DIRECTORY_API_VERSION = "v1";
private static final String DELIMITER = "/";

private final String baseUri;
private final RestTemplate restTemplate;

@Autowired
public DirectoryService(
@Value("${gridsuite.services.directory-server.base-uri:http://directory-server}") String baseUri,
RestTemplateBuilder restTemplateBuilder) {
this.baseUri = baseUri;
this.restTemplate = restTemplateBuilder.build();
}

public String getElementName(UUID elementUuid) {
Objects.requireNonNull(elementUuid);

URI path = UriComponentsBuilder
.fromPath(DELIMITER + DIRECTORY_API_VERSION + "/elements/{elementUuid}")
.buildAndExpand(elementUuid)
.toUri();

try {
ResponseEntity<Map<String, Object>> response =
restTemplate.exchange(
baseUri + path,
HttpMethod.GET,
null,
new ParameterizedTypeReference<>() { }
);

Map<String, Object> responseBody = response.getBody();
return responseBody != null ? (String) responseBody.get("elementName") : null;

} catch (HttpClientErrorException.NotFound e) {
return null;
}
}
}
2 changes: 2 additions & 0 deletions src/main/resources/application-local.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,5 @@ gridsuite:
services:
filter-server:
base-uri: http://localhost:5027
directory-server:
base-uri: http://localhost:5026