use of alien4cloud.utils.TreeNode in project alien4cloud by alien4cloud.
the class AbstractUpdateFileProcessor method process.
@Override
public void process(Csar csar, Topology topology, T operation) {
// archive content tree is actually a node that contains only the folder of the topology
TreeNode root = EditionContextManager.get().getArchiveContentTree().getChildren().first();
// walk the file path to insert an element
TreeNode target = root;
if (operation.getPath().endsWith("/")) {
throw new InvalidPathException("Path <" + operation.getPath() + "> is invalid (must be a file and not a directory).");
}
// File upload management
String[] pathElements = operation.getPath().split("/");
for (int i = 0; i < pathElements.length; i++) {
String pathElement = pathElements[i];
TreeNode child = target.getChild(pathElement);
if (child == null) {
if (target.isLeaf()) {
throw new InvalidPathException("Path <" + operation.getPath() + "> is invalid (one of the folder of the path is actualy a file).");
}
// add an element
child = new TreeNode();
child.setName(pathElement);
child.setFullPath(target.getFullPath() + "/" + pathElement);
child.setParent(target);
target.getChildren().add(child);
if (i == pathElements.length - 1) {
child.setLeaf(true);
} else {
child.setChildren(new TreeSet<>());
}
}
target = child;
}
if (target.isLeaf()) {
// store the file in the local temporary file repository
// If already applied the input stream is closed and I should just get the artifact id
String artifactFileId = operation.getTempFileId();
if (artifactFileId == null) {
artifactFileId = artifactRepository.storeFile(operation.getArtifactStream());
operation.setTempFileId(artifactFileId);
// Note the input stream should be closed by the caller (controller in our situation).
operation.setArtifactStream(null);
}
try {
if (csar.getYamlFilePath().equals(operation.getPath())) {
// the operation updates the topology file, we have to parse it and override the topology data out of it.
editorTopologyUploadService.processTopology(artifactRepository.resolveFile(artifactFileId), topology.getWorkspace());
}
} catch (RuntimeException e) {
// remove the file from the temp repository if the topology cannot be parsed
artifactRepository.deleteFile(artifactFileId);
throw e;
}
// let's just impact the url to point to the temp file.
target.setArtifactId(artifactFileId);
} else {
// Fail as we cannot override a directory
throw new InvalidPathException("Path <" + operation.getPath() + "> is invalid (must be a file and not a directory).");
}
}
use of alien4cloud.utils.TreeNode in project alien4cloud by alien4cloud.
the class AbstractUpdateFileProcessor method beforeCommit.
@Override
@SneakyThrows
public void beforeCommit(T operation) {
// validate that the operation is ok for beforeCommit process
if (canPerformBeforeCommit(operation)) {
try {
TreeNode fileTreeNode = FileProcessorHelper.getFileTreeNode(operation.getPath());
Path targetPath = EditionContextManager.get().getLocalGitPath().resolve(operation.getPath());
Files.createDirectories(targetPath.getParent());
try (InputStream inputStream = artifactRepository.getFile(operation.getTempFileId())) {
Files.copy(inputStream, targetPath, StandardCopyOption.REPLACE_EXISTING);
}
artifactRepository.deleteFile(operation.getTempFileId());
fileTreeNode.setArtifactId(null);
} catch (NotFoundException e) {
log.debug("The file is not referenced in the tree, must have been deleted in later operation.", e);
}
}
}
use of alien4cloud.utils.TreeNode in project alien4cloud by alien4cloud.
the class DeleteFileProcessor method process.
@Override
public void process(Csar csar, Topology topology, DeleteFileOperation operation) {
if (csar.getYamlFilePath().equals(operation.getPath())) {
throw new InvalidPathException("Topology yaml file cannot be removed.");
}
TreeNode target = FileProcessorHelper.getFileTreeNode(operation.getPath());
target.getParent().getChildren().remove(target);
for (NodeTemplate nodeTemplate : safe(topology.getNodeTemplates()).values()) {
for (DeploymentArtifact artifact : safe(nodeTemplate.getArtifacts()).values()) {
resetRemovedArtifact(artifact, operation.getPath());
}
cleanupInterfaces(nodeTemplate.getInterfaces(), operation.getPath());
for (RelationshipTemplate relationshipTemplate : safe(nodeTemplate.getRelationships()).values()) {
cleanupInterfaces(relationshipTemplate.getInterfaces(), operation.getPath());
}
}
}
use of alien4cloud.utils.TreeNode in project alien4cloud by alien4cloud.
the class FileProcessorHelper method getFileTreeNode.
/**
* Get the tree node that represents a file from the archive under edition.
*
* @param path The path in which to lookup for the
* @return the tree node from the archive.
*/
public static TreeNode getFileTreeNode(String path) {
TreeNode root = EditionContextManager.get().getArchiveContentTree().getChildren().first();
TreeNode target = root;
String[] pathElements = path.split("/");
for (int i = 0; i < pathElements.length; i++) {
String pathElement = pathElements[i];
TreeNode child = target.getChild(pathElement);
if (child == null) {
throw new NotFoundException("The artifact specified at path <" + path + "> does not exists in the topology archive.");
}
target = child;
}
return target;
}
Aggregations