Search in sources :

Example 16 with PropertyDefinitionKV

use of org.eclipse.winery.model.tosca.extensions.kvproperties.PropertyDefinitionKV in project winery by eclipse.

the class BackendUtils method deriveWPD.

/**
 * Derives Winery's Properties Definition from an existing properties definition
 *
 * @param ci     the entity type to try to modify the WPDs
 * @param errors the list to add errors to
 */
// FIXME this is specifically for xml backends and therefore broken under the new canonical model
public static void deriveWPD(TEntityType ci, List<String> errors, IRepository repository) {
    BackendUtils.LOGGER.trace("deriveWPD");
    TEntityType.PropertiesDefinition propertiesDefinition = ci.getProperties();
    if (propertiesDefinition == null) {
        // if there's no properties definition, there's nothing to derive because we're in YAML mode
        return;
    }
    if (!(propertiesDefinition instanceof TEntityType.XmlElementDefinition)) {
        BackendUtils.LOGGER.debug("only works for an element definition, not for types");
        return;
    }
    final QName element = ((TEntityType.XmlElementDefinition) propertiesDefinition).getElement();
    BackendUtils.LOGGER.debug("Looking for the definition of {" + element.getNamespaceURI() + "}" + element.getLocalPart());
    // fetch the XSD defining the element
    final XsdImportManager xsdImportManager = repository.getXsdImportManager();
    Map<String, RepositoryFileReference> mapFromLocalNameToXSD = xsdImportManager.getMapFromLocalNameToXSD(new Namespace(element.getNamespaceURI(), false), false);
    RepositoryFileReference ref = mapFromLocalNameToXSD.get(element.getLocalPart());
    if (ref == null) {
        String msg = "XSD not found for " + element.getNamespaceURI() + " / " + element.getLocalPart();
        BackendUtils.LOGGER.debug(msg);
        errors.add(msg);
        return;
    }
    final Optional<XSModel> xsModelOptional = BackendUtils.getXSModel(ref, repository);
    if (!xsModelOptional.isPresent()) {
        LOGGER.error("no XSModel found");
    }
    XSModel xsModel = xsModelOptional.get();
    XSElementDeclaration elementDeclaration = xsModel.getElementDeclaration(element.getLocalPart(), element.getNamespaceURI());
    if (elementDeclaration == null) {
        String msg = "XSD model claimed to contain declaration for {" + element.getNamespaceURI() + "}" + element.getLocalPart() + ", but it did not.";
        BackendUtils.LOGGER.debug(msg);
        errors.add(msg);
        return;
    }
    // go through the XSD definition and
    XSTypeDefinition typeDefinition = elementDeclaration.getTypeDefinition();
    if (!(typeDefinition instanceof XSComplexTypeDefinition)) {
        BackendUtils.LOGGER.debug("XSD does not follow the requirements put by winery: No Complex Type Definition");
        return;
    }
    XSComplexTypeDefinition cTypeDefinition = (XSComplexTypeDefinition) typeDefinition;
    XSParticle particle = cTypeDefinition.getParticle();
    if (particle == null) {
        BackendUtils.LOGGER.debug("XSD does not follow the requirements put by winery: Complex type does not contain particles");
        return;
    }
    XSTerm term = particle.getTerm();
    if (!(term instanceof XSModelGroup)) {
        BackendUtils.LOGGER.debug("XSD does not follow the requirements put by winery: Not a model group");
        return;
    }
    XSModelGroup modelGroup = (XSModelGroup) term;
    if (modelGroup.getCompositor() != XSModelGroup.COMPOSITOR_SEQUENCE) {
        BackendUtils.LOGGER.debug("XSD does not follow the requirements put by winery: Model group is not a sequence");
        return;
    }
    XSObjectList particles = modelGroup.getParticles();
    int len = particles.getLength();
    boolean everyThingIsASimpleType = true;
    List<PropertyDefinitionKV> list = new ArrayList<>();
    if (len != 0) {
        for (int i = 0; i < len; i++) {
            XSParticle innerParticle = (XSParticle) particles.item(i);
            XSTerm innerTerm = innerParticle.getTerm();
            if (innerTerm instanceof XSElementDeclaration) {
                XSElementDeclaration innerElementDeclaration = (XSElementDeclaration) innerTerm;
                String name = innerElementDeclaration.getName();
                XSTypeDefinition innerTypeDefinition = innerElementDeclaration.getTypeDefinition();
                if (innerTypeDefinition instanceof XSSimpleType) {
                    XSSimpleType xsSimpleType = (XSSimpleType) innerTypeDefinition;
                    String typeNS = xsSimpleType.getNamespace();
                    String typeName = xsSimpleType.getName();
                    if (typeNS.equals(XMLConstants.W3C_XML_SCHEMA_NS_URI)) {
                        PropertyDefinitionKV def = new PropertyDefinitionKV();
                        def.setKey(name);
                        // convention at WPD: use "xsd" as prefix for XML Schema Definition
                        def.setType("xsd:" + typeName);
                        list.add(def);
                    } else {
                        everyThingIsASimpleType = false;
                        break;
                    }
                } else {
                    everyThingIsASimpleType = false;
                    break;
                }
            } else {
                everyThingIsASimpleType = false;
                break;
            }
        }
    }
    if (!everyThingIsASimpleType) {
        BackendUtils.LOGGER.debug("XSD does not follow the requirements put by winery: Not all types in the sequence are simple types");
        return;
    }
    // everything went alright, we can add a WPD
    WinerysPropertiesDefinition wpd = new WinerysPropertiesDefinition();
    wpd.setIsDerivedFromXSD(Boolean.TRUE);
    wpd.setElementName(element.getLocalPart());
    wpd.setNamespace(element.getNamespaceURI());
    wpd.setPropertyDefinitions(list);
    ModelUtilities.replaceWinerysPropertiesDefinition(ci, wpd);
    BackendUtils.LOGGER.debug("Successfully generated WPD");
}
Also used : XSTypeDefinition(org.apache.xerces.xs.XSTypeDefinition) XSObjectList(org.apache.xerces.xs.XSObjectList) PropertyDefinitionKV(org.eclipse.winery.model.tosca.extensions.kvproperties.PropertyDefinitionKV) XSParticle(org.apache.xerces.xs.XSParticle) TEntityType(org.eclipse.winery.model.tosca.TEntityType) ArrayList(java.util.ArrayList) WinerysPropertiesDefinition(org.eclipse.winery.model.tosca.extensions.kvproperties.WinerysPropertiesDefinition) XSComplexTypeDefinition(org.apache.xerces.xs.XSComplexTypeDefinition) XSSimpleType(org.apache.xerces.impl.dv.XSSimpleType) XSElementDeclaration(org.apache.xerces.xs.XSElementDeclaration) XSModel(org.apache.xerces.xs.XSModel) XsdImportManager(org.eclipse.winery.repository.backend.xsd.XsdImportManager) XSModelGroup(org.apache.xerces.xs.XSModelGroup) XSTerm(org.apache.xerces.xs.XSTerm) QName(javax.xml.namespace.QName) HasTargetNamespace(org.eclipse.winery.model.tosca.HasTargetNamespace) Namespace(org.eclipse.winery.model.ids.Namespace) RepositoryFileReference(org.eclipse.winery.repository.common.RepositoryFileReference)

