Sho SHIMIZU
Committed by Thomas Vachuska

Pull up methods from ResourceManager to ResourceService

Change-Id: Ia4ac8e06561d38f2c60c44084dd5d6a7784cceb9
...@@ -16,11 +16,15 @@ ...@@ -16,11 +16,15 @@
16 package org.onosproject.net.newresource; 16 package org.onosproject.net.newresource;
17 17
18 import com.google.common.annotations.Beta; 18 import com.google.common.annotations.Beta;
19 +import com.google.common.collect.ImmutableList;
19 20
21 +import java.util.Arrays;
20 import java.util.Collection; 22 import java.util.Collection;
21 import java.util.List; 23 import java.util.List;
22 import java.util.Optional; 24 import java.util.Optional;
23 25
26 +import static com.google.common.base.Preconditions.checkNotNull;
27 +
24 /** 28 /**
25 * Service for allocating/releasing resource(s) and retrieving allocation(s) and availability. 29 * Service for allocating/releasing resource(s) and retrieving allocation(s) and availability.
26 */ 30 */
...@@ -33,7 +37,24 @@ public interface ResourceService { ...@@ -33,7 +37,24 @@ public interface ResourceService {
33 * @param resource resource to be allocated 37 * @param resource resource to be allocated
34 * @return allocation information enclosed by Optional. If the allocation fails, the return value is empty 38 * @return allocation information enclosed by Optional. If the allocation fails, the return value is empty
35 */ 39 */
36 - Optional<ResourceAllocation> allocate(ResourceConsumer consumer, ResourcePath resource); 40 + default Optional<ResourceAllocation> allocate(ResourceConsumer consumer, ResourcePath resource) {
41 + checkNotNull(consumer);
42 + checkNotNull(resource);
43 +
44 + List<ResourceAllocation> allocations = allocate(consumer, ImmutableList.of(resource));
45 + if (allocations.isEmpty()) {
46 + return Optional.empty();
47 + }
48 +
49 + assert allocations.size() == 1;
50 +
51 + ResourceAllocation allocation = allocations.get(0);
52 +
53 + assert allocation.resource().equals(resource);
54 +
55 + // cast is ensured by the assertions above
56 + return Optional.of(allocation);
57 + }
37 58
38 /** 59 /**
39 * Transactionally allocates the specified resources to the specified user. 60 * Transactionally allocates the specified resources to the specified user.
...@@ -53,7 +74,12 @@ public interface ResourceService { ...@@ -53,7 +74,12 @@ public interface ResourceService {
53 * @param resources resources to be allocated 74 * @param resources resources to be allocated
54 * @return non-empty list of allocation information if succeeded, otherwise empty list 75 * @return non-empty list of allocation information if succeeded, otherwise empty list
55 */ 76 */
56 - List<ResourceAllocation> allocate(ResourceConsumer consumer, ResourcePath... resources); 77 + default List<ResourceAllocation> allocate(ResourceConsumer consumer, ResourcePath... resources) {
78 + checkNotNull(consumer);
79 + checkNotNull(resources);
80 +
81 + return allocate(consumer, Arrays.asList(resources));
82 + }
57 83
58 /** 84 /**
59 * Releases the specified resource allocation. 85 * Releases the specified resource allocation.
...@@ -61,7 +87,11 @@ public interface ResourceService { ...@@ -61,7 +87,11 @@ public interface ResourceService {
61 * @param allocation resource allocation to be released 87 * @param allocation resource allocation to be released
62 * @return true if succeeded, otherwise false 88 * @return true if succeeded, otherwise false
63 */ 89 */
64 - boolean release(ResourceAllocation allocation); 90 + default boolean release(ResourceAllocation allocation) {
91 + checkNotNull(allocation);
92 +
93 + return release(ImmutableList.of(allocation));
94 + }
65 95
66 /** 96 /**
67 * Transactionally releases the specified resource allocations. 97 * Transactionally releases the specified resource allocations.
...@@ -79,7 +109,11 @@ public interface ResourceService { ...@@ -79,7 +109,11 @@ public interface ResourceService {
79 * @param allocations resource allocations to be released 109 * @param allocations resource allocations to be released
80 * @return true if succeeded, otherwise false 110 * @return true if succeeded, otherwise false
81 */ 111 */
82 - boolean release(ResourceAllocation... allocations); 112 + default boolean release(ResourceAllocation... allocations) {
113 + checkNotNull(allocations);
114 +
115 + return release(ImmutableList.copyOf(allocations));
116 + }
83 117
84 /** 118 /**
85 * Transactionally releases the resources allocated to the specified consumer. 119 * Transactionally releases the resources allocated to the specified consumer.
......
...@@ -30,7 +30,6 @@ import org.onosproject.net.newresource.ResourcePath; ...@@ -30,7 +30,6 @@ import org.onosproject.net.newresource.ResourcePath;
30 import org.onosproject.net.newresource.ResourceStore; 30 import org.onosproject.net.newresource.ResourceStore;
31 31
32 import java.util.ArrayList; 32 import java.util.ArrayList;
33 -import java.util.Arrays;
34 import java.util.Collection; 33 import java.util.Collection;
35 import java.util.List; 34 import java.util.List;
36 import java.util.Optional; 35 import java.util.Optional;
...@@ -50,26 +49,6 @@ public final class ResourceManager implements ResourceService, ResourceAdminServ ...@@ -50,26 +49,6 @@ public final class ResourceManager implements ResourceService, ResourceAdminServ
50 protected ResourceStore store; 49 protected ResourceStore store;
51 50
52 @Override 51 @Override
53 - public Optional<ResourceAllocation> allocate(ResourceConsumer consumer, ResourcePath resource) {
54 - checkNotNull(consumer);
55 - checkNotNull(resource);
56 -
57 - List<ResourceAllocation> allocations = allocate(consumer, ImmutableList.of(resource));
58 - if (allocations.isEmpty()) {
59 - return Optional.empty();
60 - }
61 -
62 - assert allocations.size() == 1;
63 -
64 - ResourceAllocation allocation = allocations.get(0);
65 -
66 - assert allocation.resource().equals(resource);
67 -
68 - // cast is ensured by the assertions above
69 - return Optional.of(allocation);
70 - }
71 -
72 - @Override
73 public List<ResourceAllocation> allocate(ResourceConsumer consumer, 52 public List<ResourceAllocation> allocate(ResourceConsumer consumer,
74 List<ResourcePath> resources) { 53 List<ResourcePath> resources) {
75 checkNotNull(consumer); 54 checkNotNull(consumer);
...@@ -89,21 +68,6 @@ public final class ResourceManager implements ResourceService, ResourceAdminServ ...@@ -89,21 +68,6 @@ public final class ResourceManager implements ResourceService, ResourceAdminServ
89 } 68 }
90 69
91 @Override 70 @Override
92 - public List<ResourceAllocation> allocate(ResourceConsumer consumer, ResourcePath... resources) {
93 - checkNotNull(consumer);
94 - checkNotNull(resources);
95 -
96 - return allocate(consumer, Arrays.asList(resources));
97 - }
98 -
99 - @Override
100 - public boolean release(ResourceAllocation allocation) {
101 - checkNotNull(allocation);
102 -
103 - return release(ImmutableList.of(allocation));
104 - }
105 -
106 - @Override
107 public boolean release(List<ResourceAllocation> allocations) { 71 public boolean release(List<ResourceAllocation> allocations) {
108 checkNotNull(allocations); 72 checkNotNull(allocations);
109 73
...@@ -118,13 +82,6 @@ public final class ResourceManager implements ResourceService, ResourceAdminServ ...@@ -118,13 +82,6 @@ public final class ResourceManager implements ResourceService, ResourceAdminServ
118 } 82 }
119 83
120 @Override 84 @Override
121 - public boolean release(ResourceAllocation... allocations) {
122 - checkNotNull(allocations);
123 -
124 - return release(ImmutableList.copyOf(allocations));
125 - }
126 -
127 - @Override
128 public boolean release(ResourceConsumer consumer) { 85 public boolean release(ResourceConsumer consumer) {
129 checkNotNull(consumer); 86 checkNotNull(consumer);
130 87
......