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 | b1f1625 | 2015-11-25 23:03:16 -0800 | [diff] [blame] | 19 | import org.onosproject.net.DeviceId; |
| 20 | import org.onosproject.net.PortNumber; |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 21 | |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 22 | import java.util.List; |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 23 | import java.util.Optional; |
| 24 | |
Sho SHIMIZU | 60ac58e | 2015-11-11 12:16:38 -0800 | [diff] [blame] | 25 | import static com.google.common.base.Preconditions.checkArgument; |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 26 | |
| 27 | /** |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 28 | * An object that represent a resource in a network. |
| 29 | * A Resource can represents path-like hierarchical structure with its ID. An ID of resource is |
| 30 | * composed of a sequence of elementary resources that are not globally identifiable. A Resource |
| 31 | * can be globally identifiable by its ID. |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 32 | * |
Sho SHIMIZU | 60ac58e | 2015-11-11 12:16:38 -0800 | [diff] [blame] | 33 | * Two types of resource are considered. One is discrete type and the other is continuous type. |
| 34 | * Discrete type resource is a resource whose amount is measured as a discrete unit. VLAN ID and |
| 35 | * MPLS label are examples of discrete type resource. Continuous type resource is a resource whose |
| 36 | * amount is measured as a continuous value. Bandwidth is an example of continuous type resource. |
| 37 | * A double value is associated with a continuous type value. |
| 38 | * |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 39 | * 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] | 40 | * 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] | 41 | * 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] | 42 | */ |
| 43 | @Beta |
Sho SHIMIZU | 2a70451 | 2016-01-26 14:41:34 -0800 | [diff] [blame] | 44 | public interface Resource { |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 45 | |
Sho SHIMIZU | 2a70451 | 2016-01-26 14:41:34 -0800 | [diff] [blame] | 46 | DiscreteResource ROOT = new DiscreteResource(); |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 47 | |
Sho SHIMIZU | 2a70451 | 2016-01-26 14:41:34 -0800 | [diff] [blame] | 48 | static DiscreteResource discrete(DeviceId device) { |
Sho SHIMIZU | f33b893 | 2016-01-25 18:43:32 -0800 | [diff] [blame] | 49 | return new DiscreteResource(ResourceId.discrete(device)); |
Sho SHIMIZU | b1f1625 | 2015-11-25 23:03:16 -0800 | [diff] [blame] | 50 | } |
| 51 | |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 52 | /** |
Sho SHIMIZU | 60ac58e | 2015-11-11 12:16:38 -0800 | [diff] [blame] | 53 | * Creates an resource path which represents a discrete-type resource from the specified components. |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 54 | * |
Sho SHIMIZU | b1f1625 | 2015-11-25 23:03:16 -0800 | [diff] [blame] | 55 | * @param device device ID which is the first component of the path |
| 56 | * @param components following components of the path. The order represents hierarchical structure of the resource. |
Sho SHIMIZU | e552456 | 2015-11-24 14:41:20 -0800 | [diff] [blame] | 57 | * @return resource path instance |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 58 | */ |
Sho SHIMIZU | 2a70451 | 2016-01-26 14:41:34 -0800 | [diff] [blame] | 59 | static DiscreteResource discrete(DeviceId device, Object... components) { |
Sho SHIMIZU | f33b893 | 2016-01-25 18:43:32 -0800 | [diff] [blame] | 60 | return new DiscreteResource(ResourceId.discrete(device, components)); |
Sho SHIMIZU | b1f1625 | 2015-11-25 23:03:16 -0800 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Creates an resource path which represents a discrete-type resource from the specified components. |
| 65 | * |
| 66 | * @param device device ID which is the first component of the path |
| 67 | * @param port port number which is the second component of the path |
| 68 | * @param components following components of the path. The order represents hierarchical structure of the resource. |
| 69 | * @return resource path instance |
| 70 | */ |
Sho SHIMIZU | 2a70451 | 2016-01-26 14:41:34 -0800 | [diff] [blame] | 71 | static DiscreteResource discrete(DeviceId device, PortNumber port, Object... components) { |
Sho SHIMIZU | f33b893 | 2016-01-25 18:43:32 -0800 | [diff] [blame] | 72 | return new DiscreteResource(ResourceId.discrete(device, port, components)); |
Sho SHIMIZU | 60ac58e | 2015-11-11 12:16:38 -0800 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Creates an resource path which represents a continuous-type resource from the specified components. |
| 77 | * |
| 78 | * @param value amount of the resource |
Sho SHIMIZU | b1f1625 | 2015-11-25 23:03:16 -0800 | [diff] [blame] | 79 | * @param device device ID which is the first component of the path |
| 80 | * @param components following components of the path. The order represents hierarchical structure of the resource. |
Sho SHIMIZU | 2d31022 | 2016-01-22 11:45:11 -0800 | [diff] [blame] | 81 | * The last element of this list must be an {@link Class} instance. Otherwise, this method throws |
| 82 | * an IllegalArgumentException. |
Sho SHIMIZU | e552456 | 2015-11-24 14:41:20 -0800 | [diff] [blame] | 83 | * @return resource path instance |
Sho SHIMIZU | 60ac58e | 2015-11-11 12:16:38 -0800 | [diff] [blame] | 84 | */ |
Sho SHIMIZU | 2a70451 | 2016-01-26 14:41:34 -0800 | [diff] [blame] | 85 | static ContinuousResource continuous(double value, DeviceId device, Object... components) { |
Sho SHIMIZU | b1f1625 | 2015-11-25 23:03:16 -0800 | [diff] [blame] | 86 | checkArgument(components.length > 0, |
| 87 | "Length of components must be greater thant 0, but " + components.length); |
| 88 | |
Sho SHIMIZU | f33b893 | 2016-01-25 18:43:32 -0800 | [diff] [blame] | 89 | return new ContinuousResource(ResourceId.continuous(device, components), value); |
Sho SHIMIZU | b1f1625 | 2015-11-25 23:03:16 -0800 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Creates an resource path which represents a continuous-type resource from the specified components. |
| 94 | * |
| 95 | * @param value amount of the resource |
| 96 | * @param device device ID which is the first component of the path. |
| 97 | * @param port port number which is the second component of the path. |
| 98 | * @param components following components of the path. The order represents hierarchical structure of the resource. |
Sho SHIMIZU | 2d31022 | 2016-01-22 11:45:11 -0800 | [diff] [blame] | 99 | * The last element of this list must be an {@link Class} instance. Otherwise, this method throws |
| 100 | * an IllegalArgumentException. |
Sho SHIMIZU | b1f1625 | 2015-11-25 23:03:16 -0800 | [diff] [blame] | 101 | * @return resource path instance |
| 102 | */ |
Sho SHIMIZU | 2a70451 | 2016-01-26 14:41:34 -0800 | [diff] [blame] | 103 | static ContinuousResource continuous(double value, DeviceId device, PortNumber port, Object... components) { |
Sho SHIMIZU | f33b893 | 2016-01-25 18:43:32 -0800 | [diff] [blame] | 104 | return new ContinuousResource(ResourceId.continuous(device, port, components), value); |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | /** |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 108 | * Returns the components of this resource path. |
| 109 | * |
| 110 | * @return the components of this resource path |
| 111 | */ |
Sho SHIMIZU | 2a70451 | 2016-01-26 14:41:34 -0800 | [diff] [blame] | 112 | List<Object> components(); |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 113 | |
| 114 | /** |
Sho SHIMIZU | 2d31022 | 2016-01-22 11:45:11 -0800 | [diff] [blame] | 115 | * Returns the volume of this resource. |
| 116 | * |
Sho SHIMIZU | f0fb361 | 2016-01-27 11:45:09 -0800 | [diff] [blame] | 117 | * @param <T> type of return value |
Sho SHIMIZU | 2d31022 | 2016-01-22 11:45:11 -0800 | [diff] [blame] | 118 | * @return the volume of this resource |
| 119 | */ |
| 120 | // TODO: think about other naming possibilities. amount? quantity? |
Sho SHIMIZU | 2a70451 | 2016-01-26 14:41:34 -0800 | [diff] [blame] | 121 | <T> T volume(); |
Sho SHIMIZU | 2d31022 | 2016-01-22 11:45:11 -0800 | [diff] [blame] | 122 | |
| 123 | /** |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 124 | * Returns the parent resource path of this instance. |
| 125 | * E.g. if this path is Link:1/VLAN ID:100, the return value is the resource path for Link:1. |
| 126 | * |
| 127 | * @return the parent resource path of this instance. |
| 128 | * If there is no parent, empty instance will be returned. |
| 129 | */ |
Sho SHIMIZU | 2a70451 | 2016-01-26 14:41:34 -0800 | [diff] [blame] | 130 | Optional<DiscreteResource> parent(); |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 131 | |
Sho SHIMIZU | 9e1e9de | 2015-11-25 15:48:48 -0800 | [diff] [blame] | 132 | /** |
| 133 | * Returns a child resource path of this instance with specifying the child object. |
| 134 | * The child resource path is discrete-type. |
| 135 | * |
| 136 | * @param child child object |
| 137 | * @return a child resource path |
| 138 | */ |
Sho SHIMIZU | 2a70451 | 2016-01-26 14:41:34 -0800 | [diff] [blame] | 139 | DiscreteResource child(Object child); |
Sho SHIMIZU | 60ac58e | 2015-11-11 12:16:38 -0800 | [diff] [blame] | 140 | |
Sho SHIMIZU | 9e1e9de | 2015-11-25 15:48:48 -0800 | [diff] [blame] | 141 | /** |
| 142 | * Returns a child resource path of this instance with specifying a child object and |
| 143 | * value. The child resource path is continuous-type. |
| 144 | * |
| 145 | * @param child child object |
| 146 | * @param value value |
| 147 | * @return a child resource path |
| 148 | */ |
Sho SHIMIZU | 2a70451 | 2016-01-26 14:41:34 -0800 | [diff] [blame] | 149 | ContinuousResource child(Class<?> child, double value); |
Sho SHIMIZU | 0112078 | 2015-08-21 15:48:43 -0700 | [diff] [blame] | 150 | |
| 151 | /** |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 152 | * Returns the last component of this instance. |
| 153 | * |
| 154 | * @return the last component of this instance. |
| 155 | * The return value is equal to the last object of {@code components()}. |
| 156 | */ |
Sho SHIMIZU | 2a70451 | 2016-01-26 14:41:34 -0800 | [diff] [blame] | 157 | Object last(); |
Sho SHIMIZU | 7e6d18e | 2016-01-07 18:44:33 -0800 | [diff] [blame] | 158 | |
| 159 | /** |
Sho SHIMIZU | 5fb1ea3 | 2016-01-14 10:58:14 -0800 | [diff] [blame] | 160 | * Returns the ID of this resource path. |
Sho SHIMIZU | 7e6d18e | 2016-01-07 18:44:33 -0800 | [diff] [blame] | 161 | * |
Sho SHIMIZU | 5fb1ea3 | 2016-01-14 10:58:14 -0800 | [diff] [blame] | 162 | * @return the ID of this resource path |
Sho SHIMIZU | 7e6d18e | 2016-01-07 18:44:33 -0800 | [diff] [blame] | 163 | */ |
Sho SHIMIZU | 2a70451 | 2016-01-26 14:41:34 -0800 | [diff] [blame] | 164 | ResourceId id(); |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 165 | } |