Search in sources :

Example 1 with NodePropsOverride

use of org.alien4cloud.alm.deployment.configuration.model.DeploymentMatchingConfiguration.NodePropsOverride in project alien4cloud by alien4cloud.

the class SetMatchedNodePropertyModifier method setNodeCapabilityProperty.

private void setNodeCapabilityProperty(FlowExecutionContext context, LocationResourceTemplate locationResourceTemplate, NodeTemplate nodeTemplate, String capabilityName, DeploymentMatchingConfiguration matchingConfiguration) throws ConstraintViolationException, ConstraintValueDoNotMatchPropertyTypeException {
    Capability locationResourceCapability = locationResourceTemplate.getTemplate().getCapabilities().get(capabilityName);
    if (locationResourceCapability == null) {
        throw new NotFoundException("The capability <" + capabilityName + "> cannot be found on node template <" + templateId + "> of type <" + locationResourceTemplate.getTemplate().getType() + ">");
    }
    PropertyDefinition propertyDefinition = ToscaContext.getOrFail(CapabilityType.class, locationResourceCapability.getType()).getProperties().get(propertyName);
    if (propertyDefinition == null) {
        throw new NotFoundException("No property with name <" + propertyName + "> can be found on capability <" + capabilityName + "> of type <" + locationResourceCapability.getType() + ">");
    }
    AbstractPropertyValue locationResourcePropertyValue = locationResourceTemplate.getTemplate().getCapabilities().get(capabilityName).getProperties().get(propertyName);
    ensureNotSet(locationResourcePropertyValue, "by the admin in the Location Resource Template", propertyName, propertyValue);
    AbstractPropertyValue originalNodePropertyValue = safe(nodeTemplate.getCapabilities().get(capabilityName).getProperties()).get(propertyName);
    ensureNotSet(originalNodePropertyValue, "in the portable topology", propertyName, propertyValue);
    // Update the configuration
    NodePropsOverride nodePropsOverride = getTemplatePropsOverride(matchingConfiguration);
    if (propertyValue == null && nodePropsOverride.getCapabilities().get(capabilityName) != null) {
        nodePropsOverride.getCapabilities().get(capabilityName).getProperties().remove(propertyName);
    } else {
        // Set check constraints
        ConstraintPropertyService.checkPropertyConstraint(propertyName, propertyValue, propertyDefinition);
        NodeCapabilitiesPropsOverride nodeCapabilitiesPropsOverride = nodePropsOverride.getCapabilities().computeIfAbsent(capabilityName, k -> new NodeCapabilitiesPropsOverride());
        nodeCapabilitiesPropsOverride.getProperties().put(propertyName, PropertyService.asPropertyValue(propertyValue));
    }
    context.saveConfiguration(matchingConfiguration);
}
Also used : Capability(org.alien4cloud.tosca.model.templates.Capability) NodePropsOverride(org.alien4cloud.alm.deployment.configuration.model.DeploymentMatchingConfiguration.NodePropsOverride) NotFoundException(alien4cloud.exception.NotFoundException) PropertyDefinition(org.alien4cloud.tosca.model.definitions.PropertyDefinition) AbstractPropertyValue(org.alien4cloud.tosca.model.definitions.AbstractPropertyValue) NodeCapabilitiesPropsOverride(org.alien4cloud.alm.deployment.configuration.model.DeploymentMatchingConfiguration.NodeCapabilitiesPropsOverride)

Example 2 with NodePropsOverride

use of org.alien4cloud.alm.deployment.configuration.model.DeploymentMatchingConfiguration.NodePropsOverride in project alien4cloud by alien4cloud.

the class PostMatchingNodeSetupModifier method doMergeNode.

