Skip to content

Commit 19605de

Browse files
authored
Merge pull request #118 from com-pas/feature/74_clear_controlblock_dataset_extref
[#74] : delete all Control Blocks and Dataset and all ExtRef @srcxxx attributes
2 parents cee4f8a + 6dcdd01 commit 19605de

File tree

17 files changed

+370
-139
lines changed

17 files changed

+370
-139
lines changed

sct-commons/pom.xml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
<dependency>
3333
<groupId>org.apache.commons</groupId>
3434
<artifactId>commons-lang3</artifactId>
35-
<version>3.11</version>
35+
<version>3.12.0</version>
3636
</dependency>
3737
<dependency>
3838
<groupId>jakarta.xml.bind</groupId>
@@ -196,6 +196,9 @@
196196
<source>${project.build.directory}/xsd/SCL2007B4/SCL.xsd</source>
197197
<source>${project.build.directory}/xsd/SCL_CoMPAS.xsd</source>
198198
</sources>
199+
<xjbSources>
200+
<xjbSource>${project.basedir}/src/main/resources/binding_configuration.xjb</xjbSource>
201+
</xjbSources>
199202
<packageName>org.lfenergy.compas.scl2007b4.model</packageName>
200203
<noPackageLevelAnnotations>true</noPackageLevelAnnotations>
201204
</configuration>
@@ -214,4 +217,4 @@
214217

215218
</plugins>
216219
</build>
217-
</project>
220+
</project>

sct-commons/src/main/java/org/lfenergy/compas/sct/commons/Utils.java

Lines changed: 38 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -6,78 +6,68 @@
66

77
import lombok.extern.slf4j.Slf4j;
88

9-
import java.lang.reflect.Field;
9+
import java.util.Objects;
10+
import java.util.function.Function;
11+
import java.util.function.Predicate;
1012

1113
@Slf4j
1214
public class Utils {
1315

16+
public static final String LEAVING_PREFIX = "<<< Leaving: ::";
17+
public static final String ENTERING_PREFIX = ">>> Entering: ::";
18+
1419
private Utils() {
1520
throw new IllegalStateException("Utils class");
1621
}
1722

18-
public static String entering(){
19-
return ">>> " +
20-
"Entering: " +
21-
"-::" +
22-
getMethodName();
23+
public static String entering() {
24+
return ENTERING_PREFIX +
25+
getMethodName();
2326
}
2427

25-
public static String leaving(Long startTime){
26-
if(startTime == null || startTime <= 0){
27-
return leaving();
28+
public static String leaving(Long startTime) {
29+
if (startTime == null || startTime <= 0) {
30+
return LEAVING_PREFIX +
31+
getMethodName();
2832
}
29-
return "<<< " +
30-
"Leaving: " +
31-
"-::" +
32-
getMethodName() +
33-
" - Timer duration: " +
34-
(System.nanoTime() - startTime) / Math.pow(10, 9) +
35-
" sec.";
33+
return LEAVING_PREFIX +
34+
getMethodName() +
35+
" - Timer duration: " +
36+
(System.nanoTime() - startTime) / Math.pow(10, 9) +
37+
" sec.";
3638
}
3739

38-
public static String getMethodName() {
40+
private static String getMethodName() {
3941
try {
4042
return (new Throwable()).getStackTrace()[2].getMethodName();
41-
} catch (Exception e){
43+
} catch (Exception e) {
4244
return "-";
4345
}
4446
}
4547

46-
public static String leaving(){
47-
return "<<< " +
48-
"Leaving: " +
49-
"::" +
50-
getMethodName();
48+
public static String leaving() {
49+
return LEAVING_PREFIX +
50+
getMethodName();
5151
}
5252

5353
/**
54-
* Returns the first {@link Field} in the hierarchy for the specified name
54+
* Test if two fields with primitive values are equals or are both not set.
55+
* @param o1 object to compare
56+
* @param o2 object to compare
57+
* @param isSet predicate that returns if fields is set
58+
* @param getValue getter that return the unboxed field
59+
* @return true if both fields are set and are equals, or if both fields are not set. False otherwise.
5560
*/
56-
public static Field getField(Class<?> clazz, String name) {
57-
Field field = null;
58-
while (clazz != null && field == null) {
59-
try {
60-
field = clazz.getDeclaredField(name);
61-
} catch (Exception e) {
62-
log.error("Cannot find field name {}", name, e);
63-
}
64-
clazz = clazz.getSuperclass();
61+
public static <T, R> boolean equalsOrNotSet(T o1, T o2, Predicate<T> isSet, Function<T, R> getValue) {
62+
Objects.requireNonNull(o1);
63+
Objects.requireNonNull(o2);
64+
if (!isSet.test(o1)){
65+
return !isSet.test(o2);
6566
}
66-
return field;
67-
}
68-
69-
/**
70-
* Sets {@code value} to the first {@link Field} in the {@code object} hierarchy, for the specified name
71-
*/
72-
public static void setField(Object object, String fieldName, Object value) {
73-
try {
74-
Field field = getField(object.getClass(), fieldName);
75-
if(field != null){
76-
field.setAccessible(true);
77-
field.set(object, value);
78-
}
79-
} catch (Exception e) {
80-
log.error("Cannot nullify {} : ",fieldName, e);
67+
if (!isSet.test(o2)){
68+
return false;
8169
}
70+
return Objects.equals(getValue.apply(o1), getValue.apply(o2));
8271
}
72+
8373
}

sct-commons/src/main/java/org/lfenergy/compas/sct/commons/dto/ControlBlock.java

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,7 @@
77
import lombok.Getter;
88
import lombok.NoArgsConstructor;
99
import lombok.Setter;
10-
import org.lfenergy.compas.scl2007b4.model.TClientLN;
11-
import org.lfenergy.compas.scl2007b4.model.TControl;
12-
import org.lfenergy.compas.scl2007b4.model.TControlWithIEDName;
13-
import org.lfenergy.compas.scl2007b4.model.TPredefinedTypeOfSecurityEnum;
14-
import org.lfenergy.compas.scl2007b4.model.TServiceSettingsNoDynEnum;
15-
import org.lfenergy.compas.scl2007b4.model.TServiceType;
16-
import org.lfenergy.compas.scl2007b4.model.TServices;
17-
import org.lfenergy.compas.sct.commons.Utils;
10+
import org.lfenergy.compas.scl2007b4.model.*;
1811
import org.lfenergy.compas.sct.commons.exception.ScdException;
1912
import org.lfenergy.compas.sct.commons.scl.SclRootAdapter;
2013
import org.lfenergy.compas.sct.commons.scl.ied.IEDAdapter;
@@ -79,14 +72,12 @@ public void validateDestination(SclRootAdapter sclRootAdapter) throws ScdExcepti
7972
)
8073
)
8174
);
82-
if(!iedName.getLnClass().isEmpty()) {
75+
if (iedName.isSetLnClass() && !iedName.getLnClass().isEmpty()) {
8376
try {
8477
lDeviceAdapter.getLNAdapter(iedName.getLnClass().get(0), iedName.getLnInst(), iedName.getPrefix());
85-
} catch (ScdException e){
78+
} catch (ScdException e) {
8679
throw new ScdException("Control block destination: " + e.getMessage());
8780
}
88-
} else {
89-
Utils.setField(iedName,"lnClass",null);
9081
}
9182
}
9283
}

sct-commons/src/main/java/org/lfenergy/compas/sct/commons/dto/DaTypeName.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public void addDaiValues(List<TVal> vals) {
7575
}
7676

7777
public void addDaiValue(TVal val) {
78-
if(val.getSGroup() == null){
78+
if(!val.isSetSGroup()){
7979
daiValues.put(0L,val.getValue());
8080
} else {
8181
daiValues.put(val.getSGroup(), val.getValue());

sct-commons/src/main/java/org/lfenergy/compas/sct/commons/dto/FCDAInfo.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,9 @@ public FCDAInfo(String dataSet, TFCDA tfcda) {
3939
lnInst = tfcda.getLnInst();
4040
doName = new DoTypeName(tfcda.getDoName());
4141
daName = new DaTypeName(tfcda.getDaName());
42-
ix = tfcda.getIx();
42+
ix = tfcda.isSetIx() ? tfcda.getIx() : null;
4343
}
4444

45-
4645
@JsonIgnore
4746
public TFCDA getFCDA(){
4847
TFCDA tfcda = new TFCDA();
@@ -75,4 +74,4 @@ public TFCDA getFCDA(){
7574
public boolean isValid() {
7675
return doName != null && doName.isDefined();
7776
}
78-
}
77+
}

sct-commons/src/main/java/org/lfenergy/compas/sct/commons/dto/GooseControlBlock.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,7 @@
77
import lombok.Getter;
88
import lombok.NoArgsConstructor;
99
import lombok.Setter;
10-
import org.lfenergy.compas.scl2007b4.model.TGSEControl;
11-
import org.lfenergy.compas.scl2007b4.model.TMcSecurity;
12-
import org.lfenergy.compas.scl2007b4.model.TPredefinedTypeOfSecurityEnum;
13-
import org.lfenergy.compas.scl2007b4.model.TProtocol;
14-
import org.lfenergy.compas.scl2007b4.model.TServiceType;
15-
import org.lfenergy.compas.scl2007b4.model.TServices;
10+
import org.lfenergy.compas.scl2007b4.model.*;
1611
import org.lfenergy.compas.sct.commons.exception.ScdException;
1712

1813

@@ -27,7 +22,7 @@ public GooseControlBlock(TGSEControl tgseControl) {
2722
super();
2823
this.id = tgseControl.getAppID();
2924
this.name = tgseControl.getName();
30-
if(tgseControl.getConfRev() != null) {
25+
if(tgseControl.isSetConfRev()) {
3126
this.confRev = tgseControl.getConfRev();
3227
}
3328
this.desc = tgseControl.getDesc();

sct-commons/src/main/java/org/lfenergy/compas/sct/commons/dto/SMVControlBlock.java

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,7 @@
77
import lombok.Getter;
88
import lombok.NoArgsConstructor;
99
import lombok.Setter;
10-
import org.lfenergy.compas.scl2007b4.model.TMcSecurity;
11-
import org.lfenergy.compas.scl2007b4.model.TPredefinedTypeOfSecurityEnum;
12-
import org.lfenergy.compas.scl2007b4.model.TProtocol;
13-
import org.lfenergy.compas.scl2007b4.model.TSampledValueControl;
14-
import org.lfenergy.compas.scl2007b4.model.TServiceType;
15-
import org.lfenergy.compas.scl2007b4.model.TServices;
16-
import org.lfenergy.compas.scl2007b4.model.TSmpMod;
10+
import org.lfenergy.compas.scl2007b4.model.*;
1711
import org.lfenergy.compas.sct.commons.exception.ScdException;
1812

1913
import java.util.Collections;
@@ -36,9 +30,7 @@ public SMVControlBlock(TSampledValueControl tSampledValueControl) {
3630
super();
3731
this.id = tSampledValueControl.getSmvID();
3832
this.name = tSampledValueControl.getName();
39-
if(tSampledValueControl.getConfRev() != null) {
40-
this.confRev = tSampledValueControl.getConfRev();
41-
}
33+
this.confRev = tSampledValueControl.isSetConfRev() ? tSampledValueControl.getConfRev() : null;
4234
this.desc = tSampledValueControl.getDesc();
4335
this.dataSetRef = tSampledValueControl.getDatSet();
4436
Collections.copy(iedNames, tSampledValueControl.getIEDName());

sct-commons/src/main/java/org/lfenergy/compas/sct/commons/scl/SclService.java

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,7 @@
2020
import org.lfenergy.compas.sct.commons.scl.dtt.EnumTypeAdapter;
2121
import org.lfenergy.compas.sct.commons.scl.dtt.LNodeTypeAdapter;
2222
import org.lfenergy.compas.sct.commons.scl.header.HeaderAdapter;
23-
import org.lfenergy.compas.sct.commons.scl.ied.AbstractLNAdapter;
24-
import org.lfenergy.compas.sct.commons.scl.ied.DAITracker;
25-
import org.lfenergy.compas.sct.commons.scl.ied.IEDAdapter;
26-
import org.lfenergy.compas.sct.commons.scl.ied.LDeviceAdapter;
23+
import org.lfenergy.compas.sct.commons.scl.ied.*;
2724
import org.lfenergy.compas.sct.commons.scl.sstation.SubstationAdapter;
2825
import org.lfenergy.compas.sct.commons.scl.sstation.VoltageLevelAdapter;
2926

@@ -514,4 +511,22 @@ private static Optional<TCompasICDHeader> getCompasICDHeader(TPrivate tPrivate)
514511
return tCompasICDHeader.map(JAXBElement::getValue);
515512
}
516513

514+
public static void removeAllControlBlocksAndDatasetsAndExtRefSrcBindings(final SCL scl) {
515+
SclRootAdapter sclRootAdapter = new SclRootAdapter(scl);
516+
List<LDeviceAdapter> lDeviceAdapters = sclRootAdapter.getIEDAdapters().stream()
517+
.map(IEDAdapter::getLDeviceAdapters).flatMap(List::stream).collect(Collectors.toList());
518+
519+
// LN0
520+
lDeviceAdapters.stream()
521+
.map(LDeviceAdapter::getLN0Adapter)
522+
.forEach(ln0 -> {
523+
ln0.removeAllControlBlocksAndDatasets();
524+
ln0.removeAllExtRefSourceBindings();
525+
});
526+
527+
// Other LN
528+
lDeviceAdapters.stream()
529+
.map(LDeviceAdapter::getLNAdapters).flatMap(List::stream)
530+
.forEach(LNAdapter::removeAllControlBlocksAndDatasets);
531+
}
517532
}

sct-commons/src/main/java/org/lfenergy/compas/sct/commons/scl/dtt/AbstractDataAttributeAdapter.java

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,7 @@
55
package org.lfenergy.compas.sct.commons.scl.dtt;
66

77
import lombok.Getter;
8-
import org.lfenergy.compas.scl2007b4.model.TAbstractDataAttribute;
9-
import org.lfenergy.compas.scl2007b4.model.TDA;
10-
import org.lfenergy.compas.scl2007b4.model.TPredefinedBasicTypeEnum;
11-
import org.lfenergy.compas.scl2007b4.model.TProtNs;
12-
import org.lfenergy.compas.scl2007b4.model.TVal;
8+
import org.lfenergy.compas.scl2007b4.model.*;
139
import org.lfenergy.compas.sct.commons.Utils;
1410
import org.lfenergy.compas.sct.commons.dto.DaTypeName;
1511
import org.lfenergy.compas.sct.commons.exception.ScdException;
@@ -48,7 +44,6 @@ public Optional<DATypeAdapter> getDATypeAdapter() {
4844

4945

5046
public boolean hasSameContentAs(T data) {
51-
final String countField = "count";
5247
if(!Objects.equals(getName(),data.getName())
5348
|| !Objects.equals(getBType(),data.getBType())
5449
|| !Objects.equals(getType(),data.getType())
@@ -67,30 +62,21 @@ public boolean hasSameContentAs(T data) {
6762
return false;
6863
}
6964
}
70-
if(!Objects.equals(currentElem.getCount(),data.getCount())){
71-
if(currentElem.getCount().isEmpty()){
72-
Utils.setField(currentElem,countField,null);
73-
}
74-
if(data.getCount().isEmpty()){
75-
Utils.setField(data,countField,null);
76-
}
77-
return false ;
78-
} else if(currentElem.getCount().isEmpty()){
79-
Utils.setField(currentElem,countField,null);
80-
Utils.setField(data,countField,null);
65+
if (!Utils.equalsOrNotSet(currentElem, data, TAbstractDataAttribute::isSetCount, TAbstractDataAttribute::getCount)){
66+
return false;
8167
}
8268

69+
8370
if((getBType() == TPredefinedBasicTypeEnum.ENUM ||
8471
getBType() == TPredefinedBasicTypeEnum.STRUCT)
8572
&& !Objects.equals(getType(),data.getType())) {
8673
return false;
8774
}
8875

8976
for(TVal prdVal : data.getVal()){
90-
boolean hasSameVal = currentElem.getVal().stream()
91-
.anyMatch(rcvVal -> rcvVal.getValue().equals(prdVal.getValue()) &&
92-
Objects.equals(rcvVal.getSGroup(), prdVal.getSGroup()));
93-
if(!hasSameVal) {
77+
if(currentElem.isSetVal() && currentElem.getVal().stream()
78+
.noneMatch(rcvVal -> rcvVal.getValue().equals(prdVal.getValue()) &&
79+
Utils.equalsOrNotSet(rcvVal, prdVal, TVal::isSetSGroup, TVal::getSGroup))) {
9480
return false;
9581
}
9682
}

sct-commons/src/main/java/org/lfenergy/compas/sct/commons/scl/ied/AbstractDAIAdapter.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,9 @@ public <S extends IDataAdapter> S getDataAdapterByName(String sName) throws Scd
3030
public void setValImport(boolean b){
3131
currentElem.setValImport(b);
3232
}
33+
3334
public Boolean isValImport(){
34-
return currentElem.isValImport();
35+
return currentElem.isSetValImport() ? currentElem.isValImport() : null;
3536
}
3637

3738
public AbstractDAIAdapter<? extends SclElementAdapter> update(Map<Long, String> daiValues) throws ScdException {
@@ -46,16 +47,16 @@ public AbstractDAIAdapter<? extends SclElementAdapter> update(Map<Long, String>
4647
}
4748

4849
public void update(Long sGroup, String val) throws ScdException {
49-
if(currentElem.isValImport() != null && !currentElem.isValImport()){
50+
if(currentElem.isSetValImport() && !currentElem.isValImport()){
5051
String msg = String.format(
5152
"DAI(%s) cannot be updated : valImport(false)",currentElem.getName()
5253
);
5354
throw new ScdException(msg);
5455
}
55-
Stream<TVal> tValStream = currentElem.getVal().stream() ;
56-
if(sGroup != 0){
57-
Optional<TVal> tVal = tValStream.filter(tValElem -> tValElem.getSGroup() != null &&
58-
tValElem.getSGroup().equals(sGroup))
56+
Stream<TVal> tValStream = currentElem.getVal().stream();
57+
if (sGroup != null && sGroup != 0) {
58+
Optional<TVal> tVal = tValStream.filter(tValElem -> tValElem.isSetSGroup() &&
59+
sGroup.equals(tValElem.getSGroup()))
5960
.findFirst();
6061
if(tVal.isPresent()){
6162
tVal.get().setValue(val);

0 commit comments

Comments
 (0)