Search in sources :

Example 56 with Topology

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);
}
Also used : NodeTemplate(org.alien4cloud.tosca.model.templates.NodeTemplate) Capability(org.alien4cloud.tosca.model.templates.Capability) Topology(org.alien4cloud.tosca.model.templates.Topology) ScalarPropertyValue(org.alien4cloud.tosca.model.definitions.ScalarPropertyValue) AbstractPropertyValue(org.alien4cloud.tosca.model.definitions.AbstractPropertyValue)

Example 57 with Topology

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

Example 58 with Topology

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);
}
Also used : NodeTemplate(org.alien4cloud.tosca.model.templates.NodeTemplate) Topology(org.alien4cloud.tosca.model.templates.Topology) DeploymentArtifact(org.alien4cloud.tosca.model.definitions.DeploymentArtifact) Test(org.junit.Test)

Example 59 with Topology

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"));
}
Also used : NodeTemplate(org.alien4cloud.tosca.model.templates.NodeTemplate) HashMap(java.util.HashMap) Topology(org.alien4cloud.tosca.model.templates.Topology) Test(org.junit.Test)

Example 60 with Topology

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);
        }
    }
}
Also used : NodeTemplate(org.alien4cloud.tosca.model.templates.NodeTemplate) DeploymentInputs(org.alien4cloud.alm.deployment.configuration.model.DeploymentInputs) DeploymentMatchingConfiguration(org.alien4cloud.alm.deployment.configuration.model.DeploymentMatchingConfiguration) Deployment(alien4cloud.model.deployment.Deployment) NotFoundException(alien4cloud.exception.NotFoundException) DeploymentTopology(alien4cloud.model.deployment.DeploymentTopology) Topology(org.alien4cloud.tosca.model.templates.Topology)

Aggregations

Topology (org.alien4cloud.tosca.model.templates.Topology)79 NodeTemplate (org.alien4cloud.tosca.model.templates.NodeTemplate)22 Map (java.util.Map)21 ApplicationEnvironment (alien4cloud.model.application.ApplicationEnvironment)17 Csar (org.alien4cloud.tosca.model.Csar)16 Application (alien4cloud.model.application.Application)15 NotFoundException (alien4cloud.exception.NotFoundException)14 ApiOperation (io.swagger.annotations.ApiOperation)13 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)13 ApplicationTopologyVersion (alien4cloud.model.application.ApplicationTopologyVersion)12 DeploymentTopology (alien4cloud.model.deployment.DeploymentTopology)12 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)12 AlienUtils.safe (alien4cloud.utils.AlienUtils.safe)9 FlowExecutionContext (org.alien4cloud.alm.deployment.configuration.flow.FlowExecutionContext)9 NodeType (org.alien4cloud.tosca.model.types.NodeType)9 IOException (java.io.IOException)8 List (java.util.List)8 DeploymentMatchingConfiguration (org.alien4cloud.alm.deployment.configuration.model.DeploymentMatchingConfiguration)8 Component (org.springframework.stereotype.Component)8 Audit (alien4cloud.audit.annotation.Audit)7