blob: ad0c29b52a7b79620727c827440b1080b14ce550 [file] [log] [blame]
Sho SHIMIZUabd849c2015-07-14 09:14:12 -07001/*
Sho SHIMIZUe18cb122016-02-22 21:04:56 -08002 * Copyright 2015-2016 Open Networking Laboratory
Sho SHIMIZUabd849c2015-07-14 09:14:12 -07003 *
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 SHIMIZUe18cb122016-02-22 21:04:56 -080016package org.onosproject.net.resource;
Sho SHIMIZUabd849c2015-07-14 09:14:12 -070017
18import com.google.common.annotations.Beta;
Sho SHIMIZU726a3582015-08-17 11:46:57 -070019import com.google.common.collect.ImmutableList;
Sho SHIMIZUfa62b472015-11-02 17:35:46 -080020import org.onosproject.event.ListenerService;
Sho SHIMIZUabd849c2015-07-14 09:14:12 -070021
Sho SHIMIZU726a3582015-08-17 11:46:57 -070022import java.util.Arrays;
Sho SHIMIZUabd849c2015-07-14 09:14:12 -070023import java.util.Collection;
24import java.util.List;
25import java.util.Optional;
Sho SHIMIZU83258ae2016-01-29 17:39:07 -080026import java.util.Set;
Sho SHIMIZUabd849c2015-07-14 09:14:12 -070027
Sho SHIMIZU726a3582015-08-17 11:46:57 -070028import static com.google.common.base.Preconditions.checkNotNull;
29
Sho SHIMIZUabd849c2015-07-14 09:14:12 -070030/**
31 * Service for allocating/releasing resource(s) and retrieving allocation(s) and availability.
32 */
33@Beta
Sho SHIMIZUfa62b472015-11-02 17:35:46 -080034public interface ResourceService extends ListenerService<ResourceEvent, ResourceListener> {
Sho SHIMIZUabd849c2015-07-14 09:14:12 -070035 /**
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 SHIMIZUabd849c2015-07-14 09:14:12 -070040 * @return allocation information enclosed by Optional. If the allocation fails, the return value is empty
41 */
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -080042 default Optional<ResourceAllocation> allocate(ResourceConsumer consumer, Resource resource) {
Sho SHIMIZU726a3582015-08-17 11:46:57 -070043 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 SHIMIZUabd849c2015-07-14 09:14:12 -070060
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 Hart56151262016-02-11 09:48:50 -080069 List<ResourceAllocation> allocate(ResourceConsumer consumer, List<Resource> resources);
Sho SHIMIZUabd849c2015-07-14 09:14:12 -070070
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 SHIMIZU8fa670a2016-01-14 11:17:18 -080079 default List<ResourceAllocation> allocate(ResourceConsumer consumer, Resource... resources) {
Sho SHIMIZU726a3582015-08-17 11:46:57 -070080 checkNotNull(consumer);
81 checkNotNull(resources);
82
83 return allocate(consumer, Arrays.asList(resources));
84 }
Sho SHIMIZUabd849c2015-07-14 09:14:12 -070085
86 /**
87 * Releases the specified resource allocation.
88 *
89 * @param allocation resource allocation to be released
Sho SHIMIZUabd849c2015-07-14 09:14:12 -070090 * @return true if succeeded, otherwise false
91 */
Sho SHIMIZU726a3582015-08-17 11:46:57 -070092 default boolean release(ResourceAllocation allocation) {
93 checkNotNull(allocation);
94
95 return release(ImmutableList.of(allocation));
96 }
Sho SHIMIZUabd849c2015-07-14 09:14:12 -070097
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 SHIMIZU1f5e5912015-08-10 17:00:00 -0700105 boolean release(List<ResourceAllocation> allocations);
Sho SHIMIZUabd849c2015-07-14 09:14:12 -0700106
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 SHIMIZU726a3582015-08-17 11:46:57 -0700114 default boolean release(ResourceAllocation... allocations) {
115 checkNotNull(allocations);
116
117 return release(ImmutableList.copyOf(allocations));
118 }
Sho SHIMIZUabd849c2015-07-14 09:14:12 -0700119
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 SHIMIZU6c9e33a2016-01-07 18:45:27 -0800130 * Returns resource allocations of the specified resource.
Sho SHIMIZUf853b0e2015-09-29 15:15:32 -0700131 *
Sho SHIMIZUdd3750c2016-02-01 11:37:04 -0800132 * @param id ID of the resource to check the allocation
Sho SHIMIZU6c9e33a2016-01-07 18:45:27 -0800133 * @return list of allocation information.
134 * If the resource is not allocated, the return value is an empty list.
Sho SHIMIZUf853b0e2015-09-29 15:15:32 -0700135 */
Sho SHIMIZUdd3750c2016-02-01 11:37:04 -0800136 List<ResourceAllocation> getResourceAllocations(ResourceId id);
Sho SHIMIZUf853b0e2015-09-29 15:15:32 -0700137
138 /**
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -0700139 * Returns allocated resources being as children of the specified parent and being the specified resource type.
Sho SHIMIZUabd849c2015-07-14 09:14:12 -0700140 *
Sho SHIMIZUdd3750c2016-02-01 11:37:04 -0800141 * @param parent parent resource ID
Sho SHIMIZUabd849c2015-07-14 09:14:12 -0700142 * @param cls class to specify a type of resource
Sho SHIMIZUabd849c2015-07-14 09:14:12 -0700143 * @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 SHIMIZUdd3750c2016-02-01 11:37:04 -0800147 <T> Collection<ResourceAllocation> getResourceAllocations(DiscreteResourceId parent, Class<T> cls);
Sho SHIMIZUabd849c2015-07-14 09:14:12 -0700148
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 SHIMIZU1f5e5912015-08-10 17:00:00 -0700155 Collection<ResourceAllocation> getResourceAllocations(ResourceConsumer consumer);
Sho SHIMIZUabd849c2015-07-14 09:14:12 -0700156
157 /**
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -0800158 * Returns resources that point available child resources under the specified resource.
Sho SHIMIZUe7f4f3f2015-10-13 16:27:25 -0700159 *
Sho SHIMIZUdd3750c2016-02-01 11:37:04 -0800160 * @param parent parent resource ID
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -0800161 * @return available resources under the specified resource
Sho SHIMIZUe7f4f3f2015-10-13 16:27:25 -0700162 */
Sho SHIMIZUdd3750c2016-02-01 11:37:04 -0800163 Set<Resource> getAvailableResources(DiscreteResourceId parent);
Sho SHIMIZUe7f4f3f2015-10-13 16:27:25 -0700164
165 /**
Sho SHIMIZU7332fe42016-02-15 14:58:33 -0800166 * 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 Yutadff91af2016-01-21 16:34:45 -0800187 * Returns resources registered under the specified resource.
188 *
Sho SHIMIZUdd3750c2016-02-01 11:37:04 -0800189 * @param parent parent resource ID
HIGUCHI Yutadff91af2016-01-21 16:34:45 -0800190 * @return registered resources under the specified resource
191 */
Sho SHIMIZUdd3750c2016-02-01 11:37:04 -0800192 Set<Resource> getRegisteredResources(DiscreteResourceId parent);
HIGUCHI Yutadff91af2016-01-21 16:34:45 -0800193
HIGUCHI Yutadff91af2016-01-21 16:34:45 -0800194 /**
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -0700195 * Returns the availability of the specified resource.
Sho SHIMIZUabd849c2015-07-14 09:14:12 -0700196 *
197 * @param resource resource to check the availability
Sho SHIMIZUabd849c2015-07-14 09:14:12 -0700198 * @return true if available, otherwise false
199 */
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -0800200 boolean isAvailable(Resource resource);
Sho SHIMIZUabd849c2015-07-14 09:14:12 -0700201
202 // TODO: listener and event mechanism need to be considered
203}