Search in sources :

Example 1 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(GenericImportId id) {
    Objects.requireNonNull(id);
    TImport theImport;
    boolean needsPersistence = false;
    final IRepository repository = RepositoryFactory.getRepository();
    final Definitions 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(id, definitions);
        } catch (IOException e) {
            LOGGER.error("Could not persist changes", e);
        }
    }
    return Optional.of(theImport);
}
Also used : RepositoryFileReference(org.eclipse.winery.common.RepositoryFileReference) Definitions(org.eclipse.winery.model.tosca.Definitions) TImport(org.eclipse.winery.model.tosca.TImport) IOException(java.io.IOException)

Example 2 with TImport

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

the class BackendUtils method createWrapperDefinitionsAndInitialEmptyElement.

public static TDefinitions createWrapperDefinitionsAndInitialEmptyElement(IRepository repository, DefinitionsChildId id) {
    final TDefinitions definitions = createWrapperDefinitions(id, repository);
    HasIdInIdOrNameField element;
    if (id instanceof RelationshipTypeImplementationId) {
        element = new TRelationshipTypeImplementation();
    } else if (id instanceof NodeTypeImplementationId) {
        element = new TNodeTypeImplementation();
    } else if (id instanceof RequirementTypeId) {
        element = new TRequirementType();
    } else if (id instanceof NodeTypeId) {
        element = new TNodeType();
    } else if (id instanceof RelationshipTypeId) {
        element = new TRelationshipType();
    } else if (id instanceof CapabilityTypeId) {
        element = new TCapabilityType();
    } else if (id instanceof DataTypeId) {
        element = new TDataType();
    } else if (id instanceof ArtifactTypeId) {
        element = new TArtifactType();
    } else if (id instanceof PolicyTypeId) {
        element = new TPolicyType();
    } else if (id instanceof PolicyTemplateId) {
        element = new TPolicyTemplate();
    } else if (id instanceof ServiceTemplateId) {
        element = new TServiceTemplate();
    } else if (id instanceof ArtifactTemplateId) {
        element = new TArtifactTemplate();
    } else if (id instanceof ComplianceRuleId) {
        element = new OTComplianceRule(new OTComplianceRule.Builder(id.getXmlId().getDecoded()));
    } else if (id instanceof PatternRefinementModelId) {
        element = new OTPatternRefinementModel(new OTPatternRefinementModel.Builder());
    } else if (id instanceof TopologyFragmentRefinementModelId) {
        element = new OTTopologyFragmentRefinementModel(new OTPatternRefinementModel.Builder());
    } else if (id instanceof TestRefinementModelId) {
        element = new OTTestRefinementModel(new OTTestRefinementModel.Builder());
    } else if (id instanceof InterfaceTypeId) {
        element = new TInterfaceType();
    } else if (id instanceof XSDImportId) {
        // TImport has no id; thus directly generating it without setting an id
        TImport tImport = new TImport();
        definitions.setElement(tImport);
        return definitions;
    } else {
        throw new IllegalStateException("Unhandled id branch. Could happen for XSDImportId");
    }
    copyIdToFields(element, id);
    definitions.setElement((TExtensibleElements) element);
    return definitions;
}
Also used : OTTopologyFragmentRefinementModel(org.eclipse.winery.model.tosca.extensions.OTTopologyFragmentRefinementModel) ArtifactTypeId(org.eclipse.winery.model.ids.definitions.ArtifactTypeId) TArtifactTemplate(org.eclipse.winery.model.tosca.TArtifactTemplate) TestRefinementModelId(org.eclipse.winery.model.ids.extensions.TestRefinementModelId) TImport(org.eclipse.winery.model.tosca.TImport) TCapabilityType(org.eclipse.winery.model.tosca.TCapabilityType) TDataType(org.eclipse.winery.model.tosca.TDataType) OTTestRefinementModel(org.eclipse.winery.model.tosca.extensions.OTTestRefinementModel) TRequirementType(org.eclipse.winery.model.tosca.TRequirementType) TNodeTypeImplementation(org.eclipse.winery.model.tosca.TNodeTypeImplementation) HasIdInIdOrNameField(org.eclipse.winery.model.tosca.HasIdInIdOrNameField) ComplianceRuleId(org.eclipse.winery.model.ids.extensions.ComplianceRuleId) InterfaceTypeId(org.eclipse.winery.model.ids.definitions.InterfaceTypeId) TArtifactType(org.eclipse.winery.model.tosca.TArtifactType) TPolicyType(org.eclipse.winery.model.tosca.TPolicyType) TopologyFragmentRefinementModelId(org.eclipse.winery.model.ids.extensions.TopologyFragmentRefinementModelId) TDefinitions(org.eclipse.winery.model.tosca.TDefinitions) TServiceTemplate(org.eclipse.winery.model.tosca.TServiceTemplate) NodeTypeImplementationId(org.eclipse.winery.model.ids.definitions.NodeTypeImplementationId) XSDImportId(org.eclipse.winery.model.ids.definitions.imports.XSDImportId) RelationshipTypeId(org.eclipse.winery.model.ids.definitions.RelationshipTypeId) CapabilityTypeId(org.eclipse.winery.model.ids.definitions.CapabilityTypeId) RelationshipTypeImplementationId(org.eclipse.winery.model.ids.definitions.RelationshipTypeImplementationId) ServiceTemplateId(org.eclipse.winery.model.ids.definitions.ServiceTemplateId) ArtifactTemplateId(org.eclipse.winery.model.ids.definitions.ArtifactTemplateId) TNodeType(org.eclipse.winery.model.tosca.TNodeType) TRelationshipType(org.eclipse.winery.model.tosca.TRelationshipType) TPolicyTemplate(org.eclipse.winery.model.tosca.TPolicyTemplate) PatternRefinementModelId(org.eclipse.winery.model.ids.extensions.PatternRefinementModelId) PolicyTypeId(org.eclipse.winery.model.ids.definitions.PolicyTypeId) TRelationshipTypeImplementation(org.eclipse.winery.model.tosca.TRelationshipTypeImplementation) RequirementTypeId(org.eclipse.winery.model.ids.definitions.RequirementTypeId) DataTypeId(org.eclipse.winery.model.ids.definitions.DataTypeId) TInterfaceType(org.eclipse.winery.model.tosca.TInterfaceType) PolicyTemplateId(org.eclipse.winery.model.ids.definitions.PolicyTemplateId) NodeTypeId(org.eclipse.winery.model.ids.definitions.NodeTypeId) OTComplianceRule(org.eclipse.winery.model.tosca.extensions.OTComplianceRule) OTPatternRefinementModel(org.eclipse.winery.model.tosca.extensions.OTPatternRefinementModel)

