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

Fix network store upgrade #150

Open
wants to merge 17 commits into
base: main
Choose a base branch
from
Open
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
39 changes: 17 additions & 22 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

<log4j2-mock-version>0.0.2</log4j2-mock-version>
<powsybl-ws-dependencies.version>2.11.0</powsybl-ws-dependencies.version>
<powsybl-ws-dependencies.version>2.12.0</powsybl-ws-dependencies.version>
<testcontainers.version>1.16.2</testcontainers.version>
<!-- FIXME: powsybl-network-store modules'version is overloaded in the dependencies section.The overloads and this property below have to be removed at next powsybl-ws-dependencies.version upgrade -->
<powsybl-network-store.version>1.13.0</powsybl-network-store.version>
<powsybl-network-store.version>1.16.0</powsybl-network-store.version>
</properties>

<build>
Expand Down Expand Up @@ -64,26 +64,6 @@

<dependencyManagement>
<dependencies>
<!-- overrides of imports -->
<!-- FIXME: to be removed at next powsybl-ws-dependencies upgrade -->
<dependency>
<groupId>com.powsybl</groupId>
<artifactId>powsybl-network-store-iidm-impl</artifactId>
<version>${powsybl-network-store.version}</version>
</dependency>
<!-- FIXME: to be removed at next powsybl-ws-dependencies upgrade -->
<dependency>
<groupId>com.powsybl</groupId>
<artifactId>powsybl-network-store-client</artifactId>
<version>${powsybl-network-store.version}</version>
</dependency>
<!-- FIXME: to be removed at next powsybl-ws-dependencies upgrade -->
<dependency>
<groupId>com.powsybl</groupId>
<artifactId>powsybl-network-store-model</artifactId>
<version>${powsybl-network-store.version}</version>
</dependency>

