Search in sources :

Example 1 with Csar

use of org.alien4cloud.tosca.model.Csar in project yorc-a4c-plugin by ystia.

the class ToscaExportersTest method testComponentSerialization.

@Test
public void testComponentSerialization() throws Exception {
    Mockito.reset(repositorySearchService);
    String rootDir = "src/test/resources/org/ystia/yorc/alien4cloud/plugin/tosca";
    Csar csar = new Csar("tosca-normative-types", "1.0.0-ALIEN20");
    Mockito.when(repositorySearchService.getArchive(csar.getName(), csar.getVersion())).thenReturn(csar);
    NodeType mockedResult = Mockito.mock(NodeType.class);
    Mockito.when(mockedResult.getArchiveName()).thenReturn("tosca-normative-types");
    Mockito.when(mockedResult.getArchiveVersion()).thenReturn("1.0.0-ALIEN20");
    DeploymentArtifact da = Mockito.mock(DeploymentArtifact.class);
    Mockito.when(da.getArtifactPath()).thenReturn("test");
    Mockito.when(da.getArtifactRef()).thenReturn("test");
    Mockito.when(da.getArtifactType()).thenReturn("file");
    Mockito.when(da.getArchiveName()).thenReturn("tosca-normative-types");
    Mockito.when(da.getArchiveVersion()).thenReturn("1.0.0-ALIEN20");
    Mockito.when(mockedResult.getArtifacts()).thenReturn(Collections.singletonMap("SoftwareComponentArtifact", da));
    Mockito.when(repositorySearchService.getElementInDependencies(Mockito.eq(NodeType.class), Mockito.eq("tosca.nodes.SoftwareComponent"), Mockito.any(Set.class))).thenReturn(mockedResult);
    Mockito.when(mockedResult.getDerivedFrom()).thenReturn(Lists.newArrayList("tosca.nodes.Root"));
    Mockito.when(repositorySearchService.getElementInDependencies(Mockito.eq(NodeType.class), Mockito.eq("tosca.nodes.Root"), Mockito.any(Set.class))).thenReturn(mockedResult);
    Mockito.when(repositorySearchService.getElementInDependencies(Mockito.eq(NodeType.class), Mockito.eq("tosca.nodes.Compute"), Mockito.any(Set.class))).thenReturn(mockedResult);
    Mockito.when(repositorySearchService.getElementInDependencies(Mockito.eq(DataType.class), Mockito.eq(NormativeCredentialConstant.DATA_TYPE), Mockito.any(Set.class))).thenReturn(Mockito.mock(DataType.class));
    RelationshipType hostedOn = new RelationshipType();
    Mockito.when(repositorySearchService.getElementInDependencies(Mockito.eq(RelationshipType.class), Mockito.eq("tosca.relationships.HostedOn"), Mockito.any(Set.class))).thenReturn(hostedOn);
    ParsingResult<ArchiveRoot> parsingResult = parser.parseFile(Paths.get(rootDir, "tosca_component_input.yaml"));
    System.out.println(parsingResult.getContext().getParsingErrors());
    assertNoBlocker(parsingResult);
    String resultYaml = toscaComponentExporter.getYaml(parsingResult.getResult());
    System.out.println(resultYaml);
    String expectedResult = FileUtils.readFileToString(Paths.get(rootDir, "tosca_component_output.yaml").toFile(), "UTF-8");
    // Make some whitespaces change here as IDEs have auto-format features that will overwrite them in the file
    expectedResult = expectedResult.replaceAll("verbose:\\n", "verbose: \n");
    expectedResult = expectedResult.replaceAll("default:\\n", "default: \n");
    Assert.assertEquals(expectedResult, resultYaml);
}
Also used : Csar(org.alien4cloud.tosca.model.Csar) LinkedHashSet(java.util.LinkedHashSet) Set(java.util.Set) ArchiveRoot(alien4cloud.tosca.model.ArchiveRoot) NodeType(org.alien4cloud.tosca.model.types.NodeType) RelationshipType(org.alien4cloud.tosca.model.types.RelationshipType) DataType(org.alien4cloud.tosca.model.types.DataType) DeploymentArtifact(org.alien4cloud.tosca.model.definitions.DeploymentArtifact) Test(org.junit.Test) AbstractPluginTest(org.ystia.yorc.alien4cloud.plugin.AbstractPluginTest)

Example 2 with Csar

