Search in sources :

Example 21 with Capability

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

the class SetMatchedNodePropertyModifier method setNodeCapabilityProperty.

private void setNodeCapabilityProperty(FlowExecutionContext context, LocationResourceTemplate locationResourceTemplate, NodeTemplate nodeTemplate, String capabilityName, DeploymentMatchingConfiguration matchingConfiguration) throws ConstraintViolationException, ConstraintValueDoNotMatchPropertyTypeException {
    Capability locationResourceCapability = locationResourceTemplate.getTemplate().getCapabilities().get(capabilityName);
    if (locationResourceCapability == null) {
        throw new NotFoundException("The capability <" + capabilityName + "> cannot be found on node template <" + templateId + "> of type <" + locationResourceTemplate.getTemplate().getType() + ">");
    }
    PropertyDefinition propertyDefinition = ToscaContext.getOrFail(CapabilityType.class, locationResourceCapability.getType()).getProperties().get(propertyName);
    if (propertyDefinition == null) {
        throw new NotFoundException("No property with name <" + propertyName + "> can be found on capability <" + capabilityName + "> of type <" + locationResourceCapability.getType() + ">");
    }
    AbstractPropertyValue locationResourcePropertyValue = locationResourceTemplate.getTemplate().getCapabilities().get(capabilityName).getProperties().get(propertyName);
    ensureNotSet(locationResourcePropertyValue, "by the admin in the Location Resource Template", propertyName, propertyValue);
    AbstractPropertyValue originalNodePropertyValue = safe(nodeTemplate.getCapabilities().get(capabilityName).getProperties()).get(propertyName);
    ensureNotSet(originalNodePropertyValue, "in the portable topology", propertyName, propertyValue);
    // Update the configuration
    NodePropsOverride nodePropsOverride = getTemplatePropsOverride(matchingConfiguration);
    if (propertyValue == null && nodePropsOverride.getCapabilities().get(capabilityName) != null) {
        nodePropsOverride.getCapabilities().get(capabilityName).getProperties().remove(propertyName);
    } else {
        // Set check constraints
        ConstraintPropertyService.checkPropertyConstraint(propertyName, propertyValue, propertyDefinition);
        NodeCapabilitiesPropsOverride nodeCapabilitiesPropsOverride = nodePropsOverride.getCapabilities().computeIfAbsent(capabilityName, k -> new NodeCapabilitiesPropsOverride());
        nodeCapabilitiesPropsOverride.getProperties().put(propertyName, PropertyService.asPropertyValue(propertyValue));
    }
    context.saveConfiguration(matchingConfiguration);
}
Also used : Capability(org.alien4cloud.tosca.model.templates.Capability) NodePropsOverride(org.alien4cloud.alm.deployment.configuration.model.DeploymentMatchingConfiguration.NodePropsOverride) NotFoundException(alien4cloud.exception.NotFoundException) PropertyDefinition(org.alien4cloud.tosca.model.definitions.PropertyDefinition) AbstractPropertyValue(org.alien4cloud.tosca.model.definitions.AbstractPropertyValue) NodeCapabilitiesPropsOverride(org.alien4cloud.alm.deployment.configuration.model.DeploymentMatchingConfiguration.NodeCapabilitiesPropsOverride)

Example 22 with Capability

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

the class InputsModifier method process.

