Search in sources :

Example 1 with CsarDependenciesBean

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;
}
Also used : CsarDependenciesBean(org.alien4cloud.tosca.model.CsarDependenciesBean) Map(java.util.Map) CSARDependency(org.alien4cloud.tosca.model.CSARDependency)

Example 2 with CsarDependenciesBean

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;
}
Also used : Path(java.nio.file.Path) Csar(org.alien4cloud.tosca.model.Csar) ParsingContext(alien4cloud.tosca.parser.ParsingContext) ToscaTypeAlreadyDefinedInOtherCSAR(alien4cloud.component.repository.exception.ToscaTypeAlreadyDefinedInOtherCSAR) CsarDependenciesBean(org.alien4cloud.tosca.model.CsarDependenciesBean) CSARDependency(org.alien4cloud.tosca.model.CSARDependency) ParsingResult(alien4cloud.tosca.parser.ParsingResult) ParsingException(alien4cloud.tosca.parser.ParsingException) CSARUsedInActiveDeployment(alien4cloud.component.repository.exception.CSARUsedInActiveDeployment) AlreadyExistException(alien4cloud.exception.AlreadyExistException)

Example 3 with CsarDependenciesBean

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;
}
Also used : Path(java.nio.file.Path) ParsingResult(alien4cloud.tosca.parser.ParsingResult) ParsingContext(alien4cloud.tosca.parser.ParsingContext) ParsingException(alien4cloud.tosca.parser.ParsingException) CsarDependenciesBean(org.alien4cloud.tosca.model.CsarDependenciesBean) CSARDependency(org.alien4cloud.tosca.model.CSARDependency) ToscaContextual(alien4cloud.tosca.context.ToscaContextual)

Example 4 with CsarDependenciesBean

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;
}
Also used : CsarDependenciesBean(org.alien4cloud.tosca.model.CsarDependenciesBean) CSARDependency(org.alien4cloud.tosca.model.CSARDependency)

Example 5 with 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));
    }
}
Also used : ProviderNotFoundException(java.nio.file.ProviderNotFoundException) ToscaMeta(alien4cloud.tosca.model.ToscaMeta) FileSystem(java.nio.file.FileSystem) IOException(java.io.IOException) CsarDependenciesBean(org.alien4cloud.tosca.model.CsarDependenciesBean) ToscaContextual(alien4cloud.tosca.context.ToscaContextual)

Aggregations

CsarDependenciesBean (org.alien4cloud.tosca.model.CsarDependenciesBean)5 CSARDependency (org.alien4cloud.tosca.model.CSARDependency)4 ToscaContextual (alien4cloud.tosca.context.ToscaContextual)2 ParsingContext (alien4cloud.tosca.parser.ParsingContext)2 ParsingException (alien4cloud.tosca.parser.ParsingException)2 ParsingResult (alien4cloud.tosca.parser.ParsingResult)2 Path (java.nio.file.Path)2 CSARUsedInActiveDeployment (alien4cloud.component.repository.exception.CSARUsedInActiveDeployment)1 ToscaTypeAlreadyDefinedInOtherCSAR (alien4cloud.component.repository.exception.ToscaTypeAlreadyDefinedInOtherCSAR)1 AlreadyExistException (alien4cloud.exception.AlreadyExistException)1 ToscaMeta (alien4cloud.tosca.model.ToscaMeta)1 IOException (java.io.IOException)1 FileSystem (java.nio.file.FileSystem)1 ProviderNotFoundException (java.nio.file.ProviderNotFoundException)1 Map (java.util.Map)1 Csar (org.alien4cloud.tosca.model.Csar)1