1717import org .lfenergy .compas .sct .commons .scl .SclRootAdapter ;
1818import org .lfenergy .compas .sct .commons .util .ControlBlockEnum ;
1919import org .lfenergy .compas .sct .commons .util .FcdaCandidates ;
20+ import org .lfenergy .compas .sct .commons .util .LdeviceStatus ;
2021import org .lfenergy .compas .sct .commons .util .Utils ;
2122
2223import java .util .*;
2324import java .util .function .Predicate ;
2425
2526import static org .lfenergy .compas .sct .commons .util .CommonConstants .*;
26- import static org .lfenergy .compas .sct .commons .util .LDeviceStatus .OFF ;
27- import static org .lfenergy .compas .sct .commons .util .LDeviceStatus .ON ;
2827
2928/**
3029 * A representation of the model object
@@ -41,17 +40,16 @@ public class InputsAdapter extends SclElementAdapter<LN0Adapter, TInputs> {
4140 private static final String MESSAGE_NO_MATCHING_COMPAS_FLOW = "The signal ExtRef has no matching compas:Flow Private" ;
4241 private static final String MESSAGE_TOO_MANY_MATCHING_COMPAS_FLOWS = "The signal ExtRef has more than one matching compas:Flow Private" ;
4342 private static final String MESSAGE_SOURCE_LDEVICE_NOT_FOUND = "The signal ExtRef ExtRefldinst does not match any " +
44- "LDevice with same inst attribute in source IED %s" ;
43+ "LDevice with same inst attribute in source IED %s" ;
4544 private static final String MESSAGE_SOURCE_LN_NOT_FOUND = "The signal ExtRef lninst, doName or daName does not match any " +
46- "source in LDevice %s" ;
45+ "source in LDevice %s" ;
4746 private static final String MESSAGE_SERVICE_TYPE_MISSING = "The signal ExtRef is missing ServiceType attribute" ;
4847 private static final String MESSAGE_INVALID_SERVICE_TYPE = "The signal ExtRef ServiceType attribute is unexpected : %s" ;
4948 private static final String MESSAGE_IED_MISSING_COMPAS_BAY_UUID = "IED is missing Private/compas:Bay@UUID attribute" ;
5049 private static final String MESSAGE_EXTREF_DESC_MALFORMED = "ExtRef.serviceType=Report but ExtRef.desc attribute is malformed" ;
5150 private static final String MESSAGE_LDEVICE_STATUS_UNDEFINED = "The LDevice status is undefined" ;
52- private static final String MESSAGE_LDEVICE_STATUS_NEITHER_ON_NOR_OFF = "The LDevice status is neither \" on\" nor \" off\" " ;
5351 private static final String MESSAGE_EXTREF_IEDNAME_DOES_NOT_MATCH_ANY_SYSTEM_VERSION_UUID = "The signal ExtRef iedName does not match any " +
54- "IED/Private/compas:ICDHeader@ICDSystemVersionUUID" ;
52+ "IED/Private/compas:ICDHeader@ICDSystemVersionUUID" ;
5553 private static final String MESSAGE_SOURCE_LDEVICE_STATUS_UNDEFINED = "The signal ExtRef source LDevice %s status is undefined" ;
5654 private static final String MESSAGE_SOURCE_LDEVICE_STATUS_NEITHER_ON_NOR_OFF = "The signal ExtRef source LDevice %s status is neither \" on\" nor \" off\" " ;
5755 private static final String MESSAGE_SOURCE_LDEVICE_STATUS_OFF = "The signal ExtRef source LDevice %s status is off" ;
@@ -97,33 +95,36 @@ public List<SclReportItem> updateAllExtRefIedNames(Map<String, IEDAdapter> icdSy
9795 if (optionalLDeviceStatus .isEmpty ()) {
9896 return List .of (getLDeviceAdapter ().buildFatalReportItem (MESSAGE_LDEVICE_STATUS_UNDEFINED ));
9997 }
100- String lDeviceStatus = optionalLDeviceStatus .get ();
101- return switch (lDeviceStatus ) {
102- case ON -> getExtRefs ().stream ()
103- .filter (tExtRef -> StringUtils .isNotBlank (tExtRef .getIedName ()) && StringUtils .isNotBlank (tExtRef .getDesc ()))
104- .map (extRef ->
105- updateExtRefIedName (extRef , icdSystemVersionToIed .get (extRef .getIedName ())))
106- .flatMap (Optional ::stream )
107- .toList ();
108- case OFF -> {
109- getExtRefs ().forEach (this ::clearBinding );
110- yield Collections .emptyList ();
111- }
112- default -> List .of (getLDeviceAdapter ().buildFatalReportItem (MESSAGE_LDEVICE_STATUS_NEITHER_ON_NOR_OFF ));
113- };
98+ try {
99+ LdeviceStatus lDeviceStatus = LdeviceStatus .fromValue (optionalLDeviceStatus .get ());
100+ return switch (lDeviceStatus ) {
101+ case ON -> getExtRefs ().stream ()
102+ .filter (tExtRef -> StringUtils .isNotBlank (tExtRef .getIedName ()) && StringUtils .isNotBlank (tExtRef .getDesc ()))
103+ .map (extRef ->
104+ updateExtRefIedName (extRef , icdSystemVersionToIed .get (extRef .getIedName ())))
105+ .flatMap (Optional ::stream )
106+ .toList ();
107+ case OFF -> {
108+ getExtRefs ().forEach (this ::clearBinding );
109+ yield Collections .emptyList ();
110+ }
111+ };
112+ } catch (IllegalArgumentException e ) {
113+ return List .of (getLDeviceAdapter ().buildFatalReportItem (e .getMessage ()));
114+ }
114115 }
115116
116117 /**
117118 * Find matching CompasFlow private and set ExtRef iedName accordingly
118119 *
119- * @param extRef extRef whose iedName will be updated
120+ * @param extRef extRef whose iedName will be updated
120121 * @return Error if ExtRef could not be updated
121122 */
122123 private Optional <SclReportItem > updateExtRefIedName (TExtRef extRef , IEDAdapter sourceIed ) {
123124 List <TCompasFlow > matchingCompasFlows = getMatchingCompasFlows (extRef );
124125 if (!singleMatch (matchingCompasFlows )) {
125126 return fatalReportItem (extRef ,
126- matchingCompasFlows .isEmpty () ? MESSAGE_NO_MATCHING_COMPAS_FLOW : MESSAGE_TOO_MANY_MATCHING_COMPAS_FLOWS );
127+ matchingCompasFlows .isEmpty () ? MESSAGE_NO_MATCHING_COMPAS_FLOW : MESSAGE_TOO_MANY_MATCHING_COMPAS_FLOWS );
127128 }
128129 TCompasFlow compasFlow = matchingCompasFlows .get (0 );
129130 if (compasFlow .getFlowStatus () == TCompasFlowStatus .INACTIVE ) {
@@ -136,7 +137,7 @@ private Optional<SclReportItem> updateExtRefIedName(TExtRef extRef, IEDAdapter s
136137 return sourceValidationError ;
137138 }
138139 String sourceIedName = PrivateService .extractCompasPrivate (sourceIed .getCurrentElem (), TCompasICDHeader .class )
139- .map (TCompasICDHeader ::getIEDName ).orElse ("" );
140+ .map (TCompasICDHeader ::getIEDName ).orElse ("" );
140141 extRef .setIedName (sourceIedName );
141142 compasFlow .setExtRefiedName (sourceIedName );
142143 log .debug (String .format ("extRef.desc=%s, iedName=%s%n" , extRef .getDesc (), sourceIedName ));
@@ -170,17 +171,19 @@ private Optional<SclReportItem> validateExtRefSource(TExtRef extRef, IEDAdapter
170171 Optional <String > optionalSourceLDeviceStatus = sourceLDevice .getLDeviceStatus ();
171172 if (optionalSourceLDeviceStatus .isEmpty ()) {
172173 return fatalReportItem (extRef , String .format (MESSAGE_SOURCE_LDEVICE_STATUS_UNDEFINED ,
173- sourceLDevice .getXPath ()));
174- }
175- return optionalSourceLDeviceStatus .map (sourceLDeviceStatus ->
176- switch (sourceLDeviceStatus ) {
177- case OFF -> SclReportItem .warning (extRefXPath (extRef .getDesc ()), String .format (MESSAGE_SOURCE_LDEVICE_STATUS_OFF ,
178174 sourceLDevice .getXPath ()));
179- case ON -> null ;
180- default -> SclReportItem .fatal (extRefXPath (extRef .getDesc ()),
181- String .format (MESSAGE_SOURCE_LDEVICE_STATUS_NEITHER_ON_NOR_OFF ,
182- sourceLDevice .getXPath ()));
183- });
175+ }
176+ return optionalSourceLDeviceStatus .map (sourceLDeviceStatus -> {
177+ try {
178+ LdeviceStatus lDeviceStatus = LdeviceStatus .fromValue (sourceLDeviceStatus );
179+ return switch (lDeviceStatus ) {
180+ case OFF -> SclReportItem .warning (extRefXPath (extRef .getDesc ()), String .format (MESSAGE_SOURCE_LDEVICE_STATUS_OFF , sourceLDevice .getXPath ()));
181+ case ON -> null ;
182+ };
183+ } catch (IllegalArgumentException e ) {
184+ return SclReportItem .fatal (extRefXPath (extRef .getDesc ()), String .format (MESSAGE_SOURCE_LDEVICE_STATUS_NEITHER_ON_NOR_OFF , sourceLDevice .getXPath ()));
185+ }
186+ });
184187 }
185188
186189 private boolean singleMatch (List <TCompasFlow > matchingCompasFlows ) {
@@ -213,13 +216,13 @@ private Optional<SclReportItem> fatalReportItem(TExtRef extRef, String message)
213216
214217 private String extRefXPath (String extRefDesc ) {
215218 return getXPath () + String .format ("/ExtRef[%s]" ,
216- Utils .xpathAttributeFilter ("desc" , extRefDesc ));
219+ Utils .xpathAttributeFilter ("desc" , extRefDesc ));
217220 }
218221
219222 /**
220223 * Find CompasFlows that match given ExtRef
221224 *
222- * @param extRef extRef to match
225+ * @param extRef extRef to match
223226 * @return list of matching CompasFlows
224227 */
225228 private List <TCompasFlow > getMatchingCompasFlows (TExtRef extRef ) {
@@ -239,18 +242,18 @@ private List<TCompasFlow> getMatchingCompasFlows(TExtRef extRef) {
239242 private boolean isMatchingExtRef (TCompasFlow compasFlow , TExtRef extRef ) {
240243 String extRefLnClass = extRef .isSetLnClass () ? extRef .getLnClass ().get (0 ) : null ;
241244 return Utils .equalsOrBothBlank (compasFlow .getDataStreamKey (), extRef .getDesc ())
242- && Utils .equalsOrBothBlank (compasFlow .getExtRefiedName (), extRef .getIedName ())
243- && Utils .equalsOrBothBlank (compasFlow .getExtRefldinst (), extRef .getLdInst ())
244- && Utils .equalsOrBothBlank (compasFlow .getExtRefprefix (), extRef .getPrefix ())
245- && Utils .equalsOrBothBlank (compasFlow .getExtReflnClass (), extRefLnClass )
246- && Utils .equalsOrBothBlank (compasFlow .getExtReflnInst (), extRef .getLnInst ());
245+ && Utils .equalsOrBothBlank (compasFlow .getExtRefiedName (), extRef .getIedName ())
246+ && Utils .equalsOrBothBlank (compasFlow .getExtRefldinst (), extRef .getLdInst ())
247+ && Utils .equalsOrBothBlank (compasFlow .getExtRefprefix (), extRef .getPrefix ())
248+ && Utils .equalsOrBothBlank (compasFlow .getExtReflnClass (), extRefLnClass )
249+ && Utils .equalsOrBothBlank (compasFlow .getExtReflnInst (), extRef .getLnInst ());
247250 }
248251
249252 private LDeviceAdapter getLDeviceAdapter () {
250253 return parentAdapter .getParentAdapter ();
251254 }
252255
253- private AbstractLNAdapter <?> getLNAdapter (){
256+ private AbstractLNAdapter <?> getLNAdapter () {
254257 return parentAdapter ;
255258 }
256259
@@ -260,19 +263,19 @@ public List<SclReportItem> updateAllSourceDataSetsAndControlBlocks() {
260263 return List .of (getIedAdapter ().buildFatalReportItem (MESSAGE_IED_MISSING_COMPAS_BAY_UUID ));
261264 }
262265 return getExtRefs ().stream ()
263- .filter (this ::areBindingAttributesPresent )
264- .filter (this ::isExternalBound )
265- .filter (this ::matchingCompasFlowIsActiveOrUntested )
266- .map (extRef -> updateSourceDataSetsAndControlBlocks (extRef , currentBayUuid ))
267- .flatMap (Optional ::stream )
268- .toList ();
266+ .filter (this ::areBindingAttributesPresent )
267+ .filter (this ::isExternalBound )
268+ .filter (this ::matchingCompasFlowIsActiveOrUntested )
269+ .map (extRef -> updateSourceDataSetsAndControlBlocks (extRef , currentBayUuid ))
270+ .flatMap (Optional ::stream )
271+ .toList ();
269272 }
270273
271274 private boolean matchingCompasFlowIsActiveOrUntested (TExtRef extRef ) {
272275 return getMatchingCompasFlows (extRef ).stream ().findFirst ()
273- .map (TCompasFlow ::getFlowStatus )
274- .filter (flowStatus -> flowStatus == TCompasFlowStatus .ACTIVE || flowStatus == TCompasFlowStatus .UNTESTED )
275- .isPresent ();
276+ .map (TCompasFlow ::getFlowStatus )
277+ .filter (flowStatus -> flowStatus == TCompasFlowStatus .ACTIVE || flowStatus == TCompasFlowStatus .UNTESTED )
278+ .isPresent ();
276279 }
277280
278281 private boolean isExternalBound (TExtRef tExtRef ) {
@@ -281,10 +284,10 @@ private boolean isExternalBound(TExtRef tExtRef) {
281284
282285 private boolean areBindingAttributesPresent (TExtRef tExtRef ) {
283286 return StringUtils .isNotBlank (tExtRef .getIedName ())
284- && StringUtils .isNotBlank (tExtRef .getDesc ())
285- && StringUtils .isNotBlank (tExtRef .getLdInst ())
286- && tExtRef .getLnClass ().stream ().findFirst ().filter (StringUtils ::isNotBlank ).isPresent ()
287- && StringUtils .isNotBlank (tExtRef .getDoName ());
287+ && StringUtils .isNotBlank (tExtRef .getDesc ())
288+ && StringUtils .isNotBlank (tExtRef .getLdInst ())
289+ && tExtRef .getLnClass ().stream ().findFirst ().filter (StringUtils ::isNotBlank ).isPresent ()
290+ && StringUtils .isNotBlank (tExtRef .getDoName ());
288291 }
289292
290293 private Optional <SclReportItem > updateSourceDataSetsAndControlBlocks (TExtRef extRef , String targetBayUuid ) {
@@ -297,8 +300,8 @@ private Optional<SclReportItem> updateSourceDataSetsAndControlBlocks(TExtRef ext
297300 }
298301 IEDAdapter sourceIed = optionalSrcIedAdapter .get ();
299302 Optional <String > sourceIedBayUuid = sourceIed .getPrivateCompasBay ()
300- .map (TCompasBay ::getUUID )
301- .filter (StringUtils ::isNotBlank );
303+ .map (TCompasBay ::getUUID )
304+ .filter (StringUtils ::isNotBlank );
302305 if (sourceIedBayUuid .isEmpty ()) {
303306 return fatalReportItem (extRef , MESSAGE_SOURCE_IED_MISSING_COMPAS_BAY_UUID );
304307 }
@@ -342,9 +345,9 @@ private void createDataSetWithFCDA(TExtRef extRef, LDeviceAdapter sourceLDevice,
342345 String fcdaDaName = extRef .getServiceType () == TServiceType .REPORT ? null : sourceDa .getDaRef ();
343346 String fcdaLnClass = extRef .getLnClass ().stream ().findFirst ().orElse (null );
344347 dataSetAdapter .createFCDAIfNotExists (extRef .getLdInst (), extRef .getPrefix (), fcdaLnClass , extRef .getLnInst (),
345- sourceDa .getDoRef (),
346- fcdaDaName ,
347- sourceDa .getFc ());
348+ sourceDa .getDoRef (),
349+ fcdaDaName ,
350+ sourceDa .getFc ());
348351 }
349352
350353 private void createControlBlockWithTarget (TExtRef extRef , LDeviceAdapter sourceLDevice , DataAttributeRef sourceDa , String cbName , String datSet ) {
0 commit comments