Search in sources :

Example 1 with ResourceId

use of org.onosproject.net.resource.ResourceId in project onos by opennetworkinglab.

the class ConnectivityIntentCompiler method allocateBandwidth.

/**
 * Allocates the bandwidth specified as intent constraint on each link
 * composing the intent, if a bandwidth constraint is specified.
 *
 * @param intent the intent requesting bandwidth allocation
 * @param connectPoints the connect points composing the intent path computed
 */
protected void allocateBandwidth(ConnectivityIntent intent, List<ConnectPoint> connectPoints) {
    // Retrieve bandwidth constraint if exists
    List<Constraint> constraints = intent.constraints();
    if (constraints == null) {
        return;
    }
    Optional<Constraint> constraint = constraints.stream().filter(c -> c instanceof BandwidthConstraint).findAny();
    // If there is no bandwidth constraint continue
    if (!constraint.isPresent()) {
        return;
    }
    BandwidthConstraint bwConstraint = (BandwidthConstraint) constraint.get();
    double bw = bwConstraint.bandwidth().bps();
    // If a resource group is set on the intent, the resource consumer is
    // set equal to it. Otherwise it's set to the intent key
    ResourceConsumer newResourceConsumer = intent.resourceGroup() != null ? intent.resourceGroup() : intent.key();
    // Get the list of current resource allocations
    Collection<ResourceAllocation> resourceAllocations = resourceService.getResourceAllocations(newResourceConsumer);
    // Get the list of resources already allocated from resource allocations
    List<Resource> resourcesAllocated = resourcesFromAllocations(resourceAllocations);
    // Get the list of resource ids for resources already allocated
    List<ResourceId> idsResourcesAllocated = resourceIds(resourcesAllocated);
    // Create the list of incoming resources requested. Exclude resources
    // already allocated.
    List<Resource> incomingResources = resources(connectPoints, bw).stream().filter(r -> !resourcesAllocated.contains(r)).collect(Collectors.toList());
    if (incomingResources.isEmpty()) {
        return;
    }
    // Create the list of resources to be added, meaning their key is not
    // present in the resources already allocated
    List<Resource> resourcesToAdd = incomingResources.stream().filter(r -> !idsResourcesAllocated.contains(r.id())).collect(Collectors.toList());
    // Resources to updated are all the new valid resources except the
    // resources to be added
    List<Resource> resourcesToUpdate = Lists.newArrayList(incomingResources);
    resourcesToUpdate.removeAll(resourcesToAdd);
    // If there are no resources to update skip update procedures
    if (!resourcesToUpdate.isEmpty()) {
        // Remove old resources that need to be updated
        // TODO: use transaction updates when available in the resource service
        List<ResourceAllocation> resourceAllocationsToUpdate = resourceAllocations.stream().filter(rA -> resourceIds(resourcesToUpdate).contains(rA.resource().id())).collect(Collectors.toList());
        log.debug("Releasing bandwidth for intent {}: {} bps", newResourceConsumer, resourcesToUpdate);
        resourceService.release(resourceAllocationsToUpdate);
        // Update resourcesToAdd with the list of both the new resources and
        // the resources to update
        resourcesToAdd.addAll(resourcesToUpdate);
    }
    // remove them
    if (intent.resourceGroup() != null) {
        // Get the list of current resource allocations made by intent key
        Collection<ResourceAllocation> resourceAllocationsByKey = resourceService.getResourceAllocations(intent.key());
        resourceService.release(Lists.newArrayList(resourceAllocationsByKey));
    }
    // Allocate resources
    log.debug("Allocating bandwidth for intent {}: {} bps", newResourceConsumer, resourcesToAdd);
    List<ResourceAllocation> allocations = resourceService.allocate(newResourceConsumer, resourcesToAdd);
    if (allocations.isEmpty()) {
        log.debug("No resources allocated for intent {}", newResourceConsumer);
    }
    log.debug("Done allocating bandwidth for intent {}", newResourceConsumer);
}
Also used : HashedPathSelectionConstraint(org.onosproject.net.intent.constraint.HashedPathSelectionConstraint) TopologyEdge(org.onosproject.net.topology.TopologyEdge) DeviceService(org.onosproject.net.device.DeviceService) LoggerFactory(org.slf4j.LoggerFactory) ElementId(org.onosproject.net.ElementId) ResourceService(org.onosproject.net.resource.ResourceService) ConnectPoint(org.onosproject.net.ConnectPoint) Lists(com.google.common.collect.Lists) ImmutableList(com.google.common.collect.ImmutableList) FluentIterable(com.google.common.collect.FluentIterable) PathNotFoundException(org.onosproject.net.intent.impl.PathNotFoundException) PathViabilityConstraint(org.onosproject.net.intent.constraint.PathViabilityConstraint) Logger(org.slf4j.Logger) Iterator(java.util.Iterator) Bandwidth(org.onlab.util.Bandwidth) IntentExtensionService(org.onosproject.net.intent.IntentExtensionService) Resources(org.onosproject.net.resource.Resources) PathService(org.onosproject.net.topology.PathService) Collection(java.util.Collection) Set(java.util.Set) ProviderId(org.onosproject.net.provider.ProviderId) Resource(org.onosproject.net.resource.Resource) Collectors(java.util.stream.Collectors) Constraint(org.onosproject.net.intent.Constraint) ResourceAllocation(org.onosproject.net.resource.ResourceAllocation) ReferenceCardinality(org.osgi.service.component.annotations.ReferenceCardinality) DefaultEdgeWeigher(org.onlab.graph.DefaultEdgeWeigher) BandwidthConstraint(org.onosproject.net.intent.constraint.BandwidthConstraint) List(java.util.List) Weight(org.onlab.graph.Weight) ResourceId(org.onosproject.net.resource.ResourceId) TopologyVertex(org.onosproject.net.topology.TopologyVertex) ConnectivityIntent(org.onosproject.net.intent.ConnectivityIntent) IntentCompiler(org.onosproject.net.intent.IntentCompiler) Optional(java.util.Optional) Path(org.onosproject.net.Path) ResourceConsumer(org.onosproject.net.resource.ResourceConsumer) LinkWeigher(org.onosproject.net.topology.LinkWeigher) Reference(org.osgi.service.component.annotations.Reference) ScalarWeight(org.onlab.graph.ScalarWeight) DeviceId(org.onosproject.net.DeviceId) Collections(java.util.Collections) DisjointPath(org.onosproject.net.DisjointPath) MarkerConstraint(org.onosproject.net.intent.constraint.MarkerConstraint) HashedPathSelectionConstraint(org.onosproject.net.intent.constraint.HashedPathSelectionConstraint) PathViabilityConstraint(org.onosproject.net.intent.constraint.PathViabilityConstraint) Constraint(org.onosproject.net.intent.Constraint) BandwidthConstraint(org.onosproject.net.intent.constraint.BandwidthConstraint) MarkerConstraint(org.onosproject.net.intent.constraint.MarkerConstraint) Resource(org.onosproject.net.resource.Resource) ResourceConsumer(org.onosproject.net.resource.ResourceConsumer) ResourceAllocation(org.onosproject.net.resource.ResourceAllocation) ResourceId(org.onosproject.net.resource.ResourceId) BandwidthConstraint(org.onosproject.net.intent.constraint.BandwidthConstraint)

