use of org.eclipse.winery.repository.exceptions.AllocationException in project winery by eclipse.
the class FulfillPolicies method assignTargetLabels.
private Map<TNodeTemplate, Set<String>> assignTargetLabels(TopologyWrapper topology) throws AllocationException {
Map<TNodeTemplate, Set<String>> possibleTargetLabels = new HashMap<>();
Map<TNodeTemplate, List<PolicyWrapper>> policiesForNTs = getPoliciesForNTs(topology, params);
for (TNodeTemplate topLevelNT : topology.getTopLevelNTs()) {
// set present target labels
Optional<String> targetLabelOptional = ModelUtilities.getTargetLabel(topLevelNT);
if (targetLabelOptional.isPresent()) {
possibleTargetLabels.put(topLevelNT, Collections.singleton(targetLabelOptional.get()));
continue;
} else {
possibleTargetLabels.put(topLevelNT, new HashSet<>());
}
// get matching fragments
List<TTopologyTemplate> matchingFragments = fragmentsCache.getAllMatchingFragments(topology, topLevelNT).values().stream().flatMap(List::stream).collect(Collectors.toList());
// remove matching fragments that don't fulfill the policies
matchingFragments = new PolicyComparison(policiesForNTs.get(topLevelNT), matchingFragments).getFragmentsFulfillingPolicies();
for (TTopologyTemplate fragment : matchingFragments) {
ModelUtilities.getTargetLabel(fragment.getNodeTemplates().get(0)).ifPresent(targetLabel -> possibleTargetLabels.get(topLevelNT).add(targetLabel));
}
if (possibleTargetLabels.get(topLevelNT).isEmpty()) {
throw new AllocationException("Couldn't find fragments from providers fulfilling specified policies");
}
}
return possibleTargetLabels;
}
use of org.eclipse.winery.repository.exceptions.AllocationException in project winery by eclipse.
the class Allocation method getCriteria.
private Criteria getCriteria(AllocationRequest.CriteriaRequest criteriaRequest, int outputCap) throws AllocationException {
String criteriaType = criteriaRequest.getCriteria();
JsonNode params = criteriaRequest.getCriteriaParams();
switch(criteriaType) {
case "FulfillPolicies":
return new FulfillPolicies(params, outputCap);
case "MinHosts":
return new MinHosts(params, outputCap);
case "MinExternalConnections":
return new MinExternalConnections(params, outputCap);
default:
throw new AllocationException("Couldn't find criteria: " + criteriaType);
}
}
use of org.eclipse.winery.repository.exceptions.AllocationException in project winery by eclipse.
the class Allocation method save.
private List<ServiceTemplateId> save(ServiceTemplateId originalId, List<TopologyWrapper> allocatedTopologies, String suffix) throws IOException, AllocationException {
if (allocatedTopologies.isEmpty()) {
throw new AllocationException("No topologies were created");
}
List<ServiceTemplateId> allocatedIds = new ArrayList<>();
for (int i = 0; i < allocatedTopologies.size(); i++) {
// generate id
ServiceTemplateId allocatedId = new ServiceTemplateId(originalId.getNamespace().getDecoded(), originalId.getXmlId().getDecoded() + "-" + suffix + (i + 1), false);
repository.forceDelete(allocatedId);
repository.flagAsExisting(allocatedId);
// generate and save service template
TServiceTemplate allocated = createServiceTemplate(allocatedTopologies.get(i));
repository.setElement(allocatedId, allocated);
allocatedIds.add(allocatedId);
}
LOGGER.debug("Saved " + allocatedIds.size() + " allocated topologies");
return allocatedIds;
}
use of org.eclipse.winery.repository.exceptions.AllocationException in project winery by eclipse.
the class MinHosts method getPossibleMatches.
/**
* Calculate all possible replacements for a topology by traversing starting from the top level hosts
* and selecting first match.
*/
@Override
protected List<List<PermutationHelper>> getPossibleMatches(TopologyWrapper topology) throws AllocationException {
Map<TNodeTemplate, List<TTopologyTemplate>> possibleReplacements = new HashMap<>();
List<TNodeTemplate> done = new ArrayList<>();
for (TNodeTemplate topLevelHost : topology.getTopLevelHosts()) {
TNodeTemplate next = topLevelHost;
// at this point all top level nts have been assigned target labels by TargetLabelAssignment
String targetLabel = ModelUtilities.getTargetLabel(topology.getHostedOnPredecessors(topLevelHost).get(0)).get();
List<TTopologyTemplate> possibleReplacementsForNT = null;
while (next != null && !done.contains(next)) {
possibleReplacementsForNT = ProviderRepository.INSTANCE.getPaaSFragments(targetLabel, topology.getPredecessorRequirements(next));
if (!possibleReplacementsForNT.isEmpty()) {
possibleReplacements.put(next, possibleReplacementsForNT);
done.add(next);
done.addAll(topology.getTransitiveTopLevelHostPredecessors(next));
// needed because in case all predecessors had target labels not all nts were removed during assignment
topology.removeNotNeededSuccessors(next);
break;
}
next = topology.getHostedOnSuccessor(next);
}
if (!done.contains(next) && (possibleReplacementsForNT == null || possibleReplacementsForNT.isEmpty())) {
throw new AllocationException("No matching fragments found");
}
}
return toPermutationHelpers(possibleReplacements);
}
use of org.eclipse.winery.repository.exceptions.AllocationException in project winery by eclipse.
the class FragmentsCache method getAllMatchingFragments.
/**
* @param topLevelNT nt to start traversing from
* @return all successors (including nt itself) and their matching fragments
*/
public Map<TNodeTemplate, List<TTopologyTemplate>> getAllMatchingFragments(TopologyWrapper topology, TNodeTemplate topLevelNT) throws AllocationException {
Set<String> targetLabels = new HashSet<>();
if (ModelUtilities.getTargetLabel(topLevelNT).isPresent()) {
targetLabels.add(ModelUtilities.getTargetLabel(topLevelNT).get());
} else {
targetLabels = presentTargetLabels;
}
Map<TNodeTemplate, List<TTopologyTemplate>> allFragments = new HashMap<>();
TNodeTemplate next = topLevelNT;
while (next != null) {
for (String targetLabel : targetLabels) {
List<TRequirement> requirements = topology.getRequirements(next);
// use QName type to avoid checking all variables of the requirements with equals
Set<QName> reqTypes = requirements.stream().map(TRequirement::getType).collect(Collectors.toSet());
List<TTopologyTemplate> fragments;
CachingKey cachingKey = new CachingKey(targetLabel, reqTypes);
if (cachedFragments.containsKey(cachingKey)) {
fragments = cachedFragments.get(cachingKey);
} else {
fragments = ProviderRepository.INSTANCE.getPaaSFragments(targetLabel, requirements);
cachedFragments.put(cachingKey, fragments);
}
if (fragments.isEmpty()) {
continue;
}
if (allFragments.get(next) == null) {
allFragments.put(next, new ArrayList<>(fragments));
} else {
allFragments.get(next).addAll(fragments);
}
}
next = topology.getHostedOnSuccessor(next);
}
if (allFragments.values().stream().flatMap(List::stream).count() == 0) {
throw new AllocationException("No matching fragments found for NT " + topLevelNT.getId() + " with target labels " + targetLabels);
}
return allFragments;
}
Aggregations