blob: 7a752967402eae5e14afadbcbe81830d1898f667 [file] [log] [blame]
Sho SHIMIZUabd849c2015-07-14 09:14:12 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present 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.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
Sho SHIMIZUa6b4dc72016-03-11 19:00:20 -080032public interface ResourceService extends ResourceQueryService, ListenerService<ResourceEvent, ResourceListener> {
Sho SHIMIZUabd849c2015-07-14 09:14:12 -070033 /**
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 SHIMIZU8fa670a2016-01-14 11:17:18 -080040 default Optional<ResourceAllocation> allocate(ResourceConsumer consumer, Resource resource) {
Sho SHIMIZU726a3582015-08-17 11:46:57 -070041 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 *
Sho SHIMIZUa6b4dc72016-03-11 19:00:20 -080063 * @param consumer resource user which the resources are allocated to
Sho SHIMIZUabd849c2015-07-14 09:14:12 -070064 * @param resources resources to be allocated
65 * @return non-empty list of allocation information if succeeded, otherwise empty list
66 */
Sho SHIMIZUef835c92016-08-08 13:51:17 -070067 List<ResourceAllocation> allocate(ResourceConsumer consumer, List<? extends Resource> 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 *
Sho SHIMIZUa6b4dc72016-03-11 19:00:20 -080073 * @param consumer resource user which the resources are allocated to
Sho SHIMIZUabd849c2015-07-14 09:14:12 -070074 * @param resources resources to be allocated
75 * @return non-empty list of allocation information if succeeded, otherwise empty list
76 */
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -080077 default List<ResourceAllocation> allocate(ResourceConsumer consumer, Resource... resources) {
Sho SHIMIZU726a3582015-08-17 11:46:57 -070078 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
Sho SHIMIZUabd849c2015-07-14 09:14:12 -0700127 // TODO: listener and event mechanism need to be considered
128}