Search in sources :

Example 1 with DiscreteResourceId

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

the class AllocationsCommand method printAllocation.

private void printAllocation(DeviceId did, PortNumber num, int level) {
    if (level == 0) {
        // print DeviceId when Port was directly specified.
        print("%s", did);
    }
    DiscreteResourceId resourceId = Resources.discrete(did, num).id();
    List<String> portConsumers = resourceService.getResourceAllocations(resourceId).stream().filter(this::isSubjectToPrint).map(ResourceAllocation::consumerId).map(AllocationsCommand::asVerboseString).collect(Collectors.toList());
    if (portConsumers.isEmpty()) {
        print("%s%s", Strings.repeat(" ", level), asVerboseString(num));
    } else {
        print("%s%s allocated by %s", Strings.repeat(" ", level), asVerboseString(num), portConsumers);
    }
    // FIXME: This workaround induces a lot of distributed store access.
    // ResourceService should have an API to get all allocations under a parent resource.
    Set<Class<?>> subResourceTypes = ImmutableSet.<Class<?>>builder().add(OchSignal.class).add(VlanId.class).add(MplsLabel.class).add(Bandwidth.class).add(TributarySlot.class).build();
    for (Class<?> t : subResourceTypes) {
        resourceService.getResourceAllocations(resourceId, t).stream().filter(a -> isSubjectToPrint(a)).forEach(a -> print("%s%s allocated by %s", Strings.repeat(" ", level + 1), a.resource().valueAs(Object.class).orElse(""), asVerboseString(a.consumerId())));
    }
}
Also used : Arrays(java.util.Arrays) DiscreteResourceId(org.onosproject.net.resource.DiscreteResourceId) PortNumber(org.onosproject.net.PortNumber) DeviceService(org.onosproject.net.device.DeviceService) ResourceService(org.onosproject.net.resource.ResourceService) Command(org.apache.karaf.shell.api.action.Command) HashSet(java.util.HashSet) Strings(com.google.common.base.Strings) Port(org.onosproject.net.Port) StreamSupport(java.util.stream.StreamSupport) DeviceId.deviceId(org.onosproject.net.DeviceId.deviceId) IntentId(org.onosproject.net.intent.IntentId) ImmutableSet(com.google.common.collect.ImmutableSet) TributarySlot(org.onosproject.net.TributarySlot) MplsLabel(org.onlab.packet.MplsLabel) Device(org.onosproject.net.Device) Bandwidth(org.onlab.util.Bandwidth) ResourceConsumerId(org.onosproject.net.resource.ResourceConsumerId) Resources(org.onosproject.net.resource.Resources) VlanId(org.onlab.packet.VlanId) Set(java.util.Set) Argument(org.apache.karaf.shell.api.action.Argument) Collectors(java.util.stream.Collectors) ResourceAllocation(org.onosproject.net.resource.ResourceAllocation) OchSignal(org.onosproject.net.OchSignal) AbstractShellCommand(org.onosproject.cli.AbstractShellCommand) List(java.util.List) Service(org.apache.karaf.shell.api.action.lifecycle.Service) Completion(org.apache.karaf.shell.api.action.Completion) Option(org.apache.karaf.shell.api.action.Option) DeviceId(org.onosproject.net.DeviceId) Collections(java.util.Collections) TributarySlot(org.onosproject.net.TributarySlot) OchSignal(org.onosproject.net.OchSignal) MplsLabel(org.onlab.packet.MplsLabel) DiscreteResourceId(org.onosproject.net.resource.DiscreteResourceId) ResourceAllocation(org.onosproject.net.resource.ResourceAllocation)

Example 2 with DiscreteResourceId

use of org.onosproject.net.resource.DiscreteResourceId 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)

Example 3 with DiscreteResourceId

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

the class LinkPropsTopovMessageHandler method getBandwidth.

/**
 * Gets the links connected to the highlighted device.
 * Creates a ContinuousResource object for each link
 * and gets the bandwidth of the link from the query
 * and sets the label of the link as the bandwidth value.
 */
