Search in sources :

Example 41 with ArchiveRoot

use of alien4cloud.tosca.model.ArchiveRoot in project alien4cloud by alien4cloud.

the class AbstractArtifactPostProcessor method process.

@Override
public void process(AbstractArtifact instance) {
    Node node = ParsingContextExecution.getObjectToNodeMap().get(instance);
    postProcessArtifactRef(node, instance.getArtifactRef());
    ArchiveRoot archiveRoot = ParsingContextExecution.getRootObj();
    // If archive name is already defined (by the type for example then don't override it)
    if (StringUtils.isBlank(instance.getArchiveName())) {
        instance.setArchiveName(archiveRoot.getArchive().getName());
        instance.setArchiveVersion(archiveRoot.getArchive().getVersion());
    }
    if (instance.getArtifactType() == null) {
        // try to get type from extension
        instance.setArtifactType(getArtifactTypeByExtension(instance.getArtifactRef(), node, archiveRoot));
    } else {
        // check the type reference
        referencePostProcessor.process(new ReferencePostProcessor.TypeReference(instance, instance.getArtifactType(), ArtifactType.class));
    }
    if (instance.getRepositoryName() != null) {
        RepositoryDefinition repositoryDefinition = archiveRoot.getRepositories() != null ? archiveRoot.getRepositories().get(instance.getRepositoryName()) : null;
        if (repositoryDefinition == null) {
            // Sometimes the information about repository has already been filled in the parent type
            if (instance.getRepositoryURL() == null) {
                ParsingContextExecution.getParsingErrors().add(new ParsingError(ErrorCode.UNKNOWN_REPOSITORY, "Implementation artifact", node.getStartMark(), "Repository definition not found", node.getEndMark(), instance.getArtifactRepository()));
            }
        } else {
            instance.setRepositoryURL(repositoryDefinition.getUrl());
            instance.setRepositoryCredential(repositoryDefinition.getCredential() != null ? repositoryDefinition.getCredential().getValue() : null);
            instance.setRepositoryName(repositoryDefinition.getId());
            instance.setArtifactRepository(repositoryDefinition.getType());
        }
    }
}
Also used : RepositoryDefinition(org.alien4cloud.tosca.model.definitions.RepositoryDefinition) ArchiveRoot(alien4cloud.tosca.model.ArchiveRoot) ParsingError(alien4cloud.tosca.parser.ParsingError) ArtifactType(org.alien4cloud.tosca.model.types.ArtifactType) Node(org.yaml.snakeyaml.nodes.Node)

Example 42 with ArchiveRoot

use of alien4cloud.tosca.model.ArchiveRoot in project alien4cloud by alien4cloud.

the class ArchiveRootPostProcessor method processImports.

/**
 * Process imports within the archive and compute its complete dependency set.
 * Resolve all dependency version conflicts using the following rules:
 * <ul>
 * <li>If two direct dependencies conflict with each other, use the latest version</li>
 * <li>If a transitive dependency conflicts with a direct dependency, use the direct dependency version</li>
 * <li>If two transitive dependency conflict with each other, use the latest version.</li>
 * </ul>
 *
 * @param archiveRoot The archive to process.
 */
