Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
Signed-off-by: Samir Romdhani <samir.romdhani@rte-france.com>
  • Loading branch information
samirromdhani committed Feb 28, 2024
1 parent 60af373 commit 472f363
Show file tree
Hide file tree
Showing 5 changed files with 230 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@

package org.lfenergy.compas.sct.commons;

import lombok.extern.slf4j.Slf4j;
import org.lfenergy.compas.scl2007b4.model.*;
import org.lfenergy.compas.sct.commons.api.LNEditor;
import org.lfenergy.compas.sct.commons.dto.DaTypeName;
import org.lfenergy.compas.sct.commons.dto.DataAttributeRef;
import org.lfenergy.compas.sct.commons.dto.DoTypeName;
import org.lfenergy.compas.sct.commons.dto.SclReportItem;
import org.lfenergy.compas.sct.commons.util.ActiveStatus;

import java.util.*;
Expand All @@ -18,6 +20,7 @@
import static org.lfenergy.compas.sct.commons.util.CommonConstants.STVAL_DA_NAME;
import static org.lfenergy.compas.sct.commons.util.SclConstructorHelper.newVal;

@Slf4j
public class LnService implements LNEditor {

public Stream<TAnyLN> getAnylns(TLDevice tlDevice) {
Expand Down Expand Up @@ -66,7 +69,7 @@ public Stream<TAnyLN> getActiveLns(TLDevice tlDevice) {
return Stream.concat(ln0Stream, tlnStream);
}

public boolean isDoObjectsInstanceAndDataAttributesInstanceExists(TAnyLN tAnyLN, DoTypeName doTypeName, DaTypeName daTypeName) {
public boolean isDOAndDAInstanceExists(TAnyLN tAnyLN, DoTypeName doTypeName, DaTypeName daTypeName) {
return tAnyLN.getDOI().stream().filter(doi -> doTypeName.getName().equals(doi.getName()))
.findFirst()
.map(doi -> {
Expand Down Expand Up @@ -105,7 +108,7 @@ public boolean isDoObjectsInstanceAndDataAttributesInstanceExists(TAnyLN tAnyLN,
.orElse(false);
}

public void updateOrCreateDoObjectsAndDataAttributesInstances(TAnyLN tAnyLN, DataAttributeRef dataAttributeRef) {
public void updateOrCreateDOAndDAInstances(TAnyLN tAnyLN, DataAttributeRef dataAttributeRef) {
createDoiSdiDaiChainIfNotExists(tAnyLN, dataAttributeRef.getDoName(), dataAttributeRef.getDaName())
.ifPresent(tdai -> {
for (Map.Entry<Long, String> mapVal : dataAttributeRef.getDaName().getDaiValues().entrySet()) {
Expand All @@ -126,6 +129,128 @@ public void updateOrCreateDoObjectsAndDataAttributesInstances(TAnyLN tAnyLN, Dat
});
}

@Override
public List<SclReportItem> getDOAndDAInstances(TAnyLN tAnyLN, DataAttributeRef dataAttributeRef) {
List<SclReportItem> sclReportItems = new ArrayList<>();
log.info(":: start ::");
return tAnyLN.getDOI().stream().filter(doi -> dataAttributeRef.getDoName().getName().equals(doi.getName()))
.findFirst()
.map(doi -> {
LinkedList<String> structNamesList = new LinkedList<>(dataAttributeRef.getDoName().getStructNames());
structNamesList.addLast(dataAttributeRef.getDaName().getName());
dataAttributeRef.getDaName().getStructNames().forEach(structNamesList::addLast);
if(structNamesList.size() > 1) {
String firstSDIName = structNamesList.remove();
return doi.getSDIOrDAI().stream()
.filter(sdi -> sdi.getClass().equals(TSDI.class))
.map(TSDI.class::cast)
.filter(sdi -> sdi.getName().equals(firstSDIName))
.findFirst()
.map(intermediateSdi -> findSDIByStructName(intermediateSdi, structNamesList))
.stream()
.findFirst()
.map(lastDsi -> {
if (structNamesList.size() == 1) {
Optional<TDAI> tdai = lastDsi.getSDIOrDAI().stream()
.filter(dai -> dai.getClass().equals(TDAI.class))
.map(TDAI.class::cast)
.filter(dai -> dai.getName().equals(structNamesList.get(0)))
.findFirst();
if(tdai.isPresent()){
log.info(":: DAI FOUND struct case ::");
checkAndCompleteDataAttribute(tdai.get(), dataAttributeRef.getDaName());
return sclReportItems;
}
log.warn("Missing DAI.name=({}) not found in SDI.name=({})",
structNamesList.get(0), lastDsi.getName());
sclReportItems.add(SclReportItem.error(null,
String.format("DAI.name=(%s) not found in SDI.name=(%s)",
structNamesList.get(0), lastDsi.getName())));
return sclReportItems;
}
log.warn("Missing DataAttributes in SDI.name=({})", lastDsi.getName());
sclReportItems.add(SclReportItem.error(null,
String.format("missing DataAttribute in SDI.name=(%s)", lastDsi.getName())));
return sclReportItems;
})
.orElseGet(() -> {
log.warn("Missing SubData Object Instance SDI or Data Attribute Instance DAI ({}) in DOI.name ({})",
dataAttributeRef.getDoName().getStructNames(), doi.getName());
sclReportItems.add(SclReportItem.error(null,
String.format("Missing SubData Object Instance SDI or Data Attribute Instance DAI (%s) in DOI.name (%s)",
dataAttributeRef.getDoName().getStructNames(), doi.getName())));
return sclReportItems;
});
} else if(structNamesList.size() == 1){
Optional<TDAI> tdai = doi.getSDIOrDAI().stream()
.filter(unNaming -> unNaming.getClass().equals(TDAI.class))
.map(TDAI.class::cast)
.filter(dai -> dai.getName().equals(structNamesList.get(0)))
.findFirst();
if(tdai.isPresent()){
log.info(":: DAI FOUND sample case ::");
checkAndCompleteDataAttribute(tdai.get(), dataAttributeRef.getDaName());
} else {
log.warn("Missing DAI.name=({}) not found in DOI.name=({})", structNamesList.get(0), doi.getName());
sclReportItems.add(SclReportItem.error(null,
String.format("DAI.name=(%s) not found in DOI.name=(%s)", structNamesList.get(0), doi.getName())));
}
return sclReportItems;
}
return sclReportItems;
})
.orElseGet(() -> {
//error
log.warn("DOI.name=({}) not found in LN.type=({})", dataAttributeRef.getDoName(), tAnyLN.getLnType());
sclReportItems.add(SclReportItem.error(null,String.format("DOI.name=(%s) not found in LN.type=(%s)",
dataAttributeRef.getDoName(), tAnyLN.getLnType())));
return sclReportItems;
});
}

private void checkAndCompleteDataAttribute(TDAI tdai, DaTypeName daTypeName) {
daTypeName.addDaiValues(tdai.getVal());
if (daTypeName.getFc() == TFCEnum.SG || daTypeName.getFc() == TFCEnum.SE) {
boolean isGroup = hasSgGroup(tdai);
if (isGroup) {
daTypeName.setValImport((!tdai.isSetValImport() || tdai.isValImport())
// && iedHasConfSG()
);
} else {
daTypeName.setValImport(false);
log.warn("Inconsistency in the SCD file - DAI ?? with fc=?? must have a sGroup attribute");
// log.warn("Inconsistency in the SCD file - DAI {} with fc={} must have a sGroup attribute",
// dataAttributeRef.getObjRef(getParentIed().getName(), parentAdapter.getInst()),
// dataAttributeRef.getDaName().getFc());
}
} else if (tdai.isSetValImport()) {
daTypeName.setValImport(tdai.isValImport());
}
}

private boolean hasSgGroup(TDAI tdai) {
return tdai.getVal().stream().anyMatch(tVal -> tVal.isSetSGroup() && tVal.getSGroup() > 0);
}

public boolean iedHasConfSG(TIED tied, TLDevice tlDevice) {
TAccessPoint accessPoint = tied.getAccessPoint().stream()
.filter(tAccessPoint ->
(tAccessPoint.getServer() != null) &&
tAccessPoint.getServer().getLDevice().stream()
.anyMatch(tlDevice1 -> tlDevice1.getInst().equals(tlDevice.getInst()))

)
.findFirst()
.orElseThrow(
() -> new IllegalArgumentException(
String.format("LD (%s) is unknown in %s", tlDevice.getInst(), tlDevice.getLdName())
)
);

TServices srv = accessPoint.getServices();
return srv != null && srv.getSettingGroups() != null && srv.getSettingGroups().getConfSG() != null;
}

private TSDI findSDIByStructName(TSDI tsdi, List<String> structNames) {
if(structNames.isEmpty()) return tsdi;
return tsdi.getSDIOrDAI().stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,16 @@
import org.lfenergy.compas.sct.commons.dto.DaTypeName;
import org.lfenergy.compas.sct.commons.dto.DataAttributeRef;
import org.lfenergy.compas.sct.commons.dto.DoTypeName;
import org.lfenergy.compas.sct.commons.dto.SclReportItem;

import java.util.List;

public interface LNEditor {

boolean isDoObjectsInstanceAndDataAttributesInstanceExists(TAnyLN tAnyLN, DoTypeName doTypeName, DaTypeName daTypeName);
boolean isDOAndDAInstanceExists(TAnyLN tAnyLN, DoTypeName doTypeName, DaTypeName daTypeName);

List<SclReportItem> getDOAndDAInstances(TAnyLN tAnyLN, DataAttributeRef dataAttributeRef);

void updateOrCreateDOAndDAInstances(TAnyLN tAnyLN, DataAttributeRef dataAttributeRef);

void updateOrCreateDoObjectsAndDataAttributesInstances(TAnyLN tAnyLN, DataAttributeRef dataAttributeRef);
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.lfenergy.compas.sct.commons.dto.DoTypeName;
import org.lfenergy.compas.sct.commons.dto.SclReportItem;
import org.lfenergy.compas.sct.commons.scl.dtt.DataTypeTemplateAdapter;
import org.lfenergy.compas.sct.commons.testhelpers.SclTestMarshaller;

import java.util.List;

Expand Down Expand Up @@ -149,7 +150,7 @@ void isDoModAndDaStValExist_when_DO_Mod_And_DA_stVal_exist_return_true() {


@Test
void isDataAttributeExist2_should_find_DO_SDO_DA_and_BDA() {
void isDataObjectsAndDataAttributesExists_should_find_DO_SDO_DA_and_BDA() {
// Given
DataTypeTemplateAdapter dttAdapter = initDttAdapterFromFile(SCD_DTT_DO_SDO_DA_BDA);
// When
Expand All @@ -171,6 +172,35 @@ void isDataAttributeExist2_should_find_DO_SDO_DA_and_BDA() {
.containsExactly(TPredefinedBasicTypeEnum.ENUM, TFCEnum.ST);
}


@Test
void isDataObjectsAndDataAttributesExists_should_find_DO_SDO_DA_and_BDA_test2() {
// Given
// DataTypeTemplateAdapter dttAdapter = initDttAdapterFromFile(SCD_DTT_DO_SDO_DA_BDA);
SCL scd = SclTestMarshaller.getSCLFromFile("/ied-test-schema-conf/ied_unit_test.xml");
TDataTypeTemplates dtt = scd.getDataTypeTemplates();
// When
DataTypeTemplatesService dataTypeTemplatesService = new DataTypeTemplatesService();
String dataRef = "Do.sdo1.d.antRef.bda1.bda2.bda3";
DataAttributeRef dataAttributeRefs = dataTypeTemplatesService.getDataObjectsAndDataAttributes(
dtt, "LNO1", dataRef);
// Then
assertThatCode(() -> dataTypeTemplatesService.getDataObjectsAndDataAttributes(
dtt, "LNO1", dataRef))
.doesNotThrowAnyException();

assertThat(dataAttributeRefs.getDoRef()).isEqualTo("Do.sdo1.d");
assertThat(dataAttributeRefs.getDaRef()).isEqualTo("antRef.bda1.bda2.bda3");
assertThat(dataAttributeRefs).extracting(DataAttributeRef::getDoRef, DataAttributeRef::getDaRef)
.containsExactly("Do.sdo1.d", "antRef.bda1.bda2.bda3");
assertThat(dataAttributeRefs.getDoName().getCdc()).isEqualTo(TPredefinedCDCEnum.WYE);
assertThat(dataAttributeRefs.getDaName()).extracting(DaTypeName::getBType, DaTypeName::getFc)
.containsExactly(TPredefinedBasicTypeEnum.ENUM, TFCEnum.ST);
assertThat(dataAttributeRefs.getDaName().isValImport()).isEqualTo(true);
assertThat(dataAttributeRefs.getDaName().isUpdatable()).isEqualTo(true);
}


@Test
void isDoObjectsAndDataAttributesExists_when_LNodeType_not_exist_should_return_error_report_item() {
// Given
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@
import org.lfenergy.compas.sct.commons.dto.DaTypeName;
import org.lfenergy.compas.sct.commons.dto.DataAttributeRef;
import org.lfenergy.compas.sct.commons.dto.DoTypeName;
import org.lfenergy.compas.sct.commons.dto.SclReportItem;
import org.lfenergy.compas.sct.commons.testhelpers.SclTestMarshaller;
import org.lfenergy.compas.sct.commons.util.ActiveStatus;

import java.util.List;
import java.util.Optional;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatCode;
import static org.lfenergy.compas.sct.commons.testhelpers.DataTypeUtils.*;

class LnServiceTest {
Expand Down Expand Up @@ -85,7 +87,7 @@ void getActiveLns_should_return_lns() {
}

@Test
void isDoObjectsInstanceAndDataAttributesInstanceExists_should_return_true_when_DO_and_DA_instances_exists() {
void isDOAndDAInstanceExists_should_return_true_when_DO_and_DA_instances_exists() {
//Given
SCL scd = SclTestMarshaller.getSCLFromFile("/ied-test-schema-conf/ied_unit_test.xml");
TAnyLN tAnyLN = scd.getIED().stream()
Expand All @@ -101,14 +103,14 @@ void isDoObjectsInstanceAndDataAttributesInstanceExists_should_return_true_when_
DaTypeName daTypeName = new DaTypeName("antRef.bda1.bda2.bda3");
//When
LnService lnService = new LnService();
boolean exist = lnService.isDoObjectsInstanceAndDataAttributesInstanceExists(tAnyLN, doTypeName, daTypeName);
boolean exist = lnService.isDOAndDAInstanceExists(tAnyLN, doTypeName, daTypeName);
//Then
assertThat(exist).isTrue();
}


@Test
void isDoObjectsInstanceAndDataAttributesInstanceExists_should_return_false_when_DO_and_DA_instances_not_exists() {
void isDOAndDAInstanceExists_should_return_false_when_DO_and_DA_instances_not_exists() {
//Given
SCL scd = SclTestMarshaller.getSCLFromFile("/ied-test-schema-conf/ied_unit_test.xml");
TAnyLN tAnyLN = scd.getIED().stream()
Expand All @@ -124,11 +126,66 @@ void isDoObjectsInstanceAndDataAttributesInstanceExists_should_return_false_when
DaTypeName daTypeName = new DaTypeName("antRef.unknown.bda2.bda3");
//When
LnService lnService = new LnService();
boolean exist = lnService.isDoObjectsInstanceAndDataAttributesInstanceExists(tAnyLN, doTypeName, daTypeName);
boolean exist = lnService.isDOAndDAInstanceExists(tAnyLN, doTypeName, daTypeName);
//Then
assertThat(exist).isFalse();
}


private TAnyLN initDOAndDAInstances(List<String> doInstances, List<String> daInstances){
TLN0 tln0 = new TLN0();
// TDOI
// tln0.getDOI().add()
// doInstances.g
return tln0;
}

@Test
void getDOAndDAInstances_should_return_when_ADF() {
//Given
SCL scd = SclTestMarshaller.getSCLFromFile("/ied-test-schema-conf/ied_unit_test.xml");
TAnyLN tAnyLN = scd.getIED().stream()
.filter(tied -> tied.getName().equals("IED_NAME")).findFirst().get()
.getAccessPoint()
.get(0)
.getServer()
.getLDevice().stream()
.filter(tlDevice -> tlDevice.getInst().equals("LD_INS1")).findFirst()
.get()
.getLN0();
DoTypeName doTypeName = new DoTypeName("Do.sdo1.d");
doTypeName.setCdc(TPredefinedCDCEnum.WYE);
DaTypeName daTypeName = new DaTypeName("antRef.bda1.bda2.bda3");
daTypeName.setFc(TFCEnum.ST);
daTypeName.setBType(TPredefinedBasicTypeEnum.ENUM);
daTypeName.setValImport(true);

DataAttributeRef dataAttributeRef = new DataAttributeRef();
dataAttributeRef.setDoName(doTypeName);
dataAttributeRef.setDaName(daTypeName);

assertThat(daTypeName.isValImport()).isEqualTo(true);
assertThat(daTypeName.isUpdatable()).isEqualTo(true);
//When
LnService lnService = new LnService();
// assertThatCode(() -> lnService.getDOAndDAInstances(tAnyLN, dataAttributeRef))
// .doesNotThrowAnyException();
List<SclReportItem> sclReportItems = lnService.getDOAndDAInstances(tAnyLN, dataAttributeRef);
//Then
assertThat(sclReportItems).isEmpty();
assertThat(dataAttributeRef.getDoRef()).isEqualTo("Do.sdo1.d");
assertThat(dataAttributeRef.getDaRef()).isEqualTo("antRef.bda1.bda2.bda3");
assertThat(dataAttributeRef.getDaName().isValImport()).isEqualTo(false);
assertThat(dataAttributeRef.getDaName().isUpdatable()).isEqualTo(false);
assertThat(dataAttributeRef.getDoName())
.usingRecursiveComparison()
.isEqualTo(doTypeName);
assertThat(dataAttributeRef.getDaName())
.usingRecursiveComparison()
.ignoringFields("valImport","daiValues")
.isEqualTo(daTypeName);
}

@Test
void updateOrCreateDoObjectsAndDataAttributesInstances_should_create_given_DO_and_DA_instances_when_no_struct_and_with_settingGroup() {
//Given
Expand All @@ -142,7 +199,7 @@ void updateOrCreateDoObjectsAndDataAttributesInstances_should_create_given_DO_an

//When
LnService lnService = new LnService();
lnService.updateOrCreateDoObjectsAndDataAttributesInstances(tAnyLN, dataAttributeRef);
lnService.updateOrCreateDOAndDAInstances(tAnyLN, dataAttributeRef);
//Then
assertThat(tAnyLN.getDOI()).hasSize(1);
assertThat(tAnyLN.getDOI().get(0).getName()).isEqualTo("Mod");
Expand All @@ -167,7 +224,7 @@ void updateOrCreateDoObjectsAndDataAttributesInstances_should_create_given_DO_an

//When
LnService lnService = new LnService();
lnService.updateOrCreateDoObjectsAndDataAttributesInstances(tAnyLN, dataAttributeRef);
lnService.updateOrCreateDOAndDAInstances(tAnyLN, dataAttributeRef);

//Then
assertThat(tAnyLN.getDOI()).hasSize(1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<SDI name="antRef"> <!-- this is a DA -->
<SDI name="bda1">
<SDI name="bda2">
<DAI name="bda3" valImport="true"> <!-- [Do.sdo1.d] && [antRef.bda1.bda2.bda3] -->
<DAI name="bda3" valImport="false"> <!-- [Do.sdo1.d] && [antRef.bda1.bda2.bda3] -->
<Val>Completed-diff</Val>
</DAI>
</SDI>
Expand Down

0 comments on commit 472f363

Please sign in to comment.