Skip to content

Commit

Permalink
add MODIFICATION type (todo dup/copy/paste)
Browse files Browse the repository at this point in the history
  • Loading branch information
dbraquart committed Feb 6, 2024
1 parent 0164569 commit c85552b
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 5 deletions.
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/network-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);
}
Expand All @@ -66,9 +67,13 @@ public void setDirectoryServerBaseUri(String directoryServerBaseUri) {
}

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

public ElementAttributes createNewElement(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 Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

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

Expand All @@ -33,11 +37,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 +55,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 +256,26 @@ 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) {
// This is important to sort on uuid, to make sure the input modification list will match the new/cloned modification list
List<ElementAttributes> sortedInputList = modificationAttributesList.stream()
.sorted(Comparator.comparing(ElementAttributes::getElementUuid))
.toList();
List<UUID> existingSortedModificationsUuids = sortedInputList.stream().map(ElementAttributes::getElementUuid).toList();

// create all duplicated modifications
List<UUID> newSortedModificationsUuids = networkModificationService.createModifications(existingSortedModificationsUuids);

// Iterate through both collection simultaneously (they have the same order)
Iterator<UUID> newUuidIterator = newSortedModificationsUuids.iterator();
Iterator<ElementAttributes> modificationAttributeIterator = sortedInputList.iterator();
while (newUuidIterator.hasNext() && modificationAttributeIterator.hasNext()) {
final UUID newid = newUuidIterator.next();
final ElementAttributes attributes = modificationAttributeIterator.next();
ElementAttributes elementAttributes = new ElementAttributes(newid, attributes.getElementName(), MODIFICATION,
null, userId, 0L, attributes.getDescription());
directoryService.createNewElement(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 List<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<List<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();
}
}
6 changes: 4 additions & 2 deletions src/main/resources/application-local.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@ 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
-
name: voltage-init-server
base-uri: http://localhost:5038

0 comments on commit c85552b

Please sign in to comment.