use of org.alien4cloud.tosca.model.Csar in project yorc-a4c-plugin by ystia.

the class OpenStackBSComputeWFModifier method doProcess.

private void doProcess(Topology topology, FlowExecutionContext context) {
    Csar csar = new Csar(topology.getArchiveName(), topology.getArchiveVersion());
    Workflow installWF = topology.getWorkflows().get("install");
    Workflow uninstallWF = topology.getWorkflows().get("uninstall");
    Set<NodeTemplate> bsSet = TopologyNavigationUtil.getNodesOfType(topology, YORC_OPENSTACK_BS_TYPE, true);
    // Let's process all BS
    bsSet.forEach(bs -> safe(bs.getRelationships()).forEach((rn, rt) -> {
        if ("tosca.capabilities.Attachment".equals(rt.getRequirementType())) {
            // Attachment found
            context.getLog().info("Found a BlockStorage <{}> with an attachment on <{}>. Let's swap their workflow steps to match Yorc " + "expectations.", bs.getName(), rt.getTarget());
            String computeNodeName = rt.getTarget();
            // Now lets locate corresponding wf steps in install wf
            for (Map.Entry<String, WorkflowStep> workflowStepEntry : installWF.getSteps().entrySet()) {
                if (workflowStepEntry.getValue().getTarget().equals(bs.getName())) {
                    for (String precedingStepName : workflowStepEntry.getValue().getPrecedingSteps()) {
                        WorkflowStep precedingStep = installWF.getSteps().get(precedingStepName);
                        if (precedingStep.getTarget().equals(computeNodeName)) {
                            // We do not use swap operation here as it may mess up other workflow edges
                            // First remove the edge between steps
                            RemoveEdgeOperation removeEdgeOperation = new RemoveEdgeOperation();
                            removeEdgeOperation.setWorkflowName(installWF.getName());
                            removeEdgeOperation.setFromStepId(precedingStepName);
                            removeEdgeOperation.setToStepId(workflowStepEntry.getKey());
                            log.debug("Swapping {} with target {}", precedingStepName, workflowStepEntry.getKey());
                            removeEdgeProcessor.process(csar, topology, removeEdgeOperation);
                            // Then reconnect them in the right sequence
                            ConnectStepFromOperation connectStepFromOperation = new ConnectStepFromOperation();
                            connectStepFromOperation.setWorkflowName(installWF.getName());
                            connectStepFromOperation.setFromStepIds(new String[] { workflowStepEntry.getKey() });
                            connectStepFromOperation.setToStepId(precedingStepName);
                            connectStepFromProcessor.process(csar, topology, connectStepFromOperation);
                            break;
                        }
                    }
                    break;
                }
            }
            // Now lets locate corresponding wf steps in uninstall wf
            for (Map.Entry<String, WorkflowStep> workflowStepEntry : uninstallWF.getSteps().entrySet()) {
                if (workflowStepEntry.getValue().getTarget().equals(bs.getName())) {
                    for (String onSuccessStepName : workflowStepEntry.getValue().getOnSuccess()) {
                        WorkflowStep onSuccessStep = uninstallWF.getSteps().get(onSuccessStepName);
                        if (onSuccessStep.getTarget().equals(computeNodeName)) {
                            // We do not use swap operation here as it may mess up other workflow edges
                            // First remove the edge between steps
                            RemoveEdgeOperation removeEdgeOperation = new RemoveEdgeOperation();
                            removeEdgeOperation.setWorkflowName(uninstallWF.getName());
                            removeEdgeOperation.setFromStepId(workflowStepEntry.getKey());
                            removeEdgeOperation.setToStepId(onSuccessStepName);
                            log.debug("Swapping {} with target {}", onSuccessStepName, workflowStepEntry.getKey());
                            removeEdgeProcessor.process(csar, topology, removeEdgeOperation);
                            // Then reconnect them in the right sequence
                            ConnectStepFromOperation connectStepFromOperation = new ConnectStepFromOperation();
                            connectStepFromOperation.setWorkflowName(uninstallWF.getName());
                            connectStepFromOperation.setFromStepIds(new String[] { onSuccessStepName });
                            connectStepFromOperation.setToStepId(workflowStepEntry.getKey());
                            connectStepFromProcessor.process(csar, topology, connectStepFromOperation);
                            break;
                        }
                    }
                    break;
                }
            }
        // Start & Stop make no sense for those kind of nodes in Yorc as those operations are not implemented.
        // Do not change those WFs
        }
    }));
}
Also used : ToscaContextual(alien4cloud.tosca.context.ToscaContextual) Csar(org.alien4cloud.tosca.model.Csar) WorkflowValidator(alien4cloud.paas.wf.validation.WorkflowValidator) Workflow(org.alien4cloud.tosca.model.workflow.Workflow) Resource(javax.annotation.Resource) Set(java.util.Set) ConnectStepFromProcessor(org.alien4cloud.tosca.editor.processors.workflow.ConnectStepFromProcessor) TopologyModifierSupport(org.alien4cloud.alm.deployment.configuration.flow.TopologyModifierSupport) TopologyNavigationUtil(org.alien4cloud.tosca.utils.TopologyNavigationUtil) AlienUtils.safe(alien4cloud.utils.AlienUtils.safe) Slf4j(lombok.extern.slf4j.Slf4j) Component(org.springframework.stereotype.Component) RemoveEdgeOperation(org.alien4cloud.tosca.editor.operations.workflow.RemoveEdgeOperation) FlowExecutionContext(org.alien4cloud.alm.deployment.configuration.flow.FlowExecutionContext) Map(java.util.Map) NodeTemplate(org.alien4cloud.tosca.model.templates.NodeTemplate) Topology(org.alien4cloud.tosca.model.templates.Topology) ConnectStepFromOperation(org.alien4cloud.tosca.editor.operations.workflow.ConnectStepFromOperation) WorkflowStep(org.alien4cloud.tosca.model.workflow.WorkflowStep) RemoveEdgeProcessor(org.alien4cloud.tosca.editor.processors.workflow.RemoveEdgeProcessor) Csar(org.alien4cloud.tosca.model.Csar) RemoveEdgeOperation(org.alien4cloud.tosca.editor.operations.workflow.RemoveEdgeOperation) NodeTemplate(org.alien4cloud.tosca.model.templates.NodeTemplate) WorkflowStep(org.alien4cloud.tosca.model.workflow.WorkflowStep) ConnectStepFromOperation(org.alien4cloud.tosca.editor.operations.workflow.ConnectStepFromOperation) Workflow(org.alien4cloud.tosca.model.workflow.Workflow)

