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
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
*/
public class ActionList {
private final List<Action> actions;
public static final String VERSION = "1.1";
public static final String VERSION = "1.2";
annetill marked this conversation as resolved.
Show resolved Hide resolved

public ActionList(List<Action> actions) {
this.actions = ImmutableList.copyOf(Objects.requireNonNull(actions));
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/**
* Copyright (c) 2022, RTE (http://www.rte-france.com)
annetill marked this conversation as resolved.
Show resolved Hide resolved
* 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/.
annetill marked this conversation as resolved.
Show resolved Hide resolved
*/
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.
*
* @author Anne Tilloy {@literal <anne.tilloy@rte-france.com>}
*/
public class TerminalConnectionAction extends AbstractAction {

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

public TerminalConnectionAction(String id, String elementId, ThreeSides side, boolean open) {
super(id);
this.elementId = Objects.requireNonNull(elementId);
this.side = Objects.requireNonNull(side);
this.open = open;
}

public TerminalConnectionAction(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. TODO: define the scope.
*/
public String getElementId() {
return elementId;
}

/**
* Return the optional side of the connection/disconnection action.
EtienneLt marked this conversation as resolved.
Show resolved Hide resolved
*/
public Optional<ThreeSides> getSide() {
return Optional.ofNullable(side);
}

/**
* If {@code true}, applying the action will open the terminal reference,
* else it will close it.
*/
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(TerminalConnectionAction.class, TerminalConnectionAction.NAME,
new TerminalConnectionActionSerializer(), new TerminalConnectionActionDeserializer());
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.TerminalConnectionAction;

import java.io.IOException;

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

public LineConnectionActionDeserializer() {
super(LineConnectionAction.class);
public TerminalConnectionActionDeserializer() {
super(TerminalConnectionAction.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 TerminalConnectionAction 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 (!TerminalConnectionAction.NAME.equals(jsonParser.nextTextValue())) {
throw JsonMappingException.from(jsonParser, "Expected type " + TerminalConnectionAction.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 TerminalConnectionAction(context.id, context.elementId, context.open);
} else {
return new TerminalConnectionAction(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,41 @@
/**
* Copyright (c) 2022, 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/.
annetill marked this conversation as resolved.
Show resolved Hide resolved
*/
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.TerminalConnectionAction;

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

/**
* @author Etienne Lesot {@literal <etienne.lesot@rte-france.com>}
annetill marked this conversation as resolved.
Show resolved Hide resolved
*/
public class TerminalConnectionActionSerializer extends StdSerializer<TerminalConnectionAction> {

public TerminalConnectionActionSerializer() {
super(TerminalConnectionAction.class);
}

@Override
public void serialize(TerminalConnectionAction 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 TerminalConnectionAction("id3", "lineId3", true)); // both sides.
actions.add(new TerminalConnectionAction("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 TerminalConnectionAction("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
@@ -1,5 +1,5 @@
{
"version" : "1.1",
"version" : "1.2",
"actions" : [ {
"type" : "SWITCH",
"id" : "id1",
Expand All @@ -15,17 +15,15 @@
"open" : true
} ]
}, {
"type" : "LINE_CONNECTION",
"type" : "TERMINAL_CONNECTION",
"id" : "id3",
"lineId" : "lineId3",
"openSide1" : true,
"openSide2" : true
"elementId" : "lineId3",
"open" : true
}, {
"type" : "LINE_CONNECTION",
"type" : "TERMINAL_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" : "TERMINAL_CONNECTION",
"id" : "id4",
"elementId" : "transformerId25",
"side" : "THREE",
"open" : true
} ]
}
Loading