Search in sources :

Example 1 with ArchiveRoot

use of alien4cloud.tosca.model.ArchiveRoot 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 ArchiveRoot

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

the class AbstractLocationConfigurer method addToAchive.

protected void addToAchive(List<PluginArchive> archives, String path) throws ParsingException {
    Path archivePath = selfContext.getPluginPath().resolve(path);
    // Parse the archives
    ParsingResult<ArchiveRoot> result = archiveParser.parseDir(archivePath, AlienConstants.GLOBAL_WORKSPACE_ID);
    PluginArchive pluginArchive = new PluginArchive(result.getResult(), archivePath);
    archives.add(pluginArchive);
}
Also used : Path(java.nio.file.Path) ArchiveRoot(alien4cloud.tosca.model.ArchiveRoot) PluginArchive(alien4cloud.orchestrators.plugin.model.PluginArchive)

Example 3 with ArchiveRoot

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

the class DeployTask method csar2zip.

/**
 * Get csar and add entries in zip file for it
 * @return relative path to the yml, ex: welcome-types/3.0-SNAPSHOT/welcome-types.yaml
 */
private String csar2zip(ZipOutputStream zout, Csar csar, int location) throws IOException, ParsingException {
    // Get path directory to the needed info:
    // should be something like: ...../runtime/csar/<module>/<version>/expanded
    // We should have a yml or a yaml here
    Path csarPath = orchestrator.getCSAR(csar.getName(), csar.getVersion());
    String dirname = csarPath.toString();
    File directory = new File(dirname);
    String relative = csar.getName() + "/" + csar.getVersion() + "/";
    String ret = relative + csar.getYamlFilePath();
    // All files under this directory must be put in the zip
    URI base = directory.toURI();
    Deque<File> queue = new LinkedList<>();
    queue.push(directory);
    while (!queue.isEmpty()) {
        directory = queue.pop();
        for (File kid : directory.listFiles()) {
            String name = base.relativize(kid.toURI()).getPath();
            if (kid.isDirectory()) {
                queue.push(kid);
            } else {
                File file = kid;
                createZipEntries(relative + name, zout);
                if (name.equals(csar.getYamlFilePath())) {
                    ToscaContext.Context oldCtx = ToscaContext.get();
                    ParsingResult<ArchiveRoot> parsingResult;
                    try {
                        ToscaContext.init(Sets.newHashSet());
                        parsingResult = orchestrator.getParser().parseFile(Paths.get(file.getAbsolutePath()));
                    } finally {
                        ToscaContext.set(oldCtx);
                    }
                    if (parsingResult.getContext().getParsingErrors().size() > 0) {
                        Boolean hasFatalError = false;
                        for (ParsingError error : parsingResult.getContext().getParsingErrors()) {
                            if (error.getErrorLevel().equals(ParsingErrorLevel.ERROR)) {
                                log.error(error.toString());
                                hasFatalError = true;
                            } else {
                                log.warn(error.toString());
                            }
                        }
                        if (hasFatalError) {
                            continue;
                        }
                    }
                    ArchiveRoot root = parsingResult.getResult();
                    if (location == LOC_KUBERNETES) {
                        matchKubernetesImplementation(root);
                    }
                    String yaml = orchestrator.getToscaComponentExporter().getYaml(root);
                    zout.write(yaml.getBytes(Charset.forName("UTF-8")));
                } else {
                    copy(file, zout);
                }
            }
        }
    }
    return ret;
}
Also used : Path(java.nio.file.Path) ToscaContext(alien4cloud.tosca.context.ToscaContext) ArchiveRoot(alien4cloud.tosca.model.ArchiveRoot) ParsingError(alien4cloud.tosca.parser.ParsingError) File(java.io.File) URI(java.net.URI) LinkedList(java.util.LinkedList)

Example 4 with ArchiveRoot

use of alien4cloud.tosca.model.ArchiveRoot 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 ArchiveRoot

use of alien4cloud.tosca.model.ArchiveRoot in project alien4cloud by alien4cloud.

the class EditorTopologyUploadService method processTopologyDir.

public void processTopologyDir(Path archivePath, String workspace) {
    // parse the archive.
    try {
        ParsingResult<ArchiveRoot> parsingResult = toscaArchiveParser.parseDir(archivePath);
        processTopologyParseResult(archivePath, parsingResult, workspace);
    } catch (ParsingException e) {
        // Manage parsing error and dispatch them in the right editor exception
        throw new EditorToscaYamlParsingException("The uploaded file to override the topology yaml is not a valid Tosca Yaml.", toParsingResult(e));
    }
}
Also used : ArchiveRoot(alien4cloud.tosca.model.ArchiveRoot) EditorToscaYamlParsingException(org.alien4cloud.tosca.editor.exception.EditorToscaYamlParsingException) ParsingException(alien4cloud.tosca.parser.ParsingException) EditorToscaYamlParsingException(org.alien4cloud.tosca.editor.exception.EditorToscaYamlParsingException)

Aggregations

ArchiveRoot (alien4cloud.tosca.model.ArchiveRoot)76 Test (org.junit.Test)43 Set (java.util.Set)26 NodeType (org.alien4cloud.tosca.model.types.NodeType)26 Csar (org.alien4cloud.tosca.model.Csar)25 RelationshipType (org.alien4cloud.tosca.model.types.RelationshipType)16 CapabilityType (org.alien4cloud.tosca.model.types.CapabilityType)14 ParsingError (alien4cloud.tosca.parser.ParsingError)13 Path (java.nio.file.Path)11 PropertyDefinition (org.alien4cloud.tosca.model.definitions.PropertyDefinition)9 NodeTemplate (org.alien4cloud.tosca.model.templates.NodeTemplate)8 Node (org.yaml.snakeyaml.nodes.Node)8 PluginArchive (alien4cloud.orchestrators.plugin.model.PluginArchive)6 CSARDependency (org.alien4cloud.tosca.model.CSARDependency)6 AbstractPropertyValue (org.alien4cloud.tosca.model.definitions.AbstractPropertyValue)6 ParsingException (alien4cloud.tosca.parser.ParsingException)5 Map (java.util.Map)5 Topology (org.alien4cloud.tosca.model.templates.Topology)5 ComplexPropertyValue (org.alien4cloud.tosca.model.definitions.ComplexPropertyValue)4 ScalarPropertyValue (org.alien4cloud.tosca.model.definitions.ScalarPropertyValue)4