Skip to content

Commit

Permalink
Merge pull request #118 from com-pas/feature/74_clear_controlblock_da…
Browse files Browse the repository at this point in the history
…taset_extref

[#74] : delete all Control Blocks and Dataset and all ExtRef @srcxxx attributes
  • Loading branch information
massifben authored Jul 5, 2022
2 parents cee4f8a + 6dcdd01 commit 19605de
Show file tree
Hide file tree
Showing 17 changed files with 370 additions and 139 deletions.
7 changes: 5 additions & 2 deletions sct-commons/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.11</version>
<version>3.12.0</version>
</dependency>
<dependency>
<groupId>jakarta.xml.bind</groupId>
Expand Down Expand Up @@ -196,6 +196,9 @@
<source>${project.build.directory}/xsd/SCL2007B4/SCL.xsd</source>
<source>${project.build.directory}/xsd/SCL_CoMPAS.xsd</source>
</sources>
<xjbSources>
<xjbSource>${project.basedir}/src/main/resources/binding_configuration.xjb</xjbSource>
</xjbSources>
<packageName>org.lfenergy.compas.scl2007b4.model</packageName>
<noPackageLevelAnnotations>true</noPackageLevelAnnotations>
</configuration>
Expand All @@ -214,4 +217,4 @@

</plugins>
</build>
</project>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -6,78 +6,68 @@

import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.Field;
import java.util.Objects;
import java.util.function.Function;
import java.util.function.Predicate;

@Slf4j
public class Utils {

public static final String LEAVING_PREFIX = "<<< Leaving: ::";
public static final String ENTERING_PREFIX = ">>> Entering: ::";

private Utils() {
throw new IllegalStateException("Utils class");
}

public static String entering(){
return ">>> " +
"Entering: " +
"-::" +
getMethodName();
public static String entering() {
return ENTERING_PREFIX +
getMethodName();
}

public static String leaving(Long startTime){
if(startTime == null || startTime <= 0){
return leaving();
public static String leaving(Long startTime) {
if (startTime == null || startTime <= 0) {
return LEAVING_PREFIX +
getMethodName();
}
return "<<< " +
"Leaving: " +
"-::" +
getMethodName() +
" - Timer duration: " +
(System.nanoTime() - startTime) / Math.pow(10, 9) +
" sec.";
return LEAVING_PREFIX +
getMethodName() +
" - Timer duration: " +
(System.nanoTime() - startTime) / Math.pow(10, 9) +
" sec.";
}

public static String getMethodName() {
private static String getMethodName() {
try {
return (new Throwable()).getStackTrace()[2].getMethodName();
} catch (Exception e){
} catch (Exception e) {
return "-";
}
}

public static String leaving(){
return "<<< " +
"Leaving: " +
"::" +
getMethodName();
public static String leaving() {
return LEAVING_PREFIX +
getMethodName();
}

/**
* Returns the first {@link Field} in the hierarchy for the specified name
* Test if two fields with primitive values are equals or are both not set.
* @param o1 object to compare
* @param o2 object to compare
* @param isSet predicate that returns if fields is set
* @param getValue getter that return the unboxed field
* @return true if both fields are set and are equals, or if both fields are not set. False otherwise.
*/
public static Field getField(Class<?> clazz, String name) {
Field field = null;
while (clazz != null && field == null) {
try {
field = clazz.getDeclaredField(name);
} catch (Exception e) {
log.error("Cannot find field name {}", name, e);
}
clazz = clazz.getSuperclass();
public static <T, R> boolean equalsOrNotSet(T o1, T o2, Predicate<T> isSet, Function<T, R> getValue) {
Objects.requireNonNull(o1);
Objects.requireNonNull(o2);
if (!isSet.test(o1)){
return !isSet.test(o2);
}
return field;
}

/**
* Sets {@code value} to the first {@link Field} in the {@code object} hierarchy, for the specified name
*/
public static void setField(Object object, String fieldName, Object value) {
try {
Field field = getField(object.getClass(), fieldName);
if(field != null){
field.setAccessible(true);
field.set(object, value);
}
} catch (Exception e) {
log.error("Cannot nullify {} : ",fieldName, e);
if (!isSet.test(o2)){
return false;
}
return Objects.equals(getValue.apply(o1), getValue.apply(o2));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,7 @@
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.lfenergy.compas.scl2007b4.model.TClientLN;
import org.lfenergy.compas.scl2007b4.model.TControl;
import org.lfenergy.compas.scl2007b4.model.TControlWithIEDName;
import org.lfenergy.compas.scl2007b4.model.TPredefinedTypeOfSecurityEnum;
import org.lfenergy.compas.scl2007b4.model.TServiceSettingsNoDynEnum;
import org.lfenergy.compas.scl2007b4.model.TServiceType;
import org.lfenergy.compas.scl2007b4.model.TServices;
import org.lfenergy.compas.sct.commons.Utils;
import org.lfenergy.compas.scl2007b4.model.*;
import org.lfenergy.compas.sct.commons.exception.ScdException;
import org.lfenergy.compas.sct.commons.scl.SclRootAdapter;
import org.lfenergy.compas.sct.commons.scl.ied.IEDAdapter;
Expand Down Expand Up @@ -79,14 +72,12 @@ public void validateDestination(SclRootAdapter sclRootAdapter) throws ScdExcepti
)
)
);
if(!iedName.getLnClass().isEmpty()) {
if (iedName.isSetLnClass() && !iedName.getLnClass().isEmpty()) {
try {
lDeviceAdapter.getLNAdapter(iedName.getLnClass().get(0), iedName.getLnInst(), iedName.getPrefix());
} catch (ScdException e){
} catch (ScdException e) {
throw new ScdException("Control block destination: " + e.getMessage());
}
} else {
Utils.setField(iedName,"lnClass",null);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public void addDaiValues(List<TVal> vals) {
}

public void addDaiValue(TVal val) {
if(val.getSGroup() == null){
if(!val.isSetSGroup()){
daiValues.put(0L,val.getValue());
} else {
daiValues.put(val.getSGroup(), val.getValue());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,9 @@ public FCDAInfo(String dataSet, TFCDA tfcda) {
lnInst = tfcda.getLnInst();
doName = new DoTypeName(tfcda.getDoName());
daName = new DaTypeName(tfcda.getDaName());
ix = tfcda.getIx();
ix = tfcda.isSetIx() ? tfcda.getIx() : null;
}


@JsonIgnore
public TFCDA getFCDA(){
TFCDA tfcda = new TFCDA();
Expand Down Expand Up @@ -75,4 +74,4 @@ public TFCDA getFCDA(){
public boolean isValid() {
return doName != null && doName.isDefined();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,7 @@
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.lfenergy.compas.scl2007b4.model.TGSEControl;
import org.lfenergy.compas.scl2007b4.model.TMcSecurity;
import org.lfenergy.compas.scl2007b4.model.TPredefinedTypeOfSecurityEnum;
import org.lfenergy.compas.scl2007b4.model.TProtocol;
import org.lfenergy.compas.scl2007b4.model.TServiceType;
import org.lfenergy.compas.scl2007b4.model.TServices;
import org.lfenergy.compas.scl2007b4.model.*;
import org.lfenergy.compas.sct.commons.exception.ScdException;


Expand All @@ -27,7 +22,7 @@ public GooseControlBlock(TGSEControl tgseControl) {
super();
this.id = tgseControl.getAppID();
this.name = tgseControl.getName();
if(tgseControl.getConfRev() != null) {
if(tgseControl.isSetConfRev()) {
this.confRev = tgseControl.getConfRev();
}
this.desc = tgseControl.getDesc();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,7 @@
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.lfenergy.compas.scl2007b4.model.TMcSecurity;
import org.lfenergy.compas.scl2007b4.model.TPredefinedTypeOfSecurityEnum;
import org.lfenergy.compas.scl2007b4.model.TProtocol;
import org.lfenergy.compas.scl2007b4.model.TSampledValueControl;
import org.lfenergy.compas.scl2007b4.model.TServiceType;
import org.lfenergy.compas.scl2007b4.model.TServices;
import org.lfenergy.compas.scl2007b4.model.TSmpMod;
import org.lfenergy.compas.scl2007b4.model.*;
import org.lfenergy.compas.sct.commons.exception.ScdException;

import java.util.Collections;
Expand All @@ -36,9 +30,7 @@ public SMVControlBlock(TSampledValueControl tSampledValueControl) {
super();
this.id = tSampledValueControl.getSmvID();
this.name = tSampledValueControl.getName();
if(tSampledValueControl.getConfRev() != null) {
this.confRev = tSampledValueControl.getConfRev();
}
this.confRev = tSampledValueControl.isSetConfRev() ? tSampledValueControl.getConfRev() : null;
this.desc = tSampledValueControl.getDesc();
this.dataSetRef = tSampledValueControl.getDatSet();
Collections.copy(iedNames, tSampledValueControl.getIEDName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@
import org.lfenergy.compas.sct.commons.scl.dtt.EnumTypeAdapter;
import org.lfenergy.compas.sct.commons.scl.dtt.LNodeTypeAdapter;
import org.lfenergy.compas.sct.commons.scl.header.HeaderAdapter;
import org.lfenergy.compas.sct.commons.scl.ied.AbstractLNAdapter;
import org.lfenergy.compas.sct.commons.scl.ied.DAITracker;
import org.lfenergy.compas.sct.commons.scl.ied.IEDAdapter;
import org.lfenergy.compas.sct.commons.scl.ied.LDeviceAdapter;
import org.lfenergy.compas.sct.commons.scl.ied.*;
import org.lfenergy.compas.sct.commons.scl.sstation.SubstationAdapter;
import org.lfenergy.compas.sct.commons.scl.sstation.VoltageLevelAdapter;

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

public static void removeAllControlBlocksAndDatasetsAndExtRefSrcBindings(final SCL scl) {
SclRootAdapter sclRootAdapter = new SclRootAdapter(scl);
List<LDeviceAdapter> lDeviceAdapters = sclRootAdapter.getIEDAdapters().stream()
.map(IEDAdapter::getLDeviceAdapters).flatMap(List::stream).collect(Collectors.toList());

// LN0
lDeviceAdapters.stream()
.map(LDeviceAdapter::getLN0Adapter)
.forEach(ln0 -> {
ln0.removeAllControlBlocksAndDatasets();
ln0.removeAllExtRefSourceBindings();
});

// Other LN
lDeviceAdapters.stream()
.map(LDeviceAdapter::getLNAdapters).flatMap(List::stream)
.forEach(LNAdapter::removeAllControlBlocksAndDatasets);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,7 @@
package org.lfenergy.compas.sct.commons.scl.dtt;

import lombok.Getter;
import org.lfenergy.compas.scl2007b4.model.TAbstractDataAttribute;
import org.lfenergy.compas.scl2007b4.model.TDA;
import org.lfenergy.compas.scl2007b4.model.TPredefinedBasicTypeEnum;
import org.lfenergy.compas.scl2007b4.model.TProtNs;
import org.lfenergy.compas.scl2007b4.model.TVal;
import org.lfenergy.compas.scl2007b4.model.*;
import org.lfenergy.compas.sct.commons.Utils;
import org.lfenergy.compas.sct.commons.dto.DaTypeName;
import org.lfenergy.compas.sct.commons.exception.ScdException;
Expand Down Expand Up @@ -48,7 +44,6 @@ public Optional<DATypeAdapter> getDATypeAdapter() {


public boolean hasSameContentAs(T data) {
final String countField = "count";
if(!Objects.equals(getName(),data.getName())
|| !Objects.equals(getBType(),data.getBType())
|| !Objects.equals(getType(),data.getType())
Expand All @@ -67,30 +62,21 @@ public boolean hasSameContentAs(T data) {
return false;
}
}
if(!Objects.equals(currentElem.getCount(),data.getCount())){
if(currentElem.getCount().isEmpty()){
Utils.setField(currentElem,countField,null);
}
if(data.getCount().isEmpty()){
Utils.setField(data,countField,null);
}
return false ;
} else if(currentElem.getCount().isEmpty()){
Utils.setField(currentElem,countField,null);
Utils.setField(data,countField,null);
if (!Utils.equalsOrNotSet(currentElem, data, TAbstractDataAttribute::isSetCount, TAbstractDataAttribute::getCount)){
return false;
}


if((getBType() == TPredefinedBasicTypeEnum.ENUM ||
getBType() == TPredefinedBasicTypeEnum.STRUCT)
&& !Objects.equals(getType(),data.getType())) {
return false;
}

for(TVal prdVal : data.getVal()){
boolean hasSameVal = currentElem.getVal().stream()
.anyMatch(rcvVal -> rcvVal.getValue().equals(prdVal.getValue()) &&
Objects.equals(rcvVal.getSGroup(), prdVal.getSGroup()));
if(!hasSameVal) {
if(currentElem.isSetVal() && currentElem.getVal().stream()
.noneMatch(rcvVal -> rcvVal.getValue().equals(prdVal.getValue()) &&
Utils.equalsOrNotSet(rcvVal, prdVal, TVal::isSetSGroup, TVal::getSGroup))) {
return false;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ public <S extends IDataAdapter> S getDataAdapterByName(String sName) throws Scd
public void setValImport(boolean b){
currentElem.setValImport(b);
}

public Boolean isValImport(){
return currentElem.isValImport();
return currentElem.isSetValImport() ? currentElem.isValImport() : null;
}

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

public void update(Long sGroup, String val) throws ScdException {
if(currentElem.isValImport() != null && !currentElem.isValImport()){
if(currentElem.isSetValImport() && !currentElem.isValImport()){
String msg = String.format(
"DAI(%s) cannot be updated : valImport(false)",currentElem.getName()
);
throw new ScdException(msg);
}
Stream<TVal> tValStream = currentElem.getVal().stream() ;
if(sGroup != 0){
Optional<TVal> tVal = tValStream.filter(tValElem -> tValElem.getSGroup() != null &&
tValElem.getSGroup().equals(sGroup))
Stream<TVal> tValStream = currentElem.getVal().stream();
if (sGroup != null && sGroup != 0) {
Optional<TVal> tVal = tValStream.filter(tValElem -> tValElem.isSetSGroup() &&
sGroup.equals(tValElem.getSGroup()))
.findFirst();
if(tVal.isPresent()){
tVal.get().setValue(val);
Expand Down
Loading

0 comments on commit 19605de

Please sign in to comment.