Skip to content
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
9 changes: 9 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@
<h2database.version>1.4.200</h2database.version>
<liquibase-hibernate-package>org.gridsuite.ds.server</liquibase-hibernate-package>
<string-template.version>4.3.1</string-template.version>

<!-- FIXME: powsybl-ws-commons module's version is overloaded in the dependencies section. The overloads and this property below have to be removed at next gridsuite-dependencies.version upgrade -->
<powsybl-ws-commons.version>1.23.0-SNAPSHOT</powsybl-ws-commons.version>
</properties>

<build>
Expand Down Expand Up @@ -87,6 +90,12 @@
<dependencyManagement>
<dependencies>
<!-- overrides of imports -->
<!-- FIXME: to be removed at next gridsuite-dependencies upgrade -->
<dependency>
<groupId>com.powsybl</groupId>
<artifactId>powsybl-ws-commons</artifactId>
<version>${powsybl-ws-commons.version}</version>
</dependency>
<!-- imports -->
<dependency>
<groupId>org.gridsuite</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import com.powsybl.timeseries.IrregularTimeSeriesIndex;
import com.powsybl.timeseries.TimeSeries;
import com.powsybl.ws.commons.computation.service.*;
import com.powsybl.ws.commons.utils.GZipUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.gridsuite.ds.server.DynamicSimulationException;
import org.gridsuite.ds.server.dto.DynamicSimulationParametersInfos;
Expand All @@ -39,7 +40,6 @@
import org.gridsuite.ds.server.service.contexts.DynamicSimulationResultContext;
import org.gridsuite.ds.server.service.contexts.DynamicSimulationRunContext;
import org.gridsuite.ds.server.service.parameters.ParametersService;
import org.gridsuite.ds.server.utils.Utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
Expand Down Expand Up @@ -277,7 +277,7 @@ private byte[] zipDumpFile(Path dumpDir) {
Path dumpFile = files.findFirst().orElse(null);
if (dumpFile != null) {
// ZIP output state
outputState = Utils.zip(dumpFile);
outputState = GZipUtils.zip(dumpFile);
}

} catch (IOException e) {
Expand All @@ -291,7 +291,7 @@ private byte[] zipParameters(DynamicSimulationParameters parameters) {
byte[] zippedJsonParameters;
try {
String jsonParameters = objectMapper.writeValueAsString(parameters);
zippedJsonParameters = Utils.zip(jsonParameters);
zippedJsonParameters = GZipUtils.zip(jsonParameters);
} catch (IOException e) {
throw new DynamicSimulationException(DYNAMIC_SIMULATION_PARAMETERS_ERROR, "Error occurred while zipping the dynamic simulation parameters");
}
Expand All @@ -302,7 +302,7 @@ private byte[] zipDynamicModel(List<DynamicModelConfig> dynamicModelContent) {
byte[] zippedJsonDynamicModelContent;
try {
String jsonDynamicModelContent = objectMapper.writeValueAsString(dynamicModelContent);
zippedJsonDynamicModelContent = Utils.zip(jsonDynamicModelContent);
zippedJsonDynamicModelContent = GZipUtils.zip(jsonDynamicModelContent);
} catch (IOException e) {
throw new DynamicSimulationException(DYNAMIC_MODEL_ERROR, "Error occurred while zipping the dynamic model");
}
Expand Down
66 changes: 0 additions & 66 deletions src/main/java/org/gridsuite/ds/server/utils/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@

package org.gridsuite.ds.server.utils;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.powsybl.dynawo.suppliers.Property;
import com.powsybl.dynawo.suppliers.PropertyBuilder;
import com.powsybl.dynawo.suppliers.PropertyType;
Expand All @@ -17,14 +15,9 @@
import org.gridsuite.ds.server.dto.dynamicmapping.automata.BasicProperty;
import org.gridsuite.ds.server.dto.event.EventPropertyInfos;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

/**
* @author Thang PHAM <quyet-thang.pham at rte-france.com>
Expand Down Expand Up @@ -89,63 +82,4 @@ public static Property convertProperty(BasicProperty property) {

return propertyBuilder.build();
}

private static byte[] zip(InputStream is) throws IOException {
try (ByteArrayOutputStream os = new ByteArrayOutputStream();
GZIPOutputStream zipOs = new GZIPOutputStream(os)) {
byte[] buffer = new byte[1024];
int length;
while ((length = is.read(buffer)) > 0) {
zipOs.write(buffer, 0, length);
}
zipOs.finish();
return os.toByteArray();
}
}

public static byte[] zip(String content) throws IOException {
try (InputStream is = new ByteArrayInputStream(content.getBytes())) {
return zip(is);
}
}

public static byte[] zip(Path filePath) {
try (InputStream is = Files.newInputStream(filePath)) {
return zip(is);
} catch (IOException e) {
throw new UncheckedIOException("Error occurred while zipping the file " + filePath.toAbsolutePath(), e);
}
}

private static void unzipToStream(byte[] zippedBytes, OutputStream outputStream) throws IOException {
try (ByteArrayInputStream is = new ByteArrayInputStream(zippedBytes);
GZIPInputStream zipIs = new GZIPInputStream(is);
BufferedOutputStream bufferedOut = new BufferedOutputStream(outputStream)) {
byte[] buffer = new byte[1024];
int length;
while ((length = zipIs.read(buffer)) > 0) {
bufferedOut.write(buffer, 0, length);
}
}
}

public static void unzip(byte[] zippedBytes, Path filePath) throws IOException {
try (FileOutputStream fos = new FileOutputStream(new File(filePath.toUri()))) {
unzipToStream(zippedBytes, fos);
}
}

public static <T> T unzip(byte[] zippedBytes, ObjectMapper objectMapper, TypeReference<T> valueTypeRef) throws IOException {
try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
unzipToStream(zippedBytes, bos);
return objectMapper.readValue(bos.toByteArray(), valueTypeRef);
}
}

public static <T> T unzip(byte[] zippedBytes, ObjectMapper objectMapper, Class<T> valueType) throws IOException {
try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
unzipToStream(zippedBytes, bos);
return objectMapper.readValue(bos.toByteArray(), valueType);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.powsybl.timeseries.TimeSeries;
import com.powsybl.timeseries.TimeSeriesDataType;
import com.powsybl.timeseries.TimeSeriesMetadata;
import com.powsybl.ws.commons.utils.GZipUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.gridsuite.ds.server.controller.utils.FileUtils;
import org.gridsuite.ds.server.controller.utils.ParameterUtils;
Expand All @@ -33,7 +34,6 @@
import org.gridsuite.ds.server.dto.event.EventInfos;
import org.gridsuite.ds.server.dto.timeseries.TimeSeriesGroupInfos;
import org.gridsuite.ds.server.service.client.timeseries.TimeSeriesClientTest;
import org.gridsuite.ds.server.utils.Utils;
import org.junit.Test;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -257,7 +257,7 @@ public void test01GivenCurvesAndEvents() throws Exception {
FileUtils.writeBytesToFile(this, outputDir + RESOURCE_PATH_DELIMITER + "outputState.dmp.gz", zippedOutputState);
File file = new File(Objects.requireNonNull(this.getClass().getClassLoader().getResource(".")).getFile() +
outputDir + RESOURCE_PATH_DELIMITER + "outputState.dmp");
Utils.unzip(zippedOutputState, file.toPath());
GZipUtils.unzip(zippedOutputState, file.toPath());

// check dynamic model persisted in result in gzip format not empty
result = mockMvc.perform(
Expand All @@ -272,13 +272,13 @@ public void test01GivenCurvesAndEvents() throws Exception {
logger.info("Size of zipped dynamic model = {} B ", zippedDynamicModel.length);

// export dynamic model in json and dump files to manual check
List<DynamicModelConfig> dynamicModel = Utils.unzip(zippedDynamicModel, objectMapper, new TypeReference<>() { });
List<DynamicModelConfig> dynamicModel = GZipUtils.unzip(zippedDynamicModel, objectMapper, new TypeReference<>() { });
String jsonDynamicModel = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(dynamicModel);
FileUtils.writeBytesToFile(this, outputDir + RESOURCE_PATH_DELIMITER + "dynamicModel.json", jsonDynamicModel.getBytes());

file = new File(Objects.requireNonNull(this.getClass().getClassLoader().getResource(".")).getFile() +
outputDir + RESOURCE_PATH_DELIMITER + "dynamicModel.dmp");
Utils.unzip(zippedDynamicModel, file.toPath());
GZipUtils.unzip(zippedDynamicModel, file.toPath());

// check parameters persisted in result in gzip format not empty
result = mockMvc.perform(
Expand All @@ -293,13 +293,13 @@ public void test01GivenCurvesAndEvents() throws Exception {
logger.info("Size of zipped parameters = {} KB ", zippedDynamicSimulationParameters.length / 1024);

// export dynamic model in json and dump files to manual check
DynamicSimulationParameters dynamicSimulationParameters = Utils.unzip(zippedDynamicSimulationParameters, objectMapper, DynamicSimulationParameters.class);
DynamicSimulationParameters dynamicSimulationParameters = GZipUtils.unzip(zippedDynamicSimulationParameters, objectMapper, DynamicSimulationParameters.class);
String jsonDynamicSimulationParameters = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(dynamicSimulationParameters);
FileUtils.writeBytesToFile(this, outputDir + RESOURCE_PATH_DELIMITER + "dynamicSimulationParameters.json", jsonDynamicSimulationParameters.getBytes());

file = new File(Objects.requireNonNull(this.getClass().getClassLoader().getResource(".")).getFile() +
outputDir + RESOURCE_PATH_DELIMITER + "dynamicSimulationParameters.dmp");
Utils.unzip(zippedDynamicSimulationParameters, file.toPath());
GZipUtils.unzip(zippedDynamicSimulationParameters, file.toPath());

}
}
Loading