Search in sources :

Example 41 with Topology

use of org.alien4cloud.tosca.model.templates.Topology in project alien4cloud by alien4cloud.

the class InputArtifactService method onCopyConfiguration.

@EventListener
@Order(11)
public void onCopyConfiguration(OnDeploymentConfigCopyEvent onDeploymentConfigCopyEvent) {
    ApplicationEnvironment source = onDeploymentConfigCopyEvent.getSourceEnvironment();
    ApplicationEnvironment target = onDeploymentConfigCopyEvent.getTargetEnvironment();
    DeploymentInputs deploymentInputs = deploymentConfigurationDao.findById(DeploymentInputs.class, AbstractDeploymentConfig.generateId(source.getTopologyVersion(), source.getId()));
    if (deploymentInputs == null || MapUtils.isEmpty(deploymentInputs.getInputArtifacts())) {
        // Nothing to copy
        return;
    }
    Topology topology = topologyServiceCore.getOrFail(Csar.createId(target.getApplicationId(), target.getTopologyVersion()));
    if (MapUtils.isNotEmpty(topology.getInputArtifacts())) {
        Map<String, DeploymentArtifact> inputsArtifactsDefinitions = topology.getInputArtifacts();
        // Copy only artifacts which exists in the new topology's definition
        Map<String, DeploymentArtifact> inputsArtifactsToCopy = deploymentInputs.getInputArtifacts().entrySet().stream().filter(inputEntry -> inputsArtifactsDefinitions.containsKey(inputEntry.getKey())).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
        if (MapUtils.isNotEmpty(inputsArtifactsToCopy)) {
            // There's something to copy
            DeploymentInputs targetDeploymentInputs = deploymentConfigurationDao.findById(DeploymentInputs.class, AbstractDeploymentConfig.generateId(target.getTopologyVersion(), target.getId()));
            if (targetDeploymentInputs == null) {
                targetDeploymentInputs = new DeploymentInputs(target.getTopologyVersion(), target.getId());
            }
            targetDeploymentInputs.setInputArtifacts(inputsArtifactsToCopy);
            deploymentConfigurationDao.save(targetDeploymentInputs);
        }
    }
}
Also used : MapUtils(org.apache.commons.collections4.MapUtils) Order(org.springframework.core.annotation.Order) Csar(org.alien4cloud.tosca.model.Csar) ApplicationEnvironment(alien4cloud.model.application.ApplicationEnvironment) TopologyServiceCore(alien4cloud.topology.TopologyServiceCore) Resource(javax.annotation.Resource) EventListener(org.springframework.context.event.EventListener) IOException(java.io.IOException) HashMap(java.util.HashMap) AbstractDeploymentConfig(org.alien4cloud.alm.deployment.configuration.model.AbstractDeploymentConfig) Collectors(java.util.stream.Collectors) NotFoundException(alien4cloud.exception.NotFoundException) Inject(javax.inject.Inject) IFileRepository(alien4cloud.component.repository.IFileRepository) Service(org.springframework.stereotype.Service) Map(java.util.Map) DeploymentInputs(org.alien4cloud.alm.deployment.configuration.model.DeploymentInputs) MultipartFile(org.springframework.web.multipart.MultipartFile) Topology(org.alien4cloud.tosca.model.templates.Topology) ArtifactRepositoryConstants(alien4cloud.component.repository.ArtifactRepositoryConstants) OnDeploymentConfigCopyEvent(org.alien4cloud.alm.deployment.configuration.events.OnDeploymentConfigCopyEvent) DeploymentArtifact(org.alien4cloud.tosca.model.definitions.DeploymentArtifact) InputStream(java.io.InputStream) DeploymentInputs(org.alien4cloud.alm.deployment.configuration.model.DeploymentInputs) Topology(org.alien4cloud.tosca.model.templates.Topology) ApplicationEnvironment(alien4cloud.model.application.ApplicationEnvironment) DeploymentArtifact(org.alien4cloud.tosca.model.definitions.DeploymentArtifact) HashMap(java.util.HashMap) Map(java.util.Map) Order(org.springframework.core.annotation.Order) EventListener(org.springframework.context.event.EventListener)

Example 42 with Topology

use of org.alien4cloud.tosca.model.templates.Topology in project alien4cloud by alien4cloud.

the class PolicyMatchingSubstitutionService method onCopyConfiguration.

