Skip to content

Commit b677539

Browse files
committed
feat: Refacto manageMonitoringLns
Signed-off-by: gleizesDor <[email protected]>
1 parent 0062f76 commit b677539

File tree

2 files changed

+78
-8
lines changed

2 files changed

+78
-8
lines changed

sct-commons/src/main/java/org/lfenergy/compas/sct/commons/SclService.java

Lines changed: 70 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,15 @@
44

55
package org.lfenergy.compas.sct.commons;
66

7+
import lombok.Getter;
78
import lombok.NonNull;
9+
import lombok.RequiredArgsConstructor;
810
import lombok.extern.slf4j.Slf4j;
911
import org.lfenergy.compas.scl2007b4.model.*;
1012
import org.lfenergy.compas.sct.commons.api.SclEditor;
1113
import org.lfenergy.compas.sct.commons.dto.*;
1214
import org.lfenergy.compas.sct.commons.exception.ScdException;
15+
import org.lfenergy.compas.sct.commons.scl.ExtRefService;
1316
import org.lfenergy.compas.sct.commons.scl.SclRootAdapter;
1417
import org.lfenergy.compas.sct.commons.scl.com.CommunicationAdapter;
1518
import org.lfenergy.compas.sct.commons.scl.com.ConnectedAPAdapter;
@@ -23,17 +26,31 @@
2326
import org.lfenergy.compas.sct.commons.scl.ldevice.LDeviceAdapter;
2427
import org.lfenergy.compas.sct.commons.scl.ln.AbstractLNAdapter;
2528
import org.lfenergy.compas.sct.commons.scl.ln.LN0Adapter;
29+
import org.lfenergy.compas.sct.commons.scl.ln.LNAdapter;
30+
import org.lfenergy.compas.sct.commons.util.MonitoringLnClassEnum;
2631
import org.lfenergy.compas.sct.commons.util.PrivateUtils;
2732
import org.lfenergy.compas.sct.commons.util.Utils;
2833

2934
import java.util.*;
3035

3136
import static org.lfenergy.compas.sct.commons.util.CommonConstants.IED_TEST_NAME;
3237
import static org.lfenergy.compas.sct.commons.util.PrivateEnum.COMPAS_ICDHEADER;
38+
import static org.lfenergy.compas.sct.commons.util.Utils.copySclElement;
3339

