From 12c386f222a8fd591fc86385ad17e1d6d49f46e6 Mon Sep 17 00:00:00 2001 From: Paul Bui-Quang Date: Sun, 5 Jan 2020 21:17:19 +0100 Subject: [PATCH] Refacto event bus to allow topic use (#3) * Refacto event bus to allow topic use Signed-off-by: Paul Bui-Quang --- afs-cassandra/pom.xml | 1 + .../afs/cassandra/CassandraAppStorage.java | 39 ++++++++----------- .../java/com/powsybl/afs/AppFileSystem.java | 5 +++ .../java/com/powsybl/afs/ProjectFile.java | 17 +++----- .../java/com/powsybl/afs/ProjectFolder.java | 6 +-- .../powsybl/afs/ext/base/AbstractScript.java | 4 +- .../afs/ext/base/ImportedCaseBuilder.java | 4 +- .../ext/base/ModificationScriptBuilder.java | 4 +- .../afs/ext/base/VirtualCaseBuilder.java | 4 +- .../afs/ext/base/events/CaseImported.java | 4 +- .../afs/ext/base/events/ScriptModified.java | 4 +- .../ext/base/events/VirtualCaseCreated.java | 4 +- .../com/powsybl/afs/ext/base/EventsTest.java | 7 ++-- .../afs/mapdb/storage/MapDbAppStorage.java | 35 ++++++++--------- .../afs/storage/AbstractAppStorage.java | 18 ++------- .../com/powsybl/afs/storage/EventsBus.java | 5 +++ .../afs/storage/InMemoryEventsBus.java | 25 ++++++------ .../storage/events/AppStorageListener.java | 7 ++++ .../events/BackwardDependencyAdded.java | 4 +- .../events/BackwardDependencyRemoved.java | 4 +- .../afs/storage/events/DependencyAdded.java | 4 +- .../afs/storage/events/DependencyRemoved.java | 4 +- .../afs/storage/events/NodeConsistent.java | 4 +- .../afs/storage/events/NodeCreated.java | 4 +- .../afs/storage/events/NodeDataRemoved.java | 4 +- .../afs/storage/events/NodeDataUpdated.java | 4 +- .../events/NodeDescriptionUpdated.java | 4 +- .../storage/events/NodeEventContainer.java | 2 +- .../afs/storage/events/NodeEventList.java | 26 +++++++++++-- .../storage/events/NodeMetadataUpdated.java | 4 +- .../afs/storage/events/NodeNameUpdated.java | 4 +- .../afs/storage/events/NodeRemoved.java | 4 +- .../afs/storage/events/ParentChanged.java | 4 +- .../afs/storage/events/TimeSeriesCleared.java | 4 +- .../afs/storage/events/TimeSeriesCreated.java | 4 +- .../storage/events/TimeSeriesDataUpdated.java | 4 +- .../afs/storage/AbstractAppStorageTest.java | 35 ++++++++++++++++- .../afs/storage/InMemoryEventsBusTest.java | 38 ------------------ .../afs/storage/events/NodeEventTest.java | 28 ++++++------- .../afs/ws/server/NodeEventServer.java | 1 + .../afs/ws/server/AppStorageServerTest.java | 9 ----- .../afs/ws/storage/NodeEventClient.java | 6 ++- .../afs/ws/storage/WebSocketEventsBus.java | 19 +++------ 43 files changed, 204 insertions(+), 217 deletions(-) delete mode 100644 afs-storage-api/src/test/java/com/powsybl/afs/storage/InMemoryEventsBusTest.java diff --git a/afs-cassandra/pom.xml b/afs-cassandra/pom.xml index 800aaebc..52bc4116 100644 --- a/afs-cassandra/pom.xml +++ b/afs-cassandra/pom.xml @@ -44,6 +44,7 @@ org.assertj assertj-core + test org.cassandraunit diff --git a/afs-cassandra/src/main/java/com/powsybl/afs/cassandra/CassandraAppStorage.java b/afs-cassandra/src/main/java/com/powsybl/afs/cassandra/CassandraAppStorage.java index 91483edd..324d1a13 100644 --- a/afs-cassandra/src/main/java/com/powsybl/afs/cassandra/CassandraAppStorage.java +++ b/afs-cassandra/src/main/java/com/powsybl/afs/cassandra/CassandraAppStorage.java @@ -439,7 +439,7 @@ private NodeInfo createNode(UUID nodeUuid, UUID parentNodeUuid, String name, Str .value(CMI, genericMetadata.getInts()) .value(CMB, genericMetadata.getBooleans())); } - pushEvent(new NodeCreated(nodeUuid.toString(), parentNodeUuid != null ? parentNodeUuid.toString() : null), NODE_CREATED); + pushEvent(new NodeCreated(nodeUuid.toString(), parentNodeUuid != null ? parentNodeUuid.toString() : null), APPSTORAGE_NODE_TOPIC); return new NodeInfo(nodeUuid.toString(), name, nodePseudoClass, description, creationTime, creationTime, version, genericMetadata); } @@ -499,7 +499,7 @@ public void setMetadata(String nodeId, NodeGenericMetadata genericMetadata) { } getSession().execute(batchStatement); - pushEvent(new NodeMetadataUpdated(nodeId, newMetadata), NODE_DESCRIPTION_UPDATED); + pushEvent(new NodeMetadataUpdated(nodeId, newMetadata), APPSTORAGE_NODE_TOPIC); } @Override @@ -536,7 +536,7 @@ public void renameNode(String nodeId, String name) { getSession().execute(update(CHILDREN_BY_NAME_AND_CLASS).with(set(NAME, name)) .where(eq(ID, nodeUuid))); }); - pushEvent(new NodeNameUpdated(nodeId, name), NODE_NAME_UPDATED); + pushEvent(new NodeNameUpdated(nodeId, name), APPSTORAGE_NODE_TOPIC); } private static UUID checkNodeId(String nodeId) { @@ -597,7 +597,7 @@ private void setAttribute(String nodeId, String attributeName, String childAttri @Override public void setDescription(String nodeId, String description) { setAttribute(nodeId, DESCRIPTION, CHILD_DESCRIPTION, description); - pushEvent(new NodeDescriptionUpdated(nodeId, description), NODE_DESCRIPTION_UPDATED); + pushEvent(new NodeDescriptionUpdated(nodeId, description), APPSTORAGE_NODE_TOPIC); } @Override @@ -605,7 +605,7 @@ public void setConsistent(String nodeId) { // flush buffer to keep change order changeBuffer.flush(); setAttribute(nodeId, CONSISTENT, CHILD_CONSISTENT, true); - pushEvent(new NodeConsistent(nodeId), NODE_CONSISTENT); + pushEvent(new NodeConsistent(nodeId), APPSTORAGE_NODE_TOPIC); } @Override @@ -760,14 +760,14 @@ public void setParentNode(String nodeId, String newParentNodeId) { .value(CMB, nodeInfo.getGenericMetadata().getBooleans())); getSession().execute(batchStatement); - pushEvent(new ParentChanged(nodeId), PARENT_CHANGED); + pushEvent(new ParentChanged(nodeId), APPSTORAGE_NODE_TOPIC); } @Override public String deleteNode(String nodeId) { UUID nodeUuid = checkNodeId(nodeId); UUID parentNodeUuid = deleteNode(nodeUuid); - pushEvent(new NodeRemoved(nodeId, String.valueOf(parentNodeUuid)), NODE_REMOVED); + pushEvent(new NodeRemoved(nodeId, String.valueOf(parentNodeUuid)), APPSTORAGE_NODE_TOPIC); return parentNodeUuid != null ? parentNodeUuid.toString() : null; } @@ -998,8 +998,7 @@ public OutputStream writeBinaryData(String nodeId, String name) { Objects.requireNonNull(name); // flush buffer to keep change order changeBuffer.flush(); - pushEvent(new NodeDataUpdated(nodeId, name), - String.valueOf(NODE_DATA_UPDATED)); + pushEvent(new NodeDataUpdated(nodeId, name), APPSTORAGE_NODE_TOPIC); return new BinaryDataOutputStream(nodeUuid, name); } @@ -1041,8 +1040,7 @@ private void removeData(UUID nodeUuid, String name, List statements) .from(NODE_DATA_NAMES) .where(eq(ID, nodeUuid)) .and(eq(NAME, name))); - pushEvent(new NodeDataRemoved(nodeUuid.toString(), name), - String.valueOf(NODE_DATA_REMOVED)); + pushEvent(new NodeDataRemoved(nodeUuid.toString(), name), APPSTORAGE_NODE_TOPIC); } private void removeAllData(UUID nodeUuid, List statements) { @@ -1082,7 +1080,7 @@ public boolean removeData(String nodeId, String name) { public void createTimeSeries(String nodeId, TimeSeriesMetadata metadata) { changeBuffer.createTimeSeries(nodeId, metadata); pushEvent(new TimeSeriesCreated(nodeId, metadata.getName()), - String.valueOf(TIME_SERIES_CREATED)); + String.valueOf(APPSTORAGE_TIMESERIES_TOPIC)); } @Override @@ -1248,15 +1246,13 @@ public Map> getStringTimeSeriesData(String nodeId, @Override public void addDoubleTimeSeriesData(String nodeId, int version, String timeSeriesName, List chunks) { changeBuffer.addDoubleTimeSeriesData(nodeId, version, timeSeriesName, chunks); - pushEvent(new TimeSeriesDataUpdated(nodeId, timeSeriesName), - String.valueOf(TIME_SERIES_DATA_UPDATED)); + pushEvent(new TimeSeriesDataUpdated(nodeId, timeSeriesName), APPSTORAGE_TIMESERIES_TOPIC); } @Override public void addStringTimeSeriesData(String nodeId, int version, String timeSeriesName, List chunks) { changeBuffer.addStringTimeSeriesData(nodeId, version, timeSeriesName, chunks); - pushEvent(new TimeSeriesDataUpdated(nodeId, timeSeriesName), - String.valueOf(TIME_SERIES_DATA_UPDATED)); + pushEvent(new TimeSeriesDataUpdated(nodeId, timeSeriesName), APPSTORAGE_TIMESERIES_TOPIC); } private void clearTimeSeries(UUID nodeUuid, List statements) { @@ -1308,8 +1304,7 @@ public void clearTimeSeries(String nodeId) { for (Statement statement : statements) { getSession().execute(statement); } - pushEvent(new TimeSeriesCleared(nodeUuid.toString()), - String.valueOf(TIME_SERIES_CLEARED)); + pushEvent(new TimeSeriesCleared(nodeUuid.toString()), APPSTORAGE_TIMESERIES_TOPIC); } @Override @@ -1332,9 +1327,9 @@ public void addDependency(String nodeId, String name, String toNodeId) { .value(FROM_ID, nodeUuid)); getSession().execute(batchStatement); pushEvent(new DependencyAdded(nodeId, name), - String.valueOf(DEPENDENCY_ADDED)); + String.valueOf(APPSTORAGE_DEPENDENCY_TOPIC)); pushEvent(new BackwardDependencyAdded(toNodeId, name), - String.valueOf(BACKWARD_DEPENDENCY_ADDED)); + String.valueOf(APPSTORAGE_DEPENDENCY_TOPIC)); } private List getDependencyNodeUuids(UUID nodeUuid) { @@ -1423,8 +1418,8 @@ public void removeDependency(String nodeId, String name, String toNodeId) { .and(eq(FROM_ID, nodeUuid))); getSession().execute(batchStatement); - pushEvent(new DependencyRemoved(nodeId, name), DEPENDENCY_REMOVED); - pushEvent(new BackwardDependencyRemoved(toNodeId, name), BACKWARD_DEPENDENCY_REMOVED); + pushEvent(new DependencyRemoved(nodeId, name), APPSTORAGE_DEPENDENCY_TOPIC); + pushEvent(new BackwardDependencyRemoved(toNodeId, name), APPSTORAGE_DEPENDENCY_TOPIC); } @Override diff --git a/afs-core/src/main/java/com/powsybl/afs/AppFileSystem.java b/afs-core/src/main/java/com/powsybl/afs/AppFileSystem.java index 1668bcd8..f58abe38 100644 --- a/afs-core/src/main/java/com/powsybl/afs/AppFileSystem.java +++ b/afs-core/src/main/java/com/powsybl/afs/AppFileSystem.java @@ -9,6 +9,7 @@ import com.google.common.base.Supplier; import com.google.common.base.Suppliers; import com.powsybl.afs.storage.AppStorage; +import com.powsybl.afs.storage.EventsBus; import com.powsybl.afs.storage.NodeInfo; import java.util.Objects; @@ -73,6 +74,10 @@ AppStorage getStorage() { return storage; } + public EventsBus getEventBus() { + return storage.getEventsBus(); + } + public Folder getRootFolder() { return new Folder(new FileCreationContext(rootNodeInfo.get(), storage, this)); } diff --git a/afs-core/src/main/java/com/powsybl/afs/ProjectFile.java b/afs-core/src/main/java/com/powsybl/afs/ProjectFile.java index 8c84f5f4..3b7a75a8 100644 --- a/afs-core/src/main/java/com/powsybl/afs/ProjectFile.java +++ b/afs-core/src/main/java/com/powsybl/afs/ProjectFile.java @@ -7,9 +7,7 @@ package com.powsybl.afs; import com.powsybl.afs.storage.NodeInfo; -import com.powsybl.afs.storage.events.AppStorageListener; -import com.powsybl.afs.storage.events.DependencyEvent; -import com.powsybl.afs.storage.events.NodeEvent; +import com.powsybl.afs.storage.events.*; import com.powsybl.commons.util.WeakListenerList; import java.util.List; @@ -23,24 +21,19 @@ */ public class ProjectFile extends ProjectNode { - private static final String DEPENDENCY_ADDED = "DEPENDENCY_ADDED"; - private static final String DEPENDENCY_REMOVED = "DEPENDENCY_REMOVED"; - private static final String BACKWARD_DEPENDENCY_ADDED = "BACKWARD_DEPENDENCY_ADDED"; - private static final String BACKWARD_DEPENDENCY_REMOVED = "BACKWARD_DEPENDENCY_REMOVED"; - private final WeakListenerList listeners = new WeakListenerList<>(); private final AppStorageListener l = eventList -> { for (NodeEvent event : eventList.getEvents()) { if (event.getId().equals(getId())) { switch (event.getType()) { - case DEPENDENCY_ADDED: - case DEPENDENCY_REMOVED: + case DependencyAdded.TYPENAME: + case DependencyRemoved.TYPENAME: listeners.notify(listener -> listener.dependencyChanged(((DependencyEvent) event).getDependencyName())); break; - case BACKWARD_DEPENDENCY_ADDED: - case BACKWARD_DEPENDENCY_REMOVED: + case BackwardDependencyAdded.TYPENAME: + case BackwardDependencyRemoved.TYPENAME: listeners.notify(listener -> listener.backwardDependencyChanged(((DependencyEvent) event).getDependencyName())); break; diff --git a/afs-core/src/main/java/com/powsybl/afs/ProjectFolder.java b/afs-core/src/main/java/com/powsybl/afs/ProjectFolder.java index 6e6ec6f1..7d815b19 100644 --- a/afs-core/src/main/java/com/powsybl/afs/ProjectFolder.java +++ b/afs-core/src/main/java/com/powsybl/afs/ProjectFolder.java @@ -32,21 +32,19 @@ public class ProjectFolder extends ProjectNode implements FolderBase listeners = new WeakListenerList<>(); private final AppStorageListener l = eventList -> { for (NodeEvent event : eventList.getEvents()) { switch (event.getType()) { - case NODE_CREATED: + case NodeCreated.TYPENAME: if (getId().equals(((NodeCreated) event).getParentId())) { listeners.notify(listener -> listener.childAdded(event.getId())); } break; - case NODE_REMOVED: + case NodeRemoved.TYPENAME: if (getId().equals(((NodeRemoved) event).getParentId())) { listeners.notify(listener -> listener.childRemoved(event.getId())); } diff --git a/afs-ext-base/src/main/java/com/powsybl/afs/ext/base/AbstractScript.java b/afs-ext-base/src/main/java/com/powsybl/afs/ext/base/AbstractScript.java index 8c49c485..df67bd28 100644 --- a/afs-ext-base/src/main/java/com/powsybl/afs/ext/base/AbstractScript.java +++ b/afs-ext-base/src/main/java/com/powsybl/afs/ext/base/AbstractScript.java @@ -28,8 +28,6 @@ */ public abstract class AbstractScript extends ProjectFile implements StorableScript { - private static final String NODE_DATA_UPDATED = "NODE_DATA_UPDATED"; - private static final String INCLUDED_SCRIPTS_DEPENDENCY_NAME = "scriptIncludes"; private static final String DEFAULT_SCRIPTS_DELIMITER = "\n\n"; private final String scriptContentName; @@ -45,7 +43,7 @@ public AbstractScript(ProjectFileCreationContext context, int codeVersion, Strin private void processEvents(List events, String nodeId, List listeners) { for (NodeEvent event : events) { - if (NODE_DATA_UPDATED.equals(event.getType())) { + if (NodeDataUpdated.TYPENAME.equals(event.getType())) { NodeDataUpdated dataUpdated = (NodeDataUpdated) event; if (dataUpdated.getId().equals(nodeId) && scriptContentName.equals(dataUpdated.getDataName())) { for (ScriptListener listener : listeners) { diff --git a/afs-ext-base/src/main/java/com/powsybl/afs/ext/base/ImportedCaseBuilder.java b/afs-ext-base/src/main/java/com/powsybl/afs/ext/base/ImportedCaseBuilder.java index 9168d5f1..c1dca707 100644 --- a/afs-ext-base/src/main/java/com/powsybl/afs/ext/base/ImportedCaseBuilder.java +++ b/afs-ext-base/src/main/java/com/powsybl/afs/ext/base/ImportedCaseBuilder.java @@ -42,8 +42,6 @@ */ public class ImportedCaseBuilder implements ProjectFileBuilder { - private static final String CASE_IMPORTED = "CASE_IMPORTED"; - private final ProjectFileBuildContext context; private final ExportersLoader exportersLoader; @@ -158,7 +156,7 @@ public ImportedCase build() { importersLoader); context.getStorage().getEventsBus().pushEvent(new CaseImported(info.getId(), - context.getFolderInfo().getId(), ic.getPath().toString()), CASE_IMPORTED); + context.getFolderInfo().getId(), ic.getPath().toString()), CaseImported.TYPENAME); return ic; } diff --git a/afs-ext-base/src/main/java/com/powsybl/afs/ext/base/ModificationScriptBuilder.java b/afs-ext-base/src/main/java/com/powsybl/afs/ext/base/ModificationScriptBuilder.java index 19030d06..c7de9f3f 100644 --- a/afs-ext-base/src/main/java/com/powsybl/afs/ext/base/ModificationScriptBuilder.java +++ b/afs-ext-base/src/main/java/com/powsybl/afs/ext/base/ModificationScriptBuilder.java @@ -24,8 +24,6 @@ */ public class ModificationScriptBuilder implements ProjectFileBuilder { - private static final String SCRIPT_MODIFIED = "SCRIPT_MODIFIED"; - private final ProjectFileBuildContext context; private String name; @@ -89,7 +87,7 @@ public ModificationScript build() { ModificationScript modificationScript = new ModificationScript(new ProjectFileCreationContext(info, context.getStorage(), context.getProject())); context.getStorage().getEventsBus().pushEvent(new ScriptModified(info.getId(), - context.getFolderInfo().getId(), modificationScript.getPath().toString()), SCRIPT_MODIFIED); + context.getFolderInfo().getId(), modificationScript.getPath().toString()), ScriptModified.TYPENAME); return modificationScript; } diff --git a/afs-ext-base/src/main/java/com/powsybl/afs/ext/base/VirtualCaseBuilder.java b/afs-ext-base/src/main/java/com/powsybl/afs/ext/base/VirtualCaseBuilder.java index 419fa5ac..f0820eb0 100644 --- a/afs-ext-base/src/main/java/com/powsybl/afs/ext/base/VirtualCaseBuilder.java +++ b/afs-ext-base/src/main/java/com/powsybl/afs/ext/base/VirtualCaseBuilder.java @@ -18,8 +18,6 @@ */ public class VirtualCaseBuilder implements ProjectFileBuilder { - private static final String VIRTUAL_CASE_CREATED = "VIRTUAL_CASE_CREATED"; - private final ProjectFileBuildContext context; private String name; @@ -96,7 +94,7 @@ public VirtualCase build() { VirtualCase virtualCase = new VirtualCase(new ProjectFileCreationContext(info, context.getStorage(), context.getProject())); context.getStorage().getEventsBus().pushEvent(new VirtualCaseCreated(info.getId(), - context.getFolderInfo().getId(), virtualCase.getPath().toString()), VIRTUAL_CASE_CREATED); + context.getFolderInfo().getId(), virtualCase.getPath().toString()), VirtualCaseCreated.TYPENAME); return virtualCase; } diff --git a/afs-ext-base/src/main/java/com/powsybl/afs/ext/base/events/CaseImported.java b/afs-ext-base/src/main/java/com/powsybl/afs/ext/base/events/CaseImported.java index 37d898b3..66a1215e 100644 --- a/afs-ext-base/src/main/java/com/powsybl/afs/ext/base/events/CaseImported.java +++ b/afs-ext-base/src/main/java/com/powsybl/afs/ext/base/events/CaseImported.java @@ -16,12 +16,12 @@ */ public class CaseImported extends BusinessEvent { - private static final String CASE_IMPORTED = "CASE_IMPORTED"; + public static final String TYPENAME = "CASE_IMPORTED"; @JsonCreator public CaseImported(@JsonProperty("id") String id, @JsonProperty("parentId") String parentId, @JsonProperty("path") String path) { - super(id, parentId, path, CASE_IMPORTED); + super(id, parentId, path, TYPENAME); } @Override diff --git a/afs-ext-base/src/main/java/com/powsybl/afs/ext/base/events/ScriptModified.java b/afs-ext-base/src/main/java/com/powsybl/afs/ext/base/events/ScriptModified.java index 012fa659..f6c18cda 100644 --- a/afs-ext-base/src/main/java/com/powsybl/afs/ext/base/events/ScriptModified.java +++ b/afs-ext-base/src/main/java/com/powsybl/afs/ext/base/events/ScriptModified.java @@ -16,11 +16,11 @@ */ public class ScriptModified extends BusinessEvent { - private static final String SCRIPT_MODIFIED = "SCRIPT_MODIFIED"; + public static final String TYPENAME = "SCRIPT_MODIFIED"; @JsonCreator public ScriptModified(@JsonProperty("id") String id, @JsonProperty("parentId") String parentId, @JsonProperty("path") String path) { - super(id, parentId, path, SCRIPT_MODIFIED); + super(id, parentId, path, TYPENAME); } @Override diff --git a/afs-ext-base/src/main/java/com/powsybl/afs/ext/base/events/VirtualCaseCreated.java b/afs-ext-base/src/main/java/com/powsybl/afs/ext/base/events/VirtualCaseCreated.java index e230588e..ae041f93 100644 --- a/afs-ext-base/src/main/java/com/powsybl/afs/ext/base/events/VirtualCaseCreated.java +++ b/afs-ext-base/src/main/java/com/powsybl/afs/ext/base/events/VirtualCaseCreated.java @@ -16,12 +16,12 @@ */ public class VirtualCaseCreated extends BusinessEvent { - private static final String VIRTUAL_CASE_CREATED = "VIRTUAL_CASE_CREATED"; + public static final String TYPENAME = "VIRTUAL_CASE_CREATED"; @JsonCreator public VirtualCaseCreated(@JsonProperty("id") String id, @JsonProperty("parentId") String parentId, @JsonProperty("path") String path) { - super(id, parentId, path, VIRTUAL_CASE_CREATED); + super(id, parentId, path, TYPENAME); } @Override diff --git a/afs-ext-base/src/test/java/com/powsybl/afs/ext/base/EventsTest.java b/afs-ext-base/src/test/java/com/powsybl/afs/ext/base/EventsTest.java index e76992e1..830ff6b5 100644 --- a/afs-ext-base/src/test/java/com/powsybl/afs/ext/base/EventsTest.java +++ b/afs-ext-base/src/test/java/com/powsybl/afs/ext/base/EventsTest.java @@ -9,7 +9,6 @@ import com.powsybl.afs.ext.base.events.CaseImported; import com.powsybl.afs.ext.base.events.ScriptModified; import com.powsybl.afs.ext.base.events.VirtualCaseCreated; -import com.powsybl.afs.storage.events.*; import org.junit.Test; import java.io.IOException; @@ -26,7 +25,7 @@ public class EventsTest { public void caseImportedTest() throws IOException { CaseImported caseImported = new CaseImported("a", "b", Paths.get("/tmp/foo").toString()); assertEquals("a", caseImported.getId()); - assertEquals("CASE_IMPORTED", caseImported.getType()); + assertEquals(CaseImported.TYPENAME, caseImported.getType()); assertEquals("b", caseImported.getParentId()); assertNotNull(caseImported.toString()); assertEquals(Paths.get("/tmp/foo").toString(), caseImported.getPath()); @@ -41,7 +40,7 @@ public void caseImportedTest() throws IOException { public void scriptModifiedTest() throws IOException { ScriptModified scriptModified = new ScriptModified("a", "b", Paths.get("/tmp/foo").toString()); assertEquals("a", scriptModified.getId()); - assertEquals("SCRIPT_MODIFIED", scriptModified.getType()); + assertEquals(ScriptModified.TYPENAME, scriptModified.getType()); assertEquals("b", scriptModified.getParentId()); assertNotNull(scriptModified.toString()); @@ -56,7 +55,7 @@ public void scriptModifiedTest() throws IOException { public void virtualCaseCreatedTest() throws IOException { VirtualCaseCreated virtualCaseCreated = new VirtualCaseCreated("a", "b", Paths.get("/tmp/foo").toString()); assertEquals("a", virtualCaseCreated.getId()); - assertEquals("VIRTUAL_CASE_CREATED", virtualCaseCreated.getType()); + assertEquals(VirtualCaseCreated.TYPENAME, virtualCaseCreated.getType()); assertEquals("b", virtualCaseCreated.getParentId()); assertNotNull(virtualCaseCreated.toString()); assertEquals(Paths.get("/tmp/foo").toString(), virtualCaseCreated.getPath()); diff --git a/afs-mapdb-storage/src/main/java/com/powsybl/afs/mapdb/storage/MapDbAppStorage.java b/afs-mapdb-storage/src/main/java/com/powsybl/afs/mapdb/storage/MapDbAppStorage.java index d9f5fac5..df7b1b96 100644 --- a/afs-mapdb-storage/src/main/java/com/powsybl/afs/mapdb/storage/MapDbAppStorage.java +++ b/afs-mapdb-storage/src/main/java/com/powsybl/afs/mapdb/storage/MapDbAppStorage.java @@ -315,14 +315,14 @@ public void setDescription(String nodeId, String description) { NodeInfo nodeInfo = getNodeInfo(nodeId); nodeInfo.setDescription(description); nodeInfoMap.put(nodeUuid, nodeInfo); - pushEvent(new NodeDescriptionUpdated(nodeId, description), NODE_DESCRIPTION_UPDATED); + pushEvent(new NodeDescriptionUpdated(nodeId, description), APPSTORAGE_NODE_TOPIC); } @Override public void setConsistent(String nodeId) { UUID nodeUuid = checkNodeId(nodeId); nodeConsistencyMap.put(nodeUuid, true); - pushEvent(new NodeConsistent(nodeId), NODE_CONSISTENT); + pushEvent(new NodeConsistent(nodeId), APPSTORAGE_NODE_TOPIC); } @Override @@ -428,8 +428,7 @@ public void setParentNode(String nodeId, String newParentNodeId) { addToList(childNodesMap, newParentNodeUuid, nodeUuid); childNodeMap.put(new NamedLink(newParentNodeUuid, name), nodeUuid); - pushEvent(new ParentChanged(nodeId), - String.valueOf(PARENT_CHANGED)); + pushEvent(new ParentChanged(nodeId), APPSTORAGE_NODE_TOPIC); } @Override @@ -459,7 +458,7 @@ public NodeInfo createNode(String parentNodeId, String name, String nodePseudoCl dependencyNodesMap.put(nodeUuid, new ArrayList<>()); backwardDependencyNodesMap.put(nodeUuid, new ArrayList<>()); nodeConsistencyMap.put(nodeUuid, false); - pushEvent(new NodeCreated(nodeUuid.toString(), parentNodeId), NODE_CREATED); + pushEvent(new NodeCreated(nodeUuid.toString(), parentNodeId), APPSTORAGE_NODE_TOPIC); return nodeInfo; } @@ -478,7 +477,7 @@ public void setMetadata(String nodeId, NodeGenericMetadata metadata) { nodeInfo.getGenericMetadata().getBooleans().putAll(metadata.getBooleans()); } nodeInfoMap.put(nodeUuid, nodeInfo); - pushEvent(new NodeMetadataUpdated(nodeUuid.toString(), metadata), NODE_CREATED); + pushEvent(new NodeMetadataUpdated(nodeUuid.toString(), metadata), APPSTORAGE_NODE_TOPIC); } @Override @@ -492,14 +491,14 @@ public void renameNode(String nodeId, String name) { }); nodeInfo.setName(name); nodeInfoMap.put(nodeUuid, nodeInfo); - pushEvent(new NodeNameUpdated(nodeId, name), NODE_NAME_UPDATED); + pushEvent(new NodeNameUpdated(nodeId, name), APPSTORAGE_NODE_TOPIC); } @Override public String deleteNode(String nodeId) { UUID nodeUuid = checkNodeId(nodeId); UUID parentNodeUuid = deleteNode(nodeUuid); - pushEvent(new NodeRemoved(nodeId, parentNodeUuid.toString()), NODE_REMOVED); + pushEvent(new NodeRemoved(nodeId, parentNodeUuid.toString()), APPSTORAGE_NODE_TOPIC); return parentNodeUuid.toString(); } @@ -571,7 +570,7 @@ public void close() throws IOException { // store the byte array dataMap.put(new NamedLink(nodeUuid, name), toByteArray()); addToSet(dataNamesMap, nodeUuid, name); - pushEvent(new NodeDataUpdated(nodeId, name), NODE_DATA_UPDATED); + pushEvent(new NodeDataUpdated(nodeId, name), APPSTORAGE_NODE_TOPIC); } }; } @@ -596,7 +595,7 @@ public boolean removeData(String nodeId, String name) { boolean removed = removeFromSet(dataNamesMap, nodeUuid, name); dataMap.remove(new NamedLink(nodeUuid, name)); if (removed) { - pushEvent(new NodeDataRemoved(nodeId, name), NODE_DATA_REMOVED); + pushEvent(new NodeDataRemoved(nodeId, name), APPSTORAGE_NODE_TOPIC); } return removed; } @@ -612,7 +611,7 @@ public void createTimeSeries(String nodeId, TimeSeriesMetadata metadata) { } addToSet(timeSeriesNamesMap, nodeUuid, metadata.getName()); timeSeriesMetadataMap.put(new NamedLink(nodeUuid, metadata.getName()), metadata); - pushEvent(new TimeSeriesCreated(nodeId, metadata.getName()), TIME_SERIES_CREATED); + pushEvent(new TimeSeriesCreated(nodeId, metadata.getName()), APPSTORAGE_TIMESERIES_TOPIC); } @Override @@ -763,7 +762,7 @@ public Map> getDoubleTimeSeriesData(String nodeId, @Override public void addDoubleTimeSeriesData(String nodeId, int version, String timeSeriesName, List chunks) { addTimeSeriesData(nodeId, version, timeSeriesName, chunks, doubleTimeSeriesChunksMap); - pushEvent(new TimeSeriesDataUpdated(nodeId, timeSeriesName), TIME_SERIES_DATA_UPDATED); + pushEvent(new TimeSeriesDataUpdated(nodeId, timeSeriesName), APPSTORAGE_TIMESERIES_TOPIC); } @Override @@ -774,7 +773,7 @@ public Map> getStringTimeSeriesData(String nodeId, @Override public void addStringTimeSeriesData(String nodeId, int version, String timeSeriesName, List chunks) { addTimeSeriesData(nodeId, version, timeSeriesName, chunks, stringTimeSeriesChunksMap); - pushEvent(new TimeSeriesDataUpdated(nodeId, timeSeriesName), TIME_SERIES_DATA_UPDATED); + pushEvent(new TimeSeriesDataUpdated(nodeId, timeSeriesName), APPSTORAGE_TIMESERIES_TOPIC); } @Override @@ -791,7 +790,7 @@ public void clearTimeSeries(String nodeId) { keys.forEach(timeSeriesLastChunkMap::remove); clearTimeSeriesData(nodeId, doubleTimeSeriesChunksMap); clearTimeSeriesData(nodeId, stringTimeSeriesChunksMap); - pushEvent(new TimeSeriesCleared(nodeId), TIME_SERIES_CLEARED); + pushEvent(new TimeSeriesCleared(nodeId), APPSTORAGE_TIMESERIES_TOPIC); } @Override @@ -804,8 +803,8 @@ public void addDependency(String nodeId, String name, String toNodeId) { addToList(dependencyNodesMap, nodeUuid, new NamedLink(toNodeUuid, name)); addToList(dependencyNodesByNameMap, new NamedLink(nodeUuid, name), toNodeUuid); addToList(backwardDependencyNodesMap, toNodeUuid, nodeUuid); - pushEvent(new DependencyAdded(nodeId, name), DEPENDENCY_ADDED); - pushEvent(new BackwardDependencyAdded(toNodeId, name), BACKWARD_DEPENDENCY_ADDED); + pushEvent(new DependencyAdded(nodeId, name), APPSTORAGE_DEPENDENCY_TOPIC); + pushEvent(new BackwardDependencyAdded(toNodeId, name), APPSTORAGE_DEPENDENCY_TOPIC); } @Override @@ -859,8 +858,8 @@ public void removeDependency(String nodeId, String name, String toNodeId) { removeFromList(dependencyNodesMap, nodeUuid, new NamedLink(toNodeUuid, name)); removeFromList(dependencyNodesByNameMap, new NamedLink(nodeUuid, name), toNodeUuid); removeFromList(backwardDependencyNodesMap, toNodeUuid, nodeUuid); - pushEvent(new DependencyRemoved(nodeId, name), DEPENDENCY_REMOVED); - pushEvent(new BackwardDependencyRemoved(toNodeId, name), BACKWARD_DEPENDENCY_REMOVED); + pushEvent(new DependencyRemoved(nodeId, name), APPSTORAGE_DEPENDENCY_TOPIC); + pushEvent(new BackwardDependencyRemoved(toNodeId, name), APPSTORAGE_DEPENDENCY_TOPIC); } @Override diff --git a/afs-storage-api/src/main/java/com/powsybl/afs/storage/AbstractAppStorage.java b/afs-storage-api/src/main/java/com/powsybl/afs/storage/AbstractAppStorage.java index 03acac0b..e1009272 100644 --- a/afs-storage-api/src/main/java/com/powsybl/afs/storage/AbstractAppStorage.java +++ b/afs-storage-api/src/main/java/com/powsybl/afs/storage/AbstractAppStorage.java @@ -15,21 +15,9 @@ */ public abstract class AbstractAppStorage implements AppStorage { - protected static final String NODE_CREATED = "NODE_CREATED"; - protected static final String NODE_REMOVED = "NODE_REMOVED"; - protected static final String NODE_DESCRIPTION_UPDATED = "NODE_DESCRIPTION_UPDATED"; - protected static final String NODE_NAME_UPDATED = "NODE_NAME_UPDATED"; - protected static final String NODE_DATA_UPDATED = "NODE_DATA_UPDATED"; - protected static final String NODE_DATA_REMOVED = "NODE_DATA_REMOVED"; - protected static final String NODE_CONSISTENT = "NODE_CONSISTENT"; - protected static final String PARENT_CHANGED = "PARENT_CHANGED"; - protected static final String DEPENDENCY_ADDED = "DEPENDENCY_ADDED"; - protected static final String DEPENDENCY_REMOVED = "DEPENDENCY_REMOVED"; - protected static final String BACKWARD_DEPENDENCY_ADDED = "BACKWARD_DEPENDENCY_ADDED"; - protected static final String BACKWARD_DEPENDENCY_REMOVED = "BACKWARD_DEPENDENCY_REMOVED"; - protected static final String TIME_SERIES_CREATED = "TIME_SERIES_CREATED"; - protected static final String TIME_SERIES_DATA_UPDATED = "TIME_SERIES_DATA_UPDATED"; - protected static final String TIME_SERIES_CLEARED = "TIME_SERIES_CLEARED"; + protected static final String APPSTORAGE_NODE_TOPIC = "APPSTORAGE_NODE_TOPIC"; + protected static final String APPSTORAGE_DEPENDENCY_TOPIC = "APPSTORAGE_DEPENDENCY_TOPIC"; + protected static final String APPSTORAGE_TIMESERIES_TOPIC = "APPSTORAGE_TIMESERIES_TOPIC"; protected EventsBus eventsBus; diff --git a/afs-storage-api/src/main/java/com/powsybl/afs/storage/EventsBus.java b/afs-storage-api/src/main/java/com/powsybl/afs/storage/EventsBus.java index be3d7184..51cb9320 100644 --- a/afs-storage-api/src/main/java/com/powsybl/afs/storage/EventsBus.java +++ b/afs-storage-api/src/main/java/com/powsybl/afs/storage/EventsBus.java @@ -13,6 +13,11 @@ * @author Chamseddine Benhamed */ public interface EventsBus { + /** + * Add a new event to the event bus + * @param event + * @param topic + */ void pushEvent(NodeEvent event, String topic); /** diff --git a/afs-storage-api/src/main/java/com/powsybl/afs/storage/InMemoryEventsBus.java b/afs-storage-api/src/main/java/com/powsybl/afs/storage/InMemoryEventsBus.java index 4d7da4eb..16e26461 100644 --- a/afs-storage-api/src/main/java/com/powsybl/afs/storage/InMemoryEventsBus.java +++ b/afs-storage-api/src/main/java/com/powsybl/afs/storage/InMemoryEventsBus.java @@ -12,9 +12,8 @@ import com.powsybl.commons.util.WeakListenerList; import java.util.ArrayList; -import java.util.HashMap; import java.util.List; -import java.util.Map; +import java.util.Objects; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; @@ -23,24 +22,25 @@ */ public class InMemoryEventsBus implements EventsBus { - private final Map> topics = new HashMap<>(); + private final List topics = new ArrayList<>(); private final WeakListenerList listeners = new WeakListenerList<>(); - private NodeEventList eventList = new NodeEventList(); - private final Lock lock = new ReentrantLock(); @Override public void pushEvent(NodeEvent event, String topic) { lock.lock(); - eventList.addEvent(event); - topics.computeIfAbsent(topic, k -> new ArrayList<>()); - topics.get(topic).add(event); + NodeEventList lastEventTopic = topics.iterator().hasNext() ? topics.iterator().next() : null; + if (lastEventTopic != null && Objects.equals(lastEventTopic.getTopic(), topic)) { + lastEventTopic.addEvent(event); + } else { + topics.add(new NodeEventList(topic, event)); + } lock.unlock(); } - Map> getTopics() { + List getTopics() { return topics; } @@ -49,8 +49,11 @@ public void flush() { lock.lock(); try { listeners.log(); - listeners.notify(l -> l.onEvents(eventList)); - eventList = new NodeEventList(); + listeners.notify(l -> topics.forEach(nodeEventList -> { + if (l.topics().isEmpty() || l.topics().contains(nodeEventList.getTopic())) { + l.onEvents(nodeEventList); + } + })); topics.clear(); } finally { lock.unlock(); diff --git a/afs-storage-api/src/main/java/com/powsybl/afs/storage/events/AppStorageListener.java b/afs-storage-api/src/main/java/com/powsybl/afs/storage/events/AppStorageListener.java index 5422457b..160078bf 100644 --- a/afs-storage-api/src/main/java/com/powsybl/afs/storage/events/AppStorageListener.java +++ b/afs-storage-api/src/main/java/com/powsybl/afs/storage/events/AppStorageListener.java @@ -6,10 +6,17 @@ */ package com.powsybl.afs.storage.events; +import java.util.Collections; +import java.util.Set; + /** * @author Geoffroy Jamgotchian */ public interface AppStorageListener { void onEvents(NodeEventList eventList); + + default Set topics() { + return Collections.emptySet(); + } } diff --git a/afs-storage-api/src/main/java/com/powsybl/afs/storage/events/BackwardDependencyAdded.java b/afs-storage-api/src/main/java/com/powsybl/afs/storage/events/BackwardDependencyAdded.java index b2e77719..77ab8eba 100644 --- a/afs-storage-api/src/main/java/com/powsybl/afs/storage/events/BackwardDependencyAdded.java +++ b/afs-storage-api/src/main/java/com/powsybl/afs/storage/events/BackwardDependencyAdded.java @@ -19,12 +19,12 @@ public class BackwardDependencyAdded extends NodeEvent implements DependencyEven @JsonProperty("dependencyName") private final String dependencyName; - private static final String BACKWARD_DEPENDENCY_ADDED = "BACKWARD_DEPENDENCY_ADDED"; + public static final String TYPENAME = "BACKWARD_DEPENDENCY_ADDED"; @JsonCreator public BackwardDependencyAdded(@JsonProperty("id") String id, @JsonProperty("dependencyName") String dependencyName) { - super(id, BACKWARD_DEPENDENCY_ADDED); + super(id, TYPENAME); this.dependencyName = Objects.requireNonNull(dependencyName); } diff --git a/afs-storage-api/src/main/java/com/powsybl/afs/storage/events/BackwardDependencyRemoved.java b/afs-storage-api/src/main/java/com/powsybl/afs/storage/events/BackwardDependencyRemoved.java index 4e4c05c4..23299ad0 100644 --- a/afs-storage-api/src/main/java/com/powsybl/afs/storage/events/BackwardDependencyRemoved.java +++ b/afs-storage-api/src/main/java/com/powsybl/afs/storage/events/BackwardDependencyRemoved.java @@ -19,12 +19,12 @@ public class BackwardDependencyRemoved extends NodeEvent implements DependencyEv @JsonProperty("dependencyName") private final String dependencyName; - private static final String BACKWARD_DEPENDENCY_REMOVED = "BACKWARD_DEPENDENCY_REMOVED"; + public static final String TYPENAME = "BACKWARD_DEPENDENCY_REMOVED"; @JsonCreator public BackwardDependencyRemoved(@JsonProperty("id") String id, @JsonProperty("dependencyName") String dependencyName) { - super(id, BACKWARD_DEPENDENCY_REMOVED); + super(id, TYPENAME); this.dependencyName = Objects.requireNonNull(dependencyName); } diff --git a/afs-storage-api/src/main/java/com/powsybl/afs/storage/events/DependencyAdded.java b/afs-storage-api/src/main/java/com/powsybl/afs/storage/events/DependencyAdded.java index 4f63ce50..6d283ae9 100644 --- a/afs-storage-api/src/main/java/com/powsybl/afs/storage/events/DependencyAdded.java +++ b/afs-storage-api/src/main/java/com/powsybl/afs/storage/events/DependencyAdded.java @@ -16,7 +16,7 @@ */ public class DependencyAdded extends NodeEvent implements DependencyEvent { - public static final String DEPENDENCY_ADDED = "DEPENDENCY_ADDED"; + public static final String TYPENAME = "DEPENDENCY_ADDED"; @JsonProperty("dependencyName") private final String dependencyName; @@ -24,7 +24,7 @@ public class DependencyAdded extends NodeEvent implements DependencyEvent { @JsonCreator public DependencyAdded(@JsonProperty("id") String id, @JsonProperty("dependencyName") String dependencyName) { - super(id, DEPENDENCY_ADDED); + super(id, TYPENAME); this.dependencyName = Objects.requireNonNull(dependencyName); } diff --git a/afs-storage-api/src/main/java/com/powsybl/afs/storage/events/DependencyRemoved.java b/afs-storage-api/src/main/java/com/powsybl/afs/storage/events/DependencyRemoved.java index 345080d8..7795d49f 100644 --- a/afs-storage-api/src/main/java/com/powsybl/afs/storage/events/DependencyRemoved.java +++ b/afs-storage-api/src/main/java/com/powsybl/afs/storage/events/DependencyRemoved.java @@ -16,7 +16,7 @@ */ public class DependencyRemoved extends NodeEvent implements DependencyEvent { - public static final String DEPENDENCY_REMOVED = "DEPENDENCY_REMOVED"; + public static final String TYPENAME = "DEPENDENCY_REMOVED"; @JsonProperty("dependencyName") private final String dependencyName; @@ -24,7 +24,7 @@ public class DependencyRemoved extends NodeEvent implements DependencyEvent { @JsonCreator public DependencyRemoved(@JsonProperty("id") String id, @JsonProperty("dependencyName") String dependencyName) { - super(id, DEPENDENCY_REMOVED); + super(id, TYPENAME); this.dependencyName = Objects.requireNonNull(dependencyName); } diff --git a/afs-storage-api/src/main/java/com/powsybl/afs/storage/events/NodeConsistent.java b/afs-storage-api/src/main/java/com/powsybl/afs/storage/events/NodeConsistent.java index d2f5e56d..4725b6ef 100644 --- a/afs-storage-api/src/main/java/com/powsybl/afs/storage/events/NodeConsistent.java +++ b/afs-storage-api/src/main/java/com/powsybl/afs/storage/events/NodeConsistent.java @@ -16,11 +16,11 @@ */ public class NodeConsistent extends NodeEvent { - public static final String NODE_CONSISTENT = "NODE_CONSISTENT"; + public static final String TYPENAME = "NODE_CONSISTENT"; @JsonCreator public NodeConsistent(@JsonProperty("id") String id) { - super(id, NODE_CONSISTENT); + super(id, TYPENAME); } @Override diff --git a/afs-storage-api/src/main/java/com/powsybl/afs/storage/events/NodeCreated.java b/afs-storage-api/src/main/java/com/powsybl/afs/storage/events/NodeCreated.java index 39eec315..490c8a1b 100644 --- a/afs-storage-api/src/main/java/com/powsybl/afs/storage/events/NodeCreated.java +++ b/afs-storage-api/src/main/java/com/powsybl/afs/storage/events/NodeCreated.java @@ -16,14 +16,14 @@ */ public class NodeCreated extends NodeEvent { - public static final String NODE_CREATED = "NODE_CREATED"; + public static final String TYPENAME = "NODE_CREATED"; @JsonProperty("parentId") protected final String parentId; @JsonCreator public NodeCreated(@JsonProperty("id") String id, @JsonProperty("parentId") String parentId) { - super(id, NODE_CREATED); + super(id, TYPENAME); this.parentId = parentId; } diff --git a/afs-storage-api/src/main/java/com/powsybl/afs/storage/events/NodeDataRemoved.java b/afs-storage-api/src/main/java/com/powsybl/afs/storage/events/NodeDataRemoved.java index faa461d2..aadddfac 100644 --- a/afs-storage-api/src/main/java/com/powsybl/afs/storage/events/NodeDataRemoved.java +++ b/afs-storage-api/src/main/java/com/powsybl/afs/storage/events/NodeDataRemoved.java @@ -16,7 +16,7 @@ */ public class NodeDataRemoved extends NodeEvent { - public static final String NODE_DATA_REMOVED = "NODE_DATA_REMOVED"; + public static final String TYPENAME = "NODE_DATA_REMOVED"; @JsonProperty("dataName") private final String dataName; @@ -24,7 +24,7 @@ public class NodeDataRemoved extends NodeEvent { @JsonCreator public NodeDataRemoved(@JsonProperty("id") String id, @JsonProperty("dataName") String dataName) { - super(id, NODE_DATA_REMOVED); + super(id, TYPENAME); this.dataName = Objects.requireNonNull(dataName); } diff --git a/afs-storage-api/src/main/java/com/powsybl/afs/storage/events/NodeDataUpdated.java b/afs-storage-api/src/main/java/com/powsybl/afs/storage/events/NodeDataUpdated.java index 2314896a..8d384cff 100644 --- a/afs-storage-api/src/main/java/com/powsybl/afs/storage/events/NodeDataUpdated.java +++ b/afs-storage-api/src/main/java/com/powsybl/afs/storage/events/NodeDataUpdated.java @@ -16,7 +16,7 @@ */ public class NodeDataUpdated extends NodeEvent { - public static final String NODE_DATA_UPDATED = "NODE_DATA_UPDATED"; + public static final String TYPENAME = "NODE_DATA_UPDATED"; @JsonProperty("dataName") private final String dataName; @@ -24,7 +24,7 @@ public class NodeDataUpdated extends NodeEvent { @JsonCreator public NodeDataUpdated(@JsonProperty("id") String id, @JsonProperty("dataName") String dataName) { - super(id, NODE_DATA_UPDATED); + super(id, TYPENAME); this.dataName = Objects.requireNonNull(dataName); } diff --git a/afs-storage-api/src/main/java/com/powsybl/afs/storage/events/NodeDescriptionUpdated.java b/afs-storage-api/src/main/java/com/powsybl/afs/storage/events/NodeDescriptionUpdated.java index ba332130..94308523 100644 --- a/afs-storage-api/src/main/java/com/powsybl/afs/storage/events/NodeDescriptionUpdated.java +++ b/afs-storage-api/src/main/java/com/powsybl/afs/storage/events/NodeDescriptionUpdated.java @@ -16,7 +16,7 @@ */ public class NodeDescriptionUpdated extends NodeEvent { - public static final String NODE_DESCRIPTION_UPDATED = "NODE_DESCRIPTION_UPDATED"; + public static final String TYPENAME = "NODE_DESCRIPTION_UPDATED"; @JsonProperty("description") private final String description; @@ -24,7 +24,7 @@ public class NodeDescriptionUpdated extends NodeEvent { @JsonCreator public NodeDescriptionUpdated(@JsonProperty("id") String id, @JsonProperty("description") String description) { - super(id, NODE_DESCRIPTION_UPDATED); + super(id, TYPENAME); this.description = Objects.requireNonNull(description); } diff --git a/afs-storage-api/src/main/java/com/powsybl/afs/storage/events/NodeEventContainer.java b/afs-storage-api/src/main/java/com/powsybl/afs/storage/events/NodeEventContainer.java index 966d408f..9c373a5a 100644 --- a/afs-storage-api/src/main/java/com/powsybl/afs/storage/events/NodeEventContainer.java +++ b/afs-storage-api/src/main/java/com/powsybl/afs/storage/events/NodeEventContainer.java @@ -23,7 +23,7 @@ public class NodeEventContainer { @JsonProperty("nodeEvent") private final NodeEvent nodeEvent; - @JsonProperty + @JsonProperty("topic") private final String topic; public NodeEventContainer() { diff --git a/afs-storage-api/src/main/java/com/powsybl/afs/storage/events/NodeEventList.java b/afs-storage-api/src/main/java/com/powsybl/afs/storage/events/NodeEventList.java index db908081..83855035 100644 --- a/afs-storage-api/src/main/java/com/powsybl/afs/storage/events/NodeEventList.java +++ b/afs-storage-api/src/main/java/com/powsybl/afs/storage/events/NodeEventList.java @@ -16,20 +16,32 @@ */ public class NodeEventList { + @JsonProperty("topic") + private String topic; + @JsonProperty("events") private List events; public NodeEventList() { - this(new ArrayList<>()); + this(new ArrayList<>(), null); + } + + public NodeEventList(String topic) { + this(new ArrayList<>(), topic); + } + + public NodeEventList(String topic, NodeEvent... events) { + this(new ArrayList<>(Arrays.asList(events)), topic); } public NodeEventList(NodeEvent... events) { - this(new ArrayList<>(Arrays.asList(events))); + this(new ArrayList<>(Arrays.asList(events)), null); } @JsonCreator - public NodeEventList(@JsonProperty("events") List events) { + public NodeEventList(@JsonProperty("events") List events, @JsonProperty("topic") String topic) { this.events = Objects.requireNonNull(events); + this.topic = topic; } public void addEvent(NodeEvent event) { @@ -40,6 +52,14 @@ public List getEvents() { return Collections.unmodifiableList(events); } + public String getTopic() { + return topic; + } + + public void setTopic(String topic) { + this.topic = topic; + } + @Override public int hashCode() { return events.hashCode(); diff --git a/afs-storage-api/src/main/java/com/powsybl/afs/storage/events/NodeMetadataUpdated.java b/afs-storage-api/src/main/java/com/powsybl/afs/storage/events/NodeMetadataUpdated.java index a410dd21..4a2e3a6c 100644 --- a/afs-storage-api/src/main/java/com/powsybl/afs/storage/events/NodeMetadataUpdated.java +++ b/afs-storage-api/src/main/java/com/powsybl/afs/storage/events/NodeMetadataUpdated.java @@ -17,7 +17,7 @@ */ public class NodeMetadataUpdated extends NodeEvent { - static final String NODE_DESCRIPTION_UPDATED = "NODE_DESCRIPTION_UPDATED"; + public static final String TYPENAME = "NODE_METADATA_UPDATED"; @JsonProperty("metadata") private final NodeGenericMetadata metadata; @@ -25,7 +25,7 @@ public class NodeMetadataUpdated extends NodeEvent { @JsonCreator public NodeMetadataUpdated(@JsonProperty("id") String id, @JsonProperty("metadata") NodeGenericMetadata metadata) { - super(id, NODE_DESCRIPTION_UPDATED); + super(id, TYPENAME); this.metadata = Objects.requireNonNull(metadata); } diff --git a/afs-storage-api/src/main/java/com/powsybl/afs/storage/events/NodeNameUpdated.java b/afs-storage-api/src/main/java/com/powsybl/afs/storage/events/NodeNameUpdated.java index b2e78e5b..e696ddd8 100644 --- a/afs-storage-api/src/main/java/com/powsybl/afs/storage/events/NodeNameUpdated.java +++ b/afs-storage-api/src/main/java/com/powsybl/afs/storage/events/NodeNameUpdated.java @@ -17,7 +17,7 @@ */ public class NodeNameUpdated extends NodeEvent { - static final String NODE_NAME_UPDATED = "NODE_NAME_UPDATED"; + public static final String TYPENAME = "NODE_NAME_UPDATED"; @JsonProperty("name") private final String name; @@ -25,7 +25,7 @@ public class NodeNameUpdated extends NodeEvent { @JsonCreator public NodeNameUpdated(@JsonProperty("id") String id, @JsonProperty("name") String name) { - super(id, NODE_NAME_UPDATED); + super(id, TYPENAME); this.name = Objects.requireNonNull(name); } diff --git a/afs-storage-api/src/main/java/com/powsybl/afs/storage/events/NodeRemoved.java b/afs-storage-api/src/main/java/com/powsybl/afs/storage/events/NodeRemoved.java index 494e80c0..a858050d 100644 --- a/afs-storage-api/src/main/java/com/powsybl/afs/storage/events/NodeRemoved.java +++ b/afs-storage-api/src/main/java/com/powsybl/afs/storage/events/NodeRemoved.java @@ -16,14 +16,14 @@ */ public class NodeRemoved extends NodeEvent { - public static final String NODE_REMOVED = "NODE_REMOVED"; + public static final String TYPENAME = "NODE_REMOVED"; @JsonProperty("parentId") protected final String parentId; @JsonCreator public NodeRemoved(@JsonProperty("id") String id, @JsonProperty("parentId") String parentId) { - super(id, NODE_REMOVED); + super(id, TYPENAME); this.parentId = Objects.requireNonNull(parentId); } diff --git a/afs-storage-api/src/main/java/com/powsybl/afs/storage/events/ParentChanged.java b/afs-storage-api/src/main/java/com/powsybl/afs/storage/events/ParentChanged.java index f864bb6b..2df9457e 100644 --- a/afs-storage-api/src/main/java/com/powsybl/afs/storage/events/ParentChanged.java +++ b/afs-storage-api/src/main/java/com/powsybl/afs/storage/events/ParentChanged.java @@ -14,11 +14,11 @@ */ public class ParentChanged extends NodeEvent { - public static final String PARENT_CHANGED = "PARENT_CHANGED"; + public static final String TYPENAME = "PARENT_CHANGED"; @JsonCreator public ParentChanged(@JsonProperty("id") String id) { - super(id, PARENT_CHANGED); + super(id, TYPENAME); } @Override diff --git a/afs-storage-api/src/main/java/com/powsybl/afs/storage/events/TimeSeriesCleared.java b/afs-storage-api/src/main/java/com/powsybl/afs/storage/events/TimeSeriesCleared.java index a4e37353..f4d424ef 100644 --- a/afs-storage-api/src/main/java/com/powsybl/afs/storage/events/TimeSeriesCleared.java +++ b/afs-storage-api/src/main/java/com/powsybl/afs/storage/events/TimeSeriesCleared.java @@ -14,11 +14,11 @@ */ public class TimeSeriesCleared extends NodeEvent { - public static final String TIME_SERIES_CLEARED = "TIME_SERIES_CLEARED"; + public static final String TYPENAME = "TIME_SERIES_CLEARED"; @JsonCreator public TimeSeriesCleared(@JsonProperty("id") String id) { - super(id, TIME_SERIES_CLEARED); + super(id, TYPENAME); } @Override diff --git a/afs-storage-api/src/main/java/com/powsybl/afs/storage/events/TimeSeriesCreated.java b/afs-storage-api/src/main/java/com/powsybl/afs/storage/events/TimeSeriesCreated.java index 9be9b7b4..5bc2453f 100644 --- a/afs-storage-api/src/main/java/com/powsybl/afs/storage/events/TimeSeriesCreated.java +++ b/afs-storage-api/src/main/java/com/powsybl/afs/storage/events/TimeSeriesCreated.java @@ -16,7 +16,7 @@ */ public class TimeSeriesCreated extends NodeEvent { - public static final String TIME_SERIES_CREATED = "TIME_SERIES_CREATED"; + public static final String TYPENAME = "TIME_SERIES_CREATED"; @JsonProperty("timeSeriesName") private final String timeSeriesName; @@ -24,7 +24,7 @@ public class TimeSeriesCreated extends NodeEvent { @JsonCreator public TimeSeriesCreated(@JsonProperty("id") String id, @JsonProperty("timeSeriesName") String timeSeriesName) { - super(id, TIME_SERIES_CREATED); + super(id, TYPENAME); this.timeSeriesName = Objects.requireNonNull(timeSeriesName); } diff --git a/afs-storage-api/src/main/java/com/powsybl/afs/storage/events/TimeSeriesDataUpdated.java b/afs-storage-api/src/main/java/com/powsybl/afs/storage/events/TimeSeriesDataUpdated.java index 3d19db19..71451bd7 100644 --- a/afs-storage-api/src/main/java/com/powsybl/afs/storage/events/TimeSeriesDataUpdated.java +++ b/afs-storage-api/src/main/java/com/powsybl/afs/storage/events/TimeSeriesDataUpdated.java @@ -16,7 +16,7 @@ */ public class TimeSeriesDataUpdated extends NodeEvent { - public static final String TIME_SERIES_DATA_UPDATED = "TIME_SERIES_DATA_UPDATED"; + public static final String TYPENAME = "TIME_SERIES_DATA_UPDATED"; @JsonProperty("timeSeriesName") private final String timeSeriesName; @@ -24,7 +24,7 @@ public class TimeSeriesDataUpdated extends NodeEvent { @JsonCreator public TimeSeriesDataUpdated(@JsonProperty("id") String id, @JsonProperty("timeSeriesName") String timeSeriesName) { - super(id, TIME_SERIES_DATA_UPDATED); + super(id, TYPENAME); this.timeSeriesName = Objects.requireNonNull(timeSeriesName); } diff --git a/afs-storage-api/src/test/java/com/powsybl/afs/storage/AbstractAppStorageTest.java b/afs-storage-api/src/test/java/com/powsybl/afs/storage/AbstractAppStorageTest.java index 297fd943..630a7ab0 100644 --- a/afs-storage-api/src/test/java/com/powsybl/afs/storage/AbstractAppStorageTest.java +++ b/afs-storage-api/src/test/java/com/powsybl/afs/storage/AbstractAppStorageTest.java @@ -24,7 +24,10 @@ import java.time.Duration; import java.util.*; import java.util.concurrent.BlockingQueue; +import java.util.concurrent.CountDownLatch; import java.util.concurrent.LinkedBlockingQueue; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicReference; import java.util.stream.Collectors; import static org.assertj.core.api.Assertions.assertThat; @@ -534,7 +537,37 @@ public void test() throws IOException, InterruptedException { // 19 check that eventsBus is not null assertNotNull(storage.getEventsBus()); - storage.getEventsBus().pushEvent(new NodeCreated("test", "test"), "test useful for RemoteStorage event push"); + storage.flush(); + eventStack.clear(); + + String topic = "some topic"; + CountDownLatch eventReceived = new CountDownLatch(1); + AtomicReference eventsCatched = new AtomicReference<>(); + storage.getEventsBus().addListener(new AppStorageListener() { + @Override + public void onEvents(NodeEventList eventList) { + eventsCatched.set(eventList); + eventReceived.countDown(); + } + + @Override + public Set topics() { + return Collections.singleton(topic); + } + }); + + NodeEvent eventToCatch = new NodeCreated("test2", "test"); + NodeEvent eventNotToCatch = new NodeCreated("test1", "test"); + storage.getEventsBus().pushEvent(eventNotToCatch, "other topic"); + storage.getEventsBus().pushEvent(eventToCatch, topic); + storage.flush(); + + eventReceived.await(1000, TimeUnit.MILLISECONDS); + assertThat(eventsCatched.get()).isNotNull(); + assertThat(eventsCatched.get().getTopic()).isEqualTo(topic); + assertThat(eventsCatched.get().getEvents()).hasSize(1); + assertThat(eventsCatched.get().getEvents().get(0)).isEqualTo(eventToCatch); + assertEventStack(eventNotToCatch, eventToCatch); } protected void testUpdateNodeMetadata(NodeInfo rootFolderInfo, AppStorage storage) throws InterruptedException { diff --git a/afs-storage-api/src/test/java/com/powsybl/afs/storage/InMemoryEventsBusTest.java b/afs-storage-api/src/test/java/com/powsybl/afs/storage/InMemoryEventsBusTest.java deleted file mode 100644 index 5552deaf..00000000 --- a/afs-storage-api/src/test/java/com/powsybl/afs/storage/InMemoryEventsBusTest.java +++ /dev/null @@ -1,38 +0,0 @@ -/** - * Copyright (c) 2017, 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/. - */ -package com.powsybl.afs.storage; - -import com.powsybl.afs.storage.events.NodeCreated; -import com.powsybl.afs.storage.events.NodeEvent; -import org.junit.Before; -import org.junit.Test; - -import java.io.IOException; - -import static org.junit.Assert.*; - -/** - * @author Geoffroy Jamgotchian - */ -public class InMemoryEventsBusTest { - - private InMemoryEventsBus eventsBus; - - @Before - public void setUp() throws Exception { - eventsBus = new InMemoryEventsBus(); - } - - @Test - public void eventsStoreTest() throws IOException { - NodeEvent nodeEvent = new NodeCreated("id", "parentId"); - eventsBus.pushEvent(nodeEvent, String.valueOf(nodeEvent.getType())); - assertEquals(1, eventsBus.getTopics().size()); - assertNotNull(eventsBus.getTopics().get(String.valueOf(nodeEvent.getType()))); - } -} - diff --git a/afs-storage-api/src/test/java/com/powsybl/afs/storage/events/NodeEventTest.java b/afs-storage-api/src/test/java/com/powsybl/afs/storage/events/NodeEventTest.java index 0c5f8ce5..6d7ba99b 100644 --- a/afs-storage-api/src/test/java/com/powsybl/afs/storage/events/NodeEventTest.java +++ b/afs-storage-api/src/test/java/com/powsybl/afs/storage/events/NodeEventTest.java @@ -32,7 +32,7 @@ public void setUp() throws Exception { public void dependencyAddedTest() throws IOException { DependencyAdded added = new DependencyAdded("a", "b"); assertEquals("a", added.getId()); - assertEquals("DEPENDENCY_ADDED", added.getType()); + assertEquals(DependencyAdded.TYPENAME, added.getType()); assertEquals("b", added.getDependencyName()); DependencyAdded added2 = objectMapper.readValue(objectMapper.writeValueAsString(added), DependencyAdded.class); @@ -48,7 +48,7 @@ public void dependencyAddedTest() throws IOException { public void backwardDependencyAddedTest() throws IOException { BackwardDependencyAdded added = new BackwardDependencyAdded("a", "b"); assertEquals("a", added.getId()); - assertEquals("BACKWARD_DEPENDENCY_ADDED", added.getType()); + assertEquals(BackwardDependencyAdded.TYPENAME, added.getType()); assertEquals("b", added.getDependencyName()); BackwardDependencyAdded added2 = objectMapper.readValue(objectMapper.writeValueAsString(added), BackwardDependencyAdded.class); @@ -64,7 +64,7 @@ public void backwardDependencyAddedTest() throws IOException { public void dependencyRemovedTest() throws IOException { DependencyRemoved removed = new DependencyRemoved("a", "b"); assertEquals("a", removed.getId()); - assertEquals("DEPENDENCY_REMOVED", removed.getType()); + assertEquals(DependencyRemoved.TYPENAME, removed.getType()); assertEquals("b", removed.getDependencyName()); DependencyRemoved removed2 = objectMapper.readValue(objectMapper.writeValueAsString(removed), DependencyRemoved.class); @@ -80,7 +80,7 @@ public void dependencyRemovedTest() throws IOException { public void backwardDependencyRemovedTest() throws IOException { BackwardDependencyRemoved removed = new BackwardDependencyRemoved("a", "b"); assertEquals("a", removed.getId()); - assertEquals("BACKWARD_DEPENDENCY_REMOVED", removed.getType()); + assertEquals(BackwardDependencyRemoved.TYPENAME, removed.getType()); assertEquals("b", removed.getDependencyName()); BackwardDependencyRemoved removed2 = objectMapper.readValue(objectMapper.writeValueAsString(removed), BackwardDependencyRemoved.class); @@ -97,7 +97,7 @@ public void nodeCreatedTest() throws IOException { NodeCreated created = new NodeCreated("a", "b"); assertEquals("a", created.getId()); assertEquals("b", created.getParentId()); - assertEquals("NODE_CREATED", created.getType()); + assertEquals(NodeCreated.TYPENAME, created.getType()); NodeCreated created2 = objectMapper.readValue(objectMapper.writeValueAsString(created), NodeCreated.class); assertEquals(created, created2); @@ -113,7 +113,7 @@ public void nodeConsistent() throws IOException { NodeConsistent nodeConsistent = new NodeConsistent("a"); assertEquals("a", nodeConsistent.getId()); assertEquals("NodeConsistent(id=a)", nodeConsistent.toString()); - assertEquals("NODE_CONSISTENT", nodeConsistent.getType()); + assertEquals(NodeConsistent.TYPENAME, nodeConsistent.getType()); NodeConsistent nodeConsistent1 = objectMapper.readValue(objectMapper.writeValueAsString(nodeConsistent), NodeConsistent.class); assertEquals(nodeConsistent, nodeConsistent1); @@ -123,7 +123,7 @@ public void nodeConsistent() throws IOException { public void nodeDataUpdatedTest() throws IOException { NodeDataUpdated updated = new NodeDataUpdated("a", "b"); assertEquals("a", updated.getId()); - assertEquals("NODE_DATA_UPDATED", updated.getType()); + assertEquals(NodeDataUpdated.TYPENAME, updated.getType()); assertEquals("b", updated.getDataName()); NodeDataUpdated updated2 = objectMapper.readValue(objectMapper.writeValueAsString(updated), NodeDataUpdated.class); @@ -139,7 +139,7 @@ public void nodeDataUpdatedTest() throws IOException { public void nodeDescriptionUpdatedTest() throws IOException { NodeDescriptionUpdated updated = new NodeDescriptionUpdated("a", "b"); assertEquals("a", updated.getId()); - assertEquals("NODE_DESCRIPTION_UPDATED", updated.getType()); + assertEquals(NodeDescriptionUpdated.TYPENAME, updated.getType()); assertEquals("b", updated.getDescription()); NodeDescriptionUpdated updated2 = objectMapper.readValue(objectMapper.writeValueAsString(updated), NodeDescriptionUpdated.class); @@ -155,7 +155,7 @@ public void nodeDescriptionUpdatedTest() throws IOException { public void nodeNameUpdatedTest() throws IOException { NodeNameUpdated updated = new NodeNameUpdated("a", "b"); assertEquals("a", updated.getId()); - assertEquals("NODE_NAME_UPDATED", updated.getType()); + assertEquals(NodeNameUpdated.TYPENAME, updated.getType()); assertEquals("b", updated.getName()); NodeNameUpdated updated2 = objectMapper.readValue(objectMapper.writeValueAsString(updated), NodeNameUpdated.class); @@ -172,7 +172,7 @@ public void nodeRemovedTest() throws IOException { NodeRemoved removed = new NodeRemoved("a", "b"); assertEquals("a", removed.getId()); assertEquals("b", removed.getParentId()); - assertEquals("NODE_REMOVED", removed.getType()); + assertEquals(NodeRemoved.TYPENAME, removed.getType()); NodeRemoved removed2 = objectMapper.readValue(objectMapper.writeValueAsString(removed), NodeRemoved.class); assertEquals(removed, removed2); @@ -187,7 +187,7 @@ public void nodeRemovedTest() throws IOException { public void parentChangedTest() throws IOException { ParentChanged changed = new ParentChanged("a"); assertEquals("a", changed.getId()); - assertEquals("PARENT_CHANGED", changed.getType()); + assertEquals(ParentChanged.TYPENAME, changed.getType()); ParentChanged changed2 = objectMapper.readValue(objectMapper.writeValueAsString(changed), ParentChanged.class); assertEquals(changed, changed2); @@ -202,7 +202,7 @@ public void parentChangedTest() throws IOException { public void timeSeriesClearedTest() throws IOException { TimeSeriesCleared cleared = new TimeSeriesCleared("a"); assertEquals("a", cleared.getId()); - assertEquals("TIME_SERIES_CLEARED", cleared.getType()); + assertEquals(TimeSeriesCleared.TYPENAME, cleared.getType()); TimeSeriesCleared cleared2 = objectMapper.readValue(objectMapper.writeValueAsString(cleared), TimeSeriesCleared.class); assertEquals(cleared, cleared2); @@ -217,7 +217,7 @@ public void timeSeriesClearedTest() throws IOException { public void timeSeriesCreatedTest() throws IOException { TimeSeriesCreated created = new TimeSeriesCreated("a", "b"); assertEquals("a", created.getId()); - assertEquals("TIME_SERIES_CREATED", created.getType()); + assertEquals(TimeSeriesCreated.TYPENAME, created.getType()); assertEquals("b", created.getTimeSeriesName()); TimeSeriesCreated created2 = objectMapper.readValue(objectMapper.writeValueAsString(created), TimeSeriesCreated.class); @@ -233,7 +233,7 @@ public void timeSeriesCreatedTest() throws IOException { public void timeSeriesDataUpdatedTest() throws IOException { TimeSeriesDataUpdated updated = new TimeSeriesDataUpdated("a", "b"); assertEquals("a", updated.getId()); - assertEquals("TIME_SERIES_DATA_UPDATED", updated.getType()); + assertEquals(TimeSeriesDataUpdated.TYPENAME, updated.getType()); assertEquals("b", updated.getTimeSeriesName()); TimeSeriesDataUpdated updated2 = objectMapper.readValue(objectMapper.writeValueAsString(updated), TimeSeriesDataUpdated.class); diff --git a/afs-ws/afs-ws-server/src/main/java/com/powsybl/afs/ws/server/NodeEventServer.java b/afs-ws/afs-ws-server/src/main/java/com/powsybl/afs/ws/server/NodeEventServer.java index 4bea8395..baf17e83 100644 --- a/afs-ws/afs-ws-server/src/main/java/com/powsybl/afs/ws/server/NodeEventServer.java +++ b/afs-ws/afs-ws-server/src/main/java/com/powsybl/afs/ws/server/NodeEventServer.java @@ -65,6 +65,7 @@ public void onMessage(Session session, NodeEventContainer nodeEventContainer) { session.getId()); AppStorage storage = appDataBean.getStorage(nodeEventContainer.getFileSystemName()); storage.getEventsBus().pushEvent(nodeEventContainer.getNodeEvent(), nodeEventContainer.getTopic()); + storage.getEventsBus().flush(); } private void removeSession(String fileSystemName, Session session) { diff --git a/afs-ws/afs-ws-server/src/test/java/com/powsybl/afs/ws/server/AppStorageServerTest.java b/afs-ws/afs-ws-server/src/test/java/com/powsybl/afs/ws/server/AppStorageServerTest.java index d5e9856c..8ac56a7e 100644 --- a/afs-ws/afs-ws-server/src/test/java/com/powsybl/afs/ws/server/AppStorageServerTest.java +++ b/afs-ws/afs-ws-server/src/test/java/com/powsybl/afs/ws/server/AppStorageServerTest.java @@ -17,7 +17,6 @@ import com.powsybl.afs.ws.client.utils.UserSession; import com.powsybl.afs.ws.server.utils.AppDataBean; import com.powsybl.afs.ws.storage.RemoteAppStorage; -import com.powsybl.afs.ws.storage.WebSocketEventsBus; import com.powsybl.afs.ws.storage.RemoteTaskMonitor; import com.powsybl.commons.exceptions.UncheckedUriSyntaxException; import org.jboss.arquillian.container.test.api.Deployment; @@ -107,14 +106,6 @@ public void getFileSystemNamesTest() { assertEquals(Collections.singletonList(AppDataBeanMock.TEST_FS_NAME), fileSystemNames); } - @Test - public void testRemoteEventsStore() throws InterruptedException { - URI restUri = getRestUri(); - RemoteAppStorage storage = new RemoteAppStorage(AppDataBeanMock.TEST_FS_NAME, restUri, - userSession.getToken()); - assertEquals(0, ((WebSocketEventsBus) storage.getEventsBus()).getTopics().size()); - } - public void createTaskRemoteTest() { RemoteTaskMonitor taskMonitor = new RemoteTaskMonitor(AppDataBeanMock.TEST_FS_NAME, getRestUri(), userSession.getToken()); NodeInfo root = storage.createRootNodeIfNotExists(storage.getFileSystemName(), Folder.PSEUDO_CLASS); diff --git a/afs-ws/afs-ws-storage/src/main/java/com/powsybl/afs/ws/storage/NodeEventClient.java b/afs-ws/afs-ws-storage/src/main/java/com/powsybl/afs/ws/storage/NodeEventClient.java index 26b3662d..b1f30bb1 100644 --- a/afs-ws/afs-ws-storage/src/main/java/com/powsybl/afs/ws/storage/NodeEventClient.java +++ b/afs-ws/afs-ws-storage/src/main/java/com/powsybl/afs/ws/storage/NodeEventClient.java @@ -59,7 +59,11 @@ public void onMessage(Session session, NodeEventList nodeEventList) { LOGGER.trace("Node event websocket session '{}' of file system '{}' received an event list: {}", session.getId(), fileSystemName, nodeEventList); listeners.log(); - listeners.notify(l -> l.onEvents(nodeEventList)); + listeners.notify(l -> { + if (l.topics().isEmpty() || l.topics().contains(nodeEventList.getTopic())) { + l.onEvents(nodeEventList); + } + }); } @OnError diff --git a/afs-ws/afs-ws-storage/src/main/java/com/powsybl/afs/ws/storage/WebSocketEventsBus.java b/afs-ws/afs-ws-storage/src/main/java/com/powsybl/afs/ws/storage/WebSocketEventsBus.java index 485e63be..55448c5c 100644 --- a/afs-ws/afs-ws-storage/src/main/java/com/powsybl/afs/ws/storage/WebSocketEventsBus.java +++ b/afs-ws/afs-ws-storage/src/main/java/com/powsybl/afs/ws/storage/WebSocketEventsBus.java @@ -27,9 +27,6 @@ */ public class WebSocketEventsBus implements EventsBus { - // To trace events per topic locally - private final Map> topics = new HashMap<>(); - private final WeakListenerList listeners = new WeakListenerList<>(); private NodeEventClient nodeEventClient; @@ -55,20 +52,9 @@ public WebSocketEventsBus(AppStorage storage, URI restUri) { @Override public void pushEvent(NodeEvent event, String topic) { - topics.computeIfAbsent(topic, k -> new ArrayList<>()); - topics.get(topic).add(event); nodeEventClient.pushEvent(event, storage.getFileSystemName(), topic); } - public Map> getTopics() { - return topics; - } - - @Override - public void flush() { - topics.clear(); - } - @Override public void addListener(AppStorageListener l) { listeners.add(l); @@ -83,4 +69,9 @@ public void removeListener(AppStorageListener l) { public void removeListeners() { listeners.removeAll(); } + + @Override + public void flush() { + // Noop + } }