Skip to content

Commit

Permalink
Add initial YAML export version
Browse files Browse the repository at this point in the history
Signed-off-by: Vladimir Yussupov <v.yussupov@gmail.com>
  • Loading branch information
v-yussupov committed Feb 7, 2020
1 parent 5290066 commit cfc4d95
Show file tree
Hide file tree
Showing 8 changed files with 410 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@
import org.eclipse.winery.repository.export.CsarExportOptions;
import org.eclipse.winery.repository.export.CsarExporter;
import org.eclipse.winery.repository.export.ToscaExportUtil;
import org.eclipse.winery.repository.export.YamlExporter;
import org.eclipse.winery.repository.rest.datatypes.LocalNameForAngular;
import org.eclipse.winery.repository.rest.datatypes.NamespaceAndDefinedLocalNamesForAngular;
import org.eclipse.winery.repository.rest.resources._support.AbstractComponentInstanceResource;
Expand Down Expand Up @@ -269,6 +270,29 @@ public static Response getYamlOfSelectedResource(DefinitionsChildId id) {
}

public static Response getYamlCSARofSelectedResource(final AbstractComponentInstanceResource resource) {
LocalDateTime start = LocalDateTime.now();
final YamlExporter exporter = new YamlExporter();
Map<String, Object> exportConfiguration = new HashMap<>();

StreamingOutput so = output -> {
try {
exporter.writeCsar(RepositoryFactory.getRepository(), resource.getId(), output, exportConfiguration);
LOGGER.debug("CSAR export lasted {}", Duration.between(LocalDateTime.now(), start).toString());
} catch (Exception e) {
LOGGER.error("Error while exporting CSAR", e);
throw new WebApplicationException(e);
}
};
String contentDisposition = String.format("attachment;filename=\"%s%s\"",
resource.getXmlId().getEncoded(),
Constants.SUFFIX_CSAR);

return Response.ok()
.header("Content-Disposition", contentDisposition)
.type(MimeTypes.MIMETYPE_ZIP)
.entity(so)
.build();

// TODO
// final Converter converter = new Converter();
// StreamingOutput so = output -> {
Expand All @@ -294,7 +318,7 @@ public static Response getYamlCSARofSelectedResource(final AbstractComponentInst
// sb.append(Constants.SUFFIX_CSAR);
// sb.append("\"");
// return Response.ok().header("Content-Disposition", sb.toString()).type(MimeTypes.MIMETYPE_ZIP).entity(so).build();
return Response.noContent().build();
// return Response.noContent().build();
}

public static Response getEdmmModel(TServiceTemplate element) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,9 @@ public InputStream writeToInputStream(TServiceTemplate serviceTemplate) {
String output = this.visit(serviceTemplate, new Parameter(0)).toString();
return new ByteArrayInputStream(output.getBytes());
} catch (Exception e) {
return null;
LOGGER.error(e.getLocalizedMessage());
}
return null;
}

