Search in sources :

Example 16 with Definitions

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

the class Datatypes method testNodeTemplateWithDataTypes.

@Test
public void testNodeTemplateWithDataTypes() throws Exception {
    String name = "node_template-using-data_types";
    String namespace = "http://www.example.com/NodeTemplateUsingDataType";
    TServiceTemplate serviceTemplate = readServiceTemplate(name, namespace);
    Definitions definitions = convert(serviceTemplate, name, namespace);
    writeXml(definitions, name, namespace);
    Assert.assertNotNull(definitions);
}
Also used : Definitions(org.eclipse.winery.model.tosca.Definitions) TServiceTemplate(org.eclipse.winery.model.tosca.yaml.TServiceTemplate) Test(org.junit.Test)

Example 17 with Definitions

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

the class BackendUtils method getGitInformation.

/**
 * @param directoryId DirectoryId of the ArtifactTemplate that should contain a reference to a git repository.
 * @return The URL and the branch/tag that contains the files for the ArtifactTemplate. null if no git information
 * is given.
 */
public static GitInfo getGitInformation(DirectoryId directoryId) {
    if (!(directoryId.getParent() instanceof ArtifactTemplateId)) {
        return null;
    }
    RepositoryFileReference ref = BackendUtils.getRefOfDefinitions((ArtifactTemplateId) directoryId.getParent());
    try (InputStream is = RepositoryFactory.getRepository().newInputStream(ref)) {
        Unmarshaller u = JAXBSupport.createUnmarshaller();
        Definitions defs = ((Definitions) u.unmarshal(is));
        Map<QName, String> atts = defs.getOtherAttributes();
        String src = atts.get(new QName(Namespaces.TOSCA_WINERY_EXTENSIONS_NAMESPACE, "gitsrc"));
        String branch = atts.get(new QName(Namespaces.TOSCA_WINERY_EXTENSIONS_NAMESPACE, "gitbranch"));
        // ^ is the XOR operator
        if (src == null ^ branch == null) {
            LOGGER.error("Git information not complete, URL or branch missing");
            return null;
        } else if (src == null && branch == null) {
            return null;
        }
        return new GitInfo(src, branch);
    } catch (IOException e) {
        LOGGER.error("Error reading definitions of " + directoryId.getParent() + " at " + ref.getFileName(), e);
    } catch (JAXBException e) {
        LOGGER.error("Error in XML in " + ref.getFileName(), e);
    }
    return null;
}
Also used : RepositoryFileReference(org.eclipse.winery.common.RepositoryFileReference) BufferedInputStream(java.io.BufferedInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) QName(javax.xml.namespace.QName) TDefinitions(org.eclipse.winery.model.tosca.TDefinitions) Definitions(org.eclipse.winery.model.tosca.Definitions) JAXBException(javax.xml.bind.JAXBException) IOException(java.io.IOException) Unmarshaller(javax.xml.bind.Unmarshaller) GitInfo(org.eclipse.winery.repository.GitInfo) ArtifactTemplateId(org.eclipse.winery.common.ids.definitions.ArtifactTemplateId)

Example 18 with Definitions

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

the class ToscaExportUtil method writeDefinitionsElement.

/**
 * Writes the Definitions belonging to the given definitgion children to the output stream
 *
 * @return a collection of DefinitionsChildIds referenced by the given component
 * @throws RepositoryCorruptException if tcId does not exist
 */
private Collection<DefinitionsChildId> writeDefinitionsElement(IRepository repository, DefinitionsChildId tcId, OutputStream out) throws JAXBException, RepositoryCorruptException, IOException {
    if (!repository.exists(tcId)) {
        String error = "Component instance " + tcId.toReadableString() + " does not exist.";
        ToscaExportUtil.LOGGER.error(error);
        throw new RepositoryCorruptException(error);
    }
    this.getPrepareForExport(repository, tcId);
    Definitions entryDefinitions = repository.getDefinitions(tcId);
    // BEGIN: Definitions modification
    // the "imports" collection contains the imports of Definitions, not of other definitions
    // the other definitions are stored in entryDefinitions.getImport()
    // we modify the internal definitions object directly. It is not written back to the storage. Therefore, we do not need to clone it
    // the imports (pointing to not-definitions (xsd, wsdl, ...)) already have a correct relative URL. (quick hack)
    URI uri = (URI) this.exportConfiguration.get(ToscaExportUtil.ExportProperties.REPOSITORY_URI.toString());
    if (uri != null) {
        // we are in the plain-XML mode, the URLs of the imports have to be adjusted
        for (TImport i : entryDefinitions.getImport()) {
            String loc = i.getLocation();
            if (!loc.startsWith("../")) {
                LOGGER.warn("Location is not relative for id " + tcId.toReadableString());
            }
            ;
            loc = loc.substring(3);
            loc = uri + loc;
            // now the location is an absolute URL
            i.setLocation(loc);
        }
    }
    // files of imports have to be added to the CSAR, too
    for (TImport i : entryDefinitions.getImport()) {
        String loc = i.getLocation();
        if (Util.isRelativeURI(loc)) {
            // locally stored, add to CSAR
            GenericImportId iid = new GenericImportId(i);
            String fileName = Util.getLastURIPart(loc);
            fileName = Util.URLdecode(fileName);
            RepositoryFileReference ref = new RepositoryFileReference(iid, fileName);
            this.putRefAsReferencedItemInCsar(ref);
        }
    }
    Collection<DefinitionsChildId> referencedDefinitionsChildIds = repository.getReferencedDefinitionsChildIds(tcId);
    // adjust imports: add imports of definitions to it
    Collection<TImport> imports = new ArrayList<>();
    for (DefinitionsChildId id : referencedDefinitionsChildIds) {
        this.addToImports(repository, id, imports);
    }
    entryDefinitions.getImport().addAll(imports);
    if (entryDefinitions.getElement() instanceof TEntityType) {
        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 URLencoded, loc on filesystem isn't
                    String locInCSAR = Util.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);
                    this.referencesToPathInCSARMap.put(new DummyRepositoryFileReferenceForGeneratedXSD(document), locInCSAR);
                }
                imp.setLocation(loc);
                // END: add import and put into CSAR
                // BEGIN: generate TOSCA conforming PropertiesDefinition
                PropertiesDefinition propertiesDefinition = new PropertiesDefinition();
                propertiesDefinition.setType(new QName(wrapperElementNamespace, wrapperElementLocalName));
                entityType.setPropertiesDefinition(propertiesDefinition);
            // END: generate TOSCA conforming PropertiesDefinition
            } else {
            // noinspection StatementWithEmptyBody
            // 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
            }
        }
    }
    // END: Definitions modification
    this.writeDefinitionsElement(entryDefinitions, out);
    return referencedDefinitionsChildIds;
}
Also used : TEntityType(org.eclipse.winery.model.tosca.TEntityType) QName(javax.xml.namespace.QName) Definitions(org.eclipse.winery.model.tosca.Definitions) TImport(org.eclipse.winery.model.tosca.TImport) WinerysPropertiesDefinition(org.eclipse.winery.model.tosca.kvproperties.WinerysPropertiesDefinition) Document(org.w3c.dom.Document) URI(java.net.URI) GenericImportId(org.eclipse.winery.common.ids.definitions.imports.GenericImportId) RepositoryFileReference(org.eclipse.winery.common.RepositoryFileReference) PropertiesDefinition(org.eclipse.winery.model.tosca.TEntityType.PropertiesDefinition) WinerysPropertiesDefinition(org.eclipse.winery.model.tosca.kvproperties.WinerysPropertiesDefinition) RepositoryCorruptException(org.eclipse.winery.repository.exceptions.RepositoryCorruptException)

