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

add MODIFICATION type #72

Merged
merged 6 commits into from
Feb 13, 2024
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
10 changes: 10 additions & 0 deletions src/main/java/org/gridsuite/explore/server/ExploreController.java
Original file line number Diff line number Diff line change
Expand Up @@ -306,4 +306,14 @@ public ResponseEntity<Void> duplicateParameters(@RequestParam("duplicateFrom") U
exploreService.duplicateParameters(parentParameterId, parametersType, parametersName, parentDirectoryUuid, userId);
return ResponseEntity.ok().build();
}

@PostMapping(value = "/explore/modifications")
@Operation(summary = "create some modification elements from existing network modifications")
@ApiResponses(value = {@ApiResponse(responseCode = "200", description = "Modifications have been duplicated and corresponding elements created in the directory")})
public ResponseEntity<Void> createNetworkModifications(@RequestBody List<ElementAttributes> bodyContent,
@RequestParam(QUERY_PARAM_PARENT_DIRECTORY_ID) UUID parentDirectoryUuid,
@RequestHeader("userId") String userId) {
exploreService.createNetworkModifications(bodyContent, userId, parentDirectoryUuid);
return ResponseEntity.ok().build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public class DirectoryService implements IDirectoryElementsService {

@Autowired
public DirectoryService(
FilterService filterService, ContingencyListService contingencyListService, StudyService studyService,
FilterService filterService, ContingencyListService contingencyListService, StudyService studyService, NetworkModificationService networkModificationService,
CaseService caseService, ParametersService parametersService, RestTemplate restTemplate, RemoteServicesProperties remoteServicesProperties) {
this.directoryServerBaseUri = remoteServicesProperties.getServiceUri("directory-server");
this.restTemplate = restTemplate;
Expand All @@ -57,6 +57,7 @@ public DirectoryService(
CONTINGENCY_LIST, contingencyListService,
STUDY, studyService,
DIRECTORY, this,
MODIFICATION, networkModificationService,
CASE, caseService,
ParametersType.VOLTAGE_INIT_PARAMETERS.name(), parametersService,
ParametersType.SECURITY_ANALYSIS_PARAMETERS.name(), parametersService);
Expand All @@ -67,9 +68,13 @@ public void setDirectoryServerBaseUri(String directoryServerBaseUri) {
}

public ElementAttributes createElement(ElementAttributes elementAttributes, UUID directoryUuid, String userId) {
return createElementWithNewName(elementAttributes, directoryUuid, userId, false);
}

public ElementAttributes createElementWithNewName(ElementAttributes elementAttributes, UUID directoryUuid, String userId, boolean allowNewName) {
String path = UriComponentsBuilder
.fromPath(DIRECTORIES_SERVER_ROOT_PATH + "/{directoryUuid}/elements")
.buildAndExpand(directoryUuid)
.fromPath(DIRECTORIES_SERVER_ROOT_PATH + "/{directoryUuid}/elements?allowNewName={allowNewName}")
.buildAndExpand(directoryUuid, allowNewName)
.toUriString();
HttpHeaders headers = new HttpHeaders();
headers.add(HEADER_USER_ID, userId);
Expand All @@ -92,7 +97,7 @@ public void deleteDirectoryElement(UUID elementUuid, String userId) {

public ElementAttributes getElementInfos(UUID elementUuid) {
String path = UriComponentsBuilder
.fromPath(ELEMENTS_SERVER_ROOT_PATH + "/{directoryUuid}")
.fromPath(ELEMENTS_SERVER_ROOT_PATH + "/{elementUuid}")
.buildAndExpand(elementUuid)
.toUriString();
return restTemplate.exchange(directoryServerBaseUri + path, HttpMethod.GET, null, ElementAttributes.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;
import java.util.UUID;

Expand All @@ -33,11 +35,13 @@ public class ExploreService {
static final String CASE = "CASE";
static final String CONTINGENCY_LIST = "CONTINGENCY_LIST";
static final String FILTER = "FILTER";
static final String MODIFICATION = "MODIFICATION";
static final String DIRECTORY = "DIRECTORY";

private DirectoryService directoryService;
private StudyService studyService;
private ContingencyListService contingencyListService;
private NetworkModificationService networkModificationService;
private FilterService filterService;
private CaseService caseService;
private ParametersService parametersService;
Expand All @@ -49,13 +53,15 @@ public ExploreService(
StudyService studyService,
ContingencyListService contingencyListService,
FilterService filterService,
NetworkModificationService networkModificationService,
CaseService caseService,
ParametersService parametersService) {

this.directoryService = directoryService;
this.studyService = studyService;
this.contingencyListService = contingencyListService;
this.filterService = filterService;
this.networkModificationService = networkModificationService;
this.caseService = caseService;
this.parametersService = parametersService;
}
Expand Down Expand Up @@ -248,4 +254,21 @@ public void duplicateParameters(UUID parentParameterId, ParametersType parameter
null, userId, 0L, null);
directoryService.createElement(elementAttributes, parentDirectoryUuid, userId);
}

public void createNetworkModifications(List<ElementAttributes> modificationAttributesList, String userId, UUID parentDirectoryUuid) {
List<UUID> existingModificationsUuids = modificationAttributesList.stream()
.map(ElementAttributes::getElementUuid)
.toList();

// create all duplicated modifications
Map<UUID, UUID> newModificationsUuids = networkModificationService.createModifications(existingModificationsUuids);

// create all corresponding directory elements
modificationAttributesList.forEach(m -> {
final UUID newId = newModificationsUuids.get(m.getElementUuid());
ElementAttributes elementAttributes = new ElementAttributes(newId, m.getElementName(), MODIFICATION,
null, userId, 0L, m.getDescription());
directoryService.createElementWithNewName(elementAttributes, parentDirectoryUuid, userId, true);
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/**
* 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.explore.server.services;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.ParameterizedTypeReference;
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.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;

import java.io.UncheckedIOException;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.stream.Collectors;

/**
* @author David Braquart <david.braquart at rte-france.com>
*/
@Service
public class NetworkModificationService implements IDirectoryElementsService {
private static final String NETWORK_MODIFICATION_API_VERSION = "v1";
private static final String DELIMITER = "/";
private static final String HEADER_USER_ID = "userId";
public static final String UUIDS = "uuids";
private static final String NETWORK_MODIFICATIONS_PATH = "network-modifications";
private String networkModificationServerBaseUri;
private final RestTemplate restTemplate;
private final ObjectMapper objectMapper;

@Autowired
public NetworkModificationService(RestTemplate restTemplate, RemoteServicesProperties remoteServicesProperties, ObjectMapper objectMapper) {
this.networkModificationServerBaseUri = remoteServicesProperties.getServiceUri("network-modification-server");
this.restTemplate = restTemplate;
this.objectMapper = objectMapper;
}

public void setNetworkModificationServerBaseUri(String networkModificationServerBaseUri) {
this.networkModificationServerBaseUri = networkModificationServerBaseUri;
}

public Map<UUID, UUID> createModifications(List<UUID> modificationUuids) {
String path = UriComponentsBuilder.fromPath(DELIMITER + NETWORK_MODIFICATION_API_VERSION + DELIMITER + NETWORK_MODIFICATIONS_PATH + "/duplicate")
.buildAndExpand()
.toUriString();
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<String> httpEntity;
try {
httpEntity = new HttpEntity<>(objectMapper.writeValueAsString(modificationUuids), headers);
} catch (JsonProcessingException e) {
throw new UncheckedIOException(e);
}
return restTemplate.exchange(networkModificationServerBaseUri + path, HttpMethod.POST, httpEntity, new ParameterizedTypeReference<Map<UUID, UUID>>() { })
.getBody();
}

@Override
public void delete(UUID id, String userId) {
String path = UriComponentsBuilder.fromPath(DELIMITER + NETWORK_MODIFICATION_API_VERSION + DELIMITER + NETWORK_MODIFICATIONS_PATH)
.queryParam(UUIDS, List.of(id))
.buildAndExpand()
.toUriString();
HttpHeaders headers = new HttpHeaders();
headers.add(HEADER_USER_ID, userId);
restTemplate.exchange(networkModificationServerBaseUri + path, HttpMethod.DELETE, new HttpEntity<>(headers), Void.class);
}

@Override
public List<Map<String, Object>> getMetadata(List<UUID> modificationUuids) {
var ids = modificationUuids.stream().map(UUID::toString).collect(Collectors.joining(","));
String path = UriComponentsBuilder
.fromPath(DELIMITER + NETWORK_MODIFICATION_API_VERSION + DELIMITER + NETWORK_MODIFICATIONS_PATH + "/metadata" + "?ids=" + ids)
.buildAndExpand()
.toUriString();
return restTemplate.exchange(networkModificationServerBaseUri + path, HttpMethod.GET, null,
new ParameterizedTypeReference<List<Map<String, Object>>>() {
}).getBody();
}
}
5 changes: 4 additions & 1 deletion src/main/resources/application-local.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ gridsuite:
-
name: actions-server
base-uri: http://localhost:5022
-
-
name: network-modification-server
base-uri: http://172.17.0.1:5007
-
name: filter-server
base-uri: http://localhost:5027
-
Expand Down
Loading
Loading