Search in sources :

Example 31 with RepositoryFileReference

use of org.eclipse.winery.common.RepositoryFileReference in project winery by eclipse.

the class WriterUtils method storeDefinitions.

public static void storeDefinitions(Definitions definitions, boolean overwrite, Path dir) {
    Path path = null;
    try {
        path = Files.createTempDirectory("winery");
    } catch (IOException e) {
        e.printStackTrace();
    }
    LOGGER.debug("Store definition: {}", definitions.getId());
    saveDefinitions(definitions, path, definitions.getTargetNamespace(), definitions.getId());
    Definitions cleanDefinitions = loadDefinitions(path, definitions.getTargetNamespace(), definitions.getId());
    CsarImporter csarImporter = new CsarImporter();
    List<Exception> exceptions = new ArrayList<>();
    cleanDefinitions.getServiceTemplateOrNodeTypeOrNodeTypeImplementation().forEach(entry -> {
        String namespace = csarImporter.getNamespace(entry, definitions.getTargetNamespace());
        csarImporter.setNamespace(entry, namespace);
        String id = ModelUtilities.getId(entry);
        Class<? extends DefinitionsChildId> widClazz = Util.getComponentIdClassForTExtensibleElements(entry.getClass());
        final DefinitionsChildId wid = BackendUtils.getDefinitionsChildId(widClazz, namespace, id, false);
        if (RepositoryFactory.getRepository().exists(wid)) {
            if (overwrite) {
                try {
                    RepositoryFactory.getRepository().forceDelete(wid);
                } catch (IOException e) {
                    exceptions.add(e);
                }
            } else {
                return;
            }
        }
        if (entry instanceof TArtifactTemplate) {
            TArtifactTemplate.ArtifactReferences artifactReferences = ((TArtifactTemplate) entry).getArtifactReferences();
            Stream.of(artifactReferences).filter(Objects::nonNull).flatMap(ref -> ref.getArtifactReference().stream()).filter(Objects::nonNull).forEach(ref -> {
                String reference = ref.getReference();
                URI refURI;
                try {
                    refURI = new URI(reference);
                } catch (URISyntaxException e) {
                    LOGGER.error("Invalid URI {}", reference);
                    return;
                }
                if (refURI.isAbsolute()) {
                    return;
                }
                Path artifactPath = dir.resolve(reference);
                if (!Files.exists(artifactPath)) {
                    LOGGER.error("File not found {}", artifactPath);
                    return;
                }
                ArtifactTemplateFilesDirectoryId aDir = new ArtifactTemplateFilesDirectoryId((ArtifactTemplateId) wid);
                RepositoryFileReference aFile = new RepositoryFileReference(aDir, artifactPath.getFileName().toString());
                MediaType mediaType = null;
                try (InputStream is = Files.newInputStream(artifactPath);
                    BufferedInputStream bis = new BufferedInputStream(is)) {
                    mediaType = BackendUtils.getMimeType(bis, artifactPath.getFileName().toString());
                    RepositoryFactory.getRepository().putContentToFile(aFile, bis, mediaType);
                } catch (IOException e) {
                    LOGGER.error("Could not read artifact template file: {}", artifactPath);
                    return;
                }
            });
        }
        final Definitions part = BackendUtils.createWrapperDefinitions(wid);
        part.getServiceTemplateOrNodeTypeOrNodeTypeImplementation().add(entry);
        RepositoryFileReference ref = BackendUtils.getRefOfDefinitions(wid);
        String content = BackendUtils.getXMLAsString(part, true);
        try {
            RepositoryFactory.getRepository().putContentToFile(ref, content, MediaTypes.MEDIATYPE_TOSCA_DEFINITIONS);
        } catch (Exception e) {
            exceptions.add(e);
        }
    });
}
Also used : Path(java.nio.file.Path) DefinitionsChildId(org.eclipse.winery.common.ids.definitions.DefinitionsChildId) CsarImporter(org.eclipse.winery.repository.importing.CsarImporter) TArtifactTemplate(org.eclipse.winery.model.tosca.TArtifactTemplate) Definitions(org.eclipse.winery.model.tosca.Definitions) TDefinitions(org.eclipse.winery.model.tosca.TDefinitions) ArrayList(java.util.ArrayList) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) TransformerException(javax.xml.transform.TransformerException) URISyntaxException(java.net.URISyntaxException) JAXBException(javax.xml.bind.JAXBException) ArtifactTemplateFilesDirectoryId(org.eclipse.winery.repository.datatypes.ids.elements.ArtifactTemplateFilesDirectoryId) RepositoryFileReference(org.eclipse.winery.common.RepositoryFileReference) MediaType(org.apache.tika.mime.MediaType)

