Search in sources :

Example 11 with RelationshipTemplate

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

the class ReplaceNodeProcessor method removeTheRelationshipsOnSource.

/**
 * If the node to replace is an target of relationship, we should swapped the relationship on source and clean used workflow steps
 */
private Map<String, Map<String, RelationshipTemplate>> removeTheRelationshipsOnSource(Topology topology, Csar csar, String nodeToReplace) {
    Map<String, Map<String, RelationshipTemplate>> relationshipDeleted = Maps.newLinkedHashMap();
    for (NodeTemplate nodeTemplate : safe(topology.getNodeTemplates()).values()) {
        if (!nodeToReplace.equals(nodeTemplate.getName())) {
            List<String> relationshipsToRemove = Lists.newArrayList();
            for (RelationshipTemplate relationshipTemplate : safe(nodeTemplate.getRelationships()).values()) {
                if (nodeToReplace.equals(relationshipTemplate.getTarget())) {
                    relationshipsToRemove.add(relationshipTemplate.getName());
                    if (!relationshipDeleted.containsKey(relationshipTemplate.getName())) {
                        Map<String, RelationshipTemplate> relationships = Maps.newHashMap();
                        // we save the RelationshipTemplate, so we will preserve the current properties of this relationship
                        relationshipDeleted.put(nodeTemplate.getName(), relationships);
                    }
                    relationshipDeleted.get(nodeTemplate.getName()).put(relationshipTemplate.getName(), relationshipTemplate);
                    // we should remove this relationship to maintain a great a workflow
                    workflowBuilderService.removeRelationship(topology, csar, nodeTemplate.getName(), relationshipTemplate.getName(), relationshipTemplate);
                }
            }
        }
    }
    return relationshipDeleted;
}
Also used : NodeTemplate(org.alien4cloud.tosca.model.templates.NodeTemplate) RelationshipTemplate(org.alien4cloud.tosca.model.templates.RelationshipTemplate) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) Maps.newLinkedHashMap(com.google.common.collect.Maps.newLinkedHashMap)

Example 12 with RelationshipTemplate

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

the class DuplicateNodeProcessor method copyAndCleanRelationships.

/**
 * Discard all relationship targeting an "external" node. External here in terms of the hostedOn hierarchy
 * Copy the valid ones
 *
 * @param nodeName
 * @param validTargets A map of oldNodeName -> duplicatedNodeName, we should keep relationships targeting one of these nodes.
 */
private void copyAndCleanRelationships(String nodeName, Map<String, String> validTargets, Map<String, NodeTemplate> nodeTemplates, TopologyContext topologyContext) {
    NodeTemplate nodeTemplate = nodeTemplates.get(nodeName);
    if (MapUtils.isNotEmpty(nodeTemplate.getRelationships())) {
        Map<String, RelationshipTemplate> relationships = nodeTemplate.getRelationships();
        Set<String> keys = Sets.newHashSet(relationships.keySet());
        for (String key : keys) {
            RelationshipTemplate rel = relationships.remove(key);
            // If so, then rename it, its target and keep it
            if (validTargets.containsKey(rel.getTarget())) {
                rel.setName(copyName(rel.getName(), relationships.keySet()));
                rel.setTarget(validTargets.get(rel.getTarget()));
                relationships.put(rel.getName(), rel);
                workflowBuilderService.addRelationship(topologyContext, nodeName, rel.getName());
            }
        }
        if (relationships.isEmpty()) {
            nodeTemplate.setRelationships(null);
        }
    }
}
Also used : NodeTemplate(org.alien4cloud.tosca.model.templates.NodeTemplate) RelationshipTemplate(org.alien4cloud.tosca.model.templates.RelationshipTemplate)

Example 13 with RelationshipTemplate

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

the class AddRelationshipProcessor method processNodeOperation.

