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; |
| 19 | import com.google.common.base.MoreObjects; |
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; |
| 24 | import java.util.Objects; |
| 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 | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 28 | import static com.google.common.base.Preconditions.checkNotNull; |
Sho SHIMIZU | 60ac58e | 2015-11-11 12:16:38 -0800 | [diff] [blame] | 29 | import static com.google.common.base.Preconditions.checkState; |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 30 | |
| 31 | /** |
| 32 | * An object that is used to locate a resource in a network. |
| 33 | * A ResourcePath represents a path that is hierarchical and composed of a sequence |
| 34 | * of elementary resources that are not globally identifiable. A ResourcePath can be a globally |
| 35 | * unique resource identifier. |
| 36 | * |
Sho SHIMIZU | 60ac58e | 2015-11-11 12:16:38 -0800 | [diff] [blame] | 37 | * Two types of resource are considered. One is discrete type and the other is continuous type. |
| 38 | * Discrete type resource is a resource whose amount is measured as a discrete unit. VLAN ID and |
| 39 | * MPLS label are examples of discrete type resource. Continuous type resource is a resource whose |
| 40 | * amount is measured as a continuous value. Bandwidth is an example of continuous type resource. |
| 41 | * A double value is associated with a continuous type value. |
| 42 | * |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 43 | * Users of this class must keep the semantics of resources regarding the hierarchical structure. |
Sho SHIMIZU | b1f1625 | 2015-11-25 23:03:16 -0800 | [diff] [blame] | 44 | * For example, resource path, Device:1/Port:1/VLAN ID:100, is valid, but resource path, |
| 45 | * 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] | 46 | */ |
| 47 | @Beta |
Sho SHIMIZU | 60ac58e | 2015-11-11 12:16:38 -0800 | [diff] [blame] | 48 | public abstract class ResourcePath { |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 49 | |
Sho SHIMIZU | 60ac58e | 2015-11-11 12:16:38 -0800 | [diff] [blame] | 50 | private final Discrete parent; |
Sho SHIMIZU | 76b30f7 | 2016-01-11 14:08:35 -0800 | [diff] [blame] | 51 | private final ResourceId id; |
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 | public static final Discrete ROOT = new Discrete(); |
Sho SHIMIZU | ba41fc1 | 2015-08-12 15:43:22 -0700 | [diff] [blame] | 54 | |
Sho SHIMIZU | b1f1625 | 2015-11-25 23:03:16 -0800 | [diff] [blame] | 55 | public static ResourcePath discrete(DeviceId device) { |
Sho SHIMIZU | 76b30f7 | 2016-01-11 14:08:35 -0800 | [diff] [blame] | 56 | return new Discrete(ResourceId.of(device)); |
Sho SHIMIZU | b1f1625 | 2015-11-25 23:03:16 -0800 | [diff] [blame] | 57 | } |
| 58 | |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 59 | /** |
Sho SHIMIZU | 60ac58e | 2015-11-11 12:16:38 -0800 | [diff] [blame] | 60 | * 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] | 61 | * |
Sho SHIMIZU | b1f1625 | 2015-11-25 23:03:16 -0800 | [diff] [blame] | 62 | * @param device device ID which is the first component of the path |
| 63 | * @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] | 64 | * @return resource path instance |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 65 | */ |
Sho SHIMIZU | b1f1625 | 2015-11-25 23:03:16 -0800 | [diff] [blame] | 66 | public static ResourcePath discrete(DeviceId device, Object... components) { |
Sho SHIMIZU | 76b30f7 | 2016-01-11 14:08:35 -0800 | [diff] [blame] | 67 | return new Discrete(ResourceId.of(device, components)); |
Sho SHIMIZU | b1f1625 | 2015-11-25 23:03:16 -0800 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Creates an resource path which represents a discrete-type resource from the specified components. |
| 72 | * |
| 73 | * @param device device ID which is the first component of the path |
| 74 | * @param port port number which is the second component of the path |
| 75 | * @param components following components of the path. The order represents hierarchical structure of the resource. |
| 76 | * @return resource path instance |
| 77 | */ |
| 78 | public static ResourcePath discrete(DeviceId device, PortNumber port, Object... components) { |
Sho SHIMIZU | 76b30f7 | 2016-01-11 14:08:35 -0800 | [diff] [blame] | 79 | return new Discrete(ResourceId.of(device, port, components)); |
Sho SHIMIZU | 60ac58e | 2015-11-11 12:16:38 -0800 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Creates an resource path which represents a continuous-type resource from the specified components. |
| 84 | * |
| 85 | * @param value amount of the resource |
Sho SHIMIZU | b1f1625 | 2015-11-25 23:03:16 -0800 | [diff] [blame] | 86 | * @param device device ID which is the first component of the path |
| 87 | * @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] | 88 | * @return resource path instance |
Sho SHIMIZU | 60ac58e | 2015-11-11 12:16:38 -0800 | [diff] [blame] | 89 | */ |
Sho SHIMIZU | b1f1625 | 2015-11-25 23:03:16 -0800 | [diff] [blame] | 90 | public static ResourcePath continuous(double value, DeviceId device, Object... components) { |
| 91 | checkArgument(components.length > 0, |
| 92 | "Length of components must be greater thant 0, but " + components.length); |
| 93 | |
Sho SHIMIZU | 76b30f7 | 2016-01-11 14:08:35 -0800 | [diff] [blame] | 94 | return new Continuous(ResourceId.of(device, components), value); |
Sho SHIMIZU | b1f1625 | 2015-11-25 23:03:16 -0800 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Creates an resource path which represents a continuous-type resource from the specified components. |
| 99 | * |
| 100 | * @param value amount of the resource |
| 101 | * @param device device ID which is the first component of the path. |
| 102 | * @param port port number which is the second component of the path. |
| 103 | * @param components following components of the path. The order represents hierarchical structure of the resource. |
| 104 | * @return resource path instance |
| 105 | */ |
| 106 | public static ResourcePath continuous(double value, DeviceId device, PortNumber port, Object... components) { |
Sho SHIMIZU | 76b30f7 | 2016-01-11 14:08:35 -0800 | [diff] [blame] | 107 | return new Continuous(ResourceId.of(device, port, components), value); |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | /** |
Sho SHIMIZU | 5fb1ea3 | 2016-01-14 10:58:14 -0800 | [diff] [blame^] | 111 | * Creates an resource path from the specified id. |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 112 | * |
Sho SHIMIZU | 5fb1ea3 | 2016-01-14 10:58:14 -0800 | [diff] [blame^] | 113 | * @param id id of the path |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 114 | */ |
Sho SHIMIZU | 76b30f7 | 2016-01-11 14:08:35 -0800 | [diff] [blame] | 115 | protected ResourcePath(ResourceId id) { |
| 116 | checkNotNull(id); |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 117 | |
Sho SHIMIZU | 76b30f7 | 2016-01-11 14:08:35 -0800 | [diff] [blame] | 118 | this.id = id; |
| 119 | if (id.components.size() == 1) { |
Sho SHIMIZU | 60ac58e | 2015-11-11 12:16:38 -0800 | [diff] [blame] | 120 | this.parent = ROOT; |
| 121 | } else { |
Sho SHIMIZU | 76b30f7 | 2016-01-11 14:08:35 -0800 | [diff] [blame] | 122 | this.parent = new Discrete(id.parent()); |
Sho SHIMIZU | 60ac58e | 2015-11-11 12:16:38 -0800 | [diff] [blame] | 123 | } |
Sho SHIMIZU | c9546a3 | 2015-11-10 11:22:28 -0800 | [diff] [blame] | 124 | } |
| 125 | |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 126 | // for serialization |
| 127 | private ResourcePath() { |
Sho SHIMIZU | c9546a3 | 2015-11-10 11:22:28 -0800 | [diff] [blame] | 128 | this.parent = null; |
Sho SHIMIZU | 76b30f7 | 2016-01-11 14:08:35 -0800 | [diff] [blame] | 129 | this.id = ResourceId.ROOT; |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Returns the components of this resource path. |
| 134 | * |
| 135 | * @return the components of this resource path |
| 136 | */ |
| 137 | public List<Object> components() { |
Sho SHIMIZU | 76b30f7 | 2016-01-11 14:08:35 -0800 | [diff] [blame] | 138 | return id.components; |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Returns the parent resource path of this instance. |
| 143 | * E.g. if this path is Link:1/VLAN ID:100, the return value is the resource path for Link:1. |
| 144 | * |
| 145 | * @return the parent resource path of this instance. |
| 146 | * If there is no parent, empty instance will be returned. |
| 147 | */ |
Sho SHIMIZU | 60ac58e | 2015-11-11 12:16:38 -0800 | [diff] [blame] | 148 | public Optional<Discrete> parent() { |
Sho SHIMIZU | c9546a3 | 2015-11-10 11:22:28 -0800 | [diff] [blame] | 149 | return Optional.ofNullable(parent); |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 150 | } |
| 151 | |
Sho SHIMIZU | 9e1e9de | 2015-11-25 15:48:48 -0800 | [diff] [blame] | 152 | /** |
| 153 | * Returns a child resource path of this instance with specifying the child object. |
| 154 | * The child resource path is discrete-type. |
| 155 | * |
| 156 | * @param child child object |
| 157 | * @return a child resource path |
| 158 | */ |
Sho SHIMIZU | c9546a3 | 2015-11-10 11:22:28 -0800 | [diff] [blame] | 159 | public ResourcePath child(Object child) { |
Sho SHIMIZU | 60ac58e | 2015-11-11 12:16:38 -0800 | [diff] [blame] | 160 | checkState(this instanceof Discrete); |
| 161 | |
Sho SHIMIZU | 76b30f7 | 2016-01-11 14:08:35 -0800 | [diff] [blame] | 162 | return new Discrete(id().child(child)); |
Sho SHIMIZU | 60ac58e | 2015-11-11 12:16:38 -0800 | [diff] [blame] | 163 | } |
| 164 | |
Sho SHIMIZU | 9e1e9de | 2015-11-25 15:48:48 -0800 | [diff] [blame] | 165 | /** |
| 166 | * Returns a child resource path of this instance with specifying a child object and |
| 167 | * value. The child resource path is continuous-type. |
| 168 | * |
| 169 | * @param child child object |
| 170 | * @param value value |
| 171 | * @return a child resource path |
| 172 | */ |
Sho SHIMIZU | 60ac58e | 2015-11-11 12:16:38 -0800 | [diff] [blame] | 173 | public ResourcePath child(Object child, double value) { |
| 174 | checkState(this instanceof Discrete); |
| 175 | |
Sho SHIMIZU | 76b30f7 | 2016-01-11 14:08:35 -0800 | [diff] [blame] | 176 | return new Continuous(id.child(child), value); |
Sho SHIMIZU | 0112078 | 2015-08-21 15:48:43 -0700 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | /** |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 180 | * Returns the last component of this instance. |
| 181 | * |
| 182 | * @return the last component of this instance. |
| 183 | * The return value is equal to the last object of {@code components()}. |
| 184 | */ |
Sho SHIMIZU | c9546a3 | 2015-11-10 11:22:28 -0800 | [diff] [blame] | 185 | public Object last() { |
Sho SHIMIZU | 76b30f7 | 2016-01-11 14:08:35 -0800 | [diff] [blame] | 186 | if (id.components.isEmpty()) { |
Sho SHIMIZU | 7e6d18e | 2016-01-07 18:44:33 -0800 | [diff] [blame] | 187 | return null; |
| 188 | } |
Sho SHIMIZU | 76b30f7 | 2016-01-11 14:08:35 -0800 | [diff] [blame] | 189 | return id.components.get(id.components.size() - 1); |
Sho SHIMIZU | 7e6d18e | 2016-01-07 18:44:33 -0800 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | /** |
Sho SHIMIZU | 5fb1ea3 | 2016-01-14 10:58:14 -0800 | [diff] [blame^] | 193 | * Returns the ID of this resource path. |
Sho SHIMIZU | 7e6d18e | 2016-01-07 18:44:33 -0800 | [diff] [blame] | 194 | * |
Sho SHIMIZU | 5fb1ea3 | 2016-01-14 10:58:14 -0800 | [diff] [blame^] | 195 | * @return the ID of this resource path |
Sho SHIMIZU | 7e6d18e | 2016-01-07 18:44:33 -0800 | [diff] [blame] | 196 | */ |
Sho SHIMIZU | 76b30f7 | 2016-01-11 14:08:35 -0800 | [diff] [blame] | 197 | public ResourceId id() { |
| 198 | return id; |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | @Override |
| 202 | public int hashCode() { |
Sho SHIMIZU | 76b30f7 | 2016-01-11 14:08:35 -0800 | [diff] [blame] | 203 | return id.hashCode(); |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | @Override |
| 207 | public boolean equals(Object obj) { |
| 208 | if (this == obj) { |
| 209 | return true; |
| 210 | } |
| 211 | if (!(obj instanceof ResourcePath)) { |
| 212 | return false; |
| 213 | } |
| 214 | final ResourcePath that = (ResourcePath) obj; |
Sho SHIMIZU | 76b30f7 | 2016-01-11 14:08:35 -0800 | [diff] [blame] | 215 | return Objects.equals(this.id, that.id); |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | @Override |
| 219 | public String toString() { |
| 220 | return MoreObjects.toStringHelper(this) |
Sho SHIMIZU | 5fb1ea3 | 2016-01-14 10:58:14 -0800 | [diff] [blame^] | 221 | .add("id", id) |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 222 | .toString(); |
| 223 | } |
Sho SHIMIZU | 60ac58e | 2015-11-11 12:16:38 -0800 | [diff] [blame] | 224 | |
| 225 | /** |
| 226 | * Represents a resource path which specifies a resource which can be measured |
| 227 | * as a discrete unit. A VLAN ID and a MPLS label of a link are examples of the resource. |
| 228 | * <p> |
| 229 | * Note: This class is exposed to the public, but intended to be used in the resource API |
| 230 | * implementation only. It is not for resource API user. |
| 231 | * </p> |
| 232 | */ |
Sho SHIMIZU | 6196cae | 2015-11-25 12:02:12 -0800 | [diff] [blame] | 233 | @Beta |
Sho SHIMIZU | 60ac58e | 2015-11-11 12:16:38 -0800 | [diff] [blame] | 234 | public static final class Discrete extends ResourcePath { |
| 235 | private Discrete() { |
| 236 | super(); |
| 237 | } |
| 238 | |
Sho SHIMIZU | 5fb1ea3 | 2016-01-14 10:58:14 -0800 | [diff] [blame^] | 239 | private Discrete(ResourceId id) { |
| 240 | super(id); |
Sho SHIMIZU | 60ac58e | 2015-11-11 12:16:38 -0800 | [diff] [blame] | 241 | } |
| 242 | } |
| 243 | |
| 244 | /** |
| 245 | * Represents a resource path which specifies a resource which can be measured |
| 246 | * as continuous value. Bandwidth of a link is an example of the resource. |
| 247 | * <p> |
| 248 | * Note: This class is exposed to the public, but intended to be used in the resource API |
| 249 | * implementation only. It is not for resource API user. |
| 250 | */ |
Sho SHIMIZU | 6196cae | 2015-11-25 12:02:12 -0800 | [diff] [blame] | 251 | @Beta |
Sho SHIMIZU | 60ac58e | 2015-11-11 12:16:38 -0800 | [diff] [blame] | 252 | public static final class Continuous extends ResourcePath { |
Sho SHIMIZU | 60ac58e | 2015-11-11 12:16:38 -0800 | [diff] [blame] | 253 | private final double value; |
| 254 | |
Sho SHIMIZU | 5fb1ea3 | 2016-01-14 10:58:14 -0800 | [diff] [blame^] | 255 | private Continuous(ResourceId id, double value) { |
| 256 | super(id); |
Sho SHIMIZU | 60ac58e | 2015-11-11 12:16:38 -0800 | [diff] [blame] | 257 | this.value = value; |
| 258 | } |
| 259 | |
Sho SHIMIZU | 7e6d18e | 2016-01-07 18:44:33 -0800 | [diff] [blame] | 260 | @Override |
| 261 | public int hashCode() { |
Sho SHIMIZU | 76b30f7 | 2016-01-11 14:08:35 -0800 | [diff] [blame] | 262 | return Objects.hash(this.id(), this.value); |
Sho SHIMIZU | 7e6d18e | 2016-01-07 18:44:33 -0800 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | @Override |
| 266 | public boolean equals(Object obj) { |
| 267 | if (this == obj) { |
| 268 | return true; |
| 269 | } |
| 270 | |
| 271 | if (!(obj instanceof Continuous)) { |
| 272 | return false; |
| 273 | } |
| 274 | |
| 275 | if (!super.equals(obj)) { |
| 276 | return false; |
| 277 | } |
| 278 | |
| 279 | final Continuous other = (Continuous) obj; |
Sho SHIMIZU | 76b30f7 | 2016-01-11 14:08:35 -0800 | [diff] [blame] | 280 | return Objects.equals(this.id(), other.id()); |
Sho SHIMIZU | 60ac58e | 2015-11-11 12:16:38 -0800 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | /** |
| 284 | * Returns the value of the resource amount. |
| 285 | * |
| 286 | * @return the value of the resource amount |
| 287 | */ |
| 288 | public double value() { |
| 289 | return value; |
| 290 | } |
| 291 | } |
Sho SHIMIZU | 7e6d18e | 2016-01-07 18:44:33 -0800 | [diff] [blame] | 292 | |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 293 | } |