Search in sources :

Example 76 with NodeTemplate

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

the class SemanticValidation method validate.

@Override
public List<AbstractWorkflowError> validate(TopologyContext topologyContext, Workflow workflow) throws WorkflowException {
    if (workflow.getSteps() == null || workflow.getSteps().isEmpty()) {
        return null;
    }
    List<AbstractWorkflowError> errors = Lists.newArrayList();
    for (WorkflowStep step : workflow.getSteps().values()) {
        if (step.getActivity() instanceof InlineWorkflowActivity) {
            // TODO when the tosca model is clear we should create InlineWorkflowStep
            String inlinedWorkflow = ((InlineWorkflowActivity) step.getActivity()).getInline();
            if (topologyContext.getTopology().getWorkflows() == null || !topologyContext.getTopology().getWorkflows().containsKey(inlinedWorkflow)) {
                errors.add(new InlinedWorkflowNotFoundError(step.getName(), inlinedWorkflow));
            }
        } else if (StringUtils.isEmpty(step.getTarget())) {
            errors.add(new UnknownNodeError(step.getName(), "undefined target in non inline workflow activity"));
        } else {
            String nodeId = step.getTarget();
            NodeTemplate nodeTemplate = null;
            if (topologyContext.getTopology().getNodeTemplates() != null) {
                nodeTemplate = topologyContext.getTopology().getNodeTemplates().get(nodeId);
            }
            if (nodeTemplate == null) {
                errors.add(new UnknownNodeError(step.getName(), nodeId));
            } else if (step instanceof RelationshipWorkflowStep) {
                RelationshipWorkflowStep relationshipWorkflowStep = (RelationshipWorkflowStep) step;
                String relationshipId = relationshipWorkflowStep.getTargetRelationship();
                if (nodeTemplate.getRelationships() == null || !nodeTemplate.getRelationships().containsKey(relationshipId)) {
                    errors.add(new UnknownRelationshipError(step.getName(), nodeId, relationshipId));
                }
            }
        // TODO: here we should check interface & operation
        }
    }
    return errors;
}
Also used : InlineWorkflowActivity(org.alien4cloud.tosca.model.workflow.activities.InlineWorkflowActivity) NodeTemplate(org.alien4cloud.tosca.model.templates.NodeTemplate) RelationshipWorkflowStep(org.alien4cloud.tosca.model.workflow.RelationshipWorkflowStep) WorkflowStep(org.alien4cloud.tosca.model.workflow.WorkflowStep) RelationshipWorkflowStep(org.alien4cloud.tosca.model.workflow.RelationshipWorkflowStep)

Example 77 with NodeTemplate

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

the class TopologyCompositionService method renameNodeTemplate.

private void renameNodeTemplate(Topology topology, String oldName, String newName) {
    // quite improbable but ...
    if (topology.getNodeTemplates().containsKey(newName)) {
        throw new AlreadyExistException(String.format("A node with name '%s' already exists in this topology", newName));
    }
    NodeTemplate nodeTemplate = topology.getNodeTemplates().remove(oldName);
    // manage relationships that target this node
    for (NodeTemplate otherNodes : topology.getNodeTemplates().values()) {
        if (otherNodes.getRelationships() == null || otherNodes.getRelationships().isEmpty()) {
            continue;
        }
        for (RelationshipTemplate relationshipTemplate : otherNodes.getRelationships().values()) {
            if (relationshipTemplate.getTarget().equals(oldName)) {
                relationshipTemplate.setTarget(newName);
            }
        }
    }
    // all output stuffs
    MapUtil.replaceKey(topology.getOutputProperties(), oldName, newName);
    MapUtil.replaceKey(topology.getOutputCapabilityProperties(), oldName, newName);
    MapUtil.replaceKey(topology.getOutputAttributes(), oldName, newName);
    // group members must be updated
    if (topology.getGroups() != null) {
        for (NodeGroup nodeGroup : topology.getGroups().values()) {
            Set<String> members = nodeGroup.getMembers();
            if (members != null && members.remove(oldName)) {
                members.add(newName);
            }
        }
    }
    // substitutions
    if (topology.getSubstitutionMapping() != null) {
        renameNodeTemplateInSubstitutionTargets(topology.getSubstitutionMapping().getCapabilities(), oldName, newName);
        renameNodeTemplateInSubstitutionTargets(topology.getSubstitutionMapping().getRequirements(), oldName, newName);
    }
    // finally the node itself
    topology.getNodeTemplates().put(newName, nodeTemplate);
}
Also used : NodeTemplate(org.alien4cloud.tosca.model.templates.NodeTemplate) RelationshipTemplate(org.alien4cloud.tosca.model.templates.RelationshipTemplate) AlreadyExistException(alien4cloud.exception.AlreadyExistException) NodeGroup(org.alien4cloud.tosca.model.templates.NodeGroup)

