Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rework of LineConnectionAction to TerminalsConnectionAction #2843

Merged
merged 12 commits into from
Jan 16, 2024

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/**
* Copyright (c) 2024, RTE (http://www.rte-france.com)
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
* SPDX-License-Identifier: MPL-2.0
*/
package com.powsybl.security.action;

import com.powsybl.iidm.network.ThreeSides;

import java.util.Objects;
import java.util.Optional;

/**
* An action of opening or closing a terminal.
jeandemanged marked this conversation as resolved.
Show resolved Hide resolved
*
* @author Anne Tilloy {@literal <anne.tilloy@rte-france.com>}
*/
public class TerminalsConnectionAction extends AbstractAction {

public static final String NAME = "TERMINALS_CONNECTION";
private final String elementId;
private ThreeSides side;
private final boolean open;

/**
* @param id the id of the action.
* @param elementId the id of the element which terminals are operated. The element could be any connectable.
* We can also imagine that the element could be a tie line, referring the terminal of an underlying
* dangling line.
* @param side the side of the element to operate in the action.
* @param open the status of the terminal to operate. {@code true} means terminal to open.
*/
jeandemanged marked this conversation as resolved.
Show resolved Hide resolved
public TerminalsConnectionAction(String id, String elementId, ThreeSides side, boolean open) {
super(id);
this.elementId = Objects.requireNonNull(elementId);
this.side = Objects.requireNonNull(side);
this.open = open;
}

/**
* @param id the id of the action.
* @param elementId the id of the element which terminals are operated. The element could be any connectable.
* We can also imagine that the element could be a tie line, referring the terminal of an underlying
* dangling line.
* @param open the status of all the terminals of the element to operate. {@code true} means all terminals to open.
*/
jeandemanged marked this conversation as resolved.
Show resolved Hide resolved
public TerminalsConnectionAction(String id, String elementId, boolean open) {
super(id);
this.elementId = Objects.requireNonNull(elementId);
this.open = open;
}

@Override
public String getType() {
return NAME;
}

/**
* @return the element id.
*/
public String getElementId() {
return elementId;
}

/**
* The side is optional. In case of empty, it means that all the terminals of the element will be operated
* in the action with the status open or close also defined.
* @return the optional side of the connection/disconnection action.
*/
public Optional<ThreeSides> getSide() {
return Optional.ofNullable(side);
}

/**
* If {@code true}, applying the action will open the terminal reference with side is given,
* else it will close it. If the side is not given, if {@code true}, applying the action will open all the terminals
* of the element, else it will close them.
*/
public boolean isOpen() {
return open;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ private void configureActionsSerialization() {
setMixInAnnotation(Action.class, ActionMixIn.class);
registerActionType(SwitchAction.class, SwitchAction.NAME,
new SwitchActionSerializer(), new SwitchActionDeserializer());
registerActionType(LineConnectionAction.class, LineConnectionAction.NAME,
new LineConnectionActionSerializer(), new LineConnectionActionDeserializer());
registerActionType(TerminalsConnectionAction.class, TerminalsConnectionAction.NAME,
new TerminalsConnectionActionSerializer(), new TerminalsConnectionActionDeserializer());
registerActionType(MultipleActionsAction.class, MultipleActionsAction.NAME,
new MultipleActionsActionSerializer(), new MultipleActionsActionDeserializer());
registerActionType(PhaseTapChangerTapPositionAction.class, PhaseTapChangerTapPositionAction.NAME,
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -11,57 +11,62 @@
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
import com.powsybl.commons.json.JsonUtil;
import com.powsybl.security.action.LineConnectionAction;
import com.powsybl.iidm.network.ThreeSides;
import com.powsybl.security.action.TerminalsConnectionAction;

import java.io.IOException;

/**
* @author Etienne Lesot {@literal <etienne.lesot@rte-france.com>}
*/
public class LineConnectionActionDeserializer extends StdDeserializer<LineConnectionAction> {
public class TerminalsConnectionActionDeserializer extends StdDeserializer<TerminalsConnectionAction> {

public LineConnectionActionDeserializer() {
super(LineConnectionAction.class);
public TerminalsConnectionActionDeserializer() {
super(TerminalsConnectionAction.class);
}

private static class ParsingContext {
String id;
String lineId;
Boolean openSide1;
Boolean openSide2;
String elementId;
ThreeSides side;
Boolean open;
}

@Override
public LineConnectionAction deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {
public TerminalsConnectionAction deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {
ParsingContext context = new ParsingContext();
JsonUtil.parsePolymorphicObject(jsonParser, name -> {
switch (name) {
case "type":
if (!LineConnectionAction.NAME.equals(jsonParser.nextTextValue())) {
throw JsonMappingException.from(jsonParser, "Expected type " + LineConnectionAction.NAME);
if (!TerminalsConnectionAction.NAME.equals(jsonParser.nextTextValue())) {
throw JsonMappingException.from(jsonParser, "Expected type " + TerminalsConnectionAction.NAME);
}
return true;
case "id":
context.id = jsonParser.nextTextValue();
return true;
case "lineId":
context.lineId = jsonParser.nextTextValue();
case "elementId":
context.elementId = jsonParser.nextTextValue();
return true;
case "openSide1":
case "side":
jsonParser.nextToken();
context.openSide1 = jsonParser.getValueAsBoolean();
context.side = ThreeSides.valueOf(jsonParser.getValueAsString());
return true;
case "openSide2":
case "open":
jsonParser.nextToken();
context.openSide2 = jsonParser.getValueAsBoolean();
context.open = jsonParser.getValueAsBoolean();
return true;
default:
return false;
}
});
if (context.openSide1 == null || context.openSide2 == null) {
throw JsonMappingException.from(jsonParser, "for line action openSide1 and openSide2 fields can't be null");
if (context.open == null) {
throw JsonMappingException.from(jsonParser, "for terminal connection action open field can't be null");
}
if (context.side == null) {
return new TerminalsConnectionAction(context.id, context.elementId, context.open);
} else {
return new TerminalsConnectionAction(context.id, context.elementId, context.side, context.open);
}
return new LineConnectionAction(context.id, context.lineId, context.openSide1, context.openSide2);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* Copyright (c) 2024, RTE (http://www.rte-france.com)
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
* SPDX-License-Identifier: MPL-2.0
*/
package com.powsybl.security.json.action;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
import com.powsybl.commons.json.JsonUtil;
import com.powsybl.iidm.network.ThreeSides;
import com.powsybl.security.action.TerminalsConnectionAction;

import java.io.IOException;
import java.util.Optional;

/**
* @author Anne Tilloy {@literal <anne.tilloy@rte-france.com>}
*/
public class TerminalsConnectionActionSerializer extends StdSerializer<TerminalsConnectionAction> {

public TerminalsConnectionActionSerializer() {
super(TerminalsConnectionAction.class);
}

@Override
public void serialize(TerminalsConnectionAction action, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
jsonGenerator.writeStartObject();
jsonGenerator.writeStringField("type", action.getType());
jsonGenerator.writeStringField("id", action.getId());
jsonGenerator.writeStringField("elementId", action.getElementId());
Optional<ThreeSides> side = action.getSide();
if (side.isPresent()) {
JsonUtil.writeOptionalStringField(jsonGenerator, "side", String.valueOf(side.get()));
}
jsonGenerator.writeBooleanField("open", action.isOpen());
jsonGenerator.writeEndObject();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ void actionRoundTrip() throws IOException {
List<Action> actions = new ArrayList<>();
actions.add(new SwitchAction("id1", "switchId1", true));
actions.add(new MultipleActionsAction("id2", Collections.singletonList(new SwitchAction("id3", "switchId2", true))));
actions.add(new LineConnectionAction("id3", "lineId3", true, true));
actions.add(new LineConnectionAction("id4", "lineId4", false));
actions.add(new TerminalsConnectionAction("id3", "lineId3", true)); // both sides.
actions.add(new TerminalsConnectionAction("id4", "lineId4", false)); // both sides.
actions.add(new PhaseTapChangerTapPositionAction("id5", "transformerId1", true, 5, ThreeSides.TWO));
actions.add(new PhaseTapChangerTapPositionAction("id6", "transformerId2", false, 12));
actions.add(new PhaseTapChangerTapPositionAction("id7", "transformerId3", true, -5, ThreeSides.ONE));
Expand Down Expand Up @@ -103,6 +103,7 @@ void actionRoundTrip() throws IOException {
actions.add(new StaticVarCompensatorActionBuilder().withId("id24")
.withStaticVarCompensatorId("svc").withRegulationMode(StaticVarCompensator.RegulationMode.REACTIVE_POWER)
.withReactivePowerSetpoint(120.0).build());
actions.add(new TerminalsConnectionAction("id4", "transformerId25", ThreeSides.THREE, true)); // only one side.
ActionList actionList = new ActionList(actions);
roundTripTest(actionList, ActionList::writeJsonFile, ActionList::readJsonFile, "/ActionFileTest.json");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,15 @@
"open" : true
} ]
}, {
"type" : "LINE_CONNECTION",
"type" : "TERMINALS_CONNECTION",
"id" : "id3",
"lineId" : "lineId3",
"openSide1" : true,
"openSide2" : true
"elementId" : "lineId3",
"open" : true
}, {
"type" : "LINE_CONNECTION",
"type" : "TERMINALS_CONNECTION",
"id" : "id4",
"lineId" : "lineId4",
"openSide1" : false,
"openSide2" : false
"elementId" : "lineId4",
"open" : false
}, {
"type" : "PHASE_TAP_CHANGER_TAP_POSITION",
"id" : "id5",
Expand Down Expand Up @@ -179,5 +177,11 @@
"staticVarCompensatorId" : "svc",
"regulationMode" : "REACTIVE_POWER",
"reactivePowerSetpoint" : 120.0
}, {
"type" : "TERMINALS_CONNECTION",
"id" : "id4",
"elementId" : "transformerId25",
"side" : "THREE",
"open" : true
} ]
}
Loading