Search in sources :

Example 16 with WinerysPropertiesDefinition

use of org.eclipse.winery.model.tosca.extensions.kvproperties.WinerysPropertiesDefinition 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 17 with WinerysPropertiesDefinition

use of org.eclipse.winery.model.tosca.extensions.kvproperties.WinerysPropertiesDefinition 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 18 with WinerysPropertiesDefinition

use of org.eclipse.winery.model.tosca.extensions.kvproperties.WinerysPropertiesDefinition 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 19 with WinerysPropertiesDefinition

use of org.eclipse.winery.model.tosca.extensions.kvproperties.WinerysPropertiesDefinition 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)

Example 20 with WinerysPropertiesDefinition

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

the class PropertiesDefinitionDeserializer method deserialize.

@Override
public TEntityType.PropertiesDefinition deserialize(JsonParser parser, DeserializationContext context) throws IOException, JsonProcessingException {
    // read as ObjectNode to enable removing empty properties
    ObjectNode node = parser.getCodec().readTree(parser);
    final JavaType targetType;
    if (node.hasNonNull("propertyDefinitionKVList")) {
        // deserialize as WinerysPropertiesDefinition
        targetType = context.constructType(WinerysPropertiesDefinition.class);
    } else if (node.hasNonNull("element")) {
        targetType = context.constructType(TEntityType.XmlElementDefinition.class);
        // remove unused properties to avoid tripping up the Json Parsing
        node.remove("type");
        node.remove("properties");
    } else if (node.hasNonNull("type")) {
        targetType = context.constructType(TEntityType.XmlTypeDefinition.class);
        // remove unused properties to avoid tripping up the Json Parsing
        node.remove("element");
        node.remove("properties");
    } else if (node.hasNonNull("properties")) {
        targetType = context.constructType(TEntityType.YamlPropertiesDefinition.class);
        // remove unused properties to avoid tripping up the Json Parsing
        node.remove("type");
        node.remove("element");
    } else {
        // must not throw an exception because PropertiesDefinitionAPIData can contain a null element
        return null;
    }
    JsonDeserializer<Object> deserializer = context.findNonContextualValueDeserializer(targetType);
    // create a new JsonParser for the delegate deserializer to account for consumed input in original parser.
    JsonParser objectParser = node.traverse();
    // advance the parser by one token because the parser initialized by node.traverse() stays at "before start"
    objectParser.nextToken();
    return (TEntityType.PropertiesDefinition) deserializer.deserialize(objectParser, context);
}
Also used : JavaType(com.fasterxml.jackson.databind.JavaType) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) TEntityType(org.eclipse.winery.model.tosca.TEntityType) WinerysPropertiesDefinition(org.eclipse.winery.model.tosca.extensions.kvproperties.WinerysPropertiesDefinition) WinerysPropertiesDefinition(org.eclipse.winery.model.tosca.extensions.kvproperties.WinerysPropertiesDefinition) JsonParser(com.fasterxml.jackson.core.JsonParser)

Aggregations

WinerysPropertiesDefinition (org.eclipse.winery.model.tosca.extensions.kvproperties.WinerysPropertiesDefinition)22 PropertyDefinitionKV (org.eclipse.winery.model.tosca.extensions.kvproperties.PropertyDefinitionKV)15 TEntityType (org.eclipse.winery.model.tosca.TEntityType)13 QName (javax.xml.namespace.QName)8 TEntityTemplate (org.eclipse.winery.model.tosca.TEntityTemplate)8 ArrayList (java.util.ArrayList)7 TNodeType (org.eclipse.winery.model.tosca.TNodeType)7 LinkedHashMap (java.util.LinkedHashMap)6 TNodeTemplate (org.eclipse.winery.model.tosca.TNodeTemplate)6 NodeTypeId (org.eclipse.winery.model.ids.definitions.NodeTypeId)5 IOException (java.io.IOException)4 Produces (javax.ws.rs.Produces)4 TInterface (org.eclipse.winery.model.tosca.TInterface)4 TRelationshipTemplate (org.eclipse.winery.model.tosca.TRelationshipTemplate)4 TRelationshipType (org.eclipse.winery.model.tosca.TRelationshipType)4 TRequirement (org.eclipse.winery.model.tosca.TRequirement)4 IRepository (org.eclipse.winery.repository.backend.IRepository)4 Consumes (javax.ws.rs.Consumes)3 GET (javax.ws.rs.GET)3 POST (javax.ws.rs.POST)3