use of org.alien4cloud.tosca.editor.operations.AbstractEditorOperation in project alien4cloud by alien4cloud.
the class EditorTopologyRecoveryHelperService method buildRecoveryOperations.
/**
* Given a set of dependencies, analyse a given topology and build a list of {@link AbstractEditorOperation} to apply to the topology to make it synch
* with the dependencies present in the repository
*
* @param topology The topology we want to recover
* @param updatedDependencies The updated dependencies within the topology
* @return a list of {@link AbstractEditorOperation} representing the operations to perform on the topology for recovery
*/
public List<AbstractEditorOperation> buildRecoveryOperations(Topology topology, Set<CSARDependency> updatedDependencies) {
List<AbstractEditorOperation> recoveryOperations = Lists.newArrayList();
if (!topology.isEmpty()) {
for (CSARDependency updatedDependency : AlienUtils.safe(updatedDependencies)) {
buildNodesRecoveryOperations(topology, updatedDependency, recoveryOperations);
buildRelationshipsRecoveryOperations(topology, updatedDependency, recoveryOperations);
}
}
return recoveryOperations;
}
use of org.alien4cloud.tosca.editor.operations.AbstractEditorOperation in project alien4cloud by alien4cloud.
the class EditionContextManager method setup.
@PostConstruct
public void setup() {
// initialize the cache
contextCache = CacheBuilder.newBuilder().expireAfterAccess(30, TimeUnit.MINUTES).removalListener(new RemovalListener<String, EditionContext>() {
@Override
public void onRemoval(RemovalNotification<String, EditionContext> removalNotification) {
log.debug("Topology edition context with id {} has been evicted. {} pending operations are lost.", removalNotification.getKey(), removalNotification.getValue().getOperations().size());
for (AbstractEditorOperation operation : removalNotification.getValue().getOperations()) {
if (operation instanceof UpdateFileOperation) {
String fileId = ((UpdateFileOperation) operation).getTempFileId();
if (artifactRepository.isFileExist(fileId)) {
artifactRepository.deleteFile(fileId);
}
}
}
}
}).build(new CacheLoader<String, EditionContext>() {
@Override
public EditionContext load(String csarId) throws Exception {
log.debug("Loading edition context for archive {}", csarId);
Csar csar = csarService.getOrFail(csarId);
Topology topology = topologyServiceCore.getOrFail(csarId);
// check if the topology git repository has been created already
Path topologyGitPath = repositoryService.createGitDirectory(csar);
log.debug("Edition context for archive {} loaded", csar);
return new EditionContext(csar, topology, topologyGitPath);
}
});
}
use of org.alien4cloud.tosca.editor.operations.AbstractEditorOperation in project alien4cloud by alien4cloud.
the class EditorService method checkSynchronization.
/**
* Ensure that the request is synchronized with the current state of the edition.
*
* @param operation, The operation under evaluation.
*/
private synchronized void checkSynchronization(AbstractEditorOperation operation) {
// there is an operation being processed so just fail (nobody could get the notification)
if (EditionContextManager.get().getCurrentOperation() != null) {
throw new EditionConcurrencyException();
}
List<AbstractEditorOperation> operations = EditionContextManager.get().getOperations();
// if someone performed some operations we have to ensure that the new operation is performed on top of a synchronized topology
if (EditionContextManager.get().getLastOperationIndex() == -1) {
if (operation.getPreviousOperationId() != null) {
throw new EditionConcurrencyException();
}
} else if (!operations.get(EditionContextManager.get().getLastOperationIndex()).getId().equals(operation.getPreviousOperationId())) {
throw new EditionConcurrencyException();
}
operation.setId(UUID.randomUUID().toString());
EditionContextManager.get().setCurrentOperation(operation);
}
use of org.alien4cloud.tosca.editor.operations.AbstractEditorOperation in project alien4cloud by alien4cloud.
the class EditorService method undoRedo.
/**
* Undo or redo operations until the given index (including)
*
* @param topologyId The id of the topology for which to undo or redo operations.
* @param at The index on which to place the undo/redo cursor (-1 means no operations, then 0 is first operation etc.)
* @param lastOperationId The last known operation id for client optimistic locking.
* @return The topology DTO.
*/
public TopologyDTO undoRedo(String topologyId, int at, String lastOperationId) {
try {
initContext(topologyId, lastOperationId);
if (-1 > at || at > EditionContextManager.get().getOperations().size()) {
throw new NotFoundException("Unable to find the requested index for undo/redo");
}
checkTopologyRecovery();
if (at == EditionContextManager.get().getLastOperationIndex()) {
// nothing to change.
return dtoBuilder.buildTopologyDTO(EditionContextManager.get());
}
// TODO Improve this by avoiding dao query for (deep) cloning topology and keeping cache for TOSCA types that are required.
editionContextManager.reset();
Topology topology = EditionContextManager.getTopology();
Csar csar = EditionContextManager.getCsar();
for (int i = 0; i < at + 1; i++) {
AbstractEditorOperation operation = EditionContextManager.get().getOperations().get(i);
IEditorOperationProcessor processor = processorMap.get(operation.getClass());
processor.process(csar, topology, operation);
}
EditionContextManager.get().setLastOperationIndex(at);
return dtoBuilder.buildTopologyDTO(EditionContextManager.get());
} catch (IOException e) {
// FIXME undo should be fail-safe...
return null;
} finally {
EditionContextManager.get().setCurrentOperation(null);
editionContextManager.destroy();
}
}
use of org.alien4cloud.tosca.editor.operations.AbstractEditorOperation in project alien4cloud by alien4cloud.
the class EditorService method initContext.
/**
* Call this method only for checking optimistic locking and initializing edition context for method that don't process an operation (save, undo etc.)
*
* @param topologyId The id of the topology under edition.
* @param lastOperationId The id of the last operation.
*/
private void initContext(String topologyId, String lastOperationId) {
// create a fake operation for optimistic locking
AbstractEditorOperation optimisticLockOperation = new AbstractEditorOperation() {
@Override
public String commitMessage() {
return "This operation will never be enqueued and never commited.";
}
};
optimisticLockOperation.setPreviousOperationId(lastOperationId);
// init the edition context with the fake operation.
initContext(topologyId, optimisticLockOperation);
}
Aggregations