blob: fcfcdd55bd61db79e504fbf708a95bb276443e8e [file] [log] [blame]
Sho SHIMIZUf33b8932016-01-25 18:43:32 -08001/*
2 * Copyright 2016 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 */
16package org.onosproject.net.newresource;
17
18import com.google.common.annotations.Beta;
Sho SHIMIZU2a704512016-01-26 14:41:34 -080019import com.google.common.base.MoreObjects;
Sho SHIMIZUf33b8932016-01-25 18:43:32 -080020
21import java.util.Objects;
Sho SHIMIZU2a704512016-01-26 14:41:34 -080022import java.util.Optional;
Sho SHIMIZUf33b8932016-01-25 18:43:32 -080023
Sho SHIMIZU0e1a4762016-02-11 13:04:41 -080024import static com.google.common.base.Preconditions.checkNotNull;
25
Sho SHIMIZUf33b8932016-01-25 18:43:32 -080026/**
27 * Represents a resource path which specifies a resource which can be measured
28 * as continuous value. Bandwidth of a link is an example of the resource.
Sho SHIMIZUf33b8932016-01-25 18:43:32 -080029 */
30@Beta
Sho SHIMIZU2a704512016-01-26 14:41:34 -080031public final class ContinuousResource implements Resource {
32 private final ContinuousResourceId id;
Sho SHIMIZUf33b8932016-01-25 18:43:32 -080033 private final double value;
34
Sho SHIMIZU2a704512016-01-26 14:41:34 -080035 ContinuousResource(ContinuousResourceId id, double value) {
36 this.id = id;
Sho SHIMIZUf33b8932016-01-25 18:43:32 -080037 this.value = value;
38 }
39
Sho SHIMIZU7f8a7cb2016-01-27 15:09:13 -080040 // for serializer
41 ContinuousResource() {
42 this.id = null;
43 this.value = 0;
44 }
45
Sho SHIMIZU2a704512016-01-26 14:41:34 -080046 @Override
47 public ContinuousResourceId id() {
48 return id;
49 }
50
Sho SHIMIZUf33b8932016-01-25 18:43:32 -080051 /**
52 * The user of this methods must receive the return value as Double or double.
53 * Otherwise, this methods throws an exception.
54 *
55 * @param <T> type of the return value
56 * @return the volume of this resource
57 */
58 @SuppressWarnings("unchecked")
59 @Override
60 public <T> T volume() {
61 return (T) Double.valueOf(value);
62 }
63
Sho SHIMIZU2a704512016-01-26 14:41:34 -080064 /**
65 * Returns the value of the resource amount.
66 *
67 * @return the value of the resource amount
68 */
69 // FIXME: overlapping a purpose with volume()
70 public double value() {
71 return value;
72 }
73
74 @Override
Sho SHIMIZUb08d5862016-02-11 12:37:28 -080075 public boolean isSubTypeOf(Class<?> ancestor) {
Sho SHIMIZU0e1a4762016-02-11 13:04:41 -080076 checkNotNull(ancestor);
77
Sho SHIMIZU9c02f3a2016-01-29 15:11:59 -080078 String typeName = (String) id.components().get(id.components().size() - 1);
Sho SHIMIZUb08d5862016-02-11 12:37:28 -080079 boolean foundInLeaf = typeName.equals(ancestor.getCanonicalName());
Sho SHIMIZU9c02f3a2016-01-29 15:11:59 -080080 boolean foundInAncestor = id.components().subList(0, id.components().size()).stream()
81 .map(Object::getClass)
Sho SHIMIZUb08d5862016-02-11 12:37:28 -080082 .filter(x -> x.equals(ancestor))
Sho SHIMIZU9c02f3a2016-01-29 15:11:59 -080083 .findAny()
84 .isPresent();
85 return foundInAncestor || foundInLeaf;
Sho SHIMIZU2a704512016-01-26 14:41:34 -080086 }
87
88 @Override
89 public Object last() {
Sho SHIMIZU42ac51f2016-01-27 12:59:31 -080090 if (id.components().isEmpty()) {
Sho SHIMIZU2a704512016-01-26 14:41:34 -080091 return null;
92 }
Sho SHIMIZU42ac51f2016-01-27 12:59:31 -080093 return id.components().get(id.components().size() - 1);
Sho SHIMIZU2a704512016-01-26 14:41:34 -080094 }
95
96 @Override
97 public DiscreteResource child(Object child) {
98 throw new UnsupportedOperationException();
99 }
100
101 @Override
102 public ContinuousResource child(Class<?> child, double value) {
103 throw new UnsupportedOperationException();
104 }
105
106 @Override
107 public Optional<DiscreteResource> parent() {
Sho SHIMIZU460b9722016-01-28 10:48:26 -0800108 return id.parent().map(x -> Resources.discrete(x).resource());
Sho SHIMIZU2a704512016-01-26 14:41:34 -0800109 }
110
Sho SHIMIZUf33b8932016-01-25 18:43:32 -0800111 @Override
112 public int hashCode() {
Sho SHIMIZU34b55b62016-01-27 12:49:43 -0800113 return Objects.hash(id, value);
Sho SHIMIZUf33b8932016-01-25 18:43:32 -0800114 }
115
116 @Override
117 public boolean equals(Object obj) {
118 if (this == obj) {
119 return true;
120 }
121 if (obj == null || getClass() != obj.getClass()) {
122 return false;
123 }
124 final ContinuousResource other = (ContinuousResource) obj;
Sho SHIMIZU34b55b62016-01-27 12:49:43 -0800125 return Objects.equals(this.id, other.id)
Sho SHIMIZUf33b8932016-01-25 18:43:32 -0800126 && Objects.equals(this.value, other.value);
127 }
128
Sho SHIMIZU2a704512016-01-26 14:41:34 -0800129 @Override
130 public String toString() {
131 return MoreObjects.toStringHelper(this)
132 .add("id", id)
133 .add("volume", value)
134 .toString();
Sho SHIMIZUf33b8932016-01-25 18:43:32 -0800135 }
136}