Search in sources :

Example 6 with SubstitutionTarget

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

the class UpdateCapabilitySubstitutionTypeProcessor method process.

@Override
public void process(Csar csar, Topology topology, UpdateCapabilitySubstitutionTypeOperation operation) {
    if (topology.getSubstitutionMapping() == null || topology.getSubstitutionMapping().getSubstitutionType() == null) {
        throw new NotFoundException("No substitution type has been found");
    }
    Map<String, SubstitutionTarget> substitutionCapabilities = topology.getSubstitutionMapping().getCapabilities();
    if (substitutionCapabilities == null) {
        throw new NotFoundException("No substitution capabilities has been found");
    }
    SubstitutionTarget target = substitutionCapabilities.remove(operation.getSubstitutionCapabilityId());
    if (target == null) {
        throw new NotFoundException("No substitution capability has been found for key " + operation.getSubstitutionCapabilityId());
    }
    if (substitutionCapabilities.containsKey(operation.getNewCapabilityId())) {
        throw new AlreadyExistException(String.format("Can not rename from <%s> to <%s> since capability <%s> already exists", operation.getSubstitutionCapabilityId(), operation.getNewCapabilityId(), operation.getNewCapabilityId()));
    }
    substitutionCapabilities.put(operation.getNewCapabilityId(), target);
}
Also used : NotFoundException(alien4cloud.exception.NotFoundException) SubstitutionTarget(org.alien4cloud.tosca.model.templates.SubstitutionTarget) AlreadyExistException(alien4cloud.exception.AlreadyExistException)

Example 7 with SubstitutionTarget

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

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

the class ManagedServiceResourceEventService method updateRunningService.

