|
| 1 | +// SPDX-FileCopyrightText: 2023 RTE FRANCE |
| 2 | +// |
| 3 | +// SPDX-License-Identifier: Apache-2.0 |
| 4 | + |
| 5 | +package org.lfenergy.compas.sct.commons.dto; |
| 6 | + |
| 7 | +import java.math.BigInteger; |
| 8 | +import java.util.List; |
| 9 | +import java.util.Optional; |
| 10 | +import java.util.function.*; |
| 11 | + |
| 12 | +import lombok.Builder; |
| 13 | +import org.apache.commons.lang3.StringUtils; |
| 14 | +import org.lfenergy.compas.scl2007b4.model.*; |
| 15 | +import org.lfenergy.compas.sct.commons.exception.ScdException; |
| 16 | +import org.lfenergy.compas.sct.commons.scl.SclRootAdapter; |
| 17 | +import org.lfenergy.compas.sct.commons.scl.ied.AbstractLNAdapter; |
| 18 | +import org.lfenergy.compas.sct.commons.scl.ied.IEDAdapter; |
| 19 | +import org.lfenergy.compas.sct.commons.scl.ied.LDeviceAdapter; |
| 20 | +import org.lfenergy.compas.sct.commons.util.LDeviceStatus; |
| 21 | +import org.lfenergy.compas.sct.commons.util.Utils; |
| 22 | + |
| 23 | + |
| 24 | +/** |
| 25 | + * Represents a list of LDEPF settings |
| 26 | + * |
| 27 | + * This is a functional interface whose functional method is @getSettings |
| 28 | + */ |
| 29 | +@FunctionalInterface |
| 30 | +public interface LDEPFSettingsSupplier { |
| 31 | + |
| 32 | + /** |
| 33 | + * This method provides list of LDEPF settings |
| 34 | + */ |
| 35 | + List<LDEPFSetting> getSettings(); |
| 36 | + |
| 37 | + @Builder |
| 38 | + record LDEPFSetting(String rteIedType, String iedRedundancy, BigInteger iedInstance, |
| 39 | + String channelShortLabel, String channelMREP, String channelLevModQ, |
| 40 | + String channelLevModLevMod, String bapVariant, String bapIgnoredValue, |
| 41 | + String ldInst, String lnPrefix, String lnClass, String lnInst, |
| 42 | + String doName, String doInst, String sdoName, |
| 43 | + String daName, String daType, |
| 44 | + String dabType, String bdaName, String sbdaName, |
| 45 | + String channelAnalogNum, String channelDigitalNum, String opt) { |
| 46 | + } |
| 47 | + |
| 48 | + BiPredicate<LDEPFSetting, TExtRef> isAnalogMatchDesc = (setting, extRef) -> |
| 49 | + setting.channelAnalogNum() != null && setting.channelDigitalNum() == null |
| 50 | + && extRef.getDesc().startsWith("DYN_LDEPF_ANALOG CHANNEL " + setting.channelAnalogNum()+"_1_AnalogueValue") |
| 51 | + && extRef.getDesc().endsWith("_" + setting.daName() + "_1"); |
| 52 | + BiPredicate<LDEPFSetting, TExtRef> isDigitalMatchDesc = (setting, extRef) -> |
| 53 | + setting.channelDigitalNum() != null && setting.channelAnalogNum() == null |
| 54 | + && extRef.getDesc().startsWith("DYN_LDEPF_DIGITAL CHANNEL " + setting.channelDigitalNum()+"_1_BOOLEEN") |
| 55 | + && extRef.getDesc().endsWith("_" + setting.daName() + "_1"); |
| 56 | + |
| 57 | + |
| 58 | + BiPredicate<IEDAdapter, LDEPFSetting> isIcdHeaderMatch = (iedAdapter, setting) -> iedAdapter.getCompasICDHeader() |
| 59 | + .map(compasICDHeader -> compasICDHeader.getIEDType().value().equals(setting.rteIedType()) |
| 60 | + && compasICDHeader.getIEDredundancy().value().equals(setting.iedRedundancy()) |
| 61 | + && compasICDHeader.getIEDSystemVersioninstance().equals(setting.iedInstance())) |
| 62 | + .orElse(false); |
| 63 | + |
| 64 | + BiFunction<IEDAdapter, LDEPFSetting, Optional<LDeviceAdapter>> getActiveSourceLDevice = (iedAdapter, setting) -> |
| 65 | + iedAdapter.findLDeviceAdapterByLdInst(setting.ldInst()) |
| 66 | + .filter(lDeviceAdapter -> lDeviceAdapter.getLDeviceStatus() |
| 67 | + .map(status -> status.equals(LDeviceStatus.ON)) |
| 68 | + .orElse(false)) |
| 69 | + .stream().findFirst(); |
| 70 | + |
| 71 | + BiFunction<LDeviceAdapter, LDEPFSetting, Optional<AbstractLNAdapter<?>>> getActiveLNodeSource = (lDeviceAdapter, setting) -> |
| 72 | + lDeviceAdapter.getLNAdaptersIncludingLN0().stream() |
| 73 | + .filter(lnAdapter -> lnAdapter.getLNClass().equals(setting.lnClass()) |
| 74 | + && lnAdapter.getLNInst().equals(setting.lnInst()) |
| 75 | + && Utils.equalsOrBothBlank(setting.lnPrefix(), lnAdapter.getPrefix())) |
| 76 | + .findFirst() |
| 77 | + .filter(lnAdapter -> lnAdapter.getDaiModStValValue() |
| 78 | + .map(status -> status.equals(LDeviceStatus.ON)) |
| 79 | + .orElse(true)); |
| 80 | + |
| 81 | + BiPredicate<AbstractLNAdapter<?>, LDEPFSetting> isValidDataTypeTemplate = (lnAdapter, setting) -> { |
| 82 | + String doName = setting.doInst() == null || setting.doInst().equals("0") ? setting.doName() : setting.doName()+setting.doInst(); |
| 83 | + var lnType = lnAdapter.getLnType(); |
| 84 | + return StringUtils.isEmpty(doName) || StringUtils.isEmpty(setting.daName()) || |
| 85 | + lnAdapter.getDataTypeTemplateAdapter().getLNodeTypeAdapterById(lnType) |
| 86 | + .filter(lNodeTypeAdapter -> { |
| 87 | + try { |
| 88 | + lNodeTypeAdapter.check(new DoTypeName(doName), new DaTypeName(setting.daName())); |
| 89 | + } catch (ScdException ex) { |
| 90 | + return false; |
| 91 | + } |
| 92 | + return true; |
| 93 | + }).isPresent(); |
| 94 | + }; |
| 95 | + |
| 96 | + /** |
| 97 | + * Provides the matching setting for an ExtRef. |
| 98 | + * @param extRef The ExtRef object |
| 99 | + * @return the matching LDEPFSetting for an ExtRef |
| 100 | + */ |
| 101 | + default Optional<LDEPFSetting> getLDEPFSettingMatchingExtRef(TExtRef extRef) { |
| 102 | + if(!extRef.isSetDesc()) return Optional.empty(); |
| 103 | + return getSettings().stream() |
| 104 | + .filter(setting -> isAnalogMatchDesc.or(isDigitalMatchDesc).test(setting, extRef)) |
| 105 | + .filter(setting -> extRef.isSetPLN() && Utils.lnClassEquals(extRef.getPLN(), setting.lnClass()) |
| 106 | + && extRef.isSetPDO() && extRef.getPDO().equals(setting.doName())) |
| 107 | + .findFirst(); |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * Provides the IED sources that belong to the LDEPF setting <br/> |
| 112 | + * Example of this setting include: |
| 113 | + * 1. COMPAS-Bay and COMPAS-ICDHeader verification.<br/> |
| 114 | + * 2. Active LDevice source object with the same DO/DA hierarchy as the LDEPF setting. |
| 115 | + * @param sclRootAdapter SCL |
| 116 | + * @param compasBay TCompasBay |
| 117 | + * @param setting LDEPFSetting |
| 118 | + * @return the IED sources matching the LDEPFSetting |
| 119 | + */ |
| 120 | + default List<TIED> getIedSources(SclRootAdapter sclRootAdapter, TCompasBay compasBay, LDEPFSetting setting) { |
| 121 | + return sclRootAdapter.streamIEDAdapters() |
| 122 | + .filter(iedAdapter -> iedAdapter.getPrivateCompasBay().filter(bay -> bay.getUUID().equals(compasBay.getUUID())).isPresent()) |
| 123 | + .filter(iedAdapter -> isIcdHeaderMatch.test(iedAdapter, setting)) |
| 124 | + .filter(iedAdapter -> getActiveSourceLDevice.apply(iedAdapter, setting) |
| 125 | + .filter(lDeviceAdapter -> getActiveLNodeSource.apply(lDeviceAdapter, setting) |
| 126 | + .filter(lnAdapter -> isValidDataTypeTemplate.test(lnAdapter, setting)).isPresent()) |
| 127 | + .isPresent()) |
| 128 | + .map(IEDAdapter::getCurrentElem).limit(2).toList(); |
| 129 | + } |
| 130 | + |
| 131 | +} |
0 commit comments