Search in sources :

Example 6 with Namespace

use of org.eclipse.winery.common.ids.Namespace in project winery by eclipse.

the class ServiceTemplateComplianceRuleRuleChecker method getRuleIds.

public List<ComplianceRuleId> getRuleIds(TServiceTemplate serviceTemplate) {
    List<ComplianceRuleId> complianceRules = Lists.newArrayList();
    Namespace namespace = new Namespace(serviceTemplate.getTargetNamespace(), false);
    Collection<Namespace> componentsNamespaces = RepositoryFactory.getRepository().getComponentsNamespaces(ComplianceRuleId.class);
    List<Namespace> relevantNamespaces = componentsNamespaces.stream().filter(ns -> namespace.getDecoded().startsWith(ns.getDecoded())).collect(Collectors.toList());
    for (Namespace space : relevantNamespaces) {
        complianceRules.addAll((Collection<? extends ComplianceRuleId>) ((FilebasedRepository) RepositoryFactory.getRepository()).getAllIdsInNamespace(ComplianceRuleId.class, space));
    }
    return complianceRules;
}
Also used : FilebasedRepository(org.eclipse.winery.repository.backend.filebased.FilebasedRepository) Logger(org.slf4j.Logger) ComplianceRuleId(org.eclipse.winery.common.ids.definitions.ComplianceRuleId) Collection(java.util.Collection) GraphMapping(org.jgrapht.GraphMapping) LoggerFactory(org.slf4j.LoggerFactory) Collectors(java.util.stream.Collectors) RepositoryFactory(org.eclipse.winery.repository.backend.RepositoryFactory) TServiceTemplate(org.eclipse.winery.model.tosca.TServiceTemplate) List(java.util.List) TOSCANode(org.eclipse.winery.compliance.model.TOSCANode) Lists(com.google.common.collect.Lists) IRepository(org.eclipse.winery.repository.backend.IRepository) Map(java.util.Map) TComplianceRule(org.eclipse.winery.model.tosca.TComplianceRule) Namespace(org.eclipse.winery.common.ids.Namespace) NonNull(org.eclipse.jdt.annotation.NonNull) ComplianceRuleId(org.eclipse.winery.common.ids.definitions.ComplianceRuleId) FilebasedRepository(org.eclipse.winery.repository.backend.filebased.FilebasedRepository) Namespace(org.eclipse.winery.common.ids.Namespace)

Example 7 with Namespace

use of org.eclipse.winery.common.ids.Namespace in project winery by eclipse.

the class Showcases method convert.

public MultiException convert(String path, String namespace, Stream<String> files) throws Exception {
    MultiException exception = new MultiException();
    files.map(name -> {
        try {
            return new LinkedHashMap.SimpleEntry<>(name, readServiceTemplate(path + File.separator + name));
        } catch (Exception e) {
            exception.add(e);
        }
        return null;
    }).filter(Objects::nonNull).map(entry -> new LinkedHashMap.SimpleEntry<>(entry.getKey(), convert(entry.getValue(), entry.getKey(), namespace))).forEach(entry -> WriterUtils.saveDefinitions(entry.getValue(), outPath, namespace, entry.getKey()));
    if (exception.hasException()) {
        throw exception.getException();
    }
    return exception;
}
Also used : ServiceTemplateId(org.eclipse.winery.common.ids.definitions.ServiceTemplateId) Test(org.junit.Test) FileInputStream(java.io.FileInputStream) XmlId(org.eclipse.winery.common.ids.XmlId) RepositoryFactory(org.eclipse.winery.repository.backend.RepositoryFactory) File(java.io.File) TServiceTemplate(org.eclipse.winery.model.tosca.TServiceTemplate) LinkedHashMap(java.util.LinkedHashMap) Objects(java.util.Objects) Stream(java.util.stream.Stream) Ignore(org.junit.Ignore) Paths(java.nio.file.Paths) MultiException(org.eclipse.winery.yaml.common.exception.MultiException) Namespaces(org.eclipse.winery.yaml.common.Namespaces) AbstractTestY2X(org.eclipse.winery.yaml.converter.yaml.support.AbstractTestY2X) Namespace(org.eclipse.winery.common.ids.Namespace) WriterUtils(org.eclipse.winery.yaml.common.writer.WriterUtils) Assert(org.junit.Assert) Converter(org.eclipse.winery.yaml.converter.Converter) InputStream(java.io.InputStream) Objects(java.util.Objects) MultiException(org.eclipse.winery.yaml.common.exception.MultiException) MultiException(org.eclipse.winery.yaml.common.exception.MultiException) LinkedHashMap(java.util.LinkedHashMap)