@Override
protected boolean doMergeNode(Topology topology, FlowExecutionContext context, String nodeTemplateId, NodePropsOverride nodePropsOverride) {
    final ConfigChanged configChanged = new ConfigChanged();
    // play the super method first. This will process nodetemplate properties
    configChanged.changed = super.doMergeNode(topology, context, nodeTemplateId, nodePropsOverride);
    // Then process capabilities
    NodeTemplate nodeTemplate = topology.getNodeTemplates().get(nodeTemplateId);
    Iterator<Entry<String, NodeCapabilitiesPropsOverride>> capabilitiesOverrideIter = safe(nodePropsOverride.getCapabilities()).entrySet().iterator();
    while (capabilitiesOverrideIter.hasNext()) {
        Entry<String, NodeCapabilitiesPropsOverride> overrideCapabilityProperties = capabilitiesOverrideIter.next();
        Capability capability = safe(nodeTemplate.getCapabilities()).get(overrideCapabilityProperties.getKey());
        if (capability == null) {
            // Manage clean logic
            configChanged.changed = true;
            capabilitiesOverrideIter.remove();
        } else {
            // Merge logic
            // When a selected node has changed we may need to cleanup properties that where defined but are not anymore on the capability
            CapabilityType capabilityType = ToscaContext.get(CapabilityType.class, capability.getType());
            capability.setProperties(mergeProperties(overrideCapabilityProperties.getValue().getProperties(), capability.getProperties(), capabilityType.getProperties(), s -> {
                configChanged.changed = true;
                context.log().info("The property [" + s + "] previously specified to configure capability [" + overrideCapabilityProperties.getKey() + "] of node [" + nodeTemplateId + "] cannot be set anymore as it is already specified by the matched location resource or in the topology.");
            }));
        }
    }
    return configChanged.changed;
}
Also used : Iterator(java.util.Iterator) NodeType(org.alien4cloud.tosca.model.types.NodeType) AlienUtils.safe(alien4cloud.utils.AlienUtils.safe) Component(org.springframework.stereotype.Component) Slf4j(lombok.extern.slf4j.Slf4j) DeploymentMatchingConfiguration(org.alien4cloud.alm.deployment.configuration.model.DeploymentMatchingConfiguration) FlowExecutionContext(org.alien4cloud.alm.deployment.configuration.flow.FlowExecutionContext) Map(java.util.Map) NodeTemplate(org.alien4cloud.tosca.model.templates.NodeTemplate) CapabilityType(org.alien4cloud.tosca.model.types.CapabilityType) Entry(java.util.Map.Entry) Topology(org.alien4cloud.tosca.model.templates.Topology) NodeCapabilitiesPropsOverride(org.alien4cloud.alm.deployment.configuration.model.DeploymentMatchingConfiguration.NodeCapabilitiesPropsOverride) NodePropsOverride(org.alien4cloud.alm.deployment.configuration.model.DeploymentMatchingConfiguration.NodePropsOverride) Capability(org.alien4cloud.tosca.model.templates.Capability) ToscaContext(alien4cloud.tosca.context.ToscaContext) Entry(java.util.Map.Entry) CapabilityType(org.alien4cloud.tosca.model.types.CapabilityType) NodeTemplate(org.alien4cloud.tosca.model.templates.NodeTemplate) Capability(org.alien4cloud.tosca.model.templates.Capability) NodeCapabilitiesPropsOverride(org.alien4cloud.alm.deployment.configuration.model.DeploymentMatchingConfiguration.NodeCapabilitiesPropsOverride) NodeCapabilitiesPropsOverride(org.alien4cloud.alm.deployment.configuration.model.DeploymentMatchingConfiguration.NodeCapabilitiesPropsOverride) NodePropsOverride(org.alien4cloud.alm.deployment.configuration.model.DeploymentMatchingConfiguration.NodePropsOverride)

Example 3 with NodePropsOverride

use of org.alien4cloud.alm.deployment.configuration.model.DeploymentMatchingConfiguration.NodePropsOverride in project alien4cloud by alien4cloud.

the class AbstractPostMatchingSetupModifier method process.

