Search in sources :

Example 26 with Capability

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

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

the class InputPropertiesStepDefinitions method i_define_the_capability_property_of_the_node_of_typeId_as_input_property.

@Given("^I define the capability \"(.*?)\" property \"(.*?)\" of the node \"(.*?)\" as input property$")
public void i_define_the_capability_property_of_the_node_of_typeId_as_input_property(String capabilityName, String propertyName, String nodeName) throws Throwable {
    String url = String.format("/rest/v1/topologies/%s", Context.getInstance().getTopologyId());
    String response = Context.getRestClientInstance().get(url);
    TopologyDTO topologyDTO = JsonUtil.read(response, TopologyDTO.class, Context.getJsonMapper()).getData();
    NodeTemplate template = MapUtils.getObject(topologyDTO.getTopology().getNodeTemplates(), nodeName);
    Capability capability = template.getCapabilities().get(capabilityName);
    CapabilityType capabilityType = topologyDTO.getCapabilityTypes().get(capability.getType());
    PropertyDefinition propertyDefinition = capabilityType.getProperties().get(propertyName);
    String fullUrl = String.format("/rest/v1/topologies/%s/inputs/%s", Context.getInstance().getTopologyId(), propertyName);
    String json = JsonUtil.toString(propertyDefinition);
    Context.getInstance().registerRestResponse(Context.getRestClientInstance().postJSon(fullUrl, json));
}
Also used : CapabilityType(org.alien4cloud.tosca.model.types.CapabilityType) NodeTemplate(org.alien4cloud.tosca.model.templates.NodeTemplate) TopologyDTO(alien4cloud.topology.TopologyDTO) Capability(org.alien4cloud.tosca.model.templates.Capability) PropertyDefinition(org.alien4cloud.tosca.model.definitions.PropertyDefinition) Given(cucumber.api.java.en.Given)

Example 28 with Capability

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

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

the class NodeInstanceService method updateCapabilities.

private void updateCapabilities(NodeTemplate nodeTemplate, Map<String, Capability> nodeCapabilities) throws ConstraintValueDoNotMatchPropertyTypeException, ConstraintViolationException {
    for (Map.Entry<String, Capability> entry : nodeCapabilities.entrySet()) {
        if (entry != null) {
            Capability patchCapability = PatchUtil.realValue(entry.getValue());
            if (patchCapability == null) {
                throw new IllegalArgumentException("It is not allowed to set null to a capability.");
            } else {
                Capability targetCapability = nodeTemplate.getCapabilities().get(entry.getKey());
                if (targetCapability == null) {
                    throw new NotFoundException("Capability <" + entry.getKey() + "> doesn't exists on the node.");
                }
                CapabilityType capabilityType = ToscaContext.get(CapabilityType.class, targetCapability.getType());
                updateCapabilitiesProperties(capabilityType, targetCapability, patchCapability);
            }
        }
    }
}
Also used : CapabilityType(org.alien4cloud.tosca.model.types.CapabilityType) Capability(org.alien4cloud.tosca.model.templates.Capability) NotFoundException(alien4cloud.exception.NotFoundException) Map(java.util.Map)

Example 30 with Capability

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

the class NodeTemplateUtilsTest method getCapabilityByTypeTest.

@Test
public void getCapabilityByTypeTest() {
    NodeTemplate nodeTemplate = new NodeTemplate();
    Capability nodeCapability = new Capability("org.alien4cloud.capabilities.SampleCapability", null);
    nodeTemplate.setCapabilities(Maps.newHashMap("test", nodeCapability));
    // if the capability type exactly equals then no tosca context and request is required
    Capability capability = getCapabilityByType(nodeTemplate, "org.alien4cloud.capabilities.SampleCapability");
    assertSame(nodeCapability, capability);
    // if the capability derives from parent type then a TOSCA context and query is required to fetch the type.
    CapabilityType capabilityType = new CapabilityType();
    capabilityType.setElementId("org.alien4cloud.capabilities.SampleCapability");
    capabilityType.setDerivedFrom(Lists.newArrayList("org.alien4cloud.capabilities.TestCapability"));
    Mockito.reset(csarRepositorySearchService);
    Mockito.when(csarRepositorySearchService.getElementInDependencies(Mockito.eq(CapabilityType.class), Mockito.eq("org.alien4cloud.capabilities.SampleCapability"), Mockito.any(Set.class))).thenReturn(capabilityType);
    capability = toscaContextualAspect.execInToscaContext(() -> getCapabilityByType(nodeTemplate, "org.alien4cloud.capabilities.TestCapability"), false, Sets.newHashSet(new CSARDependency("org.alien4cloud.testArchive", "1.0.0-SNAPSHOT")));
    assertSame(nodeCapability, capability);
}
Also used : CapabilityType(org.alien4cloud.tosca.model.types.CapabilityType) NodeTemplate(org.alien4cloud.tosca.model.templates.NodeTemplate) Set(java.util.Set) Capability(org.alien4cloud.tosca.model.templates.Capability) CSARDependency(org.alien4cloud.tosca.model.CSARDependency) Test(org.junit.Test)

Aggregations

Capability (org.alien4cloud.tosca.model.templates.Capability)50 NodeTemplate (org.alien4cloud.tosca.model.templates.NodeTemplate)27 CapabilityType (org.alien4cloud.tosca.model.types.CapabilityType)26 AbstractPropertyValue (org.alien4cloud.tosca.model.definitions.AbstractPropertyValue)14 RelationshipTemplate (org.alien4cloud.tosca.model.templates.RelationshipTemplate)12 Map (java.util.Map)10 PropertyDefinition (org.alien4cloud.tosca.model.definitions.PropertyDefinition)10 NotFoundException (alien4cloud.exception.NotFoundException)9 NodeType (org.alien4cloud.tosca.model.types.NodeType)9 Test (org.junit.Test)9 Set (java.util.Set)8 CSARDependency (org.alien4cloud.tosca.model.CSARDependency)5 CapabilityDefinition (org.alien4cloud.tosca.model.definitions.CapabilityDefinition)5 RelationshipType (org.alien4cloud.tosca.model.types.RelationshipType)5 HashMap (java.util.HashMap)4 List (java.util.List)4 ScalarPropertyValue (org.alien4cloud.tosca.model.definitions.ScalarPropertyValue)4 Topology (org.alien4cloud.tosca.model.templates.Topology)4 Location (alien4cloud.model.orchestrators.locations.Location)3 PaaSNodeTemplate (alien4cloud.paas.model.PaaSNodeTemplate)3