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 b317458
Show file tree
Hide file tree
Showing 22 changed files with 462 additions and 1,954 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,19 @@
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.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 +149,65 @@ 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> optionalModStVal = getDaiModStValValue(tln);
if (optionalModStVal.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;
}
substationService.getLNodes(scd).stream().filter(tlNode1 -> tlNode1.isSetIedName() && tlNode1.isSetLdInst())
.filter(tlNode -> tlNode.getIedName().equals(tied.getName()) && tlNode.getLdInst().equals(tlDevice.getInst()))
.findFirst()
.ifPresentOrElse(tlNode -> {
// Activate LDevice
if(enumValues.contains(ActiveStatus.ON.getValue())) {
update(tln, optionalModStVal.get().getValue(), ActiveStatus.ON.getValue());
}else {
sclReportItems.add(SclReportItem.error(xpath, "The LDevice cannot be set to 'on' but has been selected into SSD."));
}
},
// Deactivate LDevice
() -> update(tln, optionalModStVal.get().getValue(), ActiveStatus.OFF.getValue()));
})));
return sclReportItems;
}

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

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import org.lfenergy.compas.sct.commons.dto.*;
import org.lfenergy.compas.sct.commons.scl.ObjectReference;
import org.lfenergy.compas.sct.commons.scl.ied.InputsAdapter;
import org.lfenergy.compas.sct.commons.scl.ldevice.LDeviceActivation;
import org.lfenergy.compas.sct.commons.scl.ldevice.LDeviceAdapter;
import org.lfenergy.compas.sct.commons.util.PrivateUtils;

Expand Down Expand Up @@ -176,7 +175,7 @@ public void removeAllControlBlocksAndDatasets() {
* @return Set of Errors
*/
public Optional<SclReportItem> updateLDeviceStatus(List<Pair<String, String>> iedNameLDeviceInstList) {
LDeviceActivation lDeviceActivation = new LDeviceActivation(iedNameLDeviceInstList);
// LDeviceActivation lDeviceActivation = new LDeviceActivation(iedNameLDeviceInstList);
final String iedName = getParentAdapter().getParentAdapter().getName();
final String ldInst = getParentAdapter().getInst();
DataAttributeRef daiBehFilter = new DataAttributeRef(this, BEHAVIOUR_DO_TYPE_NAME, BEHAVIOUR_DA_TYPE_NAME);
Expand All @@ -199,20 +198,21 @@ public Optional<SclReportItem> updateLDeviceStatus(List<Pair<String, String>> ie
}
DataAttributeRef newDaModToSetInLN0 = optionalModStVal.get();
String initialValue = newDaModToSetInLN0.findFirstValue().orElse("");
lDeviceActivation.checkLDeviceActivationStatus(iedName, ldInst, compasLDeviceStatus, enumValues);
if (lDeviceActivation.isUpdatable()) {
if (!initialValue.equals(lDeviceActivation.getNewVal())) {
newDaModToSetInLN0.setVal(lDeviceActivation.getNewVal());
updateDAI(newDaModToSetInLN0);
}
} else {
if (lDeviceActivation.getErrorMessage() != null) {
return Optional.of(buildFatalReportItem(lDeviceActivation.getErrorMessage()));
}
}
// lDeviceActivation.checkLDeviceActivationStatus(iedName, ldInst, compasLDeviceStatus, enumValues);
// if (lDeviceActivation.isUpdatable()) {
// if (!initialValue.equals(lDeviceActivation.getNewVal())) {
// newDaModToSetInLN0.setVal(lDeviceActivation.getNewVal());
// updateDAI(newDaModToSetInLN0);
// }
// } else {
// if (lDeviceActivation.getErrorMessage() != null) {
// return Optional.of(buildFatalReportItem(lDeviceActivation.getErrorMessage()));
// }
// }
return Optional.empty();
}


private static DaTypeName getDaTypeNameForBeh() {
DaTypeName daTypeNameBeh = new DaTypeName();
daTypeNameBeh.setName(STVAL_DA_NAME);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,13 @@

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

import org.apache.commons.lang3.tuple.Pair;
import org.lfenergy.compas.scl2007b4.model.TLLN0Enum;
import org.lfenergy.compas.scl2007b4.model.TSubstation;
import org.lfenergy.compas.sct.commons.exception.ScdException;
import org.lfenergy.compas.sct.commons.scl.SclElementAdapter;
import org.lfenergy.compas.sct.commons.scl.SclRootAdapter;
import org.lfenergy.compas.sct.commons.util.Utils;

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

/**
Expand Down Expand Up @@ -119,18 +115,4 @@ public Stream<VoltageLevelAdapter> streamVoltageLevelAdapters() {
return currentElem.getVoltageLevel().stream().map(tVoltageLevel -> new VoltageLevelAdapter(this, tVoltageLevel));
}

/**
* 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<Pair<String, String>> getIedAndLDeviceNamesForLN0FromLNode() {
return streamVoltageLevelAdapters()
.flatMap(VoltageLevelAdapter::streamBayAdapters)
.flatMap(BayAdapter::streamFunctionAdapters)
.flatMap(functionAdapter -> functionAdapter.getCurrentElem().getLNode().stream())
.filter(tlNode -> tlNode.getLnClass().contains(TLLN0Enum.LLN_0.value()))
.map(tlNode -> Pair.of(tlNode.getIedName(), tlNode.getLdInst()))
.collect(Collectors.toList());
}

}
Loading

0 comments on commit b317458

Please sign in to comment.