Search in sources :

Example 1 with Namespace

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

the class ConsistencyChecker method checkNamespaceUri.

public void checkNamespaceUri(@NonNull DefinitionsChildId id) {
    Objects.requireNonNull(id);
    String uriStr = id.getNamespace().getDecoded();
    if (!uriStr.trim().equals(uriStr)) {
        printAndAddError(id, "Namespace starts or ends with white spaces");
    }
    URI uri;
    try {
        uri = new URI(uriStr);
    } catch (URISyntaxException e) {
        LOGGER.debug("Invalid URI", e);
        printAndAddError(id, "Invalid URI: " + e.getMessage());
        return;
    }
    if (!uri.isAbsolute()) {
        printAndAddError(id, "URI is relative");
    }
    if ((uriStr.startsWith("http://www.opentosca.org/") && (!uriStr.toLowerCase().equals(uriStr)))) {
        // URI is not lowercase
        // There are some special URIs, which are OK
        String[] splitUri = uriStr.split("/");
        String lastElement = splitUri[splitUri.length - 1];
        String uriStrWithoutLastElement = uriStr.substring(0, (uriStr.length() - lastElement.length()));
        if (!(id.getXmlId().toString().startsWith(lastElement)) || (!uriStrWithoutLastElement.toLowerCase().equals(uriStrWithoutLastElement))) {
            printAndAddError(id, "opentosca URI is not lowercase");
        }
    }
    if (uriStr.endsWith("/")) {
        printAndAddError(id, "URI ends with a slash");
    }
    if (uriStr.contains(ARTEFACT_BE)) {
        printAndAddError(id, "artifact is spelled with i in American English, not artefact as in British English");
    }
    // We could just check OpenTOSCA namespace rule examples. However, this would be too strict
    // Here, the idea is to check whether a string of another (!) id class appers in the namespace
    // If this is the case, the namespace is not consistent
    // For instance, a node type residing in the namespace: http://servicetemplates.example.org should not exist.
    boolean namespaceUriContainsDifferentType = DefinitionsChildId.ALL_TOSCA_COMPONENT_ID_CLASSES.stream().filter(definitionsChildIdClass -> !definitionsChildIdClass.isAssignableFrom(id.getClass())).flatMap(definitionsChildIdClass -> {
        final String lowerCaseIdClass = IdUtil.getTypeForComponentId(definitionsChildIdClass).toLowerCase();
        return Stream.of(lowerCaseIdClass + "s", lowerCaseIdClass + "/");
    }).anyMatch(uriStr::contains);
    if (namespaceUriContainsDifferentType) {
        if ((id instanceof ServiceTemplateId) && (id.getNamespace().getDecoded().contains("compliance"))) {
            // special case, because TComplianceRule models a service template, but Compliance Rules are treated as Service Template during modeling
            // example: class org.eclipse.winery.common.ids.definitions.ServiceTemplateId / {http://www.compliance.opentosca.org/compliancerules}Satisfied_Compliance_Rule_Example_w1
            printAndAddWarning(id, "Cannot perform checks for compliance rules...");
        } else {
            printAndAddError(id, "Namespace URI contains tosca definitions name from other type. E.g., Namespace is ...servicetemplates..., but the type is an artifact template");
        }
    }
}
Also used : HasIdInIdOrNameField(org.eclipse.winery.model.tosca.HasIdInIdOrNameField) IdUtil(org.eclipse.winery.model.ids.IdUtil) URISyntaxException(java.net.URISyntaxException) LoggerFactory(org.slf4j.LoggerFactory) Namespace(org.eclipse.winery.model.ids.Namespace) TServiceTemplate(org.eclipse.winery.model.tosca.TServiceTemplate) Nullable(org.eclipse.jdt.annotation.Nullable) Map(java.util.Map) URI(java.net.URI) Path(java.nio.file.Path) ZipEntry(java.util.zip.ZipEntry) TDefinitions(org.eclipse.winery.model.tosca.TDefinitions) TEntityType(org.eclipse.winery.model.tosca.TEntityType) RepositoryFileReference(org.eclipse.winery.repository.common.RepositoryFileReference) StandardOpenOption(java.nio.file.StandardOpenOption) Set(java.util.Set) TNodeType(org.eclipse.winery.model.tosca.TNodeType) WinerysPropertiesDefinition(org.eclipse.winery.model.tosca.extensions.kvproperties.WinerysPropertiesDefinition) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) List(java.util.List) PropertyDefinitionKV(org.eclipse.winery.model.tosca.extensions.kvproperties.PropertyDefinitionKV) Stream(java.util.stream.Stream) TExtensibleElements(org.eclipse.winery.model.tosca.TExtensibleElements) IRepository(org.eclipse.winery.repository.backend.IRepository) RepositoryCorruptException(org.eclipse.winery.repository.exceptions.RepositoryCorruptException) SAXException(org.xml.sax.SAXException) QName(javax.xml.namespace.QName) NonNull(org.eclipse.jdt.annotation.NonNull) DOMSource(javax.xml.transform.dom.DOMSource) ZipInputStream(java.util.zip.ZipInputStream) StreamSource(javax.xml.transform.stream.StreamSource) ServiceTemplateId(org.eclipse.winery.model.ids.definitions.ServiceTemplateId) TEntityTemplate(org.eclipse.winery.model.tosca.TEntityTemplate) HashMap(java.util.HashMap) Source(javax.xml.transform.Source) Schema(javax.xml.validation.Schema) ArrayList(java.util.ArrayList) ToscaDocumentBuilderFactory(org.eclipse.winery.common.ToscaDocumentBuilderFactory) BackendUtils(org.eclipse.winery.repository.backend.BackendUtils) DefinitionsChildId(org.eclipse.winery.model.ids.definitions.DefinitionsChildId) TNodeTemplate(org.eclipse.winery.model.tosca.TNodeTemplate) XMLConstants(javax.xml.XMLConstants) OutputStream(java.io.OutputStream) CsarExporter(org.eclipse.winery.repository.export.CsarExporter) SchemaFactory(javax.xml.validation.SchemaFactory) Logger(org.slf4j.Logger) Files(java.nio.file.Files) IOException(java.io.IOException) Validator(javax.xml.validation.Validator) EntityTemplateId(org.eclipse.winery.model.ids.definitions.EntityTemplateId) Element(org.w3c.dom.Element) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ModelUtilities(org.eclipse.winery.model.tosca.utils.ModelUtilities) Comparator(java.util.Comparator) Collections(java.util.Collections) InputStream(java.io.InputStream) NodeTypeId(org.eclipse.winery.model.ids.definitions.NodeTypeId) URISyntaxException(java.net.URISyntaxException) ServiceTemplateId(org.eclipse.winery.model.ids.definitions.ServiceTemplateId) URI(java.net.URI)

