Search in sources :

Example 1 with SERVICE_TEMPLATE_GROUP

use of org.eclipse.winery.model.adaptation.placement.Constants.SERVICE_TEMPLATE_GROUP 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;
}
Also used : URISyntaxException(java.net.URISyntaxException) LoggerFactory(org.slf4j.LoggerFactory) TServiceTemplate(org.eclipse.winery.model.tosca.TServiceTemplate) MediaType(javax.ws.rs.core.MediaType) Consumes(javax.ws.rs.Consumes) Map(java.util.Map) TRequirement(org.eclipse.winery.model.tosca.TRequirement) URI(java.net.URI) TAG_NAME_PROVIDER(org.eclipse.winery.model.adaptation.placement.Constants.TAG_NAME_PROVIDER) TDefinitions(org.eclipse.winery.model.tosca.TDefinitions) RELATIONSHIP_TEMPLATE_TRANSFER_TYPE_PULL(org.eclipse.winery.model.adaptation.placement.Constants.RELATIONSHIP_TEMPLATE_TRANSFER_TYPE_PULL) TNodeType(org.eclipse.winery.model.tosca.TNodeType) WinerysPropertiesDefinition(org.eclipse.winery.model.tosca.extensions.kvproperties.WinerysPropertiesDefinition) Collectors(java.util.stream.Collectors) RelationshipTypeId(org.eclipse.winery.model.ids.definitions.RelationshipTypeId) RepositoryFactory(org.eclipse.winery.repository.backend.RepositoryFactory) Objects(java.util.Objects) List(java.util.List) PropertyDefinitionKV(org.eclipse.winery.model.tosca.extensions.kvproperties.PropertyDefinitionKV) Response(javax.ws.rs.core.Response) IRepository(org.eclipse.winery.repository.backend.IRepository) TDeploymentArtifact(org.eclipse.winery.model.tosca.TDeploymentArtifact) Optional(java.util.Optional) RestUtils(org.eclipse.winery.repository.rest.RestUtils) QName(javax.xml.namespace.QName) ToscaBaseTypes(org.eclipse.winery.model.tosca.constants.ToscaBaseTypes) TRequirementDefinition(org.eclipse.winery.model.tosca.TRequirementDefinition) ServiceTemplateId(org.eclipse.winery.model.ids.definitions.ServiceTemplateId) TEntityTemplate(org.eclipse.winery.model.tosca.TEntityTemplate) HashMap(java.util.HashMap) TTopologyTemplate(org.eclipse.winery.model.tosca.TTopologyTemplate) LinkedHashMap(java.util.LinkedHashMap) ArtifactTemplateId(org.eclipse.winery.model.ids.definitions.ArtifactTemplateId) BackendUtils(org.eclipse.winery.repository.backend.BackendUtils) TNodeTemplate(org.eclipse.winery.model.tosca.TNodeTemplate) POST(javax.ws.rs.POST) Logger(org.slf4j.Logger) SERVICE_TEMPLATE_GROUP(org.eclipse.winery.model.adaptation.placement.Constants.SERVICE_TEMPLATE_GROUP) IOException(java.io.IOException) TTag(org.eclipse.winery.model.tosca.TTag) TAG_NAME_LOCATION(org.eclipse.winery.model.adaptation.placement.Constants.TAG_NAME_LOCATION) TArtifactTemplate(org.eclipse.winery.model.tosca.TArtifactTemplate) DataFlowModel(org.eclipse.winery.repository.rest.resources.apiData.DataFlowModel) TRelationshipType(org.eclipse.winery.model.tosca.TRelationshipType) ARTIFACT_TEMPLATE_GROUP(org.eclipse.winery.model.adaptation.placement.Constants.ARTIFACT_TEMPLATE_GROUP) TRelationshipTemplate(org.eclipse.winery.model.tosca.TRelationshipTemplate) ModelUtilities(org.eclipse.winery.model.tosca.utils.ModelUtilities) DATA_TRANSFER_TYPE_PULL(org.eclipse.winery.model.adaptation.placement.Constants.DATA_TRANSFER_TYPE_PULL) NodeTypeId(org.eclipse.winery.model.ids.definitions.NodeTypeId) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ServiceTemplateId(org.eclipse.winery.model.ids.definitions.ServiceTemplateId) TTopologyTemplate(org.eclipse.winery.model.tosca.TTopologyTemplate) TRelationshipTemplate(org.eclipse.winery.model.tosca.TRelationshipTemplate) IRepository(org.eclipse.winery.repository.backend.IRepository) TNodeTemplate(org.eclipse.winery.model.tosca.TNodeTemplate) TServiceTemplate(org.eclipse.winery.model.tosca.TServiceTemplate)

Aggregations

IOException (java.io.IOException)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 Map (java.util.Map)1 Objects (java.util.Objects)1 Optional (java.util.Optional)1 Collectors (java.util.stream.Collectors)1 Consumes (javax.ws.rs.Consumes)1 POST (javax.ws.rs.POST)1 MediaType (javax.ws.rs.core.MediaType)1 Response (javax.ws.rs.core.Response)1 QName (javax.xml.namespace.QName)1 ARTIFACT_TEMPLATE_GROUP (org.eclipse.winery.model.adaptation.placement.Constants.ARTIFACT_TEMPLATE_GROUP)1 DATA_TRANSFER_TYPE_PULL (org.eclipse.winery.model.adaptation.placement.Constants.DATA_TRANSFER_TYPE_PULL)1 RELATIONSHIP_TEMPLATE_TRANSFER_TYPE_PULL (org.eclipse.winery.model.adaptation.placement.Constants.RELATIONSHIP_TEMPLATE_TRANSFER_TYPE_PULL)1 SERVICE_TEMPLATE_GROUP (org.eclipse.winery.model.adaptation.placement.Constants.SERVICE_TEMPLATE_GROUP)1 TAG_NAME_LOCATION (org.eclipse.winery.model.adaptation.placement.Constants.TAG_NAME_LOCATION)1