private void updateRunningService(DeploymentTopology topology, Deployment deployment, ServiceResource serviceResource, String serviceState, Map<String, Map<String, InstanceInformation>> instanceInformation) {
    // update the state
    serviceResource.setState(serviceState);
    // update deploymentId, in case it is not yet (when creating the service from an already started deployment)
    serviceResource.setDeploymentId(deployment.getId());
    // ensure the service is available on all of the deployment locations
    updateLocations(serviceResource, deployment.getLocationIds());
    // Map input properties from the topology as properties of the service instance
    if (serviceResource.getNodeInstance().getNodeTemplate().getProperties() == null) {
        serviceResource.getNodeInstance().getNodeTemplate().setProperties(Maps.newHashMap());
    }
    serviceResource.getNodeInstance().getNodeTemplate().getProperties().putAll(safe(topology.getAllInputProperties()));
    // Map attributes from the instances to the actual service resource node.
    for (Entry<String, Set<String>> nodeOutputAttrEntry : safe(topology.getOutputAttributes()).entrySet()) {
        Map<String, InstanceInformation> instances = instanceInformation.get(nodeOutputAttrEntry.getKey());
        if (instances == null) {
            log.error("Failed to map attributes from node [ {} ] for service <id: {}, name: {}>. The node cannot be found in deployed topology [ {} ].", nodeOutputAttrEntry.getKey(), serviceResource.getId(), serviceResource.getName(), topology.getId());
        } else if (instances.size() > 1) {
            log.error("Services substitution does not yet supports the exposure of multiple instances");
        } else {
            InstanceInformation instance = instances.values().iterator().next();
            // let's map attribute
            for (String mappedAttribute : nodeOutputAttrEntry.getValue()) {
                serviceResource.getNodeInstance().setAttribute(mappedAttribute, instance.getAttributes().get(mappedAttribute));
            }
        }
    }
    // Map output properties as attributes of the service instance
    for (Entry<String, Set<String>> nodeOutputPropEntry : safe(topology.getOutputProperties()).entrySet()) {
        NodeTemplate nodeTemplate = topology.getNodeTemplates().get(nodeOutputPropEntry.getKey());
        for (String prop : nodeOutputPropEntry.getValue()) {
            serviceResource.getNodeInstance().setAttribute(prop, PropertyUtil.serializePropertyValue(nodeTemplate.getProperties().get(prop)));
        }
    }
    // Map capabilities output properties as attributes of the service instance (that are exposed as node properties)
    for (Entry<String, Map<String, Set<String>>> nodeOutputCapaPropEntry : safe(topology.getOutputCapabilityProperties()).entrySet()) {
        NodeTemplate nodeTemplate = topology.getNodeTemplates().get(nodeOutputCapaPropEntry.getKey());
        for (Entry<String, Set<String>> outputCapaPropEntry : nodeOutputCapaPropEntry.getValue().entrySet()) {
            Capability capability = nodeTemplate.getCapabilities().get(outputCapaPropEntry.getKey());
            for (String prop : outputCapaPropEntry.getValue()) {
                serviceResource.getNodeInstance().setAttribute(prop, PropertyUtil.serializePropertyValue(capability.getProperties().get(prop)));
            }
        }
    }
    serviceResource.getNodeInstance().getNodeTemplate().setCapabilities(Maps.newLinkedHashMap());
    // Map capabilities exposed as is for the service node.
    for (Entry<String, SubstitutionTarget> capabilityMapping : safe(topology.getSubstitutionMapping().getCapabilities()).entrySet()) {
        Capability deployedCapability = topology.getNodeTemplates().get(capabilityMapping.getValue().getNodeTemplateName()).getCapabilities().get(capabilityMapping.getValue().getTargetId());
        serviceResource.getNodeInstance().getNodeTemplate().getCapabilities().put(capabilityMapping.getKey(), deployedCapability);
        // TODO improve while capabilities attributes will be really supported
        // Workaround to support capabilities attributes is to use node attributes with keys in format capabilities.capaName.attributeName
        mapCapabilityRequirementAttributes(serviceResource, instanceInformation, capabilityMapping.getValue().getNodeTemplateName(), "capabilities", capabilityMapping.getValue().getTargetId());
    }
    serviceResource.getNodeInstance().getNodeTemplate().setRequirements(Maps.newLinkedHashMap());
    // Map requirements exposed as is for the service node.
    for (Entry<String, SubstitutionTarget> requirementMapping : safe(topology.getSubstitutionMapping().getRequirements()).entrySet()) {
        serviceResource.getNodeInstance().getNodeTemplate().getRequirements().put(requirementMapping.getKey(), topology.getNodeTemplates().get(requirementMapping.getValue().getNodeTemplateName()).getRequirements().get(requirementMapping.getValue().getTargetId()));
        // TODO improve while requirements attributes will be really supported
        // Workaround to support requirements attributes is to use node attributes with keys in format capabilities.capaName.attributeName
        mapCapabilityRequirementAttributes(serviceResource, instanceInformation, requirementMapping.getValue().getNodeTemplateName(), "requirements", requirementMapping.getValue().getTargetId());
    }
    serviceResourceService.save(serviceResource);
    // trigger a ManagedServiceUpdateEvent
    publisher.publishEvent(new ManagedServiceUpdatedEvent(this, serviceResource, topology));
}
Also used : Set(java.util.Set) NodeTemplate(org.alien4cloud.tosca.model.templates.NodeTemplate) Capability(org.alien4cloud.tosca.model.templates.Capability) ManagedServiceUpdatedEvent(org.alien4cloud.alm.events.ManagedServiceUpdatedEvent) InstanceInformation(alien4cloud.paas.model.InstanceInformation) SubstitutionTarget(org.alien4cloud.tosca.model.templates.SubstitutionTarget) Map(java.util.Map)

Example 9 with SubstitutionTarget

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

the class TopologyService method initializeTypeLoader.