Example 19 with Definitions

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

the class WineryRepositoryClient method getDefinitions.

private static Definitions getDefinitions(WebResource instanceResource) {
    // TODO: org.eclipse.winery.repository.resources.AbstractComponentInstanceResource.getDefinitionsWithAssociatedThings() could be used to do the resolving at the server
    ClientResponse response = instanceResource.accept(MimeTypes.MIMETYPE_TOSCA_DEFINITIONS).get(ClientResponse.class);
    if (response.getStatus() != 200) {
        // also handles 404
        return null;
    }
    Definitions definitions;
    try {
        Unmarshaller um = WineryRepositoryClient.createUnmarshaller();
        definitions = (Definitions) um.unmarshal(response.getEntityInputStream());
    } catch (JAXBException e) {
        LOGGER.error("Could not unmarshal Definitions", e);
        // try next service
        return null;
    }
    return definitions;
}
Also used : ClientResponse(com.sun.jersey.api.client.ClientResponse) TDefinitions(org.eclipse.winery.model.tosca.TDefinitions) Definitions(org.eclipse.winery.model.tosca.Definitions) JAXBException(javax.xml.bind.JAXBException) Unmarshaller(javax.xml.bind.Unmarshaller)

Example 20 with Definitions

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

the class WineryRepositoryClient method getDefinitions.

@Override
public Definitions getDefinitions(DefinitionsChildId id) {
    for (WebResource wr : this.repositoryResources) {
        String path = Util.getUrlPath(id);
        Definitions definitions = WineryRepositoryClient.getDefinitions(wr.path(path));
        if (definitions == null) {
            // in case of an error, just try the next one
            continue;
        }
        TExtensibleElements element = definitions.getElement();
        if (element instanceof TEntityType) {
            this.cache((TEntityType) element, id.getQName());
            return definitions;
        }
    }
    return new Definitions();
}
Also used : TEntityType(org.eclipse.winery.model.tosca.TEntityType) TDefinitions(org.eclipse.winery.model.tosca.TDefinitions) Definitions(org.eclipse.winery.model.tosca.Definitions) WebResource(com.sun.jersey.api.client.WebResource) TExtensibleElements(org.eclipse.winery.model.tosca.TExtensibleElements)

Aggregations

Definitions (org.eclipse.winery.model.tosca.Definitions)21 RepositoryFileReference (org.eclipse.winery.common.RepositoryFileReference)7 TDefinitions (org.eclipse.winery.model.tosca.TDefinitions)7 IOException (java.io.IOException)6 JAXBException (javax.xml.bind.JAXBException)5 QName (javax.xml.namespace.QName)5 Test (org.junit.Test)5 Unmarshaller (javax.xml.bind.Unmarshaller)4 TServiceTemplate (org.eclipse.winery.model.tosca.yaml.TServiceTemplate)4 BufferedInputStream (java.io.BufferedInputStream)3 InputStream (java.io.InputStream)3 DefinitionsChildId (org.eclipse.winery.common.ids.definitions.DefinitionsChildId)3 PolicyTemplateId (org.eclipse.winery.common.ids.definitions.PolicyTemplateId)3 TArtifactTemplate (org.eclipse.winery.model.tosca.TArtifactTemplate)3 TEntityType (org.eclipse.winery.model.tosca.TEntityType)3 TExtensibleElements (org.eclipse.winery.model.tosca.TExtensibleElements)3 TImport (org.eclipse.winery.model.tosca.TImport)3 TPolicyTemplate (org.eclipse.winery.model.tosca.TPolicyTemplate)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 URI (java.net.URI)2