Search in sources :

Example 66 with NodeTemplate

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

the class InputArtifactsModifier method processInputArtifacts.

/**
 * Inject input artifacts in the corresponding nodes.
 *
 * @param topology The topology in which to inject input artifacts.
 * @param inputArtifacts The input artifacts to inject in the topology.
 */
private void processInputArtifacts(Topology topology, Map<String, DeploymentArtifact> inputArtifacts) {
    if (topology.getInputArtifacts() != null && !topology.getInputArtifacts().isEmpty()) {
        // we'll build a map inputArtifactId -> List<DeploymentArtifact>
        Map<String, List<DeploymentArtifact>> artifactMap = Maps.newHashMap();
        // iterate over nodes in order to remember all nodes referencing an input artifact
        for (NodeTemplate nodeTemplate : topology.getNodeTemplates().values()) {
            if (MapUtils.isNotEmpty(nodeTemplate.getArtifacts())) {
                processInputArtifactForTemplate(artifactMap, nodeTemplate);
            }
            if (MapUtils.isNotEmpty(nodeTemplate.getRelationships())) {
                nodeTemplate.getRelationships().entrySet().stream().filter(relationshipTemplateEntry -> MapUtils.isNotEmpty(relationshipTemplateEntry.getValue().getArtifacts())).forEach(relationshipTemplateEntry -> processInputArtifactForTemplate(artifactMap, relationshipTemplateEntry.getValue()));
            }
        }
        // Override the artifacts definition in the topology with the input artifact data.
        Map<String, DeploymentArtifact> allInputArtifact = new HashMap<>();
        allInputArtifact.putAll(topology.getInputArtifacts());
        if (MapUtils.isNotEmpty(inputArtifacts)) {
            allInputArtifact.putAll(inputArtifacts);
        }
        for (Map.Entry<String, DeploymentArtifact> inputArtifact : allInputArtifact.entrySet()) {
            List<DeploymentArtifact> nodeArtifacts = artifactMap.get(inputArtifact.getKey());
            if (nodeArtifacts != null) {
                for (DeploymentArtifact nodeArtifact : nodeArtifacts) {
                    nodeArtifact.setArtifactRef(inputArtifact.getValue().getArtifactRef());
                    nodeArtifact.setArtifactName(inputArtifact.getValue().getArtifactName());
                    nodeArtifact.setArtifactRepository(inputArtifact.getValue().getArtifactRepository());
                    nodeArtifact.setRepositoryName(inputArtifact.getValue().getRepositoryName());
                    nodeArtifact.setRepositoryCredential(inputArtifact.getValue().getRepositoryCredential());
                    nodeArtifact.setRepositoryURL(inputArtifact.getValue().getRepositoryURL());
                    nodeArtifact.setArchiveName(inputArtifact.getValue().getArchiveName());
                    nodeArtifact.setArchiveVersion(inputArtifact.getValue().getArchiveVersion());
                }
            }
        }
    }
}
Also used : MapUtils(org.apache.commons.collections4.MapUtils) Lists(org.elasticsearch.common.collect.Lists) Iterator(java.util.Iterator) ApplicationEnvironment(alien4cloud.model.application.ApplicationEnvironment) InputArtifactUtil(alien4cloud.utils.InputArtifactUtil) HashMap(java.util.HashMap) Maps(com.google.common.collect.Maps) AlienUtils.safe(alien4cloud.utils.AlienUtils.safe) AbstractInstantiableTemplate(org.alien4cloud.tosca.model.templates.AbstractInstantiableTemplate) List(java.util.List) Component(org.springframework.stereotype.Component) FlowExecutionContext(org.alien4cloud.alm.deployment.configuration.flow.FlowExecutionContext) Map(java.util.Map) NodeTemplate(org.alien4cloud.tosca.model.templates.NodeTemplate) Entry(java.util.Map.Entry) DeploymentInputs(org.alien4cloud.alm.deployment.configuration.model.DeploymentInputs) Topology(org.alien4cloud.tosca.model.templates.Topology) ITopologyModifier(org.alien4cloud.alm.deployment.configuration.flow.ITopologyModifier) DeploymentArtifact(org.alien4cloud.tosca.model.definitions.DeploymentArtifact) NodeTemplate(org.alien4cloud.tosca.model.templates.NodeTemplate) HashMap(java.util.HashMap) List(java.util.List) DeploymentArtifact(org.alien4cloud.tosca.model.definitions.DeploymentArtifact) HashMap(java.util.HashMap) Map(java.util.Map)

