44
55package org .lfenergy .compas .sct .commons ;
66
7+ import lombok .Getter ;
78import lombok .RequiredArgsConstructor ;
89import org .lfenergy .compas .scl2007b4 .model .*;
910import org .lfenergy .compas .sct .commons .api .ExtRefEditor ;
1516import org .lfenergy .compas .sct .commons .model .epf .TCBScopeType ;
1617import org .lfenergy .compas .sct .commons .model .epf .TChannel ;
1718import org .lfenergy .compas .sct .commons .model .epf .TChannelType ;
19+ import org .lfenergy .compas .sct .commons .model .epf .TChannelLevMod ;
1820import org .lfenergy .compas .sct .commons .scl .SclRootAdapter ;
1921import org .lfenergy .compas .sct .commons .scl .ied .IEDAdapter ;
2022import org .lfenergy .compas .sct .commons .scl .ldevice .LDeviceAdapter ;
3436@ RequiredArgsConstructor
3537public class ExtRefEditorService implements ExtRefEditor {
3638 private static final String INVALID_OR_MISSING_ATTRIBUTES_IN_EXT_REF_BINDING_INFO = "Invalid or missing attributes in ExtRef binding info" ;
39+ private static final List <DoNameAndDaName > DO_DA_MAPPINGS = List .of (
40+ new DoNameAndDaName (CHNUM1_DO_NAME , DU_DA_NAME ),
41+ new DoNameAndDaName (LEVMOD_DO_NAME , SETVAL_DA_NAME ),
42+ new DoNameAndDaName (MOD_DO_NAME , STVAL_DA_NAME ),
43+ new DoNameAndDaName (SRCREF_DO_NAME , SETSRCREF_DA_NAME )
44+ );
3745
3846 private final IedService iedService ;
3947 private final LdeviceService ldeviceService ;
4048 private final LnEditor lnEditor ;
4149 private final DataTypeTemplatesService dataTypeTemplatesService ;
4250
51+ @ Getter
52+ private final List <SclReportItem > errorHandler = new ArrayList <>();
53+
4354 /**
4455 * Provides valid IED sources according to EPF configuration.<br/>
4556 * EPF verification include:<br/>
@@ -76,17 +87,16 @@ private List<TIED> getIedSources(SclRootAdapter sclRootAdapter, TCompasBay compa
7687 * - The location of ExtRef should be in LDevice (inst=LDEPF) <br/>
7788 * - ExtRef that lacks Bay or ICDHeader Private is not returned <br/>
7889 *
79- * @param sclReportItems List of SclReportItem
8090 * @return list of ExtRef and associated Bay
8191 */
82- private List <ExtRefInfo .ExtRefWithBayReference > getExtRefWithBayReferenceInLDEPF (TDataTypeTemplates dataTypeTemplates , TIED tied , final TLDevice tlDevice , final List < SclReportItem > sclReportItems ) {
92+ private List <ExtRefInfo .ExtRefWithBayReference > getExtRefWithBayReferenceInLDEPF (TDataTypeTemplates dataTypeTemplates , TIED tied , final TLDevice tlDevice ) {
8393 List <ExtRefInfo .ExtRefWithBayReference > extRefBayReferenceList = new ArrayList <>();
8494 String lDevicePath = "SCL/IED[@name=\" " + tied .getName () + "\" ]/AccessPoint/Server/LDevice[@inst=\" " + tlDevice .getInst () + "\" ]" ;
8595 Optional <TCompasBay > tCompasBay = PrivateUtils .extractCompasPrivate (tied , TCompasBay .class );
8696 if (tCompasBay .isEmpty ()) {
87- sclReportItems .add (SclReportItem .error (lDevicePath , "The IED has no Private Bay" ));
97+ errorHandler .add (SclReportItem .error (lDevicePath , "The IED has no Private Bay" ));
8898 if (PrivateUtils .extractCompasPrivate (tied , TCompasICDHeader .class ).isEmpty ()) {
89- sclReportItems .add (SclReportItem .error (lDevicePath , "The IED has no Private compas:ICDHeader" ));
99+ errorHandler .add (SclReportItem .error (lDevicePath , "The IED has no Private compas:ICDHeader" ));
90100 }
91101 return Collections .emptyList ();
92102 }
@@ -97,7 +107,7 @@ private List<ExtRefInfo.ExtRefWithBayReference> getExtRefWithBayReferenceInLDEPF
97107 .getExtRef ().stream ()
98108 .map (extRef -> new ExtRefInfo .ExtRefWithBayReference (tied .getName (), tCompasBay .get (), extRef )).toList ());
99109 } else {
100- sclReportItems .add (SclReportItem .error (lDevicePath , "DO@name=Mod/DA@name=stVal not found in DataTypeTemplate" ));
110+ errorHandler .add (SclReportItem .error (lDevicePath , "DO@name=Mod/DA@name=stVal not found in DataTypeTemplate" ));
101111 }
102112 return extRefBayReferenceList ;
103113 }
@@ -114,7 +124,7 @@ private static Boolean doesExtRefMatchLDEPFChannel(TExtRef extRef, TChannel tCha
114124 && extRef .getDesc ().startsWith ("DYN_LDEPF_ANALOG CHANNEL " + tChannel .getChannelNum () + "_1_AnalogueValue" )
115125 && extRef .getDesc ().endsWith ("_" + tChannel .getDAName () + "_1" );
116126 Boolean doesExtRefDescMatchDigitalChannel = tChannel .getChannelType ().equals (TChannelType .DIGITAL )
117- && extRef .getDesc ().startsWith ("DYN_LDEPF_DIGITAL CHANNEL " + tChannel .getChannelNum () + "_1_BOOLEEN " )
127+ && extRef .getDesc ().startsWith ("DYN_LDEPF_DIGITAL CHANNEL " + tChannel .getChannelNum () + "_1_BOOLEAN " )
118128 && extRef .getDesc ().endsWith ("_" + tChannel .getDAName () + "_1" );
119129 return extRef .isSetDesc () && (doesExtRefDescMatchAnalogChannel || doesExtRefDescMatchDigitalChannel )
120130 && extRef .isSetPLN () && Utils .lnClassEquals (extRef .getPLN (), tChannel .getLNClass ())
@@ -263,29 +273,29 @@ public TExtRef updateExtRefSource(SCL scd, ExtRefInfo extRefInfo) throws ScdExce
263273
264274 @ Override
265275 public List <SclReportItem > manageBindingForLDEPF (SCL scd , EPF epf ) {
266- List < SclReportItem > sclReportItems = new ArrayList <> ();
276+ errorHandler . clear ();
267277 SclRootAdapter sclRootAdapter = new SclRootAdapter (scd );
268- if (!epf .isSetChannels ()) return sclReportItems ;
278+ if (!epf .isSetChannels ()) return errorHandler ;
269279 iedService .getFilteredIeds (scd , ied -> !ied .getName ().contains ("TEST" ))
270280 .forEach (tied -> ldeviceService .findLdevice (tied , LDEVICE_LDEPF )
271- .ifPresent (tlDevice -> getExtRefWithBayReferenceInLDEPF (scd .getDataTypeTemplates (), tied , tlDevice , sclReportItems )
281+ .ifPresent (tlDevice -> getExtRefWithBayReferenceInLDEPF (scd .getDataTypeTemplates (), tied , tlDevice )
272282 .forEach (extRefBayRef -> epf .getChannels ().getChannel ().stream ().filter (tChannel -> doesExtRefMatchLDEPFChannel (extRefBayRef .extRef (), tChannel ))
273283 .findFirst ().ifPresent (channel -> {
274284 List <TIED > iedSources = getIedSources (sclRootAdapter , extRefBayRef .compasBay (), channel );
275285 if (iedSources .size () == 1 ) {
276286 updateLDEPFExtRefBinding (extRefBayRef .extRef (), iedSources .getFirst (), channel );
277287 LDeviceAdapter lDeviceAdapter = new LDeviceAdapter (new IEDAdapter (sclRootAdapter , tied .getName ()), tlDevice );
278- sclReportItems . addAll ( updateLDEPFDos (lDeviceAdapter , extRefBayRef .extRef (), channel ) );
288+ updateLDEPFDos (lDeviceAdapter , extRefBayRef .extRef (), channel );
279289 } else {
280290 if (iedSources .size () > 1 ) {
281- sclReportItems .add (SclReportItem .warning (null , "There is more than one IED source to bind the signal " +
291+ errorHandler .add (SclReportItem .warning (null , "There is more than one IED source to bind the signal " +
282292 "/IED@name=" + extRefBayRef .iedName () + "/LDevice@inst=LDEPF/LN0" +
283293 "/ExtRef@desc=" + extRefBayRef .extRef ().getDesc ()));
284294 }
285295 // If the source IED is not found, there will be no update or report message.
286296 }
287297 }))));
288- return sclReportItems ;
298+ return errorHandler ;
289299 }
290300
291301 @ Override
@@ -332,44 +342,44 @@ private void updateLDEPFExtRefBinding(TExtRef extRef, TIED iedSource, TChannel s
332342 extRef .setDoName (doName );
333343 }
334344
335- private List <SclReportItem > updateLDEPFDos (LDeviceAdapter lDeviceAdapter , TExtRef extRef , TChannel setting ) {
336- List <SclReportItem > sclReportItems = new ArrayList <>();
337- List <DoNameAndDaName > doNameAndDaNameList = List .of (
338- new DoNameAndDaName (CHNUM1_DO_NAME , DU_DA_NAME ),
339- new DoNameAndDaName (LEVMOD_DO_NAME , SETVAL_DA_NAME ),
340- new DoNameAndDaName (MOD_DO_NAME , STVAL_DA_NAME ),
341- new DoNameAndDaName (SRCREF_DO_NAME , SETSRCREF_DA_NAME )
342- );
345+ private void updateLDEPFDos (LDeviceAdapter lDeviceAdapter , TExtRef extRef , TChannel setting ) {
343346 if (setting .getChannelType ().equals (TChannelType .DIGITAL )) {
344347 //digital
345348 lDeviceAdapter .findLnAdapter (LN_RBDR , setting .getChannelNum (), null )
346- .ifPresent (lnAdapter -> doNameAndDaNameList .forEach (doNameAndDaName -> updateVal (lnAdapter , doNameAndDaName . doName , doNameAndDaName . daName , extRef , setting ). ifPresent ( sclReportItems :: add )));
349+ .ifPresent (lnAdapter -> DO_DA_MAPPINGS .forEach (doNameAndDaName -> updateVal (lnAdapter , doNameAndDaName , extRef , setting )));
347350 lDeviceAdapter .findLnAdapter (LN_RBDR , setting .getChannelNum (), LN_PREFIX_B )
348- .ifPresent (lnAdapter -> doNameAndDaNameList .forEach (doNameAndDaName -> updateVal (lnAdapter , doNameAndDaName . doName , doNameAndDaName . daName , extRef , setting ). ifPresent ( sclReportItems :: add )));
351+ .ifPresent (lnAdapter -> DO_DA_MAPPINGS .forEach (doNameAndDaName -> updateVal (lnAdapter , doNameAndDaName , extRef , setting )));
349352 }
350353 if (setting .getChannelType ().equals (TChannelType .ANALOG )) {
351354 //analog
352355 lDeviceAdapter .findLnAdapter (LN_RADR , setting .getChannelNum (), null )
353- .ifPresent (lnAdapter -> doNameAndDaNameList .forEach (doNameAndDaName -> updateVal (lnAdapter , doNameAndDaName . doName , doNameAndDaName . daName , extRef , setting ). ifPresent ( sclReportItems :: add )));
356+ .ifPresent (lnAdapter -> DO_DA_MAPPINGS .forEach (doNameAndDaName -> updateVal (lnAdapter , doNameAndDaName , extRef , setting )));
354357 lDeviceAdapter .findLnAdapter (LN_RADR , setting .getChannelNum (), LN_PREFIX_A )
355- .ifPresent (lnAdapter -> doNameAndDaNameList .forEach (doNameAndDaName -> updateVal (lnAdapter , doNameAndDaName . doName , doNameAndDaName . daName , extRef , setting ). ifPresent ( sclReportItems :: add )));
358+ .ifPresent (lnAdapter -> DO_DA_MAPPINGS .forEach (doNameAndDaName -> updateVal (lnAdapter , doNameAndDaName , extRef , setting )));
356359 }
357- return sclReportItems ;
358360 }
359361
360- private Optional <SclReportItem > updateVal (AbstractLNAdapter <?> lnAdapter , String doName , String daName , TExtRef extRef , TChannel setting ) {
361- String value = switch (daName ) {
362- case DU_DA_NAME -> setting .getChannelShortLabel ();
363- case SETVAL_DA_NAME -> LN_PREFIX_B .equals (lnAdapter .getPrefix ()) || LN_PREFIX_A .equals (lnAdapter .getPrefix ()) ? setting .getChannelLevModQ ().value () : setting .getChannelLevMod ().value ();
364- case STVAL_DA_NAME -> ActiveStatus .ON .getValue ();
365- case SETSRCREF_DA_NAME -> computeDaiValue (lnAdapter , extRef , setting .getDAName ());
366- default -> null ;
362+ private void updateVal (AbstractLNAdapter <?> lnAdapter , DoNameAndDaName doDaName , TExtRef extRef , TChannel setting ) {
363+ String lnPrefix = lnAdapter .getPrefix ();
364+ Optional <SclReportItem > sclReportItem = switch (doDaName .daName ) {
365+ case DU_DA_NAME -> setting .isSetChannelShortLabel () ? lnAdapter .getDOIAdapterByName (doDaName .doName ).updateDAI (doDaName .daName , setting .getChannelShortLabel ()) :
366+ Optional .empty ();
367+ case SETVAL_DA_NAME -> {
368+ if (LN_PREFIX_B .equals (lnPrefix ) || LN_PREFIX_A .equals (lnPrefix )) {
369+ yield setting .isSetChannelLevModQ () && !setting .getChannelLevModQ ().equals (TChannelLevMod .NA ) ? lnAdapter .getDOIAdapterByName (doDaName .doName ).updateDAI (doDaName .daName , setting .getChannelLevModQ ().value ()) : Optional .empty ();
370+ } else {
371+ yield setting .isSetChannelLevMod () && !setting .getChannelLevMod ().equals (TChannelLevMod .NA ) ? lnAdapter .getDOIAdapterByName (doDaName .doName ).updateDAI (doDaName .daName , setting .getChannelLevMod ().value ()) : Optional .empty ();
372+ }
373+ }
374+ case STVAL_DA_NAME -> lnAdapter .getDOIAdapterByName (doDaName .doName ).updateDAI (doDaName .daName , ActiveStatus .ON .getValue ());
375+ case SETSRCREF_DA_NAME -> lnAdapter .getDOIAdapterByName (doDaName .doName ).updateDAI (doDaName .daName , computeDaiValue (lnPrefix , extRef , setting .getDAName ()));
376+ default -> throw new IllegalStateException ("Unexpected value: " + doDaName .daName );
367377 };
368- return lnAdapter . getDOIAdapterByName ( doName ). updateDAI ( daName , value );
378+ sclReportItem . ifPresent ( errorHandler :: add );
369379 }
370380
371- private String computeDaiValue (AbstractLNAdapter <?> lnAdapter , TExtRef extRef , String daName ) {
372- if (LN_PREFIX_B .equals (lnAdapter . getPrefix ()) || LN_PREFIX_A .equals (lnAdapter . getPrefix () )) {
381+ private String computeDaiValue (String lnPrefix , TExtRef extRef , String daName ) {
382+ if (LN_PREFIX_B .equals (lnPrefix ) || LN_PREFIX_A .equals (lnPrefix )) {
373383 return extRef .getIedName () +
374384 extRef .getLdInst () + "/" +
375385 trimToEmpty (extRef .getPrefix ()) +
0 commit comments