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
29 changes: 25 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@
<properties>
<gridsuite-dependencies.version>47.0.0</gridsuite-dependencies.version>
<jib.from.image>powsybl/java-dynawo:3.1.0</jib.from.image>
<!-- FIXME to remove at next upgrade of powsybl-ws-dependencies -->
<testcontainers.version>1.21.4</testcontainers.version>
<liquibase-hibernate-package>org.gridsuite.dynamicmargincalculation.server</liquibase-hibernate-package>
<sonar.organization>gridsuite</sonar.organization>
<sonar.projectKey>org.gridsuite:dynamic-margin-calculation-server</sonar.projectKey>
Expand Down Expand Up @@ -86,6 +88,11 @@
<dependencyManagement>
<dependencies>
<!-- overrides of imports -->
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers</artifactId>
<version>${testcontainers.version}</version>
</dependency>

<!-- imports -->
<dependency>
Expand All @@ -102,10 +109,6 @@

<dependencies>
<!-- Compilation dependencies -->
<dependency>
<groupId>org.gridsuite</groupId>
<artifactId>gridsuite-computation</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
Expand Down Expand Up @@ -147,6 +150,24 @@
<groupId>com.powsybl</groupId>
<artifactId>powsybl-dynawo-margin-calculation</artifactId>
</dependency>
<dependency>
<groupId>com.powsybl</groupId>
<artifactId>powsybl-dynawo-contingencies</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>com.powsybl</groupId>
<artifactId>powsybl-dynamic-security-analysis</artifactId>
<version>7.1.1</version>
</dependency>
<dependency>
<groupId>org.gridsuite</groupId>
<artifactId>gridsuite-computation</artifactId>
</dependency>
<dependency>
<groupId>org.gridsuite</groupId>
<artifactId>gridsuite-filter</artifactId>
</dependency>

