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

implement remove for danglingLines #54

Merged
merged 11 commits into from
Mar 25, 2020
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import java.util.*;
import java.util.function.BiConsumer;
import java.util.stream.Collectors;

/**
* @author Geoffroy Jamgotchian <geoffroy.jamgotchian at rte-france.com>
Expand Down Expand Up @@ -44,6 +45,8 @@ public class BufferedRestNetworkStoreClient implements NetworkStoreClient {

private final Map<UUID, List<Resource<DanglingLineAttributes>>> danglingLineResourcesToFlush = new HashMap<>();

private final Map<UUID, List<String>> danglingLineToRemove = new HashMap<>();

private final Map<UUID, List<Resource<TwoWindingsTransformerAttributes>>> twoWindingsTransformerResourcesToFlush = new HashMap<>();

private final Map<UUID, List<Resource<ThreeWindingsTransformerAttributes>>> threeWindingsTransformerResourcesToFlush = new HashMap<>();
Expand Down Expand Up @@ -440,6 +443,13 @@ public int getHvdcLineCount(UUID networkUuid) {

@Override
public void createDanglingLines(UUID networkUuid, List<Resource<DanglingLineAttributes>> danglingLineResources) {
if (danglingLineToRemove.get(networkUuid) != null) {
int dlToRemoveSize = danglingLineToRemove.size();
danglingLineResources.forEach(danglingLineResource -> danglingLineToRemove.get(networkUuid).remove(danglingLineResource.getId()));
if (dlToRemoveSize != danglingLineToRemove.size()) {
return;
}
}
danglingLineResourcesToFlush.computeIfAbsent(networkUuid, k -> new ArrayList<>()).addAll(danglingLineResources);
}

Expand All @@ -458,6 +468,19 @@ public int getDanglingLineCount(UUID networkUuid) {
return client.getDanglingLineCount(networkUuid);
}

@Override
public void removeDanglingLine(UUID networkUuid, String danglingLineId) {
if (danglingLineResourcesToFlush.get(networkUuid) != null) {
List<Resource<DanglingLineAttributes>> dlToRemove =
danglingLineResourcesToFlush.get(networkUuid).stream().filter(danglingLineAttributesResource -> danglingLineAttributesResource.getId().equals(danglingLineId)).collect(Collectors.toList());
dlToRemove.forEach(danglingLineAttributesResource -> danglingLineResourcesToFlush.get(networkUuid).remove(danglingLineAttributesResource));
if (!dlToRemove.isEmpty()) {
return;
}
}
danglingLineToRemove.computeIfAbsent(networkUuid, k -> new ArrayList<>()).add(danglingLineId);
}

@Override
public void createConfiguredBuses(UUID networkUuid, List<Resource<ConfiguredBusAttributes>> busesRessources) {
busResourcesToFlush.computeIfAbsent(networkUuid, k -> new ArrayList<>()).addAll(busesRessources);
Expand Down Expand Up @@ -488,6 +511,16 @@ private static <T extends IdentifiableAttributes> void flushResources(Map<UUID,
}
}

private static void removeResources(Map<UUID, List<String>> resourcesToDelete,
BiConsumer<UUID, List<String>> deleteFct) {
if (!resourcesToDelete.isEmpty()) {
for (Map.Entry<UUID, List<String>> e : resourcesToDelete.entrySet()) {
deleteFct.accept(e.getKey(), e.getValue());
}
resourcesToDelete.clear();
}
}

@Override
public void flush() {
if (!networkResourcesToFlush.isEmpty()) {
Expand All @@ -511,5 +544,7 @@ public void flush() {
flushResources(threeWindingsTransformerResourcesToFlush, client::createThreeWindingsTransformers);
flushResources(lineResourcesToFlush, client::createLines);
flushResources(busResourcesToFlush, client::createConfiguredBuses);

removeResources(danglingLineToRemove, client::removeDanglingLines);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ int getResourceCount() {
List<Resource<T>> getContainerResources(String containerId) {
return ImmutableList.<Resource<T>>builder().addAll(resourcesByContainerId.computeIfAbsent(containerId, k -> new HashSet<>())).build();
}

void removeResource(String resourceId) {
resourcesById.remove(resourceId);
}
}

private final NestedResources<VoltageLevelAttributes> voltageLevelResources = new NestedResources<>(resource -> resource.getAttributes().getSubstationId());
Expand Down Expand Up @@ -626,6 +630,11 @@ public int getDanglingLineCount(UUID networkUuid) {
return getNetworkCache(networkUuid).getDanglingLineResources().getResourceCount();
}

@Override
public void removeDanglingLine(UUID networkUuid, String danglingLineId) {
getNetworkCache(networkUuid).getDanglingLineResources().removeResource(danglingLineId);
}

@Override
public void createConfiguredBuses(UUID networkUuid, List<Resource<ConfiguredBusAttributes>> busesResources) {
getNetworkCache(networkUuid).getBusResources().addResources(busesResources);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ public ConnectableType getType() {
return ConnectableType.DANGLING_LINE;
}

@Override
public void remove() {
index.removeDanglingLine(resource.getId());
}

@Override
protected DanglingLine getInjection() {
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,11 @@ public int getDanglingLineCount(UUID networkUuid) {
return bufferedRestNetworkStoreClient.getDanglingLineCount(networkUuid);
}

@Override
public void removeDanglingLine(UUID networkUuid, String danglingLineId) {
throw new UnsupportedOperationException("TODO");
}

@Override
public void createConfiguredBuses(UUID networkUuid, List<Resource<ConfiguredBusAttributes>> busesRessources) {
bufferedRestNetworkStoreClient.createConfiguredBuses(networkUuid, busesRessources);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,11 @@ public Identifiable<?> getIdentifiable(String id) {
return null;
}

public void removeDanglingLine(String danglingLineId) {
storeClient.removeDanglingLine(network.getUuid(), danglingLineId);
AbdelHedhili marked this conversation as resolved.
Show resolved Hide resolved
danglingLineById.remove(danglingLineId);
}

//buses

Optional<Bus> getBus(String id) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,8 @@ public interface NetworkStoreClient {

int getDanglingLineCount(UUID networkUuid);

void removeDanglingLine(UUID networkUuid, String danglingLineId);

// Bus

void createConfiguredBuses(UUID networkUuid, List<Resource<ConfiguredBusAttributes>> busesRessources);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@ public Optional<Resource<NetworkAttributes>> getNetwork(UUID networkUuid) {
public void deleteNetwork(UUID networkUuid) {
restClient.deleteNetwork(networkUuid);
cacheClient.deleteNetwork(networkUuid);
cachedResourceTypes.remove(networkUuid);
}

@Override
Expand Down Expand Up @@ -574,6 +573,12 @@ public int getDanglingLineCount(UUID networkUuid) {
return cacheClient.getDanglingLineCount(networkUuid);
}

@Override
public void removeDanglingLine(UUID networkUuid, String danglingLineId) {
restClient.removeDanglingLine(networkUuid, danglingLineId);
cacheClient.removeDanglingLine(networkUuid, danglingLineId);
}

@Override
public void createConfiguredBuses(UUID networkUuid, List<Resource<ConfiguredBusAttributes>> busesResources) {
ensureCached(ResourceType.CONFIGURED_BUS, networkUuid);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@
import org.springframework.http.*;
import org.springframework.web.client.RestTemplate;

import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.*;

/**
* @author Geoffroy Jamgotchian <geoffroy.jamgotchian at rte-france.com>
Expand Down Expand Up @@ -49,7 +47,7 @@ public <T extends IdentifiableAttributes> void create(String url, List<Resource<
restTemplate.postForObject(url, resources, Void.class, uriVariables);
}

public <T extends IdentifiableAttributes> void delete(String url, Object... uriVariables) {
public void delete(String url, Object... uriVariables) {
restTemplate.delete(url, uriVariables);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@
import org.springframework.web.client.ResourceAccessException;
import org.springframework.web.util.UriComponentsBuilder;

import java.util.List;
import java.util.Optional;
import java.util.UUID;
import java.util.*;
import java.util.concurrent.TimeUnit;

/**
Expand Down Expand Up @@ -504,6 +502,17 @@ public int getDanglingLineCount(UUID networkUuid) {
return getTotalCount("dangling line", "/networks/{networkUuid}/dangling-lines?limit=0", networkUuid);
}

@Override
public void removeDanglingLine(UUID networkUuid, String danglingLineId) {
resources.delete("/networks/{networkUuid}/dangling-lines/{danglingLineId}", networkUuid, danglingLineId);
}

public void removeDanglingLines(UUID networkUuid, List<String> danglingLinesId) {
danglingLinesId.forEach(danglingLineId -> removeDanglingLine(networkUuid, danglingLineId));
}

//ConfiguredBus

@Override
public void createConfiguredBuses(UUID networkUuid, List<Resource<ConfiguredBusAttributes>> busesResources) {
create("bus", "/networks/{networkUuid}/configured-buses", busesResources, networkUuid);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.powsybl.commons.datasource.ResourceDataSource;
import com.powsybl.commons.datasource.ResourceSet;
import com.powsybl.entsoe.util.MergedXnode;
import com.powsybl.entsoe.util.Xnode;
import com.powsybl.iidm.network.*;
import com.powsybl.iidm.network.VoltageLevel.NodeBreakerView.InternalConnection;
import com.powsybl.iidm.network.test.FictitiousSwitchFactory;
Expand All @@ -21,6 +22,7 @@
import com.powsybl.network.store.server.CassandraConfig;
import com.powsybl.network.store.server.CassandraConstants;
import com.powsybl.network.store.server.NetworkStoreApplication;
import com.powsybl.sld.iidm.extensions.ConnectablePosition;
import com.powsybl.ucte.converter.UcteImporter;
import org.apache.commons.io.FilenameUtils;
import org.cassandraunit.spring.CassandraDataSet;
Expand Down Expand Up @@ -666,17 +668,16 @@ public void testUcteNetwork() {
Map<UUID, String> networkIds = service.getNetworkIds();
assertEquals(1, networkIds.size());
Network readNetwork = service.getNetwork(networkIds.keySet().stream().findFirst().get());
//TODO uncomment when remove method for DL are implemented
// assertEquals(1, readNetwork.getDanglingLineCount());
// assertEquals("XG__F_21", readNetwork.getDanglingLineStream().findFirst().get().getUcteXnodeCode());
// Xnode xnode = (Xnode) readNetwork.getDanglingLineStream().findFirst().get().getExtensionByName("xnode");
// assertEquals("XG__F_21", xnode.getCode());
// Xnode sameXnode = (Xnode) readNetwork.getDanglingLineStream().findFirst().get().getExtension(Xnode.class);
// assertEquals("XG__F_21", sameXnode.getCode());
// ConnectablePosition connectablePosition = (ConnectablePosition) readNetwork.getDanglingLineStream().findFirst().get().getExtension(ConnectablePosition.class);
// assertNull(connectablePosition);
// ConnectablePosition connectablePosition2 = (ConnectablePosition) readNetwork.getDanglingLineStream().findFirst().get().getExtensionByName("");
// assertNull(connectablePosition2);
assertEquals(1, readNetwork.getDanglingLineCount());
assertEquals("XG__F_21", readNetwork.getDanglingLineStream().findFirst().get().getUcteXnodeCode());
Xnode xnode = (Xnode) readNetwork.getDanglingLineStream().findFirst().get().getExtensionByName("xnode");
assertEquals("XG__F_21", xnode.getCode());
Xnode sameXnode = (Xnode) readNetwork.getDanglingLineStream().findFirst().get().getExtension(Xnode.class);
assertEquals("XG__F_21", sameXnode.getCode());
ConnectablePosition connectablePosition = (ConnectablePosition) readNetwork.getDanglingLineStream().findFirst().get().getExtension(ConnectablePosition.class);
assertNull(connectablePosition);
ConnectablePosition connectablePosition2 = (ConnectablePosition) readNetwork.getDanglingLineStream().findFirst().get().getExtensionByName("");
assertNull(connectablePosition2);
assertEquals(4, readNetwork.getLineCount());
assertNotNull(readNetwork.getLine("XB__F_21 F_SU1_21 1"));
assertNotNull(readNetwork.getLine("XB__F_11 F_SU1_11 1"));
Expand Down Expand Up @@ -767,6 +768,50 @@ public void testUcteNetwork() {
}
}

@Test
public void testDanglingLineRemove() {
try (NetworkStoreService service = new NetworkStoreService(getBaseUrl())) {
service.flush(createRemoveDL(service.getNetworkFactory()));
}

try (NetworkStoreService service = new NetworkStoreService(getBaseUrl())) {
Map<UUID, String> networkIds = service.getNetworkIds();
assertEquals(1, networkIds.size());
Network readNetwork = service.getNetwork(networkIds.keySet().stream().findFirst().get());
assertEquals(1, readNetwork.getDanglingLineCount());
service.flush(readNetwork);
}

try (NetworkStoreService service = new NetworkStoreService(getBaseUrl())) {
Map<UUID, String> networkIds = service.getNetworkIds();
assertEquals(1, networkIds.size());
Network readNetwork = service.getNetwork(networkIds.keySet().stream().findFirst().get());
assertEquals(1, readNetwork.getDanglingLineCount());
readNetwork.getDanglingLine("dl1").remove();
readNetwork.getVoltageLevel("VL1").newDanglingLine().setName("dl1").setId("dl1").add();
readNetwork.getVoltageLevel("VL1").newDanglingLine().setName("dl2").setId("dl2").add();
service.flush(readNetwork);
}

try (NetworkStoreService service = new NetworkStoreService(getBaseUrl())) {
Map<UUID, String> networkIds = service.getNetworkIds();
assertEquals(1, networkIds.size());
Network readNetwork = service.getNetwork(networkIds.keySet().stream().findFirst().get());
assertEquals(2, readNetwork.getDanglingLineCount());
readNetwork.getDanglingLine("dl2").remove();

service.flush(readNetwork);
}

try (NetworkStoreService service = new NetworkStoreService(getBaseUrl())) {
Map<UUID, String> networkIds = service.getNetworkIds();
assertEquals(1, networkIds.size());
Network readNetwork = service.getNetwork(networkIds.keySet().stream().findFirst().get());
assertEquals(1, readNetwork.getDanglingLineCount());
assertNotNull(readNetwork.getDanglingLine("dl1"));
}
}

public Network loadUcteNetwork(NetworkFactory networkFactory) {
String filePath = "/uctNetwork.uct";
ReadOnlyDataSource dataSource = new ResourceDataSource(
Expand Down Expand Up @@ -924,4 +969,34 @@ private Network createGeneratorNetwork(NetworkFactory networkFactory, ReactiveLi
}
return network;
}

private Network createRemoveDL(NetworkFactory networkFactory) {
Network network = networkFactory.createNetwork("DL network", "test");
Substation s1 = network.newSubstation()
.setId("S1")
.setCountry(Country.ES)
.add();
VoltageLevel vl1 = s1.newVoltageLevel()
.setId("VL1")
.setNominalV(400f)
.setTopologyKind(TopologyKind.NODE_BREAKER)
.add();
vl1.newDanglingLine()
.setId("dl1")
.setName("dl1")
.add();
network.getDanglingLine("dl1").remove();
vl1.newDanglingLine()
.setId("dl1")
.setName("dl1")
.add();
vl1.newGenerator()
.setId("GEN")
.setNode(1)
.setMaxP(20)
.setMinP(-20)
.setVoltageRegulatorOn(true)
.add();
return network;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;
import java.util.UUID;
import java.util.*;
import java.util.function.Consumer;
import java.util.function.Supplier;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -642,6 +640,17 @@ public ResponseEntity<TopLevelDocument<DanglingLineAttributes>> getDanglingLine(
return get(() -> repository.getDanglingLine(networkId, danglingLineId));
}

@DeleteMapping(value = "/{networkId}/dangling-lines/{danglingLineId}", produces = APPLICATION_JSON_VALUE)
@ApiOperation(value = "Delete a dangling line by id")
@ApiResponses(value = {
@ApiResponse(code = 200, message = "Successfully delete dangling line")
})
public ResponseEntity<Void> deleteDanglingLine(@ApiParam(value = "Network ID", required = true) @PathVariable("networkId") UUID networkId,
@ApiParam(value = "Dangling line ID", required = true) @PathVariable("danglingLineId") String danglingLineId) {
repository.deleteDanglingLine(networkId, danglingLineId);
return ResponseEntity.ok().build();
}

// buses

@PostMapping(value = "/{networkId}/configured-buses")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,7 @@ void prepareStatements() {
.value("properties", bindMarker())
.value("v", bindMarker())
.value("angle", bindMarker()));

}

// This method unsets the null valued columns of a bound statement in order to avoid creation of tombstones
Expand Down Expand Up @@ -2957,6 +2958,10 @@ public void createDanglingLines(UUID networkUuid, List<Resource<DanglingLineAttr
}
}

public void deleteDanglingLine(UUID networkUuid, String danglingLineId) {
session.execute(delete().from("danglingLine").where(eq("networkUuid", networkUuid)).and(eq("id", danglingLineId)));
}

//Buses

public void createBuses(UUID networkUuid, List<Resource<ConfiguredBusAttributes>> resources) {
Expand Down