Example 67 with NodeTemplate

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

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

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

the class CfyMultirelationshipErrorModifier method process.

@Override
public void process(Topology topology, FlowExecutionContext context) {
    // Check if orchestrator is cloudify
    Optional<DeploymentMatchingConfiguration> configurationOptional = context.getConfiguration(DeploymentMatchingConfiguration.class, LocationMatchingModifier.class.getSimpleName());
    if (!configurationOptional.isPresent()) {
        context.log().error(new LocationPolicyTask());
        return;
    }
    DeploymentMatchingConfiguration matchingConfiguration = configurationOptional.get();
    Orchestrator orchestrator = orchestratorService.getOrFail(matchingConfiguration.getOrchestratorId());
    if (!orchestrator.getPluginId().contains("cloudify")) {
        return;
    }
    // For every node check that there is not two relationships defined
    for (Entry<String, NodeTemplate> nodeTemplateEntry : safe(topology.getNodeTemplates()).entrySet()) {
        // Keep track of the relationship id
        Set<String> relationshipTargets = Sets.newHashSet();
        for (Entry<String, RelationshipTemplate> relationshipTemplateEntry : safe(nodeTemplateEntry.getValue().getRelationships()).entrySet()) {
            if (relationshipTargets.contains(relationshipTemplateEntry.getValue().getTarget())) {
                context.log().error(TaskCode.CFY_MULTI_RELATIONS, "Cloudify orchestrator does not support multiple relationships between the same source and target nodes. Topology defines more than one between " + nodeTemplateEntry.getKey() + " and " + relationshipTemplateEntry.getValue().getTarget() + ".");
                return;
            }
            relationshipTargets.add(relationshipTemplateEntry.getValue().getTarget());
        }
    }
}
Also used : NodeTemplate(org.alien4cloud.tosca.model.templates.NodeTemplate) RelationshipTemplate(org.alien4cloud.tosca.model.templates.RelationshipTemplate) LocationPolicyTask(alien4cloud.topology.task.LocationPolicyTask) DeploymentMatchingConfiguration(org.alien4cloud.alm.deployment.configuration.model.DeploymentMatchingConfiguration) Orchestrator(alien4cloud.model.orchestrators.Orchestrator)

Example 70 with NodeTemplate

use of org.alien4cloud.tosca.model.templates.NodeTemplate 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)

Aggregations

NodeTemplate (org.alien4cloud.tosca.model.templates.NodeTemplate)162 NodeType (org.alien4cloud.tosca.model.types.NodeType)52 Map (java.util.Map)40 RelationshipTemplate (org.alien4cloud.tosca.model.templates.RelationshipTemplate)37 Test (org.junit.Test)32 Capability (org.alien4cloud.tosca.model.templates.Capability)27 AbstractPropertyValue (org.alien4cloud.tosca.model.definitions.AbstractPropertyValue)25 Topology (org.alien4cloud.tosca.model.templates.Topology)22 NotFoundException (alien4cloud.exception.NotFoundException)17 Then (cucumber.api.java.en.Then)15 ScalarPropertyValue (org.alien4cloud.tosca.model.definitions.ScalarPropertyValue)15 HashMap (java.util.HashMap)14 PropertyDefinition (org.alien4cloud.tosca.model.definitions.PropertyDefinition)13 CapabilityType (org.alien4cloud.tosca.model.types.CapabilityType)13 TopologyDTO (alien4cloud.topology.TopologyDTO)12 ComplexPropertyValue (org.alien4cloud.tosca.model.definitions.ComplexPropertyValue)12 LocationResourceTemplate (alien4cloud.model.orchestrators.locations.LocationResourceTemplate)11 PaaSNodeTemplate (alien4cloud.paas.model.PaaSNodeTemplate)11 DeploymentArtifact (org.alien4cloud.tosca.model.definitions.DeploymentArtifact)10 RelationshipType (org.alien4cloud.tosca.model.types.RelationshipType)10