Search in sources :

Example 1 with Requirement

use of org.alien4cloud.tosca.model.templates.Requirement in project yorc-a4c-plugin by ystia.

the class ShowTopology method printNode.

/**
 * Print info about a Node
 * @param node
 */
private static void printNode(PaaSNodeTemplate node) {
    log.debug("******* Compute Node " + node.getId() + " *******");
    NodeTemplate nt = node.getTemplate();
    log.debug("CsarPath = " + node.getCsarPath());
    log.debug("Type = " + nt.getType());
    // Children
    List<PaaSNodeTemplate> children = node.getChildren();
    for (PaaSNodeTemplate child : children) {
        log.info("Child: " + child.getId());
    }
    // properties
    for (String prop : nt.getProperties().keySet()) {
        AbstractPropertyValue absval = nt.getProperties().get(prop);
        if (absval instanceof ScalarPropertyValue) {
            ScalarPropertyValue scaval = (ScalarPropertyValue) absval;
            log.debug(">> Property: " + prop + "=" + scaval.getValue());
        }
    }
    // Attributes
    Map<String, IValue> attrs = nt.getAttributes();
    if (attrs != null) {
        for (String attname : attrs.keySet()) {
            IValue att = attrs.get(attname);
            log.debug(">> Attribute: " + attname + "=" + att);
        }
    }
    // capabilities
    Map<String, Capability> capabilities = nt.getCapabilities();
    if (capabilities != null) {
        for (String capname : capabilities.keySet()) {
            Capability cap = capabilities.get(capname);
            log.debug(">> Capability " + capname);
            log.debug("type : " + cap.getType());
            log.debug("properties : " + cap.getProperties());
        }
    }
    // requirements
    Map<String, Requirement> requirements = nt.getRequirements();
    if (requirements != null) {
        for (String reqname : requirements.keySet()) {
            Requirement req = requirements.get(reqname);
            log.debug(">> Requirement: " + reqname);
            log.debug("type : " + req.getType());
            log.debug("properties : " + req.getProperties());
        }
    }
    // relationships
    Map<String, RelationshipTemplate> relations = nt.getRelationships();
    if (relations != null) {
        for (String relname : relations.keySet()) {
            RelationshipTemplate rel = relations.get(relname);
            log.debug(">> Relationship: " + relname);
            log.debug("type : " + rel.getType());
            log.debug("properties : " + rel.getProperties());
        }
    }
    // artifacts
    Map<String, DeploymentArtifact> artifacts = nt.getArtifacts();
    if (artifacts != null) {
        for (DeploymentArtifact art : artifacts.values()) {
            printArtifact(art);
        }
    }
}
Also used : Capability(org.alien4cloud.tosca.model.templates.Capability) Requirement(org.alien4cloud.tosca.model.templates.Requirement) PaaSNodeTemplate(alien4cloud.paas.model.PaaSNodeTemplate) NodeTemplate(org.alien4cloud.tosca.model.templates.NodeTemplate) PaaSNodeTemplate(alien4cloud.paas.model.PaaSNodeTemplate) RelationshipTemplate(org.alien4cloud.tosca.model.templates.RelationshipTemplate)

Example 2 with Requirement

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

the class FunctionEvaluator method doGetProperty.

private static AbstractPropertyValue doGetProperty(FunctionEvaluatorContext evaluatorContext, AbstractInstantiableTemplate targetTemplate, FunctionPropertyValue function) {
    if (targetTemplate == null) {
        return null;
    }
    // If a requirement or capability name is defined then it is applied to the node template.
    if (function.getCapabilityOrRequirementName() != null) {
        if (targetTemplate instanceof RelationshipTemplate) {
            throw new IllegalArgumentException("Get property that specifies a capability or relationship target must be placed on a node template and not a relationship template.");
        }
        AbstractPropertyValue propertyValue = null;
        Capability targetCapability = safe(((NodeTemplate) targetTemplate).getCapabilities()).get(function.getCapabilityOrRequirementName());
        if (targetCapability != null) {
            propertyValue = getFromPath(evaluatorContext, targetTemplate, targetCapability.getProperties(), function.getElementNameToFetch());
        }
        if (propertyValue == null) {
            Requirement requirement = safe(((NodeTemplate) targetTemplate).getRequirements()).get(function.getCapabilityOrRequirementName());
            if (requirement != null) {
                propertyValue = getFromPath(evaluatorContext, targetTemplate, requirement.getProperties(), function.getElementNameToFetch());
            }
        }
        if (propertyValue == null) {
            // try to find the value from the host node.
            propertyValue = doGetProperty(evaluatorContext, TopologyNavigationUtil.getImmediateHostTemplate(evaluatorContext.getTopology(), (NodeTemplate) targetTemplate), function);
        }
        return tryResolveValue(evaluatorContext, targetTemplate, targetTemplate.getProperties(), propertyValue);
    }
    // Try to fetch from the node.
    AbstractPropertyValue propertyValue = getFromPath(evaluatorContext, targetTemplate, targetTemplate.getProperties(), function.getElementNameToFetch());
    if (propertyValue == null && targetTemplate instanceof NodeTemplate) {
        propertyValue = doGetProperty(evaluatorContext, TopologyNavigationUtil.getImmediateHostTemplate(evaluatorContext.getTopology(), (NodeTemplate) targetTemplate), function);
    }
    // if the property refers to a function (get_input/get_property then try to resolve it).
    return tryResolveValue(evaluatorContext, targetTemplate, targetTemplate.getProperties(), propertyValue);
}
Also used : Requirement(org.alien4cloud.tosca.model.templates.Requirement) RelationshipTemplate(org.alien4cloud.tosca.model.templates.RelationshipTemplate) NodeTemplate(org.alien4cloud.tosca.model.templates.NodeTemplate) Capability(org.alien4cloud.tosca.model.templates.Capability) AbstractPropertyValue(org.alien4cloud.tosca.model.definitions.AbstractPropertyValue)