private void processImports(ArchiveRoot archiveRoot) {
    if (archiveRoot.getArchive().getDependencies() == null || archiveRoot.getArchive().getDependencies().isEmpty()) {
        return;
    }
    // Dependencies defined in the import section only
    // These should override transitive deps regardless of type of conflict ?
    Set<CSARDependency> dependencies = archiveRoot.getArchive().getDependencies();
    // Ensure the archive does not import itself
    Csar archive = archiveRoot.getArchive();
    if (dependencies.contains(new CSARDependency(archive.getName(), archive.getVersion(), archive.getHash()))) {
        ParsingContextExecution.getParsingErrors().add(new ParsingError(ParsingErrorLevel.ERROR, ErrorCode.CSAR_IMPORT_ITSELF, AlienUtils.prefixWith(":", archive.getVersion(), archive.getName()), null, "Import itself", null, null));
    }
    /*
         * Three types of conflicts :
         * - A transitive dep has a different version than a direct dependency => Force transitive to direct version
         * - Transitive dependencies with the same name and different version are used => Use latest
         * - Direct dependencies with the same name and different version are used => Error or use latest ?
         */
    // 1. Resolve all direct dependencies using latest version
    dependencies.removeIf(dependency -> dependencyConflictsWithLatest(dependency, dependencies));
    // Compute all distinct transitives dependencies
    final Set<CSARDependency> transitiveDependencies = new HashSet<>(dependencies.stream().map(csarDependency -> ToscaContext.get().getArchive(csarDependency.getName(), csarDependency.getVersion())).map(Csar::getDependencies).filter(c -> c != null).reduce(Sets::union).orElse(Collections.emptySet()));
    // 2. Resolve all transitive vs. direct dependencies conflicts using the direct dependency's version
    transitiveDependencies.removeIf(transitiveDependency -> dependencyConflictsWithDirect(transitiveDependency, dependencies));
    // 3. Resolve all transitive dependencies conflicts using latest version
    transitiveDependencies.removeIf(transitiveDependency -> dependencyConflictsWithLatest(transitiveDependency, transitiveDependencies));
    // Merge all dependencies (direct + transitives)
    final Set<CSARDependency> mergedDependencies = new HashSet<>(Sets.union(dependencies, transitiveDependencies));
    archiveRoot.getArchive().setDependencies(mergedDependencies);
    // Update Tosca context with the complete dependency set
    ToscaContext.get().resetDependencies(mergedDependencies);
}
Also used : DataType(org.alien4cloud.tosca.model.types.DataType) VersionUtil(alien4cloud.utils.VersionUtil) AlienUtils.safe(alien4cloud.utils.AlienUtils.safe) HashSet(java.util.HashSet) Node(org.yaml.snakeyaml.nodes.Node) ArchiveRoot(alien4cloud.tosca.model.ArchiveRoot) PropertyValue(org.alien4cloud.tosca.model.definitions.PropertyValue) AbstractPropertyValue(org.alien4cloud.tosca.model.definitions.AbstractPropertyValue) Map(java.util.Map) CSARDependency(org.alien4cloud.tosca.model.CSARDependency) RepositoryDefinition(org.alien4cloud.tosca.model.definitions.RepositoryDefinition) ToscaContext(alien4cloud.tosca.context.ToscaContext) PropertyUtil(alien4cloud.utils.PropertyUtil) PropertyDefinition(org.alien4cloud.tosca.model.definitions.PropertyDefinition) Csar(org.alien4cloud.tosca.model.Csar) MapUtils(org.apache.commons.collections.MapUtils) AlienUtils(alien4cloud.utils.AlienUtils) ParsingErrorLevel(alien4cloud.tosca.parser.ParsingErrorLevel) Resource(javax.annotation.Resource) Set(java.util.Set) ErrorCode(alien4cloud.tosca.parser.impl.ErrorCode) Sets(com.google.common.collect.Sets) Objects(java.util.Objects) Component(org.springframework.stereotype.Component) ParsingContextExecution(alien4cloud.tosca.parser.ParsingContextExecution) NormativeCredentialConstant(org.alien4cloud.tosca.normative.constants.NormativeCredentialConstant) Collections(java.util.Collections) ParsingError(alien4cloud.tosca.parser.ParsingError) Csar(org.alien4cloud.tosca.model.Csar) ParsingError(alien4cloud.tosca.parser.ParsingError) Sets(com.google.common.collect.Sets) CSARDependency(org.alien4cloud.tosca.model.CSARDependency) HashSet(java.util.HashSet)

Example 43 with ArchiveRoot

use of alien4cloud.tosca.model.ArchiveRoot in project alien4cloud by alien4cloud.

the class ToscaArchiveParser method initFromToscaMeta.

private ArchiveRoot initFromToscaMeta(ParsingResult<ToscaMeta> toscaMeta) {
    ArchiveRoot archiveRoot = new ArchiveRoot();
    archiveRoot.getArchive().setName(toscaMeta.getResult().getName());
    if (toscaMeta.getResult().getVersion() != null) {
        archiveRoot.getArchive().setVersion(toscaMeta.getResult().getVersion());
    }
    if (toscaMeta.getResult().getCreatedBy() != null) {
        archiveRoot.getArchive().setTemplateAuthor(toscaMeta.getResult().getCreatedBy());
    }
    return archiveRoot;
}
Also used : ArchiveRoot(alien4cloud.tosca.model.ArchiveRoot)

Example 44 with ArchiveRoot

use of alien4cloud.tosca.model.ArchiveRoot in project alien4cloud by alien4cloud.

the class PluginArchiveIndexer method getArchivesToIndex.

/**
 * From all exposed plugin archives of the location, get the one that are not yet indexed
 *
 * @param orchestrator
 * @param location
 * @return an object of type {@link ArchiveToIndex} with the indexable archives and the full list of dependencies
 */
