blob: 749d87e7d523005a34d33a1729e57a71627677a1 [file] [log] [blame]
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -07001/*
Sho SHIMIZU7e6d18e2016-01-07 18:44:33 -08002 * Copyright 2015-2016 Open Networking Laboratory
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -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 */
16package org.onosproject.net.newresource;
17
18import com.google.common.annotations.Beta;
Sho SHIMIZUf95b96e2016-01-25 19:35:15 -080019import com.google.common.collect.ImmutableList;
Sho SHIMIZUb1f16252015-11-25 23:03:16 -080020import org.onosproject.net.DeviceId;
21import org.onosproject.net.PortNumber;
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -070022
Sho SHIMIZUf95b96e2016-01-25 19:35:15 -080023import java.util.Arrays;
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -070024import java.util.List;
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -070025import java.util.Optional;
26
Sho SHIMIZU60ac58e2015-11-11 12:16:38 -080027import static com.google.common.base.Preconditions.checkArgument;
Sho SHIMIZUf95b96e2016-01-25 19:35:15 -080028import static com.google.common.base.Preconditions.checkNotNull;
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -070029
30/**
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -080031 * An object that represent a resource in a network.
32 * A Resource can represents path-like hierarchical structure with its ID. An ID of resource is
33 * composed of a sequence of elementary resources that are not globally identifiable. A Resource
34 * can be globally identifiable by its ID.
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -070035 *
Sho SHIMIZU60ac58e2015-11-11 12:16:38 -080036 * Two types of resource are considered. One is discrete type and the other is continuous type.
37 * Discrete type resource is a resource whose amount is measured as a discrete unit. VLAN ID and
38 * MPLS label are examples of discrete type resource. Continuous type resource is a resource whose
39 * amount is measured as a continuous value. Bandwidth is an example of continuous type resource.
40 * A double value is associated with a continuous type value.
41 *
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -070042 * Users of this class must keep the semantics of resources regarding the hierarchical structure.
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -080043 * For example, resource, Device:1/Port:1/VLAN ID:100, is valid, but resource,
Sho SHIMIZUb1f16252015-11-25 23:03:16 -080044 * VLAN ID:100/Device:1/Port:1 is not valid because a link is not a sub-component of a VLAN ID.
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -070045 */
46@Beta
Sho SHIMIZU2a704512016-01-26 14:41:34 -080047public interface Resource {
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -070048
Sho SHIMIZU2a704512016-01-26 14:41:34 -080049 DiscreteResource ROOT = new DiscreteResource();
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -070050
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -070051 /**
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -070052 * Returns the components of this resource path.
53 *
54 * @return the components of this resource path
55 */
Sho SHIMIZU2a704512016-01-26 14:41:34 -080056 List<Object> components();
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -070057
58 /**
Sho SHIMIZU2d310222016-01-22 11:45:11 -080059 * Returns the volume of this resource.
60 *
Sho SHIMIZUf0fb3612016-01-27 11:45:09 -080061 * @param <T> type of return value
Sho SHIMIZU2d310222016-01-22 11:45:11 -080062 * @return the volume of this resource
63 */
64 // TODO: think about other naming possibilities. amount? quantity?
Sho SHIMIZU2a704512016-01-26 14:41:34 -080065 <T> T volume();
Sho SHIMIZU2d310222016-01-22 11:45:11 -080066
67 /**
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -070068 * Returns the parent resource path of this instance.
69 * E.g. if this path is Link:1/VLAN ID:100, the return value is the resource path for Link:1.
70 *
71 * @return the parent resource path of this instance.
72 * If there is no parent, empty instance will be returned.
73 */
Sho SHIMIZU2a704512016-01-26 14:41:34 -080074 Optional<DiscreteResource> parent();
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -070075
Sho SHIMIZU9e1e9de2015-11-25 15:48:48 -080076 /**
77 * Returns a child resource path of this instance with specifying the child object.
78 * The child resource path is discrete-type.
79 *
80 * @param child child object
81 * @return a child resource path
82 */
Sho SHIMIZU2a704512016-01-26 14:41:34 -080083 DiscreteResource child(Object child);
Sho SHIMIZU60ac58e2015-11-11 12:16:38 -080084
Sho SHIMIZU9e1e9de2015-11-25 15:48:48 -080085 /**
86 * Returns a child resource path of this instance with specifying a child object and
87 * value. The child resource path is continuous-type.
88 *
89 * @param child child object
90 * @param value value
91 * @return a child resource path
92 */
Sho SHIMIZU2a704512016-01-26 14:41:34 -080093 ContinuousResource child(Class<?> child, double value);
Sho SHIMIZU01120782015-08-21 15:48:43 -070094
95 /**
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -070096 * Returns the last component of this instance.
97 *
98 * @return the last component of this instance.
99 * The return value is equal to the last object of {@code components()}.
100 */
Sho SHIMIZU2a704512016-01-26 14:41:34 -0800101 Object last();
Sho SHIMIZU7e6d18e2016-01-07 18:44:33 -0800102
103 /**
Sho SHIMIZU5fb1ea32016-01-14 10:58:14 -0800104 * Returns the ID of this resource path.
Sho SHIMIZU7e6d18e2016-01-07 18:44:33 -0800105 *
Sho SHIMIZU5fb1ea32016-01-14 10:58:14 -0800106 * @return the ID of this resource path
Sho SHIMIZU7e6d18e2016-01-07 18:44:33 -0800107 */
Sho SHIMIZU2a704512016-01-26 14:41:34 -0800108 ResourceId id();
Sho SHIMIZUf95b96e2016-01-25 19:35:15 -0800109
110 /**
111 * Create a factory for discrete-type with the specified resource ID.
112 *
113 * @param id resource ID
114 * @return {@link DiscreteFactory}
115 */
116 static DiscreteFactory discrete(DiscreteResourceId id) {
117 checkNotNull(id);
118
119 return new DiscreteFactory(id);
120 }
121
122 /**
123 * Creates a factory for discrete-type with the specified parent ID and child.
124 *
125 * @param parent ID of the parent
126 * @param child child
127 * @return {@link DiscreteFactory}
128 */
129 static DiscreteFactory discrete(DiscreteResourceId parent, Object child) {
130 checkNotNull(parent);
131 checkNotNull(child);
132 checkArgument(!(child instanceof Class<?>));
133
134 return new DiscreteFactory(new DiscreteResourceId(ImmutableList.builder()
135 .addAll(parent.components())
136 .add(child)
137 .build()));
138 }
139
140 /**
141 * Create a factory for discrete-type with the specified device ID.
142 *
143 * @param device device ID
144 * @return {@link DiscreteFactory}
145 */
146 static DiscreteFactory discrete(DeviceId device) {
147 checkNotNull(device);
148
149 return new DiscreteFactory(new DiscreteResourceId(ImmutableList.of(device)));
150 }
151
152 /**
153 * Create a factory for discrete-type with the specified device ID and components.
154 *
155 * @param device device ID
156 * @param components resource ID components other than the device ID
157 * @return {@link DiscreteFactory}
158 */
159 static DiscreteFactory discrete(DeviceId device, Object... components) {
160 checkNotNull(device);
161 checkNotNull(components);
162
163 return new DiscreteFactory(new DiscreteResourceId(ImmutableList.builder()
164 .add(device)
165 .add(components)
166 .build()));
167 }
168
169 /**
170 * Create a factory for discrete-type with the specified device ID, port number and components.
171 *
172 * @param device device ID
173 * @param port port number
174 * @param components resource ID components other than the device ID and port number
175 * @return {@link DiscreteFactory}
176 */
177 static DiscreteFactory discrete(DeviceId device, PortNumber port, Object... components) {
178 checkNotNull(device);
179 checkNotNull(port);
180 checkNotNull(components);
181
182 return new DiscreteFactory(new DiscreteResourceId(ImmutableList.builder()
183 .add(device)
184 .add(port)
185 .add(components)
186 .build()));
187 }
188
189 /**
190 * Create a factory for continuous-type with the specified resource ID.
191 *
192 * @param id resource ID
193 * @return {@link ContinuousFactory}
194 */
195 static ContinuousFactory continuous(ContinuousResourceId id) {
196 checkNotNull(id);
197
198 return new ContinuousFactory(id);
199 }
200
201 /**
202 * Creates a factory for continuous-type wit the specified parent ID and child.
203 *
204 * @param parent ID of the parent
205 * @param child child
206 * @return {@link ContinuousFactory}
207 */
208 static ContinuousFactory continuous(DiscreteResourceId parent, Class<?> child) {
209 checkNotNull(parent);
210 checkNotNull(child);
211
212 return new ContinuousFactory(new ContinuousResourceId(ImmutableList.builder()
213 .addAll(parent.components()), child));
214 }
215
216 /**
217 * Create a factory for continuous-type with the specified device ID and type.
218 *
219 * @param device device ID
220 * @param cls type of resource the returned factory will create
221 * @return {@link ContinuousFactory}
222 */
223 static ContinuousFactory continuous(DeviceId device, Class<?> cls) {
224 checkNotNull(device);
225 checkNotNull(cls);
226
227 return new ContinuousFactory(new ContinuousResourceId(ImmutableList.builder().add(device), cls));
228 }
229
230 /**
231 * Create a factory for continuous-type with the specified device ID and components.
232 * The last element of the components must be a {@link Class} instance. Otherwise,
233 * an {@link IllegalArgumentException} is thrown.
234 *
235 * @param device device ID
236 * @param components resource ID components other than the device ID.
237 * @return {@link ContinuousFactory}
238 */
239 static ContinuousFactory continuous(DeviceId device, Object... components) {
240 checkNotNull(device);
241 checkNotNull(components);
242 checkArgument(components.length > 1);
243
244 Object last = components[components.length - 1];
245 checkArgument(last instanceof Class<?>);
246
247 return new ContinuousFactory(new ContinuousResourceId(ImmutableList.builder()
248 .add(device)
249 .add(Arrays.copyOfRange(components, 0, components.length - 1)), (Class<?>) last));
250 }
251
252 /**
253 * Create a factory for continuous-type with the specified device ID, port number and type.
254 *
255 * @param device device ID
256 * @param port port number
257 * @param cls type of resource the returned factory will create
258 * @return {@link ContinuousFactory}
259 */
260 static ContinuousFactory continuous(DeviceId device, PortNumber port, Class<?> cls) {
261 checkNotNull(device);
262 checkNotNull(port);
263 checkNotNull(cls);
264
265 return new ContinuousFactory(new ContinuousResourceId(ImmutableList.builder()
266 .add(device)
267 .add(port), cls));
268 }
269
270 /**
271 * Create a factory for continuous-type with the specified device ID and components.
272 * The last element of the components must be a {@link Class} instance. Otherwise,
273 * an {@link IllegalArgumentException} is thrown.
274 *
275 * @param device device ID
276 * @param port port number
277 * @param components resource ID components other than the device ID and port number.
278 * @return {@link ContinuousFactory}
279 */
280 static ContinuousFactory continuous(DeviceId device, PortNumber port, Object... components) {
281 checkNotNull(device);
282 checkNotNull(port);
283 checkNotNull(components);
284 checkArgument(components.length > 1);
285
286 Object last = components[components.length - 1];
287 checkArgument(last instanceof Class<?>);
288
289 return new ContinuousFactory(new ContinuousResourceId(ImmutableList.builder()
290 .add(device)
291 .add(port)
292 .add(Arrays.copyOfRange(components, 0, components.length - 1)), (Class<?>) last));
293 }
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -0700294}