Example 2 with Namespace

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

the class ConsistencyChecker method checkPropertiesValidation.

private void checkPropertiesValidation(DefinitionsChildId id) {
    if (!(id instanceof EntityTemplateId)) {
        return;
    }
    TEntityTemplate entityTemplate;
    try {
        // TEntityTemplate is abstract. IRepository does not offer getElement for abstract ids
        // Therefore, we have to use the detour through getDefinitions
        entityTemplate = (TEntityTemplate) configuration.getRepository().getDefinitions(id).getElement();
    } catch (IllegalStateException e) {
        LOGGER.debug("Illegal State Exception during reading of id {}", id.toReadableString(), e);
        printAndAddError(id, "Reading error " + e.getMessage());
        return;
    } catch (ClassCastException e) {
        LOGGER.error("Something wrong in the consistency between Ids and the TOSCA data model. See http://eclipse.github.io/winery/dev/id-system.html for more information on the ID system.");
        printAndAddError(id, "Critical error at analysis: " + e.getMessage());
        return;
    }
    if (Objects.isNull(entityTemplate.getType())) {
        // no printing necessary; type consistency is checked at other places
        return;
    }
    TEntityType entityType;
    try {
        entityType = configuration.getRepository().getTypeForTemplate(entityTemplate);
    } catch (IllegalStateException e) {
        LOGGER.debug("Illegal State Exception during getting type for template {}", entityTemplate.getId(), e);
        printAndAddError(id, "Reading error " + e.getMessage());
        return;
    }
    TEntityTemplate.Properties definedProps = entityTemplate.getProperties();
    if (requiresProperties(entityType) && definedProps == null) {
        printAndAddError(id, "Properties required, but no properties defined");
        return;
    } else if (!requiresProperties(entityType) && definedProps != null) {
        printAndAddError(id, "No properties required by type, but properties were defined on template");
        return;
    } else if (definedProps == null) {
        // no properties required and none defined
        return;
    }
    if (definedProps instanceof TEntityTemplate.XmlProperties) {
        // check defined properties any against the xml schema
        @Nullable final Object any = ((TEntityTemplate.XmlProperties) definedProps).getAny();
        if (any == null) {
            printAndAddError(id, "Properties required, but no XmlProperties were empty (any case)");
            return;
        }
        TEntityType.PropertiesDefinition def = entityType.getProperties();
        if (def == null) {
            printAndAddError(id, "XmlProperties were given, but no XmlPropertiesDefinition was specified");
            return;
        }
        if (def instanceof TEntityType.XmlElementDefinition) {
            final QName element = ((TEntityType.XmlElementDefinition) def).getElement();
            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(id, "No Xml Schema definition found for " + element);
                return;
            }
            validate(repositoryFileReference, any, id);
        }
    } else if (definedProps instanceof TEntityTemplate.WineryKVProperties) {
        final WinerysPropertiesDefinition winerysPropertiesDefinition = entityType.getWinerysPropertiesDefinition();
        Map<String, String> kvProperties = ((TEntityTemplate.WineryKVProperties) definedProps).getKVProperties();
        if (kvProperties.isEmpty()) {
            printAndAddError(id, "Properties required, but no properties set (kvproperties case)");
            return;
        }
        for (PropertyDefinitionKV propertyDefinitionKV : winerysPropertiesDefinition.getPropertyDefinitions()) {
            String key = propertyDefinitionKV.getKey();
            if (kvProperties.get(key) == null) {
                printAndAddError(id, "Property " + key + " required, but not set.");
            } else {
                // removeNamespaceProperties 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(id, "Property " + o + " set, but not defined at schema.");
        }
    } else if (definedProps instanceof TEntityTemplate.YamlProperties) {
        // FIXME todo
        LOGGER.debug("YAML Properties checking is not yet implemented!");
    }
}
Also used : EntityTemplateId(org.eclipse.winery.model.ids.definitions.EntityTemplateId) PropertyDefinitionKV(org.eclipse.winery.model.tosca.extensions.kvproperties.PropertyDefinitionKV) TEntityTemplate(org.eclipse.winery.model.tosca.TEntityTemplate) TEntityType(org.eclipse.winery.model.tosca.TEntityType) QName(javax.xml.namespace.QName) WinerysPropertiesDefinition(org.eclipse.winery.model.tosca.extensions.kvproperties.WinerysPropertiesDefinition) Namespace(org.eclipse.winery.model.ids.Namespace) RepositoryFileReference(org.eclipse.winery.repository.common.RepositoryFileReference) Map(java.util.Map) HashMap(java.util.HashMap) Nullable(org.eclipse.jdt.annotation.Nullable)

