use of org.eclipse.winery.model.ids.definitions.ServiceTemplateId 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();
}
}
use of org.eclipse.winery.model.ids.definitions.ServiceTemplateId in project winery by eclipse.
the class DataFlowResource method mergeTemplateForDataSource.
/**
* Replace a filter corresponding to a running and placed data source by a ServiceTemplate with the same location
* and provider tag which contains a NodeTemplate of the NodeType specified for the data source filter.
*/
private TTopologyTemplate mergeTemplateForDataSource(TTopologyTemplate topology, NodeTypeId nodeTypeId, String templateName, Map<String, String> properties, String location, String provider) {
// get all ServiceTemplates in the repo
IRepository repo = RepositoryFactory.getRepository();
List<ServiceTemplateId> serviceTemplateIds = repo.getAllDefinitionsChildIds().stream().filter(id -> id.getGroup().equals(SERVICE_TEMPLATE_GROUP) && id instanceof ServiceTemplateId).map(id -> (ServiceTemplateId) id).collect(Collectors.toList());
for (ServiceTemplateId id : serviceTemplateIds) {
TServiceTemplate serviceTemplate = repo.getElement(id);
// only ServiceTemplates with location and provider tags are possible substitution candidates
if (containsMatchingTags(serviceTemplate, location, provider) && containsMatchingNodeType(serviceTemplate, nodeTypeId)) {
LOGGER.debug("Found suited substitution candidate for filter {}: {}", templateName, id.getQName());
TTopologyTemplate substitutionTopology = serviceTemplate.getTopologyTemplate();
TNodeTemplate filterCorrespondingNode = null;
LinkedHashMap<String, String> filterCorrespondingNodeProperties = null;
Map<String, String> nameMap = new HashMap<>();
if (substitutionTopology == null) {
continue;
}
// insert all NodeTemplates from the substitution candidate
for (TNodeTemplate node : substitutionTopology.getNodeTemplates()) {
LinkedHashMap<String, String> propertyList = ModelUtilities.getPropertiesKV(node);
if (propertyList == null) {
propertyList = new LinkedHashMap<>();
}
if (node.getType().equals(nodeTypeId.getQName())) {
// the NodeTemplate from the data flow model must be renamed to the given name
nameMap.put(node.getId(), templateName);
node.setId(templateName);
filterCorrespondingNode = node;
filterCorrespondingNodeProperties = propertyList;
} else if (Objects.nonNull(topology.getNodeTemplate(node.getId()))) {
// all existing names must be changed too
while (Objects.nonNull(topology.getNodeTemplate(node.getId() + "-" + IdCounter))) {
IdCounter++;
}
nameMap.put(node.getId(), node.getId() + "-" + IdCounter);
node.setId(node.getId() + "-" + IdCounter);
}
// update properties of the NodeTemplate if they are set at the filter
Map<String, String> propertyCopy = new HashMap<>(properties);
for (String propertyName : propertyCopy.keySet()) {
if (propertyName != null && propertyList.containsKey(propertyName)) {
propertyList.put(propertyName, properties.get(propertyName));
properties.remove(propertyName);
}
}
// set the state of all components related to the data source to running
propertyList.put("State", "Running");
ModelUtilities.setPropertiesKV(node, propertyList);
topology.addNodeTemplate(node);
}
// add all properties that are not defined in the NodeTypes to the node corresponding to the filter
if (Objects.nonNull(filterCorrespondingNode)) {
LOGGER.debug("{} properties defined without property at a matching type. Adding to filter " + "NodeTemplate!", properties.size());
for (String propertyName : properties.keySet()) {
filterCorrespondingNodeProperties.put(propertyName, properties.get(propertyName));
}
ModelUtilities.setPropertiesKV(filterCorrespondingNode, filterCorrespondingNodeProperties);
// add location and provider attribute to the NodeTemplate
filterCorrespondingNode.getOtherAttributes().put(ModelUtilities.NODE_TEMPLATE_REGION, location);
filterCorrespondingNode.getOtherAttributes().put(ModelUtilities.NODE_TEMPLATE_PROVIDER, provider);
}
// add all relations from the substitution fragment to the incomplete topology
for (TRelationshipTemplate relation : substitutionTopology.getRelationshipTemplates()) {
// update source id if it was changed
String sourceId = relation.getSourceElement().getRef().getId();
if (nameMap.containsKey(sourceId)) {
TNodeTemplate nodeTemplate = topology.getNodeTemplate(nameMap.get(sourceId));
if (nodeTemplate != null) {
relation.setSourceNodeTemplate(nodeTemplate);
}
}
// update target id if it was changed
if (nameMap.containsKey(relation.getTargetElement().getRef().getId())) {
relation.setTargetNodeTemplate(topology.getNodeTemplate(nameMap.get(relation.getTargetElement().getRef().getId())));
}
// update id if RelationshipTemplate with same id exists
if (Objects.nonNull(topology.getRelationshipTemplate(relation.getId()))) {
while (Objects.nonNull(topology.getRelationshipTemplate(relation.getId() + "-" + IdCounter))) {
IdCounter++;
}
relation.setId(relation.getId() + "-" + IdCounter);
}
topology.addRelationshipTemplate(relation);
}
return topology;
}
}
// no substitution of data source possible
return null;
}
use of org.eclipse.winery.model.ids.definitions.ServiceTemplateId in project winery by eclipse.
the class YamlRepositoryIntegrationTests method testGroupDefinitions.
@Test
public void testGroupDefinitions() throws Exception {
this.setRevisionTo("origin/yaml");
// Setup test
ServiceTemplateId id = new ServiceTemplateId(QName.valueOf("{example.org.tosca.servicetemplates}demo_w1-wip1"));
TServiceTemplate element = repository.getElement(id);
assertNotNull(element.getTopologyTemplate());
TNodeTemplate nodeTemplate = element.getTopologyTemplate().getNodeTemplate("compute_w1-wip1_0");
assertNotNull(nodeTemplate);
TGroupDefinition testGroup = new TGroupDefinition.Builder("test", QName.valueOf("{tosca.groups}Root")).setDescription("This is a description").addMembers(QName.valueOf(nodeTemplate.getId())).build();
// Save group
element.getTopologyTemplate().addGroup(testGroup);
BackendUtils.persist(repository, id, element);
// Assertions
element = repository.getElement(id);
assertNotNull(element.getTopologyTemplate());
assertNotNull(element.getTopologyTemplate().getGroups());
assertEquals(1, element.getTopologyTemplate().getGroups().size());
TGroupDefinition actualGroup = element.getTopologyTemplate().getGroups().get(0);
assertEquals(testGroup.getDescription(), actualGroup.getDescription());
assertEquals(testGroup.getMembers().get(0), actualGroup.getMembers().get(0));
}
use of org.eclipse.winery.model.ids.definitions.ServiceTemplateId in project winery by eclipse.
the class YamlRepositoryIntegrationTests method testGetServiceTemplate.
@Test
public void testGetServiceTemplate() throws Exception {
this.setRevisionTo("bab12e7a8ca7af1c0a0ce186c81bab3899ab989b");
assertEquals(10, repository.getAllDefinitionsChildIds().size());
TServiceTemplate element = repository.getElement(new ServiceTemplateId(QName.valueOf("{example.org.tosca.servicetemplates}demo_w1-wip1")));
assertNotNull(element);
assertNotNull(element.getTopologyTemplate());
assertEquals(3, element.getTopologyTemplate().getNodeTemplateOrRelationshipTemplate().size());
assertNotNull(element.getTopologyTemplate().getNodeTemplate("compute_w1-wip1_0"));
assertNotNull(element.getTopologyTemplate().getNodeTemplate("software_w1-wip1_0"));
TRelationshipTemplate relation = element.getTopologyTemplate().getRelationshipTemplate("con_hostedOn_0");
assertNotNull(relation);
assertEquals("software_w1-wip1_0", relation.getSourceElement().getRef().getId());
assertEquals("compute_w1-wip1_0", relation.getTargetElement().getRef().getId());
}
use of org.eclipse.winery.model.ids.definitions.ServiceTemplateId in project winery by eclipse.
the class YamlRepositoryIntegrationTests method testCreateGroupWithNoType.
@Test
public void testCreateGroupWithNoType() throws Exception {
this.setRevisionTo("origin/yaml");
// Setup test
ServiceTemplateId id = new ServiceTemplateId(QName.valueOf("{example.org.tosca.servicetemplates}demo_w1-wip1"));
TServiceTemplate element = repository.getElement(id);
assertNotNull(element.getTopologyTemplate());
TNodeTemplate nodeTemplate = element.getTopologyTemplate().getNodeTemplate("compute_w1-wip1_0");
assertNotNull(nodeTemplate);
TGroupDefinition testGroup = new TGroupDefinition.Builder("test", null).setDescription("This is a description").addMembers(QName.valueOf(nodeTemplate.getId())).build();
// Save group
element.getTopologyTemplate().addGroup(testGroup);
BackendUtils.persist(repository, id, element);
// Assertions
element = repository.getElement(id);
assertNotNull(element.getTopologyTemplate());
assertNotNull(element.getTopologyTemplate().getGroups());
assertEquals(1, element.getTopologyTemplate().getGroups().size());
TGroupDefinition actualGroup = element.getTopologyTemplate().getGroups().get(0);
assertEquals(QName.valueOf("{tosca.groups}Root"), actualGroup.getType());
}
Aggregations