Search in sources :

Example 1 with ManifestContents

use of org.eclipse.virgo.util.parser.manifest.ManifestContents in project winery by eclipse.

the class AccountabilityManagerImpl method fillFilesOfModel.

private void fillFilesOfModel(ModelProvenanceElement model) {
    // 1. parse element.state as ManifestContent
    RecoveringManifestParser genericParser = new RecoveringManifestParser();
    ManifestContents manifestContents = genericParser.parse(model.getFingerprint());
    // 2. parse the ManifestContent as a TOSCAMetaFile
    TOSCAMetaFileParser parser = new TOSCAMetaFileParser();
    TOSCAMetaFile toscaMetaFile = parser.parse(manifestContents, genericParser.getProblems().size());
    // 3. retrieve files from meta file
    Objects.requireNonNull(toscaMetaFile);
    List<FileProvenanceElement> result = toscaMetaFile.getFileBlocks().stream().map(fileSection -> {
        FileProvenanceElement fileElement = new FileProvenanceElement(model);
        fileElement.setFileHash(fileSection.get(TOSCAMetaFileAttributes.HASH));
        fileElement.setAddressInImmutableStorage(fileSection.get(TOSCAMetaFileAttributes.IMMUTABLE_ADDRESS));
        fileElement.setFileName(fileSection.get(TOSCAMetaFileAttributes.NAME));
        return fileElement;
    }).sorted(Comparator.comparing(FileProvenanceElement::getFileName)).collect(Collectors.toList());
    model.setFiles(result);
}
Also used : ManifestContents(org.eclipse.virgo.util.parser.manifest.ManifestContents) RecoveringManifestParser(org.eclipse.virgo.util.parser.manifest.RecoveringManifestParser) TOSCAMetaFileParser(org.eclipse.winery.model.csar.toscametafile.TOSCAMetaFileParser) TOSCAMetaFile(org.eclipse.winery.model.csar.toscametafile.TOSCAMetaFile) FileProvenanceElement(org.eclipse.winery.accountability.model.FileProvenanceElement)

Example 2 with ManifestContents

use of org.eclipse.virgo.util.parser.manifest.ManifestContents in project winery by eclipse.

the class TOSCAMetaFileParser method parse.

/**
 * Parses and validates the <code>toscaMetaFile</code>.
 *
 * @param toscaMetaFile path to the metadata file to process
 * @return <code>TOSCAMetaFile</code> that gives access to the content of
 * the TOSCA meta file. If the given file doesn't exist or is invalid <code>null</code>.
 */
public TOSCAMetaFile parse(Path toscaMetaFile) {
    FileReader reader = null;
    ManifestParser parser;
    ManifestContents manifestContent;
    TOSCAMetaFile toscaMetaFileContent = null;
    try {
        parser = new RecoveringManifestParser();
        reader = new FileReader(toscaMetaFile.toFile());
        TOSCAMetaFileParser.LOGGER.debug("Parsing TOSCA meta file \"{}\"...", toscaMetaFile.getFileName().toString());
        manifestContent = parser.parse(reader);
        reader.close();
        // counts the errors during parsing
        int numErrors = 0;
        for (ManifestProblem problem : parser.getProblems()) {
            this.logManifestProblem(problem);
            numErrors++;
        }
        toscaMetaFileContent = this.parse(manifestContent, numErrors);
    } catch (FileNotFoundException exc) {
        TOSCAMetaFileParser.LOGGER.error("\"{}\" doesn't exist or is not a file.", toscaMetaFile, exc);
    } catch (IOException exc) {
        TOSCAMetaFileParser.LOGGER.error("An IO Exception occured.", exc);
    } finally {
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException exc) {
                TOSCAMetaFileParser.LOGGER.warn("An IOException occured.", exc);
            }
        }
    }
    return toscaMetaFileContent;
}
Also used : ManifestContents(org.eclipse.virgo.util.parser.manifest.ManifestContents) RecoveringManifestParser(org.eclipse.virgo.util.parser.manifest.RecoveringManifestParser) ManifestParser(org.eclipse.virgo.util.parser.manifest.ManifestParser) RecoveringManifestParser(org.eclipse.virgo.util.parser.manifest.RecoveringManifestParser) ManifestProblem(org.eclipse.virgo.util.parser.manifest.ManifestProblem) FileNotFoundException(java.io.FileNotFoundException) FileReader(java.io.FileReader) IOException(java.io.IOException)

Example 3 with ManifestContents

use of org.eclipse.virgo.util.parser.manifest.ManifestContents in project winery by eclipse.

the class CsarExporterTest method testPutCsarInBlockchainAndImmutableStorage.