Example 17 with PropertyDefinitionKV

use of org.eclipse.winery.model.tosca.extensions.kvproperties.PropertyDefinitionKV in project winery by eclipse.

the class ConsistencyChecker method checkServiceTemplate.

private void checkServiceTemplate(ServiceTemplateId id) {
    TServiceTemplate serviceTemplate;
    try {
        serviceTemplate = configuration.getRepository().getElement(id);
    } catch (IllegalStateException e) {
        LOGGER.debug("Illegal State Exception during reading of id {}", id.toReadableString(), e);
        printAndAddError(id, "Reading error " + e.getMessage());
        return;
    }
    if (serviceTemplate.getTopologyTemplate() == null) {
        return;
    }
    @NonNull final List<TNodeTemplate> nodeTemplates = serviceTemplate.getTopologyTemplate().getNodeTemplates();
    for (TNodeTemplate nodeTemplate : nodeTemplates) {
        NodeTypeId nodeTypeId = new NodeTypeId(nodeTemplate.getType());
        TNodeType nodeType;
        try {
            nodeType = configuration.getRepository().getElement(nodeTypeId);
        } catch (IllegalStateException e) {
            LOGGER.debug("Illegal State Exception during reading of id {}", nodeTypeId.toReadableString(), e);
            printAndAddError(nodeTypeId, "Reading error " + e.getMessage());
            return;
        }
        final WinerysPropertiesDefinition winerysPropertiesDefinition = nodeType.getWinerysPropertiesDefinition();
        if (winerysPropertiesDefinition != null) {
            List<PropertyDefinitionKV> list = winerysPropertiesDefinition.getPropertyDefinitions();
            if (list != null) {
                // iterate on all defined properties
                for (PropertyDefinitionKV propdef : list) {
                    String key = propdef.getKey();
                    if (key == null) {
                        printAndAddError(id, "key is null");
                        continue;
                    }
                    // assign value, but change "null" to "" if no property is defined
                    final Map<String, String> propertiesKV = ModelUtilities.getPropertiesKV(nodeTemplate);
                    if (propertiesKV == null) {
                        printAndAddError(id, "propertiesKV of node template " + nodeTemplate.getId() + " is null");
                    }
                }
            }
        }
    }
}
Also used : PropertyDefinitionKV(org.eclipse.winery.model.tosca.extensions.kvproperties.PropertyDefinitionKV) NonNull(org.eclipse.jdt.annotation.NonNull) WinerysPropertiesDefinition(org.eclipse.winery.model.tosca.extensions.kvproperties.WinerysPropertiesDefinition) NodeTypeId(org.eclipse.winery.model.ids.definitions.NodeTypeId) TNodeTemplate(org.eclipse.winery.model.tosca.TNodeTemplate) TServiceTemplate(org.eclipse.winery.model.tosca.TServiceTemplate) TNodeType(org.eclipse.winery.model.tosca.TNodeType)

