Search in sources :

Example 26 with RelationshipType

use of org.alien4cloud.tosca.model.types.RelationshipType in project alien4cloud by alien4cloud.

the class TopologyServiceCore method getIndexedRelationshipTypesFromTopology.

/**
 * Get IndexedRelationshipType in a topology
 *
 * @param topology the topology to find all relationship types
 * @param failOnTypeNotFound
 * @return the map containing rel
 */
public Map<String, RelationshipType> getIndexedRelationshipTypesFromTopology(Topology topology, boolean failOnTypeNotFound) {
    Map<String, RelationshipType> relationshipTypes = Maps.newHashMap();
    if (topology.getNodeTemplates() == null) {
        return relationshipTypes;
    }
    for (Map.Entry<String, NodeTemplate> templateEntry : topology.getNodeTemplates().entrySet()) {
        NodeTemplate template = templateEntry.getValue();
        if (template.getRelationships() != null) {
            for (Map.Entry<String, RelationshipTemplate> relationshipEntry : template.getRelationships().entrySet()) {
                RelationshipTemplate relationship = relationshipEntry.getValue();
                if (!relationshipTypes.containsKey(relationship.getType())) {
                    RelationshipType relationshipType = getFromContextIfDefined(RelationshipType.class, relationship.getType(), topology.getDependencies(), failOnTypeNotFound);
                    relationshipTypes.put(relationship.getType(), relationshipType);
                }
            }
        }
    }
    if (topology.getSubstitutionMapping() != null && topology.getSubstitutionMapping().getSubstitutionType() != null) {
        for (SubstitutionTarget substitutionTarget : safe(topology.getSubstitutionMapping().getCapabilities()).values()) {
            addRelationshipTypeFromSubstitutionTarget(topology, relationshipTypes, substitutionTarget, failOnTypeNotFound);
        }
        for (SubstitutionTarget substitutionTarget : safe(topology.getSubstitutionMapping().getRequirements()).values()) {
            addRelationshipTypeFromSubstitutionTarget(topology, relationshipTypes, substitutionTarget, failOnTypeNotFound);
        }
    }
    return relationshipTypes;
}
Also used : NodeTemplate(org.alien4cloud.tosca.model.templates.NodeTemplate) RelationshipTemplate(org.alien4cloud.tosca.model.templates.RelationshipTemplate) RelationshipType(org.alien4cloud.tosca.model.types.RelationshipType) SubstitutionTarget(org.alien4cloud.tosca.model.templates.SubstitutionTarget) Map(java.util.Map)

Example 27 with RelationshipType

use of org.alien4cloud.tosca.model.types.RelationshipType in project alien4cloud by alien4cloud.

the class TopologyModifierSupport method addRelationshipTemplate.

protected RelationshipTemplate addRelationshipTemplate(Csar csar, Topology topology, NodeTemplate sourceNode, String targetNodeName, String relationshipTypeName, String requirementName, String capabilityName) {
    AddRelationshipOperation addRelationshipOperation = new AddRelationshipOperation();
    addRelationshipOperation.setNodeName(sourceNode.getName());
    addRelationshipOperation.setTarget(targetNodeName);
    RelationshipType relationshipType = ToscaContext.get(RelationshipType.class, relationshipTypeName);
    addRelationshipOperation.setRelationshipType(relationshipType.getElementId());
    addRelationshipOperation.setRelationshipVersion(relationshipType.getArchiveVersion());
    addRelationshipOperation.setRequirementName(requirementName);
    addRelationshipOperation.setTargetedCapabilityName(capabilityName);
    String relationShipName = TopologyCompositionService.ensureNodeNameIsUnique(safe(sourceNode.getRelationships()).keySet(), sourceNode.getName() + "_" + targetNodeName, 0);
    addRelationshipOperation.setRelationshipName(relationShipName);
    addRelationshipProcessor.process(csar, topology, addRelationshipOperation);
    return sourceNode.getRelationships().get(relationShipName);
}
Also used : RelationshipType(org.alien4cloud.tosca.model.types.RelationshipType) AddRelationshipOperation(org.alien4cloud.tosca.editor.operations.relationshiptemplate.AddRelationshipOperation)

Example 28 with RelationshipType

use of org.alien4cloud.tosca.model.types.RelationshipType in project alien4cloud by alien4cloud.

the class ServiceResourceService method validateRelationshipTypes.