@Test
@Disabled
public // todo check how to enable
void testPutCsarInBlockchainAndImmutableStorage() throws Exception {
    setRevisionTo("origin/plain");
    CsarExporter exporter = new CsarExporter(repository);
    DefinitionsChildId id = new ServiceTemplateId("http://plain.winery.opentosca.org/servicetemplates", "ServiceTemplateWithAllReqCapVariants", false);
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    CompletableFuture<String> future = exporter.writeCsarAndSaveManifestInProvenanceLayer(id, os);
    String transactionHash = future.get();
    assertNotNull(transactionHash);
    try (InputStream inputStream = new ByteArrayInputStream(os.toByteArray());
        ZipInputStream zis = new ZipInputStream(inputStream)) {
        ManifestContents manifestContents = parseManifest(zis);
        assertNotNull(manifestContents);
        for (String section : manifestContents.getSectionNames()) {
            assertNotNull(manifestContents.getAttributesForSection(section).get(TOSCAMetaFileAttributes.IMMUTABLE_ADDRESS));
        }
    }
}
Also used : ManifestContents(org.eclipse.virgo.util.parser.manifest.ManifestContents) ZipInputStream(java.util.zip.ZipInputStream) DefinitionsChildId(org.eclipse.winery.model.ids.definitions.DefinitionsChildId) ByteArrayInputStream(java.io.ByteArrayInputStream) ZipInputStream(java.util.zip.ZipInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ServiceTemplateId(org.eclipse.winery.model.ids.definitions.ServiceTemplateId) Test(org.junit.jupiter.api.Test) Disabled(org.junit.jupiter.api.Disabled)

Example 4 with ManifestContents

use of org.eclipse.virgo.util.parser.manifest.ManifestContents in project winery by eclipse.

the class CsarExporterTest method testCsarFilesAreMentionedInTheManifest.

@Test
public void testCsarFilesAreMentionedInTheManifest() throws Exception {
    Map<String, Object> exportConfiguration = new HashMap<>();
    exportConfiguration.put(INCLUDE_HASHES.name(), null);
    try (InputStream inputStream = this.createOutputAndInputStream("origin/plain", new ServiceTemplateId("http://plain.winery.opentosca.org/servicetemplates", "ServiceTemplateWithAllReqCapVariants", false), exportConfiguration);
        ZipInputStream zis = new ZipInputStream(inputStream)) {
        ZipEntry entry;
        List<String> elementsList = new ArrayList<>();
        ManifestContents manifestContents = null;
        while ((entry = zis.getNextEntry()) != null) {
            String name = entry.getName();
            elementsList.add(name);
            assertNotNull(name);
            assertFalse(name.contains("\\"), "name contains backslashes");
            if ("TOSCA-Metadata/TOSCA.meta".equals(name)) {
                byte[] bytes = IOUtils.toByteArray(zis);
                String s = new String(bytes, StandardCharsets.UTF_8);
                manifestContents = new RecoveringManifestParser().parse(s);
            }
        }
        assertNotNull(manifestContents);
        for (String section : manifestContents.getSectionNames()) {
            // ensures that the file is contained in the archive
            assertTrue(elementsList.remove(section), "Contains element " + section);
        }
        // ensures that the manifest was part of the archive
        assertTrue(elementsList.remove("TOSCA-Metadata/TOSCA.meta"));
        // ensures that every file in the archive is listed in the manifest
        assertEquals(0, elementsList.size());
    }
}
Also used : RecoveringManifestParser(org.eclipse.virgo.util.parser.manifest.RecoveringManifestParser) HashMap(java.util.HashMap) ZipInputStream(java.util.zip.ZipInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ZipEntry(java.util.zip.ZipEntry) ArrayList(java.util.ArrayList) ServiceTemplateId(org.eclipse.winery.model.ids.definitions.ServiceTemplateId) ManifestContents(org.eclipse.virgo.util.parser.manifest.ManifestContents) ZipInputStream(java.util.zip.ZipInputStream) Test(org.junit.jupiter.api.Test)

Example 5 with ManifestContents

use of org.eclipse.virgo.util.parser.manifest.ManifestContents in project winery by eclipse.

the class CsarExporterTest method parseManifest.

private ManifestContents parseManifest(ZipInputStream zis) throws IOException {
    ZipEntry entry;
    ManifestContents manifestContents = null;
    while ((entry = zis.getNextEntry()) != null) {
        if ("TOSCA-Metadata/TOSCA.meta".equals(entry.getName())) {
            byte[] bytes = IOUtils.toByteArray(zis);
            String s = new String(bytes, StandardCharsets.UTF_8);
            manifestContents = new RecoveringManifestParser().parse(s);
        }
    }
    return manifestContents;
}
Also used : ManifestContents(org.eclipse.virgo.util.parser.manifest.ManifestContents) RecoveringManifestParser(org.eclipse.virgo.util.parser.manifest.RecoveringManifestParser) ZipEntry(java.util.zip.ZipEntry)

Aggregations

ManifestContents (org.eclipse.virgo.util.parser.manifest.ManifestContents)9 RecoveringManifestParser (org.eclipse.virgo.util.parser.manifest.RecoveringManifestParser)6 ByteArrayInputStream (java.io.ByteArrayInputStream)5 InputStream (java.io.InputStream)5 ZipInputStream (java.util.zip.ZipInputStream)5 Test (org.junit.jupiter.api.Test)5 HashMap (java.util.HashMap)4 ServiceTemplateId (org.eclipse.winery.model.ids.definitions.ServiceTemplateId)4 ArrayList (java.util.ArrayList)3 ZipEntry (java.util.zip.ZipEntry)3 FileProvenanceElement (org.eclipse.winery.accountability.model.FileProvenanceElement)2 Disabled (org.junit.jupiter.api.Disabled)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 FileReader (java.io.FileReader)1 IOException (java.io.IOException)1 ManifestParser (org.eclipse.virgo.util.parser.manifest.ManifestParser)1 ManifestProblem (org.eclipse.virgo.util.parser.manifest.ManifestProblem)1 ModelProvenanceElement (org.eclipse.winery.accountability.model.ModelProvenanceElement)1 TOSCAMetaFile (org.eclipse.winery.model.csar.toscametafile.TOSCAMetaFile)1