use of org.eclipse.winery.model.tosca.TTopologyTemplate in project winery by eclipse.
the class Splitting method getInputParamListofIncomingRelationshipTemplates.
public List<TParameter> getInputParamListofIncomingRelationshipTemplates(TTopologyTemplate topologyTemplate, List<TRelationshipTemplate> listOfIncomingRelationshipTemplates) {
List<TParameter> listOfInputs = new ArrayList<>();
IRepository repo = RepositoryFactory.getRepository();
for (TRelationshipTemplate incomingRelationshipTemplate : listOfIncomingRelationshipTemplates) {
TNodeTemplate incomingNodetemplate = ModelUtilities.getSourceNodeTemplateOfRelationshipTemplate(topologyTemplate, incomingRelationshipTemplate);
NodeTypeId incomingNodeTypeId = new NodeTypeId(incomingNodetemplate.getType());
TNodeType incomingNodeType = repo.getElement(incomingNodeTypeId);
List<TInterface> incomingNodeTypeInterfaces = incomingNodeType.getInterfaces();
RelationshipTypeId incomingRelationshipTypeId = new RelationshipTypeId(incomingRelationshipTemplate.getType());
if (!incomingNodeTypeInterfaces.isEmpty()) {
TInterface relevantInterface = null;
List<TInterface> connectionInterfaces = incomingNodeTypeInterfaces.stream().filter(tInterface -> tInterface.getIdFromIdOrNameField().contains("connection")).collect(Collectors.toList());
if (connectionInterfaces.size() > 1) {
TNodeTemplate targetNodeTemplate = ModelUtilities.getTargetNodeTemplateOfRelationshipTemplate(topologyTemplate, incomingRelationshipTemplate);
for (TInterface tInterface : connectionInterfaces) {
int separator = tInterface.getIdFromIdOrNameField().lastIndexOf("/");
String prefixRelation = tInterface.getIdFromIdOrNameField().substring(separator + 1);
if (targetNodeTemplate.getName() != null && targetNodeTemplate.getName().toLowerCase().contains(prefixRelation.toLowerCase())) {
relevantInterface = tInterface;
}
}
} else {
relevantInterface = connectionInterfaces.get(0);
}
if (relevantInterface != null) {
for (TOperation tOperation : relevantInterface.getOperations()) {
List<TParameter> inputParameters = tOperation.getInputParameters();
if (inputParameters != null) {
listOfInputs.addAll(inputParameters);
}
}
}
}
}
return listOfInputs;
}
use of org.eclipse.winery.model.tosca.TTopologyTemplate in project winery by eclipse.
the class Splitting method split.
/**
* Splits a topology template according to the attached target labels. The target labels attached to nodes determine
* at which target the nodes should be deployed. The result is a topology template containing for each target the
* required nodes. Duplicates nodes which host nodes with different target labels.
*
* @param topologyTemplate the topology template which should be split
* @return split topologyTemplate
*/
public TTopologyTemplate split(TTopologyTemplate topologyTemplate) throws SplittingException {
if (!checkValidTopology(topologyTemplate)) {
throw new SplittingException("Topology is not valid");
}
// Copy for incremental removal of the processed nodes
TTopologyTemplate topologyTemplateCopy = BackendUtils.clone(topologyTemplate);
HashSet<TNodeTemplate> nodeTemplatesWhichPredecessorsHasNoPredecessors = new HashSet<>(getNodeTemplatesWhichPredecessorsHasNoPredecessors(topologyTemplateCopy));
// Consider each node which hostedOn-predecessor nodes have no further hostedOn-predecessors
while (!nodeTemplatesWhichPredecessorsHasNoPredecessors.isEmpty()) {
for (TNodeTemplate currentNode : nodeTemplatesWhichPredecessorsHasNoPredecessors) {
List<TNodeTemplate> predecessors = getHostedOnPredecessorsOfNodeTemplate(topologyTemplateCopy, currentNode);
Set<String> predecessorsTargetLabel = new HashSet<>();
for (TNodeTemplate predecessor : predecessors) {
Optional<String> targetLabel = ModelUtilities.getTargetLabel(predecessor);
if (!targetLabel.isPresent()) {
LOGGER.error("No target label present");
LOGGER.error("id " + predecessor.getId());
}
// noinspection OptionalGetWithoutIsPresent
predecessorsTargetLabel.add(targetLabel.get().toLowerCase());
}
// If all predecessors have the same target label assign this label to the considered node
if (predecessorsTargetLabel.size() == 1) {
// noinspection OptionalGetWithoutIsPresent
ModelUtilities.setTargetLabel(currentNode, ModelUtilities.getTargetLabel(predecessors.get(0)).get());
} else {
List<TRelationshipTemplate> incomingRelationships = ModelUtilities.getIncomingRelationshipTemplates(topologyTemplateCopy, currentNode);
List<TRelationshipTemplate> outgoingRelationships = ModelUtilities.getOutgoingRelationshipTemplates(topologyTemplateCopy, currentNode);
// Otherwise, duplicate the considered node for each target label
for (String targetLabel : predecessorsTargetLabel) {
TNodeTemplate duplicatedNode = BackendUtils.clone(currentNode);
duplicatedNode.setId(Util.makeNCName(currentNode.getId() + "-" + targetLabel));
duplicatedNode.setName(Util.makeNCName(currentNode.getName() + "-" + targetLabel));
topologyTemplate.getNodeTemplateOrRelationshipTemplate().add(duplicatedNode);
topologyTemplateCopy.getNodeTemplateOrRelationshipTemplate().add(duplicatedNode);
ModelUtilities.setTargetLabel(duplicatedNode, targetLabel);
for (TRelationshipTemplate incomingRelationship : incomingRelationships) {
Object sourceElementIncommingRel = incomingRelationship.getSourceElement().getRef();
/*
* incoming hostedOn relationships from predecessors with the same label and not hostedOn
* relationships (e.g. conntectsTo) are assigned to the duplicated node.
* The origin relationships are duplicated
*/
TNodeTemplate sourceNodeTemplate = ModelUtilities.getSourceNodeTemplateOfRelationshipTemplate(topologyTemplateCopy, incomingRelationship);
if (((ModelUtilities.getTargetLabel(sourceNodeTemplate).get().equalsIgnoreCase(ModelUtilities.getTargetLabel(duplicatedNode).get()) && getBasisRelationshipType(incomingRelationship.getType()).getValidTarget().getTypeRef().getLocalPart().equalsIgnoreCase("Container")) || !predecessors.contains(sourceNodeTemplate))) {
List<TRelationshipTemplate> reassignRelationship = new ArrayList<>();
reassignRelationship.add(incomingRelationship);
// Reassign incoming relationships
List<TRelationshipTemplate> reassignedRelationship = reassignIncomingRelationships(reassignRelationship, duplicatedNode);
topologyTemplate.getNodeTemplateOrRelationshipTemplate().addAll(reassignedRelationship);
topologyTemplateCopy.getNodeTemplateOrRelationshipTemplate().addAll(reassignedRelationship);
}
}
/*
* Reassign outgoing relationships. No difference between the relationship types.
* Origin outgoing relationships are duplicated and added to the duplicated node as source
*/
List<TRelationshipTemplate> newOutgoingRelationships = reassignOutgoingRelationships(outgoingRelationships, duplicatedNode);
topologyTemplate.getNodeTemplateOrRelationshipTemplate().addAll(newOutgoingRelationships);
topologyTemplateCopy.getNodeTemplateOrRelationshipTemplate().addAll(newOutgoingRelationships);
}
// Remove the original node and its relations from the origin topology template and the copy
topologyTemplate.getNodeTemplateOrRelationshipTemplate().remove(currentNode);
topologyTemplate.getNodeTemplateOrRelationshipTemplate().removeAll(outgoingRelationships);
topologyTemplate.getNodeTemplateOrRelationshipTemplate().removeAll(incomingRelationships);
topologyTemplateCopy.getNodeTemplateOrRelationshipTemplate().remove(currentNode);
topologyTemplateCopy.getNodeTemplateOrRelationshipTemplate().removeAll(outgoingRelationships);
topologyTemplateCopy.getNodeTemplateOrRelationshipTemplate().removeAll(incomingRelationships);
}
// Remove the hostedOn-predecessors of the considered node and their relations in the working copy
topologyTemplateCopy.getNodeTemplateOrRelationshipTemplate().removeAll(predecessors);
List<TRelationshipTemplate> removingRelationships = topologyTemplateCopy.getRelationshipTemplates().stream().filter(rt -> predecessors.contains(ModelUtilities.getSourceNodeTemplateOfRelationshipTemplate(topologyTemplateCopy, rt)) || predecessors.contains(ModelUtilities.getTargetNodeTemplateOfRelationshipTemplate(topologyTemplateCopy, rt))).collect(Collectors.toList());
topologyTemplateCopy.getNodeTemplateOrRelationshipTemplate().removeAll(removingRelationships);
}
nodeTemplatesWhichPredecessorsHasNoPredecessors.clear();
nodeTemplatesWhichPredecessorsHasNoPredecessors.addAll(getNodeTemplatesWhichPredecessorsHasNoPredecessors(topologyTemplateCopy));
}
return topologyTemplate;
}
use of org.eclipse.winery.model.tosca.TTopologyTemplate in project winery by eclipse.
the class Splitting method hostMatchingWithDefaultHostSelection.
/**
* Selects default the first entry of the list of possible hosts for each node, which requires a new host
*
* @param topologyTemplate which need new hosts added
* @return topologyTemplate with randomly chosen hosts
*/
public TTopologyTemplate hostMatchingWithDefaultHostSelection(TTopologyTemplate topologyTemplate) throws SplittingException {
TTopologyTemplate newTopologyTemplate = BackendUtils.clone(topologyTemplate);
Map<String, List<TTopologyTemplate>> matchingOptions = getHostingInjectionOptions(newTopologyTemplate);
Map<String, TTopologyTemplate> defaultHostSelection = new HashMap<>();
matchingOptions.entrySet().forEach(entry -> defaultHostSelection.put(entry.getKey(), entry.getValue().get(0)));
return injectNodeTemplates(topologyTemplate, defaultHostSelection, InjectRemoval.REMOVE_REPLACED_AND_SUCCESSORS);
}
use of org.eclipse.winery.model.tosca.TTopologyTemplate in project winery by eclipse.
the class Splitting method extractSuccessorsOfNode.
private List<TNodeTemplate> extractSuccessorsOfNode(TTopologyTemplate topologyTemplate, List<TNodeTemplate> nodeTemplates, TNodeTemplate nodeTemplate) {
List<TNodeTemplate> successorsOfNodeTemplate = new ArrayList<>();
List<TRelationshipTemplate> outgoingRelationships = ModelUtilities.getOutgoingRelationshipTemplates(topologyTemplate, nodeTemplate);
if (listIsNotNullOrEmpty(outgoingRelationships)) {
for (TRelationshipTemplate relationshipTemplate : outgoingRelationships) {
if (relationshipTemplate.getSourceElement().getRef() instanceof TNodeTemplate) {
successorsOfNodeTemplate.add((TNodeTemplate) relationshipTemplate.getTargetElement().getRef());
} else {
TCapability targetElement = (TCapability) relationshipTemplate.getTargetElement().getRef();
successorsOfNodeTemplate.add(nodeTemplates.stream().filter(nt -> nt.getCapabilities() != null).filter(nt -> nt.getCapabilities().stream().anyMatch(c -> c.getId().equals(targetElement.getId()))).findAny().get());
}
}
}
return successorsOfNodeTemplate;
}
use of org.eclipse.winery.model.tosca.TTopologyTemplate in project winery by eclipse.
the class Splitting method matchTopologyOfServiceTemplate.
/**
* Splits the topology template of the given service template. Creates a new service template with "-split" suffix
* as id. Any existing "-split" service template will be deleted. Matches the split topology template to the cloud
* providers according to the target labels. Creates a new service template with "-matched" suffix as id. Any
* existing "-matched" service template will be deleted.
*
* @param id of the ServiceTemplate switch should be split and matched to cloud providers
* @return id of the ServiceTemplate which contains the matched topology
*/
public ServiceTemplateId matchTopologyOfServiceTemplate(ServiceTemplateId id) throws Exception {
long start = System.currentTimeMillis();
IRepository repository = RepositoryFactory.getRepository();
TServiceTemplate serviceTemplate = repository.getElement(id);
TTopologyTemplate topologyTemplate = serviceTemplate.getTopologyTemplate();
/*
Get all open requirements and the basis type of the required capability type
Two different basis types are distinguished:
"Container" which means a hostedOn injection is required
"Endpoint" which means a connectsTo injection is required
*/
Map<TRequirement, String> requirementsAndMatchingBasisCapabilityTypes = getOpenRequirementsAndMatchingBasisCapabilityTypeNames(topologyTemplate);
// Output check
LOGGER.debug("Matching for ServiceTemplate with ID: {}", id.getQName());
for (TRequirement req : requirementsAndMatchingBasisCapabilityTypes.keySet()) {
LOGGER.debug("Open Requirement: {}", req.getId());
LOGGER.debug("Matching basis type: {}", requirementsAndMatchingBasisCapabilityTypes.get(req));
}
TTopologyTemplate matchedConnectedTopologyTemplate;
if (requirementsAndMatchingBasisCapabilityTypes.containsValue("Container")) {
// set default target labels if they are not yet set
if (!hasTargetLabels(topologyTemplate)) {
LOGGER.debug("Target labels are not set for all NodeTemplates. Using default target labels.");
topologyTemplate.getNodeTemplates().forEach(t -> ModelUtilities.setTargetLabel(t, "*"));
}
TTopologyTemplate matchedHostsTopologyTemplate = hostMatchingWithDefaultHostSelection(topologyTemplate);
if (requirementsAndMatchingBasisCapabilityTypes.containsValue("Endpoint")) {
matchedConnectedTopologyTemplate = connectionMatchingWithDefaultConnectorSelection(matchedHostsTopologyTemplate);
} else {
matchedConnectedTopologyTemplate = matchedHostsTopologyTemplate;
}
} else if (requirementsAndMatchingBasisCapabilityTypes.containsValue("Endpoint")) {
matchedConnectedTopologyTemplate = connectionMatchingWithDefaultConnectorSelection(topologyTemplate);
} else {
throw new SplittingException("No open Requirements which can be matched");
}
TTopologyTemplate daSpecifiedTopology = matchedConnectedTopologyTemplate;
// Start additional functionality Driver Injection
if (!DASpecification.getNodeTemplatesWithAbstractDAs(matchedConnectedTopologyTemplate).isEmpty() && DASpecification.getNodeTemplatesWithAbstractDAs(matchedConnectedTopologyTemplate) != null) {
daSpecifiedTopology = DriverInjection.injectDriver(matchedConnectedTopologyTemplate);
}
// End additional functionality Driver Injection
// create wrapper service template
ServiceTemplateId matchedServiceTemplateId = new ServiceTemplateId(id.getNamespace().getDecoded(), VersionSupport.getNewComponentVersionId(id, "matched"), false);
RepositoryFactory.getRepository().forceDelete(matchedServiceTemplateId);
RepositoryFactory.getRepository().flagAsExisting(matchedServiceTemplateId);
repository.flagAsExisting(matchedServiceTemplateId);
TServiceTemplate matchedServiceTemplate = new TServiceTemplate();
matchedServiceTemplate.setName(matchedServiceTemplateId.getXmlId().getDecoded());
matchedServiceTemplate.setId(matchedServiceTemplate.getName());
matchedServiceTemplate.setTargetNamespace(id.getNamespace().getDecoded());
matchedServiceTemplate.setTopologyTemplate(daSpecifiedTopology);
LOGGER.debug("Persisting...");
repository.setElement(matchedServiceTemplateId, matchedServiceTemplate);
LOGGER.debug("Persisted.");
long duration = System.currentTimeMillis() - start;
LOGGER.debug("Execution Time in millisec: " + duration + "ms");
return matchedServiceTemplateId;
}
Aggregations