@Override
protected void processNodeOperation(Csar csar, Topology topology, AddRelationshipOperation operation, NodeTemplate sourceNode) {
    if (operation.getRelationshipName() == null || operation.getRelationshipName().isEmpty()) {
        throw new InvalidNameException("relationshipName", operation.getRelationshipName(), "Not null or empty");
    }
    if (AlienUtils.safe(sourceNode.getRelationships()).containsKey(operation.getRelationshipName())) {
        throw new AlreadyExistException("Relationship " + operation.getRelationshipName() + " already exist on node " + operation.getNodeName());
    }
    if (sourceNode.getRequirements() == null || sourceNode.getRequirements().get(operation.getRequirementName()) == null) {
        throw new NotFoundException("Unable to find requirement with name <" + operation.getRequirementName() + "> on the source node" + operation.getNodeName());
    }
    Map<String, NodeTemplate> nodeTemplates = TopologyUtils.getNodeTemplates(topology);
    // ensure that the target node exists
    TopologyUtils.getNodeTemplate(topology.getId(), operation.getTarget(), nodeTemplates);
    // We don't use the tosca context as the relationship type may not be in dependencies yet (that's why we use the load type below).
    RelationshipType indexedRelationshipType = toscaTypeSearchService.find(RelationshipType.class, operation.getRelationshipType(), operation.getRelationshipVersion());
    if (indexedRelationshipType == null) {
        throw new NotFoundException(RelationshipType.class.getName(), operation.getRelationshipType() + ":" + operation.getRelationshipVersion(), "Unable to find relationship type to create template in topology.");
    }
    boolean upperBoundReachedSource = topologyRequirementBoundsValidationServices.isRequirementUpperBoundReachedForSource(sourceNode, operation.getRequirementName(), topology.getDependencies());
    if (upperBoundReachedSource) {
        // throw exception here
        throw new RequirementBoundException(operation.getNodeName(), operation.getRequirementName());
    }
    boolean upperBoundReachedTarget = topologyCapabilityBoundsValidationServices.isCapabilityUpperBoundReachedForTarget(operation.getTarget(), nodeTemplates, operation.getTargetedCapabilityName(), topology.getDependencies());
    // return with a rest response error
    if (upperBoundReachedTarget) {
        throw new CapabilityBoundException(operation.getTarget(), operation.getTargetedCapabilityName());
    }
    topologyService.loadType(topology, indexedRelationshipType);
    NodeTemplate newSourceNode = topology.getNodeTemplates().get(sourceNode.getName());
    if (sourceNode != newSourceNode) {
        // topology has been reloaded
        sourceNode = newSourceNode;
    }
    Map<String, RelationshipTemplate> relationships = sourceNode.getRelationships();
    if (relationships == null) {
        relationships = Maps.newHashMap();
        sourceNode.setRelationships(relationships);
    }
    RelationshipTemplate relationshipTemplate = new RelationshipTemplate();
    relationshipTemplate.setName(operation.getRelationshipName());
    relationshipTemplate.setTarget(operation.getTarget());
    relationshipTemplate.setTargetedCapabilityName(operation.getTargetedCapabilityName());
    relationshipTemplate.setRequirementName(operation.getRequirementName());
    relationshipTemplate.setRequirementType(sourceNode.getRequirements().get(operation.getRequirementName()).getType());
    relationshipTemplate.setType(indexedRelationshipType.getElementId());
    relationshipTemplate.setArtifacts(newLinkedHashMap(indexedRelationshipType.getArtifacts()));
    relationshipTemplate.setAttributes(newLinkedHashMap(indexedRelationshipType.getAttributes()));
    Map<String, AbstractPropertyValue> properties = new LinkedHashMap<String, AbstractPropertyValue>();
    TemplateBuilder.fillProperties(properties, indexedRelationshipType.getProperties(), null);
    relationshipTemplate.setProperties(properties);
    relationships.put(operation.getRelationshipName(), relationshipTemplate);
    TopologyContext topologyContext = workflowBuilderService.buildTopologyContext(topology, csar);
    workflowBuilderService.addRelationship(topologyContext, operation.getNodeName(), operation.getRelationshipName());
    log.debug("Added relationship to the topology [" + topology.getId() + "], node name [" + operation.getNodeName() + "], relationship name [" + operation.getRelationshipName() + "]");
}
Also used : CapabilityBoundException(org.alien4cloud.tosca.editor.exception.CapabilityBoundException) RelationshipType(org.alien4cloud.tosca.model.types.RelationshipType) NotFoundException(alien4cloud.exception.NotFoundException) LinkedHashMap(java.util.LinkedHashMap) NodeTemplate(org.alien4cloud.tosca.model.templates.NodeTemplate) RelationshipTemplate(org.alien4cloud.tosca.model.templates.RelationshipTemplate) InvalidNameException(alien4cloud.exception.InvalidNameException) RequirementBoundException(org.alien4cloud.tosca.editor.exception.RequirementBoundException) AlreadyExistException(alien4cloud.exception.AlreadyExistException) TopologyContext(alien4cloud.paas.wf.TopologyContext) AbstractPropertyValue(org.alien4cloud.tosca.model.definitions.AbstractPropertyValue)

