Search in sources :

Example 6 with Resource

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

the class ConsistentResourceStore method allocate.

@Override
public boolean allocate(List<? extends Resource> resources, ResourceConsumer consumer) {
    checkNotNull(resources);
    checkNotNull(consumer);
    while (true) {
        TransactionContext tx = service.transactionContextBuilder().build();
        tx.begin();
        TransactionalDiscreteResourceSubStore discreteTxStore = discreteStore.transactional(tx);
        TransactionalContinuousResourceSubStore continuousTxStore = continuousStore.transactional(tx);
        for (Resource resource : resources) {
            if (resource instanceof DiscreteResource) {
                if (!discreteTxStore.allocate(consumer.consumerId(), (DiscreteResource) resource)) {
                    return abortTransaction(tx);
                }
            } else if (resource instanceof ContinuousResource) {
                if (!continuousTxStore.allocate(consumer.consumerId(), (ContinuousResource) resource)) {
                    return abortTransaction(tx);
                }
            }
        }
        try {
            if (commitTransaction(tx) == CommitStatus.SUCCESS) {
                return true;
            }
        } catch (InterruptedException | ExecutionException | TimeoutException e) {
            log.warn("Failed to allocate {}: {}", resources, e);
            return false;
        }
    }
}
Also used : TransactionContext(org.onosproject.store.service.TransactionContext) Resource(org.onosproject.net.resource.Resource) ContinuousResource(org.onosproject.net.resource.ContinuousResource) DiscreteResource(org.onosproject.net.resource.DiscreteResource) DiscreteResource(org.onosproject.net.resource.DiscreteResource) ExecutionException(java.util.concurrent.ExecutionException) ContinuousResource(org.onosproject.net.resource.ContinuousResource) TimeoutException(java.util.concurrent.TimeoutException)

Example 7 with Resource

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

the class ConsistentResourceStore method release.

@Override
public boolean release(List<ResourceAllocation> allocations) {
    checkNotNull(allocations);
    while (true) {
        TransactionContext tx = service.transactionContextBuilder().build();
        tx.begin();
        TransactionalDiscreteResourceSubStore discreteTxStore = discreteStore.transactional(tx);
        TransactionalContinuousResourceSubStore continuousTxStore = continuousStore.transactional(tx);
        for (ResourceAllocation allocation : allocations) {
            Resource resource = allocation.resource();
            ResourceConsumerId consumerId = allocation.consumerId();
            if (resource instanceof DiscreteResource) {
                if (!discreteTxStore.release(consumerId, (DiscreteResource) resource)) {
                    return abortTransaction(tx);
                }
            } else if (resource instanceof ContinuousResource) {
                if (!continuousTxStore.release(consumerId, (ContinuousResource) resource)) {
                    return abortTransaction(tx);
                }
            }
        }
        try {
            if (commitTransaction(tx) == CommitStatus.SUCCESS) {
                return true;
            }
        } catch (InterruptedException | ExecutionException | TimeoutException e) {
            log.warn("Failed to release {}: {}", allocations, e);
            return false;
        }
    }
}
Also used : TransactionContext(org.onosproject.store.service.TransactionContext) Resource(org.onosproject.net.resource.Resource) ContinuousResource(org.onosproject.net.resource.ContinuousResource) DiscreteResource(org.onosproject.net.resource.DiscreteResource) ResourceConsumerId(org.onosproject.net.resource.ResourceConsumerId) DiscreteResource(org.onosproject.net.resource.DiscreteResource) ExecutionException(java.util.concurrent.ExecutionException) ResourceAllocation(org.onosproject.net.resource.ResourceAllocation) ContinuousResource(org.onosproject.net.resource.ContinuousResource) TimeoutException(java.util.concurrent.TimeoutException)

Example 8 with Resource

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

the class ResourceDeviceListener method unregisterPortResource.

private void unregisterPortResource(Device device, Port port) {
    DiscreteResource portResource = Resources.discrete(device.id(), port.number()).resource();
    List<Resource> allResources = getDescendantResources(portResource);
    adminService.unregister(Lists.transform(allResources, Resource::id));
}
Also used : Resource(org.onosproject.net.resource.Resource) DiscreteResource(org.onosproject.net.resource.DiscreteResource) DiscreteResource(org.onosproject.net.resource.DiscreteResource)

Example 9 with Resource

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

the class ConsistentResourceStore method register.

