Skip to content

Commit

Permalink
Merge branch 'main' into feature/csa/reorganize-constants
Browse files Browse the repository at this point in the history
  • Loading branch information
bqth29 authored Dec 2, 2024
2 parents 6bf2981 + 0e149ca commit e4f5fa3
Show file tree
Hide file tree
Showing 170 changed files with 7,741 additions and 247 deletions.
2 changes: 1 addition & 1 deletion commons/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-inline</artifactId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down
2 changes: 1 addition & 1 deletion data/crac/crac-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-inline</artifactId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down
2 changes: 1 addition & 1 deletion data/crac/crac-impl/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-inline</artifactId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down
22 changes: 5 additions & 17 deletions data/crac/crac-io/crac-io-cim/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,8 @@
<build>
<plugins>
<plugin>
<groupId>org.jvnet.jaxb2.maven2</groupId>
<artifactId>maven-jaxb2-plugin</artifactId>
<configuration>
<args>
<arg>-Xinheritance</arg>
</args>
<plugins>
<plugin>
<groupId>org.jvnet.jaxb2_commons</groupId>
<artifactId>jaxb2-basics</artifactId>
<version>${maven.jvnet.jaxb2-basics.version}</version>
</plugin>
</plugins>
</configuration>
<groupId>org.jvnet.jaxb</groupId>
<artifactId>jaxb-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
Expand All @@ -63,8 +51,8 @@
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<groupId>jakarta.xml.bind</groupId>
<artifactId>jakarta.xml.bind-api</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
Expand Down Expand Up @@ -125,7 +113,7 @@
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-inline</artifactId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
import org.xml.sax.SAXException;

import javax.xml.XMLConstants;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import jakarta.xml.bind.JAXBContext;
import jakarta.xml.bind.JAXBException;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
Expand Down
2 changes: 1 addition & 1 deletion data/crac/crac-io/crac-io-commons/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-inline</artifactId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
18 changes: 3 additions & 15 deletions data/crac/crac-io/crac-io-csa-profiles/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,8 @@
<build>
<plugins>
<plugin>
<groupId>org.jvnet.jaxb2.maven2</groupId>
<artifactId>maven-jaxb2-plugin</artifactId>
<configuration>
<args>
<arg>-Xinheritance</arg>
</args>
<plugins>
<plugin>
<groupId>org.jvnet.jaxb2_commons</groupId>
<artifactId>jaxb2-basics</artifactId>
<version>${maven.jvnet.jaxb2-basics.version}</version>
</plugin>
</plugins>
</configuration>
<groupId>org.jvnet.jaxb</groupId>
<artifactId>jaxb-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
Expand Down Expand Up @@ -93,7 +81,7 @@
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-inline</artifactId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
20 changes: 5 additions & 15 deletions data/crac/crac-io/crac-io-cse/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,9 @@
<build>
<plugins>
<plugin>
<groupId>org.jvnet.jaxb2.maven2</groupId>
<artifactId>maven-jaxb2-plugin</artifactId>
<groupId>org.jvnet.jaxb</groupId>
<artifactId>jaxb-maven-plugin</artifactId>
<configuration>
<args>
<arg>-Xinheritance</arg>
</args>
<plugins>
<plugin>
<groupId>org.jvnet.jaxb2_commons</groupId>
<artifactId>jaxb2-basics</artifactId>
<version>${maven.jvnet.jaxb2-basics.version}</version>
</plugin>
</plugins>
<generatePackage>com.powsybl.openrao.data.cracio.cse.xsd</generatePackage>
<schemaDirectory>src/main/resources/com/powsybl/openrao/data/cracio/cse/xsd</schemaDirectory>
</configuration>
Expand All @@ -63,8 +53,8 @@
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<groupId>jakarta.xml.bind</groupId>
<artifactId>jakarta.xml.bind-api</artifactId>
</dependency>

<!-- Runtime dependencies -->
Expand Down Expand Up @@ -108,7 +98,7 @@
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-inline</artifactId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
import org.xml.sax.SAXException;

import javax.xml.XMLConstants;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import jakarta.xml.bind.JAXBContext;
import jakarta.xml.bind.JAXBException;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
Expand Down
29 changes: 5 additions & 24 deletions data/crac/crac-io/crac-io-fb-constraint/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,8 @@
<build>
<plugins>
<plugin>
<groupId>org.jvnet.jaxb2.maven2</groupId>
<artifactId>maven-jaxb2-plugin</artifactId>
<configuration>
<extension>true</extension>
<args>
<arg>-Xinheritance</arg>
<arg>-Xcopyable</arg>
<arg>-Xequals</arg>
</args>
<plugins>
<plugin>
<groupId>org.jvnet.jaxb2_commons</groupId>
<artifactId>jaxb2-basics</artifactId>
<version>${maven.jvnet.jaxb2-basics.version}</version>
</plugin>
</plugins>
</configuration>
<groupId>org.jvnet.jaxb</groupId>
<artifactId>jaxb-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
Expand Down Expand Up @@ -69,12 +54,8 @@
<artifactId>powsybl-iidm-api</artifactId>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
</dependency>
<dependency>
<groupId>org.jvnet.jaxb2_commons</groupId>
<artifactId>jaxb2-basics</artifactId>
<groupId>jakarta.xml.bind</groupId>
<artifactId>jakarta.xml.bind-api</artifactId>
</dependency>

