use of org.onosproject.net.resource.ResourceEvent.Type.RESOURCE_ADDED 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;
}
}
}
Aggregations