// FIXME fix this, synch with org.alien4cloud.alm.deployment.configuration.services.MatchingSubstitutionService#onCopyConfiguration
// @EventListener
// @Order(30) // Process this after location matching copy (first element).
public void onCopyConfiguration(OnDeploymentConfigCopyEvent onDeploymentConfigCopyEvent) {
    ApplicationEnvironment source = onDeploymentConfigCopyEvent.getSourceEnvironment();
    ApplicationEnvironment target = onDeploymentConfigCopyEvent.getTargetEnvironment();
    DeploymentMatchingConfiguration sourceConfiguration = deploymentConfigurationDao.findById(DeploymentMatchingConfiguration.class, AbstractDeploymentConfig.generateId(source.getTopologyVersion(), source.getId()));
    DeploymentMatchingConfiguration targetConfiguration = deploymentConfigurationDao.findById(DeploymentMatchingConfiguration.class, AbstractDeploymentConfig.generateId(target.getTopologyVersion(), target.getId()));
    if (sourceConfiguration == null || MapUtils.isEmpty(sourceConfiguration.getLocationGroups()) || targetConfiguration == null || MapUtils.isEmpty(targetConfiguration.getLocationGroups())) {
        // Nothing to copy
        return;
    }
    // We have to execute a piece of the deployment flow to find out matching candidates so we copy only required inputs
    Topology topology = topologyServiceCore.getOrFail(Csar.createId(target.getApplicationId(), target.getTopologyVersion()));
    if (MapUtils.isNotEmpty(topology.getNodeTemplates())) {
        Application application = applicationService.getOrFail(target.getApplicationId());
        FlowExecutionContext executionContext = new FlowExecutionContext(deploymentConfigurationDao, topology, new EnvironmentContext(application, target));
        flowExecutor.execute(topology, getMatchingFlow(), executionContext);
        Map<String, Set<String>> locResTemplateIdsPerNodeIds = (Map<String, Set<String>>) executionContext.getExecutionCache().get(FlowExecutionContext.SELECTED_MATCH_NODE_LOCATION_TEMPLATE_BY_NODE_ID_MAP);
        // Update the substitution on the target if available substitution is always compatible
        Map<String, String> validOnNewEnvSubstitutedNodes = safe(sourceConfiguration.getMatchedLocationResources()).entrySet().stream().filter(entry -> locResTemplateIdsPerNodeIds.containsKey(entry.getKey()) && locResTemplateIdsPerNodeIds.get(entry.getKey()).contains(entry.getValue())).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
        if (MapUtils.isNotEmpty(validOnNewEnvSubstitutedNodes)) {
            if (targetConfiguration.getMatchedLocationResources() == null) {
                targetConfiguration.setMatchedLocationResources(Maps.newHashMap());
            }
            validOnNewEnvSubstitutedNodes.forEach((key, value) -> {
                targetConfiguration.getMatchedLocationResources().put(key, value);
                // Copy properties set on the node to the new one
                targetConfiguration.getMatchedNodesConfiguration().put(key, safe(sourceConfiguration.getMatchedNodesConfiguration()).get(key));
            });
            deploymentConfigurationDao.save(targetConfiguration);
        }
    }
}
Also used : EnvironmentContext(org.alien4cloud.alm.deployment.configuration.flow.EnvironmentContext) TopologyServiceCore(alien4cloud.topology.TopologyServiceCore) PolicyMatchingCompositeModifier(org.alien4cloud.alm.deployment.configuration.flow.modifiers.matching.PolicyMatchingCompositeModifier) PolicyMatchingConfigAutoSelectModifier(org.alien4cloud.alm.deployment.configuration.flow.modifiers.matching.PolicyMatchingConfigAutoSelectModifier) AlienUtils.safe(alien4cloud.utils.AlienUtils.safe) Inject(javax.inject.Inject) Service(org.springframework.stereotype.Service) Map(java.util.Map) Application(alien4cloud.model.application.Application) ApplicationService(alien4cloud.application.ApplicationService) OnDeploymentConfigCopyEvent(org.alien4cloud.alm.deployment.configuration.events.OnDeploymentConfigCopyEvent) ITopologyModifier(org.alien4cloud.alm.deployment.configuration.flow.ITopologyModifier) MapUtils(org.apache.commons.collections4.MapUtils) Csar(org.alien4cloud.tosca.model.Csar) ApplicationEnvironment(alien4cloud.model.application.ApplicationEnvironment) Set(java.util.Set) SetMatchedPolicyModifier(org.alien4cloud.alm.deployment.configuration.flow.modifiers.action.SetMatchedPolicyModifier) AbstractDeploymentConfig(org.alien4cloud.alm.deployment.configuration.model.AbstractDeploymentConfig) Collectors(java.util.stream.Collectors) Maps(com.google.common.collect.Maps) NotFoundException(alien4cloud.exception.NotFoundException) List(java.util.List) EnvironmentContext(org.alien4cloud.alm.deployment.configuration.flow.EnvironmentContext) FlowExecutor(org.alien4cloud.alm.deployment.configuration.flow.FlowExecutor) AbstractComposedModifier(org.alien4cloud.alm.deployment.configuration.flow.modifiers.matching.AbstractComposedModifier) DeploymentMatchingConfiguration(org.alien4cloud.alm.deployment.configuration.model.DeploymentMatchingConfiguration) FlowExecutionContext(org.alien4cloud.alm.deployment.configuration.flow.FlowExecutionContext) Topology(org.alien4cloud.tosca.model.templates.Topology) Set(java.util.Set) DeploymentMatchingConfiguration(org.alien4cloud.alm.deployment.configuration.model.DeploymentMatchingConfiguration) FlowExecutionContext(org.alien4cloud.alm.deployment.configuration.flow.FlowExecutionContext) Topology(org.alien4cloud.tosca.model.templates.Topology) ApplicationEnvironment(alien4cloud.model.application.ApplicationEnvironment) Application(alien4cloud.model.application.Application) Map(java.util.Map)