Example 3 with TImport

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

the class CsarImporter method importImports.

/**
 * @param basePath the base path where to resolve files from. This is the directory of the Definitions
 * @param imports  the list of imports to import. SIDE EFFECT: this list is modified. After this method has run, the
 *                 list contains the imports to be put into the wrapper element
 * @param options  the set of options applicable while importing a CSAR
 */
private void importImports(Path basePath, TOSCAMetaFile tmf, List<TImport> imports, final List<String> errors, CsarImportOptions options) throws IOException {
    for (Iterator<TImport> iterator = imports.iterator(); iterator.hasNext(); ) {
        TImport imp = iterator.next();
        String importType = imp.getImportType();
        String namespace = imp.getNamespace();
        String loc = imp.getLocation();
        if (namespace == null) {
            errors.add("not namespace-qualified imports are not supported.");
            continue;
        }
        if (loc == null) {
            errors.add("Empty location imports are not supported.");
        } else {
            if (importType.equals(Namespaces.TOSCA_NAMESPACE)) {
                if (!Util.isRelativeURI(loc)) {
                    errors.add("Absolute URIs for definitions import not supported.");
                    continue;
                }
                // URIs are encoded
                loc = EncodingUtil.URLdecode(loc);
                Path defsPath = basePath.resolve(loc);
                // fallback for older CSARs, where the location is given from the root
                if (!Files.exists(defsPath)) {
                    defsPath = basePath.getParent().resolve(loc);
                // the real existence check is done in importDefinitions
                }
                this.importDefinitions(tmf, defsPath, errors, options);
                // imports of definitions don't have to be kept as these are managed by Winery
                iterator.remove();
            } else {
                this.importOtherImport(basePath, imp, errors, importType, options);
            }
        }
    }
}
Also used : Path(java.nio.file.Path) TImport(org.eclipse.winery.model.tosca.TImport)

