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