<!-- imports -->
<dependency>
<groupId>com.powsybl</groupId>
Expand Down Expand Up @@ -125,9 +105,24 @@
<groupId>com.powsybl</groupId>
<artifactId>powsybl-case-datasource-client</artifactId>
</dependency>
<!-- overrides of imports -->
<!-- FIXME: to be removed at next powsybl-ws-dependencies upgrade -->
<dependency>
<groupId>com.powsybl</groupId>
<artifactId>powsybl-network-store-iidm-impl</artifactId>
<version>${powsybl-network-store.version}</version>
</dependency>
<!-- FIXME: to be removed at next powsybl-ws-dependencies upgrade -->
<dependency>
<groupId>com.powsybl</groupId>
<artifactId>powsybl-network-store-client</artifactId>
<version>${powsybl-network-store.version}</version>
</dependency>
<!-- FIXME: to be removed at next powsybl-ws-dependencies upgrade -->
<dependency>
<groupId>com.powsybl</groupId>
<artifactId>powsybl-network-store-model</artifactId>
<version>${powsybl-network-store.version}</version>
</dependency>
<dependency>
<groupId>com.powsybl</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,11 @@ public ResponseEntity<NetworkInfos> importCase(@Parameter(description = "Case UU
public ResponseEntity<byte[]> exportNetwork(@Parameter(description = "Network UUID") @PathVariable("mainNetworkUuid") UUID networkUuid,
@Parameter(description = "Export format")@PathVariable("format") String format,
@Parameter(description = "Variant Id") @RequestParam(name = "variantId", required = false) String variantId,
@Parameter(description = "File name") @RequestParam(name = "fileName", required = false) String fileName,
@org.springframework.web.bind.annotation.RequestBody(required = false) Map<String, Object> formatParameters
) throws IOException {
LOGGER.debug("Exporting network {}...", networkUuid);
ExportNetworkInfos exportNetworkInfos = networkConversionObserver.observeExport(format, () -> networkConversionService.exportNetwork(networkUuid, variantId, format, formatParameters));
ExportNetworkInfos exportNetworkInfos = networkConversionObserver.observeExport(format, () -> networkConversionService.exportNetwork(networkUuid, variantId, fileName, format, formatParameters));
HttpHeaders header = new HttpHeaders();
header.setContentDisposition(ContentDisposition.builder("attachment").filename(exportNetworkInfos.getNetworkName(), StandardCharsets.UTF_8).build());
return ResponseEntity.ok().headers(header).contentType(MediaType.APPLICATION_OCTET_STREAM).body(exportNetworkInfos.getNetworkData());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@
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.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;
Expand Down Expand Up @@ -287,7 +284,7 @@ private Network getNetwork(UUID networkUuid) {
}
}

ExportNetworkInfos exportNetwork(UUID networkUuid, String variantId,
ExportNetworkInfos exportNetwork(UUID networkUuid, String variantId, String fileName,
String format, Map<String, Object> formatParameters) throws IOException {
if (!Exporter.getFormats().contains(format)) {
throw NetworkConversionException.createFormatUnsupported(format);
Expand All @@ -311,19 +308,24 @@ ExportNetworkInfos exportNetwork(UUID networkUuid, String variantId,
network.write(format, exportProperties, memDataSource);

Set<String> listNames = memDataSource.listNames(".*");
String networkName;
String fileOrNetworkName = fileName != null ? fileName : getNetworkName(network, variantId);
byte[] networkData;
networkName = network.getNameOrId();
networkName += "_" + (variantId == null ? VariantManagerConstants.INITIAL_VARIANT_ID : variantId);

if (listNames.size() == 1) {
networkName += listNames.toArray()[0];
fileOrNetworkName += listNames.toArray()[0];
networkData = memDataSource.getData(listNames.toArray()[0].toString());
} else {
networkName += ".zip";
fileOrNetworkName += ".zip";
networkData = createZipFile(listNames.toArray(new String[0]), memDataSource).toByteArray();
}
long networkSize = network.getBusView().getBusStream().count();
return new ExportNetworkInfos(networkName, networkData, networkSize);
return new ExportNetworkInfos(fileOrNetworkName, networkData, networkSize);
}

private String getNetworkName(Network network, String variantId) {
String networkName = network.getNameOrId();
networkName += "_" + (variantId == null ? VariantManagerConstants.INITIAL_VARIANT_ID : variantId);
return networkName;
}

ByteArrayOutputStream createZipFile(String[] listNames, MemDataSource dataSource) throws IOException {
Expand Down Expand Up @@ -402,14 +404,6 @@ public ExportNetworkInfos exportCgmesSv(UUID networkUuid) throws XMLStreamExcept
private static CgmesExportContext createContext(Network network) {
CgmesExportContext context = new CgmesExportContext();
context.setScenarioTime(network.getCaseDate());
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
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public void test() throws Exception {
any(HttpEntity.class),
eq(String.class), eq(UUID.fromString(caseUuid))))
.willReturn(ResponseEntity.ok("testCase"));
given(caseServerRest.getForEntity(eq("/v1/cases/" + caseUuid + "/infos"), any())).willReturn(ResponseEntity.ok(new CaseInfos(UUID.fromString(caseUuid.toString()), "testCase", "XIIDM")));
given(caseServerRest.getForEntity(eq("/v1/cases/" + caseUuid + "/infos"), any())).willReturn(ResponseEntity.ok(new CaseInfos(UUID.fromString(caseUuid.toString()), "testCase.xiidm", "XIIDM")));

MvcResult mvcResult = mvc.perform(post("/v1/networks")
.param("caseUuid", caseUuid)
Expand Down Expand Up @@ -177,7 +177,6 @@ public void test() throws Exception {
.andExpect(status().isOk())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_OCTET_STREAM))
.andReturn();

assertTrue(Objects.requireNonNull(mvcResult.getResponse().getHeader("content-disposition")).contains("attachment;"));
assertTrue(Objects.requireNonNull(mvcResult.getResponse().getHeader("content-disposition")).contains(String.format("filename*=UTF-8''20140116_0830_2D4_UX1_pst_%s.xiidm", VariantManagerConstants.INITIAL_VARIANT_ID)));
assertTrue(mvcResult.getResponse().getContentAsString().startsWith("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"));
Expand All @@ -203,6 +202,16 @@ public void test() throws Exception {
// takes the iidm.export.xml.indent param into account
assertTrue(exported1.length() > exported2.length());

//with fileName
mvcResult = mvc.perform(post("/v1/networks/{networkUuid}/export/{format}?fileName=" + "studyName_Root", UUID.randomUUID().toString(), "XIIDM").param("variantId", "second_variant_id"))
.andExpect(status().isOk())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_OCTET_STREAM))
.andReturn();

assertTrue(Objects.requireNonNull(mvcResult.getResponse().getHeader("content-disposition")).contains("attachment;"));
assertTrue(Objects.requireNonNull(mvcResult.getResponse().getHeader("content-disposition")).contains("filename*=UTF-8''studyName_Root.xiidm"));
assertTrue(mvcResult.getResponse().getContentAsString().startsWith("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"));

// non existing variantId
mvcResult = mvc.perform(post("/v1/networks/{networkUuid}/export/{format}", UUID.randomUUID().toString(), "XIIDM").param("variantId", "unknown_variant_id"))
.andExpect(status().isNotFound())
Expand Down Expand Up @@ -365,12 +374,13 @@ public void testExportSv() throws Exception {

assertTrue(Objects.requireNonNull(mvcResult.getResponse().getHeader("content-disposition")).contains("attachment;"));
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: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>"));
assertTrue(mvcResult.getResponse().getContentAsString().contains("<md:Model.description>CGMES Conformity Assessment: 'MicroGridTestConfiguration....BC (MAS BE) Test Configuration. The model is owned by ENTSO-E and is provided by ENTSO-E â\u0080\u009Cas it isâ\u0080\u009D. To the fullest extent permitted by law, ENTSO-E shall not be liable for any damages of any kind arising out of the use of the model (including any of its subsequent modifications). ENTSO-E neither warrants, nor represents that the use of the model will not infringe the rights of third parties. Any use of the model shall include a reference to ENTSO-E. ENTSO-E web site is the only official source of information related to the model.</md:Model.description>\n" +
" <md:Model.version>2</md:Model.version>\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:2399cbd1-9a39-11e0-aa80-0800200c9a66\"/>\n" +
" <md:Model.profile>http://entsoe.eu/CIM/StateVariables/4/1</md:Model.profile>\n" +
" <md:Model.modelingAuthoritySet>http://elia.be/CGMES/2.4.15</md:Model.modelingAuthoritySet>"));
}

@Test
Expand Down
Loading