Search in sources :

Example 16 with CSARDependency

use of org.alien4cloud.tosca.model.CSARDependency in project alien4cloud by alien4cloud.

the class ToscaTypeLoader method addNewDependency.

/**
 * Add a dependency
 *
 * @param dependency
 * @param type
 * @return True if the dependecy has been upgraded into the topology. False if not.
 */
private boolean addNewDependency(CSARDependency dependency, String type) {
    CSARDependency currentDependency = getDependencyWithName(dependency.getName());
    // New dependency that never exists before
    if (currentDependency == null) {
        dependenciesMap.put(dependency, Sets.newHashSet(type));
        return false;
    }
    // The new version is more recent, we will override with new version with warning
    if (VersionUtil.compare(dependency.getVersion(), currentDependency.getVersion()) > 0) {
        Set<String> typesLoadedByConflictingArchive = dependenciesMap.remove(currentDependency);
        typesLoadedByConflictingArchive.add(type);
        dependenciesMap.put(dependency, typesLoadedByConflictingArchive);
        log.warn("Version conflicting for archive [" + dependency.getName() + "] override current version [" + currentDependency.getVersion() + "] with [" + dependency.getVersion() + "]");
        return true;
    }
    log.warn("Version conflicting for archive [" + dependency.getName() + "] do not override and use current version [" + currentDependency.getVersion() + "] ignore old version [" + dependency.getVersion() + "]");
    dependenciesMap.get(currentDependency).add(type);
    return false;
}
Also used : CSARDependency(org.alien4cloud.tosca.model.CSARDependency)

Example 17 with CSARDependency

use of org.alien4cloud.tosca.model.CSARDependency in project alien4cloud by alien4cloud.

the class TopologyService method loadType.

/**
 * Load a type into the topology (add dependency for this new type, upgrade if necessary ...)
 *
 * If the dependency added has been upgraded into the topology, then recover the topology
 *
 * @param topology the topology
 * @param element the element to load
 * @param <T> tosca element type
 * @return The real loaded element if element given in argument is from older archive than topology's dependencies
 */
@SuppressWarnings("unchecked")
public <T extends AbstractToscaType> T loadType(Topology topology, T element) {
    String type = element.getElementId();
    String archiveName = element.getArchiveName();
    String archiveVersion = element.getArchiveVersion();
    CSARDependency toLoadDependency = csarDependencyLoader.buildDependencyBean(archiveName, archiveVersion);
    // FIXME Transitive dependencies could change here and thus types be affected ?
    ToscaTypeLoader typeLoader = initializeTypeLoader(topology, true);
    boolean upgraded = typeLoader.loadType(type, toLoadDependency);
    ToscaContext.get().resetDependencies(typeLoader.getLoadedDependencies());
    // Validate does not induce missing types
    try {
        this.checkForMissingTypes(topology);
    } catch (NotFoundException e) {
        // Revert changes made to the Context then throw.
        ToscaContext.get().resetDependencies(topology.getDependencies());
        throw new VersionConflictException("Adding the type [" + element.getId() + "] from archive [" + element.getArchiveName() + ":" + element.getArchiveVersion() + "] changes the topology dependencies and induces missing types. " + "Try with another version instead. Not found : [" + e.getMessage() + "].", e);
    }
    topology.setDependencies(typeLoader.getLoadedDependencies());
    // recover the topology if needed
    if (upgraded) {
        recover(topology, toLoadDependency);
    }
    return ToscaContext.getOrFail((Class<T>) element.getClass(), type);
}
Also used : VersionConflictException(alien4cloud.exception.VersionConflictException) ToscaTypeLoader(alien4cloud.tosca.container.ToscaTypeLoader) NotFoundException(alien4cloud.exception.NotFoundException) CSARDependency(org.alien4cloud.tosca.model.CSARDependency)

Example 18 with CSARDependency

use of org.alien4cloud.tosca.model.CSARDependency 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 19 with CSARDependency

use of org.alien4cloud.tosca.model.CSARDependency 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 20 with CSARDependency

use of org.alien4cloud.tosca.model.CSARDependency in project alien4cloud by alien4cloud.

the class TopologyStepDefinitions method The_topology_should_have_as_dependencies.

@And("^The topology should have as dependencies$")
public void The_topology_should_have_as_dependencies(DataTable dependencies) throws Throwable {
    Set<CSARDependency> expectedDependencies = Sets.newHashSet();
    for (List<String> row : dependencies.raw()) {
        expectedDependencies.add(new CSARDependency(row.get(0), row.get(1)));
    }
    String topologyResponseText = Context.getInstance().getRestResponse();
    Set<CSARDependency> actualDependencies = JsonUtil.read(topologyResponseText, TopologyDTO.class, Context.getJsonMapper()).getData().getTopology().getDependencies();
    Assert.assertEquals(expectedDependencies, actualDependencies);
}
Also used : TopologyDTO(alien4cloud.topology.TopologyDTO) CSARDependency(org.alien4cloud.tosca.model.CSARDependency) And(cucumber.api.java.en.And)

Aggregations

CSARDependency (org.alien4cloud.tosca.model.CSARDependency)50 Csar (org.alien4cloud.tosca.model.Csar)16 Test (org.junit.Test)11 NodeTemplate (org.alien4cloud.tosca.model.templates.NodeTemplate)8 Set (java.util.Set)7 ArchiveRoot (alien4cloud.tosca.model.ArchiveRoot)6 CapabilityType (org.alien4cloud.tosca.model.types.CapabilityType)6 ServiceResource (alien4cloud.model.service.ServiceResource)5 Capability (org.alien4cloud.tosca.model.templates.Capability)5 Topology (org.alien4cloud.tosca.model.templates.Topology)5 NodeType (org.alien4cloud.tosca.model.types.NodeType)5 NotFoundException (alien4cloud.exception.NotFoundException)4 ParsingError (alien4cloud.tosca.parser.ParsingError)4 HashSet (java.util.HashSet)4 CsarDependenciesBean (org.alien4cloud.tosca.model.CsarDependenciesBean)4 LocationResourceTemplate (alien4cloud.model.orchestrators.locations.LocationResourceTemplate)3 ParsingException (alien4cloud.tosca.parser.ParsingException)3 VersionConflictException (alien4cloud.exception.VersionConflictException)2 DeploymentTopology (alien4cloud.model.deployment.DeploymentTopology)2 Location (alien4cloud.model.orchestrators.locations.Location)2