Example 4 with TImport

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

the class CsarImporter method processDefinitionsImport.

protected Optional<ServiceTemplateId> processDefinitionsImport(TDefinitions defs, TOSCAMetaFile tmf, Path definitionsPath, List<String> errors, CsarImportOptions options) throws IOException {
    List<TImport> imports = defs.getImport();
    this.importImports(definitionsPath.getParent(), tmf, imports, errors, options);
    // imports has been modified to contain necessary imports only
    // this method adds new imports to defs which may not be imported using "importImports".
    // Therefore, "importTypes" has to be called *after* importImports
    this.importTypes(defs, errors);
    Optional<ServiceTemplateId> entryServiceTemplate = Optional.empty();
    String defaultNamespace = defs.getTargetNamespace();
    List<TExtensibleElements> componentInstanceList = defs.getServiceTemplateOrNodeTypeOrNodeTypeImplementation();
    for (final TExtensibleElements ci : componentInstanceList) {
        // Determine namespace
        String namespace = this.getNamespace(ci, defaultNamespace);
        // Ensure that element has the namespace
        this.setNamespace(ci, namespace);
        // Determine id
        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());
                CsarImporter.LOGGER.debug(msg);
            } else {
                String msg = String.format("Skipped %1$s %2$s, because it already exists", ci.getClass().getName(), wid.getQName().toString());
                CsarImporter.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);
        // copy over the inputs determined by this.importImports
        newDefs.getImport().addAll(imports);
        // add the current TExtensibleElements as the only content to it
        newDefs.getServiceTemplateOrNodeTypeOrNodeTypeImplementation().add(ci);
        if (ci instanceof TArtifactTemplate) {
            // convention: Definitions are stored in the "Definitions" directory, therefore going to levels up (Definitions dir -> root dir) resolves to the root dir
            // COS01, line 2663 states that the path has to be resolved from the *root* of the CSAR
            this.adjustArtifactTemplate(definitionsPath.getParent().getParent(), tmf, (ArtifactTemplateId) wid, (TArtifactTemplate) ci, errors);
        } else 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);
        } else if (ci instanceof TServiceTemplate) {
            this.adjustServiceTemplate(definitionsPath.getParent().getParent(), tmf, (ServiceTemplateId) wid, (TServiceTemplate) ci, errors);
            entryServiceTemplate = Optional.of((ServiceTemplateId) wid);
        }
        // import license and readme files
        importLicenseAndReadme(definitionsPath.getParent().getParent(), wid, tmf, errors);
        // Therefore, we check the entity type separately here
        if (ci instanceof TEntityType) {
            if (options.isAsyncWPDParsing()) {
                // Adjusting takes a long time
                // Therefore, we first save the type as is and convert to Winery-Property-Definitions in the background
                CsarImporter.storeDefinitions(targetRepository, wid, newDefs);
                CsarImporter.entityTypeAdjustmentService.submit(() -> {
                    adjustEntityType((TEntityType) ci, (EntityTypeId) wid, newDefs, errors);
                    CsarImporter.storeDefinitions(targetRepository, wid, newDefs);
                });
            } else {
                adjustEntityType((TEntityType) ci, (EntityTypeId) wid, newDefs, errors);
                CsarImporter.storeDefinitions(targetRepository, wid, newDefs);
            }
        } else {
            CsarImporter.storeDefinitions(targetRepository, wid, newDefs);
        }
    }
    return entryServiceTemplate;
}
Also used : DefinitionsChildId(org.eclipse.winery.model.ids.definitions.DefinitionsChildId) TArtifactTemplate(org.eclipse.winery.model.tosca.TArtifactTemplate) TEntityType(org.eclipse.winery.model.tosca.TEntityType) TImport(org.eclipse.winery.model.tosca.TImport) TExtensibleElements(org.eclipse.winery.model.tosca.TExtensibleElements) ServiceTemplateId(org.eclipse.winery.model.ids.definitions.ServiceTemplateId) TNodeType(org.eclipse.winery.model.tosca.TNodeType) TRelationshipType(org.eclipse.winery.model.tosca.TRelationshipType) NodeTypeId(org.eclipse.winery.model.ids.definitions.NodeTypeId) TDefinitions(org.eclipse.winery.model.tosca.TDefinitions) TServiceTemplate(org.eclipse.winery.model.tosca.TServiceTemplate)