@Override
public void process(Topology topology, FlowExecutionContext context) {
    EnvironmentContext environmentContext = context.getEnvironmentContext().orElseThrow(() -> new IllegalArgumentException("Input modifier requires an environment context."));
    ApplicationEnvironment environment = environmentContext.getEnvironment();
    DeploymentInputs deploymentInputs = context.getConfiguration(DeploymentInputs.class, InputsModifier.class.getSimpleName()).orElse(new DeploymentInputs(environment.getTopologyVersion(), environment.getId()));
    if (deploymentInputs.getInputs() == null) {
        deploymentInputs.setInputs(Maps.newHashMap());
    }
    Map<String, Location> locations = (Map<String, Location>) context.getExecutionCache().get(FlowExecutionContext.DEPLOYMENT_LOCATIONS_MAP_CACHE_KEY);
    Map<String, PropertyValue> applicationInputs = inputService.getAppContextualInputs(context.getEnvironmentContext().get().getApplication(), topology.getInputs());
    Map<String, PropertyValue> locationInputs = inputService.getLocationContextualInputs(locations, topology.getInputs());
    // If the initial topology or any modifier configuration has changed since last inputs update then we refresh inputs.
    if (deploymentInputs.getLastUpdateDate() == null || deploymentInputs.getLastUpdateDate().before(context.getLastFlowParamUpdate())) {
        // FIXME exclude the application and location provided inputs from this method as it process them...
        boolean updated = deploymentInputService.synchronizeInputs(topology.getInputs(), deploymentInputs.getInputs());
        if (updated) {
            // save the config if changed. This is for ex, if an input has been deleted from the topology
            context.saveConfiguration(deploymentInputs);
        }
    }
    PreconfiguredInputsConfiguration preconfiguredInputsConfiguration = context.getConfiguration(PreconfiguredInputsConfiguration.class, InputsModifier.class.getSimpleName()).orElseThrow(() -> new IllegalStateException("PreconfiguredInputsConfiguration must be in the context"));
    Map<String, AbstractPropertyValue> inputValues = Maps.newHashMap(deploymentInputs.getInputs());
    inputValues.putAll(applicationInputs);
    inputValues.putAll(locationInputs);
    inputValues.putAll(preconfiguredInputsConfiguration.getInputs());
    // Now that we have inputs ready let's process get_input functions in the topology to actually replace values.
    if (topology.getNodeTemplates() != null) {
        FunctionEvaluatorContext evaluatorContext = new FunctionEvaluatorContext(topology, inputValues);
        for (Entry<String, NodeTemplate> entry : topology.getNodeTemplates().entrySet()) {
            NodeTemplate nodeTemplate = entry.getValue();
            processGetInput(evaluatorContext, nodeTemplate, nodeTemplate.getProperties());
            if (nodeTemplate.getRelationships() != null) {
                for (Entry<String, RelationshipTemplate> relEntry : nodeTemplate.getRelationships().entrySet()) {
                    RelationshipTemplate relationshipTemplate = relEntry.getValue();
                    processGetInput(evaluatorContext, relationshipTemplate, relationshipTemplate.getProperties());
                }
            }
            if (nodeTemplate.getCapabilities() != null) {
                for (Entry<String, Capability> capaEntry : nodeTemplate.getCapabilities().entrySet()) {
                    Capability capability = capaEntry.getValue();
                    processGetInput(evaluatorContext, nodeTemplate, capability.getProperties());
                }
            }
            if (nodeTemplate.getRequirements() != null) {
                for (Entry<String, Requirement> requirementEntry : nodeTemplate.getRequirements().entrySet()) {
                    Requirement requirement = requirementEntry.getValue();
                    processGetInput(evaluatorContext, nodeTemplate, requirement.getProperties());
                }
            }
        }
    }
}
Also used : Capability(org.alien4cloud.tosca.model.templates.Capability) DeploymentInputs(org.alien4cloud.alm.deployment.configuration.model.DeploymentInputs) PreconfiguredInputsConfiguration(org.alien4cloud.alm.deployment.configuration.model.PreconfiguredInputsConfiguration) FunctionEvaluatorContext(org.alien4cloud.tosca.utils.FunctionEvaluatorContext) PropertyValue(org.alien4cloud.tosca.model.definitions.PropertyValue) AbstractPropertyValue(org.alien4cloud.tosca.model.definitions.AbstractPropertyValue) ApplicationEnvironment(alien4cloud.model.application.ApplicationEnvironment) EnvironmentContext(org.alien4cloud.alm.deployment.configuration.flow.EnvironmentContext) Requirement(org.alien4cloud.tosca.model.templates.Requirement) NodeTemplate(org.alien4cloud.tosca.model.templates.NodeTemplate) RelationshipTemplate(org.alien4cloud.tosca.model.templates.RelationshipTemplate) Map(java.util.Map) AbstractPropertyValue(org.alien4cloud.tosca.model.definitions.AbstractPropertyValue) Location(alien4cloud.model.orchestrators.locations.Location)

Example 23 with Capability

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

the class SuggestionService method postProcessSuggestionFromArchive.