Example 3 with Csar

use of org.alien4cloud.tosca.model.Csar in project yorc-a4c-plugin by ystia.

the class DeployTask method buildZip.

/**
 * Create the zip for yorc, with a modified yaml and all needed archives.
 * Assumes a file original.yml exists in the current directory
 * @param ctx all needed information about the deployment
 * @throws IOException
 */
private void buildZip(PaaSTopologyDeploymentContext ctx) throws IOException {
    // Check location
    int location = LOC_OPENSTACK;
    Location loc = ctx.getLocations().get("_A4C_ALL");
    Set<CSARDependency> locdeps = loc.getDependencies();
    for (CSARDependency dep : locdeps) {
        if (dep.getName().contains("kubernetes")) {
            location = LOC_KUBERNETES;
            break;
        }
        if (dep.getName().contains("slurm")) {
            location = LOC_SLURM;
            break;
        }
        if (dep.getName().contains("aws")) {
            location = LOC_AWS;
            break;
        }
    }
    // Final zip file will be named topology.zip
    final File zip = new File("topology.zip");
    final OutputStream out = new FileOutputStream(zip);
    final ZipOutputStream zout = new ZipOutputStream(out);
    final Closeable res = zout;
    final int finalLocation = location;
    this.ctx.getDeploymentTopology().getDependencies().forEach(d -> {
        if (!"tosca-normative-types".equals(d.getName())) {
            Csar csar = csarRepoSearchService.getArchive(d.getName(), d.getVersion());
            if (CSARSource.ORCHESTRATOR != CSARSource.valueOf(csar.getImportSource())) {
                try {
                    csar2zip(zout, csar, finalLocation);
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
        }
    });
    // Copy overwritten artifacts for each node
    PaaSTopology ptopo = ctx.getPaaSTopology();
    for (PaaSNodeTemplate node : ptopo.getAllNodes().values()) {
        copyArtifacts(node, zout);
    }
    String topoFileName = "topology.yml";
    // Copy modified topology
    createZipEntries(topoFileName, zout);
    // Get the yaml of the application as built by from a4c
    DeploymentTopology dtopo = ctx.getDeploymentTopology();
    Csar myCsar = new Csar(ctx.getDeploymentPaaSId(), dtopo.getArchiveVersion());
    myCsar.setToscaDefinitionsVersion(ToscaParser.LATEST_DSL);
    String yaml = orchestrator.getToscaTopologyExporter().getYaml(myCsar, dtopo, true);
    zout.write(yaml.getBytes(Charset.forName("UTF-8")));
    zout.closeEntry();
    res.close();
}
Also used : Csar(org.alien4cloud.tosca.model.Csar) PaaSTopology(alien4cloud.paas.model.PaaSTopology) DeploymentTopology(alien4cloud.model.deployment.DeploymentTopology) ZipOutputStream(java.util.zip.ZipOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) Closeable(java.io.Closeable) ZipException(java.util.zip.ZipException) YorcRestException(org.ystia.yorc.alien4cloud.plugin.rest.YorcRestException) ParsingException(alien4cloud.tosca.parser.ParsingException) IOException(java.io.IOException) CSARDependency(org.alien4cloud.tosca.model.CSARDependency) PaaSNodeTemplate(alien4cloud.paas.model.PaaSNodeTemplate) ZipOutputStream(java.util.zip.ZipOutputStream) FileOutputStream(java.io.FileOutputStream) File(java.io.File) Location(alien4cloud.model.orchestrators.locations.Location)

Example 4 with Csar

use of org.alien4cloud.tosca.model.Csar in project alien4cloud by alien4cloud.

the class FunctionEvaluatorTest method postConstruct.

@PostConstruct
public void postConstruct() throws Throwable {
    if (!INITIALIZED) {
        if (Files.exists(Paths.get(alienRepoDir))) {
            try {
                FileUtil.delete(Paths.get(alienRepoDir));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        SecurityTestUtils.setTestAuthentication(Role.ADMIN);
        alienDAO.delete(Csar.class, QueryBuilders.matchAllQuery());
        String normativeLocalName = "tosca-normative-types";
        repositoryManager.cloneOrCheckout(artifactsDirectory, "https://github.com/alien4cloud/tosca-normative-types.git", "1.2.0", normativeLocalName);
        String sampleLocalName = "samples";
        repositoryManager.cloneOrCheckout(artifactsDirectory, "https://github.com/alien4cloud/samples.git", "1.4.0-RC1", sampleLocalName);
        Path typesPath = artifactsDirectory.resolve(normativeLocalName);
        Path typesZipPath = artifactsDirectory.resolve(normativeLocalName + ".zip");
        FileUtil.zip(typesPath, typesZipPath);
        ParsingResult<Csar> result = archiveUploadService.upload(typesZipPath, CSARSource.OTHER, AlienConstants.GLOBAL_WORKSPACE_ID);
        ParserTestUtil.displayErrors(result);
        AbstractToscaParserSimpleProfileTest.assertNoBlocker(result);
        // typesPath = artifactsDirectory.resolve(extendedLocalName).resolve("alien-base-types");
        // typesZipPath = artifactsDirectory.resolve("alien-base-types.zip");
        // FileUtil.zip(typesPath, typesZipPath);
        // result = archiveUploadService.upload(typesZipPath, CSARSource.OTHER, AlienConstants.GLOBAL_WORKSPACE_ID);
        // AbstractToscaParserSimpleProfileTest.assertNoBlocker(result);
        typesPath = artifactsDirectory.resolve(sampleLocalName).resolve("jdk");
        typesZipPath = artifactsDirectory.resolve("jdk.zip");
        FileUtil.zip(typesPath, typesZipPath);
        result = archiveUploadService.upload(typesZipPath, CSARSource.OTHER, AlienConstants.GLOBAL_WORKSPACE_ID);
        AbstractToscaParserSimpleProfileTest.assertNoBlocker(result);
        typesPath = artifactsDirectory.resolve(sampleLocalName).resolve("tomcat-war");
        typesZipPath = artifactsDirectory.resolve("tomcat_war.zip");
        FileUtil.zip(typesPath, typesZipPath);
        result = archiveUploadService.upload(typesZipPath, CSARSource.OTHER, AlienConstants.GLOBAL_WORKSPACE_ID);
        AbstractToscaParserSimpleProfileTest.assertNoBlocker(result);
        typesPath = Paths.get("src/test/resources/alien4cloud/paas/function/test-types");
        typesZipPath = artifactsDirectory.resolve("target/test-types.zip");
        FileUtil.zip(typesPath, typesZipPath);
        result = archiveUploadService.upload(typesZipPath, CSARSource.OTHER, AlienConstants.GLOBAL_WORKSPACE_ID);
        AbstractToscaParserSimpleProfileTest.assertNoBlocker(result);
        INITIALIZED = true;
    }
    ParsingResult<ArchiveRoot> result = applicationUtil.parseYamlTopology("src/test/resources/alien4cloud/paas/function/topology/badFunctionsTomcatWar");
    // AbstractToscaParserSimpleProfileTest.assertNoBlocker(result);
    Topology topology = result.getResult().getTopology();
    topology.setId(UUID.randomUUID().toString());
    topology.setWorkspace(AlienConstants.GLOBAL_WORKSPACE_ID);
    builtPaaSNodeTemplates = treeBuilder.buildPaaSTopology(topology).getAllNodes();
}
Also used : Path(java.nio.file.Path) Csar(org.alien4cloud.tosca.model.Csar) ArchiveRoot(alien4cloud.tosca.model.ArchiveRoot) IOException(java.io.IOException) Topology(org.alien4cloud.tosca.model.templates.Topology) PostConstruct(javax.annotation.PostConstruct)

Example 5 with Csar

use of org.alien4cloud.tosca.model.Csar in project alien4cloud by alien4cloud.

the class CsarServiceTest method isArchiveDeployedTest.

@Test
public void isArchiveDeployedTest() throws ExecutionException, InterruptedException {
    alienDao.getClient().prepareDeleteByQuery(new String[] { "csar" }).setQuery(QueryBuilders.matchAllQuery()).execute().get();
    Csar csar = new Csar("archive", "1.0.0-SNAPSHOT");
    csar.setDependencies(Sets.newHashSet(new CSARDependency("toto", "1.0.0"), new CSARDependency("titi", "2.0.0")));
    alienDao.save(csar);
    csar = new Csar("archive2", "1.0.0-SNAPSHOT");
    csar.setDependencies(Sets.newHashSet(new CSARDependency("tata", "1.0.0"), new CSARDependency("tutu", "2.0.0")));
    alienDao.save(csar);
    Csar[] csars = csarService.getDependantCsars("toto", "2.0.0");
    Assert.assertEquals(0, csars.length);
    csar = new Csar("archive3", "1.0.0-SNAPSHOT");
    csar.setDependencies(Sets.newHashSet(new CSARDependency("tata", "1.0.0"), new CSARDependency("toto", "2.0.0")));
    alienDao.save(csar);
    csars = csarService.getDependantCsars("toto", "2.0.0");
    Assert.assertEquals(1, csars.length);
    csar = new Csar("archive4", "1.0.0-SNAPSHOT");
    csar.setDependencies(Sets.newHashSet(new CSARDependency("tata", "1.0.0"), new CSARDependency("toto", "2.0.0")));
    alienDao.save(csar);
    csars = csarService.getDependantCsars("toto", "2.0.0");
    Assert.assertEquals(2, csars.length);
}
Also used : Csar(org.alien4cloud.tosca.model.Csar) CSARDependency(org.alien4cloud.tosca.model.CSARDependency) Test(org.junit.Test)

Aggregations

Csar (org.alien4cloud.tosca.model.Csar)61 CSARDependency (org.alien4cloud.tosca.model.CSARDependency)16 ArchiveRoot (alien4cloud.tosca.model.ArchiveRoot)15 Topology (org.alien4cloud.tosca.model.templates.Topology)11 Test (org.junit.Test)11 Set (java.util.Set)10 Path (java.nio.file.Path)9 NodeType (org.alien4cloud.tosca.model.types.NodeType)9 Usage (alien4cloud.model.common.Usage)7 CapabilityType (org.alien4cloud.tosca.model.types.CapabilityType)7 IOException (java.io.IOException)6 NodeTemplate (org.alien4cloud.tosca.model.templates.NodeTemplate)6 RelationshipType (org.alien4cloud.tosca.model.types.RelationshipType)6 ApiOperation (io.swagger.annotations.ApiOperation)5 List (java.util.List)5 Application (alien4cloud.model.application.Application)4 ParsingError (alien4cloud.tosca.parser.ParsingError)4 PostConstruct (javax.annotation.PostConstruct)4 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)4 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)4