Example 3 with Namespace

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

the class YamlRepository method getDefinitionsChildIds.

/**
 * Creates Set of Definitions Child Id Mapps xml definition to compatible yaml definition
 *
 * @param inputIdClass            requested id class
 * @param omitDevelopmentVersions omit development versions
 * @return set of definitions child id
 */
@Override
public <T extends DefinitionsChildId> SortedSet<T> getDefinitionsChildIds(Class<T> inputIdClass, boolean omitDevelopmentVersions) {
    SortedSet<T> res = new TreeSet<>();
    List<Class<T>> idClasses = new ArrayList<>();
    idClasses.add(inputIdClass);
    idClasses = convertDefinitionsChildIdIfNeeded(idClasses);
    for (Class<T> idClass : idClasses) {
        String rootPathFragment = IdUtil.getRootPathFragment(idClass);
        Path dir = this.getRepositoryRoot().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) {
                        List<XmlId> xmlIds = new ArrayList<>();
                        if (ArtifactTemplateId.class.isAssignableFrom(inputIdClass)) {
                            List<String> artifactNames = getAllArtifactNamesFromType(idP, idClass, ns.getDecoded());
                            for (String artifactName : artifactNames) {
                                xmlIds.add(new XmlId(artifactName + "@" + IdUtil.getFolderName(idClass), true));
                            }
                        } else {
                            xmlIds.add(new XmlId(idP.getFileName().toString(), true));
                        }
                        for (XmlId xmlId : xmlIds) {
                            if (omitDevelopmentVersions) {
                                WineryVersion version = VersionUtils.getVersion(xmlId.getDecoded());
                                if (version.toString().length() > 0 && version.getWorkInProgressVersion() > 0) {
                                    continue;
                                }
                            }
                            Constructor<T> constructor;
                            try {
                                constructor = inputIdClass.getConstructor(Namespace.class, XmlId.class);
                            } catch (Exception e) {
                                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) {
                                LOGGER.debug("Internal error at invocation of id constructor", e);
                                // abort everything, return invalid result
                                return res;
                            }
                            res.add(id);
                        }
                    }
                }
            }
        } catch (IOException e) {
            LOGGER.debug("Cannot close ds", e);
        }
    }
    return res;
}
Also used : Path(java.nio.file.Path) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Namespace(org.eclipse.winery.model.ids.Namespace) JAXBException(javax.xml.bind.JAXBException) InvocationTargetException(java.lang.reflect.InvocationTargetException) MultiException(org.eclipse.winery.model.converter.support.exception.MultiException) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) OnlyNonHiddenDirectories(org.eclipse.winery.repository.backend.filebased.OnlyNonHiddenDirectories) TreeSet(java.util.TreeSet) XmlId(org.eclipse.winery.model.ids.XmlId) WineryVersion(org.eclipse.winery.common.version.WineryVersion)

