Sho SHIMIZU | abd849c | 2015-07-14 09:14:12 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
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.List; |
| 24 | import java.util.Optional; |
| 25 | |
Sho SHIMIZU | 726a358 | 2015-08-17 11:46:57 -0700 | [diff] [blame] | 26 | import static com.google.common.base.Preconditions.checkNotNull; |
| 27 | |
Sho SHIMIZU | abd849c | 2015-07-14 09:14:12 -0700 | [diff] [blame] | 28 | /** |
| 29 | * Service for allocating/releasing resource(s) and retrieving allocation(s) and availability. |
| 30 | */ |
| 31 | @Beta |
Sho SHIMIZU | a6b4dc7 | 2016-03-11 19:00:20 -0800 | [diff] [blame] | 32 | public interface ResourceService extends ResourceQueryService, ListenerService<ResourceEvent, ResourceListener> { |
Sho SHIMIZU | abd849c | 2015-07-14 09:14:12 -0700 | [diff] [blame] | 33 | /** |
| 34 | * Allocates the specified resource to the specified user. |
| 35 | * |
| 36 | * @param consumer resource user which the resource is allocated to |
| 37 | * @param resource resource to be allocated |
Sho SHIMIZU | abd849c | 2015-07-14 09:14:12 -0700 | [diff] [blame] | 38 | * @return allocation information enclosed by Optional. If the allocation fails, the return value is empty |
| 39 | */ |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 40 | default Optional<ResourceAllocation> allocate(ResourceConsumer consumer, Resource resource) { |
Sho SHIMIZU | 726a358 | 2015-08-17 11:46:57 -0700 | [diff] [blame] | 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 | } |
Sho SHIMIZU | abd849c | 2015-07-14 09:14:12 -0700 | [diff] [blame] | 58 | |
| 59 | /** |
| 60 | * Transactionally allocates the specified resources to the specified user. |
| 61 | * All allocations are made when this method succeeds, or no allocation is made when this method fails. |
| 62 | * |
Sho SHIMIZU | a6b4dc7 | 2016-03-11 19:00:20 -0800 | [diff] [blame] | 63 | * @param consumer resource user which the resources are allocated to |
Sho SHIMIZU | abd849c | 2015-07-14 09:14:12 -0700 | [diff] [blame] | 64 | * @param resources resources to be allocated |
| 65 | * @return non-empty list of allocation information if succeeded, otherwise empty list |
| 66 | */ |
Sho SHIMIZU | ef835c9 | 2016-08-08 13:51:17 -0700 | [diff] [blame] | 67 | List<ResourceAllocation> allocate(ResourceConsumer consumer, List<? extends Resource> resources); |
Sho SHIMIZU | abd849c | 2015-07-14 09:14:12 -0700 | [diff] [blame] | 68 | |
| 69 | /** |
| 70 | * Transactionally allocates the specified resources to the specified user. |
| 71 | * All allocations are made when this method succeeds, or no allocation is made when this method fails. |
| 72 | * |
Sho SHIMIZU | a6b4dc7 | 2016-03-11 19:00:20 -0800 | [diff] [blame] | 73 | * @param consumer resource user which the resources are allocated to |
Sho SHIMIZU | abd849c | 2015-07-14 09:14:12 -0700 | [diff] [blame] | 74 | * @param resources resources to be allocated |
| 75 | * @return non-empty list of allocation information if succeeded, otherwise empty list |
| 76 | */ |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 77 | default List<ResourceAllocation> allocate(ResourceConsumer consumer, Resource... resources) { |
Sho SHIMIZU | 726a358 | 2015-08-17 11:46:57 -0700 | [diff] [blame] | 78 | checkNotNull(consumer); |
| 79 | checkNotNull(resources); |
| 80 | |
| 81 | return allocate(consumer, Arrays.asList(resources)); |
| 82 | } |
Sho SHIMIZU | abd849c | 2015-07-14 09:14:12 -0700 | [diff] [blame] | 83 | |
| 84 | /** |
| 85 | * Releases the specified resource allocation. |
| 86 | * |
| 87 | * @param allocation resource allocation to be released |
Sho SHIMIZU | abd849c | 2015-07-14 09:14:12 -0700 | [diff] [blame] | 88 | * @return true if succeeded, otherwise false |
| 89 | */ |
Sho SHIMIZU | 726a358 | 2015-08-17 11:46:57 -0700 | [diff] [blame] | 90 | default boolean release(ResourceAllocation allocation) { |
| 91 | checkNotNull(allocation); |
| 92 | |
| 93 | return release(ImmutableList.of(allocation)); |
| 94 | } |
Sho SHIMIZU | abd849c | 2015-07-14 09:14:12 -0700 | [diff] [blame] | 95 | |
| 96 | /** |
| 97 | * Transactionally releases the specified resource allocations. |
| 98 | * All allocations are released when this method succeeded, or no allocation is released when this method fails. |
| 99 | * |
| 100 | * @param allocations resource allocations to be released |
| 101 | * @return true if succeeded, otherwise false |
| 102 | */ |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 103 | boolean release(List<ResourceAllocation> allocations); |
Sho SHIMIZU | abd849c | 2015-07-14 09:14:12 -0700 | [diff] [blame] | 104 | |
| 105 | /** |
| 106 | * Transactionally releases the specified resource allocations. |
| 107 | * All allocations are released when this method succeeded, or no allocation is released when this method fails. |
| 108 | * |
| 109 | * @param allocations resource allocations to be released |
| 110 | * @return true if succeeded, otherwise false |
| 111 | */ |
Sho SHIMIZU | 726a358 | 2015-08-17 11:46:57 -0700 | [diff] [blame] | 112 | default boolean release(ResourceAllocation... allocations) { |
| 113 | checkNotNull(allocations); |
| 114 | |
| 115 | return release(ImmutableList.copyOf(allocations)); |
| 116 | } |
Sho SHIMIZU | abd849c | 2015-07-14 09:14:12 -0700 | [diff] [blame] | 117 | |
| 118 | /** |
| 119 | * Transactionally releases the resources allocated to the specified consumer. |
| 120 | * All allocations are released when this method succeeded, or no allocation is released when this method fails. |
| 121 | * |
| 122 | * @param consumer consumer whose allocated resources are to be released |
| 123 | * @return true if succeeded, otherwise false |
| 124 | */ |
| 125 | boolean release(ResourceConsumer consumer); |
| 126 | |
Sho SHIMIZU | abd849c | 2015-07-14 09:14:12 -0700 | [diff] [blame] | 127 | // TODO: listener and event mechanism need to be considered |
| 128 | } |