Example 14 with RelationshipTemplate

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

the class NodeFilterValidationService method validateFiltersForNode.

private void validateFiltersForNode(NodeType sourceNodeType, Map<String, RelationshipTemplate> relationshipsMap, Topology topology, Map<String, NodeType> nodeTypes, Map<String, CapabilityType> capabilityTypes, NodeFiltersTask task, boolean skipInputs) {
    Map<String, RequirementDefinition> requirementDefinitionMap = getRequirementsAsMap(sourceNodeType);
    for (Map.Entry<String, RelationshipTemplate> relationshipEntry : relationshipsMap.entrySet()) {
        RequirementDefinition requirementDefinition = requirementDefinitionMap.get(relationshipEntry.getValue().getRequirementName());
        NodeFilter nodeFilter = requirementDefinition.getNodeFilter();
        if (nodeFilter != null) {
            NodeTemplate targetNode = topology.getNodeTemplates().get(relationshipEntry.getValue().getTarget());
            NodeType targetType = nodeTypes.get(relationshipEntry.getValue().getTarget());
            NodeFilterToSatisfy nodeFilterToSatisfy = new NodeFilterToSatisfy();
            nodeFilterToSatisfy.setRelationshipName(relationshipEntry.getKey());
            nodeFilterToSatisfy.setTargetName(targetNode.getName());
            nodeFilterToSatisfy.setMissingCapabilities(Lists.<String>newArrayList());
            nodeFilterToSatisfy.setViolations(Lists.<Violations>newArrayList());
            validateNodeFilter(nodeFilter, targetNode, targetType, capabilityTypes, nodeFilterToSatisfy, skipInputs);
            if (!nodeFilterToSatisfy.getViolations().isEmpty() || !nodeFilterToSatisfy.getMissingCapabilities().isEmpty()) {
                task.getNodeFiltersToSatisfy().add(nodeFilterToSatisfy);
            }
        }
    }
}
Also used : RelationshipTemplate(org.alien4cloud.tosca.model.templates.RelationshipTemplate) NodeTemplate(org.alien4cloud.tosca.model.templates.NodeTemplate) NodeType(org.alien4cloud.tosca.model.types.NodeType) RequirementDefinition(org.alien4cloud.tosca.model.definitions.RequirementDefinition) Map(java.util.Map) NodeFilterToSatisfy(alien4cloud.topology.task.NodeFilterToSatisfy) NodeFilter(org.alien4cloud.tosca.model.definitions.NodeFilter)

Example 15 with RelationshipTemplate

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

the class TopologyPropertiesValidationService method validateNodeTemplate.

