From e682b263e8535f564c734d46964ef538ced26131 Mon Sep 17 00:00:00 2001 From: Samir Romdhani Date: Thu, 18 Jan 2024 15:34:20 +0100 Subject: [PATCH] feat(#373): review note Signed-off-by: Samir Romdhani --- .../compas/sct/app/SclAutomationService.java | 10 --- .../compas/sct/commons/SclService.java | 63 +++++-------------- .../compas/sct/commons/dto/SCDBuilder.java | 0 .../compas/sct/commons/dto/SCDRoot.java | 19 ++++++ .../commons/scl/com/ConnectedAPAdapter.java | 2 +- .../compas/sct/commons/SclServiceTest.java | 12 ++-- 6 files changed, 41 insertions(+), 65 deletions(-) create mode 100644 sct-commons/src/main/java/org/lfenergy/compas/sct/commons/dto/SCDBuilder.java create mode 100644 sct-commons/src/main/java/org/lfenergy/compas/sct/commons/dto/SCDRoot.java diff --git a/sct-app/src/main/java/org/lfenergy/compas/sct/app/SclAutomationService.java b/sct-app/src/main/java/org/lfenergy/compas/sct/app/SclAutomationService.java index 38ace789a..43b0fadde 100644 --- a/sct-app/src/main/java/org/lfenergy/compas/sct/app/SclAutomationService.java +++ b/sct-app/src/main/java/org/lfenergy/compas/sct/app/SclAutomationService.java @@ -11,8 +11,6 @@ import org.lfenergy.compas.sct.commons.api.SclEditor; import org.lfenergy.compas.sct.commons.api.SubstationEditor; import org.lfenergy.compas.sct.commons.dto.HeaderDTO; -import org.lfenergy.compas.sct.commons.dto.SubNetworkDTO; -import org.lfenergy.compas.sct.commons.dto.SubNetworkTypeDTO; import org.lfenergy.compas.sct.commons.exception.ScdException; import java.util.*; @@ -33,14 +31,6 @@ public class SclAutomationService { private final SubstationEditor substationEditor; private final ControlBlockEditor controlBlockEditor; - /** - * Possible Subnetwork and ConnectedAP names which should be used in generated SCD in order a have global coherence - * Configuration based on used framework can be used to externalize this datas - */ - public static final List SUB_NETWORK_TYPES = List.of( - new SubNetworkTypeDTO("RSPACE_PROCESS_NETWORK", SubNetworkDTO.SubnetworkType.MMS.toString(), List.of("PROCESS_AP", "TOTO_AP_GE")), - new SubNetworkTypeDTO("RSPACE_ADMIN_NETWORK", SubNetworkDTO.SubnetworkType.IP.toString(), List.of("ADMIN_AP", "TATA_AP_EFFACEC"))); - /** * Create an SCD file from specified parameters, it calls all functions defined in the process one by one, every step * return an SCD file which will be used by the next step. diff --git a/sct-commons/src/main/java/org/lfenergy/compas/sct/commons/SclService.java b/sct-commons/src/main/java/org/lfenergy/compas/sct/commons/SclService.java index e04acc548..93e35bb44 100644 --- a/sct-commons/src/main/java/org/lfenergy/compas/sct/commons/SclService.java +++ b/sct-commons/src/main/java/org/lfenergy/compas/sct/commons/SclService.java @@ -104,54 +104,21 @@ public void addSubnetworks(SCL scd, List subNetworks, SCL icd) th } @Override - public void addSubnetworks(SCL scd, SCL std, String iedName) throws ScdException { - Optional.ofNullable(std.getCommunication()).ifPresent(tCommunication -> - tCommunication.getSubNetwork().forEach(stdSubNetwork -> - stdSubNetwork.getConnectedAP().forEach(stdConnectedAP -> { - // verify if exist SCD/IED/ConnectedAP/apName equal to STD/Communication/SubNetwork/ConnectedAP/apName - if (scd.getIED().stream().filter(ied -> Objects.equals(ied.getName(), iedName)) - .flatMap(tied -> tied.getAccessPoint().stream()) - .noneMatch(tAccessPoint -> tAccessPoint.getName().equals(stdConnectedAP.getApName()))) { - throw new ScdException("Unknown AccessPoint :" + stdConnectedAP.getApName() + " in IED :" + iedName); - } - // add SubNetwork if not exist - if(scd.getCommunication() == null) { - scd.setCommunication(new TCommunication()); - } - TSubNetwork tSubNetwork = scd.getCommunication().getSubNetwork().stream() - .filter(subNetwork -> subNetwork.getName().equals(stdSubNetwork.getName())) - .findFirst() - .orElseGet(() -> { - TSubNetwork newSubNetwork = new TSubNetwork(); - newSubNetwork.setName(stdSubNetwork.getName()); - newSubNetwork.setType(stdSubNetwork.getType()); - scd.getCommunication().getSubNetwork().add(newSubNetwork); - return newSubNetwork; - }); - // add ConnectedAP to SubNetwork if not exist - String apName = stdConnectedAP.getApName(); - TConnectedAP tConnectedAP = tSubNetwork.getConnectedAP().stream() - .filter(connectedAP -> Objects.equals(connectedAP.getIedName(), iedName) - && Objects.equals(connectedAP.getApName(), apName)) - .findFirst() - .orElseGet(() -> { - TConnectedAP newConnectedAP = new TConnectedAP(); - newConnectedAP.setIedName(iedName); - newConnectedAP.setApName(apName); - tSubNetwork.getConnectedAP().add(newConnectedAP); - return newConnectedAP; - }); - //copy Address And PhysConn From Icd to Scd - std.getCommunication().getSubNetwork().stream() - .flatMap(subNetwork -> subNetwork.getConnectedAP().stream()) - .filter(connectedAP -> connectedAP.getApName().equals(tConnectedAP.getApName())) - .findFirst() - .ifPresent(connectedAP -> { - Optional.ofNullable(connectedAP.getAddress()).ifPresent(tConnectedAP::setAddress); - tConnectedAP.getPhysConn().addAll(connectedAP.getPhysConn()); - }); - })) - ); + public void addSubnetworks(SCL scd, SCL icd, String iedName) throws ScdException { + SclRootAdapter sclRootAdapter = new SclRootAdapter(scd); + Optional.ofNullable(icd.getCommunication()).ifPresent(tCommunication -> + tCommunication.getSubNetwork().forEach(icdSubNetwork -> + icdSubNetwork.getConnectedAP().forEach(icdConnectedAP -> { + // init Communication if not exist + CommunicationAdapter communicationAdapter = sclRootAdapter.getCommunicationAdapter(true); + // add SubNetwork if not exist, add ConnectedAP to SubNetwork if not exist + SubNetworkAdapter subNetworkAdapter = communicationAdapter + .addSubnetwork(icdSubNetwork.getName(), icdSubNetwork.getType(), iedName, icdConnectedAP.getApName()); + // copy Address And PhysConn From Icd to Scd + subNetworkAdapter.getConnectedAPAdapter(iedName, icdConnectedAP.getApName()) + .copyAddressAndPhysConnFromIcd(icd); + }) + )); } @Override diff --git a/sct-commons/src/main/java/org/lfenergy/compas/sct/commons/dto/SCDBuilder.java b/sct-commons/src/main/java/org/lfenergy/compas/sct/commons/dto/SCDBuilder.java new file mode 100644 index 000000000..e69de29bb diff --git a/sct-commons/src/main/java/org/lfenergy/compas/sct/commons/dto/SCDRoot.java b/sct-commons/src/main/java/org/lfenergy/compas/sct/commons/dto/SCDRoot.java new file mode 100644 index 000000000..5f8d6828d --- /dev/null +++ b/sct-commons/src/main/java/org/lfenergy/compas/sct/commons/dto/SCDRoot.java @@ -0,0 +1,19 @@ +package org.lfenergy.compas.sct.commons.dto; + +import lombok.Builder; +import lombok.Getter; +import lombok.NonNull; +import org.lfenergy.compas.sct.commons.scl.SclRootAdapter; + +@Builder(toBuilder = true) +@Getter +public class SCDRoot { + + @NonNull + final SclRootAdapter scdRootAdapter ; + +// public static SCDRootBuilder builder(final SclRootAdapter scdRootAdapter) { +// return new SCDRoot(scdRootAdapter); +// } + +} diff --git a/sct-commons/src/main/java/org/lfenergy/compas/sct/commons/scl/com/ConnectedAPAdapter.java b/sct-commons/src/main/java/org/lfenergy/compas/sct/commons/scl/com/ConnectedAPAdapter.java index 3569f4ef6..f5ef69d7d 100644 --- a/sct-commons/src/main/java/org/lfenergy/compas/sct/commons/scl/com/ConnectedAPAdapter.java +++ b/sct-commons/src/main/java/org/lfenergy/compas/sct/commons/scl/com/ConnectedAPAdapter.java @@ -84,7 +84,7 @@ public void copyAddressAndPhysConnFromIcd(SCL icd) { .filter(connectedAP -> connectedAP.getApName().equals(currentElem.getApName())) .findFirst() .ifPresent(connectedAP -> { - currentElem.setAddress(connectedAP.getAddress()); + Optional.ofNullable(connectedAP.getAddress()).ifPresent(currentElem::setAddress); currentElem.getPhysConn().addAll(connectedAP.getPhysConn()); }); } diff --git a/sct-commons/src/test/java/org/lfenergy/compas/sct/commons/SclServiceTest.java b/sct-commons/src/test/java/org/lfenergy/compas/sct/commons/SclServiceTest.java index 5620f4414..dafaf271a 100644 --- a/sct-commons/src/test/java/org/lfenergy/compas/sct/commons/SclServiceTest.java +++ b/sct-commons/src/test/java/org/lfenergy/compas/sct/commons/SclServiceTest.java @@ -173,8 +173,9 @@ void addSubnetworks_whenNoCommunicationTagInIcd_should_not_add_subnetwork() { } @Test - void addSubnetworks_shouldNotUpdateScd_when_noCommunicationInICDExist() { + void addSubnetworks_shouldNotUpdateScd_when_noCommunicationInSTDExist() { //Givens + SCL initial = SclTestMarshaller.getSCLFromFile("/scl_update_communication/scd_without_communication.xml"); SCL scd = SclTestMarshaller.getSCLFromFile("/scl_update_communication/scd_without_communication.xml"); SCL icd = SclTestMarshaller.getSCLFromFile("/scl_update_communication/std_without_communication.xml"); assertThat(scd.getCommunication()).isNull(); @@ -211,8 +212,8 @@ void addSubnetworks_shouldAddSubNetwork_and_ConnectedAp_and_updateConnectedApIED @Test - void addSubnetworks_shouldCopyAddressAndPhysConnFromIcd() { - //Givens + void addSubnetworks_shouldCopyAddressAndPhysConnFromStd() { + //Given SCL scd = SclTestMarshaller.getSCLFromFile("/scl_update_communication/scd_without_communication.xml"); SCL std = SclTestMarshaller.getSCLFromFile("/scl_update_communication/std_with_full_filled_communication.xml"); assertThat(scd.getCommunication()).isNull(); @@ -246,15 +247,14 @@ void addSubnetworks_shouldDoNothing_when_subNetworkAlreadyExist() { @Test void addSubnetworks_shouldThrowError_When_IedNameNotExistInScd() { - //Givens + //Given SCL scd = SclTestMarshaller.getSCLFromFile("/scl_update_communication/scd_without_communication.xml"); SCL icd = SclTestMarshaller.getSCLFromFile("/scl_update_communication/std_with_communication.xml"); //When //Then assertThatCode(() -> sclService.addSubnetworks(scd, icd, "UnknownIedName")) .isInstanceOf(ScdException.class) - .hasMessage("Unknown AccessPoint :ConnectedAP_Name in IED :UnknownIedName"); - assertIsMarshallable(scd); + .hasMessage("IED.name 'UnknownIedName' not found in SCD"); } @Test