Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
massifben committed Dec 16, 2024
1 parent b80e273 commit a837d82
Show file tree
Hide file tree
Showing 6 changed files with 339 additions and 307 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public Optional<DoLinkedToDa> findDoLinkedToDa(TDataTypeTemplates dtt, String lN
// Prepare DataObject
DataObject dataObject = new DataObject(tdo.getName(), tdoType.getCdc(), doLinkedToDaFilter.sdoNames());
// Search first DA from last DoType
return sdoOrDAService.findDA(lastDoType, tda1 -> tda1.getName().equals(doLinkedToDaFilter.daName()))
return sdoOrDAService.findDA(lastDoType, tda -> tda.getName().equals(doLinkedToDaFilter.daName()))
.flatMap(tda -> {
// Prepare DataAttribute
DataAttribute dataAttribute = new DataAttribute();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,24 @@

package org.lfenergy.compas.sct.commons;

import org.apache.commons.lang3.StringUtils;
import org.lfenergy.compas.scl2007b4.model.SCL;
import org.lfenergy.compas.scl2007b4.model.TAnyLN;
import org.lfenergy.compas.scl2007b4.model.TBaseElement;
import org.lfenergy.compas.scl2007b4.model.TPredefinedBasicTypeEnum;
import org.lfenergy.compas.scl2007b4.model.*;
import org.lfenergy.compas.sct.commons.domain.DataAttribute;
import org.lfenergy.compas.sct.commons.domain.DoLinkedToDa;
import org.lfenergy.compas.sct.commons.domain.DoLinkedToDaFilter;
import org.lfenergy.compas.sct.commons.dto.SclReportItem;
import org.lfenergy.compas.sct.commons.util.CommonConstants;
import org.lfenergy.compas.sct.commons.util.PrivateUtils;
import org.lfenergy.compas.sct.commons.util.SclConstructorHelper;

import java.util.List;
import java.util.Optional;
import java.util.stream.Stream;

public class LNodeStatusService {

private static final String ON = "on";
private static final String OFF = "off";
private static final String LNODE_STATUS_PRIVATE_TYPE = "COMPAS-LNodeStatus";
private final LnService lnService = new LnService();
private final DataTypeTemplatesService dataTypeTemplatesService = new DataTypeTemplatesService();

Expand All @@ -30,62 +32,59 @@ public List<SclReportItem> updateLnModStValBasedOnLNodeStatus(SCL scl) {
.flatMap(tBay -> tBay.getFunction().stream())
.flatMap(tFunction -> tFunction.getLNode().stream())
.map(tlNode -> {
String lNodeLnodeStatus = extractStringPrivate(tlNode, "COMPAS-LNodeStatus")
.orElseThrow(); // FIXME
TAnyLN tAnyLN = findLn(scl, tlNode.getIedName(), tlNode.getLdInst(), tlNode.getLnClass().getFirst(), tlNode.getLnInst(), tlNode.getPrefix())
.orElseThrow();// FIXME
String lnLNodeStatus = extractStringPrivate(tAnyLN, "COMPAS-LNodeStatus")
.orElseThrow();
switch (lNodeLnodeStatus) {
case "on" -> {
if (lnLNodeStatus.contains("on")) {
// Met à jour Mod stVal s'il existe
lnService.getDaiModStVal(tAnyLN)
.ifPresent(tdai -> {
String lNodeLNS = PrivateUtils.extractStringPrivate(tlNode, LNODE_STATUS_PRIVATE_TYPE)
.orElseThrow(); // FIXME
TAnyLN anyLn = findLn(scl, tlNode.getIedName(), tlNode.getLdInst(), tlNode.getLnClass().getFirst(), tlNode.getLnInst(), tlNode.getPrefix())
.orElseThrow(() -> new RuntimeException("Aucun LN trouvé dans l'IED %s, LD %s, LN %s,%s,%s".formatted(tlNode.getIedName(), tlNode.getLdInst(), tlNode.getLnClass().getFirst(), tlNode.getLnInst(), tlNode.getPrefix())));// FIXME
String anyLnLNS = PrivateUtils.extractStringPrivate(anyLn, LNODE_STATUS_PRIVATE_TYPE)
.orElseThrow();
if (!lNodeLNS.equals(ON) && !lNodeLNS.equals(OFF)) {
throw new RuntimeException("FIXME");
}

dataTypeTemplatesService.findDoLinkedToDa(scl.getDataTypeTemplates(), tAnyLN.getLnType(), DoLinkedToDaFilter.from(CommonConstants.MOD_DO_NAME, CommonConstants.STVAL_DA_NAME))
.map(DoLinkedToDa::dataAttribute)
.filter(dataAttribute -> TPredefinedBasicTypeEnum.ENUM.equals(dataAttribute.getBType()))
.map(DataAttribute::getType);
});
} else {
// erreur // FIXME
}
}
case "of" -> {
if (lnLNodeStatus.contains("off")) {
// Met à jour Mod stVal s'il existe
} else {
// erreur // FIXME
}
}
default -> {
// erreur // FIXME
}
}
return null;
}
);
if (anyLnLNS.contains(lNodeLNS)) {
// Met à jour Mod stVal s'il existe
lnService.getDaiModStVal(anyLn)
.ifPresent(tdai -> getModStValEnumValues(scl.getDataTypeTemplates(), anyLn.getLnType())
.filter(lNodeLNS::equals)
.findFirst()
.ifPresentOrElse(ignored -> {
tdai.unsetVal();
tdai.getVal().add(SclConstructorHelper.newVal(lNodeLNS));
},
() -> {
throw new RuntimeException("FIXME");
}));
} else {
throw new RuntimeException("FIXME");
}
return null;
})
.toList();

return List.of();
}
}

private static Optional<String> extractStringPrivate(TBaseElement tBaseElement, String privateType) {
return tBaseElement.getPrivate().stream()
.filter(tPrivate -> privateType.equals(tPrivate.getType()))
.flatMap(tPrivate -> tPrivate.getContent().stream())
.filter(String.class::isInstance)
.map(String.class::cast)
.filter(StringUtils::isNotBlank)
.findFirst();
}
private Stream<String> getModStValEnumValues(TDataTypeTemplates dataTypeTemplates, String lnType) {
return dataTypeTemplatesService.findDoLinkedToDa(dataTypeTemplates, lnType, DoLinkedToDaFilter.from(CommonConstants.MOD_DO_NAME, CommonConstants.STVAL_DA_NAME))
.map(DoLinkedToDa::dataAttribute)
.filter(dataAttribute -> TPredefinedBasicTypeEnum.ENUM.equals(dataAttribute.getBType()))
.map(DataAttribute::getType)
.flatMap(enumId ->
dataTypeTemplates.getEnumType().stream()
.filter(tEnumType -> tEnumType.getId().equals(enumId))
.findFirst())
.stream()
.flatMap(tEnumType -> tEnumType.getEnumVal().stream())
.map(TEnumVal::getValue);
}

private Optional<TAnyLN> findLn(SCL scl, String iedName, String ldInst, String lnClass, String lnInst, String prefix) {
return scl.getIED().stream()
.filter(tied -> iedName.equals(tied.getName()))
.findFirst()
.flatMap(tied -> new LdeviceService().findLdevice(tied, tlDevice -> ldInst.equals(tlDevice.getInst())))
.flatMap(tlDevice -> lnService.findAnyLn(tlDevice, tAnyLN -> lnService.matchesLn(tAnyLN, lnInst, lnClass, prefix)));
return scl.getIED().stream()
.filter(tied -> iedName.equals(tied.getName()))
.findFirst()
.flatMap(tied -> new LdeviceService().findLdevice(tied, tlDevice -> ldInst.equals(tlDevice.getInst())))
.flatMap(tlDevice -> lnService.findAnyLn(tlDevice, tAnyLN -> lnService.matchesLn(tAnyLN, lnClass, lnInst, prefix)));

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import jakarta.xml.bind.JAXBElement;
import lombok.NonNull;
import lombok.experimental.UtilityClass;
import org.apache.commons.lang3.StringUtils;
import org.lfenergy.compas.scl2007b4.model.*;
import org.lfenergy.compas.sct.commons.dto.PrivateLinkedToStds;
import org.lfenergy.compas.sct.commons.exception.ScdException;
Expand Down Expand Up @@ -320,4 +321,13 @@ public static void copyCompasICDHeaderFromLNodePrivateIntoSTDPrivate(TPrivate st
}


public static Optional<String> extractStringPrivate(TBaseElement tBaseElement, String privateType) {
return tBaseElement.getPrivate().stream()
.filter(tPrivate -> privateType.equals(tPrivate.getType()))
.flatMap(tPrivate -> tPrivate.getContent().stream())
.filter(String.class::isInstance)
.map(String.class::cast)
.filter(StringUtils::isNotBlank)
.findFirst();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,19 @@
package org.lfenergy.compas.sct.commons;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.lfenergy.compas.scl2007b4.model.SCL;
import org.lfenergy.compas.sct.commons.dto.SclReportItem;
import org.lfenergy.compas.sct.commons.testhelpers.SclHelper;
import org.lfenergy.compas.sct.commons.testhelpers.SclTestMarshaller;

import java.util.List;
import java.util.stream.Stream;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Named.named;
import static org.lfenergy.compas.sct.commons.testhelpers.SclHelper.*;

class LNodeStatusServiceTest {
Expand All @@ -25,16 +29,31 @@ void setUp() throws Exception {
lNodeStatusService = new LNodeStatusService();
}

@Test
void updateLnStatusBasedOnPrivateLNodeStatus_should_succeed() {
@ParameterizedTest
@MethodSource("provideUpdateModStVal")
void updateLnStatusBasedOnPrivateLNodeStatus_should_update_Mod_stVal(String ldInst, String lnClass, String lnInst, String expected) {
// Given
SCL scl = SclTestMarshaller.getSCLFromFile("/scl-lnodestatus/lnodestatus.scd");
// When
List<SclReportItem> sclReportItems = lNodeStatusService.updateLnModStValBasedOnLNodeStatus(scl);
// Then
assertThat(sclReportItems).isEmpty();
assertThat(getValue(findDai(findLn(scl, "IED_NAME_1", "LDEVICE_1", "PDIS", "1", ""), "Mod.stVal")))
.isEqualTo("on");
assertThat(findDai(scl, "IED_NAME_1", ldInst, lnClass, lnInst, "", "Mod", "stVal"))
.map(SclHelper::getValue)
.hasValue(expected);
}

public static Stream<Arguments> provideUpdateModStVal() {
return Stream.of(
Arguments.of(named("LN 'on' à mettre à 'on'", "LDEVICE_1"), "PDIS", "1", "on"),
Arguments.of(named("LN 'off;on' à mettre à 'on'", "LDEVICE_1"), "PDIS", "2", "on"),
Arguments.of(named("LN 'off' à mettre à 'off'", "LDEVICE_1"), "PDIS", "3", "off"),
Arguments.of(named("LN 'off;on' à mettre à 'off'", "LDEVICE_1"), "PDIS", "3", "off"),
Arguments.of(named("LN0 'on' à mettre à 'on'", "LDEVICE_1"), "LLN0", "", "on"),
Arguments.of(named("LN0 'off;on' à mettre à 'on'", "LDEVICE_2"), "LLN0", "", "on"),
Arguments.of(named("LN0 'off' à mettre à 'off'", "LDEVICE_3"), "LLN0", "", "off"),
Arguments.of(named("LN0 'off;on' à mettre à 'off'", "LDEVICE_4"), "LLN0", "", "off")
);
}


Expand Down
Loading

0 comments on commit a837d82

Please sign in to comment.