Example 18 with PropertyDefinitionKV

use of org.eclipse.winery.model.tosca.extensions.kvproperties.PropertyDefinitionKV in project winery by eclipse.

the class PropertiesDefinitionResource method getInheritedPropertiesDefinitionResource.

/**
 * Returns a list of properties definition of each parent.
 *
 * Only self-defined properties definition are considered.
 * Properties definition of a parent's parent are not merged into the properties definition of the parent.
 */
@GET
@Path("inherited")
@Produces(MediaType.APPLICATION_JSON)
public List<InheritedPropertiesDefinitionsResourceApiData> getInheritedPropertiesDefinitionResource() {
    ArrayList<TEntityType> parents = RepositoryFactory.getRepository().getParents(this.parentRes.getEntityType());
    ArrayList<InheritedPropertiesDefinitionsResourceApiData> list = new ArrayList<>();
    for (TEntityType parent : parents) {
        // Add winerys properties definition of parent if defined to list
        WinerysPropertiesDefinition winerysPropertiesDefinition = parent.getWinerysPropertiesDefinition();
        if (winerysPropertiesDefinition != null) {
            // Add derived from information
            List<PropertyDefinitionKV> propertyDefinitions = winerysPropertiesDefinition.getPropertyDefinitions();
            if (propertyDefinitions != null) {
                for (PropertyDefinitionKV propertyDefinition : propertyDefinitions) {
                    propertyDefinition.setDerivedFromType(parent.getQName());
                    propertyDefinition.setDerivedFromStatus("SELF");
                }
            }
            // Add winerys properties definition to list
            PropertiesDefinitionResourceApiData propertiesDefinitionResourceApiData = new PropertiesDefinitionResourceApiData(parent.getProperties(), winerysPropertiesDefinition);
            list.add(new InheritedPropertiesDefinitionsResourceApiData(parent.getQName(), propertiesDefinitionResourceApiData));
        }
    }
    return list;
}
Also used : PropertyDefinitionKV(org.eclipse.winery.model.tosca.extensions.kvproperties.PropertyDefinitionKV) PropertiesDefinitionResourceApiData(org.eclipse.winery.repository.rest.resources.apiData.PropertiesDefinitionResourceApiData) TEntityType(org.eclipse.winery.model.tosca.TEntityType) InheritedPropertiesDefinitionsResourceApiData(org.eclipse.winery.repository.rest.resources.apiData.InheritedPropertiesDefinitionsResourceApiData) ArrayList(java.util.ArrayList) WinerysPropertiesDefinition(org.eclipse.winery.model.tosca.extensions.kvproperties.WinerysPropertiesDefinition) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 19 with PropertyDefinitionKV