Example 43 with Topology

use of org.alien4cloud.tosca.model.templates.Topology in project alien4cloud by alien4cloud.

the class NodeMatchingCandidateModifier method getAvailableSubstitutions.

private Map<String, List<LocationResourceTemplate>> getAvailableSubstitutions(Topology topology, Map<String, NodeGroup> locationGroups, Map<String, Location> locationByIds, String environmentId) {
    // Fetch all node types for templates in the topology
    Map<String, NodeType> nodeTypes = getNodeTypes(topology);
    Map<String, List<LocationResourceTemplate>> availableSubstitutions = Maps.newHashMap();
    // Based on our model nodes may come from various locations actually.
    for (final Map.Entry<String, NodeGroup> locationGroupEntry : locationGroups.entrySet()) {
        String groupName = locationGroupEntry.getKey();
        final NodeGroup locationNodeGroup = locationGroupEntry.getValue();
        Map<String, NodeTemplate> nodesToMatch = Maps.newHashMap();
        if (MapUtils.isNotEmpty(topology.getNodeTemplates())) {
            if (AlienConstants.GROUP_ALL.equals(groupName)) {
                locationNodeGroup.setMembers(topology.getNodeTemplates().keySet());
                nodesToMatch = topology.getNodeTemplates();
            } else {
                nodesToMatch = Maps.filterEntries(topology.getNodeTemplates(), input -> locationNodeGroup.getMembers().contains(input.getKey()));
            }
        }
        availableSubstitutions.putAll(nodeMatcherService.match(nodeTypes, nodesToMatch, locationByIds.get(groupName), environmentId));
    }
    return availableSubstitutions;
}
Also used : MapUtils(org.apache.commons.collections4.MapUtils) NodeMatcherService(alien4cloud.deployment.matching.services.nodes.NodeMatcherService) NodeType(org.alien4cloud.tosca.model.types.NodeType) Maps(com.google.common.collect.Maps) AlienUtils.safe(alien4cloud.utils.AlienUtils.safe) Location(alien4cloud.model.orchestrators.locations.Location) Inject(javax.inject.Inject) AlienConstants(alien4cloud.utils.AlienConstants) List(java.util.List) Component(org.springframework.stereotype.Component) DeploymentMatchingConfiguration(org.alien4cloud.alm.deployment.configuration.model.DeploymentMatchingConfiguration) FlowExecutionContext(org.alien4cloud.alm.deployment.configuration.flow.FlowExecutionContext) LocationPolicyTask(alien4cloud.topology.task.LocationPolicyTask) Map(java.util.Map) NodeGroup(org.alien4cloud.tosca.model.templates.NodeGroup) NodeTemplate(org.alien4cloud.tosca.model.templates.NodeTemplate) Optional(java.util.Optional) Topology(org.alien4cloud.tosca.model.templates.Topology) ITopologyModifier(org.alien4cloud.alm.deployment.configuration.flow.ITopologyModifier) LocationResourceTemplate(alien4cloud.model.orchestrators.locations.LocationResourceTemplate) ToscaContext(alien4cloud.tosca.context.ToscaContext) NodeTemplate(org.alien4cloud.tosca.model.templates.NodeTemplate) NodeType(org.alien4cloud.tosca.model.types.NodeType) List(java.util.List) Map(java.util.Map) NodeGroup(org.alien4cloud.tosca.model.templates.NodeGroup)

