Search in sources :

Example 1 with VersionConflictException

use of alien4cloud.exception.VersionConflictException 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 2 with VersionConflictException

use of alien4cloud.exception.VersionConflictException in project alien4cloud by alien4cloud.

the class TopologyService method updateDependencies.

public void updateDependencies(EditionContext context, CSARDependency newDependency) {
    final Set<CSARDependency> oldDependencies = new HashSet<>(context.getTopology().getDependencies());
    final Set<CSARDependency> newDependencies = csarDependencyLoader.getDependencies(newDependency.getName(), newDependency.getVersion());
    newDependencies.add(newDependency);
    // Update context with the new dependencies.
    newDependencies.forEach(csarDependency -> context.getToscaContext().updateDependency(csarDependency));
    // Validate that the dependency change does not induce missing types.
    try {
        this.checkForMissingTypes(context.getTopology());
    } catch (NotFoundException e) {
        // Revert changes made to the Context then throw.
        context.getToscaContext().resetDependencies(oldDependencies);
        context.getTopology().setDependencies(oldDependencies);
        throw new VersionConflictException("Changing the dependency [" + newDependency.getName() + "] to version [" + newDependency.getVersion() + "] induces missing types in the topology. Not found : [" + e.getMessage() + "].", e);
    }
    // Perform the dependency update on the topology.
    context.getTopology().setDependencies(new HashSet<>(context.getToscaContext().getDependencies()));
}
Also used : VersionConflictException(alien4cloud.exception.VersionConflictException) NotFoundException(alien4cloud.exception.NotFoundException) CSARDependency(org.alien4cloud.tosca.model.CSARDependency) HashSet(java.util.HashSet)

Aggregations

NotFoundException (alien4cloud.exception.NotFoundException)2 VersionConflictException (alien4cloud.exception.VersionConflictException)2 CSARDependency (org.alien4cloud.tosca.model.CSARDependency)2 ToscaTypeLoader (alien4cloud.tosca.container.ToscaTypeLoader)1 HashSet (java.util.HashSet)1