@Override
public boolean register(List<? extends Resource> resources) {
    checkNotNull(resources);
    if (log.isTraceEnabled()) {
        resources.forEach(r -> log.trace("registering {}", r));
    }
    // Retry the transaction until successful.
    while (true) {
        TransactionContext tx = service.transactionContextBuilder().build();
        tx.begin();
        // the order is preserved by LinkedHashMap
        Map<DiscreteResource, List<Resource>> resourceMap = resources.stream().filter(x -> x.parent().isPresent()).collect(groupingBy(x -> x.parent().get(), LinkedHashMap::new, Collectors.<Resource>toList()));
        TransactionalDiscreteResourceSubStore discreteTxStore = discreteStore.transactional(tx);
        TransactionalContinuousResourceSubStore continuousTxStore = continuousStore.transactional(tx);
        for (Map.Entry<DiscreteResource, List<Resource>> entry : resourceMap.entrySet()) {
            DiscreteResourceId parentId = entry.getKey().id();
            if (!discreteTxStore.lookup(parentId).isPresent()) {
                return abortTransaction(tx);
            }
            if (!register(discreteTxStore, continuousTxStore, parentId, entry.getValue())) {
                return abortTransaction(tx);
            }
        }
        try {
            CommitStatus status = commitTransaction(tx);
            if (status == CommitStatus.SUCCESS) {
                log.trace("Transaction commit succeeded on registration: resources={}", resources);
                List<ResourceEvent> events = resources.stream().filter(x -> x.parent().isPresent()).map(x -> new ResourceEvent(RESOURCE_ADDED, x)).collect(Collectors.toList());
                notifyDelegate(events);
                return true;
            }
        } catch (InterruptedException | ExecutionException | TimeoutException e) {
            log.warn("Transaction commit failed on registration", 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) DiscreteResource(org.onosproject.net.resource.DiscreteResource) TransactionContext(org.onosproject.store.service.TransactionContext) CommitStatus(org.onosproject.store.service.CommitStatus) ResourceEvent(org.onosproject.net.resource.ResourceEvent) List(java.util.List) DiscreteResourceId(org.onosproject.net.resource.DiscreteResourceId) ExecutionException(java.util.concurrent.ExecutionException) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) TimeoutException(java.util.concurrent.TimeoutException)

Example 10 with Resource

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

the class ResourceDeviceListener method registerPortResource.

private void registerPortResource(Device device, Port port) {
    Resource portPath = Resources.discrete(device.id(), port.number()).resource();
    if (!adminService.register(portPath)) {
        log.error("Failed to register Port: {}", portPath.id());
    }
    queryBandwidth(device.id(), port.number()).map(bw -> portPath.child(Bandwidth.class, bw.bps())).map(adminService::register).ifPresent(success -> {
        if (!success) {
            log.error("Failed to register Bandwidth for {}", portPath.id());
        }
    });
    // for VLAN IDs
    Set<VlanId> vlans = queryVlanIds(device.id(), port.number());
    if (!vlans.isEmpty()) {
        boolean success = adminService.register(vlans.stream().map(portPath::child).collect(Collectors.toList()));
        if (!success) {
            log.error("Failed to register VLAN IDs for {}", portPath.id());
        }
    }
    // for MPLS labels
    Set<MplsLabel> mplsLabels = queryMplsLabels(device.id(), port.number());
    if (!mplsLabels.isEmpty()) {
        boolean success = adminService.register(mplsLabels.stream().map(portPath::child).collect(Collectors.toList()));
        if (!success) {
            log.error("Failed to register MPLS Labels for {}", portPath.id());
        }
    }
    // for Lambdas
    Set<OchSignal> lambdas = queryLambdas(device.id(), port.number());
    if (!lambdas.isEmpty()) {
        boolean success = adminService.register(lambdas.stream().map(portPath::child).collect(Collectors.toList()));
        if (!success) {
            log.error("Failed to register lambdas for {}", portPath.id());
        }
    }
    // for Tributary slots
    Set<TributarySlot> tSlots = queryTributarySlots(device.id(), port.number());
    if (!tSlots.isEmpty()) {
        boolean success = adminService.register(tSlots.stream().map(portPath::child).collect(Collectors.toList()));
        if (!success) {
            log.error("Failed to register tributary slots for {}", portPath.id());
        }
    }
}
Also used : Bandwidth(org.onlab.util.Bandwidth) Resource(org.onosproject.net.resource.Resource) DiscreteResource(org.onosproject.net.resource.DiscreteResource) TributarySlot(org.onosproject.net.TributarySlot) MplsLabel(org.onlab.packet.MplsLabel) OchSignal(org.onosproject.net.OchSignal) VlanId(org.onlab.packet.VlanId)

Aggregations

Resource (org.onosproject.net.resource.Resource)16 Set (java.util.Set)8 DiscreteResource (org.onosproject.net.resource.DiscreteResource)8 ResourceAllocation (org.onosproject.net.resource.ResourceAllocation)8 List (java.util.List)7 ConnectPoint (org.onosproject.net.ConnectPoint)7 Resources (org.onosproject.net.resource.Resources)7 Optional (java.util.Optional)6 Collectors (java.util.stream.Collectors)6 Reference (org.osgi.service.component.annotations.Reference)6 ReferenceCardinality (org.osgi.service.component.annotations.ReferenceCardinality)6 Logger (org.slf4j.Logger)6 LoggerFactory (org.slf4j.LoggerFactory)6 Preconditions.checkArgument (com.google.common.base.Preconditions.checkArgument)5 ImmutableSet (com.google.common.collect.ImmutableSet)5 Collection (java.util.Collection)5 Collections (java.util.Collections)5 DeviceId (org.onosproject.net.DeviceId)5 ImmutableList (com.google.common.collect.ImmutableList)4 ArrayList (java.util.ArrayList)4