Example 78 with NodeTemplate

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

the class TopologyCompositionService method processComposition.

/**
 * Process the composition:
 * <ul>
 * <li>remove the 'proxy' node from the parent.
 * <li>merge the child topology nodes into the parent nodes.
 * <li>
 * </ul>
 *
 * @param compositionCouple
 */
private void processComposition(CompositionCouple compositionCouple) {
    // first of all, remove the proxy node from the parent
    NodeTemplate proxyNodeTemplate = compositionCouple.parent.getNodeTemplates().remove(compositionCouple.nodeName);
    // properties of the proxy are used to feed the property values of child node that use get_input
    for (NodeTemplate childNodeTemplate : compositionCouple.child.getNodeTemplates().values()) {
        for (Entry<String, AbstractPropertyValue> propertyEntry : childNodeTemplate.getProperties().entrySet()) {
            AbstractPropertyValue pValue = propertyEntry.getValue();
            if (isGetInput(pValue)) {
                String inputName = ((FunctionPropertyValue) pValue).getTemplateName();
                propertyEntry.setValue(proxyNodeTemplate.getProperties().get(inputName));
            }
        }
        for (Entry<String, Capability> capabilityEntry : childNodeTemplate.getCapabilities().entrySet()) {
            if (capabilityEntry.getValue().getProperties() != null) {
                for (Entry<String, AbstractPropertyValue> propertyEntry : capabilityEntry.getValue().getProperties().entrySet()) {
                    AbstractPropertyValue pValue = propertyEntry.getValue();
                    if (isGetInput(pValue)) {
                        String inputName = ((FunctionPropertyValue) pValue).getTemplateName();
                        propertyEntry.setValue(proxyNodeTemplate.getProperties().get(inputName));
                    }
                }
            }
        }
    }
    // all relations from the proxy must now start from the corresponding node
    if (proxyNodeTemplate.getRelationships() != null) {
        for (Entry<String, RelationshipTemplate> e : proxyNodeTemplate.getRelationships().entrySet()) {
            String relationShipKey = e.getKey();
            RelationshipTemplate proxyRelationShip = e.getValue();
            String requirementName = proxyRelationShip.getRequirementName();
            SubstitutionTarget substitutionTarget = compositionCouple.child.getSubstitutionMapping().getRequirements().get(requirementName);
            NodeTemplate nodeTemplate = compositionCouple.child.getNodeTemplates().get(substitutionTarget.getNodeTemplateName());
            if (nodeTemplate.getRelationships() == null) {
                Map<String, RelationshipTemplate> relationships = Maps.newHashMap();
                nodeTemplate.setRelationships(relationships);
            }
            nodeTemplate.getRelationships().put(relationShipKey, proxyRelationShip);
            proxyRelationShip.setRequirementName(substitutionTarget.getTargetId());
        }
    }
    // all relations that target the proxy must be redirected to the corresponding child node
    for (NodeTemplate otherNodes : compositionCouple.parent.getNodeTemplates().values()) {
        if (otherNodes.getRelationships() != null) {
            for (RelationshipTemplate relationshipTemplate : otherNodes.getRelationships().values()) {
                if (relationshipTemplate.getTarget().equals(compositionCouple.nodeName)) {
                    SubstitutionTarget st = compositionCouple.child.getSubstitutionMapping().getCapabilities().get(relationshipTemplate.getTargetedCapabilityName());
                    relationshipTemplate.setTarget(st.getNodeTemplateName());
                    relationshipTemplate.setTargetedCapabilityName(st.getTargetId());
                }
            }
        }
    }
    if (compositionCouple.parent.getOutputAttributes() != null) {
        Set<String> outputAttributes = compositionCouple.parent.getOutputAttributes().remove(compositionCouple.nodeName);
        if (outputAttributes != null) {
            for (String proxyAttributeName : outputAttributes) {
                sustituteGetAttribute(compositionCouple.child, compositionCouple.parent, proxyAttributeName);
            }
        }
    }
    // the parent itself expose stuffs, we eventually need to replace substitution targets
    if (compositionCouple.parent.getSubstitutionMapping() != null) {
        if (compositionCouple.parent.getSubstitutionMapping().getCapabilities() != null) {
            for (Entry<String, SubstitutionTarget> substitutionCapabilityEntry : compositionCouple.parent.getSubstitutionMapping().getCapabilities().entrySet()) {
                if (substitutionCapabilityEntry.getValue().getNodeTemplateName().equals(compositionCouple.nodeName)) {
                    String targetCapability = substitutionCapabilityEntry.getValue().getTargetId();
                    // just substitute the substitution target
                    substitutionCapabilityEntry.setValue(compositionCouple.child.getSubstitutionMapping().getCapabilities().get(targetCapability));
                }
            }
        }
        if (compositionCouple.parent.getSubstitutionMapping().getRequirements() != null) {
            for (Entry<String, SubstitutionTarget> e : compositionCouple.parent.getSubstitutionMapping().getRequirements().entrySet()) {
                if (e.getValue().getNodeTemplateName().equals(compositionCouple.nodeName)) {
                    String targetCapability = e.getValue().getTargetId();
                    // just substitute the substitution target
                    e.setValue(compositionCouple.child.getSubstitutionMapping().getRequirements().get(targetCapability));
                }
            }
        }
    }
    // merge each child nodes into the parent
    compositionCouple.parent.getNodeTemplates().putAll(compositionCouple.child.getNodeTemplates());
}
Also used : Capability(org.alien4cloud.tosca.model.templates.Capability) SubstitutionTarget(org.alien4cloud.tosca.model.templates.SubstitutionTarget) NodeTemplate(org.alien4cloud.tosca.model.templates.NodeTemplate) RelationshipTemplate(org.alien4cloud.tosca.model.templates.RelationshipTemplate) FunctionPropertyValue(org.alien4cloud.tosca.model.definitions.FunctionPropertyValue) AbstractPropertyValue(org.alien4cloud.tosca.model.definitions.AbstractPropertyValue)