Example 3 with Requirement

use of org.alien4cloud.tosca.model.templates.Requirement 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 4 with Requirement

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

the class TopologyRequirementBoundsValidationServices method isRequirementUpperBoundReachedForSource.

/**
 * Check if the upperBound of a requirement is reached on a node template
 *
 * @param nodeTemplate the node to check for requirement bound
 * @param requirementName the name of the requirement
 * @param dependencies the dependencies of the topology
 * @return true if requirement upper bound is reached, false otherwise
 */
public boolean isRequirementUpperBoundReachedForSource(NodeTemplate nodeTemplate, String requirementName, Set<CSARDependency> dependencies) {
    NodeType relatedIndexedNodeType = toscaTypeSearchService.getRequiredElementInDependencies(NodeType.class, nodeTemplate.getType(), dependencies);
    Requirement requirement = nodeTemplate.getRequirements().get(requirementName);
    if (nodeTemplate.getRelationships() == null || nodeTemplate.getRelationships().isEmpty()) {
        return false;
    }
    RequirementDefinition requirementDefinition = getRequirementDefinition(relatedIndexedNodeType.getRequirements(), requirementName, requirement.getType());
    if (requirementDefinition.getUpperBound() == Integer.MAX_VALUE) {
        return false;
    }
    int count = countRelationshipsForRequirement(nodeTemplate, requirementDefinition);
    return count >= requirementDefinition.getUpperBound();
}
Also used : Requirement(org.alien4cloud.tosca.model.templates.Requirement) NodeTemplateUtils.countRelationshipsForRequirement(org.alien4cloud.tosca.utils.NodeTemplateUtils.countRelationshipsForRequirement) NodeType(org.alien4cloud.tosca.model.types.NodeType) RequirementDefinition(org.alien4cloud.tosca.model.definitions.RequirementDefinition)

Example 5 with Requirement

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

the class TemplateBuilder method fillRequirementsMap.

private static void fillRequirementsMap(Map<String, Requirement> map, List<RequirementDefinition> elements, Map<String, Requirement> mapToMerge, boolean adaptToType) {
    if (elements == null) {
        return;
    }
    for (RequirementDefinition requirement : elements) {
        Requirement toAddRequirement = MapUtils.getObject(mapToMerge, requirement.getId());
        // the type of a requirement is a capability type in TOSCA as they match each other.
        CapabilityType requirementType = ToscaContext.get(CapabilityType.class, requirement.getType());
        if (toAddRequirement == null || !Objects.equals(toAddRequirement.getType(), requirement.getType())) {
            toAddRequirement = new Requirement();
            toAddRequirement.setType(requirement.getType());
        }
        Map<String, AbstractPropertyValue> properties = Maps.newLinkedHashMap();
        fillProperties(properties, requirementType != null ? requirementType.getProperties() : null, toAddRequirement.getProperties(), adaptToType);
        toAddRequirement.setProperties(properties);
        map.put(requirement.getId(), toAddRequirement);
    }
}
Also used : Requirement(org.alien4cloud.tosca.model.templates.Requirement) CapabilityType(org.alien4cloud.tosca.model.types.CapabilityType) RequirementDefinition(org.alien4cloud.tosca.model.definitions.RequirementDefinition) AbstractPropertyValue(org.alien4cloud.tosca.model.definitions.AbstractPropertyValue)

Aggregations

Requirement (org.alien4cloud.tosca.model.templates.Requirement)5 AbstractPropertyValue (org.alien4cloud.tosca.model.definitions.AbstractPropertyValue)3 Capability (org.alien4cloud.tosca.model.templates.Capability)3 NodeTemplate (org.alien4cloud.tosca.model.templates.NodeTemplate)3 RelationshipTemplate (org.alien4cloud.tosca.model.templates.RelationshipTemplate)3 RequirementDefinition (org.alien4cloud.tosca.model.definitions.RequirementDefinition)2 ApplicationEnvironment (alien4cloud.model.application.ApplicationEnvironment)1 Location (alien4cloud.model.orchestrators.locations.Location)1 PaaSNodeTemplate (alien4cloud.paas.model.PaaSNodeTemplate)1 Map (java.util.Map)1 EnvironmentContext (org.alien4cloud.alm.deployment.configuration.flow.EnvironmentContext)1 DeploymentInputs (org.alien4cloud.alm.deployment.configuration.model.DeploymentInputs)1 PreconfiguredInputsConfiguration (org.alien4cloud.alm.deployment.configuration.model.PreconfiguredInputsConfiguration)1 PropertyValue (org.alien4cloud.tosca.model.definitions.PropertyValue)1 CapabilityType (org.alien4cloud.tosca.model.types.CapabilityType)1 NodeType (org.alien4cloud.tosca.model.types.NodeType)1 FunctionEvaluatorContext (org.alien4cloud.tosca.utils.FunctionEvaluatorContext)1 NodeTemplateUtils.countRelationshipsForRequirement (org.alien4cloud.tosca.utils.NodeTemplateUtils.countRelationshipsForRequirement)1