Skip to content

Commit

Permalink
feat: remove LD LDevice Status verification
Browse files Browse the repository at this point in the history
Signed-off-by: Samir Romdhani <[email protected]>

add test , LDevice present and not present in Substation

Signed-off-by: Samir Romdhani <[email protected]>
  • Loading branch information
samirromdhani committed Jan 27, 2025
1 parent 4dc58ba commit 3f9604f
Show file tree
Hide file tree
Showing 23 changed files with 615 additions and 1,888 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,20 @@
import org.lfenergy.compas.scl2007b4.model.*;
import org.lfenergy.compas.sct.commons.api.LnEditor;
import org.lfenergy.compas.sct.commons.domain.*;
import org.lfenergy.compas.sct.commons.dto.SclReportItem;
import org.lfenergy.compas.sct.commons.scl.ldevice.LDeviceActivation;
import org.lfenergy.compas.sct.commons.scl.ln.LnId;
import org.lfenergy.compas.sct.commons.util.ActiveStatus;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static org.lfenergy.compas.sct.commons.util.CommonConstants.MOD_DO_NAME;
import static org.lfenergy.compas.sct.commons.util.CommonConstants.STVAL_DA_NAME;
import static org.lfenergy.compas.sct.commons.util.CommonConstants.*;
import static org.lfenergy.compas.sct.commons.util.SclConstructorHelper.newVal;

@Slf4j
Expand Down Expand Up @@ -146,6 +150,77 @@ public void updateOrCreateDOAndDAInstances(TAnyLN tAnyLN, DoLinkedToDa doLinkedT
});
}

/**
* Activate used LDevice and Deactivate unused LDevice in {@link TLNode <em><b>TLNode </b></em>}
*
* @param scd SCL file for which LDevice should be activated or deactivated
* @return list of encountered errors
*/
public List<SclReportItem> updateLDeviceStatus(SCL scd, SubstationService substationService, LdeviceService ldeviceService, DataTypeTemplatesService dataTypeTemplatesService) {
List<SclReportItem> sclReportItems = new ArrayList<>();
scd.getIED().forEach(tied -> ldeviceService.getLdevices(tied)
.forEach(tlDevice -> getAnylns(tlDevice)
.forEach(tln -> {
String xpath = "/SCL/IED[@name=%s]/AccessPoint/Server/LDevice[@inst=%s]/LN[class=%s]".formatted(tied.getName(), tlDevice.getInst(), LnId.from(tln).lnClass());
DoLinkedToDaFilter doLinkedToDaFilter = DoLinkedToDaFilter.from(BEHAVIOUR_DO_NAME, STVAL_DA_NAME);
Optional<DoLinkedToDa> optionalBehStVal = dataTypeTemplatesService.getFilteredDoLinkedToDa(scd.getDataTypeTemplates(), tln.getLnType(), doLinkedToDaFilter).findFirst();
if (optionalBehStVal.isEmpty()) {
sclReportItems.add(SclReportItem.error(xpath, "The LDevice doesn't have a DO @name='Beh'"));
return;
}

Optional<ActiveStatus> modStValOptional = getDaiModStValValue(tln);
if (modStValOptional.isEmpty()) {
sclReportItems.add(SclReportItem.error(xpath, "The LDevice doesn't have a DO @name='Mod'"));
return;
}

Set<String> enumValues = dataTypeTemplatesService.getEnumValues(scd.getDataTypeTemplates(), tln.getLnType(), doLinkedToDaFilter).collect(Collectors.toSet());
if (!enumValues.contains(ActiveStatus.ON.getValue()) && !enumValues.contains(ActiveStatus.OFF.getValue())) {
sclReportItems.add(SclReportItem.error(xpath, "The LDevice cannot be activated or desactivated because its BehaviourKind Enum contains NOT 'on' AND NOT 'off'."));
return;
}

Optional<TLNode> tlNodeOptional = substationService.getLNodes(scd).stream().filter(tlNode1 -> tlNode1.isSetIedName() && tlNode1.isSetLdInst())
.filter(tlNode -> tlNode.getIedName().equals(tied.getName()) && tlNode.getLdInst().equals(tlDevice.getInst()))
.findFirst();

LDeviceActivation lDeviceStatusActivation = new LDeviceActivation(modStValOptional.get().getValue(), tlNodeOptional.isPresent(), enumValues, tln);
activateOrDeactivateLDeviceStatus(lDeviceStatusActivation, xpath, sclReportItems);
})));
return sclReportItems;
}