private void validateRelationshipTypes(ServiceResource serviceResource, final NodeType nodeType) {
    safe(serviceResource.getCapabilitiesRelationshipTypes()).forEach((capabilityName, relationshipTypeId) -> {
        RelationshipType relationshipType = toscaTypeSearchService.findByIdOrFail(RelationshipType.class, relationshipTypeId);
        String[] validTargets = relationshipType.getValidTargets();
        if (ArrayUtils.isNotEmpty(validTargets)) {
            CapabilityDefinition capabilityDefinition = nodeType.getCapabilities().stream().filter(c -> c.getId().equals(capabilityName)).findFirst().get();
            Csar csar = toscaTypeSearchService.getArchive(nodeType.getArchiveName(), nodeType.getArchiveVersion());
            Set<CSARDependency> allDependencies = new HashSet<>(safe(csar.getDependencies()));
            allDependencies.add(new CSARDependency(csar.getName(), csar.getVersion(), csar.getHash()));
            CapabilityType capabilityType = toscaTypeSearchService.getElementInDependencies(CapabilityType.class, capabilityDefinition.getType(), allDependencies);
            Set<String> allAcceptedTypes = new HashSet<>();
            allAcceptedTypes.addAll(capabilityType.getDerivedFrom());
            allAcceptedTypes.add(capabilityType.getElementId());
            boolean isValid = false;
            for (String validTarget : validTargets) {
                if (allAcceptedTypes.contains(validTarget)) {
                    isValid = true;
                    break;
                }
            }
            if (!isValid) {
                throw new IncompatibleHalfRelationshipException("[" + relationshipType.getId() + "] is not compatible with [" + capabilityType.getId() + "]");
            }
        }
    });
    safe(serviceResource.getRequirementsRelationshipTypes()).forEach((k, v) -> {
        toscaTypeSearchService.findByIdOrFail(RelationshipType.class, v);
    });
}
Also used : Csar(org.alien4cloud.tosca.model.Csar) CapabilityType(org.alien4cloud.tosca.model.types.CapabilityType) RelationshipType(org.alien4cloud.tosca.model.types.RelationshipType) CapabilityDefinition(org.alien4cloud.tosca.model.definitions.CapabilityDefinition) IncompatibleHalfRelationshipException(org.alien4cloud.alm.service.exceptions.IncompatibleHalfRelationshipException) CSARDependency(org.alien4cloud.tosca.model.CSARDependency) HashSet(java.util.HashSet)

Example 29 with RelationshipType

use of org.alien4cloud.tosca.model.types.RelationshipType in project alien4cloud by alien4cloud.

the class ReplaceNodeProcessor method updateRelationshipsCapabilitiesRelationships.

private void updateRelationshipsCapabilitiesRelationships(Topology topology, NodeTemplate nodeTemplate) {
    List<RelationshipEntry> targetRelationships = TopologyUtils.getTargetRelationships(nodeTemplate.getName(), topology.getNodeTemplates());
    for (RelationshipEntry targetRelationshipEntry : targetRelationships) {
        RelationshipTemplate targetRelationship = targetRelationshipEntry.getRelationship();
        Capability capability = safe(nodeTemplate.getCapabilities()).get(targetRelationship.getTargetedCapabilityName());
        if (capability == null || isCapabilityNotOfType(capability, targetRelationship.getRequirementType())) {
            Entry<String, Capability> capabilityEntry = NodeTemplateUtils.getCapabilitEntryyByType(nodeTemplate, targetRelationship.getRequirementType());
            targetRelationship.setTargetedCapabilityName(capabilityEntry.getKey());
            // check that the relationship type is still valid with the new capability
            RelationshipType relationshipType = ToscaContext.get(RelationshipType.class, targetRelationship.getType());
            if (!isValidRelationship(relationshipType, capabilityEntry.getValue())) {
                NodeType sourceNodeType = ToscaContext.get(NodeType.class, targetRelationshipEntry.getSource().getType());
                RequirementDefinition requirementDefinition = NodeTypeUtils.getRequirementById(sourceNodeType, targetRelationshipEntry.getRelationship().getRequirementName());
                NodeType targetNodeType = ToscaContext.get(NodeType.class, nodeTemplate.getType());
                CapabilityDefinition capabilityDefinition = NodeTypeUtils.getCapabilityById(targetNodeType, capabilityEntry.getKey());
                RelationshipType validRelationshipType = danglingRequirementService.fetchValidRelationshipType(requirementDefinition, capabilityDefinition);
                targetRelationship.setType(validRelationshipType.getElementId());
                targetRelationship.setType(validRelationshipType.getElementId());
                targetRelationship.setArtifacts(newLinkedHashMap(safe(validRelationshipType.getArtifacts())));
                targetRelationship.setAttributes(newLinkedHashMap(safe(validRelationshipType.getAttributes())));
                Map<String, AbstractPropertyValue> properties = new LinkedHashMap();
                TemplateBuilder.fillProperties(properties, validRelationshipType.getProperties(), null);
                targetRelationship.setProperties(properties);
            }
        }
    }
}
Also used : Capability(org.alien4cloud.tosca.model.templates.Capability) RelationshipType(org.alien4cloud.tosca.model.types.RelationshipType) RequirementDefinition(org.alien4cloud.tosca.model.definitions.RequirementDefinition) LinkedHashMap(java.util.LinkedHashMap) Maps.newLinkedHashMap(com.google.common.collect.Maps.newLinkedHashMap) RelationshipEntry(org.alien4cloud.tosca.utils.TopologyUtils.RelationshipEntry) RelationshipTemplate(org.alien4cloud.tosca.model.templates.RelationshipTemplate) NodeType(org.alien4cloud.tosca.model.types.NodeType) CapabilityDefinition(org.alien4cloud.tosca.model.definitions.CapabilityDefinition) AbstractPropertyValue(org.alien4cloud.tosca.model.definitions.AbstractPropertyValue)