private ToscaTypeLoader initializeTypeLoader(Topology topology, boolean failOnTypeNotFound) {
    // FIXME we should use ToscaContext here, and why not allowing the caller to pass ona Context?
    ToscaTypeLoader loader = new ToscaTypeLoader(csarDependencyLoader);
    Map<String, NodeType> nodeTypes = topologyServiceCore.getIndexedNodeTypesFromTopology(topology, false, false, failOnTypeNotFound);
    Map<String, RelationshipType> relationshipTypes = topologyServiceCore.getIndexedRelationshipTypesFromTopology(topology, failOnTypeNotFound);
    Map<String, PolicyType> policyTypes = topologyServiceCore.getPolicyTypesFromTopology(topology, failOnTypeNotFound);
    for (NodeTemplate nodeTemplate : safe(topology.getNodeTemplates()).values()) {
        NodeType nodeType = nodeTypes.get(nodeTemplate.getType());
        // just load found types: the type might be null when failOnTypeNotFound is set to false.
        if (nodeType != null) {
            loader.loadType(nodeTemplate.getType(), csarDependencyLoader.buildDependencyBean(nodeType.getArchiveName(), nodeType.getArchiveVersion()));
        }
        for (RelationshipTemplate relationshipTemplate : safe(nodeTemplate.getRelationships()).values()) {
            RelationshipType relationshipType = relationshipTypes.get(relationshipTemplate.getType());
            // just load found types: the type might be null when failOnTypeNotFound is set to false.
            if (relationshipType != null) {
                loader.loadType(relationshipTemplate.getType(), csarDependencyLoader.buildDependencyBean(relationshipType.getArchiveName(), relationshipType.getArchiveVersion()));
            }
        }
    }
    for (PolicyTemplate policyTemplate : safe(topology.getPolicies()).values()) {
        PolicyType policyType = policyTypes.get(policyTemplate.getType());
        if (policyType != null) {
            loader.loadType(policyTemplate.getType(), csarDependencyLoader.buildDependencyBean(policyType.getArchiveName(), policyType.getArchiveVersion()));
        }
    }
    if (topology.getSubstitutionMapping() != null && topology.getSubstitutionMapping().getSubstitutionType() != null) {
        NodeType substitutionType = nodeTypes.get(topology.getSubstitutionMapping().getSubstitutionType());
        loader.loadType(substitutionType.getElementId(), csarDependencyLoader.buildDependencyBean(substitutionType.getArchiveName(), substitutionType.getArchiveVersion()));
        for (SubstitutionTarget substitutionTarget : safe(topology.getSubstitutionMapping().getCapabilities()).values()) {
            initializeSubstitutionTarget(loader, relationshipTypes, substitutionTarget);
        }
        for (SubstitutionTarget substitutionTarget : safe(topology.getSubstitutionMapping().getRequirements()).values()) {
            initializeSubstitutionTarget(loader, relationshipTypes, substitutionTarget);
        }
    }
    return loader;
}
Also used : PolicyType(org.alien4cloud.tosca.model.types.PolicyType) NodeTemplate(org.alien4cloud.tosca.model.templates.NodeTemplate) RelationshipTemplate(org.alien4cloud.tosca.model.templates.RelationshipTemplate) ToscaTypeLoader(alien4cloud.tosca.container.ToscaTypeLoader) NodeType(org.alien4cloud.tosca.model.types.NodeType) RelationshipType(org.alien4cloud.tosca.model.types.RelationshipType) SubstitutionTarget(org.alien4cloud.tosca.model.templates.SubstitutionTarget) PolicyTemplate(org.alien4cloud.tosca.model.templates.PolicyTemplate)

Example 10 with SubstitutionTarget

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

Aggregations

SubstitutionTarget (org.alien4cloud.tosca.model.templates.SubstitutionTarget)16 NotFoundException (alien4cloud.exception.NotFoundException)8 NodeTemplate (org.alien4cloud.tosca.model.templates.NodeTemplate)8 AlreadyExistException (alien4cloud.exception.AlreadyExistException)4 Map (java.util.Map)4 RelationshipTemplate (org.alien4cloud.tosca.model.templates.RelationshipTemplate)3 NodeType (org.alien4cloud.tosca.model.types.NodeType)3 Capability (org.alien4cloud.tosca.model.templates.Capability)2 RelationshipType (org.alien4cloud.tosca.model.types.RelationshipType)2 InstanceInformation (alien4cloud.paas.model.InstanceInformation)1 ToscaTypeLoader (alien4cloud.tosca.container.ToscaTypeLoader)1 ParsingError (alien4cloud.tosca.parser.ParsingError)1 Set (java.util.Set)1 ManagedServiceUpdatedEvent (org.alien4cloud.alm.events.ManagedServiceUpdatedEvent)1 AbstractPropertyValue (org.alien4cloud.tosca.model.definitions.AbstractPropertyValue)1 CapabilityDefinition (org.alien4cloud.tosca.model.definitions.CapabilityDefinition)1 FunctionPropertyValue (org.alien4cloud.tosca.model.definitions.FunctionPropertyValue)1 RequirementDefinition (org.alien4cloud.tosca.model.definitions.RequirementDefinition)1 PolicyTemplate (org.alien4cloud.tosca.model.templates.PolicyTemplate)1 PolicyType (org.alien4cloud.tosca.model.types.PolicyType)1