use of org.eclipse.winery.model.tosca.extensions.kvproperties.PropertyDefinitionKV in project winery by eclipse.

the class PropertiesDefinitionResource method getMerged.

/**
 * Merge properties definitions with inherited definitions.
 */
@GET
@Path("merged")
@Produces(MediaType.APPLICATION_JSON)
public PropertiesDefinitionResourceApiData getMerged() {
    // Get complete inheritance hierarchy
    List<TEntityType> hierarchy = RepositoryFactory.getRepository().getParentsAndChild(this.getEntityType());
    // Merge properties definitions
    List<PropertyDefinitionKV> propertyDefinitions = ModelUtilities.mergePropertiesDefinitions(hierarchy);
    // Create new WPD
    WinerysPropertiesDefinition winerysPropertiesDefinition = new WinerysPropertiesDefinition();
    winerysPropertiesDefinition.setElementName(this.getEntityType().getName());
    winerysPropertiesDefinition.setNamespace(this.getEntityType().getTargetNamespace());
    winerysPropertiesDefinition.setPropertyDefinitions(propertyDefinitions);
    return new PropertiesDefinitionResourceApiData(this.getEntityType().getProperties(), winerysPropertiesDefinition);
}
Also used : PropertyDefinitionKV(org.eclipse.winery.model.tosca.extensions.kvproperties.PropertyDefinitionKV) PropertiesDefinitionResourceApiData(org.eclipse.winery.repository.rest.resources.apiData.PropertiesDefinitionResourceApiData) TEntityType(org.eclipse.winery.model.tosca.TEntityType) WinerysPropertiesDefinition(org.eclipse.winery.model.tosca.extensions.kvproperties.WinerysPropertiesDefinition) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 20 with PropertyDefinitionKV

use of org.eclipse.winery.model.tosca.extensions.kvproperties.PropertyDefinitionKV in project winery by eclipse.

the class PropertiesDefinitionResource method onJsonPost.

