Search in sources :

Example 1 with EntityTemplateId

use of org.eclipse.winery.model.ids.definitions.EntityTemplateId 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 2 with EntityTemplateId

use of org.eclipse.winery.model.ids.definitions.EntityTemplateId in project winery by eclipse.

the class AbstractComponentsWithTypeReferenceResource method onJsonPost.

@POST
@Consumes(MediaType.APPLICATION_JSON)
public Response onJsonPost(QNameWithTypeApiData jsonData) {
    // only check for type parameter as namespace and name are checked in super.onPost
    if (StringUtils.isEmpty(jsonData.type)) {
        return Response.status(Status.BAD_REQUEST).build();
    }
    ResourceResult creationResult = super.onPost(jsonData.namespace, jsonData.localname);
    if (!creationResult.isSuccess()) {
        return creationResult.getResponse();
    }
    if (creationResult.getStatus().equals(Status.CREATED)) {
        final DefinitionsChildId id = (DefinitionsChildId) creationResult.getId();
        final TDefinitions definitions = requestRepository.getDefinitions(id);
        final TExtensibleElements element = definitions.getElement();
        ((HasType) element).setType(jsonData.type);
        if (id instanceof EntityTemplateId) {
            BackendUtils.initializeProperties(requestRepository, (TEntityTemplate) element);
        }
        try {
            BackendUtils.persist(requestRepository, id, definitions);
        } catch (IOException e) {
            throw new WebApplicationException(e);
        }
    }
    return creationResult.getResponse();
}
Also used : EntityTemplateId(org.eclipse.winery.model.ids.definitions.EntityTemplateId) WebApplicationException(javax.ws.rs.WebApplicationException) DefinitionsChildId(org.eclipse.winery.model.ids.definitions.DefinitionsChildId) HasType(org.eclipse.winery.model.tosca.HasType) TExtensibleElements(org.eclipse.winery.model.tosca.TExtensibleElements) IOException(java.io.IOException) TDefinitions(org.eclipse.winery.model.tosca.TDefinitions) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes)

Aggregations

EntityTemplateId (org.eclipse.winery.model.ids.definitions.EntityTemplateId)2 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Consumes (javax.ws.rs.Consumes)1 POST (javax.ws.rs.POST)1 WebApplicationException (javax.ws.rs.WebApplicationException)1 QName (javax.xml.namespace.QName)1 Nullable (org.eclipse.jdt.annotation.Nullable)1 Namespace (org.eclipse.winery.model.ids.Namespace)1 DefinitionsChildId (org.eclipse.winery.model.ids.definitions.DefinitionsChildId)1 HasType (org.eclipse.winery.model.tosca.HasType)1 TDefinitions (org.eclipse.winery.model.tosca.TDefinitions)1 TEntityTemplate (org.eclipse.winery.model.tosca.TEntityTemplate)1 TEntityType (org.eclipse.winery.model.tosca.TEntityType)1 TExtensibleElements (org.eclipse.winery.model.tosca.TExtensibleElements)1 PropertyDefinitionKV (org.eclipse.winery.model.tosca.extensions.kvproperties.PropertyDefinitionKV)1 WinerysPropertiesDefinition (org.eclipse.winery.model.tosca.extensions.kvproperties.WinerysPropertiesDefinition)1 RepositoryFileReference (org.eclipse.winery.repository.common.RepositoryFileReference)1