blob: 1dadcc2acf11a687f9511cc190115d3c8f1acb83 [file] [log] [blame]
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -07001/*
Sho SHIMIZU7e6d18e2016-01-07 18:44:33 -08002 * Copyright 2015-2016 Open Networking Laboratory
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -07003 *
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 */
16package org.onosproject.net.newresource;
17
18import com.google.common.annotations.Beta;
19import com.google.common.base.MoreObjects;
Sho SHIMIZUb1f16252015-11-25 23:03:16 -080020import org.onosproject.net.DeviceId;
21import org.onosproject.net.PortNumber;
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -070022
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -070023import java.util.List;
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -070024import java.util.Optional;
25
Sho SHIMIZU60ac58e2015-11-11 12:16:38 -080026import static com.google.common.base.Preconditions.checkArgument;
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -070027import static com.google.common.base.Preconditions.checkNotNull;
Sho SHIMIZU60ac58e2015-11-11 12:16:38 -080028import static com.google.common.base.Preconditions.checkState;
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -070029
30/**
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -080031 * 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 SHIMIZU1f5e5912015-08-10 17:00:00 -070035 *
Sho SHIMIZU60ac58e2015-11-11 12:16:38 -080036 * 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 SHIMIZU1f5e5912015-08-10 17:00:00 -070042 * Users of this class must keep the semantics of resources regarding the hierarchical structure.
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -080043 * For example, resource, Device:1/Port:1/VLAN ID:100, is valid, but resource,
Sho SHIMIZUb1f16252015-11-25 23:03:16 -080044 * VLAN ID:100/Device:1/Port:1 is not valid because a link is not a sub-component of a VLAN ID.
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -070045 */
46@Beta
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -080047public abstract class Resource {
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -070048
Sho SHIMIZUf33b8932016-01-25 18:43:32 -080049 private final DiscreteResource parent;
Sho SHIMIZU76b30f72016-01-11 14:08:35 -080050 private final ResourceId id;
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -070051
Sho SHIMIZUf33b8932016-01-25 18:43:32 -080052 public static final DiscreteResource ROOT = new DiscreteResource();
Sho SHIMIZUba41fc12015-08-12 15:43:22 -070053
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -080054 public static Resource discrete(DeviceId device) {
Sho SHIMIZUf33b8932016-01-25 18:43:32 -080055 return new DiscreteResource(ResourceId.discrete(device));
Sho SHIMIZUb1f16252015-11-25 23:03:16 -080056 }
57
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -070058 /**
Sho SHIMIZU60ac58e2015-11-11 12:16:38 -080059 * Creates an resource path which represents a discrete-type resource from the specified components.
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -070060 *
Sho SHIMIZUb1f16252015-11-25 23:03:16 -080061 * @param device device ID which is the first component of the path
62 * @param components following components of the path. The order represents hierarchical structure of the resource.
Sho SHIMIZUe5524562015-11-24 14:41:20 -080063 * @return resource path instance
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -070064 */
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -080065 public static Resource discrete(DeviceId device, Object... components) {
Sho SHIMIZUf33b8932016-01-25 18:43:32 -080066 return new DiscreteResource(ResourceId.discrete(device, components));
Sho SHIMIZUb1f16252015-11-25 23:03:16 -080067 }
68
69 /**
70 * Creates an resource path which represents a discrete-type resource from the specified components.
71 *
72 * @param device device ID which is the first component of the path
73 * @param port port number which is the second component of the path
74 * @param components following components of the path. The order represents hierarchical structure of the resource.
75 * @return resource path instance
76 */
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -080077 public static Resource discrete(DeviceId device, PortNumber port, Object... components) {
Sho SHIMIZUf33b8932016-01-25 18:43:32 -080078 return new DiscreteResource(ResourceId.discrete(device, port, components));
Sho SHIMIZU60ac58e2015-11-11 12:16:38 -080079 }
80
81 /**
82 * Creates an resource path which represents a continuous-type resource from the specified components.
83 *
84 * @param value amount of the resource
Sho SHIMIZUb1f16252015-11-25 23:03:16 -080085 * @param device device ID which is the first component of the path
86 * @param components following components of the path. The order represents hierarchical structure of the resource.
Sho SHIMIZU2d310222016-01-22 11:45:11 -080087 * The last element of this list must be an {@link Class} instance. Otherwise, this method throws
88 * an IllegalArgumentException.
Sho SHIMIZUe5524562015-11-24 14:41:20 -080089 * @return resource path instance
Sho SHIMIZU60ac58e2015-11-11 12:16:38 -080090 */
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -080091 public static Resource continuous(double value, DeviceId device, Object... components) {
Sho SHIMIZUb1f16252015-11-25 23:03:16 -080092 checkArgument(components.length > 0,
93 "Length of components must be greater thant 0, but " + components.length);
94
Sho SHIMIZUf33b8932016-01-25 18:43:32 -080095 return new ContinuousResource(ResourceId.continuous(device, components), value);
Sho SHIMIZUb1f16252015-11-25 23:03:16 -080096 }
97
98 /**
99 * Creates an resource path which represents a continuous-type resource from the specified components.
100 *
101 * @param value amount of the resource
102 * @param device device ID which is the first component of the path.
103 * @param port port number which is the second component of the path.
104 * @param components following components of the path. The order represents hierarchical structure of the resource.
Sho SHIMIZU2d310222016-01-22 11:45:11 -0800105 * The last element of this list must be an {@link Class} instance. Otherwise, this method throws
106 * an IllegalArgumentException.
Sho SHIMIZUb1f16252015-11-25 23:03:16 -0800107 * @return resource path instance
108 */
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -0800109 public static Resource continuous(double value, DeviceId device, PortNumber port, Object... components) {
Sho SHIMIZUf33b8932016-01-25 18:43:32 -0800110 return new ContinuousResource(ResourceId.continuous(device, port, components), value);
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -0700111 }
112
113 /**
Sho SHIMIZU5fb1ea32016-01-14 10:58:14 -0800114 * Creates an resource path from the specified id.
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -0700115 *
Sho SHIMIZU5fb1ea32016-01-14 10:58:14 -0800116 * @param id id of the path
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -0700117 */
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -0800118 protected Resource(ResourceId id) {
Sho SHIMIZU76b30f72016-01-11 14:08:35 -0800119 checkNotNull(id);
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -0700120
Sho SHIMIZU76b30f72016-01-11 14:08:35 -0800121 this.id = id;
122 if (id.components.size() == 1) {
Sho SHIMIZU60ac58e2015-11-11 12:16:38 -0800123 this.parent = ROOT;
124 } else {
Sho SHIMIZUf33b8932016-01-25 18:43:32 -0800125 this.parent = new DiscreteResource(id.parent());
Sho SHIMIZU60ac58e2015-11-11 12:16:38 -0800126 }
Sho SHIMIZUc9546a32015-11-10 11:22:28 -0800127 }
128
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -0700129 // for serialization
Sho SHIMIZUf33b8932016-01-25 18:43:32 -0800130 protected Resource() {
Sho SHIMIZUc9546a32015-11-10 11:22:28 -0800131 this.parent = null;
Sho SHIMIZU76b30f72016-01-11 14:08:35 -0800132 this.id = ResourceId.ROOT;
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -0700133 }
134
135 /**
136 * Returns the components of this resource path.
137 *
138 * @return the components of this resource path
139 */
140 public List<Object> components() {
Sho SHIMIZU76b30f72016-01-11 14:08:35 -0800141 return id.components;
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -0700142 }
143
144 /**
Sho SHIMIZU2d310222016-01-22 11:45:11 -0800145 * Returns the volume of this resource.
146 *
147 * @return the volume of this resource
148 */
149 // TODO: think about other naming possibilities. amount? quantity?
150 public abstract <T> T volume();
151
152 /**
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -0700153 * Returns the parent resource path of this instance.
154 * E.g. if this path is Link:1/VLAN ID:100, the return value is the resource path for Link:1.
155 *
156 * @return the parent resource path of this instance.
157 * If there is no parent, empty instance will be returned.
158 */
Sho SHIMIZUf33b8932016-01-25 18:43:32 -0800159 public Optional<DiscreteResource> parent() {
Sho SHIMIZUc9546a32015-11-10 11:22:28 -0800160 return Optional.ofNullable(parent);
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -0700161 }
162
Sho SHIMIZU9e1e9de2015-11-25 15:48:48 -0800163 /**
164 * Returns a child resource path of this instance with specifying the child object.
165 * The child resource path is discrete-type.
166 *
167 * @param child child object
168 * @return a child resource path
169 */
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -0800170 public Resource child(Object child) {
Sho SHIMIZUf33b8932016-01-25 18:43:32 -0800171 checkState(this instanceof DiscreteResource);
Sho SHIMIZU60ac58e2015-11-11 12:16:38 -0800172
Sho SHIMIZUf33b8932016-01-25 18:43:32 -0800173 return new DiscreteResource(id().child(child));
Sho SHIMIZU60ac58e2015-11-11 12:16:38 -0800174 }
175
Sho SHIMIZU9e1e9de2015-11-25 15:48:48 -0800176 /**
177 * Returns a child resource path of this instance with specifying a child object and
178 * value. The child resource path is continuous-type.
179 *
180 * @param child child object
181 * @param value value
182 * @return a child resource path
183 */
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -0800184 public Resource child(Object child, double value) {
Sho SHIMIZUf33b8932016-01-25 18:43:32 -0800185 checkState(this instanceof DiscreteResource);
Sho SHIMIZU60ac58e2015-11-11 12:16:38 -0800186
Sho SHIMIZUf33b8932016-01-25 18:43:32 -0800187 return new ContinuousResource(id.child(child), value);
Sho SHIMIZU01120782015-08-21 15:48:43 -0700188 }
189
190 /**
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -0700191 * Returns the last component of this instance.
192 *
193 * @return the last component of this instance.
194 * The return value is equal to the last object of {@code components()}.
195 */
Sho SHIMIZUc9546a32015-11-10 11:22:28 -0800196 public Object last() {
Sho SHIMIZU76b30f72016-01-11 14:08:35 -0800197 if (id.components.isEmpty()) {
Sho SHIMIZU7e6d18e2016-01-07 18:44:33 -0800198 return null;
199 }
Sho SHIMIZU76b30f72016-01-11 14:08:35 -0800200 return id.components.get(id.components.size() - 1);
Sho SHIMIZU7e6d18e2016-01-07 18:44:33 -0800201 }
202
203 /**
Sho SHIMIZU5fb1ea32016-01-14 10:58:14 -0800204 * Returns the ID of this resource path.
Sho SHIMIZU7e6d18e2016-01-07 18:44:33 -0800205 *
Sho SHIMIZU5fb1ea32016-01-14 10:58:14 -0800206 * @return the ID of this resource path
Sho SHIMIZU7e6d18e2016-01-07 18:44:33 -0800207 */
Sho SHIMIZU76b30f72016-01-11 14:08:35 -0800208 public ResourceId id() {
209 return id;
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -0700210 }
211
212 @Override
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -0700213 public String toString() {
214 return MoreObjects.toStringHelper(this)
Sho SHIMIZU2d310222016-01-22 11:45:11 -0800215 .add("id", id())
216 .add("volume", volume())
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -0700217 .toString();
218 }
Sho SHIMIZU60ac58e2015-11-11 12:16:38 -0800219
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -0700220}