blob: 6dcd7ef7784b26f1cd53cabf8db20805b004f305 [file] [log] [blame]
Sho SHIMIZUabd849c2015-07-14 09:14:12 -07001/*
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 */
16package org.onosproject.net.newresource;
17
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 */
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -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 *
132 * @param resource 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 SHIMIZU2d310222016-01-22 11:45:11 -0800136 // TODO: need to change the argument type to ResourceId
Sho SHIMIZU4a1e59f2016-01-26 15:27:13 -0800137 List<ResourceAllocation> getResourceAllocations(Resource resource);
Sho SHIMIZUf853b0e2015-09-29 15:15:32 -0700138
139 /**
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -0700140 * Returns allocated resources being as children of the specified parent and being the specified resource type.
Sho SHIMIZUabd849c2015-07-14 09:14:12 -0700141 *
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -0800142 * @param parent parent resource
Sho SHIMIZUabd849c2015-07-14 09:14:12 -0700143 * @param cls class to specify a type of resource
Sho SHIMIZUabd849c2015-07-14 09:14:12 -0700144 * @param <T> type of the resource
145 * @return non-empty collection of resource allocations if resources are allocated with the subject and type,
146 * empty collection if no resource is allocated with the subject and type
147 */
Sho SHIMIZU2d310222016-01-22 11:45:11 -0800148 // TODO: might need to change the first argument type to ResourceId or ResourceId.Discrete
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -0800149 <T> Collection<ResourceAllocation> getResourceAllocations(Resource parent, Class<T> cls);
Sho SHIMIZUabd849c2015-07-14 09:14:12 -0700150
151 /**
152 * Returns resources allocated to the specified consumer.
153 *
154 * @param consumer consumer whose allocated resources are to be returned
155 * @return resources allocated to the consumer
156 */
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -0700157 Collection<ResourceAllocation> getResourceAllocations(ResourceConsumer consumer);
Sho SHIMIZUabd849c2015-07-14 09:14:12 -0700158
159 /**
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -0800160 * Returns resources that point available child resources under the specified resource.
Sho SHIMIZUe7f4f3f2015-10-13 16:27:25 -0700161 *
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -0800162 * @param parent parent resource
163 * @return available resources under the specified resource
Sho SHIMIZUe7f4f3f2015-10-13 16:27:25 -0700164 */
Sho SHIMIZU2d310222016-01-22 11:45:11 -0800165 // TODO: need to change the argument type to ResourceId or ResourceId.Discrete
Sho SHIMIZU83258ae2016-01-29 17:39:07 -0800166 Set<Resource> getAvailableResources(Resource parent);
Sho SHIMIZUe7f4f3f2015-10-13 16:27:25 -0700167
168 /**
HIGUCHI Yutadff91af2016-01-21 16:34:45 -0800169 * Returns resources registered under the specified resource.
170 *
171 * @param parent parent resource
172 * @return registered resources under the specified resource
173 */
Sho SHIMIZU2d310222016-01-22 11:45:11 -0800174 // TODO: need to change the argument type to ResourceId or ResourceId.Discrete
Sho SHIMIZU83258ae2016-01-29 17:39:07 -0800175 Set<Resource> getRegisteredResources(Resource parent);
HIGUCHI Yutadff91af2016-01-21 16:34:45 -0800176
177
178 /**
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -0700179 * Returns the availability of the specified resource.
Sho SHIMIZUabd849c2015-07-14 09:14:12 -0700180 *
181 * @param resource resource to check the availability
Sho SHIMIZUabd849c2015-07-14 09:14:12 -0700182 * @return true if available, otherwise false
183 */
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -0800184 boolean isAvailable(Resource resource);
Sho SHIMIZUabd849c2015-07-14 09:14:12 -0700185
186 // TODO: listener and event mechanism need to be considered
187}