Search in sources :

Example 1 with DiscreteResource

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

the class ResourcesCommand method printResource.

private void printResource(Resource resource, int level) {
    // workaround to preserve the original behavior of ResourceService#getRegisteredResources
    Set<Resource> children;
    if (resource instanceof DiscreteResource) {
        children = resourceService.getRegisteredResources(((DiscreteResource) resource).id());
    } else {
        children = Collections.emptySet();
    }
    if (resource.equals(Resource.ROOT)) {
        print("ROOT");
    } else {
        String resourceName = resource.simpleTypeName();
        if (resource instanceof ContinuousResource) {
            if (availablesOnly) {
                // Get the total resource
                double total = ((ContinuousResource) resource).value();
                // Get allocated resource
                double allocated = resourceService.getResourceAllocations(resource.id()).stream().mapToDouble(rA -> ((ContinuousResource) rA.resource()).value()).sum();
                // Difference
                double difference = total - allocated;
                print("%s%s: %f", Strings.repeat(" ", level), resourceName, difference);
            } else {
                print("%s%s: %f", Strings.repeat(" ", level), resourceName, ((ContinuousResource) resource).value());
            }
            // Continuous resource is terminal node, stop here
            return;
        } else {
            String availability = "";
            if (availablesOnly && !children.isEmpty()) {
                // intermediate nodes cannot be omitted, print availability
                if (resourceService.isAvailable(resource)) {
                    availability = " ✔";
                } else {
                    availability = " ✘";
                }
            }
            String toString = String.valueOf(resource.valueAs(Object.class).orElse(""));
            if (toString.startsWith(resourceName)) {
                print("%s%s%s", Strings.repeat(" ", level), toString, availability);
            } else {
                print("%s%s: %s%s", Strings.repeat(" ", level), resourceName, toString, availability);
            }
        }
    }
    // Classify children into aggregatable terminal resources and everything else
    Set<Class<?>> aggregatableTypes = ImmutableSet.<Class<?>>builder().add(VlanId.class).add(MplsLabel.class).build();
    // (last() resource name) -> { Resource }
    Multimap<String, Resource> aggregatables = ArrayListMultimap.create();
    List<Resource> nonAggregatable = new ArrayList<>();
    for (Resource r : children) {
        if (!isPrintTarget(r)) {
            // A
            continue;
        }
        if (r instanceof DiscreteResource) {
            if (resourceService.getRegisteredResources(((DiscreteResource) r).id()).isEmpty()) {
                // resource which has children should be printed
                continue;
            }
        }
        if (r instanceof ContinuousResource) {
            // non-aggregatable terminal node
            nonAggregatable.add(r);
        } else if (Iterables.any(aggregatableTypes, r::isTypeOf)) {
            // aggregatable & terminal node
            String simpleName = r.simpleTypeName();
            aggregatables.put(simpleName, r);
        } else {
            nonAggregatable.add(r);
        }
    }
    // print aggregated (terminal)
    aggregatables.asMap().entrySet().forEach(e -> {
        // for each type...
        String resourceName = e.getKey();
        RangeSet<Long> rangeSet = TreeRangeSet.create();
        // aggregate into RangeSet
        e.getValue().stream().map(res -> {
            if (res.isTypeOf(VlanId.class)) {
                return (long) res.valueAs(VlanId.class).get().toShort();
            } else if (res.isTypeOf(MplsLabel.class)) {
                return (long) res.valueAs(MplsLabel.class).get().toInt();
            } else if (res.isTypeOf(TributarySlot.class)) {
                return res.valueAs(TributarySlot.class).get().index();
            }
            // TODO support Lambda (OchSignal types)
            return 0L;
        }).map(Range::singleton).map(range -> range.canonical(DiscreteDomain.longs())).forEach(rangeSet::add);
        print("%s%s: %s", Strings.repeat(" ", level + 1), resourceName, rangeSet);
    });
    // print non-aggregatables (recurse)
    if (sort) {
        nonAggregatable.stream().sorted((o1, o2) -> String.valueOf(o1.id()).compareTo(String.valueOf(o2.id()))).forEach(r -> printResource(r, level + 1));
    } else {
        nonAggregatable.forEach(r -> printResource(r, level + 1));
    }
}
Also used : ArrayListMultimap(com.google.common.collect.ArrayListMultimap) Arrays(java.util.Arrays) Iterables(com.google.common.collect.Iterables) RangeSet(com.google.common.collect.RangeSet) PortNumber(org.onosproject.net.PortNumber) Multimap(com.google.common.collect.Multimap) TreeRangeSet(com.google.common.collect.TreeRangeSet) ArrayList(java.util.ArrayList) Command(org.apache.karaf.shell.api.action.Command) HashSet(java.util.HashSet) Strings(com.google.common.base.Strings) DiscreteDomain(com.google.common.collect.DiscreteDomain) DeviceId.deviceId(org.onosproject.net.DeviceId.deviceId) ImmutableSet(com.google.common.collect.ImmutableSet) TributarySlot(org.onosproject.net.TributarySlot) MplsLabel(org.onlab.packet.MplsLabel) Resources(org.onosproject.net.resource.Resources) VlanId(org.onlab.packet.VlanId) Range(com.google.common.collect.Range) Set(java.util.Set) Argument(org.apache.karaf.shell.api.action.Argument) Resource(org.onosproject.net.resource.Resource) 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) ResourceQueryService(org.onosproject.net.resource.ResourceQueryService) Option(org.apache.karaf.shell.api.action.Option) DeviceId(org.onosproject.net.DeviceId) Collections(java.util.Collections) 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) ArrayList(java.util.ArrayList) DiscreteResource(org.onosproject.net.resource.DiscreteResource) Range(com.google.common.collect.Range) TributarySlot(org.onosproject.net.TributarySlot) MplsLabel(org.onlab.packet.MplsLabel) VlanId(org.onlab.packet.VlanId) ContinuousResource(org.onosproject.net.resource.ContinuousResource)