Example 2 with ResourceId

use of org.onosproject.net.resource.ResourceId in project onos by opennetworkinglab.

the class ConsistentResourceStore method unregister.

@Override
public boolean unregister(List<? extends ResourceId> ids) {
    checkNotNull(ids);
    // Retry the transaction until successful.
    while (true) {
        TransactionContext tx = service.transactionContextBuilder().build();
        tx.begin();
        TransactionalDiscreteResourceSubStore discreteTxStore = discreteStore.transactional(tx);
        TransactionalContinuousResourceSubStore continuousTxStore = continuousStore.transactional(tx);
        // Look up resources by resource IDs
        List<Resource> resources = ids.stream().filter(x -> x.parent().isPresent()).map(x -> {
            // avoid access to consistent map in the case of discrete resource
            if (x instanceof DiscreteResourceId) {
                return Optional.of(Resources.discrete((DiscreteResourceId) x).resource());
            } else {
                return continuousTxStore.lookup((ContinuousResourceId) x);
            }
        }).flatMap(Tools::stream).collect(Collectors.toList());
        // the order is preserved by LinkedHashMap
        Map<DiscreteResourceId, List<Resource>> resourceMap = resources.stream().collect(Collectors.groupingBy(x -> x.parent().get().id(), LinkedHashMap::new, Collectors.toList()));
        for (Map.Entry<DiscreteResourceId, List<Resource>> entry : resourceMap.entrySet()) {
            if (!unregister(discreteTxStore, continuousTxStore, entry.getKey(), entry.getValue())) {
                log.warn("Failed to unregister {}: Failed to remove {} values.", entry.getKey(), entry.getValue().size());
                return abortTransaction(tx);
            }
        }
        try {
            CommitStatus status = commitTransaction(tx);
            if (status == CommitStatus.SUCCESS) {
                List<ResourceEvent> events = resources.stream().filter(x -> x.parent().isPresent()).map(x -> new ResourceEvent(RESOURCE_REMOVED, x)).collect(Collectors.toList());
                notifyDelegate(events);
                return true;
            }
        } catch (InterruptedException | ExecutionException | TimeoutException e) {
            String message = resources.stream().map(Resource::simpleTypeName).collect(Collectors.groupingBy(Function.identity(), Collectors.counting())).entrySet().stream().map(entry -> String.format("%d %s type resources", entry.getValue(), entry.getKey())).collect(Collectors.joining(", "));
            log.warn("Failed to unregister {}: {}", message, e);
            return false;
        }
    }
}
Also used : DiscreteResourceId(org.onosproject.net.resource.DiscreteResourceId) Tools(org.onlab.util.Tools) LoggerFactory(org.slf4j.LoggerFactory) Collectors.groupingBy(java.util.stream.Collectors.groupingBy) ResourceStoreDelegate(org.onosproject.net.resource.ResourceStoreDelegate) TimeoutException(java.util.concurrent.TimeoutException) KryoNamespace(org.onlab.util.KryoNamespace) DistributedPrimitive(org.onosproject.store.service.DistributedPrimitive) Function(java.util.function.Function) LinkedHashMap(java.util.LinkedHashMap) Component(org.osgi.service.component.annotations.Component) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) StorageService(org.onosproject.store.service.StorageService) Map(java.util.Map) ResourceStore(org.onosproject.net.resource.ResourceStore) Activate(org.osgi.service.component.annotations.Activate) KryoNamespaces(org.onosproject.store.serializers.KryoNamespaces) LinkedHashSet(java.util.LinkedHashSet) Serializer(org.onosproject.store.service.Serializer) ImmutableSet(com.google.common.collect.ImmutableSet) Logger(org.slf4j.Logger) RESOURCE_REMOVED(org.onosproject.net.resource.ResourceEvent.Type.RESOURCE_REMOVED) ResourceConsumerId(org.onosproject.net.resource.ResourceConsumerId) ResourceEvent(org.onosproject.net.resource.ResourceEvent) Resources(org.onosproject.net.resource.Resources) CommitStatus(org.onosproject.store.service.CommitStatus) Collection(java.util.Collection) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) Set(java.util.Set) Resource(org.onosproject.net.resource.Resource) TransactionContext(org.onosproject.store.service.TransactionContext) Collectors(java.util.stream.Collectors) ResourceAllocation(org.onosproject.net.resource.ResourceAllocation) Beta(com.google.common.annotations.Beta) ReferenceCardinality(org.osgi.service.component.annotations.ReferenceCardinality) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) RESOURCE_ADDED(org.onosproject.net.resource.ResourceEvent.Type.RESOURCE_ADDED) List(java.util.List) Stream(java.util.stream.Stream) ResourceId(org.onosproject.net.resource.ResourceId) AbstractStore(org.onosproject.store.AbstractStore) Optional(java.util.Optional) ResourceConsumer(org.onosproject.net.resource.ResourceConsumer) ContinuousResourceId(org.onosproject.net.resource.ContinuousResourceId) Reference(org.osgi.service.component.annotations.Reference) ContinuousResource(org.onosproject.net.resource.ContinuousResource) DiscreteResource(org.onosproject.net.resource.DiscreteResource) Resource(org.onosproject.net.resource.Resource) ContinuousResource(org.onosproject.net.resource.ContinuousResource) DiscreteResource(org.onosproject.net.resource.DiscreteResource) TransactionContext(org.onosproject.store.service.TransactionContext) CommitStatus(org.onosproject.store.service.CommitStatus) ResourceEvent(org.onosproject.net.resource.ResourceEvent) DiscreteResourceId(org.onosproject.net.resource.DiscreteResourceId) List(java.util.List) ExecutionException(java.util.concurrent.ExecutionException) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) TimeoutException(java.util.concurrent.TimeoutException)

Aggregations

Collection (java.util.Collection)2 List (java.util.List)2 Optional (java.util.Optional)2 Set (java.util.Set)2 Collectors (java.util.stream.Collectors)2 Resource (org.onosproject.net.resource.Resource)2 ResourceAllocation (org.onosproject.net.resource.ResourceAllocation)2 ResourceConsumer (org.onosproject.net.resource.ResourceConsumer)2 ResourceId (org.onosproject.net.resource.ResourceId)2 Resources (org.onosproject.net.resource.Resources)2 Reference (org.osgi.service.component.annotations.Reference)2 ReferenceCardinality (org.osgi.service.component.annotations.ReferenceCardinality)2 Logger (org.slf4j.Logger)2 LoggerFactory (org.slf4j.LoggerFactory)2 Beta (com.google.common.annotations.Beta)1 Preconditions.checkArgument (com.google.common.base.Preconditions.checkArgument)1 Preconditions.checkNotNull (com.google.common.base.Preconditions.checkNotNull)1 FluentIterable (com.google.common.collect.FluentIterable)1 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableSet (com.google.common.collect.ImmutableSet)1