Skip to content

Commit

Permalink
Merge branch 'main' into enable-prometheus
Browse files Browse the repository at this point in the history
  • Loading branch information
antoinebhs authored May 22, 2024
2 parents 8825c39 + d87a7ec commit 96586c7
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 42 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

<log4j2-mock-version>0.0.2</log4j2-mock-version>
<powsybl-ws-dependencies.version>2.9.0</powsybl-ws-dependencies.version>
<powsybl-ws-dependencies.version>2.10.0</powsybl-ws-dependencies.version>
<testcontainers.version>1.16.2</testcontainers.version>
</properties>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@
import com.powsybl.cases.datasource.CaseDataSourceClient;
import com.powsybl.cgmes.conversion.export.CgmesExportContext;
import com.powsybl.cgmes.conversion.export.StateVariablesExport;
import com.powsybl.cgmes.extensions.CgmesSshMetadata;
import com.powsybl.cgmes.extensions.CgmesSvMetadata;
import com.powsybl.cgmes.extensions.CgmesMetadataModels;
import com.powsybl.cgmes.model.CgmesMetadataModel;
import com.powsybl.cgmes.model.CgmesSubset;
import com.powsybl.commons.PowsyblException;
import com.powsybl.commons.datasource.MemDataSource;
import com.powsybl.commons.parameters.ParameterScope;
import com.powsybl.commons.reporter.Reporter;
import com.powsybl.commons.reporter.ReporterModel;
import com.powsybl.commons.reporter.ReporterModelDeserializer;
import com.powsybl.commons.reporter.ReporterModelJsonModule;
import com.powsybl.commons.report.ReportNode;
import com.powsybl.commons.report.ReportNodeDeserializer;
import com.powsybl.commons.report.ReportNodeJsonModule;
import com.powsybl.commons.xml.XmlUtil;
import com.powsybl.iidm.network.*;
import com.powsybl.network.conversion.server.dto.*;
Expand Down Expand Up @@ -123,8 +123,8 @@ public NetworkConversionService(@Value("${powsybl.services.case-server.base-uri:
reportServerRest.setUriTemplateHandler(new DefaultUriBuilderFactory(reportServerURI));

objectMapper = Jackson2ObjectMapperBuilder.json().build();
objectMapper.registerModule(new ReporterModelJsonModule());
objectMapper.setInjectableValues(new InjectableValues.Std().addValue(ReporterModelDeserializer.DICTIONARY_VALUE_ID, null));
objectMapper.registerModule(new ReportNodeJsonModule());
objectMapper.setInjectableValues(new InjectableValues.Std().addValue(ReportNodeDeserializer.DICTIONARY_VALUE_ID, null));
}

static EquipmentInfos toEquipmentInfos(Identifiable<?> i, UUID networkUuid, String variantId) {
Expand Down Expand Up @@ -189,18 +189,24 @@ Consumer<Message<UUID>> consumeCaseImportStart() {
NetworkInfos importCase(UUID caseUuid, String variantId, UUID reportUuid, String caseFormat, Map<String, Object> importParameters) {
CaseDataSourceClient dataSource = new CaseDataSourceClient(caseServerRest, caseUuid);

Reporter rootReporter = Reporter.NO_OP;
Reporter reporter = Reporter.NO_OP;
ReportNode rootReport = ReportNode.NO_OP;
ReportNode reporter = ReportNode.NO_OP;
if (reportUuid != null) {
String reporterId = "Root@" + IMPORT_TYPE_REPORT;
rootReporter = new ReporterModel(reporterId, reporterId);
rootReport = ReportNode.newRootReportNode()
.withMessageTemplate(reporterId, reporterId)
.build();

String subReporterId = "Import Case : " + dataSource.getBaseName();
reporter = rootReporter.createSubReporter(subReporterId, subReporterId);
reporter = rootReport.newReportNode()
.withMessageTemplate(subReporterId, subReporterId)
.add();

}

AtomicReference<Long> startTime = new AtomicReference<>(System.nanoTime());
Network network;
Reporter finalReporter = reporter;
ReportNode finalReporter = reporter;
if (!importParameters.isEmpty()) {
Properties importProperties = new Properties();
importProperties.putAll(importParameters);
Expand All @@ -210,11 +216,11 @@ NetworkInfos importCase(UUID caseUuid, String variantId, UUID reportUuid, String
}
UUID networkUuid = networkStoreService.getNetworkUuid(network);
LOGGER.trace("Import network '{}' : {} seconds", networkUuid, TimeUnit.NANOSECONDS.toSeconds(System.nanoTime() - startTime.get()));
saveNetwork(network, networkUuid, variantId, rootReporter, reportUuid);
saveNetwork(network, networkUuid, variantId, rootReport, reportUuid);
return new NetworkInfos(networkUuid, network.getId());
}

private void saveNetwork(Network network, UUID networkUuid, String variantId, Reporter reporter, UUID reportUuid) {
private void saveNetwork(Network network, UUID networkUuid, String variantId, ReportNode reporter, UUID reportUuid) {
CompletableFuture<Void> saveInParallel;
if (reportUuid == null) {
saveInParallel = CompletableFuture.allOf(
Expand Down Expand Up @@ -396,8 +402,14 @@ public ExportNetworkInfos exportCgmesSv(UUID networkUuid) throws XMLStreamExcept
private static CgmesExportContext createContext(Network network) {
CgmesExportContext context = new CgmesExportContext();
context.setScenarioTime(network.getCaseDate());
context.getSvModelDescription().addDependencies(network.getExtension(CgmesSvMetadata.class).getDependencies());
context.getSshModelDescription().addDependencies(network.getExtension(CgmesSshMetadata.class).getDependencies());
Optional<CgmesMetadataModel> cgmesMetadataModelOpt = network.getExtension(CgmesMetadataModels.class).getModelForSubset(CgmesSubset.STATE_VARIABLES);
if (cgmesMetadataModelOpt.isPresent()) {
context.getExportedSVModel().addDependentOn(cgmesMetadataModelOpt.get().getId());
}
cgmesMetadataModelOpt = network.getExtension(CgmesMetadataModels.class).getModelForSubset(CgmesSubset.STEADY_STATE_HYPOTHESIS);
if (cgmesMetadataModelOpt.isPresent()) {
context.getExportedSSHModel().addDependentOn(cgmesMetadataModelOpt.get().getId());
}
context.addIidmMappings(network);
return context;
}
Expand Down Expand Up @@ -440,14 +452,14 @@ private void insertEquipmentIndexes(Network network, UUID networkUuid, String va
}
}

private void sendReport(UUID networkUuid, Reporter reporter, UUID reportUuid) {
private void sendReport(UUID networkUuid, ReportNode reportNode, UUID reportUuid) {
AtomicReference<Long> startTime = new AtomicReference<>(System.nanoTime());
var headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
var resourceUrl = DELIMITER + REPORT_API_VERSION + DELIMITER + "reports" + DELIMITER + reportUuid.toString();
var uriBuilder = UriComponentsBuilder.fromPath(resourceUrl);
try {
reportServerRest.exchange(uriBuilder.toUriString(), HttpMethod.PUT, new HttpEntity<>(objectMapper.writeValueAsString(reporter), headers), ReporterModel.class);
reportServerRest.exchange(uriBuilder.toUriString(), HttpMethod.PUT, new HttpEntity<>(objectMapper.writeValueAsString(reportNode), headers), ReportNode.class);
} catch (JsonProcessingException error) {
throw new PowsyblException("error creating report", error);
} finally {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,14 @@
import com.powsybl.commons.datasource.ReadOnlyDataSource;
import com.powsybl.commons.datasource.ResourceDataSource;
import com.powsybl.commons.datasource.ResourceSet;
import com.powsybl.commons.reporter.Reporter;
import com.powsybl.commons.reporter.ReporterModel;
import com.powsybl.commons.report.ReportNode;
import com.powsybl.iidm.network.*;
import com.powsybl.iidm.serde.XMLImporter;
import com.powsybl.network.conversion.server.dto.*;
import com.powsybl.network.store.client.NetworkStoreService;
import com.powsybl.network.store.client.PreloadingStrategy;
import com.powsybl.network.store.iidm.impl.NetworkFactoryImpl;
import com.powsybl.network.store.iidm.impl.NetworkImpl;
import org.apache.commons.compress.utils.IOUtils;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand Down Expand Up @@ -109,7 +107,7 @@ public void setup() {
@Test
public void test() throws Exception {
try (InputStream inputStream = getClass().getResourceAsStream("/testCase.xiidm")) {
byte[] networkByte = IOUtils.toByteArray(inputStream);
byte[] networkByte = inputStream.readAllBytes();

given(caseServerRest.exchange(any(String.class), any(HttpMethod.class), any(HttpEntity.class), any(Class.class)))
.willReturn(new ResponseEntity<>(networkByte, HttpStatus.OK));
Expand All @@ -118,12 +116,12 @@ public void test() throws Exception {
new ResourceSet("", "testCase.xiidm"));
Network network = new XMLImporter().importData(dataSource, new NetworkFactoryImpl(), null);

given(networkStoreClient.importNetwork(any(ReadOnlyDataSource.class), any(Reporter.class), any(Boolean.class))).willReturn(network);
given(networkStoreClient.importNetwork(any(ReadOnlyDataSource.class), any(ReportNode.class), any(Boolean.class))).willReturn(network);
UUID randomUuid = UUID.fromString("78e13f90-f351-4c2e-a383-2ad08dd5f8fb");
given(networkStoreClient.getNetworkUuid(network)).willReturn(randomUuid);

UUID reportUuid = UUID.fromString("11111111-f351-4c2e-a383-2ad08dd5f8fb");
given(reportServerRest.exchange(eq("/v1/reports/" + reportUuid), eq(HttpMethod.PUT), any(HttpEntity.class), eq(ReporterModel.class)))
given(reportServerRest.exchange(eq("/v1/reports/" + reportUuid), eq(HttpMethod.PUT), any(HttpEntity.class), eq(ReportNode.class)))
.willReturn(new ResponseEntity<>(HttpStatus.OK));

String caseUuid = UUID.randomUUID().toString();
Expand Down Expand Up @@ -198,7 +196,6 @@ public void test() throws Exception {
.andReturn();
String exported2 = mvcResult.getResponse().getContentAsString(StandardCharsets.UTF_8);

//assertEquals("attachment; filename*=UTF-8''20140116_0830_2D4_UX1_pst_second_variant_id.xiidm", mvcResult.getResponse().getHeader("content-disposition"));
assertTrue(Objects.requireNonNull(mvcResult.getResponse().getHeader("content-disposition")).contains("attachment;"));
assertTrue(Objects.requireNonNull(mvcResult.getResponse().getHeader("content-disposition")).contains("filename*=UTF-8''20140116_0830_2D4_UX1_pst_second_variant_id.xiidm"));
assertTrue(mvcResult.getResponse().getContentAsString().startsWith("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"));
Expand Down Expand Up @@ -252,7 +249,7 @@ public void test() throws Exception {
Map<String, Object> importParameters = new HashMap<>();
importParameters.put("randomImportParameters", "randomImportValue");

given(networkStoreClient.importNetwork(any(ReadOnlyDataSource.class), any(Reporter.class), any(Properties.class), any(Boolean.class))).willReturn(network);
given(networkStoreClient.importNetwork(any(ReadOnlyDataSource.class), any(ReportNode.class), any(Properties.class), any(Boolean.class))).willReturn(network);

mvc.perform(post("/v1/networks")
.contentType(MediaType.APPLICATION_JSON)
Expand Down Expand Up @@ -291,7 +288,7 @@ public void testAsyncImport() throws Exception {
String caseUuid = UUID.randomUUID().toString();
String receiver = "test receiver";
given(networkStoreClient.getNetworkUuid(network)).willReturn(randomUuid);
given(networkStoreClient.importNetwork(any(ReadOnlyDataSource.class), any(Reporter.class), any(Properties.class), any(Boolean.class))).willReturn(network);
given(networkStoreClient.importNetwork(any(ReadOnlyDataSource.class), any(ReportNode.class), any(Properties.class), any(Boolean.class))).willReturn(network);
given(caseServerRest.getForEntity(eq("/v1/cases/" + caseUuid + "/infos"), any())).willReturn(ResponseEntity.ok(new CaseInfos(UUID.fromString(caseUuid), "testCase", "XIIDM")));
given(caseServerRest.exchange(eq("/v1/cases/{caseUuid}/datasource/baseName"),
eq(HttpMethod.GET),
Expand Down Expand Up @@ -327,7 +324,7 @@ public void testFailedAsyncImport() throws Exception {
String caseUuid = UUID.randomUUID().toString();
String receiver = "test receiver";
given(networkStoreClient.getNetworkUuid(network)).willReturn(randomUuid);
given(networkStoreClient.importNetwork(any(ReadOnlyDataSource.class), any(Reporter.class), any(Properties.class), any(Boolean.class))).willThrow(new NullPointerException(IMPORT_CASE_ERROR_MESSAGE));
given(networkStoreClient.importNetwork(any(ReadOnlyDataSource.class), any(ReportNode.class), any(Properties.class), any(Boolean.class))).willThrow(new NullPointerException(IMPORT_CASE_ERROR_MESSAGE));
given(caseServerRest.getForEntity(eq("/v1/cases/" + caseUuid + "/infos"), any())).willReturn(ResponseEntity.ok(new CaseInfos(UUID.fromString(caseUuid), "testCase", "XIIDM")));
given(caseServerRest.exchange(eq("/v1/cases/{caseUuid}/datasource/baseName"),
eq(HttpMethod.GET),
Expand Down Expand Up @@ -369,9 +366,7 @@ public void testExportSv() throws Exception {
assertTrue(Objects.requireNonNull(mvcResult.getResponse().getHeader("content-disposition")).contains("filename*=UTF-8''urn%3Auuid%3Ad400c631-75a0-4c30-8aed-832b0d282e73"));
assertTrue(mvcResult.getResponse().getContentAsString().contains("<md:Model.description>SV Model</md:Model.description>\n" +
" <md:Model.version>1</md:Model.version>\n" +
" <md:Model.DependentOn rdf:resource=\"urn:uuid:2399cbd1-9a39-11e0-aa80-0800200c9a66\"/>\n" +
" <md:Model.DependentOn rdf:resource=\"urn:uuid:d400c631-75a0-4c30-8aed-832b0d282e73\"/>\n" +
" <md:Model.DependentOn rdf:resource=\"urn:uuid:f2f43818-09c8-4252-9611-7af80c398d20\"/>\n" +
" <md:Model.DependentOn rdf:resource=\"urn:uuid:c2960b34-0a04-4cd1-9c4d-f3112d85ec6c\"/>\n" +
" <md:Model.profile>http://entsoe.eu/CIM/StateVariables/4/1</md:Model.profile>\n" +
" <md:Model.modelingAuthoritySet>powsybl.org</md:Model.modelingAuthoritySet>\n" +
" </md:FullModel>"));
Expand Down Expand Up @@ -423,7 +418,7 @@ public void testImportCgmesCase() throws Exception {

Network network = new CgmesImport().importData(CgmesConformity1Catalog.microGridBaseCaseBE().dataSource(), new NetworkFactoryImpl(), null);
given(networkStoreClient.importNetwork(any(ReadOnlyDataSource.class))).willReturn(network);
given(networkStoreClient.importNetwork(any(ReadOnlyDataSource.class), any(Reporter.class), any(Boolean.class))).willReturn(network);
given(networkStoreClient.importNetwork(any(ReadOnlyDataSource.class), any(ReportNode.class), any(Boolean.class))).willReturn(network);
given(networkStoreClient.getNetworkUuid(network)).willReturn(networkUuid);
given(caseServerRest.exchange(eq("/v1/cases/{caseUuid}/datasource/baseName"),
eq(HttpMethod.GET),
Expand Down Expand Up @@ -461,13 +456,15 @@ public void testSendReport() throws Exception {
networkConversionService.setReportServerRest(reportServerRest);

Network network = new CgmesImport().importData(CgmesConformity1Catalog.microGridBaseCaseBE().dataSource(), new NetworkFactoryImpl(), null);
given(networkStoreClient.importNetwork(any(ReadOnlyDataSource.class), any(ReporterModel.class), any(Boolean.class))).willAnswer((Answer<Network>) invocationOnMock -> {
var reporter = invocationOnMock.getArgument(1, ReporterModel.class);
reporter.addSubReporter(new ReporterModel("test", "test"));
given(networkStoreClient.importNetwork(any(ReadOnlyDataSource.class), any(ReportNode.class), any(Boolean.class))).willAnswer((Answer<Network>) invocationOnMock -> {
var reportNode = invocationOnMock.getArgument(1, ReportNode.class);
reportNode.newReportNode()
.withMessageTemplate("test", "test")
.add();
return network;
});
given(networkStoreClient.getNetworkUuid(network)).willReturn(networkUuid);
given(reportServerRest.exchange(eq("/v1/reports/" + reportUuid), eq(HttpMethod.PUT), any(HttpEntity.class), eq(ReporterModel.class)))
given(reportServerRest.exchange(eq("/v1/reports/" + reportUuid), eq(HttpMethod.PUT), any(HttpEntity.class), eq(ReportNode.class)))
.willReturn(new ResponseEntity<>(HttpStatus.OK));
given(caseServerRest.exchange(eq("/v1/cases/{caseUuid}/datasource/baseName"),
eq(HttpMethod.GET),
Expand Down Expand Up @@ -501,10 +498,10 @@ public void testImportWithError() {
.willReturn(ResponseEntity.ok("testCase"));

Network network = createNetwork("test");
given(networkStoreClient.importNetwork(any(ReadOnlyDataSource.class), any(ReporterModel.class), any(Boolean.class)))
given(networkStoreClient.importNetwork(any(ReadOnlyDataSource.class), any(ReportNode.class), any(Boolean.class)))
.willThrow(NetworkConversionException.createFailedNetworkSaving(networkUuid, NetworkConversionException.createEquipmentTypeUnknown(NetworkImpl.class.getSimpleName())));
given(networkStoreClient.getNetworkUuid(network)).willReturn(networkUuid);
given(reportServerRest.exchange(eq("/v1/reports/" + reportUuid), eq(HttpMethod.PUT), any(HttpEntity.class), eq(ReporterModel.class)))
given(reportServerRest.exchange(eq("/v1/reports/" + reportUuid), eq(HttpMethod.PUT), any(HttpEntity.class), eq(ReportNode.class)))
.willReturn(new ResponseEntity<>(HttpStatus.OK));
given(caseServerRest.getForEntity(eq("/v1/cases/" + caseUuid + "/infos"), any())).willReturn(ResponseEntity.ok(new CaseInfos(UUID.fromString(caseUuid.toString()), "testCase", "XIIDM")));

Expand All @@ -520,11 +517,11 @@ public void testFlushNetworkWithError() {
networkConversionService.setReportServerRest(reportServerRest);

Network network = createNetwork("test");
given(networkStoreClient.importNetwork(any(ReadOnlyDataSource.class), any(Reporter.class), any(Boolean.class))).willReturn(network);
given(networkStoreClient.importNetwork(any(ReadOnlyDataSource.class), any(ReportNode.class), any(Boolean.class))).willReturn(network);
doThrow(NetworkConversionException.createFailedNetworkSaving(networkUuid, NetworkConversionException.createEquipmentTypeUnknown(NetworkImpl.class.getSimpleName())))
.when(networkStoreClient).flush(network);
given(networkStoreClient.getNetworkUuid(network)).willReturn(networkUuid);
given(reportServerRest.exchange(eq("/v1/reports/" + reportUuid), eq(HttpMethod.PUT), any(HttpEntity.class), eq(ReporterModel.class)))
given(reportServerRest.exchange(eq("/v1/reports/" + reportUuid), eq(HttpMethod.PUT), any(HttpEntity.class), eq(ReportNode.class)))
.willReturn(new ResponseEntity<>(HttpStatus.OK));
given(reportServerRest.exchange(eq("/v1/reports/" + reportUuid), eq(HttpMethod.DELETE), any(HttpEntity.class), eq(Void.class)))
.willReturn(new ResponseEntity<>(HttpStatus.OK));
Expand Down

0 comments on commit 96586c7

Please sign in to comment.