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