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));
}
}
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));
}
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;
}
}
}
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;
}
}
}
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)));
}
Aggregations