use of org.eclipse.winery.model.tosca.TTopologyTemplate in project winery by eclipse.
the class TopologyTemplateResource method applyGroupingAndPlacement.
@POST
@Path("applyplacement")
@Produces(MediaType.APPLICATION_JSON)
public Response applyGroupingAndPlacement(@Context UriInfo uriInfo) {
try {
TTopologyTemplate topology = PlacementUtils.groupAndPlaceComponents((ServiceTemplateId) this.parent.getId(), this.topologyTemplate);
this.parent.setTopology(topology, this.type);
RestUtils.persist(this.parent);
ServiceTemplateId thisServiceTemplateId = (ServiceTemplateId) this.parent.getId();
URI url = uriInfo.getBaseUri().resolve(RestUtils.getAbsoluteURL(thisServiceTemplateId));
return Response.created(url).build();
} catch (InvalidParameterException e) {
LOGGER.debug("Error while grouping and placing: {}", e.getMessage());
return Response.serverError().entity(e.getMessage()).build();
}
}
use of org.eclipse.winery.model.tosca.TTopologyTemplate in project winery by eclipse.
the class TopologyTemplateResource method updateVersionOfNodeTemplate.
@POST
@Path("update")
@Consumes(MediaType.APPLICATION_JSON)
public TTopologyTemplate updateVersionOfNodeTemplate(UpdateInfo updateInfo) {
TTopologyTemplate localTemplate = updateInfo.getTopologyTemplate() == null ? this.topologyTemplate : updateInfo.getTopologyTemplate();
TNodeTemplate nodeTemplate = localTemplate.getNodeTemplate(updateInfo.getNodeTemplateId());
if (nodeTemplate != null && nodeTemplate.getProperties() != null) {
Map<String, String> propertyMappings = new LinkedHashMap<>();
updateInfo.getMappingList().forEach(propertyMatching -> propertyMappings.put(propertyMatching.getOldKey(), propertyMatching.getNewKey()));
LinkedHashMap<String, String> resultKvs = new LinkedHashMap<>();
Map<String, String> oldKvs = ModelUtilities.getPropertiesKV(nodeTemplate);
if (oldKvs != null) {
oldKvs.forEach((key, value) -> {
if (propertyMappings.containsKey(key)) {
resultKvs.put(propertyMappings.get(key), value);
}
if (updateInfo.getResolvedList().contains(key)) {
resultKvs.put(key, value);
}
});
updateInfo.getNewList().forEach(key -> {
if (!resultKvs.containsKey(key)) {
resultKvs.put(key, "");
}
});
ModelUtilities.setPropertiesKV(nodeTemplate, resultKvs);
}
}
BackendUtils.updateVersionOfNodeTemplate(localTemplate, updateInfo.getNodeTemplateId(), updateInfo.getNewComponentType());
if (updateInfo.isSaveAfterUpdate()) {
this.setModel(localTemplate);
}
return localTemplate;
}
use of org.eclipse.winery.model.tosca.TTopologyTemplate in project winery by eclipse.
the class TopologyTemplateResource method applyAvailableFeatures.
@PUT
@Path("availablefeatures")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public TTopologyTemplate applyAvailableFeatures(ArrayList<AvailableFeaturesApiData> featuresList) {
Map<String, Map<QName, String>> featureMap = new HashMap<>();
featuresList.forEach(availableFeaturesApiData -> {
HashMap<QName, String> featureTypesMap = new HashMap<>();
if (Objects.nonNull(availableFeaturesApiData.getFeatures()) && availableFeaturesApiData.getFeatures().size() > 0) {
availableFeaturesApiData.getFeatures().forEach(features -> featureTypesMap.put(features.getType(), features.getFeatureName()));
featureMap.put(availableFeaturesApiData.getNodeTemplateId(), featureTypesMap);
}
});
TTopologyTemplate enrichedTopology = EnhancementUtils.applyFeaturesForTopology(this.topologyTemplate, featureMap);
this.parent.setTopology(enrichedTopology, this.type);
RestUtils.persist(this.parent);
return enrichedTopology;
}
use of org.eclipse.winery.model.tosca.TTopologyTemplate in project winery by eclipse.
the class Splitting method splitTopologyOfServiceTemplate.
/**
* 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 splitTopologyOfServiceTemplate(ServiceTemplateId id) throws SplittingException, IOException {
long start = System.currentTimeMillis();
IRepository repository = RepositoryFactory.getRepository();
TServiceTemplate serviceTemplate = repository.getElement(id);
// create wrapper service template
ServiceTemplateId splitServiceTemplateId = new ServiceTemplateId(id.getNamespace().getDecoded(), VersionSupport.getNewComponentVersionId(id, "split"), false);
repository.forceDelete(splitServiceTemplateId);
repository.flagAsExisting(splitServiceTemplateId);
TServiceTemplate splitServiceTemplate = new TServiceTemplate();
splitServiceTemplate.setName(splitServiceTemplateId.getXmlId().getDecoded());
splitServiceTemplate.setId(splitServiceTemplate.getName());
splitServiceTemplate.setTargetNamespace(id.getNamespace().getDecoded());
TTopologyTemplate splitTopologyTemplate = split(serviceTemplate.getTopologyTemplate());
splitServiceTemplate.setTopologyTemplate(splitTopologyTemplate);
LOGGER.debug("Persisting...");
repository.setElement(splitServiceTemplateId, splitServiceTemplate);
LOGGER.debug("Persisted.");
// create wrapper service template
ServiceTemplateId matchedServiceTemplateId = new ServiceTemplateId(id.getNamespace().getDecoded(), VersionSupport.getNewComponentVersionId(id, "split-matched"), false);
repository.forceDelete(matchedServiceTemplateId);
repository.flagAsExisting(matchedServiceTemplateId);
TServiceTemplate matchedServiceTemplate = new TServiceTemplate();
matchedServiceTemplate.setName(matchedServiceTemplateId.getXmlId().getDecoded());
matchedServiceTemplate.setId(matchedServiceTemplate.getName());
matchedServiceTemplate.setTargetNamespace(id.getNamespace().getDecoded());
TTopologyTemplate matchedTopologyTemplate = hostMatchingWithDefaultHostSelection(splitTopologyTemplate);
matchedServiceTemplate.setTopologyTemplate(matchedTopologyTemplate);
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;
}
use of org.eclipse.winery.model.tosca.TTopologyTemplate in project winery by eclipse.
the class Splitting method getHostingInjectionOptions.
/**
* This method returns the possible hosts for each lowest level node. Before a suitable node is found nodes may be
* removed.
*
* @param topologyTemplate which should be mapped to the cloud provider according to the attached target labels -
* target labels have to be attached to each node
* @return map with a list of possible hosts for each lowest level node
*/
public Map<String, List<TTopologyTemplate>> getHostingInjectionOptions(TTopologyTemplate topologyTemplate) throws SplittingException {
ProviderRepository repository = new ProviderRepository();
Map<String, List<TTopologyTemplate>> injectionOptions = new HashMap<>();
// Contains all nodes for which at least one cloud provider node is found to host them
List<TNodeTemplate> nodesForWhichHostsFound = new ArrayList<>();
nodesForWhichHostsFound.clear();
List<TNodeTemplate> needHostNodeTemplateCandidates = getNodeTemplatesWithoutOutgoingHostedOnRelationships(topologyTemplate);
List<TNodeTemplate> nodesToCheck = new ArrayList<>();
// Find lowest level nodes with open requirements which means they can be hosted by an other component
for (TNodeTemplate nodeTemplateCandidate : needHostNodeTemplateCandidates) {
if (hasNodeOpenRequirement(topologyTemplate, nodeTemplateCandidate)) {
if (nodeTemplateCandidate.getRequirements() != null && nodeTemplateCandidate.getRequirements().stream().anyMatch(req -> getBasisCapabilityType(getRequiredCapabilityTypeQNameOfRequirement(req)).getName().equalsIgnoreCase("Container"))) {
nodesToCheck.add(nodeTemplateCandidate);
}
}
}
LOGGER.debug("Start...");
if (!nodesToCheck.isEmpty()) {
// Check all lowest level nodes with open requirements if a compatible node is available
for (TNodeTemplate needHostNode : nodesToCheck) {
Optional<String> label = ModelUtilities.getTargetLabel(needHostNode);
if (!label.isPresent()) {
LOGGER.error("No target label present");
LOGGER.error("id " + needHostNode.getId());
throw new SplittingException("No target label present for Node Template " + needHostNode.getId());
}
// noinspection OptionalGetWithoutIsPresent
String targetLabel = ModelUtilities.getTargetLabel(needHostNode).get();
List<TRequirement> openHostedOnRequirements = needHostNode.getRequirements().stream().filter(req -> getBasisCapabilityType(getRequiredCapabilityTypeQNameOfRequirement(req)).getName().equalsIgnoreCase("Container")).collect(Collectors.toList());
List<TTopologyTemplate> compatibleTopologyFragments = repository.getAllTopologyFragmentsForLocationAndOfferingCapability(targetLabel, openHostedOnRequirements.get(0));
LOGGER.debug("Found {} compatible topology fragments for NodeTemplate {}", compatibleTopologyFragments.size(), needHostNode.getId());
// Add compatible nodes to the injectionOptions to host the considered lowest level node
if (!compatibleTopologyFragments.isEmpty()) {
injectionOptions.put(needHostNode.getId(), compatibleTopologyFragments);
nodesForWhichHostsFound.add(needHostNode);
}
}
}
LOGGER.debug("Nodes to check: {}; Nodes with found host: {}", nodesToCheck.size(), nodesForWhichHostsFound.size());
/*
* Only the lowest components which are not in the matching list and which have still hostedOn-predecessors
* are candidates which can be replaced by an other node
*/
List<TNodeTemplate> replacementNodeTemplateCandidates = getReplacementNodeTemplateCandidatesForMatching(topologyTemplate, nodesForWhichHostsFound);
while (!replacementNodeTemplateCandidates.isEmpty()) {
for (TNodeTemplate replacementCandidate : replacementNodeTemplateCandidates) {
List<TNodeTemplate> predecessorsOfReplacementCandidate = getHostedOnPredecessorsOfNodeTemplate(topologyTemplate, replacementCandidate);
Optional<String> label = ModelUtilities.getTargetLabel(replacementCandidate);
if (!label.isPresent()) {
LOGGER.error("No target label present");
LOGGER.error("id " + replacementCandidate.getId());
throw new SplittingException("No target label present for Node Template " + replacementCandidate.getId());
}
// noinspection OptionalGetWithoutIsPresent
String targetLabel = ModelUtilities.getTargetLabel(replacementCandidate).get();
// For each replacement candidate the predecessors are considered
for (TNodeTemplate predecessor : predecessorsOfReplacementCandidate) {
// Check if a compatible node for the predecessor from the right provider is available
if (predecessor.getRequirements() == null) {
nodesForWhichHostsFound.add(predecessor);
// throw new SplittingException("The Node Template with the ID " + predecessor.getId() + " has no requirement assigned and the injected can't be processed");
} else {
List<TRequirement> openHostedOnRequirements = predecessor.getRequirements().stream().filter(req -> getBasisCapabilityType(getRequiredCapabilityTypeQNameOfRequirement(req)).getName().equalsIgnoreCase("Container")).collect(Collectors.toList());
List<TTopologyTemplate> compatibleTopologyFragments = repository.getAllTopologyFragmentsForLocationAndOfferingCapability(targetLabel, openHostedOnRequirements.get(0));
// Add compatible nodes to the injectionOptions to host the considered lowest level node
if (!compatibleTopologyFragments.isEmpty()) {
injectionOptions.put(predecessor.getId(), compatibleTopologyFragments);
nodesForWhichHostsFound.add(predecessor);
}
}
}
}
// Delete all replacement candidates and their relationships.
topologyTemplate.getNodeTemplateOrRelationshipTemplate().removeAll(replacementNodeTemplateCandidates);
List<TRelationshipTemplate> removingIncomingRelationships = ModelUtilities.getAllRelationshipTemplates(topologyTemplate).stream().filter(ir -> replacementNodeTemplateCandidates.contains(ir.getTargetElement().getRef())).collect(Collectors.toList());
topologyTemplate.getNodeTemplateOrRelationshipTemplate().removeAll(removingIncomingRelationships);
List<TRelationshipTemplate> removingOutgoingRelationships = ModelUtilities.getAllRelationshipTemplates(topologyTemplate).stream().filter(ir -> replacementNodeTemplateCandidates.contains(ir.getSourceElement().getRef())).collect(Collectors.toList());
topologyTemplate.getNodeTemplateOrRelationshipTemplate().removeAll(removingIncomingRelationships);
topologyTemplate.getNodeTemplateOrRelationshipTemplate().removeAll(removingOutgoingRelationships);
// The former predecessors now are either matched by a new host or they are new replacement candidates
replacementNodeTemplateCandidates.clear();
replacementNodeTemplateCandidates.addAll(getReplacementNodeTemplateCandidatesForMatching(topologyTemplate, nodesForWhichHostsFound));
}
/*
* Check if all lowest level nodes are contained in the nodesForWhichHostsFound, i.e., at least one
* cloud provider node is found to host them.
* The application-specific nodes must not be replacement candidates!
*/
List<TNodeTemplate> checkListAllNodesMatched = topologyTemplate.getNodeTemplates().stream().filter(z -> getNodeTemplatesWithoutOutgoingHostedOnRelationships(topologyTemplate).contains(z)).filter(node -> hasNodeOpenRequirement(topologyTemplate, node)).filter(y -> !nodesForWhichHostsFound.contains(y)).collect(Collectors.toList());
LOGGER.debug("{} nodes without matching:", checkListAllNodesMatched.size());
for (TNodeTemplate node : checkListAllNodesMatched) {
LOGGER.debug(node.getId());
}
if (!checkListAllNodesMatched.isEmpty()) {
throw new SplittingException("No matching possible");
}
return injectionOptions;
}
Aggregations