private void activateOrDeactivateLDeviceStatus(LDeviceActivation lDeviceStatusActivation, String xpath, List<SclReportItem> sclReportItems) {
if(lDeviceStatusActivation.isPresentInSubstation()){
// Activate LDevice
if(lDeviceStatusActivation.enumValues().contains(ActiveStatus.ON.getValue())) {
update(lDeviceStatusActivation.tln(), lDeviceStatusActivation.modStValCurrentValue(), ActiveStatus.ON.getValue());
}else {
sclReportItems.add(SclReportItem.error(xpath, "The LDevice cannot be set to 'on' but has been selected into SSD."));
}
} else {
// Deactivate LDevice
if(lDeviceStatusActivation.enumValues().contains(ActiveStatus.OFF.getValue())) {
update(lDeviceStatusActivation.tln(), lDeviceStatusActivation.modStValCurrentValue(), ActiveStatus.OFF.getValue());
}else {
sclReportItems.add(SclReportItem.error(xpath, "The LDevice cannot be set to 'off' but it has not been selected into SSD."));
}
}
}

private void update(TAnyLN tAnyLN, String modStValValue, String modStValNewVal) {
if (!modStValValue.equals(modStValNewVal)) {
DataObject dataObject = new DataObject();
dataObject.setDoName(MOD_DO_NAME);
DataAttribute dataAttribute = new DataAttribute();
dataAttribute.setDaName(STVAL_DA_NAME);
dataAttribute.setDaiValues(List.of(new DaVal(null, modStValNewVal)));
DoLinkedToDa doLinkedToDa = new DoLinkedToDa(dataObject, dataAttribute);
updateOrCreateDOAndDAInstances(tAnyLN, doLinkedToDa);
}
}

public void completeFromDAInstance(TIED tied, String ldInst, TAnyLN anyLN, DoLinkedToDa doLinkedToDa) {
getDOAndDAInstances(anyLN, doLinkedToDa.toFilter())
.ifPresent(tdai -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.tuple.Pair;
import org.lfenergy.compas.scl2007b4.model.*;
import org.lfenergy.compas.sct.commons.api.SclEditor;
import org.lfenergy.compas.sct.commons.dto.*;
Expand Down Expand Up @@ -187,19 +186,6 @@ public void importSTDElementsInSCD(SCL scd, List<SCL> stds) throws ScdException
});
}

@Override
public List<SclReportItem> updateLDeviceStatus(SCL scd) {
SclRootAdapter sclRootAdapter = new SclRootAdapter(scd);
SubstationAdapter substationAdapter = sclRootAdapter.getSubstationAdapter();
final List<Pair<String, String>> iedNameLdInstList = substationAdapter.getIedAndLDeviceNamesForLN0FromLNode();
return sclRootAdapter.streamIEDAdapters()
.flatMap(IEDAdapter::streamLDeviceAdapters)
.map(LDeviceAdapter::getLN0Adapter)
.map(ln0Adapter -> ln0Adapter.updateLDeviceStatus(iedNameLdInstList))
.flatMap(Optional::stream)
.toList();
}

