Search in sources :

Example 1 with RecoveringManifestParser

use of org.eclipse.virgo.util.parser.manifest.RecoveringManifestParser 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 RecoveringManifestParser

use of org.eclipse.virgo.util.parser.manifest.RecoveringManifestParser 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 RecoveringManifestParser

use of org.eclipse.virgo.util.parser.manifest.RecoveringManifestParser 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 4 with RecoveringManifestParser

use of org.eclipse.virgo.util.parser.manifest.RecoveringManifestParser 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)

Example 5 with RecoveringManifestParser

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

the class AccountabilityManagerImpl method getHistoryOfSingleFile.

protected List<FileProvenanceElement> getHistoryOfSingleFile(List<ModelProvenanceElement> historyElements, String fileId, AuthorizationInfo authorizationInfo) {
    List<FileProvenanceElement> history = new ArrayList<>();
    if (Objects.nonNull(historyElements) && historyElements.size() > 0) {
        for (ModelProvenanceElement modelProvenanceElement : historyElements) {
            if (Objects.nonNull(modelProvenanceElement.getFingerprint())) {
                ManifestContents manifestContents = new RecoveringManifestParser().parse(modelProvenanceElement.getFingerprint());
                for (String name : manifestContents.getSectionNames()) {
                    if (name.equals(fileId)) {
                        modelProvenanceElement.setAuthorizedFlag(authorizationInfo);
                        modelProvenanceElement.setAuthorName(authorizationInfo.getRealWorldIdentity(modelProvenanceElement.getAuthorAddress()).orElseGet(String::new));
                        FileProvenanceElement currentFile = new FileProvenanceElement(modelProvenanceElement);
                        currentFile.setAddressInImmutableStorage(manifestContents.getAttributesForSection(name).get(TOSCAMetaFileAttributes.IMMUTABLE_ADDRESS));
                        currentFile.setFileHash(TOSCAMetaFileAttributes.HASH + "-" + manifestContents.getAttributesForSection(name).get(TOSCAMetaFileAttributes.HASH));
                        currentFile.setFileName(fileId);
                        history.add(currentFile);
                        break;
                    }
                }
            }
        }
    }
    if (history.size() > 0)
        return history;
    return null;
}
Also used : ManifestContents(org.eclipse.virgo.util.parser.manifest.ManifestContents) RecoveringManifestParser(org.eclipse.virgo.util.parser.manifest.RecoveringManifestParser) ModelProvenanceElement(org.eclipse.winery.accountability.model.ModelProvenanceElement) ArrayList(java.util.ArrayList) FileProvenanceElement(org.eclipse.winery.accountability.model.FileProvenanceElement)

Aggregations

ManifestContents (org.eclipse.virgo.util.parser.manifest.ManifestContents)6 RecoveringManifestParser (org.eclipse.virgo.util.parser.manifest.RecoveringManifestParser)6 ArrayList (java.util.ArrayList)3 ZipEntry (java.util.zip.ZipEntry)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 InputStream (java.io.InputStream)2 HashMap (java.util.HashMap)2 ZipInputStream (java.util.zip.ZipInputStream)2 FileProvenanceElement (org.eclipse.winery.accountability.model.FileProvenanceElement)2 ServiceTemplateId (org.eclipse.winery.model.ids.definitions.ServiceTemplateId)2 Test (org.junit.jupiter.api.Test)2 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 TOSCAMetaFileParser (org.eclipse.winery.model.csar.toscametafile.TOSCAMetaFileParser)1