@POST
@Consumes(MediaType.APPLICATION_JSON)
public Response onJsonPost(PropertiesDefinitionResourceApiData data) {
    // CASE: XML
    if (data.selectedValue == PropertiesDefinitionEnum.Element || data.selectedValue == PropertiesDefinitionEnum.Type) {
        // first of all, remove Winery's Properties definition (if it exists)
        ModelUtilities.removeWinerysPropertiesDefinition(this.getEntityType());
        // FIXME need to actually handle propertiesData properly!
        if (data.propertiesDefinition == null) {
            return Response.status(Status.BAD_REQUEST).entity("Wrong data submitted!").build();
        }
        this.getEntityType().setProperties(data.propertiesDefinition);
        List<String> errors = new ArrayList<>();
        BackendUtils.deriveWPD(this.getEntityType(), errors, RepositoryFactory.getRepository());
        // currently the errors are just logged
        for (String error : errors) {
            PropertiesDefinitionResource.LOGGER.debug(error);
        }
        return RestUtils.persist(this.parentRes);
    }
    // Only definitions are stored which are not defined by any parent
    if (data.selectedValue == PropertiesDefinitionEnum.Custom) {
        ArrayList<TEntityType> parents = RepositoryFactory.getRepository().getParents(this.parentRes.getEntityType());
        // Get all definitions defined by any parent
        List<PropertyDefinitionKV> parentsPropertiesDefinitions = new ArrayList<>();
        for (TEntityType parent : parents) {
            WinerysPropertiesDefinition winerysPropertiesDefinition = parent.getWinerysPropertiesDefinition();
            if (winerysPropertiesDefinition != null) {
                parentsPropertiesDefinitions.addAll(winerysPropertiesDefinition.getPropertyDefinitions());
            }
        }
        // Get only definitions that are not defined by any parent
        List<PropertyDefinitionKV> definitions = new ArrayList<>();
        for (PropertyDefinitionKV definition : data.winerysPropertiesDefinition.getPropertyDefinitions()) {
            boolean exists = false;
            for (PropertyDefinitionKV currentDefinition : parentsPropertiesDefinitions) {
                if (definition.equals(currentDefinition)) {
                    exists = true;
                    break;
                }
            }
            if (!exists) {
                definitions.add(definition);
            }
        }
        // Update and store definitions
        data.winerysPropertiesDefinition.setPropertyDefinitions(definitions);
        this.getEntityType().setProperties(data.winerysPropertiesDefinition);
        return RestUtils.persist(this.parentRes);
    }
    // CASE: YAML
    if (data.selectedValue == PropertiesDefinitionEnum.Yaml) {
        TEntityType entityType = this.parentRes.getEntityType();
        if (!(data.propertiesDefinition instanceof TEntityType.YamlPropertiesDefinition)) {
            return Response.status(Status.BAD_REQUEST).entity("Expected YamlPropertiesDefinition element").build();
        }
        entityType.setProperties(data.propertiesDefinition);
        return RestUtils.persist(this.parentRes);
    }
    // OTHERWISE: throw error
    return Response.status(Status.BAD_REQUEST).entity("Wrong data submitted!").build();
}
Also used : PropertyDefinitionKV(org.eclipse.winery.model.tosca.extensions.kvproperties.PropertyDefinitionKV) TEntityType(org.eclipse.winery.model.tosca.TEntityType) ArrayList(java.util.ArrayList) WinerysPropertiesDefinition(org.eclipse.winery.model.tosca.extensions.kvproperties.WinerysPropertiesDefinition) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes)

Aggregations

PropertyDefinitionKV (org.eclipse.winery.model.tosca.extensions.kvproperties.PropertyDefinitionKV)21 WinerysPropertiesDefinition (org.eclipse.winery.model.tosca.extensions.kvproperties.WinerysPropertiesDefinition)16 TEntityType (org.eclipse.winery.model.tosca.TEntityType)11 ArrayList (java.util.ArrayList)9 LinkedHashMap (java.util.LinkedHashMap)8 QName (javax.xml.namespace.QName)8 TNodeType (org.eclipse.winery.model.tosca.TNodeType)8 NodeTypeId (org.eclipse.winery.model.ids.definitions.NodeTypeId)7 IOException (java.io.IOException)6 TEntityTemplate (org.eclipse.winery.model.tosca.TEntityTemplate)6 TNodeTemplate (org.eclipse.winery.model.tosca.TNodeTemplate)6 TRelationshipTemplate (org.eclipse.winery.model.tosca.TRelationshipTemplate)6 IRepository (org.eclipse.winery.repository.backend.IRepository)6 Map (java.util.Map)5 Path (javax.ws.rs.Path)5 Produces (javax.ws.rs.Produces)5 HashMap (java.util.HashMap)4 Consumes (javax.ws.rs.Consumes)4 POST (javax.ws.rs.POST)4 List (java.util.List)3