Example 5 with TImport

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

the class CsarImporter method importTypes.

/**
 * Imports the specified types into the repository. The types are converted to an import statement
 *
 * @param errors Container for error messages
 */
private void importTypes(TDefinitions defs, final List<String> errors) {
    Types typesContainer = defs.getTypes();
    if (typesContainer != null) {
        List<Object> types = typesContainer.getAny();
        for (Object type : types) {
            if (type instanceof Element) {
                Element element = (Element) type;
                // generate id part of ImportId out of definitions' id
                // we do not use the name as the name has to be URLencoded again and we have issues with the interplay with org.eclipse.winery.common.ids.definitions.imports.GenericImportId.getId(TImport) then.
                String id = defs.getId();
                // try to  make the id unique by hashing the "content" of the definition
                id = id + "-" + Integer.toHexString(element.hashCode());
                // set importId
                DefinitionsChildId importId;
                String ns;
                if (element.getNamespaceURI().equals(XMLConstants.W3C_XML_SCHEMA_NS_URI)) {
                    ns = element.getAttribute("targetNamespace");
                    importId = new XSDImportId(ns, id, false);
                } else {
                    // Quick hack for non-XML-Schema-definitions
                    ns = "unknown";
                    importId = new GenericImportId(ns, id, false, element.getNamespaceURI());
                }
                // Following code is adapted from importOtherImports
                TDefinitions wrapperDefs = BackendUtils.createWrapperDefinitions(importId, targetRepository);
                TImport imp = new TImport();
                String fileName = id + ".xsd";
                imp.setLocation(fileName);
                imp.setImportType(XMLConstants.W3C_XML_SCHEMA_NS_URI);
                imp.setNamespace(ns);
                wrapperDefs.getImport().add(imp);
                CsarImporter.storeDefinitions(targetRepository, importId, wrapperDefs);
                // put the file itself to the repo
                // ref is required to generate fileRef
                RepositoryFileReference ref = BackendUtils.getRefOfDefinitions(importId);
                RepositoryFileReference fileRef = new RepositoryFileReference(ref.getParent(), fileName);
                // convert element to document
                // QUICK HACK. Alternative: Add new method targetRepository.getOutputStream and transform DOM node to OuptputStream
                String content = Util.getXMLAsString(element);
                try {
                    targetRepository.putContentToFile(fileRef, content, MediaTypes.MEDIATYPE_TEXT_XML);
                } catch (IOException e) {
                    CsarImporter.LOGGER.debug("Could not put XML Schema definition to file " + fileRef.toString(), e);
                    errors.add("Could not put XML Schema definition to file " + fileRef.toString());
                }
                // add import to definitions
                // adapt path - similar to importOtherImport
                String newLoc = "../" + Util.getUrlPath(fileRef);
                imp.setLocation(newLoc);
                defs.getImport().add(imp);
            } else {
                // This is a known type. Otherwise JAX-B would render it as Element
                errors.add("There is a Type of class " + type.getClass().toString() + " which is unknown to Winery. The type element is imported as is");
            }
        }
    }
}
Also used : Types(org.eclipse.winery.model.tosca.TDefinitions.Types) MediaTypes(org.eclipse.winery.repository.backend.constants.MediaTypes) XSDImportId(org.eclipse.winery.model.ids.definitions.imports.XSDImportId) RepositoryFileReference(org.eclipse.winery.repository.common.RepositoryFileReference) DefinitionsChildId(org.eclipse.winery.model.ids.definitions.DefinitionsChildId) Element(org.w3c.dom.Element) TImport(org.eclipse.winery.model.tosca.TImport) IOException(java.io.IOException) TDefinitions(org.eclipse.winery.model.tosca.TDefinitions) GenericImportId(org.eclipse.winery.model.ids.definitions.imports.GenericImportId)

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