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);
}
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;
}
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);
}
}
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);
}
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();
}
}
Aggregations