Search in sources :

Example 16 with TImport

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;
}
Also used : RelationshipTypeId(org.eclipse.winery.model.ids.definitions.RelationshipTypeId) DefinitionsChildId(org.eclipse.winery.model.ids.definitions.DefinitionsChildId) TImport(org.eclipse.winery.model.tosca.TImport) TExtensibleElements(org.eclipse.winery.model.tosca.TExtensibleElements) ServiceTemplateId(org.eclipse.winery.model.ids.definitions.ServiceTemplateId) TServiceTemplate(org.eclipse.winery.model.tosca.TServiceTemplate) TNodeType(org.eclipse.winery.model.tosca.TNodeType) TRelationshipType(org.eclipse.winery.model.tosca.TRelationshipType) TDefinitions(org.eclipse.winery.model.tosca.TDefinitions) TServiceTemplate(org.eclipse.winery.model.tosca.TServiceTemplate) YTServiceTemplate(org.eclipse.winery.model.tosca.yaml.YTServiceTemplate)

Example 17 with TImport

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
        }
    }
}
Also used : DocumentBasedCsarEntry(org.eclipse.winery.repository.export.entries.DocumentBasedCsarEntry) TEntityType(org.eclipse.winery.model.tosca.TEntityType) QName(javax.xml.namespace.QName) WinerysPropertiesDefinition(org.eclipse.winery.model.tosca.extensions.kvproperties.WinerysPropertiesDefinition) TImport(org.eclipse.winery.model.tosca.TImport) Document(org.w3c.dom.Document)

Aggregations

TImport (org.eclipse.winery.model.tosca.TImport)17 TDefinitions (org.eclipse.winery.model.tosca.TDefinitions)7 IOException (java.io.IOException)5 DefinitionsChildId (org.eclipse.winery.model.ids.definitions.DefinitionsChildId)5 TEntityType (org.eclipse.winery.model.tosca.TEntityType)5 QName (javax.xml.namespace.QName)4 ServiceTemplateId (org.eclipse.winery.model.ids.definitions.ServiceTemplateId)4 RepositoryFileReference (org.eclipse.winery.repository.common.RepositoryFileReference)4 URI (java.net.URI)3 NodeTypeId (org.eclipse.winery.model.ids.definitions.NodeTypeId)3 RelationshipTypeId (org.eclipse.winery.model.ids.definitions.RelationshipTypeId)3 XSDImportId (org.eclipse.winery.model.ids.definitions.imports.XSDImportId)3 TNodeType (org.eclipse.winery.model.tosca.TNodeType)3 TRelationshipType (org.eclipse.winery.model.tosca.TRelationshipType)3 TServiceTemplate (org.eclipse.winery.model.tosca.TServiceTemplate)3 WinerysPropertiesDefinition (org.eclipse.winery.model.tosca.extensions.kvproperties.WinerysPropertiesDefinition)3 Path (java.nio.file.Path)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 RepositoryFileReference (org.eclipse.winery.common.RepositoryFileReference)2