public void validateNodeTemplate(List<PropertiesTask> toReturnTaskList, NodeType relatedIndexedNodeType, NodeTemplate nodeTemplate, String nodeTempalteName, boolean skipInputProperties) {
    // Define a task regarding properties
    PropertiesTask task = new PropertiesTask();
    task.setNodeTemplateName(nodeTempalteName);
    task.setComponent(relatedIndexedNodeType);
    task.setCode(TaskCode.PROPERTIES);
    task.setProperties(Maps.newHashMap());
    // Check the properties of node template
    if (MapUtils.isNotEmpty(nodeTemplate.getProperties())) {
        addRequiredPropertyIdToTaskProperties(null, nodeTemplate.getProperties(), relatedIndexedNodeType.getProperties(), task, skipInputProperties);
    }
    // Check relationships PD
    for (Map.Entry<String, RelationshipTemplate> relationshipEntry : safe(nodeTemplate.getRelationships()).entrySet()) {
        RelationshipTemplate relationship = relationshipEntry.getValue();
        if (relationship.getProperties() == null || relationship.getProperties().isEmpty()) {
            continue;
        }
        addRequiredPropertyIdToTaskProperties("relationships[" + relationshipEntry.getKey() + "]", relationship.getProperties(), safe(ToscaContext.getOrFail(RelationshipType.class, relationshipEntry.getValue().getType()).getProperties()), task, skipInputProperties);
    }
    for (Map.Entry<String, Capability> capabilityEntry : safe(nodeTemplate.getCapabilities()).entrySet()) {
        Capability capability = capabilityEntry.getValue();
        if (capability.getProperties() == null || capability.getProperties().isEmpty()) {
            continue;
        }
        addRequiredPropertyIdToTaskProperties("capabilities[" + capabilityEntry.getKey() + "]", capability.getProperties(), safe(ToscaContext.getOrFail(CapabilityType.class, capabilityEntry.getValue().getType()).getProperties()), task, skipInputProperties);
        if (capability.getType().equals(NormativeCapabilityTypes.SCALABLE)) {
            Map<String, AbstractPropertyValue> scalableProperties = capability.getProperties();
            verifyScalableProperties(scalableProperties, toReturnTaskList, nodeTempalteName, skipInputProperties);
        }
    }
    if (MapUtils.isNotEmpty(task.getProperties())) {
        toReturnTaskList.add(task);
    }
}
Also used : CapabilityType(org.alien4cloud.tosca.model.types.CapabilityType) RelationshipTemplate(org.alien4cloud.tosca.model.templates.RelationshipTemplate) PropertiesTask(alien4cloud.topology.task.PropertiesTask) Capability(org.alien4cloud.tosca.model.templates.Capability) RelationshipType(org.alien4cloud.tosca.model.types.RelationshipType) Map(java.util.Map) AbstractPropertyValue(org.alien4cloud.tosca.model.definitions.AbstractPropertyValue)

Aggregations

RelationshipTemplate (org.alien4cloud.tosca.model.templates.RelationshipTemplate)47 NodeTemplate (org.alien4cloud.tosca.model.templates.NodeTemplate)37 Map (java.util.Map)12 Capability (org.alien4cloud.tosca.model.templates.Capability)12 RelationshipType (org.alien4cloud.tosca.model.types.RelationshipType)11 NodeType (org.alien4cloud.tosca.model.types.NodeType)9 AbstractPropertyValue (org.alien4cloud.tosca.model.definitions.AbstractPropertyValue)7 NotFoundException (alien4cloud.exception.NotFoundException)6 PaaSRelationshipTemplate (alien4cloud.paas.model.PaaSRelationshipTemplate)4 LinkedHashMap (java.util.LinkedHashMap)4 Topology (org.alien4cloud.tosca.model.templates.Topology)4 AlreadyExistException (alien4cloud.exception.AlreadyExistException)3 ParsingError (alien4cloud.tosca.parser.ParsingError)3 HashMap (java.util.HashMap)3 List (java.util.List)3 Interface (org.alien4cloud.tosca.model.definitions.Interface)3 RequirementDefinition (org.alien4cloud.tosca.model.definitions.RequirementDefinition)3 Requirement (org.alien4cloud.tosca.model.templates.Requirement)3 CapabilityType (org.alien4cloud.tosca.model.types.CapabilityType)3 Workflow (org.alien4cloud.tosca.model.workflow.Workflow)3