use of org.eclipse.winery.model.csar.toscametafile.TOSCAMetaFile in project winery by eclipse.
the class CsarImporter method importFromDir.
/**
* Import an extracted CSAR from a directory
*
* @param path the root path of an extracted CSAR file
* @param overwrite if true: contents of the repo are overwritten
* @param asyncWPDParsing true if WPD should be parsed asynchronously to speed up the import. Required, because
* JUnit terminates the used ExecutorService
*/
public ImportMetaInformation importFromDir(final Path path, final boolean overwrite, final boolean asyncWPDParsing) throws IOException {
final ImportMetaInformation importMetaInformation = new ImportMetaInformation();
Path toscaMetaPath = path.resolve("TOSCA-Metadata/TOSCA.meta");
if (!Files.exists(toscaMetaPath)) {
importMetaInformation.errors.add("TOSCA.meta does not exist");
return importMetaInformation;
}
final TOSCAMetaFileParser tmfp = new TOSCAMetaFileParser();
final TOSCAMetaFile tmf = tmfp.parse(toscaMetaPath);
if (tmf.getEntryDefinitions() != null) {
// we obey the entry definitions and "just" import that
// imported definitions are added recursively
Path defsPath = path.resolve(tmf.getEntryDefinitions());
importMetaInformation.entryServiceTemplate = this.importDefinitions(tmf, defsPath, importMetaInformation.errors, overwrite, asyncWPDParsing);
this.importSelfServiceMetaData(tmf, path, defsPath, importMetaInformation.errors);
} else {
// no explicit entry definitions found
// we import all available definitions
// The specification says (cos01, Section 16.1, line 2935) that all definitions are contained in the "Definitions" directory
// The alternative is to go through all entries in the TOSCA Meta File, but there is no guarantee that this list is complete
Path definitionsDir = path.resolve("Definitions");
if (!Files.exists(definitionsDir)) {
importMetaInformation.errors.add("No entry definitions defined and Definitions directory does not exist.");
return importMetaInformation;
}
final List<IOException> exceptions = new ArrayList<>();
Files.walkFileTree(definitionsDir, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) {
if (dir.endsWith("Definitions")) {
return FileVisitResult.CONTINUE;
} else {
return FileVisitResult.SKIP_SUBTREE;
}
}
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
try {
CsarImporter.this.importDefinitions(tmf, file, importMetaInformation.errors, overwrite, asyncWPDParsing);
} catch (IOException e) {
exceptions.add(e);
return FileVisitResult.TERMINATE;
}
return FileVisitResult.CONTINUE;
}
});
if (!exceptions.isEmpty()) {
// we rethrow the exception
throw exceptions.get(0);
}
}
this.importNamespacePrefixes(path);
return importMetaInformation;
}
use of org.eclipse.winery.model.csar.toscametafile.TOSCAMetaFile 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);
}
use of org.eclipse.winery.model.csar.toscametafile.TOSCAMetaFile in project winery by eclipse.
the class Converter method convertX2Y.
public InputStream convertX2Y(InputStream csar) {
Path filePath = Utils.unzipFile(csar);
Path fileOutPath = filePath.resolve("tmp");
try {
TOSCAMetaFileParser parser = new TOSCAMetaFileParser();
TOSCAMetaFile metaFile = parser.parse(filePath.resolve("TOSCA-Metadata").resolve("TOSCA.meta"));
org.eclipse.winery.yaml.common.reader.xml.Reader reader = new org.eclipse.winery.yaml.common.reader.xml.Reader();
try {
String fileName = metaFile.getEntryDefinitions();
Definitions definitions = reader.parse(filePath, Paths.get(fileName));
this.convertX2Y(definitions, fileOutPath);
} catch (MultiException e) {
LOGGER.error("Convert TOSCA XML to TOSCA YAML error", e);
}
return Utils.zipPath(fileOutPath);
} catch (Exception e) {
LOGGER.error("Error", e);
throw new AssertionError();
}
}
use of org.eclipse.winery.model.csar.toscametafile.TOSCAMetaFile in project winery by eclipse.
the class CsarImporter method importFromDir.
/**
* Import an extracted CSAR from a directory
*
* @param path the root path of an extracted CSAR file
* @param options the set of options applicable while importing a CSAR
* @param fileMap Contains all files which were extracted from the CSAR and have to be validated using the
* accountability layer
*/
private ImportMetaInformation importFromDir(final Path path, CsarImportOptions options, Map<String, File> fileMap) throws IOException, AccountabilityException, ExecutionException, InterruptedException {
final ImportMetaInformation importMetaInformation = new ImportMetaInformation();
Path toscaMetaPath = path.resolve("TOSCA-Metadata/TOSCA.meta");
if (!Files.exists(toscaMetaPath)) {
importMetaInformation.errors.add("TOSCA.meta does not exist");
return importMetaInformation;
}
final TOSCAMetaFile tmf = parseTOSCAMetaFile(toscaMetaPath);
parseCsarContents(path, tmf, importMetaInformation, options, fileMap);
if (importMetaInformation.errors.isEmpty()) {
importNamespacePrefixes(path);
}
return importMetaInformation;
}
use of org.eclipse.winery.model.csar.toscametafile.TOSCAMetaFile in project winery by eclipse.
the class YamlCsarImporter method importArtifacts.
private void importArtifacts(Path rootPath, TExtensibleElements ci, DefinitionsChildId wid, TOSCAMetaFile tmf, final List<String> errors) {
if (ci instanceof TServiceTemplate) {
TServiceTemplate st = (TServiceTemplate) ci;
if (st.getTopologyTemplate() != null) {
st.getTopologyTemplate().getNodeTemplates().forEach(node -> {
if (Objects.nonNull(node.getArtifacts()) && !node.getArtifacts().isEmpty()) {
node.getArtifacts().stream().map(this::fixForwardSlash).filter(a -> this.isImportable(rootPath, a)).forEach(a -> {
DirectoryId stFilesDir = new GenericDirectoryId(wid, IdNames.FILES_DIRECTORY);
DirectoryId ntFilesDir = new GenericDirectoryId(stFilesDir, node.getId());
DirectoryId artifactDir = new GenericDirectoryId(ntFilesDir, a.getName());
importArtifact(rootPath, a, artifactDir, tmf, errors);
fixArtifactRefName(rootPath, a);
});
}
});
}
} else if (ci instanceof TNodeType) {
TNodeType nt = (TNodeType) ci;
fixOperationImplFileRef(nt);
if (Objects.nonNull(nt.getArtifacts()) && !nt.getArtifacts().isEmpty()) {
nt.getArtifacts().stream().map(this::fixForwardSlash).filter(a -> this.isImportable(rootPath, a)).forEach(a -> {
DirectoryId typeFilesDir = new GenericDirectoryId(wid, IdNames.FILES_DIRECTORY);
DirectoryId artifactDir = new GenericDirectoryId(typeFilesDir, a.getName());
importArtifact(rootPath, a, artifactDir, tmf, errors);
fixArtifactRefName(rootPath, a);
});
}
}
}
Aggregations