Search in sources :

Example 1 with AfterArchiveIndexed

use of org.alien4cloud.tosca.catalog.events.AfterArchiveIndexed in project alien4cloud by alien4cloud.

the class ArchiveIndexer method importNewArchive.

/**
 * <p>
 * Import a new empty archive with a topology.
 * </p>
 * <p>
 * Note: this archive is not created from parsing but from alien4cloud API. This service will index the archive and topology as well as initialize the file
 * repository and tosca yaml.
 * </p>
 * <p>
 * This method cannot be used to override a topology, even a SNAPSHOT as any update to a topology from the API MUST be done through the editor.
 * </p>
 *
 * @param csar The archive to be imported.
 * @param topology The topology to be part of the topology.
 * @param topologyPath if the new topology must be created inside this directory to have all its artifacts
 */
@SneakyThrows
public synchronized void importNewArchive(Csar csar, Topology topology, Path topologyPath) {
    ArchiveRoot archiveRoot = new ArchiveRoot();
    archiveRoot.setArchive(csar);
    archiveRoot.setTopology(topology);
    csar.setHasTopology(true);
    // dispatch event before indexing
    publisher.publishEvent(new BeforeArchiveIndexed(this, archiveRoot));
    // Ensure that the archive does not already exists
    ensureUniqueness(csar.getName(), csar.getVersion());
    workflowBuilderService.initWorkflows(workflowBuilderService.buildTopologyContext(topology, csar));
    // generate the initial yaml in a temporary directory
    if (csar.getYamlFilePath() == null) {
        csar.setYamlFilePath("topology.yml");
    }
    String yaml = exportService.getYaml(csar, topology);
    // synch the dependencies before indexing
    csar.setDependencies(topology.getDependencies());
    // index the archive and topology
    csarService.save(csar);
    topologyServiceCore.save(topology);
    // Initialize the file repository for the archive
    if (topologyPath == null) {
        // This is an empty topology without artifacts
        archiveRepositry.storeCSAR(csar, yaml);
    } else {
        Files.write(topologyPath.resolve(csar.getYamlFilePath()), yaml.getBytes(Charset.forName("UTF-8")));
        archiveRepositry.storeCSAR(csar, topologyPath);
    }
    topologySubstitutionService.updateSubstitutionType(topology, archiveRoot.getArchive());
    // dispatch event after indexing
    publisher.publishEvent(new AfterArchiveIndexed(this, archiveRoot));
}
Also used : ArchiveRoot(alien4cloud.tosca.model.ArchiveRoot) AfterArchiveIndexed(org.alien4cloud.tosca.catalog.events.AfterArchiveIndexed) BeforeArchiveIndexed(org.alien4cloud.tosca.catalog.events.BeforeArchiveIndexed) SneakyThrows(lombok.SneakyThrows)

Example 2 with AfterArchiveIndexed

use of org.alien4cloud.tosca.catalog.events.AfterArchiveIndexed in project alien4cloud by alien4cloud.

the class ArchiveIndexer method importArchive.

/**
 * Import a pre-parsed archive to alien 4 cloud indexed catalog.
 *
 * @param source the source of the archive (alien, orchestrator, upload, git).
 * @param archiveRoot The parsed archive object.
 * @param archivePath The optional path of the archive (should be null if the archive has been java-generated and not parsed).
 * @param parsingErrors The non-null list of parsing errors in which to add errors.
 * @throws CSARUsedInActiveDeployment
 */