Example 30 with RelationshipType

use of org.alien4cloud.tosca.model.types.RelationshipType in project alien4cloud by alien4cloud.

the class SetRelationshipPropertyAsInputProcessor method processRelationshipOperation.

@Override
protected void processRelationshipOperation(Csar csar, Topology topology, SetRelationshipPropertyAsInputOperation operation, NodeTemplate nodeTemplate, RelationshipTemplate relationshipTemplate) {
    PropertyDefinition inputPropertyDefinition = getOrFail(topology.getInputs(), operation.getInputName(), "Input {} not found in topology", operation.getInputName());
    RelationshipType relationshipType = ToscaContext.get(RelationshipType.class, relationshipTemplate.getType());
    PropertyDefinition relationshipPropertyDefinition = getOrFail(relationshipType.getProperties(), operation.getPropertyName(), "Property {} do not exist for relationship {} of node {}", operation.getPropertyName(), operation.getRelationshipName(), operation.getNodeName());
    // Check that the property definition of the input is indeed compatible with the property definition of the capability.
    inputPropertyDefinition.checkIfCompatibleOrFail(relationshipPropertyDefinition);
    FunctionPropertyValue getInput = new FunctionPropertyValue();
    getInput.setFunction(ToscaFunctionConstants.GET_INPUT);
    getInput.setParameters(Arrays.asList(operation.getInputName()));
    relationshipTemplate.getProperties().put(operation.getPropertyName(), getInput);
    log.debug("Associate the property [ {} ] of relationship template [ {} ] of node [ {} ] to an input of the topology [ {} ].", operation.getPropertyName(), operation.getRelationshipName(), operation.getNodeName(), topology.getId());
}
Also used : RelationshipType(org.alien4cloud.tosca.model.types.RelationshipType) FunctionPropertyValue(org.alien4cloud.tosca.model.definitions.FunctionPropertyValue) PropertyDefinition(org.alien4cloud.tosca.model.definitions.PropertyDefinition)

Aggregations

RelationshipType (org.alien4cloud.tosca.model.types.RelationshipType)41 NodeType (org.alien4cloud.tosca.model.types.NodeType)22 ArchiveRoot (alien4cloud.tosca.model.ArchiveRoot)15 Set (java.util.Set)14 CapabilityType (org.alien4cloud.tosca.model.types.CapabilityType)14 Test (org.junit.Test)14 NodeTemplate (org.alien4cloud.tosca.model.templates.NodeTemplate)9 RelationshipTemplate (org.alien4cloud.tosca.model.templates.RelationshipTemplate)9 Csar (org.alien4cloud.tosca.model.Csar)8 PropertyDefinition (org.alien4cloud.tosca.model.definitions.PropertyDefinition)8 AbstractPropertyValue (org.alien4cloud.tosca.model.definitions.AbstractPropertyValue)5 CapabilityDefinition (org.alien4cloud.tosca.model.definitions.CapabilityDefinition)5 Interface (org.alien4cloud.tosca.model.definitions.Interface)5 RequirementDefinition (org.alien4cloud.tosca.model.definitions.RequirementDefinition)4 NotFoundException (alien4cloud.exception.NotFoundException)3 FunctionPropertyValue (org.alien4cloud.tosca.model.definitions.FunctionPropertyValue)3 PropertyConstraint (org.alien4cloud.tosca.model.definitions.PropertyConstraint)3 Capability (org.alien4cloud.tosca.model.templates.Capability)3 DataType (org.alien4cloud.tosca.model.types.DataType)3 ParsingError (alien4cloud.tosca.parser.ParsingError)2