Skip to content

Commit

Permalink
Merge branch 'main' into tck_fix_notification_on_terminal_connection_…
Browse files Browse the repository at this point in the history
…disconnection
  • Loading branch information
FranckLecuyer authored Sep 24, 2024
2 parents 9a48283 + 832cd8b commit 47ddfb4
Show file tree
Hide file tree
Showing 25 changed files with 2,069 additions and 201 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -871,4 +871,16 @@ public void removeConfiguredBuses(UUID networkUuid, int variantNum, List<String>
ensureCached(ResourceType.CONFIGURED_BUS, networkUuid, variantNum);
delegate.removeConfiguredBuses(networkUuid, variantNum, configuredBusesId);
}

@Override
public Optional<ExtensionAttributes> getExtensionAttributes(UUID networkUuid, int variantNum, ResourceType resourceType, String identifiableId, String extensionName) {
delegate.getAllExtensionsAttributesByResourceTypeAndExtensionName(networkUuid, variantNum, resourceType, extensionName);
return delegate.getExtensionAttributes(networkUuid, variantNum, resourceType, identifiableId, extensionName);
}

@Override
public Map<String, ExtensionAttributes> getAllExtensionsAttributesByIdentifiableId(UUID networkUuid, int variantNum, ResourceType resourceType, String id) {
delegate.getAllExtensionsAttributesByResourceType(networkUuid, variantNum, resourceType);
return delegate.getAllExtensionsAttributesByIdentifiableId(networkUuid, variantNum, resourceType, id);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package com.powsybl.network.store.client;

import com.powsybl.network.store.model.Attributes;
import com.powsybl.network.store.model.ExtensionAttributes;
import com.powsybl.network.store.model.IdentifiableAttributes;
import com.powsybl.network.store.model.Resource;
import org.springframework.core.ParameterizedTypeReference;
Expand All @@ -23,6 +24,13 @@ public interface RestClient {

<T extends IdentifiableAttributes> Optional<Resource<T>> getOne(String target, String url, Object... uriVariables);

/**
* Retrieves one extension attributes from the server.
* @return {@link ExtensionAttributes} which is a subset of an identifiable resource. The extension attributes can be put in the extensionAttributes
* map of an {@link IdentifiableAttributes} or used to load an extension.
*/
Optional<ExtensionAttributes> getOneExtensionAttributes(String url, Object... uriVariables);

<T extends IdentifiableAttributes> List<Resource<T>> getAll(String target, String url, Object... uriVariables);

<T extends Attributes> void updateAll(String url, List<Resource<T>> resources, Object... uriVariables);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,16 @@ private static MappingJackson2HttpMessageConverter createMapping() {
return converter;
}

private <T extends IdentifiableAttributes> ResponseEntity<TopLevelDocument<T>> getDocument(String url, Object... uriVariables) {
private <T, D extends AbstractTopLevelDocument<T>> ResponseEntity<D> getDocument(String url, ParameterizedTypeReference<D> parameterizedTypeReference, Object... uriVariables) {
return restTemplate.exchange(url,
HttpMethod.GET,
new HttpEntity<>(new HttpHeaders()),
new ParameterizedTypeReference<TopLevelDocument<T>>() { // this unnecessary type should not be removed!!! https://github.com/powsybl/powsybl-network-store/commit/9744168f47210eab11796861f9dcf4ffdd5aea0c
},
parameterizedTypeReference,
uriVariables);
}

private static <T extends IdentifiableAttributes> TopLevelDocument<T> getBody(ResponseEntity<TopLevelDocument<T>> response) {
TopLevelDocument<T> body = response.getBody();
private static <T, D extends AbstractTopLevelDocument<T>> D getBody(ResponseEntity<D> response) {
D body = response.getBody();
if (body == null) {
throw new PowsyblException("Body is null");
}
Expand All @@ -92,9 +91,20 @@ public <T extends IdentifiableAttributes> void createAll(String url, List<Resour

@Override
public <T extends IdentifiableAttributes> Optional<Resource<T>> getOne(String target, String url, Object... uriVariables) {
ResponseEntity<TopLevelDocument<T>> response = getDocument(url, uriVariables);
return getOneDocument(url, new ParameterizedTypeReference<TopLevelDocument<T>>() {
}, uriVariables);
}

@Override
public Optional<ExtensionAttributes> getOneExtensionAttributes(String url, Object... uriVariables) {
return getOneDocument(url, new ParameterizedTypeReference<ExtensionAttributesTopLevelDocument>() {
}, uriVariables);
}

private <T, D extends AbstractTopLevelDocument<T>> Optional<T> getOneDocument(String url, ParameterizedTypeReference<D> parameterizedTypeReference, Object... uriVariables) {
ResponseEntity<D> response = getDocument(url, parameterizedTypeReference, uriVariables);
if (response.getStatusCode() == HttpStatus.OK) {
TopLevelDocument<T> body = getBody(response);
AbstractTopLevelDocument<T> body = getBody(response);
return Optional.of(body.getData().get(0));
} else if (response.getStatusCode() == HttpStatus.NOT_FOUND) {
return Optional.empty();
Expand All @@ -105,7 +115,8 @@ public <T extends IdentifiableAttributes> Optional<Resource<T>> getOne(String ta

@Override
public <T extends IdentifiableAttributes> List<Resource<T>> getAll(String target, String url, Object... uriVariables) {
ResponseEntity<TopLevelDocument<T>> response = getDocument(url, uriVariables);
ResponseEntity<TopLevelDocument<T>> response = getDocument(url, new ParameterizedTypeReference<>() {
}, uriVariables);
if (response.getStatusCode() != HttpStatus.OK) {
throw createHttpException(url, "get", response.getStatusCode());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,49 @@ private <T extends IdentifiableAttributes> Optional<Resource<T>> get(String targ
return resource;
}

private Optional<ExtensionAttributes> getExtensionAttributes(String urlTemplate, Object... uriVariables) {
logGetExtensionAttributesUrl(urlTemplate, uriVariables);
Stopwatch stopwatch = Stopwatch.createStarted();
Optional<ExtensionAttributes> extensionAttributes = restClient.getOneExtensionAttributes(urlTemplate, uriVariables);
stopwatch.stop();
logGetExtensionAttributesTime(extensionAttributes.isPresent() ? 1 : 0, stopwatch.elapsed(TimeUnit.MILLISECONDS));

return extensionAttributes;
}

private Map<String, ExtensionAttributes> getExtensionAttributesMap(String urlTemplate, Object... uriVariables) {
logGetExtensionAttributesUrl(urlTemplate, uriVariables);
Stopwatch stopwatch = Stopwatch.createStarted();
Map<String, ExtensionAttributes> extensionAttributes = restClient.get(urlTemplate, new ParameterizedTypeReference<>() { }, uriVariables);
stopwatch.stop();
logGetExtensionAttributesTime(extensionAttributes.size(), stopwatch.elapsed(TimeUnit.MILLISECONDS));

return extensionAttributes;
}

private Map<String, Map<String, ExtensionAttributes>> getExtensionAttributesNestedMap(String urlTemplate, Object... uriVariables) {
logGetExtensionAttributesUrl(urlTemplate, uriVariables);
Stopwatch stopwatch = Stopwatch.createStarted();
Map<String, Map<String, ExtensionAttributes>> extensionAttributes = restClient.get(urlTemplate, new ParameterizedTypeReference<>() { }, uriVariables);
stopwatch.stop();
long loadedAttributesCount = extensionAttributes.values().stream()
.mapToLong(innerMap -> innerMap.values().size())
.sum();
logGetExtensionAttributesTime(loadedAttributesCount, stopwatch.elapsed(TimeUnit.MILLISECONDS));

return extensionAttributes;
}

private static void logGetExtensionAttributesUrl(String urlTemplate, Object... uriVariables) {
if (LOGGER.isInfoEnabled()) {
LOGGER.info("Loading extension attributes {}", UriComponentsBuilder.fromUriString(urlTemplate).build(uriVariables));
}
}

private static void logGetExtensionAttributesTime(long loadedAttributesCount, long timeElapsed) {
LOGGER.info("{} extension attributes loaded in {} ms", loadedAttributesCount, timeElapsed);
}

private <T extends IdentifiableAttributes> void updatePartition(String target, String url, AttributeFilter attributeFilter, List<Resource<T>> resources, Object[] uriVariables) {
if (attributeFilter == null) {
if (LOGGER.isInfoEnabled()) {
Expand Down Expand Up @@ -848,4 +891,29 @@ public void updateGrounds(UUID networkUuid, List<Resource<GroundAttributes>> gro
AttributeFilter attributeFilter) {
updateAll(STR_GROUND, "/networks/{networkUuid}/grounds", groundResources, attributeFilter, networkUuid);
}

@Override
public Optional<ExtensionAttributes> getExtensionAttributes(UUID networkUuid, int variantNum, ResourceType resourceType, String identifiableId, String extensionName) {
return getExtensionAttributes("/networks/{networkUuid}/{variantNum}/identifiables/{identifiableId}/extensions/{extensionName}", networkUuid, variantNum, identifiableId, extensionName);
}

@Override
public Map<String, ExtensionAttributes> getAllExtensionsAttributesByResourceTypeAndExtensionName(UUID networkUuid, int variantNum, ResourceType resourceType, String extensionName) {
return getExtensionAttributesMap("/networks/{networkUuid}/{variantNum}/identifiables/types/{type}/extensions/{extensionName}", networkUuid, variantNum, resourceType, extensionName);
}

@Override
public Map<String, ExtensionAttributes> getAllExtensionsAttributesByIdentifiableId(UUID networkUuid, int variantNum, ResourceType resourceType, String identifiableId) {
return getExtensionAttributesMap("/networks/{networkUuid}/{variantNum}/identifiables/{identifiableId}/extensions", networkUuid, variantNum, identifiableId);
}

@Override
public Map<String, Map<String, ExtensionAttributes>> getAllExtensionsAttributesByResourceType(UUID networkUuid, int variantNum, ResourceType resourceType) {
return getExtensionAttributesNestedMap("/networks/{networkUuid}/{variantNum}/identifiables/types/{resourceType}/extensions", networkUuid, variantNum, resourceType);
}

@Override
public void removeExtensionAttributes(UUID networkUuid, int variantNum, ResourceType resourceType, String identifiableId, String extensionName) {
restClient.delete("/networks/{networkUuid}/{variantNum}/identifiables/{identifiableId}/extensions/{extensionName}", networkUuid, variantNum, identifiableId, extensionName);
}
}
Loading

0 comments on commit 47ddfb4

Please sign in to comment.