use of org.onosproject.net.resource.DiscreteResource in project onos by opennetworkinglab.
the class GenericDiscreteResourcesTest method testIfDifferenceIsNotEmpty.
@Test
public void testIfDifferenceIsNotEmpty() {
DiscreteResource res1 = Resources.discrete(DeviceId.deviceId("a")).resource();
DiscreteResource res2 = Resources.discrete(DeviceId.deviceId("b")).resource();
DiscreteResources sut = GenericDiscreteResources.of(ImmutableSet.of(res1, res2));
DiscreteResources other = GenericDiscreteResources.of(ImmutableSet.of(res1));
DiscreteResources expected = GenericDiscreteResources.of(ImmutableSet.of(res2));
assertThat(sut.difference(other), is(expected));
}
use of org.onosproject.net.resource.DiscreteResource in project onos by opennetworkinglab.
the class ResourceDeviceListener method unregisterDeviceResource.
private void unregisterDeviceResource(Device device) {
DiscreteResource devResource = Resources.discrete(device.id()).resource();
List<Resource> allResources = getDescendantResources(devResource);
adminService.unregister(Lists.transform(allResources, Resource::id));
}
use of org.onosproject.net.resource.DiscreteResource 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);
}
use of org.onosproject.net.resource.DiscreteResource 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;
}
}
}
use of org.onosproject.net.resource.DiscreteResource in project onos by opennetworkinglab.
the class TransactionalDiscreteResourceSubStore method allocate.
@Override
public boolean allocate(ResourceConsumerId consumerId, DiscreteResource resource) {
// if the resource is not registered, then abort
Optional<DiscreteResource> lookedUp = lookup(resource.id());
if (!lookedUp.isPresent()) {
return false;
}
ResourceConsumerId oldValue = consumers.put(resource.id(), consumerId);
return oldValue == null;
}
Aggregations