public void write(TServiceTemplate serviceTemplate, Path fileName) {
Expand Down Expand Up @@ -421,7 +422,7 @@ public YamlPrinter visit(TParameterDefinition node, Parameter parameter) {
.print(printVisitorNode(node.getEntrySchema(), parameter))
.printKeyObject(parameter.getKey(), node.getValue());
}

public YamlPrinter visit(TConstraintClause node, Parameter parameter) {
if (node.getValue() != null) {
return new YamlPrinter(parameter.getIndent())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
import org.eclipse.winery.repository.datatypes.ids.elements.ServiceTemplateSelfServiceFilesDirectoryId;
import org.eclipse.winery.repository.exceptions.RepositoryCorruptException;
import org.eclipse.winery.repository.export.entries.CsarEntry;
import org.eclipse.winery.repository.export.entries.DefinitionsBasedCsarEntry;
import org.eclipse.winery.repository.export.entries.XMLDefinitionsBasedCsarEntry;
import org.eclipse.winery.repository.export.entries.DocumentBasedCsarEntry;
import org.eclipse.winery.repository.export.entries.RepositoryRefBasedCsarEntry;

Expand Down Expand Up @@ -260,8 +260,8 @@ private void immutablyStoreRefFiles(Map<CsarContentProperties, CsarEntry> filesT
* @param fileProperties Describing the path to the file inside the archive
* @throws IOException thrown when the temporary directory can not be created
*/
private void addArtifactTemplateToZipFile(ZipOutputStream zos, RepositoryRefBasedCsarEntry csarEntry,
CsarContentProperties fileProperties) throws IOException {
protected void addArtifactTemplateToZipFile(ZipOutputStream zos, RepositoryRefBasedCsarEntry csarEntry,
CsarContentProperties fileProperties) throws IOException {
GitInfo gitInfo = BackendUtils.getGitInformation((DirectoryId) csarEntry.getReference().getParent());

if (gitInfo == null) {
Expand Down Expand Up @@ -519,7 +519,7 @@ private void putRefIntoRefMap(String targetDir, Map<CsarContentProperties, CsarE
}
}

private void addLicenseAndReadmeFiles(IRepository repository, DefinitionsChildId entryId, Map<CsarContentProperties, CsarEntry> refMap) {
protected void addLicenseAndReadmeFiles(IRepository repository, DefinitionsChildId entryId, Map<CsarContentProperties, CsarEntry> refMap) {
final RepositoryFileReference licenseRef = new RepositoryFileReference(entryId, Constants.LICENSE_FILE_NAME);
if (repository.exists(licenseRef)) {
refMap.put(new CsarContentProperties(BackendUtils.getPathInsideRepo(licenseRef)), new RepositoryRefBasedCsarEntry(licenseRef));
Expand Down Expand Up @@ -551,7 +551,7 @@ private void addSelfServiceFiles(IRepository repository, ServiceTemplateId servi
}

private String addManifest(IRepository repository, DefinitionsChildId id, Map<CsarContentProperties, CsarEntry> refMap,
ZipOutputStream out, Map<String, Object> exportConfiguration) throws IOException {
ZipOutputStream out, Map<String, Object> exportConfiguration) throws IOException {
String entryDefinitionsReference = CsarExporter.getDefinitionsPathInsideCSAR(repository, id);

out.putNextEntry(new ZipEntry("TOSCA-Metadata/TOSCA.meta"));
Expand Down Expand Up @@ -587,7 +587,7 @@ private String addManifest(IRepository repository, DefinitionsChildId id, Map<Cs

if (csarEntry instanceof DocumentBasedCsarEntry) {
mimeType = MimeTypes.MIMETYPE_XSD;
} else if (csarEntry instanceof DefinitionsBasedCsarEntry) {
} else if (csarEntry instanceof XMLDefinitionsBasedCsarEntry) {
mimeType = MimeTypes.MIMETYPE_TOSCA_DEFINITIONS;
} else {
mimeType = repository.getMimeType(((RepositoryRefBasedCsarEntry) csarEntry).getReference());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@
import org.eclipse.winery.repository.datatypes.ids.elements.VisualAppearanceId;
import org.eclipse.winery.repository.exceptions.RepositoryCorruptException;
import org.eclipse.winery.repository.export.entries.CsarEntry;
import org.eclipse.winery.repository.export.entries.DefinitionsBasedCsarEntry;
import org.eclipse.winery.repository.export.entries.DocumentBasedCsarEntry;
import org.eclipse.winery.repository.export.entries.RepositoryRefBasedCsarEntry;
import org.eclipse.winery.repository.export.entries.XMLDefinitionsBasedCsarEntry;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -73,19 +73,19 @@ public class ToscaExportUtil {
// collects the references to be put in the CSAR and the assigned path in
// the CSAR MANIFEST
// this allows to use other paths in the CSAR than on the local storage
private Map<CsarContentProperties, CsarEntry> referencesToPathInCSARMap = null;
protected Map<CsarContentProperties, CsarEntry> referencesToPathInCSARMap = null;

/**
* Currently a very simple approach to configure the export
*/
private Map<String, Object> exportConfiguration;
protected Map<String, Object> exportConfiguration;

public void writeTOSCA(IRepository repository, DefinitionsChildId id,
Map<String, Object> conf, OutputStream outputStream)
throws RepositoryCorruptException, IOException {
this.processTOSCA(repository, id, new CsarContentProperties(id.getQName().toString()), conf);
CsarEntry csarEntry = this.referencesToPathInCSARMap.values().stream()
.filter(entry -> entry instanceof DefinitionsBasedCsarEntry)
.filter(entry -> entry instanceof XMLDefinitionsBasedCsarEntry)
.findFirst()
.orElseThrow(() -> new RepositoryCorruptException("Definition not found!"));
csarEntry.writeToOutputStream(outputStream);
Expand Down Expand Up @@ -123,8 +123,8 @@ private void checkConfig(ExportProperties propKey, Boolean bo) {
}

/**
* Completes the tosca xml in preparation to write it. Additionally, a the artifactMap is filled to
* enable the CSAR exporter to create necessary entries in TOSCA-Meta and to add them to the CSAR itself
* Completes the tosca xml in preparation to write it. Additionally, a the artifactMap is filled to enable the CSAR
* exporter to create necessary entries in TOSCA-Meta and to add them to the CSAR itself
*
* @param id the component instance to export
* @param exportConfiguration Configures the exporter
Expand All @@ -144,7 +144,7 @@ protected Collection<DefinitionsChildId> processTOSCA(IRepository repository, De
* @return a collection of DefinitionsChildIds referenced by the given component
* @throws RepositoryCorruptException if tcId does not exist
*/
private Collection<DefinitionsChildId> processDefinitionsElement(IRepository repository, DefinitionsChildId tcId, CsarContentProperties definitionsFileProperties)
protected Collection<DefinitionsChildId> processDefinitionsElement(IRepository repository, DefinitionsChildId tcId, CsarContentProperties definitionsFileProperties)
throws RepositoryCorruptException, IOException {
if (!repository.exists(tcId)) {
String error = "Component instance " + tcId.toReadableString() + " does not exist.";
Expand Down Expand Up @@ -207,12 +207,12 @@ private Collection<DefinitionsChildId> processDefinitionsElement(IRepository rep

// END: Definitions modification

this.referencesToPathInCSARMap.put(definitionsFileProperties, new DefinitionsBasedCsarEntry(entryDefinitions));
this.referencesToPathInCSARMap.put(definitionsFileProperties, new XMLDefinitionsBasedCsarEntry(entryDefinitions));

return referencedDefinitionsChildIds;
}

private void exportEntityType(Definitions entryDefinitions, URI uri, DefinitionsChildId tcId) {
protected void exportEntityType(Definitions entryDefinitions, URI uri, DefinitionsChildId tcId) {
TEntityType entityType = (TEntityType) entryDefinitions.getElement();

// we have an entity type with a possible properties definition
Expand Down Expand Up @@ -291,7 +291,7 @@ private void getPrepareForExport(IRepository repository, DefinitionsChildId id)
/**
* Adds the given id as import to the given imports collection
*/
private void addToImports(IRepository repository, DefinitionsChildId id, Collection<TImport> imports) {
protected void addToImports(IRepository repository, DefinitionsChildId id, Collection<TImport> imports) {
TImport imp = new TImport();
imp.setImportType(Namespaces.TOSCA_NAMESPACE);
imp.setNamespace(id.getNamespace().getDecoded());
Expand Down Expand Up @@ -326,7 +326,7 @@ private void addToImports(IRepository repository, DefinitionsChildId id, Collect
/**
* Synchronizes the plan model references and adds the plans to the csar (putRefAsReferencedItemInCsar)
*/
private void prepareForExport(IRepository repository, ServiceTemplateId id) throws IOException {
protected void prepareForExport(IRepository repository, ServiceTemplateId id) throws IOException {
// ensure that the plans stored locally are the same ones as stored in the definitions
BackendUtils.synchronizeReferences(id);

Expand All @@ -349,7 +349,7 @@ private void prepareForExport(IRepository repository, ServiceTemplateId id) thro
*
* @return a collection of referenced definition child Ids
*/
private void prepareForExport(IRepository repository, ArtifactTemplateId id) throws RepositoryCorruptException, IOException {
protected void prepareForExport(IRepository repository, ArtifactTemplateId id) throws RepositoryCorruptException, IOException {
// Export files

// This method is called BEFORE the concrete definitions element is written.
Expand All @@ -375,7 +375,7 @@ private void prepareForExport(IRepository repository, ArtifactTemplateId id) thr
* <p>
* Thereby, it uses the global variable referencesToPathInCSARMap
*/
private void putRefAsReferencedItemInCsar(RepositoryFileReference ref) {
protected void putRefAsReferencedItemInCsar(RepositoryFileReference ref) {
// Determine path
String pathInsideRepo = BackendUtils.getPathInsideRepo(ref);

Expand All @@ -384,7 +384,7 @@ private void putRefAsReferencedItemInCsar(RepositoryFileReference ref) {
this.referencesToPathInCSARMap.put(new CsarContentProperties(pathInsideRepo), new RepositoryRefBasedCsarEntry(ref));
}

private void addVisualAppearanceToCSAR(IRepository repository, TopologyGraphElementEntityTypeId id) {
protected void addVisualAppearanceToCSAR(IRepository repository, TopologyGraphElementEntityTypeId id) {
VisualAppearanceId visId = new VisualAppearanceId(id);
if (repository.exists(visId)) {
// we do NOT check for the id, but simply check for bigIcon.png (only exists in NodeType) and smallIcon.png (exists in NodeType and RelationshipType)
Expand Down
Loading

0 comments on commit cfc4d95

Please sign in to comment.