Example 79 with NodeTemplate

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

the class TopologyCompositionService method recursivelyBuildSubstitutionStack.

/**
 * Deeply explore this topology to detect if some type must be substituted by the corresponding topology template content and feed the {@link Deque}. <br>
 * BTW, rename the nodes by prefixing all the node names.
 */
private void recursivelyBuildSubstitutionStack(Topology topology, Deque<CompositionCouple> stack, String prefix) {
    if (topology == null || topology.getNodeTemplates() == null || topology.getNodeTemplates().isEmpty()) {
        return;
    }
    for (Entry<String, NodeTemplate> nodeEntry : topology.getNodeTemplates().entrySet()) {
        String nodeName = nodeEntry.getKey();
        String type = nodeEntry.getValue().getType();
        // FIXME use tosca context, beware of child topologies (dependencies to use ? conflicts ?)
        NodeType nodeType = csarRepoSearchService.getRequiredElementInDependencies(NodeType.class, type, topology.getDependencies());
        if (nodeType.getSubstitutionTopologyId() != null) {
            // this node type is a proxy for a topology template
            Topology child = topologyServiceCore.getOrFail(nodeType.getSubstitutionTopologyId());
            CompositionCouple couple = new CompositionCouple(topology, child, nodeName, nodeName + "_");
            renameNodes(couple);
            stack.offer(couple);
            recursivelyBuildSubstitutionStack(child, stack, nodeName + "_");
        }
    }
}
Also used : NodeTemplate(org.alien4cloud.tosca.model.templates.NodeTemplate) NodeType(org.alien4cloud.tosca.model.types.NodeType) Topology(org.alien4cloud.tosca.model.templates.Topology)