public synchronized void importArchive(final ArchiveRoot archiveRoot, CSARSource source, Path archivePath, List<ParsingError> parsingErrors) throws CSARUsedInActiveDeployment, ToscaTypeAlreadyDefinedInOtherCSAR {
    archiveIndexerAuthorizationFilter.checkAuthorization(archiveRoot);
    String archiveName = archiveRoot.getArchive().getName();
    String archiveVersion = archiveRoot.getArchive().getVersion();
    Csar currentIndexedArchive = csarService.get(archiveName, archiveVersion);
    if (currentIndexedArchive != null) {
        if (Objects.equals(currentIndexedArchive.getWorkspace(), archiveRoot.getArchive().getWorkspace())) {
            if (currentIndexedArchive.getHash() != null && currentIndexedArchive.getHash().equals(archiveRoot.getArchive().getHash())) {
                // if the archive has not changed do nothing.
                parsingErrors.add(new ParsingError(ParsingErrorLevel.INFO, ErrorCode.CSAR_ALREADY_INDEXED, "", null, "The archive already exists in alien4cloud with an identical content (SHA-1 on archive content excluding hidden files is identical).", null, archiveName));
                return;
            }
        } else {
            // If the archive existed in a different workspace then throw error
            parsingErrors.add(new ParsingError(ParsingErrorLevel.ERROR, ErrorCode.CSAR_ALREADY_EXISTS_IN_ANOTHER_WORKSPACE, "", null, "The archive already exists in alien4cloud in a different workspace.", null, archiveName));
            return;
        }
    }
    // dispatch event before indexing
    publisher.publishEvent(new BeforeArchiveIndexed(this, archiveRoot));
    // Throw an exception if we are trying to override a released (non SNAPSHOT) version.
    checkNotReleased(currentIndexedArchive);
    // In the current version of alien4cloud we must prevent from overriding an archive that is used in a deployment as we still use catalog information at
    // runtime.
    checkNotUsedInActiveDeployment(currentIndexedArchive);
    // FIXME If the archive already exists but can be indexed we should actually call an editor operation to keep git tracking, or should we just prevent
    // that ?
    checkIfToscaTypesAreDefinedInOtherArchive(archiveRoot);
    // save the archive (before we index and save other data so we can cleanup if anything goes wrong).
    if (source == null) {
        source = CSARSource.OTHER;
    }
    archiveRoot.getArchive().setImportSource(source.name());
    archiveRoot.getArchive().setHasTopology(archiveRoot.hasToscaTopologyTemplate() && !archiveRoot.getTopology().isEmpty());
    archiveRoot.getArchive().setNodeTypesCount(archiveRoot.getNodeTypes().size());
    // TODO load transitives dependencies here before saving, as it is not done when parsing
    csarService.save(archiveRoot.getArchive());
    log.debug("Imported archive {}", archiveRoot.getArchive().getId());
    // save the archive in the repository
    archiveRepositry.storeCSAR(archiveRoot.getArchive(), archivePath);
    // manage images before archive storage in the repository
    imageLoader.importImages(archivePath, archiveRoot, parsingErrors);
    // index the archive content in elastic-search
    indexArchiveTypes(archiveName, archiveVersion, archiveRoot.getArchive().getWorkspace(), archiveRoot, currentIndexedArchive);
    indexTopology(archiveRoot, parsingErrors, archiveName, archiveVersion);
    publisher.publishEvent(new AfterArchiveIndexed(this, archiveRoot));
}
Also used : Csar(org.alien4cloud.tosca.model.Csar) ParsingError(alien4cloud.tosca.parser.ParsingError) AfterArchiveIndexed(org.alien4cloud.tosca.catalog.events.AfterArchiveIndexed) BeforeArchiveIndexed(org.alien4cloud.tosca.catalog.events.BeforeArchiveIndexed)

Aggregations

AfterArchiveIndexed (org.alien4cloud.tosca.catalog.events.AfterArchiveIndexed)2 BeforeArchiveIndexed (org.alien4cloud.tosca.catalog.events.BeforeArchiveIndexed)2 ArchiveRoot (alien4cloud.tosca.model.ArchiveRoot)1 ParsingError (alien4cloud.tosca.parser.ParsingError)1 SneakyThrows (lombok.SneakyThrows)1 Csar (org.alien4cloud.tosca.model.Csar)1