@Override
public List<SclReportItem> updateDoInRef(SCL scd) {
SclRootAdapter sclRootAdapter = new SclRootAdapter(scd);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@

import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import org.lfenergy.compas.scl2007b4.model.SCL;
import org.lfenergy.compas.scl2007b4.model.TBay;
import org.lfenergy.compas.scl2007b4.model.TSubstation;
import org.lfenergy.compas.scl2007b4.model.TVoltageLevel;
import org.apache.commons.lang3.tuple.Pair;
import org.lfenergy.compas.scl2007b4.model.*;
import org.lfenergy.compas.sct.commons.api.SubstationEditor;
import org.lfenergy.compas.sct.commons.exception.ScdException;
import org.lfenergy.compas.sct.commons.scl.sstation.BayAdapter;
import org.lfenergy.compas.sct.commons.scl.sstation.VoltageLevelAdapter;

import java.util.List;
import java.util.stream.Collectors;

@RequiredArgsConstructor
public class SubstationService implements SubstationEditor {
Expand Down Expand Up @@ -42,6 +45,21 @@ public void addSubstation(@NonNull SCL scd, @NonNull SCL ssd) throws ScdExceptio
}
}

/**
* Gets a pair of IedName and LDevice inst from Substation LNodes for LN0 type object
* @return a pair of Ied name and LDevice inst attributes
*/
public List<TLNode> getLNodes(SCL scd) {
return scd.getSubstation().getFirst()
.getVoltageLevel().stream()
.flatMap(tVoltageLevel -> tVoltageLevel.getBay().stream())
.flatMap(tBay -> tBay.getFunction().stream())
.flatMap(tFunction -> tFunction.getLNode().stream())
.filter(tlNode -> tlNode.getLnClass().contains(TLLN0Enum.LLN_0.value()))
.toList();
}


/**
* Creates new VoltageLevel section or updates VoltageLevel contents
* @param scd SCL contain Substation in which VoltageLevel should be created/updated
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

import lombok.NonNull;
import org.lfenergy.compas.scl2007b4.model.SCL;
import org.lfenergy.compas.scl2007b4.model.TLNode;
import org.lfenergy.compas.sct.commons.dto.*;
import org.lfenergy.compas.sct.commons.exception.ScdException;

Expand Down Expand Up @@ -137,13 +136,6 @@ public interface SclEditor {
*/
void importSTDElementsInSCD(SCL scd, List<SCL> stds) throws ScdException;

/**
* Activate used LDevice and Deactivate unused LDevice in {@link TLNode <em><b>TLNode </b></em>}
*
* @param scd SCL file for which LDevice should be activated or deactivated
* @return list of encountered errors
*/
List<SclReportItem> updateLDeviceStatus(SCL scd);

