|
| 1 | +/** |
| 2 | + * Copyright (c) 2025, RTE (http://www.rte-france.com) |
| 3 | + * This Source Code Form is subject to the terms of the Mozilla Public |
| 4 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 | + * file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 6 | + */ |
| 7 | +package org.gridsuite.modification.modifications; |
| 8 | + |
| 9 | +import com.powsybl.commons.report.ReportNode; |
| 10 | +import com.powsybl.iidm.network.*; |
| 11 | +import com.powsybl.iidm.network.extensions.ConnectablePosition; |
| 12 | +import com.powsybl.iidm.network.extensions.ConnectablePositionAdder; |
| 13 | +import org.gridsuite.modification.ModificationType; |
| 14 | +import org.gridsuite.modification.NetworkModificationException; |
| 15 | +import org.gridsuite.modification.dto.*; |
| 16 | +import org.gridsuite.modification.utils.ModificationUtils; |
| 17 | + |
| 18 | +import static org.gridsuite.modification.NetworkModificationException.Type.MOVE_VOLTAGE_LEVEL_FEEDER_BAYS_ERROR; |
| 19 | + |
| 20 | +/** |
| 21 | + * @author Etienne Lesot <etienne.lesot at rte-france.com> |
| 22 | + */ |
| 23 | +public class MoveVoltageLevelFeederBays extends AbstractModification { |
| 24 | + private static final String VOLTAGE_LEVEL_NOT_FOUND = "Voltage level %s is not found"; |
| 25 | + private static final String CONNECTABLE_NOT_FOUND = "Connectable %s not found"; |
| 26 | + private static final String BUSBAR_NOT_FOUND = "Bus or busbar section %s where connectable %s is supposed to be is not found in voltage level %s"; |
| 27 | + private static final String UNSUPPORTED_CONNECTABLE = "MoveVoltageLevelFeederBays is not implemented for %s"; |
| 28 | + private static final String INVALID_CONNECTION_SIDE = "Invalid connection side: %s for branch %s"; |
| 29 | + |
| 30 | + private final MoveVoltageLevelFeederBaysInfos modificationInfos; |
| 31 | + |
| 32 | + public MoveVoltageLevelFeederBays(MoveVoltageLevelFeederBaysInfos modificationInfos) { |
| 33 | + this.modificationInfos = modificationInfos; |
| 34 | + } |
| 35 | + |
| 36 | + @Override |
| 37 | + public void check(Network network) throws NetworkModificationException { |
| 38 | + VoltageLevel voltageLevel = getVoltageLevelOrThrow(network, modificationInfos.getVoltageLevelId()); |
| 39 | + for (MoveFeederBayInfos info : modificationInfos.getFeederBays()) { |
| 40 | + checkBusOrBusbarSectionExist(voltageLevel, info); |
| 41 | + checkConnectable(network, info); |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + private VoltageLevel getVoltageLevelOrThrow(Network network, String voltageLevelId) { |
| 46 | + VoltageLevel voltageLevel = network.getVoltageLevel(voltageLevelId); |
| 47 | + if (voltageLevel == null) { |
| 48 | + throw new NetworkModificationException(MOVE_VOLTAGE_LEVEL_FEEDER_BAYS_ERROR, String.format(VOLTAGE_LEVEL_NOT_FOUND, voltageLevelId)); |
| 49 | + } |
| 50 | + return voltageLevel; |
| 51 | + } |
| 52 | + |
| 53 | + private void checkBusOrBusbarSectionExist(VoltageLevel voltageLevel, MoveFeederBayInfos info) { |
| 54 | + boolean busOrBusbarSectionExists = voltageLevel.getTopologyKind().equals(TopologyKind.NODE_BREAKER) |
| 55 | + ? voltageLevel.getNodeBreakerView().getBusbarSection(info.getBusbarSectionId()) != null |
| 56 | + : voltageLevel.getBusBreakerView().getBus(info.getBusbarSectionId()) != null; |
| 57 | + if (!busOrBusbarSectionExists) { |
| 58 | + throw new NetworkModificationException(MOVE_VOLTAGE_LEVEL_FEEDER_BAYS_ERROR, String.format(BUSBAR_NOT_FOUND, |
| 59 | + info.getBusbarSectionId(), info.getEquipmentId(), modificationInfos.getVoltageLevelId())); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + private void checkConnectable(Network network, MoveFeederBayInfos info) { |
| 64 | + Connectable<?> connectable = network.getConnectable(info.getEquipmentId()); |
| 65 | + if (connectable == null) { |
| 66 | + throw new NetworkModificationException(MOVE_VOLTAGE_LEVEL_FEEDER_BAYS_ERROR, String.format(CONNECTABLE_NOT_FOUND, info.getEquipmentId())); |
| 67 | + } |
| 68 | + if (connectable instanceof BusbarSection || connectable instanceof ThreeWindingsTransformer) { |
| 69 | + throw new NetworkModificationException(MOVE_VOLTAGE_LEVEL_FEEDER_BAYS_ERROR, String.format(UNSUPPORTED_CONNECTABLE, connectable.getClass())); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + public void apply(Network network, ReportNode subReportNode) { |
| 75 | + for (MoveFeederBayInfos info : modificationInfos.getFeederBays()) { |
| 76 | + Connectable<?> connectable = network.getConnectable(info.getEquipmentId()); |
| 77 | + modifyConnectablePosition(network, connectable, info, subReportNode); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + public String getName() { |
| 83 | + return ModificationType.MOVE_VOLTAGE_LEVEL_FEEDER_BAYS.name(); |
| 84 | + } |
| 85 | + |
| 86 | + private void modifyConnectablePosition(Network network, Connectable<?> connectable, MoveFeederBayInfos newConnectablePositionInfos, ReportNode subReportNode) { |
| 87 | + ConnectablePosition<?> oldConnectablePosition = (ConnectablePosition<?>) connectable.getExtension(ConnectablePosition.class); |
| 88 | + ConnectablePositionAdder<?> connectablePositionAdder = connectable.newExtension(ConnectablePositionAdder.class); |
| 89 | + |
| 90 | + switch (connectable) { |
| 91 | + case Injection<?> injection -> { |
| 92 | + InjectionModificationInfos injectionModificationInfos = buildInjectionModificationInfos(newConnectablePositionInfos); |
| 93 | + ModificationUtils.getInstance().modifyInjectionConnectivityAttributes(oldConnectablePosition, connectablePositionAdder, injection, injectionModificationInfos, subReportNode); |
| 94 | + } |
| 95 | + case Branch<?> branch -> { |
| 96 | + BranchModificationInfos branchModificationInfos = buildBranchModificationInfos(newConnectablePositionInfos); |
| 97 | + ModificationUtils.getInstance().modifyBranchConnectivityAttributes(oldConnectablePosition, connectablePositionAdder, branch, branchModificationInfos, subReportNode); |
| 98 | + } |
| 99 | + default -> throw new NetworkModificationException(MOVE_VOLTAGE_LEVEL_FEEDER_BAYS_ERROR, String.format(UNSUPPORTED_CONNECTABLE, connectable.getClass())); |
| 100 | + } |
| 101 | + moveFeederBay(network, connectable, newConnectablePositionInfos, subReportNode); |
| 102 | + } |
| 103 | + |
| 104 | + private InjectionModificationInfos buildInjectionModificationInfos(MoveFeederBayInfos newConnectablePositionInfos) { |
| 105 | + InjectionModificationInfos injectionInfos = new InjectionModificationInfos(); |
| 106 | + injectionInfos.setEquipmentId(newConnectablePositionInfos.getEquipmentId()); |
| 107 | + setConnectionAttributes(injectionInfos::setConnectionPosition, |
| 108 | + injectionInfos::setConnectionName, |
| 109 | + injectionInfos::setConnectionDirection, |
| 110 | + newConnectablePositionInfos); |
| 111 | + return injectionInfos; |
| 112 | + } |
| 113 | + |
| 114 | + private BranchModificationInfos buildBranchModificationInfos(MoveFeederBayInfos info) { |
| 115 | + BranchModificationInfos branchInfos = new BranchModificationInfos(); |
| 116 | + branchInfos.setEquipmentId(info.getEquipmentId()); |
| 117 | + ThreeSides connectionSide = ThreeSides.valueOf(info.getConnectionSide()); |
| 118 | + switch (connectionSide) { |
| 119 | + case ONE -> setConnectionAttributes(branchInfos::setConnectionPosition1, |
| 120 | + branchInfos::setConnectionName1, |
| 121 | + branchInfos::setConnectionDirection1, |
| 122 | + info); |
| 123 | + case TWO -> setConnectionAttributes(branchInfos::setConnectionPosition2, |
| 124 | + branchInfos::setConnectionName2, |
| 125 | + branchInfos::setConnectionDirection2, |
| 126 | + info); |
| 127 | + default -> throw new NetworkModificationException(MOVE_VOLTAGE_LEVEL_FEEDER_BAYS_ERROR, String.format(INVALID_CONNECTION_SIDE, info.getConnectionSide(), branchInfos.getEquipmentId())); |
| 128 | + } |
| 129 | + return branchInfos; |
| 130 | + } |
| 131 | + |
| 132 | + private void setConnectionAttributes(java.util.function.Consumer<AttributeModification<Integer>> setPosition, |
| 133 | + java.util.function.Consumer<AttributeModification<String>> setName, |
| 134 | + java.util.function.Consumer<AttributeModification<ConnectablePosition.Direction>> setDirection, |
| 135 | + MoveFeederBayInfos info) { |
| 136 | + setPosition.accept(new AttributeModification<>(info.getConnectionPosition(), OperationType.SET)); |
| 137 | + setName.accept(new AttributeModification<>(info.getConnectionName(), OperationType.SET)); |
| 138 | + setDirection.accept(new AttributeModification<>(info.getConnectionDirection(), OperationType.SET)); |
| 139 | + } |
| 140 | + |
| 141 | + private void moveFeederBay(Network network, Connectable<?> connectable, MoveFeederBayInfos info, ReportNode subReportNode) { |
| 142 | + Terminal terminal = getTerminal(network, info); |
| 143 | + String currentBusbarId = ModificationUtils.getInstance().getBusOrBusbarSection(terminal); |
| 144 | + String targetBusbarId = info.getBusbarSectionId(); |
| 145 | + if (!currentBusbarId.equals(targetBusbarId)) { |
| 146 | + ModificationUtils.getInstance().moveFeederBay( |
| 147 | + connectable, terminal, |
| 148 | + new AttributeModification<>(modificationInfos.getVoltageLevelId(), OperationType.SET), |
| 149 | + new AttributeModification<>(targetBusbarId, OperationType.SET), |
| 150 | + subReportNode); |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + public Terminal getTerminal(Network network, MoveFeederBayInfos info) { |
| 155 | + Connectable<?> connectable = network.getConnectable(info.getEquipmentId()); |
| 156 | + return switch (connectable) { |
| 157 | + case Injection<?> injection -> injection.getTerminal(); |
| 158 | + case Branch<?> branch -> { |
| 159 | + try { |
| 160 | + TwoSides side = TwoSides.valueOf(info.getConnectionSide()); |
| 161 | + yield branch.getTerminal(side); |
| 162 | + } catch (IllegalArgumentException e) { |
| 163 | + throw new NetworkModificationException(MOVE_VOLTAGE_LEVEL_FEEDER_BAYS_ERROR, |
| 164 | + String.format(INVALID_CONNECTION_SIDE, info.getConnectionSide(), branch.getId())); |
| 165 | + } |
| 166 | + } |
| 167 | + default -> throw new NetworkModificationException(MOVE_VOLTAGE_LEVEL_FEEDER_BAYS_ERROR, String.format(UNSUPPORTED_CONNECTABLE, connectable.getClass())); |
| 168 | + }; |
| 169 | + } |
| 170 | +} |
0 commit comments