Example 4 with Namespace

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

the class ImportUtilsTest method getLocationForImportTest.

@Test
public void getLocationForImportTest() throws Exception {
    this.setRevisionTo("5fdcffa9ccd17743d5498cab0914081fc33606e9");
    XSDImportId id = new XSDImportId(new Namespace("http://opentosca.org/nodetypes", false), new XmlId("CloudProviderProperties", false));
    Optional<String> importLocation = ImportUtils.getLocation(repository, id);
    assertEquals(true, importLocation.isPresent());
}
Also used : XSDImportId(org.eclipse.winery.model.ids.definitions.imports.XSDImportId) XmlId(org.eclipse.winery.model.ids.XmlId) Namespace(org.eclipse.winery.model.ids.Namespace) Test(org.junit.jupiter.api.Test)

Example 5 with Namespace

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

the class MultiRepository method addNamespacesToRepository.

private void addNamespacesToRepository(IRepository repository, GenericId id) {
    if (id instanceof DefinitionsChildId) {
        Namespace namespace = ((DefinitionsChildId) id).getNamespace();
        String ns = namespace.getDecoded();
        IRepository r = determineRepositoryRef(repository);
        Set<String> set = repositoryGlobal.get(r);
        set.add(ns);
        repositoryGlobal.put(r, set);
        String pns;
        try {
            if (this.localRepository.getRepository() instanceof XmlRepository) {
                pns = namespace.getEncoded().substring(0, namespace.getEncoded().lastIndexOf(RepositoryUtils.getUrlSeparatorEncoded()));
            } else {
                pns = namespace.getEncoded();
            }
        } catch (UnsupportedEncodingException ex) {
            LOGGER.error("Error when generating the namespace", ex);
            return;
        }
        Set<Namespace> setPre = repositoryCommonNamespace.get(r);
        setPre.add(new Namespace(pns, true));
        repositoryCommonNamespace.put(r, setPre);
    }
}
Also used : XmlRepository(org.eclipse.winery.repository.xml.XmlRepository) DefinitionsChildId(org.eclipse.winery.model.ids.definitions.DefinitionsChildId) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IRepository(org.eclipse.winery.repository.backend.IRepository) Namespace(org.eclipse.winery.model.ids.Namespace)

Aggregations

Namespace (org.eclipse.winery.model.ids.Namespace)13 IOException (java.io.IOException)5 ArrayList (java.util.ArrayList)5 IRepository (org.eclipse.winery.repository.backend.IRepository)5 Path (java.nio.file.Path)4 QName (javax.xml.namespace.QName)4 XmlId (org.eclipse.winery.model.ids.XmlId)4 Map (java.util.Map)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 List (java.util.List)2 TreeSet (java.util.TreeSet)2 Collectors (java.util.stream.Collectors)2 JAXBException (javax.xml.bind.JAXBException)2 DocumentBuilder (javax.xml.parsers.DocumentBuilder)2 NonNull (org.eclipse.jdt.annotation.NonNull)2 Nullable (org.eclipse.jdt.annotation.Nullable)2 DefinitionsChildId (org.eclipse.winery.model.ids.definitions.DefinitionsChildId)2 EntityTemplateId (org.eclipse.winery.model.ids.definitions.EntityTemplateId)2