Example 2 with DiscreteResource

use of org.onosproject.net.resource.DiscreteResource 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 3 with DiscreteResource

use of org.onosproject.net.resource.DiscreteResource 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 4 with DiscreteResource

use of org.onosproject.net.resource.DiscreteResource 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 5 with DiscreteResource

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

the class EncodableDiscreteResourcesSerializer method read.

@Override
public EncodableDiscreteResources read(Kryo kryo, Input input, Class<EncodableDiscreteResources> cls) {
    DiscreteResource parent = kryo.readObject(input, DiscreteResource.class);
    @SuppressWarnings("unchecked") Set<EncodedDiscreteResources> resources = kryo.readObject(input, LinkedHashSet.class);
    return new EncodableDiscreteResources(parent, resources.stream().collect(Collectors.toMap(EncodedDiscreteResources::encodedClass, Function.identity(), (v1, v2) -> v1, LinkedHashMap::new)));
}
Also used : DiscreteResource(org.onosproject.net.resource.DiscreteResource) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

DiscreteResource (org.onosproject.net.resource.DiscreteResource)47 Test (org.junit.Test)38 Resource (org.onosproject.net.resource.Resource)6 ImmutableSet (com.google.common.collect.ImmutableSet)4 Set (java.util.Set)4 ContinuousResource (org.onosproject.net.resource.ContinuousResource)4 Resources (org.onosproject.net.resource.Resources)4 LinkedHashMap (java.util.LinkedHashMap)3 List (java.util.List)3 Optional (java.util.Optional)3 ExecutionException (java.util.concurrent.ExecutionException)3 TimeoutException (java.util.concurrent.TimeoutException)3 Collectors (java.util.stream.Collectors)3 Stream (java.util.stream.Stream)3 PortNumber (org.onosproject.net.PortNumber)3 ResourceConsumerId (org.onosproject.net.resource.ResourceConsumerId)3 LinkedHashSet (java.util.LinkedHashSet)2 Map (java.util.Map)2 VlanId (org.onlab.packet.VlanId)2 DeviceId (org.onosproject.net.DeviceId)2