Example 44 with Topology

use of org.alien4cloud.tosca.model.templates.Topology in project alien4cloud by alien4cloud.

the class LocationMatchingModifier method process.

@Override
public void process(Topology topology, FlowExecutionContext context) {
    // first process
    processLocationMatching(topology, context);
    Optional<DeploymentMatchingConfiguration> configurationOptional = context.getConfiguration(DeploymentMatchingConfiguration.class, LocationMatchingModifier.class.getSimpleName());
    // perform validation
    locationPolicyValidationService.validateLocationPolicies(configurationOptional.orElse(new DeploymentMatchingConfiguration())).forEach(locationPolicyTask -> context.log().error(locationPolicyTask));
    // No errors from validation let's inject location topology modifiers if any.
    if (context.log().isValid()) {
        Map<String, Location> selectedLocations = (Map<String, Location>) context.getExecutionCache().get(FlowExecutionContext.DEPLOYMENT_LOCATIONS_MAP_CACHE_KEY);
        List<Location> locationsWithVault = selectedLocations.values().stream().filter(location -> location.getSecretProviderConfiguration() != null && location.getSecretProviderConfiguration().getConfiguration() != null).collect(Collectors.toList());
        boolean needVaultCredential = locationsWithVault.size() > 0;
        if (needVaultCredential) {
            List<SecretCredentialInfo> secretCredentialInfos = new LinkedList<>();
            for (Location location : locationsWithVault) {
                try {
                    SecretCredentialInfo info = new SecretCredentialInfo();
                    String pluginName = location.getSecretProviderConfiguration().getPluginName();
                    Object rawSecretConfiguration = location.getSecretProviderConfiguration().getConfiguration();
                    secretCredentialInfos.add(secretProviderService.getSecretCredentialInfo(pluginName, rawSecretConfiguration));
                } catch (Exception e) {
                    log.error("Cannot process secret provider configuration", e);
                }
            }
            context.getExecutionCache().put(FlowExecutionContext.SECRET_CREDENTIAL, secretCredentialInfos);
        } else {
            context.getExecutionCache().remove(FlowExecutionContext.SECRET_CREDENTIAL);
        }
        for (LocationModifierReference modifierReference : safe(selectedLocations.values().iterator().next().getModifiers())) {
            if (pluginManager.getPluginOrFail(modifierReference.getPluginId()).isEnabled()) {
                injectLocationTopologyModfier(context, selectedLocations.values().iterator().next().getName(), modifierReference);
            } else {
                log.info("The plugin " + modifierReference.getPluginId() + " is not activated. Ignoring " + modifierReference.getBeanName() + ".");
            }
        }
    }
}
Also used : LocationModifierReference(alien4cloud.model.orchestrators.locations.LocationModifierReference) LocationService(alien4cloud.orchestrators.locations.services.LocationService) PluginManager(alien4cloud.plugin.PluginManager) MetaPropertiesService(alien4cloud.common.MetaPropertiesService) LocationPolicyValidationService(alien4cloud.topology.validation.LocationPolicyValidationService) AlienUtils.safe(alien4cloud.utils.AlienUtils.safe) Location(alien4cloud.model.orchestrators.locations.Location) Inject(javax.inject.Inject) SecretCredentialInfo(org.alien4cloud.alm.deployment.configuration.model.SecretCredentialInfo) LocationMatchingService(alien4cloud.deployment.matching.services.location.LocationMatchingService) Lists(com.google.common.collect.Lists) Map(java.util.Map) SecretProviderService(org.alien4cloud.secret.services.SecretProviderService) LinkedList(java.util.LinkedList) ITopologyModifier(org.alien4cloud.alm.deployment.configuration.flow.ITopologyModifier) ILocationMatch(alien4cloud.model.deployment.matching.ILocationMatch) ToscaContext(alien4cloud.tosca.context.ToscaContext) MapUtils(org.apache.commons.collections4.MapUtils) ApplicationEnvironment(alien4cloud.model.application.ApplicationEnvironment) MissingPluginException(alien4cloud.plugin.exception.MissingPluginException) Collectors(java.util.stream.Collectors) Maps(com.google.common.collect.Maps) List(java.util.List) Component(org.springframework.stereotype.Component) Slf4j(lombok.extern.slf4j.Slf4j) DeploymentMatchingConfiguration(org.alien4cloud.alm.deployment.configuration.model.DeploymentMatchingConfiguration) FlowExecutionContext(org.alien4cloud.alm.deployment.configuration.flow.FlowExecutionContext) Optional(java.util.Optional) Topology(org.alien4cloud.tosca.model.templates.Topology) SecretCredentialInfo(org.alien4cloud.alm.deployment.configuration.model.SecretCredentialInfo) LinkedList(java.util.LinkedList) MissingPluginException(alien4cloud.plugin.exception.MissingPluginException) LocationModifierReference(alien4cloud.model.orchestrators.locations.LocationModifierReference) DeploymentMatchingConfiguration(org.alien4cloud.alm.deployment.configuration.model.DeploymentMatchingConfiguration) Map(java.util.Map) Location(alien4cloud.model.orchestrators.locations.Location)

