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 Sonar issues #214

Merged
merged 1 commit into from
Jan 15, 2025
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import org.apache.commons.io.FilenameUtils;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
Expand All @@ -27,9 +29,9 @@ private FileUtil() {

public static String getFilenameFromUrl(String stringUrl) {
try {
URL url = new URL(stringUrl);
URL url = new URI(stringUrl).toURL();
return FilenameUtils.getName(url.getPath());
} catch (IOException e) {
} catch (IOException | URISyntaxException | IllegalArgumentException e) {
throw new CseDataException(String.format("Exception occurred while retrieving file name from : %s", stringUrl), e);
}
}
Expand All @@ -48,16 +50,18 @@ public static String getFileVersion(String cgmFilename, ProcessType processType

public static void checkCgmFileName(String cgmFileUrl, ProcessType processType) {
String cgmFilename = getFilenameFromUrl(cgmFileUrl);
if (processType == ProcessType.IDCC) {
if (!Pattern.matches("\\d{8}_\\d{4}_\\d{3}_Transit_CSE\\d+.(uct|UCT)", cgmFilename)) {
throw new CseDataException(String.format("CGM file name %s is incorrect for process %s.", cgmFilename, processType.name()));
switch (processType) {
case IDCC -> {
if (!Pattern.matches("\\d{8}_\\d{4}_\\d{3}_Transit_CSE\\d+.(uct|UCT)", cgmFilename)) {
throw new CseDataException(String.format("CGM file name %s is incorrect for process %s.", cgmFilename, processType.name()));
}
}
} else if (processType == ProcessType.D2CC) {
if (!Pattern.matches("\\d{8}_\\d{4}_2D\\d_CO_Transit_CSE\\d+.(uct|UCT)", cgmFilename)) {
throw new CseDataException(String.format("CGM file name %s is incorrect for process %s.", cgmFilename, processType.name()));
case D2CC -> {
if (!Pattern.matches("\\d{8}_\\d{4}_2D\\d_CO_Transit_CSE\\d+.(uct|UCT)", cgmFilename)) {
throw new CseDataException(String.format("CGM file name %s is incorrect for process %s.", cgmFilename, processType.name()));
}
}
} else {
throw new CseInternalException(String.format("Process type %s is not handled", processType));
case null, default -> throw new CseInternalException(String.format("Process type %s is not handled", processType));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ String getFinalNetworkFilenameWithoutExtension(OffsetDateTime processTargetDate,

private void runLoadFlow(Network network) {
LoadFlowResult result = LoadFlow.run(network, LoadFlowParameters.load());
if (!result.isOk()) {
if (result.isFailed()) {
LOGGER.error("Loadflow computation diverged on network '{}'", network.getId());
throw new CseInternalException(String.format("Loadflow computation diverged on network %s", network.getId()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,21 @@
import com.farao_community.farao.cse.export_runner.app.FileUtil;
import com.farao_community.farao.cse.export_runner.app.configurations.UrlConfiguration;
import com.farao_community.farao.cse.runner.api.exception.CseInvalidDataException;

import com.powsybl.openrao.data.crac.api.Crac;

import com.powsybl.iidm.network.Network;
import com.powsybl.openrao.data.crac.api.Crac;
import com.powsybl.openrao.data.crac.io.cse.xsd.CRACDocumentType;
import com.powsybl.openrao.data.raoresult.api.RaoResult;
import com.powsybl.openrao.data.raoresult.io.json.RaoResultJsonImporter;
import jakarta.xml.bind.JAXBContext;
import jakarta.xml.bind.JAXBException;
import org.slf4j.Logger;
import org.springframework.stereotype.Service;

import jakarta.xml.bind.JAXBContext;
import jakarta.xml.bind.JAXBException;
import javax.xml.transform.stream.StreamSource;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;

/**
Expand Down Expand Up @@ -69,9 +69,9 @@ InputStream openUrlStream(String urlString) {
if (urlConfiguration.getWhitelist().stream().noneMatch(urlString::startsWith)) {
throw new CseInvalidDataException(String.format("URL '%s' is not part of application's whitelisted url's.", urlString));
}
URL url = new URL(urlString);
URL url = new URI(urlString).toURL();
return url.openStream(); // NOSONAR Usage of whitelist not triggered by Sonar quality assessment, even if listed as a solution to the vulnerability
} catch (IOException e) {
} catch (IOException | URISyntaxException | IllegalArgumentException e) {
businessLogger.error("Error while retrieving content of file \"{}\", link may have expired.", FileUtil.getFilenameFromUrl(urlString));
throw new CseDataException(String.format("Exception occurred while retrieving file content from %s", urlString), e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ private static boolean checkIfPrasCombinationHasImpactOnNetwork(Set<String> raTo
private List<String> getActivatedRangeActionInPreventive(Crac crac, DichotomyResult<DichotomyRaoResponse> dichotomyResult) {
if (dichotomyResult.hasValidStep() && dichotomyResult.getHighestValidStep().getRaoResult() != null) {
List<String> prasNames = dichotomyResult.getHighestValidStep().getRaoResult().getActivatedNetworkActionsDuringState(crac.getPreventiveState()).stream().map(NetworkAction::getName).collect(Collectors.toList());
prasNames.addAll(dichotomyResult.getHighestValidStep().getRaoResult().getActivatedRangeActionsDuringState(crac.getPreventiveState()).stream().map(RangeAction::getName).collect(Collectors.toList()));
prasNames.addAll(dichotomyResult.getHighestValidStep().getRaoResult().getActivatedRangeActionsDuringState(crac.getPreventiveState()).stream().map(RangeAction::getName).toList());
return prasNames;
} else {
return Collections.emptyList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import com.powsybl.iidm.network.Substation;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
Expand All @@ -34,7 +33,7 @@ public ZonalScalableProvider(FileImporter fileImporter) {
this.fileImporter = fileImporter;
}

public ZonalData<Scalable> get(String glskUrl, Network network, ProcessType processType) throws IOException {
public ZonalData<Scalable> get(String glskUrl, Network network, ProcessType processType) {
ZonalData<Scalable> zonalScalable = fileImporter.importGlsk(glskUrl, network);
Arrays.stream(CseCountry.values()).forEach(country -> checkCseCountryInGlsk(zonalScalable, country));
stackScalableOnLoads(network, zonalScalable, processType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,34 +10,40 @@
import com.farao_community.farao.cse.data.CseDataException;
import com.farao_community.farao.cse.data.CseReferenceExchanges;
import com.farao_community.farao.cse.data.DataUtil;
import com.farao_community.farao.cse.data.ntc.*;
import com.farao_community.farao.cse.data.ntc.DailyNtcDocument;
import com.farao_community.farao.cse.data.ntc.DailyNtcDocumentAdapted;
import com.farao_community.farao.cse.data.ntc.Ntc;
import com.farao_community.farao.cse.data.ntc.YearlyNtcDocument;
import com.farao_community.farao.cse.data.ntc.YearlyNtcDocumentAdapted;
import com.farao_community.farao.cse.data.ntc2.Ntc2;
import com.farao_community.farao.cse.data.target_ch.LineFixedFlows;
import com.farao_community.farao.cse.data.xsd.NTCAnnualDocument;
import com.farao_community.farao.cse.data.xsd.NTCReductionsDocument;
import com.farao_community.farao.cse.import_runner.app.configurations.UrlConfiguration;
import com.farao_community.farao.cse.import_runner.app.util.FileUtil;
import com.farao_community.farao.cse.import_runner.app.util.Ntc2Util;
import com.farao_community.farao.cse.runner.api.resource.ProcessType;
import com.powsybl.openrao.data.crac.api.Crac;
import com.powsybl.openrao.data.crac.io.cse.xsd.CRACDocumentType;
import com.powsybl.openrao.data.raoresult.api.RaoResult;
import com.farao_community.farao.cse.runner.api.exception.CseInvalidDataException;
import com.farao_community.farao.cse.runner.api.resource.ProcessType;
import com.powsybl.glsk.api.io.GlskDocumentImporters;
import com.powsybl.glsk.commons.ZonalData;
import com.powsybl.iidm.modification.scalable.Scalable;
import com.powsybl.iidm.network.Network;
import com.powsybl.openrao.data.crac.api.Crac;
import com.powsybl.openrao.data.crac.io.cse.xsd.CRACDocumentType;
import com.powsybl.openrao.data.raoresult.api.RaoResult;
import com.powsybl.openrao.data.raoresult.io.json.RaoResultJsonImporter;
import jakarta.xml.bind.JAXBContext;
import jakarta.xml.bind.JAXBException;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.springframework.stereotype.Service;

import jakarta.xml.bind.JAXBContext;
import jakarta.xml.bind.JAXBException;
import javax.xml.transform.stream.StreamSource;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.time.OffsetDateTime;
import java.util.HashMap;
Expand Down Expand Up @@ -172,19 +178,19 @@ InputStream openUrlStream(String urlString) {
if (urlConfiguration.getWhitelist().stream().noneMatch(urlString::startsWith)) {
throw new CseInvalidDataException(String.format("URL '%s' is not part of application's whitelisted url's.", urlString));
}
URL url = new URL(urlString);
URL url = new URI(urlString).toURL();
return url.openStream(); // NOSONAR Usage of whitelist not triggered by Sonar quality assessment, even if listed as a solution to the vulnerability
} catch (IOException e) {
} catch (IOException | URISyntaxException | IllegalArgumentException e) {
businessLogger.error("Error while retrieving content of file \"{}\", link may have expired.", getFileNameFromUrl(urlString));
throw new CseDataException(String.format("Exception occurred while retrieving file content from %s", urlString), e);
}
}

private String getFileNameFromUrl(String stringUrl) {
try {
URL url = new URL(stringUrl);
URL url = new URI(stringUrl).toURL();
return FilenameUtils.getName(url.getPath());
} catch (IOException e) {
} catch (IOException | URISyntaxException | IllegalArgumentException e) {
throw new CseDataException(String.format("Exception occurred while retrieving file name from : %s", stringUrl), e);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,25 @@
package com.farao_community.farao.cse.import_runner.app.services;

import com.farao_community.farao.cse.import_runner.app.dichotomy.ZonalScalableProvider;
import com.farao_community.farao.cse.import_runner.app.util.FileUtil;
import com.farao_community.farao.cse.runner.api.resource.ProcessType;
import com.powsybl.openrao.commons.EICode;
import com.farao_community.farao.cse.computation.BorderExchanges;
import com.farao_community.farao.cse.import_runner.app.CseData;
import com.farao_community.farao.cse.import_runner.app.configurations.ProcessConfiguration;
import com.farao_community.farao.cse.import_runner.app.dichotomy.CseCountry;
import com.farao_community.farao.cse.import_runner.app.dichotomy.NetworkShifterUtil;
import com.farao_community.farao.cse.import_runner.app.dichotomy.ZonalScalableProvider;
import com.farao_community.farao.cse.import_runner.app.util.FileUtil;
import com.farao_community.farao.cse.runner.api.resource.CseRequest;
import com.farao_community.farao.cse.runner.api.resource.ProcessType;
import com.farao_community.farao.minio_adapter.starter.GridcapaFileGroup;
import com.powsybl.glsk.api.io.GlskDocumentImporters;
import com.powsybl.glsk.commons.ZonalData;
import com.powsybl.iidm.modification.scalable.Scalable;
import com.powsybl.iidm.modification.scalable.ScalingParameters;
import com.powsybl.iidm.network.Country;
import com.powsybl.iidm.network.Network;
import com.powsybl.openrao.commons.EICode;
import org.slf4j.Logger;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.security.SecureRandom;
import java.util.HashMap;
import java.util.Map;
Expand All @@ -44,7 +43,7 @@ public InitialShiftService(ZonalScalableProvider zonalScalableProvider, Logger b
this.zonalScalableProvider = zonalScalableProvider;
}

void performInitialShiftFromVulcanusLevelToNtcLevel(Network network, CseData cseData, CseRequest cseRequest, Map<String, Double> referenceExchanges, Map<String, Double> ntcsByEic) throws IOException {
void performInitialShiftFromVulcanusLevelToNtcLevel(Network network, CseData cseData, CseRequest cseRequest, Map<String, Double> referenceExchanges, Map<String, Double> ntcsByEic) {
Map<String, Double> preprocessedNetworkNps = BorderExchanges.computeCseCountriesBalances(network);
for (Map.Entry<String, Double> entry : preprocessedNetworkNps.entrySet()) {
businessLogger.info("Summary : Net positions on preprocessed network : for area {} : net position is {}.", entry.getKey(), entry.getValue());
Expand Down Expand Up @@ -87,7 +86,7 @@ public Map<String, Double> getInitialShiftValues(CseData cseData, Map<String, Do
return initialShifts;
}

private void shiftNetwork(Map<String, Double> scalingValuesByCountry, CseRequest cseRequest, Network network) throws IOException {
private void shiftNetwork(Map<String, Double> scalingValuesByCountry, CseRequest cseRequest, Network network) {
ZonalData<Scalable> zonalScalable = getZonalScalableForProcess(cseRequest, network);
String initialVariantId = network.getVariantManager().getWorkingVariantId();
// SecureRandom used to be compliant with sonar
Expand All @@ -102,11 +101,12 @@ private void shiftNetwork(Map<String, Double> scalingValuesByCountry, CseRequest
String zoneId = entry.getKey();
double asked = entry.getValue();
double done = zonalScalable.getData(zoneId).scale(network, asked, scalingParameters);
businessLogger.info(String.format("Applying variation on zone %s (target: %.2f, done: %.2f)", zoneId, asked, done));
final String message = String.format("Applying variation on zone %s (target: %.2f, done: %.2f)", zoneId, asked, done);
businessLogger.info(message);

if (Math.abs(done - asked) > 1e-2) {
businessLogger.warn(String.format("Glsk limitation : Incomplete variation on zone %s (target: %.3f, done: %.3f)",
zoneId, asked, done));
final String warningMessage = String.format("Glsk limitation : Incomplete variation on zone %s (target: %.3f, done: %.3f)", zoneId, asked, done);
businessLogger.warn(warningMessage);
if (zoneId.equals(new EICode(Country.IT).getAreaCode())) {
double italyGlskLimitationSplittingFactor = done / asked;
businessLogger.warn("Glsk limitation is reached for italy, shifts will be updated proportionally to coefficient: {}", italyGlskLimitationSplittingFactor);
Expand All @@ -124,7 +124,7 @@ private void shiftNetwork(Map<String, Double> scalingValuesByCountry, CseRequest

}

private ZonalData<Scalable> getZonalScalableForProcess(CseRequest cseRequest, Network network) throws IOException {
private ZonalData<Scalable> getZonalScalableForProcess(CseRequest cseRequest, Network network) {
return cseRequest.getProcessType().equals(ProcessType.D2CC) ?
zonalScalableProvider.get(cseRequest.getMergedGlskUrl(), network, ProcessType.D2CC) :
GlskDocumentImporters.importGlsk(fileImporter.openUrlStream(cseRequest.getMergedGlskUrl())).getZonalScalable(network);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,10 @@ public MerchantLineService(MendrisioConfiguration mendrisioConfiguration, Logger
}

public void activateMerchantLine(ProcessType processType, Network network, CseData cseData) {
if (processType == ProcessType.IDCC) {
activateMerchantLineForIdcc(network, cseData);
} else if (processType == ProcessType.D2CC) {
activateMerchantLineForD2cc(network, cseData);
} else {
throw new CseInternalException(String.format("Process type %s is not handled", processType));
switch (processType) {
case IDCC -> activateMerchantLineForIdcc(network, cseData);
case D2CC -> activateMerchantLineForD2cc(network, cseData);
default -> throw new CseInternalException(String.format("Process type %s is not handled", processType));
}
}

Expand Down Expand Up @@ -74,7 +72,7 @@ private double getMendrisioTargetFlowForD2cc(Network network, CseData cseData) {
mendrisioConfiguration.getMendrisioCagnoTargetChId(),
network,
ucteNetworkHelper);
double mendrisioCagnoTargetFlow = reducedFlow.isEmpty() ? defaultFlow : Math.min(defaultFlow, reducedFlow.get());
final double mendrisioCagnoTargetFlow = reducedFlow.map(aDouble -> Math.min(defaultFlow, aDouble)).orElse(defaultFlow);

if (LOGGER.isInfoEnabled()) {
LOGGER.info(String.format("Target flow for Mendrisio-Cagno is %.0f MW", mendrisioCagnoTargetFlow));
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
import org.apache.commons.io.FilenameUtils;

import java.net.MalformedURLException;
import java.net.URL;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand All @@ -27,8 +28,8 @@ private FileUtil() {

public static String getFilenameFromUrl(String url) {
try {
return FilenameUtils.getName(new URL(url).getPath());
} catch (MalformedURLException e) {
return FilenameUtils.getName(new URI(url).toURL().getPath());
} catch (MalformedURLException | URISyntaxException | IllegalArgumentException e) {
throw new CseInvalidDataException(String.format("URL is invalid: %s", url), e);
}
}
Expand Down
Loading
Loading