Example 32 with RepositoryFileReference

use of org.eclipse.winery.common.RepositoryFileReference 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 33 with RepositoryFileReference

use of org.eclipse.winery.common.RepositoryFileReference in project winery by eclipse.

the class BackendUtils method persist.

/**
 * Persists the given definitions
 *
 * @param id          the id of the definition child to persist
 * @param definitions the definitions to persist
 */
public static void persist(DefinitionsChildId id, Definitions definitions) throws IOException {
    RepositoryFileReference ref = BackendUtils.getRefOfDefinitions(id);
    BackendUtils.persist(definitions, ref, MediaTypes.MEDIATYPE_TOSCA_DEFINITIONS);
}
Also used : RepositoryFileReference(org.eclipse.winery.common.RepositoryFileReference)

Example 34 with RepositoryFileReference

use of org.eclipse.winery.common.RepositoryFileReference in project winery by eclipse.

the class ConsistencyChecker method checkXmlSchemaValidation.

public static void checkXmlSchemaValidation(ConsistencyErrorLogger errorLogger, ConsistencyCheckerConfiguration configuration, DefinitionsChildId id) {
    RepositoryFileReference refOfDefinitions = BackendUtils.getRefOfDefinitions(id);
    if (!configuration.getRepository().exists(refOfDefinitions)) {
        printAndAddError(errorLogger, configuration.getVerbosity(), id, "Id exists, but corresponding XML file does not.");
        return;
    }
    try (InputStream inputStream = configuration.getRepository().newInputStream(refOfDefinitions)) {
        DocumentBuilder documentBuilder = ToscaDocumentBuilderFactory.INSTANCE.getSchemaAwareToscaDocumentBuilder();
        StringBuilder errorStringBuilder = new StringBuilder();
        documentBuilder.setErrorHandler(BackendUtils.getErrorHandler(errorStringBuilder));
        documentBuilder.parse(inputStream);
        String errors = errorStringBuilder.toString();
        if (!errors.isEmpty()) {
            printAndAddError(errorLogger, configuration.getVerbosity(), id, errors);
        }
    } catch (IOException e) {
        LOGGER.debug("I/O error", e);
        printAndAddError(errorLogger, configuration.getVerbosity(), id, "I/O error during XML validation " + e.getMessage());
    } catch (SAXException e) {
        LOGGER.debug("SAX exception", e);
        printAndAddError(errorLogger, configuration.getVerbosity(), id, "SAX error during XML validation: " + e.getMessage());
    }
}
Also used : RepositoryFileReference(org.eclipse.winery.common.RepositoryFileReference) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ZipInputStream(java.util.zip.ZipInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) SAXException(org.xml.sax.SAXException)

Example 35 with RepositoryFileReference

use of org.eclipse.winery.common.RepositoryFileReference in project winery by eclipse.

the class ConsistencyChecker method checkPropertiesValidation.

