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; ...@@ -13,28 +13,26 @@ import java.util.Optional;
13 public interface ResourceStore { 13 public interface ResourceStore {
14 14
15 /** 15 /**
16 - * Registers the resources as children of the parent resource in transactional way. 16 + * Registers the resources in transactional way.
17 * Resource registration is must be done before resource allocation. The state after completion 17 * Resource registration is must be done before resource allocation. The state after completion
18 * of this method is all the resources are registered, or no resource is registered. 18 * of this method is all the resources are registered, or no resource is registered.
19 * The whole registration fails when any one of the resource can't be registered. 19 * The whole registration fails when any one of the resource can't be registered.
20 * 20 *
21 - * @param parent resource which is the parent of the resource to be registered 21 + * @param resources resources to be registered
22 - * @param children resources to be registered
23 * @return true if the registration succeeds, false otherwise 22 * @return true if the registration succeeds, false otherwise
24 */ 23 */
25 - boolean register(ResourcePath parent, List<ResourcePath> children); 24 + boolean register(List<ResourcePath> resources);
26 25
27 /** 26 /**
28 - * Unregisters the resources as children of the parent resource in transactional way. 27 + * Unregisters the resources in transactional way.
29 * The state after completion of this method is all the resources are unregistered, 28 * The state after completion of this method is all the resources are unregistered,
30 * or no resource is unregistered. The whole unregistration fails when any one of the 29 * or no resource is unregistered. The whole unregistration fails when any one of the
31 * resource can't be unregistered. 30 * resource can't be unregistered.
32 * 31 *
33 - * @param parent resource which is the parent of the resource to be unregistered 32 + * @param resources resources to be unregistered
34 - * @param children resources to be unregistered
35 * @return true if the registration succeeds, false otherwise 33 * @return true if the registration succeeds, false otherwise
36 */ 34 */
37 - boolean unregister(ResourcePath parent, List<ResourcePath> children); 35 + boolean unregister(List<ResourcePath> resources);
38 36
39 /** 37 /**
40 * Allocates the specified resources to the specified consumer in transactional way. 38 * Allocates the specified resources to the specified consumer in transactional way.
......
...@@ -133,7 +133,7 @@ public final class ResourceManager implements ResourceService, ResourceAdminServ ...@@ -133,7 +133,7 @@ public final class ResourceManager implements ResourceService, ResourceAdminServ
133 checkArgument(!children.isEmpty()); 133 checkArgument(!children.isEmpty());
134 134
135 List<ResourcePath> resources = Lists.transform(children, x -> ResourcePath.child(parent, x)); 135 List<ResourcePath> resources = Lists.transform(children, x -> ResourcePath.child(parent, x));
136 - return store.register(parent, resources); 136 + return store.register(resources);
137 } 137 }
138 138
139 @Override 139 @Override
...@@ -143,6 +143,6 @@ public final class ResourceManager implements ResourceService, ResourceAdminServ ...@@ -143,6 +143,6 @@ public final class ResourceManager implements ResourceService, ResourceAdminServ
143 checkArgument(!children.isEmpty()); 143 checkArgument(!children.isEmpty());
144 144
145 List<ResourcePath> resources = Lists.transform(children, x -> ResourcePath.child(parent, x)); 145 List<ResourcePath> resources = Lists.transform(children, x -> ResourcePath.child(parent, x));
146 - return store.unregister(parent, resources); 146 + return store.unregister(resources);
147 } 147 }
148 } 148 }
......
...@@ -94,9 +94,8 @@ public class ConsistentResourceStore implements ResourceStore { ...@@ -94,9 +94,8 @@ public class ConsistentResourceStore implements ResourceStore {
94 } 94 }
95 95
96 @Override 96 @Override
97 - public boolean register(ResourcePath resource, List<ResourcePath> children) { 97 + public boolean register(List<ResourcePath> resources) {
98 - checkNotNull(resource); 98 + checkNotNull(resources);
99 - checkNotNull(children);
100 99
101 TransactionContext tx = service.transactionContextBuilder().build(); 100 TransactionContext tx = service.transactionContextBuilder().build();
102 tx.begin(); 101 tx.begin();
...@@ -105,12 +104,18 @@ public class ConsistentResourceStore implements ResourceStore { ...@@ -105,12 +104,18 @@ public class ConsistentResourceStore implements ResourceStore {
105 TransactionalMap<ResourcePath, List<ResourcePath>> childTxMap = 104 TransactionalMap<ResourcePath, List<ResourcePath>> childTxMap =
106 tx.getTransactionalMap(CHILD_MAP, SERIALIZER); 105 tx.getTransactionalMap(CHILD_MAP, SERIALIZER);
107 106
108 - if (!isRegistered(childTxMap, resource)) { 107 + Map<ResourcePath, List<ResourcePath>> resourceMap = resources.stream()
109 - return abortTransaction(tx); 108 + .filter(x -> x.parent().isPresent())
110 - } 109 + .collect(Collectors.groupingBy(x -> x.parent().get()));
110 +
111 + for (Map.Entry<ResourcePath, List<ResourcePath>> entry: resourceMap.entrySet()) {
112 + if (!isRegistered(childTxMap, entry.getKey())) {
113 + return abortTransaction(tx);
114 + }
111 115
112 - if (!appendValues(childTxMap, resource, children)) { 116 + if (!appendValues(childTxMap, entry.getKey(), entry.getValue())) {
113 - return abortTransaction(tx); 117 + return abortTransaction(tx);
118 + }
114 } 119 }
115 120
116 return commitTransaction(tx); 121 return commitTransaction(tx);
...@@ -121,9 +126,8 @@ public class ConsistentResourceStore implements ResourceStore { ...@@ -121,9 +126,8 @@ public class ConsistentResourceStore implements ResourceStore {
121 } 126 }
122 127
123 @Override 128 @Override
124 - public boolean unregister(ResourcePath resource, List<ResourcePath> children) { 129 + public boolean unregister(List<ResourcePath> resources) {
125 - checkNotNull(resource); 130 + checkNotNull(resources);
126 - checkNotNull(children);
127 131
128 TransactionContext tx = service.transactionContextBuilder().build(); 132 TransactionContext tx = service.transactionContextBuilder().build();
129 tx.begin(); 133 tx.begin();
...@@ -134,14 +138,20 @@ public class ConsistentResourceStore implements ResourceStore { ...@@ -134,14 +138,20 @@ public class ConsistentResourceStore implements ResourceStore {
134 TransactionalMap<ResourcePath, ResourceConsumer> consumerTxMap = 138 TransactionalMap<ResourcePath, ResourceConsumer> consumerTxMap =
135 tx.getTransactionalMap(CONSUMER_MAP, SERIALIZER); 139 tx.getTransactionalMap(CONSUMER_MAP, SERIALIZER);
136 140
141 + Map<ResourcePath, List<ResourcePath>> resourceMap = resources.stream()
142 + .filter(x -> x.parent().isPresent())
143 + .collect(Collectors.groupingBy(x -> x.parent().get()));
144 +
137 // even if one of the resources is allocated to a consumer, 145 // even if one of the resources is allocated to a consumer,
138 // all unregistrations are regarded as failure 146 // all unregistrations are regarded as failure
139 - if (children.stream().anyMatch(x -> consumerTxMap.get(x) != null)) { 147 + for (Map.Entry<ResourcePath, List<ResourcePath>> entry: resourceMap.entrySet()) {
140 - return abortTransaction(tx); 148 + if (entry.getValue().stream().anyMatch(x -> consumerTxMap.get(x) != null)) {
141 - } 149 + return abortTransaction(tx);
150 + }
142 151
143 - if (!removeValues(childTxMap, resource, children)) { 152 + if (!removeValues(childTxMap, entry.getKey(), entry.getValue())) {
144 - return abortTransaction(tx); 153 + return abortTransaction(tx);
154 + }
145 } 155 }
146 156
147 return commitTransaction(tx); 157 return commitTransaction(tx);
......