use of org.onosproject.net.resource.ContinuousResource 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.ContinuousResource in project onos by opennetworkinglab.
the class HostToHostIntentCompilerTest method testBandwidthConstrainedIntentAllocation.
/**
* Tests if bandwidth resources get allocated correctly.
*/
@Test
public void testBandwidthConstrainedIntentAllocation() {
final double bpsTotal = 1000.0;
final double bpsToReserve = 100.0;
ContinuousResource resourceSw1P1 = Resources.continuous(DID_S1, PORT_1, Bandwidth.class).resource(bpsToReserve);
ContinuousResource resourceSw1P2 = Resources.continuous(DID_S1, PORT_2, Bandwidth.class).resource(bpsToReserve);
ContinuousResource resourceSw2P1 = Resources.continuous(DID_S2, PORT_1, Bandwidth.class).resource(bpsToReserve);
ContinuousResource resourceSw2P2 = Resources.continuous(DID_S2, PORT_2, Bandwidth.class).resource(bpsToReserve);
ContinuousResource resourceSw3P1 = Resources.continuous(DID_S3, PORT_1, Bandwidth.class).resource(bpsToReserve);
ContinuousResource resourceSw3P2 = Resources.continuous(DID_S3, PORT_2, Bandwidth.class).resource(bpsToReserve);
String[] hops = { HOST_ONE, S1, S2, S3, HOST_TWO };
final ResourceService resourceService = MockResourceService.makeCustomBandwidthResourceService(bpsTotal);
final List<Constraint> constraints = Collections.singletonList(new BandwidthConstraint(Bandwidth.bps(bpsToReserve)));
final HostToHostIntent intent = makeIntent(HOST_ONE, HOST_TWO, constraints);
HostToHostIntentCompiler compiler = makeCompiler(hops, resourceService);
compiler.compile(intent, null);
Key intentKey = intent.key();
ResourceAllocation rAOne = new ResourceAllocation(resourceSw1P1, intentKey);
ResourceAllocation rATwo = new ResourceAllocation(resourceSw1P2, intentKey);
ResourceAllocation rAThree = new ResourceAllocation(resourceSw2P1, intentKey);
ResourceAllocation rAFour = new ResourceAllocation(resourceSw2P2, intentKey);
ResourceAllocation rAFive = new ResourceAllocation(resourceSw3P1, intentKey);
ResourceAllocation rASix = new ResourceAllocation(resourceSw3P2, intentKey);
Set<ResourceAllocation> expectedresourceAllocations = ImmutableSet.of(rAOne, rATwo, rAThree, rAFour, rAFive, rASix);
Set<ResourceAllocation> resourceAllocations = ImmutableSet.copyOf(resourceService.getResourceAllocations(intentKey));
assertThat(resourceAllocations, hasSize(6));
assertEquals(expectedresourceAllocations, resourceAllocations);
}
use of org.onosproject.net.resource.ContinuousResource in project onos by opennetworkinglab.
the class MultiPointToSinglePointIntentCompilerTest method testBandwidthConstrainedIntentAllocation.
/**
* Tests if bandwidth resources get allocated correctly.
*/
@Test
public void testBandwidthConstrainedIntentAllocation() {
final double bpsTotal = 1000.0;
final double bpsToReserve = 100.0;
ContinuousResource resourceSw1P1 = Resources.continuous(DID_1, PORT_1, Bandwidth.class).resource(bpsToReserve);
ContinuousResource resourceSw1P2 = Resources.continuous(DID_1, PORT_2, Bandwidth.class).resource(bpsToReserve);
ContinuousResource resourceSw2P1 = Resources.continuous(DID_2, PORT_1, Bandwidth.class).resource(bpsToReserve);
ContinuousResource resourceSw2P2 = Resources.continuous(DID_2, PORT_2, Bandwidth.class).resource(bpsToReserve);
ContinuousResource resourceSw3P1 = Resources.continuous(DID_3, PORT_1, Bandwidth.class).resource(bpsToReserve);
ContinuousResource resourceSw3P2 = Resources.continuous(DID_3, PORT_2, Bandwidth.class).resource(bpsToReserve);
ContinuousResource resourceSw3P3 = Resources.continuous(DID_3, PORT_3, Bandwidth.class).resource(bpsToReserve);
ContinuousResource resourceSw4P1 = Resources.continuous(DID_4, PORT_1, Bandwidth.class).resource(bpsToReserve);
ContinuousResource resourceSw4P2 = Resources.continuous(DID_4, PORT_2, Bandwidth.class).resource(bpsToReserve);
String[] hops = { DID_3.toString() };
final ResourceService resourceService = MockResourceService.makeCustomBandwidthResourceService(bpsTotal);
final List<Constraint> constraints = Collections.singletonList(new BandwidthConstraint(Bandwidth.bps(bpsToReserve)));
Set<FilteredConnectPoint> ingress = ImmutableSet.of(new FilteredConnectPoint(new ConnectPoint(DID_1, PORT_1)), new FilteredConnectPoint(new ConnectPoint(DID_2, PORT_1)));
TrafficSelector ipPrefixSelector = DefaultTrafficSelector.builder().matchIPDst(IpPrefix.valueOf("192.168.100.0/24")).build();
FilteredConnectPoint egress = new FilteredConnectPoint(new ConnectPoint(DID_4, PORT_2));
MultiPointToSinglePointIntent intent = makeIntent(ingress, egress, ipPrefixSelector, constraints);
MultiPointToSinglePointIntentCompiler compiler = makeCompiler(null, new IntentTestsMocks.FixedMP2MPMockPathService(hops), resourceService);
compiler.compile(intent, null);
Key intentKey = intent.key();
ResourceAllocation rA1 = new ResourceAllocation(resourceSw1P1, intentKey);
ResourceAllocation rA2 = new ResourceAllocation(resourceSw1P2, intentKey);
ResourceAllocation rA3 = new ResourceAllocation(resourceSw2P1, intentKey);
ResourceAllocation rA4 = new ResourceAllocation(resourceSw2P2, intentKey);
ResourceAllocation rA5 = new ResourceAllocation(resourceSw3P1, intentKey);
ResourceAllocation rA6 = new ResourceAllocation(resourceSw3P2, intentKey);
ResourceAllocation rA7 = new ResourceAllocation(resourceSw3P3, intentKey);
ResourceAllocation rA8 = new ResourceAllocation(resourceSw4P1, intentKey);
ResourceAllocation rA9 = new ResourceAllocation(resourceSw4P2, intentKey);
Set<ResourceAllocation> expectedResourceAllocations = ImmutableSet.of(rA1, rA2, rA3, rA4, rA5, rA6, rA7, rA8, rA9);
Set<ResourceAllocation> resourceAllocations = ImmutableSet.copyOf(resourceService.getResourceAllocations(intentKey));
assertThat(resourceAllocations, hasSize(9));
assertEquals(expectedResourceAllocations, resourceAllocations);
}
use of org.onosproject.net.resource.ContinuousResource in project onos by opennetworkinglab.
the class SinglePointToMultiPointIntentCompilerTest method testBandwidthConstrainedIntentAllocation.
/**
* Tests if bandwidth resources get allocated correctly.
*/
@Test
public void testBandwidthConstrainedIntentAllocation() {
final double bpsTotal = 1000.0;
final double bpsToReserve = 100.0;
ContinuousResource resourceSw1P1 = Resources.continuous(DID_1, PORT_1, Bandwidth.class).resource(bpsToReserve);
ContinuousResource resourceSw1P2 = Resources.continuous(DID_1, PORT_2, Bandwidth.class).resource(bpsToReserve);
ContinuousResource resourceSw2P1 = Resources.continuous(DID_2, PORT_1, Bandwidth.class).resource(bpsToReserve);
ContinuousResource resourceSw2P2 = Resources.continuous(DID_2, PORT_2, Bandwidth.class).resource(bpsToReserve);
ContinuousResource resourceSw3P1 = Resources.continuous(DID_3, PORT_1, Bandwidth.class).resource(bpsToReserve);
ContinuousResource resourceSw3P2 = Resources.continuous(DID_3, PORT_2, Bandwidth.class).resource(bpsToReserve);
ContinuousResource resourceSw3P3 = Resources.continuous(DID_3, PORT_3, Bandwidth.class).resource(bpsToReserve);
ContinuousResource resourceSw4P1 = Resources.continuous(DID_4, PORT_1, Bandwidth.class).resource(bpsToReserve);
ContinuousResource resourceSw4P2 = Resources.continuous(DID_4, PORT_2, Bandwidth.class).resource(bpsToReserve);
String[] hops = { DID_3.toString() };
final ResourceService resourceService = MockResourceService.makeCustomBandwidthResourceService(bpsTotal);
final List<Constraint> constraints = Collections.singletonList(new BandwidthConstraint(Bandwidth.bps(bpsToReserve)));
FilteredConnectPoint ingress = new FilteredConnectPoint(new ConnectPoint(DID_4, PORT_1));
Set<FilteredConnectPoint> egress = ImmutableSet.of(new FilteredConnectPoint(new ConnectPoint(DID_1, PORT_2)), new FilteredConnectPoint(new ConnectPoint(DID_2, PORT_2)));
TrafficSelector ipPrefixSelector = DefaultTrafficSelector.builder().matchIPDst(IpPrefix.valueOf("192.168.100.0/24")).build();
SinglePointToMultiPointIntent intent = makeIntent(ingress, egress, ipPrefixSelector, constraints);
SinglePointToMultiPointIntentCompiler compiler = makeCompiler(null, new IntentTestsMocks.FixedMP2MPMockPathService(hops), resourceService);
compiler.compile(intent, null);
Key intentKey = intent.key();
ResourceAllocation rA1 = new ResourceAllocation(resourceSw1P1, intentKey);
ResourceAllocation rA2 = new ResourceAllocation(resourceSw1P2, intentKey);
ResourceAllocation rA3 = new ResourceAllocation(resourceSw2P1, intentKey);
ResourceAllocation rA4 = new ResourceAllocation(resourceSw2P2, intentKey);
ResourceAllocation rA5 = new ResourceAllocation(resourceSw3P1, intentKey);
ResourceAllocation rA6 = new ResourceAllocation(resourceSw3P2, intentKey);
ResourceAllocation rA7 = new ResourceAllocation(resourceSw3P3, intentKey);
ResourceAllocation rA8 = new ResourceAllocation(resourceSw4P1, intentKey);
ResourceAllocation rA9 = new ResourceAllocation(resourceSw4P2, intentKey);
Set<ResourceAllocation> expectedResourceAllocations = ImmutableSet.of(rA1, rA2, rA3, rA4, rA5, rA6, rA7, rA8, rA9);
Set<ResourceAllocation> resourceAllocations = ImmutableSet.copyOf(resourceService.getResourceAllocations(intentKey));
assertThat(resourceAllocations, hasSize(9));
assertEquals(expectedResourceAllocations, resourceAllocations);
}
use of org.onosproject.net.resource.ContinuousResource 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;
}
}
}
Aggregations