public static void checkPropertiesValidation(ConsistencyErrorLogger errorLogger, ConsistencyCheckerConfiguration configuration, DefinitionsChildId id) {
    if (id instanceof EntityTemplateId) {
        TEntityTemplate entityTemplate = (TEntityTemplate) configuration.getRepository().getDefinitions(id).getElement();
        if (Objects.isNull(entityTemplate.getType())) {
            // no printing necessary; type consistency is checked at other places
            return;
        }
        final TEntityType entityType = configuration.getRepository().getTypeForTemplate(entityTemplate);
        final WinerysPropertiesDefinition winerysPropertiesDefinition = entityType.getWinerysPropertiesDefinition();
        final TEntityType.PropertiesDefinition propertiesDefinition = entityType.getPropertiesDefinition();
        if ((winerysPropertiesDefinition != null) || (propertiesDefinition != null)) {
            final TEntityTemplate.Properties properties = entityTemplate.getProperties();
            if (properties == null) {
                printAndAddError(errorLogger, configuration.getVerbosity(), id, "Properties required, but no properties defined");
                return;
            }
            if (winerysPropertiesDefinition != null) {
                Map<String, String> kvProperties = entityTemplate.getProperties().getKVProperties();
                if (kvProperties.isEmpty()) {
                    printAndAddError(errorLogger, configuration.getVerbosity(), id, "Properties required, but no properties set (any case)");
                    return;
                }
                for (PropertyDefinitionKV propertyDefinitionKV : winerysPropertiesDefinition.getPropertyDefinitionKVList().getPropertyDefinitionKVs()) {
                    String key = propertyDefinitionKV.getKey();
                    if (kvProperties.get(key) == null) {
                        printAndAddError(errorLogger, configuration.getVerbosity(), id, "Property " + key + " required, but not set.");
                    } else {
                        // remove the key from the map to enable checking below whether a property is defined which not requried by the property definition
                        kvProperties.remove(key);
                    }
                }
                // If any key is left, this is a key not defined at the schema
                for (Object o : kvProperties.keySet()) {
                    printAndAddError(errorLogger, configuration.getVerbosity(), id, "Property " + o + " set, but not defined at schema.");
                }
            } else if (propertiesDefinition != null) {
                @Nullable final Object any = properties.getAny();
                if (any == null) {
                    printAndAddError(errorLogger, configuration.getVerbosity(), id, "Properties required, but no properties defined (any case)");
                    return;
                }
                @Nullable final QName element = propertiesDefinition.getElement();
                if (element != null) {
                    final Map<String, RepositoryFileReference> mapFromLocalNameToXSD = configuration.getRepository().getXsdImportManager().getMapFromLocalNameToXSD(new Namespace(element.getNamespaceURI(), false), false);
                    final RepositoryFileReference repositoryFileReference = mapFromLocalNameToXSD.get(element.getLocalPart());
                    if (repositoryFileReference == null) {
                        printAndAddError(errorLogger, configuration.getVerbosity(), id, "No Xml Schema definition found for " + element);
                        return;
                    }
                    validate(errorLogger, repositoryFileReference, any, configuration, id);
                }
            }
        }
    }
}
Also used : PropertyDefinitionKV(org.eclipse.winery.model.tosca.kvproperties.PropertyDefinitionKV) QName(javax.xml.namespace.QName) WinerysPropertiesDefinition(org.eclipse.winery.model.tosca.kvproperties.WinerysPropertiesDefinition) Namespace(org.eclipse.winery.common.ids.Namespace) RepositoryFileReference(org.eclipse.winery.common.RepositoryFileReference)

Aggregations

RepositoryFileReference (org.eclipse.winery.common.RepositoryFileReference)51 IOException (java.io.IOException)15 InputStream (java.io.InputStream)8 ArtifactTemplateId (org.eclipse.winery.common.ids.definitions.ArtifactTemplateId)8 Definitions (org.eclipse.winery.model.tosca.Definitions)8 BufferedInputStream (java.io.BufferedInputStream)6 ArtifactTemplateSourceDirectoryId (org.eclipse.winery.repository.datatypes.ids.elements.ArtifactTemplateSourceDirectoryId)6 Test (org.junit.Test)6 Path (java.nio.file.Path)5 JAXBException (javax.xml.bind.JAXBException)5 MediaType (org.apache.tika.mime.MediaType)5 ArtifactTemplateFilesDirectoryId (org.eclipse.winery.repository.datatypes.ids.elements.ArtifactTemplateFilesDirectoryId)5 ZipInputStream (java.util.zip.ZipInputStream)4 Unmarshaller (javax.xml.bind.Unmarshaller)4 QName (javax.xml.namespace.QName)4 XmlId (org.eclipse.winery.common.ids.XmlId)4 GenericImportId (org.eclipse.winery.common.ids.definitions.imports.GenericImportId)4 XSDImportId (org.eclipse.winery.common.ids.definitions.imports.XSDImportId)4 PlanId (org.eclipse.winery.common.ids.elements.PlanId)4 PlansId (org.eclipse.winery.common.ids.elements.PlansId)4