use of org.alien4cloud.tosca.model.CsarDependenciesBean in project alien4cloud by alien4cloud.
the class CsarGitService method sort.
private List<CsarDependenciesBean> sort(Map<CSARDependency, CsarDependenciesBean> elements) {
List<CsarDependenciesBean> sortedCsars = Lists.newArrayList();
List<CsarDependenciesBean> independents = Lists.newArrayList();
for (Map.Entry<CSARDependency, CsarDependenciesBean> entry : elements.entrySet()) {
CsarDependenciesBean csar = entry.getValue();
if (csar.getDependencies() == null) {
// the element has no dependencies
independents.add(csar);
} else {
// complete the list of dependent elements
List<CSARDependency> toClears = Lists.newArrayList();
for (CSARDependency dependency : csar.getDependencies()) {
CsarDependenciesBean providedDependency = elements.get(dependency);
if (providedDependency == null) {
// remove the dependency as it may be in the alien repo
toClears.add(dependency);
} else {
providedDependency.getDependents().add(csar);
}
}
for (CSARDependency toClear : toClears) {
csar.getDependencies().remove(toClear);
}
if (csar.getDependencies().isEmpty()) {
independents.add(csar);
}
}
}
while (!independents.isEmpty()) {
CsarDependenciesBean independent = independents.remove(0);
// remove from the elements
elements.remove(independent.getSelf());
// element has no more dependencies
sortedCsars.add(independent);
for (CsarDependenciesBean dependent : independent.getDependents()) {
dependent.getDependencies().remove(independent.getSelf());
if (dependent.getDependencies().isEmpty()) {
independents.add(dependent);
}
}
}
if (elements.size() > 0) {
// TODO there is looping dependencies throw exception or ignore ?
}
return sortedCsars;
}
use of org.alien4cloud.tosca.model.CsarDependenciesBean in project alien4cloud by alien4cloud.
the class CsarGitService method processImport.
private List<ParsingResult<Csar>> processImport(CsarGitRepository csarGitRepository, CsarGitCheckoutLocation csarGitCheckoutLocation, String gitHash) {
// find all the archives under the given hierarchy and zip them to create archives
Path archiveZipRoot = tempZipDirPath.resolve(csarGitRepository.getId());
Path archiveGitRoot = tempDirPath.resolve(csarGitRepository.getId());
if (csarGitCheckoutLocation.getSubPath() != null && !csarGitCheckoutLocation.getSubPath().isEmpty()) {
archiveGitRoot = archiveGitRoot.resolve(csarGitCheckoutLocation.getSubPath());
}
Set<Path> archivePaths = csarFinderService.prepare(archiveGitRoot, archiveZipRoot);
List<ParsingResult<Csar>> parsingResults = Lists.newArrayList();
Map<CSARDependency, CsarDependenciesBean> csarDependenciesBeans = uploadService.preParsing(archivePaths, parsingResults);
List<CsarDependenciesBean> sorted = sort(csarDependenciesBeans);
for (CsarDependenciesBean csarBean : sorted) {
String archiveRepoPath = archiveZipRoot.relativize(csarBean.getPath().getParent()).toString();
if (csarGitCheckoutLocation.getLastImportedHash() != null && csarGitCheckoutLocation.getLastImportedHash().equals(gitHash) && csarService.get(csarBean.getSelf().getName(), csarBean.getSelf().getVersion()) != null) {
// no commit since last import and the archive still exist in the repo, so do not import
addAlreadyImportParsingResult(archiveRepoPath, parsingResults);
continue;
}
try {
// FIXME Add possibility to choose an workspace
ParsingResult<Csar> result = uploadService.upload(csarBean.getPath(), CSARSource.GIT, AlienConstants.GLOBAL_WORKSPACE_ID);
result.getContext().setFileName(archiveRepoPath + "/" + result.getContext().getFileName());
parsingResults.add(result);
} catch (ParsingException e) {
ParsingResult<Csar> failedResult = new ParsingResult<>();
failedResult.setContext(new ParsingContext(archiveRepoPath));
failedResult.getContext().setParsingErrors(e.getParsingErrors());
parsingResults.add(failedResult);
log.debug("Failed to import archive from git as it cannot be parsed", e);
} catch (AlreadyExistException | ToscaTypeAlreadyDefinedInOtherCSAR | CSARUsedInActiveDeployment e) {
ParsingResult<Csar> failedResult = new ParsingResult<>();
failedResult.setContext(new ParsingContext(archiveRepoPath));
failedResult.getContext().setParsingErrors(Lists.newArrayList(UploadExceptionUtil.parsingErrorFromException(e)));
parsingResults.add(failedResult);
}
}
return parsingResults;
}
use of org.alien4cloud.tosca.model.CsarDependenciesBean in project alien4cloud by alien4cloud.
the class ArchiveUploadService method preParsing.
@ToscaContextual
public Map<CSARDependency, CsarDependenciesBean> preParsing(Set<Path> paths, List<ParsingResult<Csar>> parsingResults) {
Map<CSARDependency, CsarDependenciesBean> csarDependenciesBeans = Maps.newHashMap();
for (Path path : paths) {
try {
// FIXME cleanup git import archives
ParsingResult<CsarDependenciesBean> parsingResult = parser.parseImports(path);
parsingResult.getResult().setPath(path);
csarDependenciesBeans.put(parsingResult.getResult().getSelf(), parsingResult.getResult());
} catch (ParsingException e) {
ParsingResult<Csar> failedResult = new ParsingResult<>();
failedResult.setContext(new ParsingContext(path.getFileName().toString()));
failedResult.getContext().setParsingErrors(e.getParsingErrors());
parsingResults.add(failedResult);
log.debug("Not able to parse archive, ignoring it", e);
}
}
return csarDependenciesBeans;
}
use of org.alien4cloud.tosca.model.CsarDependenciesBean in project alien4cloud by alien4cloud.
the class ToscaArchiveParser method initDependencyBeanFromToscaMeta.
private CsarDependenciesBean initDependencyBeanFromToscaMeta(ToscaMeta toscaMeta) {
CsarDependenciesBean csarDependenciesBean = new CsarDependenciesBean();
csarDependenciesBean.setSelf(new CSARDependency(toscaMeta.getName(), toscaMeta.getVersion()));
return csarDependenciesBean;
}
use of org.alien4cloud.tosca.model.CsarDependenciesBean in project alien4cloud by alien4cloud.
the class ToscaArchiveParser method parseImports.
@ToscaContextual(requiresNew = true)
public ParsingResult<CsarDependenciesBean> parseImports(Path archiveFile) throws ParsingException {
try (FileSystem csarFS = FileSystems.newFileSystem(archiveFile, null)) {
if (Files.exists(csarFS.getPath(TOSCA_META_FILE_LOCATION))) {
YamlSimpleParser<ToscaMeta> parser = new YamlSimpleParser<ToscaMeta>(toscaMetaMapping.getParser());
ParsingResult<ToscaMeta> parsingResult = parser.parseFile(csarFS.getPath(TOSCA_META_FILE_LOCATION));
CsarDependenciesBean csarDependenciesBean = initDependencyBeanFromToscaMeta(parsingResult.getResult());
return parseFromToscaMeta(csarFS, parsingResult.getResult(), TOSCA_META_FILE_LOCATION, csarDependenciesBean, toscaImportParser);
}
return parseFromRootDefinitions(csarFS, toscaImportParser);
} catch (IOException e) {
log.error("Unable to read uploaded archive [" + archiveFile + "]", e);
throw new ParsingException("Archive", new ParsingError(ErrorCode.FAILED_TO_READ_FILE, "Problem happened while accessing file", null, null, null, archiveFile.toString()));
} catch (ProviderNotFoundException e) {
log.warn("Failed to import archive", e);
throw new ParsingException("Archive", new ParsingError(ErrorCode.ERRONEOUS_ARCHIVE_FILE, "File is not in good format, only zip file is supported ", null, e.getMessage(), null, null));
}
}
Aggregations