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();
}
}
Aggregations