blob: 3076ec93d0fefc207ac4fdd34f85040a8348af12 [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;
26
Sho SHIMIZU726a3582015-08-17 11:46:57 -070027import static com.google.common.base.Preconditions.checkNotNull;
28
Sho SHIMIZUabd849c2015-07-14 09:14:12 -070029/**
30 * Service for allocating/releasing resource(s) and retrieving allocation(s) and availability.
31 */
32@Beta
Sho SHIMIZUfa62b472015-11-02 17:35:46 -080033public interface ResourceService extends ListenerService<ResourceEvent, ResourceListener> {
Sho SHIMIZUabd849c2015-07-14 09:14:12 -070034 /**
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 SHIMIZUabd849c2015-07-14 09:14:12 -070039 * @return allocation information enclosed by Optional. If the allocation fails, the return value is empty
40 */
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -080041 default Optional<ResourceAllocation> allocate(ResourceConsumer consumer, Resource resource) {
Sho SHIMIZU726a3582015-08-17 11:46:57 -070042 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 SHIMIZUabd849c2015-07-14 09:14:12 -070059
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 SHIMIZU8fa670a2016-01-14 11:17:18 -080068 List<ResourceAllocation> allocate(ResourceConsumer consumer, List<Resource> resources);
Sho SHIMIZUabd849c2015-07-14 09:14:12 -070069
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 SHIMIZU8fa670a2016-01-14 11:17:18 -080078 default List<ResourceAllocation> allocate(ResourceConsumer consumer, Resource... resources) {
Sho SHIMIZU726a3582015-08-17 11:46:57 -070079 checkNotNull(consumer);
80 checkNotNull(resources);
81
82 return allocate(consumer, Arrays.asList(resources));
83 }
Sho SHIMIZUabd849c2015-07-14 09:14:12 -070084
85 /**
86 * Releases the specified resource allocation.
87 *
88 * @param allocation resource allocation to be released
Sho SHIMIZUabd849c2015-07-14 09:14:12 -070089 * @return true if succeeded, otherwise false
90 */
Sho SHIMIZU726a3582015-08-17 11:46:57 -070091 default boolean release(ResourceAllocation allocation) {
92 checkNotNull(allocation);
93
94 return release(ImmutableList.of(allocation));
95 }
Sho SHIMIZUabd849c2015-07-14 09:14:12 -070096
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 SHIMIZU1f5e5912015-08-10 17:00:00 -0700104 boolean release(List<ResourceAllocation> allocations);
Sho SHIMIZUabd849c2015-07-14 09:14:12 -0700105
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 SHIMIZU726a3582015-08-17 11:46:57 -0700113 default boolean release(ResourceAllocation... allocations) {
114 checkNotNull(allocations);
115
116 return release(ImmutableList.copyOf(allocations));
117 }
Sho SHIMIZUabd849c2015-07-14 09:14:12 -0700118
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 SHIMIZU6c9e33a2016-01-07 18:45:27 -0800129 * Returns resource allocations of the specified resource.
Sho SHIMIZUf853b0e2015-09-29 15:15:32 -0700130 *
131 * @param resource resource to check the allocation
Sho SHIMIZU6c9e33a2016-01-07 18:45:27 -0800132 * @return list of allocation information.
133 * If the resource is not allocated, the return value is an empty list.
Sho SHIMIZUf853b0e2015-09-29 15:15:32 -0700134 */
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -0800135 List<ResourceAllocation> getResourceAllocation(Resource resource);
Sho SHIMIZUf853b0e2015-09-29 15:15:32 -0700136
137 /**
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -0700138 * Returns allocated resources being as children of the specified parent and being the specified resource type.
Sho SHIMIZUabd849c2015-07-14 09:14:12 -0700139 *
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -0800140 * @param parent parent resource
Sho SHIMIZUabd849c2015-07-14 09:14:12 -0700141 * @param cls class to specify a type of resource
Sho SHIMIZUabd849c2015-07-14 09:14:12 -0700142 * @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 SHIMIZU8fa670a2016-01-14 11:17:18 -0800146 <T> Collection<ResourceAllocation> getResourceAllocations(Resource parent, Class<T> cls);
Sho SHIMIZUabd849c2015-07-14 09:14:12 -0700147
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 SHIMIZU1f5e5912015-08-10 17:00:00 -0700154 Collection<ResourceAllocation> getResourceAllocations(ResourceConsumer consumer);
Sho SHIMIZUabd849c2015-07-14 09:14:12 -0700155
156 /**
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -0800157 * Returns resources that point available child resources under the specified resource.
Sho SHIMIZUe7f4f3f2015-10-13 16:27:25 -0700158 *
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -0800159 * @param parent parent resource
160 * @return available resources under the specified resource
Sho SHIMIZUe7f4f3f2015-10-13 16:27:25 -0700161 */
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -0800162 Collection<Resource> getAvailableResources(Resource parent);
Sho SHIMIZUe7f4f3f2015-10-13 16:27:25 -0700163
164 /**
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -0700165 * Returns the availability of the specified resource.
Sho SHIMIZUabd849c2015-07-14 09:14:12 -0700166 *
167 * @param resource resource to check the availability
Sho SHIMIZUabd849c2015-07-14 09:14:12 -0700168 * @return true if available, otherwise false
169 */
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -0800170 boolean isAvailable(Resource resource);
Sho SHIMIZUabd849c2015-07-14 09:14:12 -0700171
172 // TODO: listener and event mechanism need to be considered
173}