blob: ad684c8c314026e2ca7e1537e19d9aed2ba064a8 [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 SHIMIZUabd849c2015-07-14 09:14:12 -070020
Sho SHIMIZU726a3582015-08-17 11:46:57 -070021import java.util.Arrays;
Sho SHIMIZUabd849c2015-07-14 09:14:12 -070022import java.util.Collection;
23import java.util.List;
24import java.util.Optional;
25
Sho SHIMIZU726a3582015-08-17 11:46:57 -070026import static com.google.common.base.Preconditions.checkNotNull;
27
Sho SHIMIZUabd849c2015-07-14 09:14:12 -070028/**
29 * Service for allocating/releasing resource(s) and retrieving allocation(s) and availability.
30 */
31@Beta
32public interface ResourceService {
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 SHIMIZUabd849c2015-07-14 09:14:12 -070038 * @return allocation information enclosed by Optional. If the allocation fails, the return value is empty
39 */
Sho SHIMIZU726a3582015-08-17 11:46:57 -070040 default Optional<ResourceAllocation> allocate(ResourceConsumer consumer, ResourcePath resource) {
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 SHIMIZUabd849c2015-07-14 09:14:12 -070058
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 *
63 * @param consumer resource user which the resources are allocated to
64 * @param resources resources to be allocated
65 * @return non-empty list of allocation information if succeeded, otherwise empty list
66 */
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -070067 List<ResourceAllocation> allocate(ResourceConsumer consumer, List<ResourcePath> resources);
Sho SHIMIZUabd849c2015-07-14 09:14:12 -070068
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 *
73 * @param consumer resource user which the resources are allocated to
74 * @param resources resources to be allocated
75 * @return non-empty list of allocation information if succeeded, otherwise empty list
76 */
Sho SHIMIZU726a3582015-08-17 11:46:57 -070077 default List<ResourceAllocation> allocate(ResourceConsumer consumer, ResourcePath... resources) {
78 checkNotNull(consumer);
79 checkNotNull(resources);
80
81 return allocate(consumer, Arrays.asList(resources));
82 }
Sho SHIMIZUabd849c2015-07-14 09:14:12 -070083
84 /**
85 * Releases the specified resource allocation.
86 *
87 * @param allocation resource allocation to be released
Sho SHIMIZUabd849c2015-07-14 09:14:12 -070088 * @return true if succeeded, otherwise false
89 */
Sho SHIMIZU726a3582015-08-17 11:46:57 -070090 default boolean release(ResourceAllocation allocation) {
91 checkNotNull(allocation);
92
93 return release(ImmutableList.of(allocation));
94 }
Sho SHIMIZUabd849c2015-07-14 09:14:12 -070095
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 SHIMIZU1f5e5912015-08-10 17:00:00 -0700103 boolean release(List<ResourceAllocation> allocations);
Sho SHIMIZUabd849c2015-07-14 09:14:12 -0700104
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 SHIMIZU726a3582015-08-17 11:46:57 -0700112 default boolean release(ResourceAllocation... allocations) {
113 checkNotNull(allocations);
114
115 return release(ImmutableList.copyOf(allocations));
116 }
Sho SHIMIZUabd849c2015-07-14 09:14:12 -0700117
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
127 /**
Sho SHIMIZUf853b0e2015-09-29 15:15:32 -0700128 * Returns resource allocation of the specified resource.
129 *
130 * @param resource resource to check the allocation
131 * @return allocation information enclosed by Optional.
132 * If the resource is not allocated, the return value is empty.
133 */
134 Optional<ResourceAllocation> getResourceAllocation(ResourcePath resource);
135
136 /**
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -0700137 * Returns allocated resources being as children of the specified parent and being the specified resource type.
Sho SHIMIZUabd849c2015-07-14 09:14:12 -0700138 *
Madan Jampanif97edc12015-08-31 14:41:01 -0700139 * @param parent parent resource path
Sho SHIMIZUabd849c2015-07-14 09:14:12 -0700140 * @param cls class to specify a type of resource
Sho SHIMIZUabd849c2015-07-14 09:14:12 -0700141 * @param <T> type of the resource
142 * @return non-empty collection of resource allocations if resources are allocated with the subject and type,
143 * empty collection if no resource is allocated with the subject and type
144 */
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -0700145 <T> Collection<ResourceAllocation> getResourceAllocations(ResourcePath parent, Class<T> cls);
Sho SHIMIZUabd849c2015-07-14 09:14:12 -0700146
147 /**
148 * Returns resources allocated to the specified consumer.
149 *
150 * @param consumer consumer whose allocated resources are to be returned
151 * @return resources allocated to the consumer
152 */
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -0700153 Collection<ResourceAllocation> getResourceAllocations(ResourceConsumer consumer);
Sho SHIMIZUabd849c2015-07-14 09:14:12 -0700154
155 /**
Sho SHIMIZUe7f4f3f2015-10-13 16:27:25 -0700156 * Returns resource paths that point available child resources under the specified resource path.
157 *
158 * @param parent parent resource path
159 * @return available resource paths under the specified resource path
160 */
161 Collection<ResourcePath> getAvailableResources(ResourcePath parent);
162
163 /**
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -0700164 * Returns the availability of the specified resource.
Sho SHIMIZUabd849c2015-07-14 09:14:12 -0700165 *
166 * @param resource resource to check the availability
Sho SHIMIZUabd849c2015-07-14 09:14:12 -0700167 * @return true if available, otherwise false
168 */
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -0700169 boolean isAvailable(ResourcePath resource);
Sho SHIMIZUabd849c2015-07-14 09:14:12 -0700170
171 // TODO: listener and event mechanism need to be considered
172}