diff --git a/pom.xml b/pom.xml index 5a4f2d61..fee13a60 100644 --- a/pom.xml +++ b/pom.xml @@ -48,6 +48,9 @@ 1.4.200 org.gridsuite.ds.server 4.3.1 + + + 1.23.0-SNAPSHOT @@ -87,6 +90,12 @@ + + + com.powsybl + powsybl-ws-commons + ${powsybl-ws-commons.version} + org.gridsuite diff --git a/src/main/java/org/gridsuite/ds/server/service/DynamicSimulationWorkerService.java b/src/main/java/org/gridsuite/ds/server/service/DynamicSimulationWorkerService.java index 8b8c9a3b..2275503a 100644 --- a/src/main/java/org/gridsuite/ds/server/service/DynamicSimulationWorkerService.java +++ b/src/main/java/org/gridsuite/ds/server/service/DynamicSimulationWorkerService.java @@ -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; @@ -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; @@ -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) { @@ -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"); } @@ -302,7 +302,7 @@ private byte[] zipDynamicModel(List 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"); } diff --git a/src/main/java/org/gridsuite/ds/server/utils/Utils.java b/src/main/java/org/gridsuite/ds/server/utils/Utils.java index 1e9a7376..f023c111 100644 --- a/src/main/java/org/gridsuite/ds/server/utils/Utils.java +++ b/src/main/java/org/gridsuite/ds/server/utils/Utils.java @@ -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; @@ -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 @@ -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 unzip(byte[] zippedBytes, ObjectMapper objectMapper, TypeReference valueTypeRef) throws IOException { - try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) { - unzipToStream(zippedBytes, bos); - return objectMapper.readValue(bos.toByteArray(), valueTypeRef); - } - } - - public static T unzip(byte[] zippedBytes, ObjectMapper objectMapper, Class valueType) throws IOException { - try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) { - unzipToStream(zippedBytes, bos); - return objectMapper.readValue(bos.toByteArray(), valueType); - } - } } diff --git a/src/test/java/org/gridsuite/ds/server/controller/DynamicSimulationControllerIEEE14Test.java b/src/test/java/org/gridsuite/ds/server/controller/DynamicSimulationControllerIEEE14Test.java index 0a4a53fa..2c31d22c 100644 --- a/src/test/java/org/gridsuite/ds/server/controller/DynamicSimulationControllerIEEE14Test.java +++ b/src/test/java/org/gridsuite/ds/server/controller/DynamicSimulationControllerIEEE14Test.java @@ -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; @@ -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; @@ -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( @@ -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 dynamicModel = Utils.unzip(zippedDynamicModel, objectMapper, new TypeReference<>() { }); + List 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( @@ -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()); } }