@Override
public void process(Topology topology, FlowExecutionContext context) {
    Optional<DeploymentMatchingConfiguration> configurationOptional = context.getConfiguration(DeploymentMatchingConfiguration.class, AbstractPostMatchingSetupModifier.class.getSimpleName());
    if (!configurationOptional.isPresent()) {
        // we should not end-up here as location matching should be processed first
        context.log().error(new LocationPolicyTask());
        return;
    }
    DeploymentMatchingConfiguration deploymentMatchingConfiguration = configurationOptional.get();
    Map<String, String> lastUserSubstitutions = getUserMatches(deploymentMatchingConfiguration);
    Map<String, NodePropsOverride> propertiesOverrides = getPropertiesOverrides(deploymentMatchingConfiguration);
    boolean configChanged = false;
    Iterator<Entry<String, NodePropsOverride>> propertiesOverrideIter = safe(propertiesOverrides).entrySet().iterator();
    while (propertiesOverrideIter.hasNext()) {
        Entry<String, NodePropsOverride> nodePropsOverrideEntry = propertiesOverrideIter.next();
        if (lastUserSubstitutions.containsKey(nodePropsOverrideEntry.getKey())) {
            // Merge the user overrides into the node.
            configChanged = mergeNode(topology, context, nodePropsOverrideEntry.getKey(), nodePropsOverrideEntry.getValue());
        } else {
            // This node is no more a matched node, remove from configuration
            configChanged = true;
            context.log().info("Definition of user properties for " + getSubject() + " [" + nodePropsOverrideEntry.getKey() + "] have been removed as the " + getSubject() + " is not available for matching anymore.");
            propertiesOverrideIter.remove();
        }
    }
    // If the configuration has changed then update it.
    if (configChanged) {
        context.saveConfiguration(deploymentMatchingConfiguration);
    }
}
Also used : Entry(java.util.Map.Entry) LocationPolicyTask(alien4cloud.topology.task.LocationPolicyTask) NodePropsOverride(org.alien4cloud.alm.deployment.configuration.model.DeploymentMatchingConfiguration.NodePropsOverride) DeploymentMatchingConfiguration(org.alien4cloud.alm.deployment.configuration.model.DeploymentMatchingConfiguration) NodePropsOverride(org.alien4cloud.alm.deployment.configuration.model.DeploymentMatchingConfiguration.NodePropsOverride)

Example 4 with NodePropsOverride

use of org.alien4cloud.alm.deployment.configuration.model.DeploymentMatchingConfiguration.NodePropsOverride in project alien4cloud by alien4cloud.

the class AbstractSetMatchedPropertyModifier method setProperty.

protected void setProperty(FlowExecutionContext context, V resourceTemplate, U template, DeploymentMatchingConfiguration matchingConfiguration) throws ConstraintViolationException, ConstraintValueDoNotMatchPropertyTypeException {
    PropertyDefinition propertyDefinition = ((T) ToscaContext.getOrFail(AbstractToscaType.class, resourceTemplate.getTemplate().getType())).getProperties().get(propertyName);
    if (propertyDefinition == null) {
        throw new NotFoundException("No property of name [" + propertyName + "] can be found on the " + getSubject() + " template [" + templateId + "] of type [" + resourceTemplate.getTemplate().getType() + "]");
    }
    AbstractPropertyValue locationResourcePropertyValue = resourceTemplate.getTemplate().getProperties().get(propertyName);
    ensureNotSet(locationResourcePropertyValue, "by the admin in the Location Resource Template", propertyName, propertyValue);
    ensureNotSet(template.getProperties().get(propertyName), "in the portable topology", propertyName, propertyValue);
    // Update the configuration
    NodePropsOverride nodePropsOverride = getTemplatePropsOverride(matchingConfiguration);
    // Perform the update of the property
    if (propertyValue == null) {
        nodePropsOverride.getProperties().remove(propertyName);
    } else {
        AbstractPropertyValue abstractPropertyValue = PropertyService.asPropertyValue(propertyValue);
        if (!(abstractPropertyValue instanceof FunctionPropertyValue)) {
            ConstraintPropertyService.checkPropertyConstraint(propertyName, propertyValue, propertyDefinition);
        }
        nodePropsOverride.getProperties().put(propertyName, abstractPropertyValue);
    }
    context.saveConfiguration(matchingConfiguration);
}
Also used : NodePropsOverride(org.alien4cloud.alm.deployment.configuration.model.DeploymentMatchingConfiguration.NodePropsOverride) FunctionPropertyValue(org.alien4cloud.tosca.model.definitions.FunctionPropertyValue) NotFoundException(alien4cloud.exception.NotFoundException) PropertyDefinition(org.alien4cloud.tosca.model.definitions.PropertyDefinition) AbstractPropertyValue(org.alien4cloud.tosca.model.definitions.AbstractPropertyValue)

Example 5 with NodePropsOverride

use of org.alien4cloud.alm.deployment.configuration.model.DeploymentMatchingConfiguration.NodePropsOverride in project alien4cloud by alien4cloud.

