Search in sources :

Example 11 with TImport

use of org.eclipse.winery.model.tosca.TImport in project winery by eclipse.

the class ImportUtils method getTheImport.

/**
 * SIDE EFFECT: persists the import when something is changed
 */
public static Optional<TImport> getTheImport(IRepository repository, GenericImportId id) {
    Objects.requireNonNull(id);
    TImport theImport;
    boolean needsPersistence = false;
    final TDefinitions definitions = repository.getDefinitions(id);
    if (!repository.exists(id)) {
        return Optional.empty();
    }
    if (definitions.getServiceTemplateOrNodeTypeOrNodeTypeImplementation().isEmpty()) {
        // definitions exist
        // we have to manually assign our import right
        theImport = definitions.getImport().get(0);
    } else {
        // someone created a new import
        // store it locally
        theImport = (TImport) definitions.getElement();
        // undo the side effect of adding it at the wrong place at TDefinitions
        definitions.getServiceTemplateOrNodeTypeOrNodeTypeImplementation().clear();
        // add import at the right place
        definitions.getImport().add(theImport);
        // Super class has persisted the definitions
        // We have to persist the new variant
        needsPersistence = true;
    }
    if (theImport.getLocation() == null) {
        // invalid import -- try to synchronize with storage
        SortedSet<RepositoryFileReference> containedFiles = repository.getContainedFiles(id);
        // we are only interested in the non-.definitions
        for (RepositoryFileReference ref : containedFiles) {
            if (!ref.getFileName().endsWith(".definitions")) {
                // associated file found
                // set the filename of the import to the found xsd
                // TODO: no more validity checks are done currently. In the case of XSD: targetNamespace matches, not more than one xsd
                theImport.setLocation(ref.getFileName());
                needsPersistence = true;
                break;
            }
        }
    }
    if (needsPersistence) {
        try {
            BackendUtils.persist(repository, id, definitions);
        } catch (IOException e) {
            LOGGER.error("Could not persist changes", e);
        }
    }
    return Optional.of(theImport);
}
Also used : RepositoryFileReference(org.eclipse.winery.repository.common.RepositoryFileReference) TImport(org.eclipse.winery.model.tosca.TImport) IOException(java.io.IOException) TDefinitions(org.eclipse.winery.model.tosca.TDefinitions)

Example 12 with TImport

use of org.eclipse.winery.model.tosca.TImport in project winery by eclipse.

the class XSDImportResource method createNewElement.

