Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 1 | /* |
Sho SHIMIZU | 7e6d18e | 2016-01-07 18:44:33 -0800 | [diff] [blame] | 2 | * Copyright 2015-2016 Open Networking Laboratory |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 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 | */ |
| 16 | package org.onosproject.net.newresource; |
| 17 | |
| 18 | import com.google.common.annotations.Beta; |
Sho SHIMIZU | f95b96e | 2016-01-25 19:35:15 -0800 | [diff] [blame^] | 19 | import com.google.common.collect.ImmutableList; |
Sho SHIMIZU | b1f1625 | 2015-11-25 23:03:16 -0800 | [diff] [blame] | 20 | import org.onosproject.net.DeviceId; |
| 21 | import org.onosproject.net.PortNumber; |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 22 | |
Sho SHIMIZU | f95b96e | 2016-01-25 19:35:15 -0800 | [diff] [blame^] | 23 | import java.util.Arrays; |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 24 | import java.util.List; |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 25 | import java.util.Optional; |
| 26 | |
Sho SHIMIZU | 60ac58e | 2015-11-11 12:16:38 -0800 | [diff] [blame] | 27 | import static com.google.common.base.Preconditions.checkArgument; |
Sho SHIMIZU | f95b96e | 2016-01-25 19:35:15 -0800 | [diff] [blame^] | 28 | import static com.google.common.base.Preconditions.checkNotNull; |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 29 | |
| 30 | /** |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 31 | * 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 SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 35 | * |
Sho SHIMIZU | 60ac58e | 2015-11-11 12:16:38 -0800 | [diff] [blame] | 36 | * 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 SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 42 | * Users of this class must keep the semantics of resources regarding the hierarchical structure. |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 43 | * For example, resource, Device:1/Port:1/VLAN ID:100, is valid, but resource, |
Sho SHIMIZU | b1f1625 | 2015-11-25 23:03:16 -0800 | [diff] [blame] | 44 | * VLAN ID:100/Device:1/Port:1 is not valid because a link is not a sub-component of a VLAN ID. |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 45 | */ |
| 46 | @Beta |
Sho SHIMIZU | 2a70451 | 2016-01-26 14:41:34 -0800 | [diff] [blame] | 47 | public interface Resource { |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 48 | |
Sho SHIMIZU | 2a70451 | 2016-01-26 14:41:34 -0800 | [diff] [blame] | 49 | DiscreteResource ROOT = new DiscreteResource(); |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 50 | |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 51 | /** |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 52 | * Returns the components of this resource path. |
| 53 | * |
| 54 | * @return the components of this resource path |
| 55 | */ |
Sho SHIMIZU | 2a70451 | 2016-01-26 14:41:34 -0800 | [diff] [blame] | 56 | List<Object> components(); |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 57 | |
| 58 | /** |
Sho SHIMIZU | 2d31022 | 2016-01-22 11:45:11 -0800 | [diff] [blame] | 59 | * Returns the volume of this resource. |
| 60 | * |
Sho SHIMIZU | f0fb361 | 2016-01-27 11:45:09 -0800 | [diff] [blame] | 61 | * @param <T> type of return value |
Sho SHIMIZU | 2d31022 | 2016-01-22 11:45:11 -0800 | [diff] [blame] | 62 | * @return the volume of this resource |
| 63 | */ |
| 64 | // TODO: think about other naming possibilities. amount? quantity? |
Sho SHIMIZU | 2a70451 | 2016-01-26 14:41:34 -0800 | [diff] [blame] | 65 | <T> T volume(); |
Sho SHIMIZU | 2d31022 | 2016-01-22 11:45:11 -0800 | [diff] [blame] | 66 | |
| 67 | /** |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 68 | * 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 SHIMIZU | 2a70451 | 2016-01-26 14:41:34 -0800 | [diff] [blame] | 74 | Optional<DiscreteResource> parent(); |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 75 | |
Sho SHIMIZU | 9e1e9de | 2015-11-25 15:48:48 -0800 | [diff] [blame] | 76 | /** |
| 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 SHIMIZU | 2a70451 | 2016-01-26 14:41:34 -0800 | [diff] [blame] | 83 | DiscreteResource child(Object child); |
Sho SHIMIZU | 60ac58e | 2015-11-11 12:16:38 -0800 | [diff] [blame] | 84 | |
Sho SHIMIZU | 9e1e9de | 2015-11-25 15:48:48 -0800 | [diff] [blame] | 85 | /** |
| 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 SHIMIZU | 2a70451 | 2016-01-26 14:41:34 -0800 | [diff] [blame] | 93 | ContinuousResource child(Class<?> child, double value); |
Sho SHIMIZU | 0112078 | 2015-08-21 15:48:43 -0700 | [diff] [blame] | 94 | |
| 95 | /** |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 96 | * 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 SHIMIZU | 2a70451 | 2016-01-26 14:41:34 -0800 | [diff] [blame] | 101 | Object last(); |
Sho SHIMIZU | 7e6d18e | 2016-01-07 18:44:33 -0800 | [diff] [blame] | 102 | |
| 103 | /** |
Sho SHIMIZU | 5fb1ea3 | 2016-01-14 10:58:14 -0800 | [diff] [blame] | 104 | * Returns the ID of this resource path. |
Sho SHIMIZU | 7e6d18e | 2016-01-07 18:44:33 -0800 | [diff] [blame] | 105 | * |
Sho SHIMIZU | 5fb1ea3 | 2016-01-14 10:58:14 -0800 | [diff] [blame] | 106 | * @return the ID of this resource path |
Sho SHIMIZU | 7e6d18e | 2016-01-07 18:44:33 -0800 | [diff] [blame] | 107 | */ |
Sho SHIMIZU | 2a70451 | 2016-01-26 14:41:34 -0800 | [diff] [blame] | 108 | ResourceId id(); |
Sho SHIMIZU | f95b96e | 2016-01-25 19:35:15 -0800 | [diff] [blame^] | 109 | |
| 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 SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 294 | } |