the class PostMatchingNodeSetupModifierTest method testPropertiesCleanup.

@Test
public void testPropertiesCleanup() throws ParsingException {
    Mockito.reset(csarRepositorySearchService);
    Mockito.when(csarRepositorySearchService.getArchive("tosca-normative-types", "1.0.0-SNAPSHOT")).thenReturn(Mockito.mock(Csar.class));
    NodeType mockType = Mockito.mock(NodeType.class);
    Mockito.when(mockType.isAbstract()).thenReturn(true);
    Mockito.when(csarRepositorySearchService.getElementInDependencies(Mockito.eq(NodeType.class), Mockito.eq("tosca.nodes.Root"), Mockito.any(Set.class))).thenReturn(mockType);
    CapabilityType mockCapaType = Mockito.mock(CapabilityType.class);
    Mockito.when(mockCapaType.getElementId()).thenReturn("tosca.capabilities.Root");
    Mockito.when(csarRepositorySearchService.getElementInDependencies(Mockito.eq(CapabilityType.class), Mockito.eq("tosca.capabilities.Root"), Mockito.any(Set.class))).thenReturn(mockCapaType);
    ParsingResult<ArchiveRoot> parsingResult = parser.parseFile(Paths.get("../alien4cloud-test-common/src/test/resources/data/csars/matching-change-cleanup/tosca.yml"));
    for (Entry<String, CapabilityType> capabilityTypeEntry : parsingResult.getResult().getCapabilityTypes().entrySet()) {
        Mockito.when(csarRepositorySearchService.getElementInDependencies(Mockito.eq(CapabilityType.class), Mockito.eq(capabilityTypeEntry.getKey()), Mockito.any(Set.class))).thenReturn(capabilityTypeEntry.getValue());
    }
    for (Entry<String, NodeType> nodeTypeEntry : parsingResult.getResult().getNodeTypes().entrySet()) {
        Mockito.when(csarRepositorySearchService.getElementInDependencies(Mockito.eq(NodeType.class), Mockito.eq(nodeTypeEntry.getKey()), Mockito.any(Set.class))).thenReturn(nodeTypeEntry.getValue());
    }
    try {
        ToscaContext.init(Sets.newHashSet());
        PostMatchingNodeSetupModifier postMatchingNodeSetupModifier = new PostMatchingNodeSetupModifier();
        Topology topology = new Topology();
        topology.setNodeTemplates(Maps.newHashMap());
        topology.getNodeTemplates().put("my_node", TemplateBuilder.buildNodeTemplate(parsingResult.getResult().getNodeTypes().get("org.alien4cloud.test.matching.nodes.LocationCustomImplOne")));
        // Configure the deployment config
        DeploymentMatchingConfiguration matchingConfiguration = new DeploymentMatchingConfiguration();
        matchingConfiguration.setMatchedLocationResources(Maps.newHashMap());
        matchingConfiguration.getMatchedLocationResources().put("my_node", "a_location_resource");
        NodePropsOverride nodePropsOverride = new NodePropsOverride();
        nodePropsOverride.getProperties().put("common_property", new ScalarPropertyValue("p_val"));
        nodePropsOverride.getProperties().put("unique_prop", new ScalarPropertyValue("p_val"));
        nodePropsOverride.getProperties().put("type_variant_prop", new ScalarPropertyValue("p_val"));
        nodePropsOverride.getProperties().put("constraint_variant_prop", new ScalarPropertyValue("p_val"));
        NodeCapabilitiesPropsOverride nodeCapabilitiesPropsOverride = new NodeCapabilitiesPropsOverride();
        nodeCapabilitiesPropsOverride.getProperties().put("common_property", new ScalarPropertyValue("p_val"));
        nodeCapabilitiesPropsOverride.getProperties().put("unique_prop", new ScalarPropertyValue("p_val"));
        nodeCapabilitiesPropsOverride.getProperties().put("type_variant_prop", new ScalarPropertyValue("p_val"));
        nodeCapabilitiesPropsOverride.getProperties().put("constraint_variant_prop", new ScalarPropertyValue("p_val"));
        nodePropsOverride.getCapabilities().put("my_capability", nodeCapabilitiesPropsOverride);
        matchingConfiguration.setMatchedNodesConfiguration(Maps.newHashMap());
        matchingConfiguration.getMatchedNodesConfiguration().put("my_node", nodePropsOverride);
        FlowExecutionContext mockContext = Mockito.mock(FlowExecutionContext.class);
        Mockito.when(mockContext.log()).thenReturn(Mockito.mock(FlowExecutionLog.class));
        Mockito.when(mockContext.getConfiguration(DeploymentMatchingConfiguration.class, AbstractPostMatchingSetupModifier.class.getSimpleName())).thenReturn(Optional.of(matchingConfiguration));
        // All properties resources should be remain as matched type is compliant
        postMatchingNodeSetupModifier.process(topology, mockContext);
        Assert.assertEquals(4, nodePropsOverride.getProperties().size());
        Assert.assertEquals(4, nodePropsOverride.getCapabilities().get("my_capability").getProperties().size());
        // Change the type and check that properties are cleared
        topology.getNodeTemplates().clear();
        topology.getNodeTemplates().put("my_node", TemplateBuilder.buildNodeTemplate(parsingResult.getResult().getNodeTypes().get("org.alien4cloud.test.matching.nodes.LocationCustomImplTwo")));
        postMatchingNodeSetupModifier.process(topology, mockContext);
        Assert.assertEquals(1, nodePropsOverride.getProperties().size());
        Assert.assertEquals(1, nodePropsOverride.getCapabilities().get("my_capability").getProperties().size());
    } finally {
        ToscaContext.destroy();
    }
}
Also used : Csar(org.alien4cloud.tosca.model.Csar) CapabilityType(org.alien4cloud.tosca.model.types.CapabilityType) Set(java.util.Set) NodePropsOverride(org.alien4cloud.alm.deployment.configuration.model.DeploymentMatchingConfiguration.NodePropsOverride) Topology(org.alien4cloud.tosca.model.templates.Topology) NodeCapabilitiesPropsOverride(org.alien4cloud.alm.deployment.configuration.model.DeploymentMatchingConfiguration.NodeCapabilitiesPropsOverride) ArchiveRoot(alien4cloud.tosca.model.ArchiveRoot) NodeType(org.alien4cloud.tosca.model.types.NodeType) DeploymentMatchingConfiguration(org.alien4cloud.alm.deployment.configuration.model.DeploymentMatchingConfiguration) FlowExecutionContext(org.alien4cloud.alm.deployment.configuration.flow.FlowExecutionContext) ScalarPropertyValue(org.alien4cloud.tosca.model.definitions.ScalarPropertyValue) FlowExecutionLog(org.alien4cloud.alm.deployment.configuration.flow.FlowExecutionLog) Test(org.junit.Test)

