|
4 | 4 |
|
5 | 5 | package org.lfenergy.compas.sct.commons;
|
6 | 6 |
|
| 7 | +import lombok.Getter; |
7 | 8 | import lombok.NonNull;
|
| 9 | +import lombok.RequiredArgsConstructor; |
8 | 10 | import lombok.extern.slf4j.Slf4j;
|
9 | 11 | import org.lfenergy.compas.scl2007b4.model.*;
|
10 | 12 | import org.lfenergy.compas.sct.commons.api.SclEditor;
|
11 | 13 | import org.lfenergy.compas.sct.commons.dto.*;
|
12 | 14 | import org.lfenergy.compas.sct.commons.exception.ScdException;
|
| 15 | +import org.lfenergy.compas.sct.commons.scl.ExtRefService; |
13 | 16 | import org.lfenergy.compas.sct.commons.scl.SclRootAdapter;
|
14 | 17 | import org.lfenergy.compas.sct.commons.scl.com.CommunicationAdapter;
|
15 | 18 | import org.lfenergy.compas.sct.commons.scl.com.ConnectedAPAdapter;
|
|
23 | 26 | import org.lfenergy.compas.sct.commons.scl.ldevice.LDeviceAdapter;
|
24 | 27 | import org.lfenergy.compas.sct.commons.scl.ln.AbstractLNAdapter;
|
25 | 28 | 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; |
26 | 31 | import org.lfenergy.compas.sct.commons.util.PrivateUtils;
|
27 | 32 | import org.lfenergy.compas.sct.commons.util.Utils;
|
28 | 33 |
|
29 | 34 | import java.util.*;
|
30 | 35 |
|
31 | 36 | import static org.lfenergy.compas.sct.commons.util.CommonConstants.IED_TEST_NAME;
|
32 | 37 | import static org.lfenergy.compas.sct.commons.util.PrivateEnum.COMPAS_ICDHEADER;
|
| 38 | +import static org.lfenergy.compas.sct.commons.util.Utils.copySclElement; |
33 | 39 |
|
34 | 40 | @Slf4j
|
| 41 | +@RequiredArgsConstructor |
35 | 42 | public class SclService implements SclEditor {
|
36 | 43 |
|
| 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 | + |
37 | 54 | @Override
|
38 | 55 | public SCL initScl(final UUID hId, final String hVersion, final String hRevision) throws ScdException {
|
39 | 56 | SclRootAdapter scdAdapter = new SclRootAdapter(hId.toString(), hVersion, hRevision);
|
@@ -198,11 +215,59 @@ public List<SclReportItem> updateDoInRef(SCL scd) {
|
198 | 215 |
|
199 | 216 | @Override
|
200 | 217 | 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())) |
206 | 269 | .toList();
|
| 270 | + tlDevice.unsetLN(); |
| 271 | + tlDevice.getLN().addAll(lnToKeep); |
207 | 272 | }
|
208 | 273 | }
|
0 commit comments