@Override
protected TExtensibleElements createNewElement() {
    TImport imp = new TImport();
    imp.setImportType(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    return imp;
}
Also used : TImport(org.eclipse.winery.model.tosca.TImport)

Example 13 with TImport

use of org.eclipse.winery.model.tosca.TImport in project winery by eclipse.

the class GenericImportResource method getFile.

@GET
@Path("{filename}")
public Response getFile(@PathParam("filename") @NonNull final String encodedFileName) {
    Objects.requireNonNull(encodedFileName);
    final Optional<TImport> theImport = ImportUtils.getTheImport(RepositoryFactory.getRepository(), (GenericImportId) id);
    if (!theImport.isPresent()) {
        return Response.status(Status.NOT_FOUND).build();
    }
    @Nullable final String location = theImport.get().getLocation();
    @NonNull String fileName = EncodingUtil.URLdecode(encodedFileName);
    if (!fileName.equals(location)) {
        LOGGER.debug("Filename mismatch %s vs %s", fileName, location);
        return Response.status(Status.NOT_FOUND).build();
    }
    RepositoryFileReference ref = new RepositoryFileReference(this.id, location);
    return RestUtils.returnRepoPath(ref, null);
}
Also used : RepositoryFileReference(org.eclipse.winery.repository.common.RepositoryFileReference) NonNull(org.eclipse.jdt.annotation.NonNull) TImport(org.eclipse.winery.model.tosca.TImport) Nullable(org.eclipse.jdt.annotation.Nullable) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Example 14 with TImport

use of org.eclipse.winery.model.tosca.TImport in project winery by eclipse.

the class ToCanonical method convert.

public TDataType convert(YTDataType node, String id) {
    String name = fixNamespaceDuplication(id, node.getMetadata().get("targetNamespace"));
    TDataType.Builder builder = new TDataType.Builder(name).addConstraints(convertList(node.getConstraints(), this::convert));
    fillEntityTypeProperties(node, builder);
    TDataType result = builder.build();
    // FIXME need to actually transform the node.getProperties() to an xml schema
    // to be able to import it and add a PropertiesDefinition reference to that schema
    String namespace = this.namespace;
    if (namespace == null) {
        // attempt to patch namespace with the definitions' targetNamespace
        namespace = result.getTargetNamespace();
    }
    if (namespace == null) {
        LOGGER.warn("Could not determine namespace for DataType {}. Imports may be incorrect!", id);
        return result;
    }
    TImport importDefinition = new TImport.Builder(Namespaces.XML_NS).setLocation(EncodingUtil.URLencode(namespace) + ".xsd").setNamespace(namespace).build();
    if (!this.imports.contains(importDefinition)) {
        this.imports.add(importDefinition);
    }
    return result;
}
Also used : TImport(org.eclipse.winery.model.tosca.TImport) TDataType(org.eclipse.winery.model.tosca.TDataType) YTDataType(org.eclipse.winery.model.tosca.yaml.YTDataType)

Example 15 with TImport

use of org.eclipse.winery.model.tosca.TImport in project winery by eclipse.

the class YamlToscaExportUtil method processDefinitionsElement.

@Override
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.";
        LOGGER.error(error);
        throw new RepositoryCorruptException(error);
    }
    TDefinitions entryDefinitions = repository.getDefinitions(tcId);
    this.getPrepareForExport(repository, tcId, entryDefinitions);
    Collection<DefinitionsChildId> referencedDefinitionsChildIds = repository.getReferencedDefinitionsChildIds(tcId);
    if (!EXPORT_NORMATIVE_TYPES) {
        referencedDefinitionsChildIds.removeIf(id -> id.getQName().getNamespaceURI().startsWith("tosca"));
    }
    // adjust imports: add imports of definitions
    Collection<TImport> imports = new ArrayList<>();
    Map<String, QName> importDefinitions = new HashMap<>();
    for (DefinitionsChildId id : referencedDefinitionsChildIds) {
        this.addToImports(repository, id, imports);
        String defName = YamlExporter.getDefinitionsName(repository, id).concat(Constants.SUFFIX_TOSCA_DEFINITIONS);
        importDefinitions.put(defName, id.getQName());
    }
    entryDefinitions.getImport().addAll(imports);
    entryDefinitions.setImportDefinitions(importDefinitions);
    // END: Definitions modification
    YAMLDefinitionsBasedCsarEntry entry = new YAMLDefinitionsBasedCsarEntry(repository, entryDefinitions);
    // Custom Adjustments for Service Templates
    YamlExportAdjustmentsBuilder adjustmentsBuilder = new YamlExportAdjustmentsBuilder(entry);
    if (!EXPORT_NORMATIVE_TYPES) {
        adjustmentsBuilder.removeNormativeTypeImports();
    }
    entry = adjustmentsBuilder.setMetadataName(tcId).build();
    this.referencesToPathInCSARMap.put(definitionsFileProperties, entry);
    return referencedDefinitionsChildIds;
}
Also used : DefinitionsChildId(org.eclipse.winery.model.ids.definitions.DefinitionsChildId) HashMap(java.util.HashMap) QName(javax.xml.namespace.QName) TImport(org.eclipse.winery.model.tosca.TImport) ArrayList(java.util.ArrayList) YAMLDefinitionsBasedCsarEntry(org.eclipse.winery.repository.yaml.export.entries.YAMLDefinitionsBasedCsarEntry) RepositoryCorruptException(org.eclipse.winery.repository.exceptions.RepositoryCorruptException) TDefinitions(org.eclipse.winery.model.tosca.TDefinitions)

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