Search in sources :

Example 1 with DataFlowModel

use of org.eclipse.winery.repository.rest.resources.apiData.DataFlowModel in project winery by eclipse.

the class DataFlowResource method parseDataFlowToServiceTemplate.

@POST
@Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public Response parseDataFlowToServiceTemplate(DataFlowModel dataFlowModel) {
    if (Objects.isNull(dataFlowModel)) {
        return Response.status(Response.Status.BAD_REQUEST).entity("Passed data flow model is null!").build();
    }
    if (Objects.isNull(dataFlowModel.getId().getNamespaceURI())) {
        return Response.status(Response.Status.BAD_REQUEST).entity("Namespace must be defined for the data flow " + "model ID!").build();
    }
    IRepository repo = RepositoryFactory.getRepository();
    ServiceTemplateId templateId = new ServiceTemplateId(dataFlowModel.getId());
    if (repo.exists(templateId)) {
        return Response.status(Response.Status.CONFLICT).entity("ServiceTemplate with name of the data flow model already exists!").build();
    }
    TDefinitions definitions = BackendUtils.createWrapperDefinitionsAndInitialEmptyElement(repo, templateId);
    TServiceTemplate serviceTemplate = definitions.getServiceTemplates().stream().filter(template -> template.getId().equals(templateId.getQName().getLocalPart()) && templateId.getQName().getNamespaceURI().equals(template.getTargetNamespace())).findFirst().orElse(null);
    if (Objects.isNull(serviceTemplate)) {
        return Response.serverError().entity("Unable to create ServiceTemplate for the given data flow model!").build();
    }
    TTopologyTemplate topology = serviceTemplate.getTopologyTemplate();
    if (Objects.isNull(topology)) {
        topology = new TTopologyTemplate.Builder().build();
    }
    // iterate over all filters of the data flow and create corresponding NodeTemplates
    for (DataFlowModel.Filter filter : dataFlowModel.getFilters()) {
        if (Objects.isNull(filter.getType())) {
            return Response.serverError().entity("Type is missing for a filter!").build();
        }
        NodeTypeId nodeTypeId = BackendUtils.getDefinitionsChildId(NodeTypeId.class, filter.getType());
        if (!repo.exists(nodeTypeId)) {
            TNodeType newNodeType = new TNodeType.Builder(nodeTypeId.getQName().getLocalPart()).setTargetNamespace(nodeTypeId.getQName().getNamespaceURI()).build();
            try {
                BackendUtils.persist(repo, nodeTypeId, newNodeType);
            } catch (IOException e) {
                return Response.serverError().entity("Unable to create NodeType " + filter.getType() + " which is not contained in the repository!").build();
            }
        }
        topology = handleFilter(topology, nodeTypeId, filter.getId(), filter.getProperties(), filter.getArtifacts(), filter.getLocation(), filter.getProvider());
        if (Objects.isNull(topology)) {
            return Response.serverError().entity("Unable to handle filter with name: " + filter.getId()).build();
        }
    }
    // without available connectsTo RelationshipType the transformation can not be done
    RelationshipTypeId relationTypeId = BackendUtils.getDefinitionsChildId(RelationshipTypeId.class, ToscaBaseTypes.connectsToRelationshipType);
    if (!repo.exists(relationTypeId)) {
        return Response.serverError().entity("Unable to parse data flow model without available connectsTo " + "RelationshipType!").build();
    }
    // create connectsTo RelationshipTemplates between NodeTemplates corresponding to connected filters
    for (DataFlowModel.Pipes pipe : dataFlowModel.getPipes()) {
        if (Objects.isNull(pipe.getSource()) || Objects.isNull(pipe.getTarget())) {
            return Response.serverError().entity("Unable to create RelationshipTemplate for pipe with source or " + "target equal to null!").build();
        }
        TNodeTemplate source = topology.getNodeTemplate(pipe.getSource());
        TNodeTemplate target = topology.getNodeTemplate(pipe.getTarget());
        if (Objects.isNull(source) || Objects.isNull(target)) {
            return Response.serverError().entity("Unable to find NodeTemplates for relation with source: " + pipe.getSource() + " and target: " + pipe.getTarget()).build();
        }
        TRelationshipTemplate relationshipTemplate = createRelationshipTemplate(relationTypeId, source, target, pipe.getDataTransferType());
        if (Objects.isNull(relationshipTemplate)) {
            return Response.serverError().entity("Unable to create RelationshipTemplate between " + source.getId() + " and " + target.getId()).build();
        }
        topology.addRelationshipTemplate(relationshipTemplate);
    }
    serviceTemplate.setTopologyTemplate(topology);
    try {
        BackendUtils.persist(repo, templateId, definitions);
        return Response.created(new URI(RestUtils.getAbsoluteURL(templateId))).build();
    } catch (IOException e) {
        return Response.serverError().entity("IOException while persisting ServiceTemplate for data flow model!").build();
    } catch (URISyntaxException e) {
        return Response.serverError().entity("Unable to parse URI for created ServiceTemplate!").build();
    }
}
Also used : RelationshipTypeId(org.eclipse.winery.model.ids.definitions.RelationshipTypeId) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) ServiceTemplateId(org.eclipse.winery.model.ids.definitions.ServiceTemplateId) DataFlowModel(org.eclipse.winery.repository.rest.resources.apiData.DataFlowModel) URI(java.net.URI) TNodeType(org.eclipse.winery.model.tosca.TNodeType) TTopologyTemplate(org.eclipse.winery.model.tosca.TTopologyTemplate) TRelationshipTemplate(org.eclipse.winery.model.tosca.TRelationshipTemplate) NodeTypeId(org.eclipse.winery.model.ids.definitions.NodeTypeId) IRepository(org.eclipse.winery.repository.backend.IRepository) TDefinitions(org.eclipse.winery.model.tosca.TDefinitions) TNodeTemplate(org.eclipse.winery.model.tosca.TNodeTemplate) TServiceTemplate(org.eclipse.winery.model.tosca.TServiceTemplate) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes)

Aggregations

IOException (java.io.IOException)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 Consumes (javax.ws.rs.Consumes)1 POST (javax.ws.rs.POST)1 NodeTypeId (org.eclipse.winery.model.ids.definitions.NodeTypeId)1 RelationshipTypeId (org.eclipse.winery.model.ids.definitions.RelationshipTypeId)1 ServiceTemplateId (org.eclipse.winery.model.ids.definitions.ServiceTemplateId)1 TDefinitions (org.eclipse.winery.model.tosca.TDefinitions)1 TNodeTemplate (org.eclipse.winery.model.tosca.TNodeTemplate)1 TNodeType (org.eclipse.winery.model.tosca.TNodeType)1 TRelationshipTemplate (org.eclipse.winery.model.tosca.TRelationshipTemplate)1 TServiceTemplate (org.eclipse.winery.model.tosca.TServiceTemplate)1 TTopologyTemplate (org.eclipse.winery.model.tosca.TTopologyTemplate)1 IRepository (org.eclipse.winery.repository.backend.IRepository)1 DataFlowModel (org.eclipse.winery.repository.rest.resources.apiData.DataFlowModel)1