public void postProcessSuggestionFromArchive(ParsingResult<ArchiveRoot> parsingResult) {
    ArchiveRoot archiveRoot = parsingResult.getResult();
    ParsingContext context = parsingResult.getContext();
    if (archiveRoot.hasToscaTopologyTemplate()) {
        Topology topology = archiveRoot.getTopology();
        Map<String, NodeTemplate> nodeTemplateMap = topology.getNodeTemplates();
        if (MapUtils.isEmpty(nodeTemplateMap)) {
            return;
        }
        for (Map.Entry<String, NodeTemplate> nodeTemplateEntry : nodeTemplateMap.entrySet()) {
            NodeTemplate nodeTemplate = nodeTemplateEntry.getValue();
            String nodeName = nodeTemplateEntry.getKey();
            if (MapUtils.isNotEmpty(nodeTemplate.getProperties())) {
                checkProperties(nodeName, nodeTemplate.getProperties(), NodeType.class, nodeTemplate.getType(), context);
            }
            Map<String, Capability> capabilityMap = nodeTemplate.getCapabilities();
            if (MapUtils.isNotEmpty(capabilityMap)) {
                for (Map.Entry<String, Capability> capabilityEntry : capabilityMap.entrySet()) {
                    String capabilityName = capabilityEntry.getKey();
                    Capability capability = capabilityEntry.getValue();
                    if (MapUtils.isNotEmpty(capability.getProperties())) {
                        checkProperties(nodeName + ".capabilities." + capabilityName, capability.getProperties(), CapabilityType.class, capability.getType(), context);
                    }
                }
            }
            Map<String, RelationshipTemplate> relationshipTemplateMap = nodeTemplate.getRelationships();
            if (MapUtils.isNotEmpty(relationshipTemplateMap)) {
                for (Map.Entry<String, RelationshipTemplate> relationshipEntry : relationshipTemplateMap.entrySet()) {
                    String relationshipName = relationshipEntry.getKey();
                    RelationshipTemplate relationship = relationshipEntry.getValue();
                    if (MapUtils.isNotEmpty(relationship.getProperties())) {
                        checkProperties(nodeName + ".relationships." + relationshipName, relationship.getProperties(), RelationshipType.class, relationship.getType(), context);
                    }
                }
            }
        }
    }
    if (archiveRoot.hasToscaTypes()) {
        Map<String, NodeType> allNodeTypes = archiveRoot.getNodeTypes();
        if (MapUtils.isNotEmpty(allNodeTypes)) {
            for (Map.Entry<String, NodeType> nodeTypeEntry : allNodeTypes.entrySet()) {
                NodeType nodeType = nodeTypeEntry.getValue();
                if (nodeType.getRequirements() != null && !nodeType.getRequirements().isEmpty()) {
                    for (RequirementDefinition requirementDefinition : nodeType.getRequirements()) {
                        NodeFilter nodeFilter = requirementDefinition.getNodeFilter();
                        if (nodeFilter != null) {
                            Map<String, FilterDefinition> capabilitiesFilters = nodeFilter.getCapabilities();
                            if (MapUtils.isNotEmpty(capabilitiesFilters)) {
                                for (Map.Entry<String, FilterDefinition> capabilityFilterEntry : capabilitiesFilters.entrySet()) {
                                    FilterDefinition filterDefinition = capabilityFilterEntry.getValue();
                                    for (Map.Entry<String, List<PropertyConstraint>> constraintEntry : filterDefinition.getProperties().entrySet()) {
                                        List<PropertyConstraint> constraints = constraintEntry.getValue();
                                        checkPropertyConstraints("node_filter.capabilities", CapabilityType.class, capabilityFilterEntry.getKey(), constraintEntry.getKey(), constraints, context);
                                    }
                                }
                            }
                        // FIXME check also the value properties filter of a node filter
                        }
                    }
                }
            }
        }
    }
}
Also used : RequirementDefinition(org.alien4cloud.tosca.model.definitions.RequirementDefinition) FilterDefinition(org.alien4cloud.tosca.model.definitions.FilterDefinition) PropertyConstraint(org.alien4cloud.tosca.model.definitions.PropertyConstraint) ArchiveRoot(alien4cloud.tosca.model.ArchiveRoot) RelationshipTemplate(org.alien4cloud.tosca.model.templates.RelationshipTemplate) List(java.util.List) ArrayList(java.util.ArrayList) ParsingContext(alien4cloud.tosca.parser.ParsingContext) Capability(org.alien4cloud.tosca.model.templates.Capability) Topology(org.alien4cloud.tosca.model.templates.Topology) NodeTemplate(org.alien4cloud.tosca.model.templates.NodeTemplate) NodeType(org.alien4cloud.tosca.model.types.NodeType) Map(java.util.Map) NodeFilter(org.alien4cloud.tosca.model.definitions.NodeFilter)

Example 24 with Capability

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

the class PostMatchingNodeSetupModifier method doMergeNode.