Aggregations

NodePropsOverride (org.alien4cloud.alm.deployment.configuration.model.DeploymentMatchingConfiguration.NodePropsOverride)5 DeploymentMatchingConfiguration (org.alien4cloud.alm.deployment.configuration.model.DeploymentMatchingConfiguration)3 NodeCapabilitiesPropsOverride (org.alien4cloud.alm.deployment.configuration.model.DeploymentMatchingConfiguration.NodeCapabilitiesPropsOverride)3 NotFoundException (alien4cloud.exception.NotFoundException)2 Entry (java.util.Map.Entry)2 FlowExecutionContext (org.alien4cloud.alm.deployment.configuration.flow.FlowExecutionContext)2 AbstractPropertyValue (org.alien4cloud.tosca.model.definitions.AbstractPropertyValue)2 PropertyDefinition (org.alien4cloud.tosca.model.definitions.PropertyDefinition)2 Capability (org.alien4cloud.tosca.model.templates.Capability)2 Topology (org.alien4cloud.tosca.model.templates.Topology)2 CapabilityType (org.alien4cloud.tosca.model.types.CapabilityType)2 NodeType (org.alien4cloud.tosca.model.types.NodeType)2 LocationPolicyTask (alien4cloud.topology.task.LocationPolicyTask)1 ToscaContext (alien4cloud.tosca.context.ToscaContext)1 ArchiveRoot (alien4cloud.tosca.model.ArchiveRoot)1 AlienUtils.safe (alien4cloud.utils.AlienUtils.safe)1 Iterator (java.util.Iterator)1 Map (java.util.Map)1 Set (java.util.Set)1 Slf4j (lombok.extern.slf4j.Slf4j)1