Sho SHIMIZU | abd849c | 2015-07-14 09:14:12 -0700 | [diff] [blame] | 1 | /* |
Sho SHIMIZU | e18cb12 | 2016-02-22 21:04:56 -0800 | [diff] [blame] | 2 | * Copyright 2015-2016 Open Networking Laboratory |
Sho SHIMIZU | abd849c | 2015-07-14 09:14:12 -0700 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Sho SHIMIZU | e18cb12 | 2016-02-22 21:04:56 -0800 | [diff] [blame] | 16 | package org.onosproject.net.resource; |
Sho SHIMIZU | abd849c | 2015-07-14 09:14:12 -0700 | [diff] [blame] | 17 | |
| 18 | import com.google.common.annotations.Beta; |
Sho SHIMIZU | 726a358 | 2015-08-17 11:46:57 -0700 | [diff] [blame] | 19 | import com.google.common.collect.ImmutableList; |
Sho SHIMIZU | fa62b47 | 2015-11-02 17:35:46 -0800 | [diff] [blame] | 20 | import org.onosproject.event.ListenerService; |
Sho SHIMIZU | abd849c | 2015-07-14 09:14:12 -0700 | [diff] [blame] | 21 | |
Sho SHIMIZU | 726a358 | 2015-08-17 11:46:57 -0700 | [diff] [blame] | 22 | import java.util.Arrays; |
Sho SHIMIZU | abd849c | 2015-07-14 09:14:12 -0700 | [diff] [blame] | 23 | import java.util.Collection; |
| 24 | import java.util.List; |
| 25 | import java.util.Optional; |
Sho SHIMIZU | 83258ae | 2016-01-29 17:39:07 -0800 | [diff] [blame] | 26 | import java.util.Set; |
Sho SHIMIZU | abd849c | 2015-07-14 09:14:12 -0700 | [diff] [blame] | 27 | |
Sho SHIMIZU | 726a358 | 2015-08-17 11:46:57 -0700 | [diff] [blame] | 28 | import static com.google.common.base.Preconditions.checkNotNull; |
| 29 | |
Sho SHIMIZU | abd849c | 2015-07-14 09:14:12 -0700 | [diff] [blame] | 30 | /** |
| 31 | * Service for allocating/releasing resource(s) and retrieving allocation(s) and availability. |
| 32 | */ |
| 33 | @Beta |
Sho SHIMIZU | fa62b47 | 2015-11-02 17:35:46 -0800 | [diff] [blame] | 34 | public interface ResourceService extends ListenerService<ResourceEvent, ResourceListener> { |
Sho SHIMIZU | abd849c | 2015-07-14 09:14:12 -0700 | [diff] [blame] | 35 | /** |
| 36 | * Allocates the specified resource to the specified user. |
| 37 | * |
| 38 | * @param consumer resource user which the resource is allocated to |
| 39 | * @param resource resource to be allocated |
Sho SHIMIZU | abd849c | 2015-07-14 09:14:12 -0700 | [diff] [blame] | 40 | * @return allocation information enclosed by Optional. If the allocation fails, the return value is empty |
| 41 | */ |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 42 | default Optional<ResourceAllocation> allocate(ResourceConsumer consumer, Resource resource) { |
Sho SHIMIZU | 726a358 | 2015-08-17 11:46:57 -0700 | [diff] [blame] | 43 | checkNotNull(consumer); |
| 44 | checkNotNull(resource); |
| 45 | |
| 46 | List<ResourceAllocation> allocations = allocate(consumer, ImmutableList.of(resource)); |
| 47 | if (allocations.isEmpty()) { |
| 48 | return Optional.empty(); |
| 49 | } |
| 50 | |
| 51 | assert allocations.size() == 1; |
| 52 | |
| 53 | ResourceAllocation allocation = allocations.get(0); |
| 54 | |
| 55 | assert allocation.resource().equals(resource); |
| 56 | |
| 57 | // cast is ensured by the assertions above |
| 58 | return Optional.of(allocation); |
| 59 | } |
Sho SHIMIZU | abd849c | 2015-07-14 09:14:12 -0700 | [diff] [blame] | 60 | |
| 61 | /** |
| 62 | * Transactionally allocates the specified resources to the specified user. |
| 63 | * All allocations are made when this method succeeds, or no allocation is made when this method fails. |
| 64 | * |
| 65 | * @param consumer resource user which the resources are allocated to |
| 66 | * @param resources resources to be allocated |
| 67 | * @return non-empty list of allocation information if succeeded, otherwise empty list |
| 68 | */ |
Jonathan Hart | 5615126 | 2016-02-11 09:48:50 -0800 | [diff] [blame] | 69 | List<ResourceAllocation> allocate(ResourceConsumer consumer, List<Resource> resources); |
Sho SHIMIZU | abd849c | 2015-07-14 09:14:12 -0700 | [diff] [blame] | 70 | |
| 71 | /** |
| 72 | * Transactionally allocates the specified resources to the specified user. |
| 73 | * All allocations are made when this method succeeds, or no allocation is made when this method fails. |
| 74 | * |
| 75 | * @param consumer resource user which the resources are allocated to |
| 76 | * @param resources resources to be allocated |
| 77 | * @return non-empty list of allocation information if succeeded, otherwise empty list |
| 78 | */ |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 79 | default List<ResourceAllocation> allocate(ResourceConsumer consumer, Resource... resources) { |
Sho SHIMIZU | 726a358 | 2015-08-17 11:46:57 -0700 | [diff] [blame] | 80 | checkNotNull(consumer); |
| 81 | checkNotNull(resources); |
| 82 | |
| 83 | return allocate(consumer, Arrays.asList(resources)); |
| 84 | } |
Sho SHIMIZU | abd849c | 2015-07-14 09:14:12 -0700 | [diff] [blame] | 85 | |
| 86 | /** |
| 87 | * Releases the specified resource allocation. |
| 88 | * |
| 89 | * @param allocation resource allocation to be released |
Sho SHIMIZU | abd849c | 2015-07-14 09:14:12 -0700 | [diff] [blame] | 90 | * @return true if succeeded, otherwise false |
| 91 | */ |
Sho SHIMIZU | 726a358 | 2015-08-17 11:46:57 -0700 | [diff] [blame] | 92 | default boolean release(ResourceAllocation allocation) { |
| 93 | checkNotNull(allocation); |
| 94 | |
| 95 | return release(ImmutableList.of(allocation)); |
| 96 | } |
Sho SHIMIZU | abd849c | 2015-07-14 09:14:12 -0700 | [diff] [blame] | 97 | |
| 98 | /** |
| 99 | * Transactionally releases the specified resource allocations. |
| 100 | * All allocations are released when this method succeeded, or no allocation is released when this method fails. |
| 101 | * |
| 102 | * @param allocations resource allocations to be released |
| 103 | * @return true if succeeded, otherwise false |
| 104 | */ |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 105 | boolean release(List<ResourceAllocation> allocations); |
Sho SHIMIZU | abd849c | 2015-07-14 09:14:12 -0700 | [diff] [blame] | 106 | |
| 107 | /** |
| 108 | * Transactionally releases the specified resource allocations. |
| 109 | * All allocations are released when this method succeeded, or no allocation is released when this method fails. |
| 110 | * |
| 111 | * @param allocations resource allocations to be released |
| 112 | * @return true if succeeded, otherwise false |
| 113 | */ |
Sho SHIMIZU | 726a358 | 2015-08-17 11:46:57 -0700 | [diff] [blame] | 114 | default boolean release(ResourceAllocation... allocations) { |
| 115 | checkNotNull(allocations); |
| 116 | |
| 117 | return release(ImmutableList.copyOf(allocations)); |
| 118 | } |
Sho SHIMIZU | abd849c | 2015-07-14 09:14:12 -0700 | [diff] [blame] | 119 | |
| 120 | /** |
| 121 | * Transactionally releases the resources allocated to the specified consumer. |
| 122 | * All allocations are released when this method succeeded, or no allocation is released when this method fails. |
| 123 | * |
| 124 | * @param consumer consumer whose allocated resources are to be released |
| 125 | * @return true if succeeded, otherwise false |
| 126 | */ |
| 127 | boolean release(ResourceConsumer consumer); |
| 128 | |
| 129 | /** |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 130 | * Returns resource allocations of the specified resource. |
Sho SHIMIZU | f853b0e | 2015-09-29 15:15:32 -0700 | [diff] [blame] | 131 | * |
Sho SHIMIZU | dd3750c | 2016-02-01 11:37:04 -0800 | [diff] [blame] | 132 | * @param id ID of the resource to check the allocation |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 133 | * @return list of allocation information. |
| 134 | * If the resource is not allocated, the return value is an empty list. |
Sho SHIMIZU | f853b0e | 2015-09-29 15:15:32 -0700 | [diff] [blame] | 135 | */ |
Sho SHIMIZU | dd3750c | 2016-02-01 11:37:04 -0800 | [diff] [blame] | 136 | List<ResourceAllocation> getResourceAllocations(ResourceId id); |
Sho SHIMIZU | f853b0e | 2015-09-29 15:15:32 -0700 | [diff] [blame] | 137 | |
| 138 | /** |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 139 | * Returns allocated resources being as children of the specified parent and being the specified resource type. |
Sho SHIMIZU | abd849c | 2015-07-14 09:14:12 -0700 | [diff] [blame] | 140 | * |
Sho SHIMIZU | dd3750c | 2016-02-01 11:37:04 -0800 | [diff] [blame] | 141 | * @param parent parent resource ID |
Sho SHIMIZU | abd849c | 2015-07-14 09:14:12 -0700 | [diff] [blame] | 142 | * @param cls class to specify a type of resource |
Sho SHIMIZU | abd849c | 2015-07-14 09:14:12 -0700 | [diff] [blame] | 143 | * @param <T> type of the resource |
| 144 | * @return non-empty collection of resource allocations if resources are allocated with the subject and type, |
| 145 | * empty collection if no resource is allocated with the subject and type |
| 146 | */ |
Sho SHIMIZU | dd3750c | 2016-02-01 11:37:04 -0800 | [diff] [blame] | 147 | <T> Collection<ResourceAllocation> getResourceAllocations(DiscreteResourceId parent, Class<T> cls); |
Sho SHIMIZU | abd849c | 2015-07-14 09:14:12 -0700 | [diff] [blame] | 148 | |
| 149 | /** |
| 150 | * Returns resources allocated to the specified consumer. |
| 151 | * |
| 152 | * @param consumer consumer whose allocated resources are to be returned |
| 153 | * @return resources allocated to the consumer |
| 154 | */ |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 155 | Collection<ResourceAllocation> getResourceAllocations(ResourceConsumer consumer); |
Sho SHIMIZU | abd849c | 2015-07-14 09:14:12 -0700 | [diff] [blame] | 156 | |
| 157 | /** |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 158 | * Returns resources that point available child resources under the specified resource. |
Sho SHIMIZU | e7f4f3f | 2015-10-13 16:27:25 -0700 | [diff] [blame] | 159 | * |
Sho SHIMIZU | dd3750c | 2016-02-01 11:37:04 -0800 | [diff] [blame] | 160 | * @param parent parent resource ID |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 161 | * @return available resources under the specified resource |
Sho SHIMIZU | e7f4f3f | 2015-10-13 16:27:25 -0700 | [diff] [blame] | 162 | */ |
Sho SHIMIZU | dd3750c | 2016-02-01 11:37:04 -0800 | [diff] [blame] | 163 | Set<Resource> getAvailableResources(DiscreteResourceId parent); |
Sho SHIMIZU | e7f4f3f | 2015-10-13 16:27:25 -0700 | [diff] [blame] | 164 | |
| 165 | /** |
Sho SHIMIZU | 7332fe4 | 2016-02-15 14:58:33 -0800 | [diff] [blame] | 166 | * Returns available resources which are child resources of the specified parent and |
| 167 | * whose type is the specified type. |
| 168 | * |
| 169 | * @param parent parent resource ID |
| 170 | * @param cls class to specify a type of resource |
| 171 | * @param <T> type of the resource |
| 172 | * @return available resources of the specified type under the specified parent resource |
| 173 | */ |
| 174 | <T> Set<Resource> getAvailableResources(DiscreteResourceId parent, Class<T> cls); |
| 175 | |
| 176 | /** |
| 177 | * Returns available resource values which are the values of the child resource of |
| 178 | * the specified parent and whose type is the specified type. |
| 179 | * |
| 180 | * @param parent parent resource ID |
| 181 | * @param cls class to specify a type of resource |
| 182 | * @param <T> type of the resource |
| 183 | * @return available resource value of the specified type under the specified parent resource |
| 184 | */ |
| 185 | <T> Set<T> getAvailableResourceValues(DiscreteResourceId parent, Class<T> cls); |
| 186 | /** |
HIGUCHI Yuta | dff91af | 2016-01-21 16:34:45 -0800 | [diff] [blame] | 187 | * Returns resources registered under the specified resource. |
| 188 | * |
Sho SHIMIZU | dd3750c | 2016-02-01 11:37:04 -0800 | [diff] [blame] | 189 | * @param parent parent resource ID |
HIGUCHI Yuta | dff91af | 2016-01-21 16:34:45 -0800 | [diff] [blame] | 190 | * @return registered resources under the specified resource |
| 191 | */ |
Sho SHIMIZU | dd3750c | 2016-02-01 11:37:04 -0800 | [diff] [blame] | 192 | Set<Resource> getRegisteredResources(DiscreteResourceId parent); |
HIGUCHI Yuta | dff91af | 2016-01-21 16:34:45 -0800 | [diff] [blame] | 193 | |
HIGUCHI Yuta | dff91af | 2016-01-21 16:34:45 -0800 | [diff] [blame] | 194 | /** |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 195 | * Returns the availability of the specified resource. |
Sho SHIMIZU | abd849c | 2015-07-14 09:14:12 -0700 | [diff] [blame] | 196 | * |
| 197 | * @param resource resource to check the availability |
Sho SHIMIZU | abd849c | 2015-07-14 09:14:12 -0700 | [diff] [blame] | 198 | * @return true if available, otherwise false |
| 199 | */ |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 200 | boolean isAvailable(Resource resource); |
Sho SHIMIZU | abd849c | 2015-07-14 09:14:12 -0700 | [diff] [blame] | 201 | |
| 202 | // TODO: listener and event mechanism need to be considered |
| 203 | } |