Example 8 with Namespace

use of org.eclipse.winery.common.ids.Namespace in project winery by eclipse.

the class ConsistencyChecker method checkNamespaceUri.

public static void checkNamespaceUri(ConsistencyErrorLogger errorLogger, EnumSet<ConsistencyCheckerVerbosity> verbosity, DefinitionsChildId id) {
    Objects.requireNonNull(verbosity);
    Objects.requireNonNull(id);
    String uriStr = id.getNamespace().getDecoded();
    if (!uriStr.trim().equals(uriStr)) {
        printAndAddError(errorLogger, verbosity, id, "Namespace starts or ends with white spaces");
    }
    URI uri;
    try {
        uri = new URI(uriStr);
    } catch (URISyntaxException e) {
        LOGGER.debug("Invalid URI", e);
        printAndAddError(errorLogger, verbosity, id, "Invalid URI: " + e.getMessage());
        return;
    }
    if (!uri.isAbsolute()) {
        printAndAddError(errorLogger, verbosity, id, "URI is relative");
    }
    if ((uriStr.startsWith("http://www.opentosca.org/") && (!uriStr.toLowerCase().equals(uriStr)))) {
        printAndAddError(errorLogger, verbosity, id, "opentosca URI is not lowercase");
    }
    if (uriStr.endsWith("/")) {
        printAndAddError(errorLogger, verbosity, id, "URI ends with a slash");
    }
    if (uriStr.contains(ARTEFACT_BE)) {
        printAndAddError(errorLogger, verbosity, id, "artifact is spelled with i in American English, not artefact as in British English");
    }
    boolean namespaceUriContainsDifferentType = DefinitionsChildId.ALL_TOSCA_COMPONENT_ID_CLASSES.stream().filter(definitionsChildIdClass -> !definitionsChildIdClass.isAssignableFrom(id.getClass())).flatMap(definitionsChildIdClass -> {
        final String lowerCaseIdClass = Util.getTypeForComponentId(definitionsChildIdClass).toLowerCase();
        return Stream.of(lowerCaseIdClass + "s", lowerCaseIdClass + "/");
    }).anyMatch(definitionsChildName -> uriStr.contains(definitionsChildName));
    if (namespaceUriContainsDifferentType) {
        printAndAddError(errorLogger, verbosity, id, "Namespace URI contains tosca definitions name from other type. E.g., Namespace is ...servicetemplates..., but the type is an artifact template");
    }
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) java.util(java.util) ZipInputStream(java.util.zip.ZipInputStream) StreamSource(javax.xml.transform.stream.StreamSource) URISyntaxException(java.net.URISyntaxException) LoggerFactory(org.slf4j.LoggerFactory) Source(javax.xml.transform.Source) StringUtils(org.apache.commons.lang3.StringUtils) Schema(javax.xml.validation.Schema) org.eclipse.winery.common.ids.definitions(org.eclipse.winery.common.ids.definitions) ToscaDocumentBuilderFactory(org.eclipse.winery.common.ToscaDocumentBuilderFactory) BackendUtils(org.eclipse.winery.repository.backend.BackendUtils) URI(java.net.URI) XMLConstants(javax.xml.XMLConstants) Path(java.nio.file.Path) ZipEntry(java.util.zip.ZipEntry) RepositoryFileReference(org.eclipse.winery.common.RepositoryFileReference) Nullable(org.eclipse.jgit.annotations.Nullable) OutputStream(java.io.OutputStream) CsarExporter(org.eclipse.winery.repository.export.CsarExporter) ArchiveException(org.apache.commons.compress.archivers.ArchiveException) Logger(org.slf4j.Logger) SchemaFactory(javax.xml.validation.SchemaFactory) PropertyDefinitionKV(org.eclipse.winery.model.tosca.kvproperties.PropertyDefinitionKV) Files(java.nio.file.Files) StandardOpenOption(java.nio.file.StandardOpenOption) Validator(javax.xml.validation.Validator) IOException(java.io.IOException) WinerysPropertiesDefinition(org.eclipse.winery.model.tosca.kvproperties.WinerysPropertiesDefinition) Util(org.eclipse.winery.common.Util) JAXBException(javax.xml.bind.JAXBException) Collectors(java.util.stream.Collectors) RepositoryFactory(org.eclipse.winery.repository.backend.RepositoryFactory) PropertyDefinitionKVList(org.eclipse.winery.model.tosca.kvproperties.PropertyDefinitionKVList) Element(org.w3c.dom.Element) Stream(java.util.stream.Stream) org.eclipse.winery.model.tosca(org.eclipse.winery.model.tosca) RepositoryCorruptException(org.eclipse.winery.repository.exceptions.RepositoryCorruptException) NonNull(org.eclipse.jgit.annotations.NonNull) SAXException(org.xml.sax.SAXException) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ModelUtilities(org.eclipse.winery.model.tosca.utils.ModelUtilities) Namespace(org.eclipse.winery.common.ids.Namespace) QName(javax.xml.namespace.QName) InputStream(java.io.InputStream) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI)