<!-- runtime dependencies -->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* Copyright (c) 2026, RTE (http://www.rte-france.com)
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

package com.powsybl.dynawo.suppliers.dynamicmodels;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.powsybl.commons.json.JsonUtil;

import java.util.List;

/**
* @author Thang PHAM <quyet-thang.pham at rte-france.com>
*/
public final class DynamicModelConfigJsonUtils {

private DynamicModelConfigJsonUtils() {
throw new AssertionError("Utility class should not be instantiated");
}

public static ObjectMapper createObjectMapper() {
ObjectMapper mapper = JsonUtil.createObjectMapper();

SimpleModule module = new SimpleModule("dynamic-model-configs");
module.addSerializer(new DynamicModelConfigsJsonSerializer());
module.addDeserializer(List.class, new DynamicModelConfigsJsonDeserializer());

mapper.registerModule(module);
return mapper;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
/**
* Copyright (c) 2026, RTE (http://www.rte-france.com)
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

package com.powsybl.dynawo.suppliers.dynamicmodels;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
import com.powsybl.dynawo.suppliers.Property;
import com.powsybl.dynawo.suppliers.PropertyType;
import com.powsybl.iidm.network.TwoSides;

import java.io.IOException;
import java.util.List;

/**
* Serialize a List<DynamicModelConfig> with "models" as root:
*
* {
* "models": [
* {
* "model": "...",
* "group": "...",
* "groupType": "FIXED",
* "properties": [ ... ]
* }
* ]
* }
*
* This matches {@link DynamicModelConfigsJsonDeserializer}.
*
* TODO : to remove when available at powsybl-dynawo
*
* @author Thang PHAM <quyet-thang.pham at rte-france.com>
*/
public class DynamicModelConfigsJsonSerializer extends StdSerializer<List<DynamicModelConfig>> {

public DynamicModelConfigsJsonSerializer() {
super((Class<List<DynamicModelConfig>>) (Class<?>) List.class);
}

@Override
public void serialize(List<DynamicModelConfig> configs,
JsonGenerator gen,
SerializerProvider provider) throws IOException {

gen.writeStartObject();

gen.writeFieldName("models");
writeDynamicModelConfigs(configs, gen);

gen.writeEndObject();
}

private static void writeDynamicModelConfigs(List<DynamicModelConfig> configs, JsonGenerator gen) throws IOException {
gen.writeStartArray();
if (configs != null) {
for (DynamicModelConfig cfg : configs) {
writeDynamicModelConfig(cfg, gen);
}
}
gen.writeEndArray();
}

private static void writeDynamicModelConfig(DynamicModelConfig cfg,
JsonGenerator gen) throws IOException {

gen.writeStartObject();

gen.writeStringField("model", cfg.model());
gen.writeStringField("group", cfg.group());

if (cfg.groupType() != null) {
gen.writeStringField("groupType", cfg.groupType().name());
}

gen.writeFieldName("properties");
writeProperties(cfg.properties(), gen);

gen.writeEndObject();
}

private static void writeProperties(List<Property> properties, JsonGenerator gen) throws IOException {
gen.writeStartArray();
if (properties != null) {
for (Property p : properties) {
writeProperty(p, gen);
}
}
gen.writeEndArray();
}

/**
* See {@code PropertyParserUtils.parseProperty}:
* - always writes "name"
* - writes exactly one of: "value" (string value), "values" (string array), "arrays" (array of string arrays)
* - writes "type" when it can be inferred from propertyClass
*/
private static void writeProperty(Property property, JsonGenerator gen) throws IOException {
gen.writeStartObject();

gen.writeStringField("name", property.name());

Object value = property.value();
if (value instanceof List<?> list) {
if (!list.isEmpty()) {
// lists: List<String|int|double|boolean|TwoSides>
gen.writeFieldName("values");
gen.writeStartArray();
for (Object v : list) {
gen.writeString(String.valueOf(v));
}
gen.writeEndArray();
writeOptionalType(gen, property);
}
} else if (value != null && value.getClass().isArray()) {
// arrays: List<List<String|integer|double|boolean|TwoSides>>
gen.writeFieldName("arrays");
gen.writeStartArray();
for (List<?> row : (List<?>[]) value) {
gen.writeStartArray();
for (Object v : row) {
gen.writeString(String.valueOf(v));
}
gen.writeEndArray();
}
gen.writeEndArray();
writeOptionalType(gen, property);
} else {
if (value instanceof TwoSides ts) {
gen.writeStringField("value", ts.name());
writeOptionalType(gen, property);
} else if (value != null) {
gen.writeStringField("value", String.valueOf(value));
writeOptionalType(gen, property);
}
}

gen.writeEndObject();
}

/**
* Writes the "type" field if we can (or if Property already carries an explicit type via propertyClass()).
*
* This keeps compatibility with PropertyParserUtils:
* case "type" -> builder.type(PropertyType.valueOf(parser.nextTextValue()));
*/
private static void writeOptionalType(JsonGenerator gen, Property property) throws IOException {
PropertyType inferredType = PropertyType.STRING;

Class<?> propertyClass = property.propertyClass();
if (propertyClass != null) {
if (propertyClass == double.class) {
inferredType = PropertyType.DOUBLE;
} else if (propertyClass == int.class) {
inferredType = PropertyType.INTEGER;
} else if (propertyClass == boolean.class) {
inferredType = PropertyType.BOOLEAN;
} else if (propertyClass == TwoSides.class) {
inferredType = PropertyType.TWO_SIDES;
}
}

gen.writeStringField("type", inferredType.name());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
* Copyright (c) 2026, RTE (http://www.rte-france.com)
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
package org.gridsuite.dynamicmargincalculation.server.config;

import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.powsybl.commons.report.ReportNodeJsonModule;
import com.powsybl.dynamicsimulation.json.DynamicSimulationParametersJsonModule;
import com.powsybl.dynawo.margincalculation.json.MarginCalculationParametersJsonModule;
import com.powsybl.security.dynamic.json.DynamicSecurityAnalysisJsonModule;
import org.gridsuite.computation.ComputationConfig;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.client.RestTemplate;

/**
* @author Thang PHAM <quyet-thang.pham at rte-france.com>
*/
@Configuration
@Import(ComputationConfig.class)
public class RestTemplateConfig {
@Bean
public RestTemplate restTemplate(RestTemplateBuilder restTemplateBuilder, ObjectMapper objectMapper) {
MappingJackson2HttpMessageConverter messageConverter = new MappingJackson2HttpMessageConverter(objectMapper);

return restTemplateBuilder
.additionalMessageConverters(messageConverter)
.build();
}

@Bean
public Jackson2ObjectMapperBuilderCustomizer appJsonCustomizer() {
return builder -> builder
.featuresToDisable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
.featuresToEnable(MapperFeature.ACCEPT_CASE_INSENSITIVE_ENUMS)
.modulesToInstall(new MarginCalculationParametersJsonModule(), new DynamicSecurityAnalysisJsonModule(),
new DynamicSimulationParametersJsonModule(), new ReportNodeJsonModule());
}
}
Loading