Example 80 with NodeTemplate

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

the class NodeMatcherService method match.

public Map<String, List<LocationResourceTemplate>> match(Map<String, NodeType> nodesTypes, Map<String, NodeTemplate> nodesToMatch, Location location, String environmentId) {
    Map<String, List<LocationResourceTemplate>> matchingResult = Maps.newHashMap();
    // fetch location resources
    LocationResources locationResources = locationResourceService.getLocationResources(location);
    // Authorization filtering of location resources
    filterOnAuthorization(locationResources.getNodeTemplates(), environmentId);
    // fetch service resources
    List<ServiceResource> services = serviceResourceService.searchByLocation(location.getId());
    // self filtering: remove managed service linked to this location
    filterSelfManagedService(services, environmentId);
    // Authorization filtering of location resources
    filterOnAuthorization(services, environmentId);
    // from serviceResource to locationResource
    populateLocationResourcesWithServiceResource(locationResources, services, location.getId());
    Map<String, MatchingConfiguration> matchingConfigurations = locationMatchingConfigurationService.getMatchingConfiguration(location);
    Set<String> typesManagedByLocation = Sets.newHashSet();
    for (NodeType nodeType : locationResources.getNodeTypes().values()) {
        typesManagedByLocation.add(nodeType.getElementId());
        typesManagedByLocation.addAll(nodeType.getDerivedFrom());
    }
    INodeMatcherPlugin nodeMatcherPlugin = getNodeMatcherPlugin();
    for (Map.Entry<String, NodeTemplate> nodeTemplateEntry : nodesToMatch.entrySet()) {
        String nodeTemplateId = nodeTemplateEntry.getKey();
        NodeTemplate nodeTemplate = nodeTemplateEntry.getValue();
        if (typesManagedByLocation.contains(nodeTemplate.getType())) {
            NodeType nodeTemplateType = nodesTypes.get(nodeTemplate.getType());
            if (nodeTemplateType == null) {
                throw new InvalidArgumentException("The given node types map must contain the type of the node template");
            }
            matchingResult.put(nodeTemplateId, nodeMatcherPlugin.matchNode(nodeTemplate, nodeTemplateType, locationResources, matchingConfigurations));
        }
    }
    return matchingResult;
}
Also used : MatchingConfiguration(alien4cloud.model.deployment.matching.MatchingConfiguration) LocationResources(alien4cloud.model.orchestrators.locations.LocationResources) ServiceNodeTemplate(org.alien4cloud.tosca.model.templates.ServiceNodeTemplate) NodeTemplate(org.alien4cloud.tosca.model.templates.NodeTemplate) InvalidArgumentException(alien4cloud.exception.InvalidArgumentException) NodeType(org.alien4cloud.tosca.model.types.NodeType) List(java.util.List) ServiceResource(alien4cloud.model.service.ServiceResource) INodeMatcherPlugin(alien4cloud.deployment.matching.plugins.INodeMatcherPlugin) Map(java.util.Map)

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