<!-- Runtime dependencies -->
Expand Down Expand Up @@ -119,7 +100,7 @@
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-inline</artifactId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import com.powsybl.openrao.data.cracio.commons.ucte.UctePstHelper;
import com.powsybl.openrao.data.cracio.commons.ucte.UcteTopologicalElementHelper;

import javax.xml.bind.JAXBElement;
import jakarta.xml.bind.JAXBElement;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
import org.xml.sax.SAXException;

import javax.xml.XMLConstants;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import jakarta.xml.bind.JAXBContext;
import jakarta.xml.bind.JAXBException;
import jakarta.xml.bind.Unmarshaller;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
Expand Down
6 changes: 5 additions & 1 deletion data/crac/crac-io/crac-io-json/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
<artifactId>open-rao-crac-api</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.networknt</groupId>
<artifactId>json-schema-validator</artifactId>
</dependency>

<!-- Test dependencies -->
<dependency>
Expand Down Expand Up @@ -57,7 +61,7 @@
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-inline</artifactId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,30 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.google.auto.service.AutoService;
import com.networknt.schema.JsonSchema;
import com.powsybl.iidm.network.Network;
import com.powsybl.openrao.commons.OpenRaoException;
import com.powsybl.openrao.data.cracapi.Crac;
import com.powsybl.openrao.data.cracapi.CracCreationContext;
import com.powsybl.openrao.data.cracapi.io.Importer;
import com.powsybl.openrao.data.cracapi.parameters.CracCreationParameters;
import com.powsybl.openrao.data.cracio.json.deserializers.CracDeserializer;
import org.apache.commons.io.FilenameUtils;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UncheckedIOException;
import java.nio.charset.StandardCharsets;
import java.time.OffsetDateTime;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static com.powsybl.commons.json.JsonUtil.createObjectMapper;
import static com.powsybl.openrao.commons.logs.OpenRaoLoggerProvider.TECHNICAL_LOGS;
import static com.powsybl.openrao.data.cracio.json.JsonSchemaProvider.getSchema;
import static com.powsybl.openrao.data.cracio.json.JsonSchemaProvider.getValidationErrors;
import static com.powsybl.openrao.data.cracio.json.JsonSchemaProvider.isCracFile;

/**
* @author Viktor Terrier {@literal <viktor.terrier at rte-france.com>}
Expand All @@ -39,18 +48,24 @@ public String getFormat() {

@Override
public boolean exists(String filename, InputStream inputStream) {
if (!FilenameUtils.getExtension(filename).equals("json")) {
if (!filename.endsWith(".json")) {
return false;
}
try {
ObjectMapper objectMapper = createObjectMapper();
SimpleModule module = new SimpleModule();
module.addDeserializer(Crac.class, new CracDeserializer(true));
objectMapper.registerModule(module);
// TODO: replace this by a call to CracDeserializer.isValid
objectMapper.readValue(inputStream, Crac.class);
return true;
} catch (OpenRaoException | IOException e) {
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(inputStream.readAllBytes());
if (isCracFile(byteArrayInputStream)) {
byteArrayInputStream.reset();
Version cracVersion = readVersion(byteArrayInputStream);
JsonSchema jsonSchema = getSchema(cracVersion);
List<String> validationError = getValidationErrors(jsonSchema, byteArrayInputStream);
if (validationError.isEmpty()) {
return true;
}
throw new OpenRaoException("JSON file is not a valid CRAC v%s.%s. Reasons: %s".formatted(cracVersion.majorVersion(), cracVersion.minorVersion(), String.join("; ", validationError)));
}
return false;
} catch (IOException e) {
TECHNICAL_LOGS.debug("JSON file could not be processed as CRAC. Reason: {}", e.getMessage());
return false;
}
}
Expand Down Expand Up @@ -80,4 +95,12 @@ public CracCreationContext importData(InputStream inputStream, CracCreationParam
}
}

private static Version readVersion(ByteArrayInputStream cracByteArrayInputStream) {
String cracContent = new String(cracByteArrayInputStream.readAllBytes(), StandardCharsets.UTF_8);
cracByteArrayInputStream.reset();
Pattern versionPattern = Pattern.compile("\"version\"\\s?:\\s?\"([1-9]\\d*)\\.(\\d+)\"");
Matcher versionMatcher = versionPattern.matcher(cracContent);
versionMatcher.find();
return new Version(Integer.parseInt(versionMatcher.group(1)), Integer.parseInt(versionMatcher.group(2)));
}
}
Loading

0 comments on commit e4f5fa3

Please sign in to comment.