Example 9 with Namespace

use of org.eclipse.winery.common.ids.Namespace 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)

Example 10 with Namespace

use of org.eclipse.winery.common.ids.Namespace in project winery by eclipse.

the class FilebasedRepository method getAllDefinitionsChildIds.

@Override
public <T extends DefinitionsChildId> SortedSet<T> getAllDefinitionsChildIds(Class<T> idClass) {
    SortedSet<T> res = new TreeSet<>();
    String rootPathFragment = Util.getRootPathFragment(idClass);
    Path dir = this.repositoryRoot.resolve(rootPathFragment);
    if (!Files.exists(dir)) {
        // return empty list if no ids are available
        return res;
    }
    assert (Files.isDirectory(dir));
    final OnlyNonHiddenDirectories onhdf = new OnlyNonHiddenDirectories();
    // list all directories contained in this directory
    try (DirectoryStream<Path> ds = Files.newDirectoryStream(dir, onhdf)) {
        for (Path nsP : ds) {
            // the current path is the namespace
            Namespace ns = new Namespace(nsP.getFileName().toString(), true);
            try (DirectoryStream<Path> idDS = Files.newDirectoryStream(nsP, onhdf)) {
                for (Path idP : idDS) {
                    XmlId xmlId = new XmlId(idP.getFileName().toString(), true);
                    Constructor<T> constructor;
                    try {
                        constructor = idClass.getConstructor(Namespace.class, XmlId.class);
                    } catch (Exception e) {
                        FilebasedRepository.LOGGER.debug("Internal error at determining id constructor", e);
                        // abort everything, return invalid result
                        return res;
                    }
                    T id;
                    try {
                        id = constructor.newInstance(ns, xmlId);
                    } catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
                        FilebasedRepository.LOGGER.debug("Internal error at invocation of id constructor", e);
                        // abort everything, return invalid result
                        return res;
                    }
                    res.add(id);
                }
            }
        }
    } catch (IOException e) {
        FilebasedRepository.LOGGER.debug("Cannot close ds", e);
    }
    return res;
}
Also used : Namespace(org.eclipse.winery.common.ids.Namespace) InvalidPathException(org.eclipse.jgit.dircache.InvalidPathException) WineryRepositoryException(org.eclipse.winery.repository.exceptions.WineryRepositoryException) ArchiveException(org.apache.commons.compress.archivers.ArchiveException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ConfigurationException(org.apache.commons.configuration.ConfigurationException) InvocationTargetException(java.lang.reflect.InvocationTargetException) XmlId(org.eclipse.winery.common.ids.XmlId)

Aggregations

Namespace (org.eclipse.winery.common.ids.Namespace)13 QName (javax.xml.namespace.QName)5 XmlId (org.eclipse.winery.common.ids.XmlId)5 RepositoryFactory (org.eclipse.winery.repository.backend.RepositoryFactory)4 IOException (java.io.IOException)3 InputStream (java.io.InputStream)3 Collectors (java.util.stream.Collectors)3 RepositoryFileReference (org.eclipse.winery.common.RepositoryFileReference)3 ServiceTemplateId (org.eclipse.winery.common.ids.definitions.ServiceTemplateId)3 TServiceTemplate (org.eclipse.winery.model.tosca.TServiceTemplate)3 PropertyDefinitionKV (org.eclipse.winery.model.tosca.kvproperties.PropertyDefinitionKV)3 WinerysPropertiesDefinition (org.eclipse.winery.model.tosca.kvproperties.WinerysPropertiesDefinition)3 IRepository (org.eclipse.winery.repository.backend.IRepository)3 Logger (org.slf4j.Logger)3 LoggerFactory (org.slf4j.LoggerFactory)3 FileInputStream (java.io.FileInputStream)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 java.util (java.util)2 Stream (java.util.stream.Stream)2 DocumentBuilder (javax.xml.parsers.DocumentBuilder)2