private Highlights getBandwidth(Set<Link> links, DeviceId devId) {
    LpLinkMap linkMap = new LpLinkMap();
    Highlights highlights = new Highlights();
    if (links != null) {
        log.debug("Processing {} links", links.size());
        links.forEach(linkMap::add);
        PortNumber portnum = PortNumber.portNumber((int) links.iterator().next().src().port().toLong());
        for (LpLink dlink : linkMap.biLinks()) {
            DiscreteResourceId parent = Resources.discrete(devId, portnum).id();
            ContinuousResource continuousResource = (ContinuousResource) resourceQueryService.getAvailableResources(parent, Bandwidth.class).iterator().next();
            double availBandwidth = continuousResource.value();
            dlink.makeImportant().setLabel(Double.toString(availBandwidth) + " bytes/s");
            highlights.add(dlink.highlight(null));
        }
    } else {
        log.debug("No egress links found for device {}", devId);
    }
    return highlights;
}
Also used : Highlights(org.onosproject.ui.topo.Highlights) Bandwidth(org.onlab.util.Bandwidth) DiscreteResourceId(org.onosproject.net.resource.DiscreteResourceId) PortNumber(org.onosproject.net.PortNumber) ContinuousResource(org.onosproject.net.resource.ContinuousResource)

Example 4 with DiscreteResourceId

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

the class EncodableDiscreteResources method lookup.

@Override
public Optional<DiscreteResource> lookup(DiscreteResourceId id) {
    if (!id.parent().filter(parent.id()::equals).isPresent()) {
        return Optional.empty();
    }
    DiscreteResource resource = Resources.discrete(id).resource();
    Class<?> cls = getClass(resource);
    return Optional.ofNullable(map.get(cls)).filter(x -> x.contains(resource)).map(x -> resource);
}
Also used : DiscreteResourceId(org.onosproject.net.resource.DiscreteResourceId) ImmutableSet(com.google.common.collect.ImmutableSet) Resources(org.onosproject.net.resource.Resources) MoreObjects(com.google.common.base.MoreObjects) Set(java.util.Set) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) LinkedHashMap(java.util.LinkedHashMap) Objects(java.util.Objects) DiscreteResourceCodec(org.onosproject.net.resource.DiscreteResourceCodec) Stream(java.util.stream.Stream) Map(java.util.Map) Entry(java.util.Map.Entry) Optional(java.util.Optional) LinkedHashSet(java.util.LinkedHashSet) DiscreteResource(org.onosproject.net.resource.DiscreteResource) DiscreteResource(org.onosproject.net.resource.DiscreteResource)

Example 5 with DiscreteResourceId

use of org.onosproject.net.resource.DiscreteResourceId 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)

Aggregations

DiscreteResourceId (org.onosproject.net.resource.DiscreteResourceId)5 ImmutableSet (com.google.common.collect.ImmutableSet)4 Set (java.util.Set)4 Collectors (java.util.stream.Collectors)4 Resources (org.onosproject.net.resource.Resources)4 LinkedHashMap (java.util.LinkedHashMap)3 LinkedHashSet (java.util.LinkedHashSet)3 List (java.util.List)3 Map (java.util.Map)3 Optional (java.util.Optional)3 Stream (java.util.stream.Stream)3 ResourceAllocation (org.onosproject.net.resource.ResourceAllocation)3 ResourceConsumerId (org.onosproject.net.resource.ResourceConsumerId)3 Beta (com.google.common.annotations.Beta)2 Preconditions.checkArgument (com.google.common.base.Preconditions.checkArgument)2 Preconditions.checkNotNull (com.google.common.base.Preconditions.checkNotNull)2 Collection (java.util.Collection)2 ExecutionException (java.util.concurrent.ExecutionException)2 TimeUnit (java.util.concurrent.TimeUnit)2 TimeoutException (java.util.concurrent.TimeoutException)2