Sho SHIMIZU | abd849c | 2015-07-14 09:14:12 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Open Networking Laboratory |
| 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 | */ |
| 16 | package org.onosproject.net.newresource; |
| 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; |
| 26 | |
Sho SHIMIZU | 726a358 | 2015-08-17 11:46:57 -0700 | [diff] [blame] | 27 | import static com.google.common.base.Preconditions.checkNotNull; |
| 28 | |
Sho SHIMIZU | abd849c | 2015-07-14 09:14:12 -0700 | [diff] [blame] | 29 | /** |
| 30 | * Service for allocating/releasing resource(s) and retrieving allocation(s) and availability. |
| 31 | */ |
| 32 | @Beta |
Sho SHIMIZU | fa62b47 | 2015-11-02 17:35:46 -0800 | [diff] [blame] | 33 | public interface ResourceService extends ListenerService<ResourceEvent, ResourceListener> { |
Sho SHIMIZU | abd849c | 2015-07-14 09:14:12 -0700 | [diff] [blame] | 34 | /** |
| 35 | * Allocates the specified resource to the specified user. |
| 36 | * |
| 37 | * @param consumer resource user which the resource is allocated to |
| 38 | * @param resource resource to be allocated |
Sho SHIMIZU | abd849c | 2015-07-14 09:14:12 -0700 | [diff] [blame] | 39 | * @return allocation information enclosed by Optional. If the allocation fails, the return value is empty |
| 40 | */ |
Sho SHIMIZU | 726a358 | 2015-08-17 11:46:57 -0700 | [diff] [blame] | 41 | default Optional<ResourceAllocation> allocate(ResourceConsumer consumer, ResourcePath resource) { |
| 42 | checkNotNull(consumer); |
| 43 | checkNotNull(resource); |
| 44 | |
| 45 | List<ResourceAllocation> allocations = allocate(consumer, ImmutableList.of(resource)); |
| 46 | if (allocations.isEmpty()) { |
| 47 | return Optional.empty(); |
| 48 | } |
| 49 | |
| 50 | assert allocations.size() == 1; |
| 51 | |
| 52 | ResourceAllocation allocation = allocations.get(0); |
| 53 | |
| 54 | assert allocation.resource().equals(resource); |
| 55 | |
| 56 | // cast is ensured by the assertions above |
| 57 | return Optional.of(allocation); |
| 58 | } |
Sho SHIMIZU | abd849c | 2015-07-14 09:14:12 -0700 | [diff] [blame] | 59 | |
| 60 | /** |
| 61 | * Transactionally allocates the specified resources to the specified user. |
| 62 | * All allocations are made when this method succeeds, or no allocation is made when this method fails. |
| 63 | * |
| 64 | * @param consumer resource user which the resources are allocated to |
| 65 | * @param resources resources to be allocated |
| 66 | * @return non-empty list of allocation information if succeeded, otherwise empty list |
| 67 | */ |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 68 | List<ResourceAllocation> allocate(ResourceConsumer consumer, List<ResourcePath> resources); |
Sho SHIMIZU | abd849c | 2015-07-14 09:14:12 -0700 | [diff] [blame] | 69 | |
| 70 | /** |
| 71 | * Transactionally allocates the specified resources to the specified user. |
| 72 | * All allocations are made when this method succeeds, or no allocation is made when this method fails. |
| 73 | * |
| 74 | * @param consumer resource user which the resources are allocated to |
| 75 | * @param resources resources to be allocated |
| 76 | * @return non-empty list of allocation information if succeeded, otherwise empty list |
| 77 | */ |
Sho SHIMIZU | 726a358 | 2015-08-17 11:46:57 -0700 | [diff] [blame] | 78 | default List<ResourceAllocation> allocate(ResourceConsumer consumer, ResourcePath... resources) { |
| 79 | checkNotNull(consumer); |
| 80 | checkNotNull(resources); |
| 81 | |
| 82 | return allocate(consumer, Arrays.asList(resources)); |
| 83 | } |
Sho SHIMIZU | abd849c | 2015-07-14 09:14:12 -0700 | [diff] [blame] | 84 | |
| 85 | /** |
| 86 | * Releases the specified resource allocation. |
| 87 | * |
| 88 | * @param allocation resource allocation to be released |
Sho SHIMIZU | abd849c | 2015-07-14 09:14:12 -0700 | [diff] [blame] | 89 | * @return true if succeeded, otherwise false |
| 90 | */ |
Sho SHIMIZU | 726a358 | 2015-08-17 11:46:57 -0700 | [diff] [blame] | 91 | default boolean release(ResourceAllocation allocation) { |
| 92 | checkNotNull(allocation); |
| 93 | |
| 94 | return release(ImmutableList.of(allocation)); |
| 95 | } |
Sho SHIMIZU | abd849c | 2015-07-14 09:14:12 -0700 | [diff] [blame] | 96 | |
| 97 | /** |
| 98 | * Transactionally releases the specified resource allocations. |
| 99 | * All allocations are released when this method succeeded, or no allocation is released when this method fails. |
| 100 | * |
| 101 | * @param allocations resource allocations to be released |
| 102 | * @return true if succeeded, otherwise false |
| 103 | */ |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 104 | boolean release(List<ResourceAllocation> allocations); |
Sho SHIMIZU | abd849c | 2015-07-14 09:14:12 -0700 | [diff] [blame] | 105 | |
| 106 | /** |
| 107 | * Transactionally releases the specified resource allocations. |
| 108 | * All allocations are released when this method succeeded, or no allocation is released when this method fails. |
| 109 | * |
| 110 | * @param allocations resource allocations to be released |
| 111 | * @return true if succeeded, otherwise false |
| 112 | */ |
Sho SHIMIZU | 726a358 | 2015-08-17 11:46:57 -0700 | [diff] [blame] | 113 | default boolean release(ResourceAllocation... allocations) { |
| 114 | checkNotNull(allocations); |
| 115 | |
| 116 | return release(ImmutableList.copyOf(allocations)); |
| 117 | } |
Sho SHIMIZU | abd849c | 2015-07-14 09:14:12 -0700 | [diff] [blame] | 118 | |
| 119 | /** |
| 120 | * Transactionally releases the resources allocated to the specified consumer. |
| 121 | * All allocations are released when this method succeeded, or no allocation is released when this method fails. |
| 122 | * |
| 123 | * @param consumer consumer whose allocated resources are to be released |
| 124 | * @return true if succeeded, otherwise false |
| 125 | */ |
| 126 | boolean release(ResourceConsumer consumer); |
| 127 | |
| 128 | /** |
Sho SHIMIZU | f853b0e | 2015-09-29 15:15:32 -0700 | [diff] [blame] | 129 | * Returns resource allocation of the specified resource. |
| 130 | * |
| 131 | * @param resource resource to check the allocation |
| 132 | * @return allocation information enclosed by Optional. |
| 133 | * If the resource is not allocated, the return value is empty. |
| 134 | */ |
| 135 | Optional<ResourceAllocation> getResourceAllocation(ResourcePath resource); |
| 136 | |
| 137 | /** |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 138 | * 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] | 139 | * |
Madan Jampani | f97edc1 | 2015-08-31 14:41:01 -0700 | [diff] [blame] | 140 | * @param parent parent resource path |
Sho SHIMIZU | abd849c | 2015-07-14 09:14:12 -0700 | [diff] [blame] | 141 | * @param cls class to specify a type of resource |
Sho SHIMIZU | abd849c | 2015-07-14 09:14:12 -0700 | [diff] [blame] | 142 | * @param <T> type of the resource |
| 143 | * @return non-empty collection of resource allocations if resources are allocated with the subject and type, |
| 144 | * empty collection if no resource is allocated with the subject and type |
| 145 | */ |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 146 | <T> Collection<ResourceAllocation> getResourceAllocations(ResourcePath parent, Class<T> cls); |
Sho SHIMIZU | abd849c | 2015-07-14 09:14:12 -0700 | [diff] [blame] | 147 | |
| 148 | /** |
| 149 | * Returns resources allocated to the specified consumer. |
| 150 | * |
| 151 | * @param consumer consumer whose allocated resources are to be returned |
| 152 | * @return resources allocated to the consumer |
| 153 | */ |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 154 | Collection<ResourceAllocation> getResourceAllocations(ResourceConsumer consumer); |
Sho SHIMIZU | abd849c | 2015-07-14 09:14:12 -0700 | [diff] [blame] | 155 | |
| 156 | /** |
Sho SHIMIZU | e7f4f3f | 2015-10-13 16:27:25 -0700 | [diff] [blame] | 157 | * Returns resource paths that point available child resources under the specified resource path. |
| 158 | * |
| 159 | * @param parent parent resource path |
| 160 | * @return available resource paths under the specified resource path |
| 161 | */ |
| 162 | Collection<ResourcePath> getAvailableResources(ResourcePath parent); |
| 163 | |
| 164 | /** |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 165 | * Returns the availability of the specified resource. |
Sho SHIMIZU | abd849c | 2015-07-14 09:14:12 -0700 | [diff] [blame] | 166 | * |
| 167 | * @param resource resource to check the availability |
Sho SHIMIZU | abd849c | 2015-07-14 09:14:12 -0700 | [diff] [blame] | 168 | * @return true if available, otherwise false |
| 169 | */ |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 170 | boolean isAvailable(ResourcePath resource); |
Sho SHIMIZU | abd849c | 2015-07-14 09:14:12 -0700 | [diff] [blame] | 171 | |
| 172 | // TODO: listener and event mechanism need to be considered |
| 173 | } |