use of org.eclipse.winery.repository.datatypes.ids.elements.DirectoryId in project winery by eclipse.
the class NodeTemplateResource method postYamlArtifactFile.
@Path("yamlartifacts/{artifactId}")
public GenericFileResource postYamlArtifactFile(@PathParam("artifactId") String id) {
DirectoryId serviceTemplateYamlArtifactsDir = new GenericDirectoryId(getServiceTemplateResource().getId(), IdNames.FILES_DIRECTORY);
DirectoryId nodeTemplateYamlArtifactsDir = new GenericDirectoryId(serviceTemplateYamlArtifactsDir, nodeTemplate.getId());
DirectoryId yamlArtifactFilesDirectoryId = new GenericDirectoryId(nodeTemplateYamlArtifactsDir, id);
return new GenericFileResource(yamlArtifactFilesDirectoryId);
}
use of org.eclipse.winery.repository.datatypes.ids.elements.DirectoryId in project winery by eclipse.
the class YamlExporter method writeCsar.
/**
* Writes a complete CSAR containing all necessary things reachable from the given service template
*
* @param entryId the id of the service template to export
* @param out the output stream to write to
* @return the TOSCA meta file for the generated Csar
*/
@Override
public String writeCsar(DefinitionsChildId entryId, OutputStream out, Map<String, Object> exportConfiguration) throws IOException, RepositoryCorruptException {
LOGGER.trace("Starting CSAR export with {}", entryId.toString());
Map<CsarContentProperties, CsarEntry> refMap = new HashMap<>();
YamlToscaExportUtil exporter = new YamlToscaExportUtil();
ExportedState exportedState = new ExportedState();
DefinitionsChildId currentId = entryId;
Collection<DefinitionsChildId> referencedIds;
// Process definitions and referenced files
do {
String definitionsPathInsideCSAR = getDefinitionsPathInsideCSAR(repository, currentId);
CsarContentProperties definitionsFileProperties = new CsarContentProperties(definitionsPathInsideCSAR);
if (!YamlRepository.ROOT_TYPE_QNAME.equals(currentId.getQName())) {
referencedIds = exporter.processTOSCA(repository, currentId, definitionsFileProperties, refMap, exportConfiguration);
// for each entryId add license and readme files (if they exist) to the refMap
addLicenseAndReadmeFiles(currentId, refMap);
exportedState.flagAsExported(currentId);
exportedState.flagAsExportRequired(referencedIds);
}
currentId = exportedState.pop();
} while (currentId != null);
// Archive creation
try (final ZipOutputStream zos = new ZipOutputStream(out)) {
// write all referenced files
for (Map.Entry<CsarContentProperties, CsarEntry> entry : refMap.entrySet()) {
CsarContentProperties fileProperties = entry.getKey();
CsarEntry ref = entry.getValue();
LOGGER.trace("Creating {}", fileProperties.getPathInsideCsar());
if (ref instanceof RepositoryRefBasedCsarEntry && ((RepositoryRefBasedCsarEntry) ref).getReference().getParent() instanceof DirectoryId) {
addArtifactTemplateToZipFile(zos, (RepositoryRefBasedCsarEntry) ref, fileProperties);
} else {
addCsarEntryToArchive(zos, ref, fileProperties);
}
}
// create manifest file and add it to archive
return this.addManifest(repository, entryId, refMap, zos, exportConfiguration);
}
}
use of org.eclipse.winery.repository.datatypes.ids.elements.DirectoryId in project winery by eclipse.
the class CsarImporter method adjustArtifactTemplate.
/**
* Adjusts the given artifact template to conform with the repository format
* <p>
* We import the files given at the artifact references
*/
private void adjustArtifactTemplate(Path rootPath, TOSCAMetaFile tmf, ArtifactTemplateId atid, TArtifactTemplate ci, final List<String> errors) {
List<TArtifactReference> refs = ci.getArtifactReferences();
if (refs == null) {
// no references stored - break
return;
}
for (TArtifactReference ref : refs) {
String reference = ref.getReference();
// URLs are stored encoded -> undo the encoding
reference = EncodingUtil.URLdecode(reference);
URI refURI;
try {
refURI = new URI(reference);
} catch (URISyntaxException e) {
errors.add(String.format("Invalid URI %1$s", ref));
continue;
}
if (refURI.isAbsolute()) {
// We have to do nothing
continue;
}
Path path = rootPath.resolve(reference);
if (!Files.exists(path)) {
errors.add(String.format("Reference %1$s not found", reference));
return;
}
Set<Path> allFiles;
if (Files.isRegularFile(path)) {
allFiles = new HashSet<>();
allFiles.add(path);
} else {
if (!Files.isDirectory(path)) {
LOGGER.error("path {} is not a directory", path);
}
Path localRoot = rootPath.resolve(path);
List<TArtifactReference.IncludeOrExclude> includeOrExclude = ref.getIncludeOrExclude();
if (includeOrExclude.get(0) instanceof Exclude) {
// Implicit semantics of an exclude listed first:
// include all files and then exclude the files matched by the pattern
allFiles = this.getAllFiles(localRoot);
} else {
// semantics if include listed as first:
// same as listed at other places
allFiles = new HashSet<>();
}
for (Object object : includeOrExclude) {
if (object instanceof Include) {
this.handleInclude((Include) object, localRoot, allFiles);
} else {
assert (object instanceof Exclude);
this.handleExclude((Exclude) object, localRoot, allFiles);
}
}
}
DirectoryId fileDir = new ArtifactTemplateFilesDirectoryId(atid);
this.importAllFiles(rootPath, allFiles, fileDir, tmf, errors);
}
if (refs.isEmpty()) {
// everything is imported and is a file stored locally
// we don't need the references stored locally: they are generated on the fly when exporting
ci.setArtifactReferences(null);
}
}
use of org.eclipse.winery.repository.datatypes.ids.elements.DirectoryId 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);
});
}
}
}
use of org.eclipse.winery.repository.datatypes.ids.elements.DirectoryId in project winery by eclipse.
the class CsarExporter method writeCsar.
/**
* Writes a complete CSAR containing all necessary things reachable from the given service template
*
* @param entryId the id of the service template to export
* @param out the output stream to write to
* @return the TOSCA meta file for the generated Csar
*/
public String writeCsar(DefinitionsChildId entryId, OutputStream out, Map<String, Object> exportConfiguration) throws IOException, RepositoryCorruptException, InterruptedException, AccountabilityException, ExecutionException {
CsarExporter.LOGGER.trace("Starting CSAR export with {}", entryId.toString());
Map<CsarContentProperties, CsarEntry> refMap = new HashMap<>();
ToscaExportUtil exporter = new ToscaExportUtil();
ExportedState exportedState = new ExportedState();
DefinitionsChildId currentId = entryId;
Collection<DefinitionsChildId> referencedIds;
if (entryId.isSelfContained()) {
exportConfiguration.put(CsarExportConfiguration.INCLUDE_HASHES.name(), true);
}
// Process definitions and referenced files
do {
String definitionsPathInsideCSAR = CsarExporter.getDefinitionsPathInsideCSAR(repository, currentId);
CsarContentProperties definitionsFileProperties = new CsarContentProperties(definitionsPathInsideCSAR);
referencedIds = exporter.processTOSCA(repository, currentId, definitionsFileProperties, refMap, exportConfiguration);
// for each entryId add license and readme files (if they exist) to the refMap
addLicenseAndReadmeFiles(currentId, refMap);
exportedState.flagAsExported(currentId);
exportedState.flagAsExportRequired(referencedIds);
currentId = exportedState.pop();
} while (currentId != null);
// if we export a ServiceTemplate, data for the self-service portal might exist
if (entryId instanceof ServiceTemplateId) {
ServiceTemplateId serviceTemplateId = (ServiceTemplateId) entryId;
this.addSelfServiceMetaData(serviceTemplateId, refMap);
this.addSelfServiceFiles(serviceTemplateId, refMap);
}
this.addNamespacePrefixes(refMap);
// Calculate hashes for referenced files if necessary
if (exportConfiguration.containsKey(CsarExportConfiguration.INCLUDE_HASHES.name())) {
LOGGER.trace("Calculating checksum for {} files.", refMap.size());
calculateFileHashes(refMap);
}
// Store referenced files in immutable file storage if necessary
if (exportConfiguration.containsKey(CsarExportConfiguration.STORE_IMMUTABLY.name())) {
try {
LOGGER.trace("Storing {} files in the immutable file storage", refMap.size());
immutablyStoreRefFiles(refMap);
} catch (InterruptedException | ExecutionException | AccountabilityException e) {
LOGGER.error("Failed to store files in immutable storage. Reason: {}", e.getMessage());
throw e;
}
}
// Archive creation
try (final ZipOutputStream zos = new ZipOutputStream(out)) {
// write all referenced files
for (Map.Entry<CsarContentProperties, CsarEntry> entry : refMap.entrySet()) {
CsarContentProperties fileProperties = entry.getKey();
CsarEntry ref = entry.getValue();
CsarExporter.LOGGER.trace("Creating {}", fileProperties.getPathInsideCsar());
if (ref instanceof RepositoryRefBasedCsarEntry && ((RepositoryRefBasedCsarEntry) ref).getReference().getParent() instanceof DirectoryId) {
addArtifactTemplateToZipFile(zos, (RepositoryRefBasedCsarEntry) ref, fileProperties);
} else {
addCsarEntryToArchive(zos, ref, fileProperties);
}
}
// create manifest file and add it to archive
return this.addManifest(entryId, refMap, zos, exportConfiguration);
}
}
Aggregations