Example 45 with Topology

use of org.alien4cloud.tosca.model.templates.Topology in project alien4cloud by alien4cloud.

the class EditorInputHelperService method getInputCandidates.

/**
 * Utility method to get the list of inputs (ids) that are compatible with the given property definition (no constraint conflicts)..
 *
 * @param topologyId The id of the topology for which to find input candidates.
 * @param nodeTemplateName The name of the node template for which to get input candidates.
 * @param propertyDefinitionGetter Implementation on how to get the property definition (from node properties, capabilities properties, relationships
 *            properties).
 * @return A list of input candidates that are compatible with the requested property definition.
 */
public List<String> getInputCandidates(String topologyId, String nodeTemplateName, IPropertyDefinitionGetter propertyDefinitionGetter) {
    try {
        editionContextManager.init(topologyId);
        // check authorization to update a topology
        topologyService.checkEditionAuthorizations(EditionContextManager.getTopology());
        Topology topology = EditionContextManager.getTopology();
        NodeTemplate nodeTemplate = topology.getNodeTemplates().get(nodeTemplateName);
        PropertyDefinition pd = propertyDefinitionGetter.get(nodeTemplate);
        if (pd == null) {
            throw new NotFoundException("Unexpected error, property definition cannot be found for node <" + nodeTemplateName + ">");
        }
        Map<String, PropertyDefinition> inputs = topology.getInputs();
        List<String> inputIds = new ArrayList<String>();
        if (inputs != null && !inputs.isEmpty()) {
            // iterate overs existing inputs and filter them by checking constraint compatibility
            for (Map.Entry<String, PropertyDefinition> inputEntry : inputs.entrySet()) {
                try {
                    inputEntry.getValue().checkIfCompatibleOrFail(pd);
                    inputIds.add(inputEntry.getKey());
                } catch (IncompatiblePropertyDefinitionException e) {
                // Nothing to do here, the id won't be added to the list
                }
            }
        }
        return inputIds;
    } finally {
        editionContextManager.destroy();
    }
}
Also used : NodeTemplate(org.alien4cloud.tosca.model.templates.NodeTemplate) ArrayList(java.util.ArrayList) NotFoundException(alien4cloud.exception.NotFoundException) Topology(org.alien4cloud.tosca.model.templates.Topology) PropertyDefinition(org.alien4cloud.tosca.model.definitions.PropertyDefinition) Map(java.util.Map) IncompatiblePropertyDefinitionException(alien4cloud.model.components.IncompatiblePropertyDefinitionException)

Aggregations

Topology (org.alien4cloud.tosca.model.templates.Topology)80 NodeTemplate (org.alien4cloud.tosca.model.templates.NodeTemplate)23 Map (java.util.Map)21 ApplicationEnvironment (alien4cloud.model.application.ApplicationEnvironment)17 Csar (org.alien4cloud.tosca.model.Csar)17 Application (alien4cloud.model.application.Application)15 NotFoundException (alien4cloud.exception.NotFoundException)14 ApiOperation (io.swagger.annotations.ApiOperation)13 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)13 ApplicationTopologyVersion (alien4cloud.model.application.ApplicationTopologyVersion)12 DeploymentTopology (alien4cloud.model.deployment.DeploymentTopology)12 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)12 AlienUtils.safe (alien4cloud.utils.AlienUtils.safe)10 FlowExecutionContext (org.alien4cloud.alm.deployment.configuration.flow.FlowExecutionContext)10 NodeType (org.alien4cloud.tosca.model.types.NodeType)9 Component (org.springframework.stereotype.Component)9 IOException (java.io.IOException)8 List (java.util.List)8 Set (java.util.Set)8 DeploymentMatchingConfiguration (org.alien4cloud.alm.deployment.configuration.model.DeploymentMatchingConfiguration)8