Search in sources :

Example 1 with AllocationException

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;
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) HashMap(java.util.HashMap) TTopologyTemplate(org.eclipse.winery.model.tosca.TTopologyTemplate) ArrayList(java.util.ArrayList) List(java.util.List) TNodeTemplate(org.eclipse.winery.model.tosca.TNodeTemplate) AllocationException(org.eclipse.winery.repository.exceptions.AllocationException)

Example 2 with AllocationException

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);
    }
}
Also used : MinHosts(org.eclipse.winery.repository.targetallocation.criteria.minhosts.MinHosts) FulfillPolicies(org.eclipse.winery.repository.targetallocation.criteria.fulfillpolicies.FulfillPolicies) MinExternalConnections(org.eclipse.winery.repository.targetallocation.criteria.minexternalconnections.MinExternalConnections) JsonNode(com.fasterxml.jackson.databind.JsonNode) AllocationException(org.eclipse.winery.repository.exceptions.AllocationException)

Example 3 with AllocationException

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;
}
Also used : ArrayList(java.util.ArrayList) ServiceTemplateId(org.eclipse.winery.model.ids.definitions.ServiceTemplateId) AllocationException(org.eclipse.winery.repository.exceptions.AllocationException) TServiceTemplate(org.eclipse.winery.model.tosca.TServiceTemplate)

Example 4 with AllocationException

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);
}
Also used : HashMap(java.util.HashMap) TTopologyTemplate(org.eclipse.winery.model.tosca.TTopologyTemplate) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) TNodeTemplate(org.eclipse.winery.model.tosca.TNodeTemplate) AllocationException(org.eclipse.winery.repository.exceptions.AllocationException)

Example 5 with AllocationException

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;
}
Also used : HashMap(java.util.HashMap) QName(javax.xml.namespace.QName) TRequirement(org.eclipse.winery.model.tosca.TRequirement) TTopologyTemplate(org.eclipse.winery.model.tosca.TTopologyTemplate) ArrayList(java.util.ArrayList) List(java.util.List) TNodeTemplate(org.eclipse.winery.model.tosca.TNodeTemplate) HashSet(java.util.HashSet) AllocationException(org.eclipse.winery.repository.exceptions.AllocationException)

Aggregations

AllocationException (org.eclipse.winery.repository.exceptions.AllocationException)6 ArrayList (java.util.ArrayList)5 List (java.util.List)4 TNodeTemplate (org.eclipse.winery.model.tosca.TNodeTemplate)4 HashMap (java.util.HashMap)3 TTopologyTemplate (org.eclipse.winery.model.tosca.TTopologyTemplate)3 HashSet (java.util.HashSet)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 Map (java.util.Map)1 Set (java.util.Set)1 QName (javax.xml.namespace.QName)1 ServiceTemplateId (org.eclipse.winery.model.ids.definitions.ServiceTemplateId)1 TRequirement (org.eclipse.winery.model.tosca.TRequirement)1 TServiceTemplate (org.eclipse.winery.model.tosca.TServiceTemplate)1 FulfillPolicies (org.eclipse.winery.repository.targetallocation.criteria.fulfillpolicies.FulfillPolicies)1 MinExternalConnections (org.eclipse.winery.repository.targetallocation.criteria.minexternalconnections.MinExternalConnections)1 MinHosts (org.eclipse.winery.repository.targetallocation.criteria.minhosts.MinHosts)1 PermutationHelper (org.eclipse.winery.repository.targetallocation.util.PermutationHelper)1