/**
* Update DAIs of DO InRef in all LN0 of the SCD using matching ExtRef information.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,122 +1,12 @@
// SPDX-FileCopyrightText: 2022 RTE FRANCE
// SPDX-FileCopyrightText: 2025 RTE FRANCE
//
// SPDX-License-Identifier: Apache-2.0

package org.lfenergy.compas.sct.commons.scl.ldevice;

import lombok.Getter;
import lombok.Setter;
import org.apache.commons.lang3.tuple.Pair;
import org.lfenergy.compas.scl2007b4.model.TCompasLDeviceStatus;
import org.lfenergy.compas.sct.commons.util.ActiveStatus;
import org.lfenergy.compas.scl2007b4.model.TAnyLN;

import java.util.List;
import java.util.Set;

/**
* Common class for all states that define if LDevice should be activated or not
* regardless of the CompasLDeviceStatus Private, Enum Values of DO 'Beh' and if it's referenced in Substation...LNode or not
*/
@Getter
@Setter
public class LDeviceActivation {

private final List<Pair<String, String>> iedNameLdInstList;
private boolean isUpdatable;
private String newVal;
private String errorMessage;

public LDeviceActivation(List<Pair<String, String>> iedNameLdInstList) {
this.iedNameLdInstList = iedNameLdInstList;
}

/**
* checks whether LDevice status is authorized to be activated or Not
* @param iedName Ied name value which LDevice appear
* @param ldInst LDevice inst value
* @param compasLDeviceStatus Private value
* @param enumValues enum values
*/
public void checkLDeviceActivationStatus(String iedName, String ldInst, TCompasLDeviceStatus compasLDeviceStatus, Set<String> enumValues) {
if (!enumValues.contains(ActiveStatus.ON.getValue()) && !enumValues.contains(ActiveStatus.OFF.getValue())) {
errorMessage = "The LDevice cannot be activated or desactivated because its BehaviourKind Enum contains NOT 'on' AND NOT 'off'.";
}
if (!enumValues.contains(ActiveStatus.ON.getValue()) && enumValues.contains(ActiveStatus.OFF.getValue())) {
if (isDeclaredInSubstation(iedName, ldInst)) {
errorMessage = "The LDevice cannot be set to 'on' but has been selected into SSD.";
} else {
isUpdatable = true;
newVal = ActiveStatus.OFF.getValue();
}
}
if(compasLDeviceStatus.equals(TCompasLDeviceStatus.ACTIVE) ||
compasLDeviceStatus.equals(TCompasLDeviceStatus.UNTESTED)){
checkAuthorisationToActivateLDevice(iedName, ldInst, enumValues);
}
if(compasLDeviceStatus.equals(TCompasLDeviceStatus.INACTIVE)){
checkAuthorisationToDeactivateLDevice(iedName, ldInst, enumValues);
}
}

/**
* checks whether LDevice status is authorized to be activated when CompasLDeviceStatus Private is ACTIVE or UNTESTED
* @param iedName Ied name value which contains LDevice
* @param ldInst LDevice inst value
* @param enumValues enum values
*/
private void checkAuthorisationToActivateLDevice(String iedName, String ldInst, Set<String> enumValues) {
if (!enumValues.contains(ActiveStatus.OFF.getValue()) && enumValues.contains(ActiveStatus.ON.getValue())) {
if (isDeclaredInSubstation(iedName, ldInst)) {
isUpdatable = true;
newVal = ActiveStatus.ON.getValue();
} else {
errorMessage = "The LDevice cannot be set to 'off' but has not been selected into SSD.";
}
}
if (enumValues.contains(ActiveStatus.ON.getValue()) && enumValues.contains(ActiveStatus.OFF.getValue())) {
isUpdatable = true;
if (isDeclaredInSubstation(iedName, ldInst)) {
newVal = ActiveStatus.ON.getValue();
} else {
newVal = ActiveStatus.OFF.getValue();
}
}

}

/**
* checks whether LDevice Status is authorized to be deactivated when CompasLDeviceStatus Private is INACTIVE
* @param iedName Ied name value which contains LDevice
* @param ldInst LDevice inst value
* @param enumValues enum values
*/
private void checkAuthorisationToDeactivateLDevice(String iedName, String ldInst, Set<String> enumValues) {
if (!enumValues.contains(ActiveStatus.OFF.getValue()) && enumValues.contains(ActiveStatus.ON.getValue())) {
if (isDeclaredInSubstation(iedName, ldInst)) {
errorMessage = "The LDevice is not qualified into STD but has been selected into SSD.";
} else {
errorMessage = "The LDevice cannot be set to 'off' but has not been selected into SSD.";
}
}
if (enumValues.contains(ActiveStatus.ON.getValue()) && enumValues.contains(ActiveStatus.OFF.getValue())) {
if (isDeclaredInSubstation(iedName, ldInst)) {
errorMessage = "The LDevice is not qualified into STD but has been selected into SSD.";
} else {
isUpdatable = true;
newVal = ActiveStatus.OFF.getValue();
}
}
}

/**
* checks whether a pair of IED name and LDevice inst are referenced in Substation...LNode list
* @param iedName Ied name value
* @param ldInst LDevice inst value
* @return Returns whether a pair of IED name and LDevice inst are referenced in Substation...LNode list
*/
private boolean isDeclaredInSubstation(String iedName, String ldInst){
return iedNameLdInstList.contains(Pair.of(iedName, ldInst));
}


public record LDeviceActivation(String modStValCurrentValue, boolean isPresentInSubstation, Set<String> enumValues, TAnyLN tln) {
}
Loading

0 comments on commit 3f9604f

Please sign in to comment.