|
4 | 4 |
|
5 | 5 | package org.lfenergy.compas.sct.commons;
|
6 | 6 |
|
| 7 | +import jakarta.xml.bind.JAXBContext; |
| 8 | +import lombok.Getter; |
7 | 9 | import lombok.NonNull;
|
| 10 | +import lombok.RequiredArgsConstructor; |
8 | 11 | import lombok.extern.slf4j.Slf4j;
|
9 | 12 | import org.lfenergy.compas.scl2007b4.model.*;
|
| 13 | +import org.lfenergy.compas.sct.commons.api.DataTypeTemplateReader; |
10 | 14 | import org.lfenergy.compas.sct.commons.api.SclEditor;
|
| 15 | +import org.lfenergy.compas.sct.commons.domain.DaVal; |
| 16 | +import org.lfenergy.compas.sct.commons.domain.DoLinkedToDaFilter; |
11 | 17 | import org.lfenergy.compas.sct.commons.dto.*;
|
12 | 18 | import org.lfenergy.compas.sct.commons.exception.ScdException;
|
13 | 19 | import org.lfenergy.compas.sct.commons.scl.SclRootAdapter;
|
|
23 | 29 | import org.lfenergy.compas.sct.commons.scl.ldevice.LDeviceAdapter;
|
24 | 30 | import org.lfenergy.compas.sct.commons.scl.ln.AbstractLNAdapter;
|
25 | 31 | import org.lfenergy.compas.sct.commons.scl.ln.LN0Adapter;
|
| 32 | +import org.lfenergy.compas.sct.commons.util.MonitoringLnClassEnum; |
26 | 33 | import org.lfenergy.compas.sct.commons.util.PrivateUtils;
|
27 | 34 | import org.lfenergy.compas.sct.commons.util.Utils;
|
28 | 35 |
|
29 | 36 | import java.util.*;
|
| 37 | +import java.util.stream.Collectors; |
30 | 38 |
|
31 |
| -import static org.lfenergy.compas.sct.commons.util.CommonConstants.IED_TEST_NAME; |
| 39 | +import static org.lfenergy.compas.sct.commons.util.CommonConstants.*; |
32 | 40 | import static org.lfenergy.compas.sct.commons.util.PrivateEnum.COMPAS_ICDHEADER;
|
33 | 41 |
|
34 | 42 | @Slf4j
|
| 43 | +@RequiredArgsConstructor |
35 | 44 | public class SclService implements SclEditor {
|
36 | 45 |
|
| 46 | + private static final String DO_GOCBREF = "GoCBRef"; |
| 47 | + private static final String DO_SVCBREF = "SvCBRef"; |
| 48 | + private static final String DA_SETSRCREF = "setSrcRef"; |
| 49 | + |
| 50 | + private final IedService iedService; |
| 51 | + private final LdeviceService ldeviceService; |
| 52 | + private final LnService lnService; |
| 53 | + private final ExtRefReaderService extRefReaderService; |
| 54 | + private final DataTypeTemplateReader dataTypeTemplateService; |
| 55 | + |
| 56 | + @Getter |
| 57 | + private final List<SclReportItem> errorHanlder = new ArrayList<>(); |
| 58 | + |
37 | 59 | @Override
|
38 | 60 | public SCL initScl(final UUID hId, final String hVersion, final String hRevision) throws ScdException {
|
39 | 61 | SclRootAdapter scdAdapter = new SclRootAdapter(hId.toString(), hVersion, hRevision);
|
@@ -196,13 +218,77 @@ public List<SclReportItem> updateDoInRef(SCL scd) {
|
196 | 218 | .toList();
|
197 | 219 | }
|
198 | 220 |
|
| 221 | + |
199 | 222 | @Override
|
200 | 223 | 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) |
206 |
| - .toList(); |
| 224 | + errorHanlder.clear(); |
| 225 | + iedService.getFilteredIeds(scd, ied -> !ied.getName().contains(IED_TEST_NAME)) |
| 226 | + .forEach(tied -> { |
| 227 | + Map<TServiceType, List<TExtRef>> serviceTypeToExtRefs = ldeviceService.getLdevices(tied) |
| 228 | + .flatMap(tlDevice -> extRefReaderService.getExtRefs(tlDevice.getLN0())) |
| 229 | + .filter(tExtRef -> tExtRef.isSetServiceType() && tExtRef.isSetSrcCBName() && (tExtRef.getServiceType().equals(TServiceType.GOOSE) || tExtRef.getServiceType().equals(TServiceType.SMV))) |
| 230 | + .collect(Collectors.groupingBy(TExtRef::getServiceType)); |
| 231 | + ldeviceService.findLdevice(tied, LDEVICE_LDSUIED).ifPresent(ldSUIEDLDevice -> { |
| 232 | + Optional.ofNullable(serviceTypeToExtRefs.get(TServiceType.GOOSE)) |
| 233 | + .ifPresent(extRefs -> manageMonitoringLns(extRefs, scd, tied, ldSUIEDLDevice, DO_GOCBREF, MonitoringLnClassEnum.LGOS)); |
| 234 | + Optional.ofNullable(serviceTypeToExtRefs.get(TServiceType.SMV)) |
| 235 | + .ifPresent(extRefs -> manageMonitoringLns(extRefs, scd, tied, ldSUIEDLDevice, DO_SVCBREF, MonitoringLnClassEnum.LSVS)); |
| 236 | + }); |
| 237 | + }); |
| 238 | + return errorHanlder; |
| 239 | + } |
| 240 | + |
| 241 | + private void manageMonitoringLns(List<TExtRef> tExtRefs, SCL scd, TIED tied, TLDevice ldsuiedLdevice, String doName, MonitoringLnClassEnum monitoringLnClassEnum) { |
| 242 | + List<TLN> lgosOrLsvsLns = lnService.getFilteredLns(ldsuiedLdevice, tln -> monitoringLnClassEnum.value().equals(tln.getLnClass().getFirst())).toList(); |
| 243 | + if (lgosOrLsvsLns.isEmpty()) |
| 244 | + errorHanlder.add(SclReportItem.warning(tied.getName() + "/" + LDEVICE_LDSUIED + "/" + monitoringLnClassEnum.value(), "There is no LN %s present in LDevice".formatted(monitoringLnClassEnum.value()))); |
| 245 | + DoLinkedToDaFilter doLinkedToDaFilter = new DoLinkedToDaFilter(doName, List.of(), DA_SETSRCREF, List.of()); |
| 246 | + lgosOrLsvsLns.forEach(lgosOrLsvs -> dataTypeTemplateService.getFilteredDoLinkedToDa(scd.getDataTypeTemplates(), lgosOrLsvs.getLnType(), doLinkedToDaFilter) |
| 247 | + .map(doLinkedToDa -> lnService.getDoLinkedToDaCompletedFromDAI(tied, LDEVICE_LDSUIED, lgosOrLsvs, doLinkedToDa)) |
| 248 | + .findFirst() |
| 249 | + .filter(doLinkedToDa -> { |
| 250 | + if (!doLinkedToDa.isUpdatable()) |
| 251 | + errorHanlder.add(SclReportItem.warning(tied.getName() + "/" + LDEVICE_LDSUIED + "/" + monitoringLnClassEnum.value() + "/DOI@name=\"" + doName + "\"/DAI@name=\"setSrcRef\"/Val", "The DAI cannot be updated")); |
| 252 | + return doLinkedToDa.isUpdatable(); |
| 253 | + }) |
| 254 | + .ifPresent(doLinkedToDa -> { |
| 255 | + log.info("Processing %d ExtRefs in LDName=%s of service type %s for LN (lnClass=%s, inst=%s, prefix=%s)".formatted(tExtRefs.size(), ldsuiedLdevice.getLdName(), monitoringLnClassEnum.value(), lgosOrLsvs.getLnClass().getFirst(), lgosOrLsvs.getInst(), lgosOrLsvs.getPrefix())); |
| 256 | + for (int i = 0; i < tExtRefs.size(); i++) { |
| 257 | + TLN lnToAdd = copyLn(lgosOrLsvs); //duplicate actual LGOS or LSVS in order to add LDSUIED with extRefs properties |
| 258 | + TExtRef tExtRef = tExtRefs.get(i); |
| 259 | + TIED sourceIed = iedService.findByName(scd, tExtRef.getIedName()).orElseThrow(() -> new ScdException("IED.name '" + tExtRef.getIedName() + "' not found in SCD")); |
| 260 | + String sourceLdName = ldeviceService.findLdevice(sourceIed, tExtRef.getSrcLDInst()).orElseThrow(() -> new ScdException(String.format("LDevice.inst '%s' not found in IED '%s'", tExtRef.getSrcLDInst(), tExtRef.getIedName()))).getLdName(); |
| 261 | + String lnClass = !tExtRef.isSetSrcLNClass() ? TLLN0Enum.LLN_0.value() : tExtRef.getSrcLNClass().getFirst(); |
| 262 | + lnToAdd.setInst(String.valueOf(i + 1)); |
| 263 | + DaVal newVal = new DaVal(null, sourceLdName + "/" + lnClass + "." + tExtRef.getSrcCBName()); |
| 264 | + doLinkedToDa.dataAttribute().getDaiValues().clear(); |
| 265 | + doLinkedToDa.dataAttribute().getDaiValues().add(newVal); |
| 266 | + lnService.updateOrCreateDOAndDAInstances(lnToAdd, doLinkedToDa); |
| 267 | + log.info("Processing %d ExtRefs in LDName=%s - added LN (lnClass=%s, inst=%s, prefix=%s) - update DOI(name=%s)/DAI(name=%s) with value=%s".formatted(tExtRefs.size(), ldsuiedLdevice.getLdName(), lgosOrLsvs.getLnClass().getFirst(), String.valueOf(i + 1), lgosOrLsvs.getPrefix(), doName, DA_SETSRCREF, newVal.val())); |
| 268 | + ldsuiedLdevice.getLN().add(lnToAdd); |
| 269 | + } |
| 270 | + ldsuiedLdevice.getLN().remove(lgosOrLsvs); //We can remove this LGOS or LSVS as we already added new ones |
| 271 | + })); |
207 | 272 | }
|
| 273 | + |
| 274 | + private TLN copyLn(TLN tln) { |
| 275 | + TLN newLn = new TLN(); |
| 276 | + newLn.getLnClass().addAll(tln.getLnClass()); |
| 277 | + newLn.setInst(tln.getInst()); |
| 278 | + newLn.setLnType(tln.getLnType()); |
| 279 | + newLn.setPrefix(tln.getPrefix()); |
| 280 | + newLn.setDesc(tln.getDesc()); |
| 281 | + newLn.setInputs(tln.getInputs()); |
| 282 | + newLn.setText(tln.getText()); |
| 283 | + newLn.getPrivate().addAll(tln.getPrivate()); |
| 284 | + newLn.getDataSet().addAll(tln.getDataSet()); |
| 285 | + newLn.getAny().addAll(tln.getAny()); |
| 286 | + newLn.getDOI().addAll(tln.getDOI()); |
| 287 | + newLn.getLog().addAll(tln.getLog()); |
| 288 | + newLn.getLogControl().addAll(tln.getLogControl()); |
| 289 | + newLn.getOtherAttributes().putAll(tln.getOtherAttributes()); |
| 290 | + newLn.getReportControl().addAll(tln.getReportControl()); |
| 291 | + return newLn; |
| 292 | + } |
| 293 | + |
208 | 294 | }
|
0 commit comments