use of org.alien4cloud.tosca.model.templates.Topology in project alien4cloud by alien4cloud.
the class FunctionEvaluatorTest method getEvaluationContext.
/**
* Generate the topology below (no need type validations) and inputs to be used for next tests.
*
* @return A function evaluator context for the test topology.
*/
private FunctionEvaluatorContext getEvaluationContext() {
NodeTemplate myNode = new NodeTemplate();
myNode.setProperties(Maps.newHashMap());
setPropertiesValues(myNode.getProperties(), "");
Capability capability = new Capability();
myNode.setCapabilities(Maps.newHashMap());
myNode.getCapabilities().put("my_capability", capability);
capability.setProperties(Maps.newHashMap());
setPropertiesValues(capability.getProperties(), "capa ");
Topology topology = new Topology();
topology.setNodeTemplates(Maps.newHashMap());
topology.getNodeTemplates().put("my_node", myNode);
Map<String, AbstractPropertyValue> inputs = Maps.newHashMap();
inputs.put("scalar_input", new ScalarPropertyValue("scalar input value"));
return new FunctionEvaluatorContext(topology, inputs);
}
use of org.alien4cloud.tosca.model.templates.Topology 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();
}
}
use of org.alien4cloud.tosca.model.templates.Topology in project alien4cloud by alien4cloud.
the class ToscaSerializerUtilsTest method testArtifactsAndRepositoriesExport.
@Test
public void testArtifactsAndRepositoriesExport() {
Topology topology = new Topology();
topology.setNodeTemplates(Maps.newHashMap());
NodeTemplate node = new NodeTemplate();
node.setArtifacts(Maps.newHashMap());
topology.getNodeTemplates().put("Compute", node);
// no repositories
DeploymentArtifact deploymentArtifact = createArtifact();
deploymentArtifact.setArtifactRef("aaa/bbb.zip");
deploymentArtifact.setArtifactType("tosca.artifacts.File");
node.getArtifacts().put("local_war", deploymentArtifact);
Assert.assertFalse(ToscaSerializerUtils.hasRepositories("test", "1.0", topology));
String deploymentArtifactExport = ToscaSerializerUtils.formatArtifact(deploymentArtifact, 2);
Assert.assertEquals(" file: aaa/bbb.zip\n type: tosca.artifacts.File", deploymentArtifactExport);
// one repository should success
DeploymentArtifact deploymentArtifact2 = createArtifact();
deploymentArtifact2.setArtifactRef("aaa/bbb.zip");
deploymentArtifact2.setArtifactType("tosca.artifacts.File");
deploymentArtifact2.setRepositoryName("my_company");
deploymentArtifact2.setRepositoryURL("http://my_company.org");
deploymentArtifact2.setArtifactRepository("http");
deploymentArtifact2.setRepositoryCredential(Maps.newHashMap());
deploymentArtifact2.getRepositoryCredential().put("user", "my_user");
deploymentArtifact2.getRepositoryCredential().put("token", "password");
node.getArtifacts().put("http_war", deploymentArtifact2);
Assert.assertTrue(ToscaSerializerUtils.hasRepositories("test", "1.0", topology));
String deploymentArtifact2Export = ToscaSerializerUtils.formatArtifact(deploymentArtifact2, 1);
String repositoriesExport = ToscaSerializerUtils.formatRepositories("test", "1.0", topology);
Assert.assertEquals(" file: aaa/bbb.zip\n type: tosca.artifacts.File\n repository: my_company", deploymentArtifact2Export);
Assert.assertEquals(" my_company:\n url: http://my_company.org\n type: http\n credential:\n token: password\n user: my_user", repositoriesExport);
// two repositories with different name
DeploymentArtifact deploymentArtifact3 = createArtifact();
deploymentArtifact3.setArtifactRef("aaa/ccc.zip");
deploymentArtifact3.setArtifactType("tosca.artifacts.File");
deploymentArtifact3.setRepositoryName("my_companyBis");
deploymentArtifact3.setRepositoryURL("http://my_company.org");
deploymentArtifact3.setArtifactRepository("maven");
node.getArtifacts().put("http_war2", deploymentArtifact3);
Assert.assertTrue(ToscaSerializerUtils.hasRepositories("test", "1.0", topology));
repositoriesExport = ToscaSerializerUtils.formatRepositories("test", "1.0", topology);
Assert.assertEquals(" my_company:\n url: http://my_company.org\n type: http\n credential:\n token: password\n user: my_user\n my_companyBis:\n url: http://my_company.org\n type: maven", repositoriesExport);
// add a new artifact with an already existing repository, should not duplicate the repository
DeploymentArtifact deploymentArtifact4 = createArtifact();
deploymentArtifact4.setArtifactRef("aaa/ddd.zip");
deploymentArtifact4.setArtifactType("tosca.artifacts.File");
deploymentArtifact4.setRepositoryName("my_company");
deploymentArtifact4.setRepositoryURL("http://my_company.org");
deploymentArtifact4.setArtifactRepository("http");
node.getArtifacts().put("http_war3", deploymentArtifact4);
Assert.assertTrue(ToscaSerializerUtils.hasRepositories("test", "1.0", topology));
repositoriesExport = ToscaSerializerUtils.formatRepositories("test", "1.0", topology);
Assert.assertEquals(" my_company:\n url: http://my_company.org\n type: http\n credential:\n token: password\n user: my_user\n my_companyBis:\n url: http://my_company.org\n type: maven", repositoriesExport);
}
use of org.alien4cloud.tosca.model.templates.Topology in project alien4cloud by alien4cloud.
the class TopologyServiceTest method normalizeAllNodeTemplateName.
@Test
public void normalizeAllNodeTemplateName() {
Topology topology = new Topology();
topology.setArchiveName("test-topology");
topology.setArchiveVersion("1.0.0");
topology.setWorkspace(AlienConstants.GLOBAL_WORKSPACE_ID);
Map<String, NodeTemplate> nodeTemplates = new HashMap<>();
nodeTemplates.put("Computé", new NodeTemplate());
nodeTemplates.put("Compute-2", new NodeTemplate());
nodeTemplates.put("Compute.2", new NodeTemplate());
nodeTemplates.put("Compute 2", new NodeTemplate());
topology.setNodeTemplates(nodeTemplates);
TopologyUtils.normalizeAllNodeTemplateName(topology, null, null);
Assert.assertTrue(topology.getNodeTemplates().containsKey("Compute"));
Assert.assertTrue(topology.getNodeTemplates().containsKey("Compute_2"));
Assert.assertTrue(topology.getNodeTemplates().containsKey("Compute_21"));
Assert.assertTrue(topology.getNodeTemplates().containsKey("Compute_22"));
}
use of org.alien4cloud.tosca.model.templates.Topology in project alien4cloud by alien4cloud.
the class BlockStorageEventHandler method updateApplicationTopology.
private void updateApplicationTopology(PaaSInstancePersistentResourceMonitorEvent persistentResourceEvent, final Map<String, Object> persistentProperties) {
Deployment deployment = deploymentService.get(persistentResourceEvent.getDeploymentId());
String topologyId = deployment.getSourceId() + ":" + deployment.getVersionId();
Topology topology = topoServiceCore.getOrFail(topologyId);
// The deployment topology may have changed and the node removed, in such situations there is nothing to update as the block won't be reused.
NodeTemplate nodeTemplate;
try {
nodeTemplate = TopologyUtils.getNodeTemplate(topology, persistentResourceEvent.getNodeTemplateId());
} catch (NotFoundException e) {
log.warn("Fail to update persistent resource for node {}", persistentResourceEvent.getNodeTemplateId(), e);
return;
}
for (String propertyName : persistentProperties.keySet()) {
Object propertyValue = persistentProperties.get(propertyName);
AbstractPropertyValue abstractPropertyValue = nodeTemplate.getProperties().get(propertyName);
if (abstractPropertyValue != null && abstractPropertyValue instanceof FunctionPropertyValue) {
// the value is set in the topology
FunctionPropertyValue function = (FunctionPropertyValue) abstractPropertyValue;
if (function.getFunction().equals(ToscaFunctionConstants.GET_INPUT) && propertyValue instanceof String) {
DeploymentInputs deploymentInputs = deploymentConfigurationDao.findById(DeploymentInputs.class, AbstractDeploymentConfig.generateId(deployment.getVersionId(), deployment.getEnvironmentId()));
// the value is set in the input (deployment setup)
log.info("Updating deploymentsetup [ {} ] input properties [ {} ] to add a new VolumeId", deploymentInputs.getId(), function.getTemplateName());
log.debug("Property [ {} ] to update: [ {} ]. New value is [ {} ]", propertyName, persistentResourceEvent.getPersistentProperties().get(propertyName), propertyValue);
deploymentInputs.getInputs().put(function.getTemplateName(), new ScalarPropertyValue((String) propertyValue));
deploymentConfigurationDao.save(deploymentInputs);
} else {
// this is not supported / print a warning
log.warn("Failed to store the id of the created block storage [ {} ] for deployment [ {} ] application [ {} ] environment [ {} ]");
return;
}
} else {
DeploymentMatchingConfiguration matchingConfiguration = deploymentConfigurationDao.findById(DeploymentMatchingConfiguration.class, AbstractDeploymentConfig.generateId(deployment.getVersionId(), deployment.getEnvironmentId()));
log.info("Updating deployment topology: Persistent resource property [ {} ] for node template <{}.{}> to add a value", propertyName, matchingConfiguration.getId(), persistentResourceEvent.getNodeTemplateId());
log.debug("Value to add: [ {} ]. New value is [ {} ]", persistentResourceEvent.getPersistentProperties().get(propertyName), propertyValue);
matchingConfiguration.getMatchedNodesConfiguration().get(persistentResourceEvent.getNodeTemplateId()).getProperties().put(propertyName, getPropertyValue(propertyValue));
deploymentConfigurationDao.save(matchingConfiguration);
}
}
}
Aggregations