Skip to content

Commit

Permalink
AFS events Bus (powsybl#820)
Browse files Browse the repository at this point in the history
Signed-off-by: ChamseddineBhd <[email protected]>
  • Loading branch information
ChamseddineBhd authored and geofjamg committed Dec 9, 2019
1 parent 9c1eb7e commit 668689a
Show file tree
Hide file tree
Showing 88 changed files with 1,096 additions and 659 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.powsybl.afs.ProjectFileExtension;
import com.powsybl.afs.mapdb.storage.MapDbAppStorage;
import com.powsybl.afs.storage.AppStorage;
import com.powsybl.afs.storage.InMemoryEventsBus;
import com.powsybl.contingency.BranchContingency;
import com.powsybl.contingency.Contingency;
import com.powsybl.iidm.network.Line;
Expand All @@ -31,7 +32,7 @@ public class ActionScriptTest extends AbstractProjectFileTest {

@Override
protected AppStorage createStorage() {
return MapDbAppStorage.createMem("mem");
return MapDbAppStorage.createMem("mem", new InMemoryEventsBus());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
*/
package com.powsybl.afs;

import com.powsybl.afs.storage.AppStorage;
import com.powsybl.afs.storage.AppStorageArchive;
import com.powsybl.afs.storage.ListenableAppStorage;
import com.powsybl.afs.storage.NodeInfo;

import java.io.IOException;
Expand All @@ -30,11 +30,11 @@ public abstract class AbstractNodeBase<F> {

protected final NodeInfo info;

protected final ListenableAppStorage storage;
protected final AppStorage storage;

protected int codeVersion;

public AbstractNodeBase(NodeInfo info, ListenableAppStorage storage, int codeVersion) {
public AbstractNodeBase(NodeInfo info, AppStorage storage, int codeVersion) {
this.info = Objects.requireNonNull(info);
this.storage = Objects.requireNonNull(storage);
this.codeVersion = codeVersion;
Expand Down
43 changes: 38 additions & 5 deletions afs/afs-core/src/main/java/com/powsybl/afs/AppData.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
*/
package com.powsybl.afs;

import com.powsybl.afs.storage.ListenableAppStorage;
import com.powsybl.afs.storage.AppStorage;
import com.powsybl.afs.storage.EventsBus;
import com.powsybl.afs.storage.InMemoryEventsBus;
import com.powsybl.commons.util.ServiceLoaderCache;
import com.powsybl.computation.ComputationManager;

Expand Down Expand Up @@ -63,27 +65,47 @@ public class AppData implements AutoCloseable {

private final Set<Class<? extends ProjectFile>> projectFileClasses = new HashSet<>();

private final EventsBus eventsBus;

private Map<ServiceExtension.ServiceKey, Object> services;

private SecurityTokenProvider tokenProvider = () -> null;

public AppData(ComputationManager shortTimeExecutionComputationManager, ComputationManager longTimeExecutionComputationManager) {
this(shortTimeExecutionComputationManager, longTimeExecutionComputationManager, getDefaultEventsBus());
}

public AppData(ComputationManager shortTimeExecutionComputationManager, ComputationManager longTimeExecutionComputationManager, EventsBus eventsBus) {
this(shortTimeExecutionComputationManager, longTimeExecutionComputationManager,
getDefaultFileSystemProviders(), getDefaultFileExtensions(), getDefaultProjectFileExtensions(), getDefaultServiceExtensions());
getDefaultFileSystemProviders(), getDefaultFileExtensions(), getDefaultProjectFileExtensions(), getDefaultServiceExtensions(), eventsBus);
}

public AppData(ComputationManager shortTimeExecutionComputationManager,
ComputationManager longTimeExecutionComputationManager, List<AppFileSystemProvider> fileSystemProviders) {
this(shortTimeExecutionComputationManager, longTimeExecutionComputationManager,
fileSystemProviders, getDefaultFileExtensions(), getDefaultProjectFileExtensions(), getDefaultServiceExtensions());
fileSystemProviders, getDefaultEventsBus());
}

public AppData(ComputationManager shortTimeExecutionComputationManager,
ComputationManager longTimeExecutionComputationManager, List<AppFileSystemProvider> fileSystemProviders, EventsBus eventsBus) {
this(shortTimeExecutionComputationManager, longTimeExecutionComputationManager,
fileSystemProviders, getDefaultFileExtensions(), getDefaultProjectFileExtensions(), getDefaultServiceExtensions(), eventsBus);
}

public AppData(ComputationManager shortTimeExecutionComputationManager, ComputationManager longTimeExecutionComputationManager,
List<AppFileSystemProvider> fileSystemProviders, List<FileExtension> fileExtensions,
List<ProjectFileExtension> projectFileExtensions, List<ServiceExtension> serviceExtensions) {
this(shortTimeExecutionComputationManager, longTimeExecutionComputationManager, fileSystemProviders, fileExtensions,
projectFileExtensions, serviceExtensions, getDefaultEventsBus());
}

public AppData(ComputationManager shortTimeExecutionComputationManager, ComputationManager longTimeExecutionComputationManager,
List<AppFileSystemProvider> fileSystemProviders, List<FileExtension> fileExtensions,
List<ProjectFileExtension> projectFileExtensions, List<ServiceExtension> serviceExtensions, EventsBus eventsBus) {
Objects.requireNonNull(fileSystemProviders);
Objects.requireNonNull(fileExtensions);
Objects.requireNonNull(projectFileExtensions);
this.eventsBus = Objects.requireNonNull(eventsBus);
this.shortTimeExecutionComputationManager = Objects.requireNonNull(shortTimeExecutionComputationManager);
this.longTimeExecutionComputationManager = longTimeExecutionComputationManager;
this.fileSystemProviders = Objects.requireNonNull(fileSystemProviders);
Expand Down Expand Up @@ -112,14 +134,18 @@ private static List<ProjectFileExtension> getDefaultProjectFileExtensions() {
return new ServiceLoaderCache<>(ProjectFileExtension.class).getServices();
}

private static EventsBus getDefaultEventsBus() {
return new InMemoryEventsBus();
}

private static List<ServiceExtension> getDefaultServiceExtensions() {
return new ServiceLoaderCache<>(ServiceExtension.class).getServices();
}

private void loadFileSystems() {
if (fileSystems == null) {
fileSystems = new HashMap<>();
AppFileSystemProviderContext context = new AppFileSystemProviderContext(shortTimeExecutionComputationManager, tokenProvider.getToken());
AppFileSystemProviderContext context = new AppFileSystemProviderContext(shortTimeExecutionComputationManager, tokenProvider.getToken(), eventsBus);
for (AppFileSystemProvider provider : fileSystemProviders) {
for (AppFileSystem fileSystem : provider.getFileSystems(context)) {
fileSystem.setData(this);
Expand Down Expand Up @@ -256,7 +282,7 @@ public List<String> getRemotelyAccessibleFileSystemNames() {
/**
* Gets low level storage interface for remotely accessible file systems. Should not be used by the AFS API users.
*/
public ListenableAppStorage getRemotelyAccessibleStorage(String fileSystemName) {
public AppStorage getRemotelyAccessibleStorage(String fileSystemName) {
Objects.requireNonNull(fileSystemName);
loadFileSystems();
AppFileSystem afs = fileSystems.get(fileSystemName);
Expand Down Expand Up @@ -285,6 +311,13 @@ <U> U findService(Class<U> serviceClass, boolean remoteStorage) {
return service;
}

/**
* get the appData event Store instance.
*/
public EventsBus getEventsBus() {
return eventsBus;
}

/**
* Closes any resources used by underlying file systems.
*/
Expand Down
10 changes: 4 additions & 6 deletions afs/afs-core/src/main/java/com/powsybl/afs/AppFileSystem.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
import com.google.common.base.Supplier;
import com.google.common.base.Suppliers;
import com.powsybl.afs.storage.AppStorage;
import com.powsybl.afs.storage.DefaultListenableAppStorage;
import com.powsybl.afs.storage.ListenableAppStorage;
import com.powsybl.afs.storage.NodeInfo;

import java.util.Objects;
Expand Down Expand Up @@ -43,7 +41,7 @@ public class AppFileSystem implements AutoCloseable {

private final boolean remotelyAccessible;

private final ListenableAppStorage storage;
private final AppStorage storage;

private final Supplier<NodeInfo> rootNodeInfo;

Expand All @@ -52,10 +50,10 @@ public class AppFileSystem implements AutoCloseable {
private AppData data;

public AppFileSystem(String name, boolean remotelyAccessible, AppStorage storage) {
this(name, remotelyAccessible, new DefaultListenableAppStorage(storage), new LocalTaskMonitor());
this(name, remotelyAccessible, storage, new LocalTaskMonitor());
}

public AppFileSystem(String name, boolean remotelyAccessible, ListenableAppStorage storage, TaskMonitor taskMonitor) {
public AppFileSystem(String name, boolean remotelyAccessible, AppStorage storage, TaskMonitor taskMonitor) {
this.name = Objects.requireNonNull(name);
this.remotelyAccessible = remotelyAccessible;
this.storage = Objects.requireNonNull(storage);
Expand All @@ -71,7 +69,7 @@ public boolean isRemotelyAccessible() {
return remotelyAccessible;
}

ListenableAppStorage getStorage() {
AppStorage getStorage() {
return storage;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/
package com.powsybl.afs;

import com.powsybl.afs.storage.EventsBus;
import com.powsybl.computation.ComputationManager;

import java.util.Objects;
Expand All @@ -19,9 +20,12 @@ public class AppFileSystemProviderContext {

private final String token;

public AppFileSystemProviderContext(ComputationManager computationManager, String token) {
private final EventsBus eventsBus;

public AppFileSystemProviderContext(ComputationManager computationManager, String token, EventsBus eventsBus) {
this.computationManager = Objects.requireNonNull(computationManager);
this.token = token;
this.eventsBus = Objects.requireNonNull(eventsBus);
}

public ComputationManager getComputationManager() {
Expand All @@ -31,4 +35,8 @@ public ComputationManager getComputationManager() {
public String getToken() {
return token;
}

public EventsBus getEventsBus() {
return eventsBus;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/
package com.powsybl.afs;

import com.powsybl.afs.storage.ListenableAppStorage;
import com.powsybl.afs.storage.AppStorage;
import com.powsybl.afs.storage.NodeInfo;

import java.util.Objects;
Expand All @@ -18,11 +18,11 @@ public class FileCreationContext {

private final NodeInfo info;

private final ListenableAppStorage storage;
private final AppStorage storage;

private final AppFileSystem fileSystem;

public FileCreationContext(NodeInfo info, ListenableAppStorage storage, AppFileSystem fileSystem) {
public FileCreationContext(NodeInfo info, AppStorage storage, AppFileSystem fileSystem) {
this.info = Objects.requireNonNull(info);
this.storage = Objects.requireNonNull(storage);
this.fileSystem = Objects.requireNonNull(fileSystem);
Expand All @@ -32,7 +32,7 @@ public NodeInfo getInfo() {
return info;
}

public ListenableAppStorage getStorage() {
public AppStorage getStorage() {
return storage;
}

Expand Down
7 changes: 6 additions & 1 deletion afs/afs-core/src/main/java/com/powsybl/afs/ProjectFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@
*/
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<ProjectFileListener> listeners = new WeakListenerList<>();

private final AppStorageListener l = eventList -> {
Expand All @@ -48,7 +53,7 @@ public class ProjectFile extends ProjectNode {

protected ProjectFile(ProjectFileCreationContext context, int codeVersion) {
super(context, codeVersion, true);
storage.addListener(l);
storage.getEventsBus().addListener(l);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/
package com.powsybl.afs;

import com.powsybl.afs.storage.ListenableAppStorage;
import com.powsybl.afs.storage.AppStorage;
import com.powsybl.afs.storage.NodeInfo;

import java.util.Objects;
Expand All @@ -18,7 +18,7 @@ public class ProjectFileBuildContext extends ProjectFileContext {

private final NodeInfo folderInfo;

public ProjectFileBuildContext(NodeInfo folderInfo, ListenableAppStorage storage, Project project) {
public ProjectFileBuildContext(NodeInfo folderInfo, AppStorage storage, Project project) {
super(storage, project);
this.folderInfo = Objects.requireNonNull(folderInfo);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/
package com.powsybl.afs;

import com.powsybl.afs.storage.ListenableAppStorage;
import com.powsybl.afs.storage.AppStorage;

import java.util.Objects;

Expand All @@ -15,16 +15,16 @@
*/
public class ProjectFileContext {

private final ListenableAppStorage storage;
private final AppStorage storage;

private final Project project;

protected ProjectFileContext(ListenableAppStorage storage, Project project) {
protected ProjectFileContext(AppStorage storage, Project project) {
this.storage = Objects.requireNonNull(storage);
this.project = Objects.requireNonNull(project);
}

public ListenableAppStorage getStorage() {
public AppStorage getStorage() {
return storage;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/
package com.powsybl.afs;

import com.powsybl.afs.storage.ListenableAppStorage;
import com.powsybl.afs.storage.AppStorage;
import com.powsybl.afs.storage.NodeInfo;

import java.util.Objects;
Expand All @@ -18,7 +18,7 @@ public class ProjectFileCreationContext extends ProjectFileContext {

private final NodeInfo info;

public ProjectFileCreationContext(NodeInfo info, ListenableAppStorage storage, Project project) {
public ProjectFileCreationContext(NodeInfo info, AppStorage storage, Project project) {
super(storage, project);
this.info = Objects.requireNonNull(info);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ public class ProjectFolder extends ProjectNode implements FolderBase<ProjectNode

public static final String PSEUDO_CLASS = "projectFolder";
public static final int VERSION = 0;
private static final String NODE_CREATED = "NODE_CREATED";
private static final String NODE_REMOVED = "NODE_REMOVED";

private final WeakListenerList<ProjectFolderListener> listeners = new WeakListenerList<>();

Expand All @@ -58,7 +60,7 @@ public class ProjectFolder extends ProjectNode implements FolderBase<ProjectNode

public ProjectFolder(ProjectFileCreationContext context) {
super(context, VERSION, true);
storage.addListener(l);
storage.getEventsBus().addListener(l);
}

/**
Expand Down
13 changes: 8 additions & 5 deletions afs/afs-core/src/test/java/com/powsybl/afs/AfsBaseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import com.google.common.jimfs.Jimfs;
import com.powsybl.afs.mapdb.storage.MapDbAppStorage;
import com.powsybl.afs.storage.AppStorage;
import com.powsybl.afs.storage.DefaultListenableAppStorage;
import com.powsybl.afs.storage.InMemoryEventsBus;
import com.powsybl.computation.ComputationManager;
import com.powsybl.iidm.network.NetworkFactoryService;
import org.junit.After;
Expand Down Expand Up @@ -47,13 +47,15 @@ public class AfsBaseTest {
@Before
public void setup() {
fileSystem = Jimfs.newFileSystem(Configuration.unix());
storage = new DefaultListenableAppStorage(MapDbAppStorage.createMem("mem"));

ComputationManager computationManager = Mockito.mock(ComputationManager.class);
afs = new AppFileSystem("mem", true, storage);
ad = new AppData(computationManager, computationManager, Collections.singletonList(computationManager1 -> Collections.singletonList(afs)),
ad = new AppData(computationManager, computationManager, Collections.emptyList(),
Collections.emptyList(), Collections.singletonList(new FooFileExtension()), Collections.emptyList());

storage = MapDbAppStorage.createMem("mem", ad.getEventsBus());

afs = new AppFileSystem("mem", true, storage);
afs.setData(ad);
ad.addFileSystem(afs);
}

@After
Expand All @@ -64,6 +66,7 @@ public void tearDown() throws Exception {

@Test
public void baseTest() {
assertSame(InMemoryEventsBus.class, ad.getEventsBus().getClass());
assertSame(afs, ad.getFileSystem("mem"));
assertNull(ad.getFileSystem("???"));
assertEquals(Collections.singletonList("mem"), ad.getRemotelyAccessibleFileSystemNames());
Expand Down
Loading

0 comments on commit 668689a

Please sign in to comment.