private ArchiveToIndex getArchivesToIndex(Orchestrator orchestrator, Location location) {
    Set<CSARDependency> dependencies = Sets.newHashSet();
    IOrchestratorPlugin orchestratorInstance = (IOrchestratorPlugin) orchestratorPluginService.getOrFail(orchestrator.getId());
    ILocationConfiguratorPlugin configuratorPlugin = orchestratorInstance.getConfigurator(location.getInfrastructureType());
    List<PluginArchive> allPluginArchives = configuratorPlugin.pluginArchives();
    Set<PluginArchive> archivesToIndex = Sets.newHashSet();
    for (PluginArchive pluginArchive : safe(allPluginArchives)) {
        ArchiveRoot archive = pluginArchive.getArchive();
        Csar csar = csarService.get(archive.getArchive().getName(), archive.getArchive().getVersion());
        String lastParsedHash = null;
        if (csar == null) {
            // the archive does not exist into the repository: should be indexed
            lastParsedHash = archive.getArchive().getHash();
            archivesToIndex.add(pluginArchive);
        } else {
            // Else, just take the hash
            lastParsedHash = csar.getHash();
        }
        if (archive.getArchive().getDependencies() != null) {
            dependencies.addAll(archive.getArchive().getDependencies());
        }
        dependencies.add(new CSARDependency(archive.getArchive().getName(), archive.getArchive().getVersion(), lastParsedHash));
    }
    return new ArchiveToIndex(dependencies, archivesToIndex);
}
Also used : Csar(org.alien4cloud.tosca.model.Csar) ArchiveRoot(alien4cloud.tosca.model.ArchiveRoot) ILocationConfiguratorPlugin(alien4cloud.orchestrators.plugin.ILocationConfiguratorPlugin) PluginArchive(alien4cloud.orchestrators.plugin.model.PluginArchive) CSARDependency(org.alien4cloud.tosca.model.CSARDependency) IOrchestratorPlugin(alien4cloud.orchestrators.plugin.IOrchestratorPlugin)

Example 45 with ArchiveRoot

use of alien4cloud.tosca.model.ArchiveRoot in project alien4cloud by alien4cloud.

the class PluginArchiveIndexer method indexArchive.

private void indexArchive(PluginArchive pluginArchive, Orchestrator orchestrator, Location location) {
    ArchiveRoot archive = pluginArchive.getArchive();
    // inject a specific tag to allow components catalog filtering search
    injectWorkSpace(archive.getNodeTypes().values(), orchestrator, location);
    injectWorkSpace(archive.getArtifactTypes().values(), orchestrator, location);
    injectWorkSpace(archive.getCapabilityTypes().values(), orchestrator, location);
    injectWorkSpace(archive.getRelationshipTypes().values(), orchestrator, location);
    List<ParsingError> parsingErrors = Lists.newArrayList();
    // index the archive in alien catalog
    try {
        archiveIndexer.importArchive(archive, CSARSource.ORCHESTRATOR, pluginArchive.getArchiveFilePath(), parsingErrors);
    } catch (AlreadyExistException e) {
        log.debug("Skipping location archive import as the released version already exists in the repository.");
    } catch (CSARUsedInActiveDeployment e) {
        log.debug("Skipping orchestrator archive import as it is used in an active deployment. " + e.getMessage());
    } catch (ToscaTypeAlreadyDefinedInOtherCSAR e) {
        log.debug("Skipping orchestrator archive import, it's archive contain's a tosca type already defined in an other archive." + e.getMessage());
    }
    // Publish event to allow plugins to post-process elements (portability plugin for example).
    publishLocationTypeIndexedEvent(archive.getNodeTypes().values(), orchestrator, location);
}
Also used : ArchiveRoot(alien4cloud.tosca.model.ArchiveRoot) ParsingError(alien4cloud.tosca.parser.ParsingError) CSARUsedInActiveDeployment(alien4cloud.component.repository.exception.CSARUsedInActiveDeployment) ToscaTypeAlreadyDefinedInOtherCSAR(alien4cloud.component.repository.exception.ToscaTypeAlreadyDefinedInOtherCSAR) AlreadyExistException(alien4cloud.exception.AlreadyExistException)

Aggregations

ArchiveRoot (alien4cloud.tosca.model.ArchiveRoot)78 Test (org.junit.Test)45 Csar (org.alien4cloud.tosca.model.Csar)27 Set (java.util.Set)26 NodeType (org.alien4cloud.tosca.model.types.NodeType)26 RelationshipType (org.alien4cloud.tosca.model.types.RelationshipType)16 CapabilityType (org.alien4cloud.tosca.model.types.CapabilityType)14 ParsingError (alien4cloud.tosca.parser.ParsingError)13 Path (java.nio.file.Path)11 PropertyDefinition (org.alien4cloud.tosca.model.definitions.PropertyDefinition)9 NodeTemplate (org.alien4cloud.tosca.model.templates.NodeTemplate)8 Node (org.yaml.snakeyaml.nodes.Node)8 PluginArchive (alien4cloud.orchestrators.plugin.model.PluginArchive)6 CSARDependency (org.alien4cloud.tosca.model.CSARDependency)6 AbstractPropertyValue (org.alien4cloud.tosca.model.definitions.AbstractPropertyValue)6 ParsingException (alien4cloud.tosca.parser.ParsingException)5 Map (java.util.Map)5 Topology (org.alien4cloud.tosca.model.templates.Topology)5 ComplexPropertyValue (org.alien4cloud.tosca.model.definitions.ComplexPropertyValue)4 ScalarPropertyValue (org.alien4cloud.tosca.model.definitions.ScalarPropertyValue)4