use of org.onosproject.net.resource.ContinuousResource 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.ContinuousResource in project onos by opennetworkinglab.
the class TransactionalContinuousResourceSubStore method allocate.
@Override
public boolean allocate(ResourceConsumerId consumerId, ContinuousResource request) {
// if the resource is not registered, then abort
Optional<ContinuousResource> lookedUp = lookup(request.id());
if (!lookedUp.isPresent()) {
return false;
}
// Down cast: this must be safe as ContinuousResource is associated with ContinuousResourceId
ContinuousResource original = lookedUp.get();
ContinuousResourceAllocation allocations = consumers.get(request.id());
if (!Optional.ofNullable(allocations).orElse(ContinuousResourceAllocation.empty(original)).hasEnoughResource(request)) {
return false;
}
return appendValue(original, new ResourceAllocation(request, consumerId));
}
use of org.onosproject.net.resource.ContinuousResource in project onos by opennetworkinglab.
the class ContinuousResourceAllocationTest method testReleaseWhenDifferentConsumerIsSpecified.
@Test
public void testReleaseWhenDifferentConsumerIsSpecified() {
ContinuousResource original = Resources.continuous(DID, PN1, Bandwidth.class).resource(Bandwidth.gbps(1).bps());
ContinuousResource allocated = Resources.continuous(DID, PN1, Bandwidth.class).resource(Bandwidth.mbps(500).bps());
ResourceConsumer consumer = IntentId.valueOf(1);
ResourceConsumer otherConsumer = IntentId.valueOf(2);
ContinuousResourceAllocation sut = new ContinuousResourceAllocation(original, ImmutableList.of(new ResourceAllocation(allocated, consumer)));
ContinuousResource request = Resources.continuous(DID, PN1, Bandwidth.class).resource(Bandwidth.mbps(500).bps());
ImmutableList<ResourceAllocation> allocations = sut.release(request, otherConsumer.consumerId()).allocations();
assertThat(allocations.size(), is(1));
assertThat(allocations.get(0).resource().equals(allocated), is(true));
}
use of org.onosproject.net.resource.ContinuousResource in project onos by opennetworkinglab.
the class ContinuousResourceAllocationTest method testAllocateDifferentValue.
@Test
public void testAllocateDifferentValue() {
ContinuousResource original = Resources.continuous(DID, PN1, Bandwidth.class).resource(Bandwidth.gbps(1).bps());
ContinuousResource allocated = Resources.continuous(DID, PN1, Bandwidth.class).resource(Bandwidth.mbps(500).bps());
ResourceConsumer consumer = IntentId.valueOf(1);
ContinuousResourceAllocation sut = new ContinuousResourceAllocation(original, ImmutableList.of(new ResourceAllocation(allocated, consumer)));
ContinuousResource request = Resources.continuous(DID, PN1, Bandwidth.class).resource(Bandwidth.mbps(200).bps());
ContinuousResourceAllocation newValue = sut.allocate(new ResourceAllocation(request, consumer));
assertThat(newValue.allocations().size(), is(2));
assertThat(newValue.allocations(), hasItem(new ResourceAllocation(allocated, consumer)));
assertThat(newValue.allocations(), hasItem(new ResourceAllocation(request, consumer)));
}
use of org.onosproject.net.resource.ContinuousResource in project onos by opennetworkinglab.
the class ContinuousResourceAllocationTest method testHasEnoughResourceWhenLargeResourceIsRequested.
@Test
public void testHasEnoughResourceWhenLargeResourceIsRequested() {
ContinuousResource original = Resources.continuous(DID, PN1, Bandwidth.class).resource(Bandwidth.gbps(1).bps());
ContinuousResource allocated = Resources.continuous(DID, PN1, Bandwidth.class).resource(Bandwidth.mbps(500).bps());
ResourceConsumer consumer = IntentId.valueOf(1);
ContinuousResourceAllocation sut = new ContinuousResourceAllocation(original, ImmutableList.of(new ResourceAllocation(allocated, consumer)));
ContinuousResource request = Resources.continuous(DID, PN1, Bandwidth.class).resource(Bandwidth.mbps(600).bps());
assertThat(sut.hasEnoughResource(request), is(false));
}
Aggregations