@Override
protected boolean doMergeNode(Topology topology, FlowExecutionContext context, String nodeTemplateId, NodePropsOverride nodePropsOverride) {
    final ConfigChanged configChanged = new ConfigChanged();
    // play the super method first. This will process nodetemplate properties
    configChanged.changed = super.doMergeNode(topology, context, nodeTemplateId, nodePropsOverride);
    // Then process capabilities
    NodeTemplate nodeTemplate = topology.getNodeTemplates().get(nodeTemplateId);
    Iterator<Entry<String, NodeCapabilitiesPropsOverride>> capabilitiesOverrideIter = safe(nodePropsOverride.getCapabilities()).entrySet().iterator();
    while (capabilitiesOverrideIter.hasNext()) {
        Entry<String, NodeCapabilitiesPropsOverride> overrideCapabilityProperties = capabilitiesOverrideIter.next();
        Capability capability = safe(nodeTemplate.getCapabilities()).get(overrideCapabilityProperties.getKey());
        if (capability == null) {
            // Manage clean logic
            configChanged.changed = true;
            capabilitiesOverrideIter.remove();
        } else {
            // Merge logic
            // When a selected node has changed we may need to cleanup properties that where defined but are not anymore on the capability
            CapabilityType capabilityType = ToscaContext.get(CapabilityType.class, capability.getType());
            capability.setProperties(mergeProperties(overrideCapabilityProperties.getValue().getProperties(), capability.getProperties(), capabilityType.getProperties(), s -> {
                configChanged.changed = true;
                context.log().info("The property [" + s + "] previously specified to configure capability [" + overrideCapabilityProperties.getKey() + "] of node [" + nodeTemplateId + "] cannot be set anymore as it is already specified by the matched location resource or in the topology.");
            }));
        }
    }
    return configChanged.changed;
}
Also used : Iterator(java.util.Iterator) NodeType(org.alien4cloud.tosca.model.types.NodeType) AlienUtils.safe(alien4cloud.utils.AlienUtils.safe) 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) Map(java.util.Map) NodeTemplate(org.alien4cloud.tosca.model.templates.NodeTemplate) CapabilityType(org.alien4cloud.tosca.model.types.CapabilityType) Entry(java.util.Map.Entry) Topology(org.alien4cloud.tosca.model.templates.Topology) NodeCapabilitiesPropsOverride(org.alien4cloud.alm.deployment.configuration.model.DeploymentMatchingConfiguration.NodeCapabilitiesPropsOverride) NodePropsOverride(org.alien4cloud.alm.deployment.configuration.model.DeploymentMatchingConfiguration.NodePropsOverride) Capability(org.alien4cloud.tosca.model.templates.Capability) ToscaContext(alien4cloud.tosca.context.ToscaContext) Entry(java.util.Map.Entry) CapabilityType(org.alien4cloud.tosca.model.types.CapabilityType) NodeTemplate(org.alien4cloud.tosca.model.templates.NodeTemplate) Capability(org.alien4cloud.tosca.model.templates.Capability) NodeCapabilitiesPropsOverride(org.alien4cloud.alm.deployment.configuration.model.DeploymentMatchingConfiguration.NodeCapabilitiesPropsOverride) NodeCapabilitiesPropsOverride(org.alien4cloud.alm.deployment.configuration.model.DeploymentMatchingConfiguration.NodeCapabilitiesPropsOverride) NodePropsOverride(org.alien4cloud.alm.deployment.configuration.model.DeploymentMatchingConfiguration.NodePropsOverride)

Example 25 with Capability

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

the class PaaSUtils method injectTargetedCapabilityProperties.

private static void injectTargetedCapabilityProperties(PaaSNodeTemplate target, String capabilityName, Map<String, Interface> interfaces) {
    if (target.getTemplate().getCapabilities() != null && target.getTemplate().getCapabilities().containsKey(capabilityName)) {
        Capability capability = target.getTemplate().getCapabilities().get(capabilityName);
        // input name: TARGET_CAPABILITIES_<capabilityName>_<propertyName>
        injectPropertiesAsInputs(ToscaFunctionConstants.TARGET, capabilityName, capability.getProperties(), interfaces, baseName -> StringUtils.joinWith(AlienUtils.DEFAULT_PREFIX_SEPARATOR, ToscaFunctionConstants.TARGET, CAPABILITIES, capabilityName, baseName));
    }
}
Also used : Capability(org.alien4cloud.tosca.model.templates.Capability)

Aggregations

Capability (org.alien4cloud.tosca.model.templates.Capability)50 NodeTemplate (org.alien4cloud.tosca.model.templates.NodeTemplate)27 CapabilityType (org.alien4cloud.tosca.model.types.CapabilityType)26 AbstractPropertyValue (org.alien4cloud.tosca.model.definitions.AbstractPropertyValue)14 RelationshipTemplate (org.alien4cloud.tosca.model.templates.RelationshipTemplate)12 Map (java.util.Map)10 PropertyDefinition (org.alien4cloud.tosca.model.definitions.PropertyDefinition)10 NotFoundException (alien4cloud.exception.NotFoundException)9 NodeType (org.alien4cloud.tosca.model.types.NodeType)9 Test (org.junit.Test)9 Set (java.util.Set)8 CSARDependency (org.alien4cloud.tosca.model.CSARDependency)5 CapabilityDefinition (org.alien4cloud.tosca.model.definitions.CapabilityDefinition)5 RelationshipType (org.alien4cloud.tosca.model.types.RelationshipType)5 HashMap (java.util.HashMap)4 List (java.util.List)4 ScalarPropertyValue (org.alien4cloud.tosca.model.definitions.ScalarPropertyValue)4 Topology (org.alien4cloud.tosca.model.templates.Topology)4 Location (alien4cloud.model.orchestrators.locations.Location)3 PaaSNodeTemplate (alien4cloud.paas.model.PaaSNodeTemplate)3