3440
@Slf4j
41+
@RequiredArgsConstructor
3542
public class SclService implements SclEditor {
3643

44+
private static final String DO_GOCBREF = "GoCBRef";
45+
private static final String DO_SVCBREF = "SvCBRef";
46+
private static final String DA_SETSRCREF = "setSrcRef";
47+
48+
private final IedService iedService;
49+
private final LdeviceService ldeviceService;
50+
private final LnService lnService;
51+
@Getter
52+
private final List<SclReportItem> errorHanlder = new ArrayList<>();
53+
3754
@Override
3855
public SCL initScl(final UUID hId, final String hVersion, final String hRevision) throws ScdException {
3956
SclRootAdapter scdAdapter = new SclRootAdapter(hId.toString(), hVersion, hRevision);
@@ -198,11 +215,59 @@ public List<SclReportItem> updateDoInRef(SCL scd) {
198215

199216
@Override
200217
public List<SclReportItem> manageMonitoringLns(SCL scd) {
201-
SclRootAdapter sclRootAdapter = new SclRootAdapter(scd);
202-
return sclRootAdapter.streamIEDAdapters()
203-
.filter(iedAdapter -> !iedAdapter.getName().contains(IED_TEST_NAME))
204-
.map(IEDAdapter::manageMonitoringLns)
205-
.flatMap(List::stream)
218+
errorHanlder.clear();
219+
iedService.getFilteredIeds(scd, ied -> !ied.getName().contains(IED_TEST_NAME))
220+
.forEach(tied -> ldeviceService.findLdevice(tied, tlDevice -> "LDSUIED".equals(tlDevice.getInst()))
221+
.filter(tlDevice -> tlDevice.getLN0().isSetInputs())
222+
.ifPresent(tlDevice -> {
223+
List<TExtRef> tExtRefs = new ExtRefService().filterDuplicatedExtRefs(tlDevice.getLN0().getInputs().getExtRef())
224+
.stream()
225+
.filter(TExtRef::isSetServiceType)
226+
.filter(TExtRef::isSetSrcCBName)
227+
.toList();
228+
manageMonitoringLns(tExtRefs.stream().filter(tExtRef -> TServiceType.GOOSE.equals(tExtRef.getServiceType())).toList(), scd, tied, tlDevice, DO_GOCBREF, MonitoringLnClassEnum.LGOS);
229+
manageMonitoringLns(tExtRefs.stream().filter(tExtRef -> TServiceType.SMV.equals(tExtRef.getServiceType())).toList(), scd, tied, tlDevice, DO_SVCBREF, MonitoringLnClassEnum.LSVS);
230+
}));
231+
return errorHanlder;
232+
}
233+
234+
private void manageMonitoringLns(List<TExtRef> tExtRefs, SCL scd, TIED tied, TLDevice tlDevice, String doName, MonitoringLnClassEnum monitoringLnClassEnum) {
235+
List<TLN> tlns = lnService.getFilteredLns(tlDevice, tln -> monitoringLnClassEnum.value().equals(tln.getLnClass().getFirst())).toList();
236+
if (tlns.isEmpty())
237+
errorHanlder.add(SclReportItem.warning(tied.getName()+"/"+tlDevice.getInst()+"/"+monitoringLnClassEnum.value(), "There is no LN %s present in LDevice".formatted(monitoringLnClassEnum.value())));
238+
239+
tlns.forEach(tln -> {
240+
LNAdapter lnAdapter = new LNAdapter(new LDeviceAdapter(new IEDAdapter(new SclRootAdapter(scd), tied), tlDevice), tln);
241+
lnAdapter
242+
.getDAI(new DataAttributeRef(tln, new DoTypeName(doName), new DaTypeName(DA_SETSRCREF)), true)
243+
.stream()
244+
.findFirst()
245+
.ifPresentOrElse(daToUpdateFilter -> {
246+
removeLnsByLnClass(monitoringLnClassEnum, tlDevice);
247+
for (int i = 0; i < tExtRefs.size(); i++) {
248+
TLN copiedLn = copySclElement(tln, TLN.class);
249+
TExtRef tExtRef = tExtRefs.get(i);
250+
TIED sourceIed = iedService.findByName(scd, tExtRef.getIedName())
251+
.orElseThrow(() -> new ScdException("IED.name '" + tExtRef.getIedName() + "' not found in SCD"));
252+
String sourceLdName = ldeviceService.findLdevice(sourceIed, tExtRef.getSrcLDInst())
253+
.orElseThrow(() -> new ScdException(String.format("LDevice.inst '%s' not found in IED '%s'", tExtRef.getSrcLDInst(), tExtRef.getIedName())))
254+
.getLdName();
255+
String lnClass = !tExtRef.isSetSrcLNClass() ? TLLN0Enum.LLN_0.value() : tExtRef.getSrcLNClass().getFirst();
256+
lnAdapter.getCurrentElem().setInst(String.valueOf(i + 1));
257+
daToUpdateFilter.setVal(sourceLdName + "/" + lnClass + "." + tExtRef.getSrcCBName());
258+
lnAdapter.updateDAI(daToUpdateFilter);
259+
tlDevice.getLN().add(copiedLn);//value copy
260+
}
261+
}, () -> errorHanlder.add(SclReportItem.warning(lnAdapter.getXPath() + "/DOI@name=\"" + doName + "\"/DAI@name=\"setSrcRef\"/Val",
262+
"The DAI cannot be updated")));
263+
});
264+
}
265+
266+
private void removeLnsByLnClass(MonitoringLnClassEnum monitoringLnClassEnum, TLDevice tlDevice) {
267+
List<TLN> lnToKeep = tlDevice.getLN().stream()
268+
.filter(tln -> !Utils.lnClassEquals(tln.getLnClass(), monitoringLnClassEnum.value()))
206269
.toList();
270+
tlDevice.unsetLN();
271+
tlDevice.getLN().addAll(lnToKeep);
207272
}
208273
}

sct-commons/src/main/java/org/lfenergy/compas/sct/commons/dto/DataAttributeRef.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,14 @@ public class DataAttributeRef {
5151
@NonNull
5252
private DaTypeName daName = new DaTypeName("");
5353

54-
/**
55-
* Constructor
56-
*/
54+
public DataAttributeRef(TLN tln, DoTypeName doName, DaTypeName daName) {
55+
this.prefix = tln.getPrefix();
56+
this.lnClass = tln.getLnClass().getFirst();
57+
this.lnInst = tln.getInst();
58+
this.lnType = tln.getLnType();
59+
this.doName = doName;
60+
this.daName = daName;
61+
}
5762

5863
public DataAttributeRef(AbstractLNAdapter<?> lnAdapter, DoTypeName doName, DaTypeName daName) {
5964
this.lnClass = lnAdapter.getLNClass();

0 commit comments

Comments
 (0)