Sho SHIMIZU
Committed by Gerrit Code Review

ONOS-2710: Change singnatures of register/unregister in ResourceStore

Change-Id: I2e90871d79cac94474a39c797168870173441eeb
......@@ -13,28 +13,26 @@ import java.util.Optional;
public interface ResourceStore {
/**
* Registers the resources as children of the parent resource in transactional way.
* Registers the resources in transactional way.
* Resource registration is must be done before resource allocation. The state after completion
* of this method is all the resources are registered, or no resource is registered.
* The whole registration fails when any one of the resource can't be registered.
*
* @param parent resource which is the parent of the resource to be registered
* @param children resources to be registered
* @param resources resources to be registered
* @return true if the registration succeeds, false otherwise
*/
boolean register(ResourcePath parent, List<ResourcePath> children);
boolean register(List<ResourcePath> resources);
/**
* Unregisters the resources as children of the parent resource in transactional way.
* Unregisters the resources in transactional way.
* The state after completion of this method is all the resources are unregistered,
* or no resource is unregistered. The whole unregistration fails when any one of the
* resource can't be unregistered.
*
* @param parent resource which is the parent of the resource to be unregistered
* @param children resources to be unregistered
* @param resources resources to be unregistered
* @return true if the registration succeeds, false otherwise
*/
boolean unregister(ResourcePath parent, List<ResourcePath> children);
boolean unregister(List<ResourcePath> resources);
/**
* Allocates the specified resources to the specified consumer in transactional way.
......
......@@ -133,7 +133,7 @@ public final class ResourceManager implements ResourceService, ResourceAdminServ
checkArgument(!children.isEmpty());
List<ResourcePath> resources = Lists.transform(children, x -> ResourcePath.child(parent, x));
return store.register(parent, resources);
return store.register(resources);
}
@Override
......@@ -143,6 +143,6 @@ public final class ResourceManager implements ResourceService, ResourceAdminServ
checkArgument(!children.isEmpty());
List<ResourcePath> resources = Lists.transform(children, x -> ResourcePath.child(parent, x));
return store.unregister(parent, resources);
return store.unregister(resources);
}
}
......
......@@ -94,9 +94,8 @@ public class ConsistentResourceStore implements ResourceStore {
}
@Override
public boolean register(ResourcePath resource, List<ResourcePath> children) {
checkNotNull(resource);
checkNotNull(children);
public boolean register(List<ResourcePath> resources) {
checkNotNull(resources);
TransactionContext tx = service.transactionContextBuilder().build();
tx.begin();
......@@ -105,12 +104,18 @@ public class ConsistentResourceStore implements ResourceStore {
TransactionalMap<ResourcePath, List<ResourcePath>> childTxMap =
tx.getTransactionalMap(CHILD_MAP, SERIALIZER);
if (!isRegistered(childTxMap, resource)) {
return abortTransaction(tx);
}
Map<ResourcePath, List<ResourcePath>> resourceMap = resources.stream()
.filter(x -> x.parent().isPresent())
.collect(Collectors.groupingBy(x -> x.parent().get()));
for (Map.Entry<ResourcePath, List<ResourcePath>> entry: resourceMap.entrySet()) {
if (!isRegistered(childTxMap, entry.getKey())) {
return abortTransaction(tx);
}
if (!appendValues(childTxMap, resource, children)) {
return abortTransaction(tx);
if (!appendValues(childTxMap, entry.getKey(), entry.getValue())) {
return abortTransaction(tx);
}
}
return commitTransaction(tx);
......@@ -121,9 +126,8 @@ public class ConsistentResourceStore implements ResourceStore {
}
@Override
public boolean unregister(ResourcePath resource, List<ResourcePath> children) {
checkNotNull(resource);
checkNotNull(children);
public boolean unregister(List<ResourcePath> resources) {
checkNotNull(resources);
TransactionContext tx = service.transactionContextBuilder().build();
tx.begin();
......@@ -134,14 +138,20 @@ public class ConsistentResourceStore implements ResourceStore {
TransactionalMap<ResourcePath, ResourceConsumer> consumerTxMap =
tx.getTransactionalMap(CONSUMER_MAP, SERIALIZER);
Map<ResourcePath, List<ResourcePath>> resourceMap = resources.stream()
.filter(x -> x.parent().isPresent())
.collect(Collectors.groupingBy(x -> x.parent().get()));
// even if one of the resources is allocated to a consumer,
// all unregistrations are regarded as failure
if (children.stream().anyMatch(x -> consumerTxMap.get(x) != null)) {
return abortTransaction(tx);
}
for (Map.Entry<ResourcePath, List<ResourcePath>> entry: resourceMap.entrySet()) {
if (entry.getValue().stream().anyMatch(x -> consumerTxMap.get(x) != null)) {
return abortTransaction(tx);
}
if (!removeValues(childTxMap, resource, children)) {
return abortTransaction(tx);
if (!removeValues(childTxMap, entry.getKey(), entry.getValue())) {
return abortTransaction(tx);
}
}
return commitTransaction(tx);
......