use of org.eclipse.winery.model.tosca.TImport in project winery by eclipse.
the class YamlCsarImporter method processDefinitionsImport.
@Override
protected Optional<ServiceTemplateId> processDefinitionsImport(TDefinitions defs, TOSCAMetaFile tmf, Path definitionsPath, List<String> errors, CsarImportOptions options) throws IOException {
Optional<ServiceTemplateId> entryServiceTemplate = Optional.empty();
String defaultNamespace = defs.getTargetNamespace();
List<TExtensibleElements> componentInstanceList = defs.getServiceTemplateOrNodeTypeOrNodeTypeImplementation();
for (final TExtensibleElements ci : componentInstanceList) {
if (ci instanceof org.eclipse.winery.model.tosca.TServiceTemplate && Objects.isNull(((org.eclipse.winery.model.tosca.TServiceTemplate) ci).getTopologyTemplate())) {
continue;
}
// Determine & ensure that element has the namespace
String namespace = this.getNamespace(ci, defaultNamespace);
this.setNamespace(ci, namespace);
String id = ModelUtilities.getId(ci);
final DefinitionsChildId wid = determineWineryId(ci, namespace, id);
if (targetRepository.exists(wid)) {
if (options.isOverwrite()) {
targetRepository.forceDelete(wid);
String msg = String.format("Deleted %1$s %2$s to enable replacement", ci.getClass().getName(), wid.getQName().toString());
LOGGER.debug(msg);
} else {
String msg = String.format("Skipped %1$s %2$s, because it already exists", ci.getClass().getName(), wid.getQName().toString());
LOGGER.debug(msg);
// this is not displayed in the UI as we currently do not distinguish between pre-existing types and types created during the import.
continue;
}
}
// Create a fresh definitions object without the other data.
final TDefinitions newDefs = BackendUtils.createWrapperDefinitions(wid, targetRepository);
// add the current TExtensibleElements as the only content to it
newDefs.getServiceTemplateOrNodeTypeOrNodeTypeImplementation().add(ci);
// import license and readme files
importLicenseAndReadme(definitionsPath.getParent().getParent(), wid, tmf, errors);
importArtifacts(definitionsPath.getParent().getParent(), ci, wid, tmf, errors);
if (ci instanceof TNodeType) {
this.adjustNodeType(definitionsPath.getParent().getParent(), (TNodeType) ci, (NodeTypeId) wid, tmf, errors);
} else if (ci instanceof TRelationshipType) {
this.adjustRelationshipType(definitionsPath.getParent().getParent(), (TRelationshipType) ci, (RelationshipTypeId) wid, tmf, errors);
}
storeDefs(wid, newDefs);
}
List<TImport> imports = defs.getImport();
this.importImports(definitionsPath.getParent(), tmf, imports, errors, options);
return entryServiceTemplate;
}
use of org.eclipse.winery.model.tosca.TImport in project winery by eclipse.
the class ToscaExportUtil method exportEntityType.
protected void exportEntityType(TDefinitions entryDefinitions, URI uri, DefinitionsChildId tcId) {
TEntityType entityType = (TEntityType) entryDefinitions.getElement();
// we have an entity type with a possible properties definition
WinerysPropertiesDefinition wpd = entityType.getWinerysPropertiesDefinition();
if (wpd != null) {
if (wpd.getIsDerivedFromXSD() == null) {
// Write WPD only to file if it exists and is NOT derived from an XSD (which may happen during import)
String wrapperElementNamespace = wpd.getNamespace();
String wrapperElementLocalName = wpd.getElementName();
// BEGIN: add import and put into CSAR
TImport imp = new TImport();
entryDefinitions.getImport().add(imp);
// fill known import values
imp.setImportType(XMLConstants.W3C_XML_SCHEMA_NS_URI);
imp.setNamespace(wrapperElementNamespace);
// add "winerysPropertiesDefinition" flag to import tag to support
Map<QName, String> otherAttributes = imp.getOtherAttributes();
otherAttributes.put(QNames.QNAME_WINERYS_PROPERTIES_DEFINITION_ATTRIBUTE, "true");
// Determine location
String loc = BackendUtils.getImportLocationForWinerysPropertiesDefinitionXSD((EntityTypeId) tcId, uri, wrapperElementLocalName);
if (uri == null) {
ToscaExportUtil.LOGGER.trace("CSAR Export mode. Putting XSD into CSAR");
// CSAR Export mode
// XSD has to be put into the CSAR
Document document = ModelUtilities.getWinerysPropertiesDefinitionXsdAsDocument(wpd);
// loc in import is URL encoded, loc on filesystem isn't
String locInCSAR = EncodingUtil.URLdecode(loc);
// furthermore, the path has to start from the root of the CSAR; currently, it starts from Definitions/
locInCSAR = locInCSAR.substring(3);
ToscaExportUtil.LOGGER.trace("Location in CSAR: {}", locInCSAR);
CsarContentProperties csarContentProperties = new CsarContentProperties(locInCSAR);
this.referencesToPathInCSARMap.put(csarContentProperties, new DocumentBasedCsarEntry(document));
}
imp.setLocation(loc);
// END: add import and put into CSAR
// BEGIN: generate TOSCA conforming PropertiesDefinition
// Winerys properties definitions are serialized as an XSD.
// As such their TOSCA representation is a type reference, so we generate that here.
// Of course that breaks when we export as YAML, but this exporter basically targets XML in the first place
// TODO: future work — deal with WPD when exporting to YAML
// (a map[string, string] can be represented as a TDataType)
TEntityType.XmlTypeDefinition propertiesDefinition = new TEntityType.XmlTypeDefinition();
propertiesDefinition.setType(new QName(wrapperElementNamespace, wrapperElementLocalName));
entityType.setProperties(propertiesDefinition);
// END: generate TOSCA conforming PropertiesDefinition
} else {
// otherwise WPD exists, but is derived from XSD
// we DO NOT have to remove the winery properties definition from the output to allow "debugging" of the CSAR
}
}
}
Aggregations