blob: cc3b30a3e17b30c67e517a51dc6b2ba7906e4b2d [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;
23
24import static com.google.common.base.Preconditions.checkArgument;
Sho SHIMIZU0e1a4762016-02-11 13:04:41 -080025import static com.google.common.base.Preconditions.checkNotNull;
Sho SHIMIZUf33b8932016-01-25 18:43:32 -080026
27/**
28 * Represents a resource path which specifies a resource which can be measured
29 * as a discrete unit. A VLAN ID and a MPLS label of a link are examples of the resource.
Sho SHIMIZUf33b8932016-01-25 18:43:32 -080030 */
31@Beta
Sho SHIMIZU2a704512016-01-26 14:41:34 -080032public final class DiscreteResource implements Resource {
33 private final DiscreteResourceId id;
34
35 DiscreteResource(DiscreteResourceId id) {
36 this.id = id;
Sho SHIMIZUf33b8932016-01-25 18:43:32 -080037 }
38
Sho SHIMIZUf95b96e2016-01-25 19:35:15 -080039 DiscreteResource() {
Sho SHIMIZU2a704512016-01-26 14:41:34 -080040 this.id = ResourceId.ROOT;
41 }
42
43 @Override
44 public DiscreteResourceId id() {
45 return id;
Sho SHIMIZUf33b8932016-01-25 18:43:32 -080046 }
47
Sho SHIMIZU003ed322016-02-11 12:58:42 -080048 @Override
Sho SHIMIZU74ac9012016-02-12 10:03:21 -080049 public String simpleTypeName() {
50 return id.simpleTypeName();
51 }
52
53 @Override
Sho SHIMIZU003ed322016-02-11 12:58:42 -080054 public boolean isTypeOf(Class<?> type) {
55 checkNotNull(type);
56
57 if (isRoot()) {
58 return false;
59 }
60
61 return type.isAssignableFrom(id.components().get(id.components().size() - 1).getClass());
62 }
63
Sho SHIMIZUf33b8932016-01-25 18:43:32 -080064 @Override
Sho SHIMIZUb08d5862016-02-11 12:37:28 -080065 public boolean isSubTypeOf(Class<?> ancestor) {
Sho SHIMIZU0e1a4762016-02-11 13:04:41 -080066 checkNotNull(ancestor);
67
Sho SHIMIZU9c02f3a2016-01-29 15:11:59 -080068 return id.components().stream()
69 .map(Object::getClass)
Sho SHIMIZUb08d5862016-02-11 12:37:28 -080070 .filter(x -> x.equals(ancestor))
Sho SHIMIZU9c02f3a2016-01-29 15:11:59 -080071 .findAny()
72 .isPresent();
Sho SHIMIZU2a704512016-01-26 14:41:34 -080073 }
74
75 @Override
Sho SHIMIZUf08cb4c2016-02-11 18:35:59 -080076 public <T> Optional<T> valueAs(Class<T> type) {
77 checkNotNull(type);
78
79 if (!isTypeOf(type)) {
80 return Optional.empty();
81 }
82
83 @SuppressWarnings("unchecked")
84 T value = (T) id.components().get(id.components().size() - 1);
85 return Optional.of(value);
86 }
87
Sho SHIMIZU003ed322016-02-11 12:58:42 -080088 private boolean isRoot() {
89 return id.equals(ResourceId.ROOT);
90 }
91
Sho SHIMIZU2a704512016-01-26 14:41:34 -080092 @Override
93 public DiscreteResource child(Object child) {
94 checkArgument(!(child instanceof Class<?>));
95
Sho SHIMIZU460b9722016-01-28 10:48:26 -080096 return Resources.discrete(id.child(child)).resource();
Sho SHIMIZU2a704512016-01-26 14:41:34 -080097 }
98
99 @Override
100 public ContinuousResource child(Class<?> child, double value) {
Sho SHIMIZU460b9722016-01-28 10:48:26 -0800101 return Resources.continuous(id.child(child)).resource(value);
Sho SHIMIZU2a704512016-01-26 14:41:34 -0800102 }
103
104 @Override
105 public Optional<DiscreteResource> parent() {
Sho SHIMIZU460b9722016-01-28 10:48:26 -0800106 return id.parent().map(x -> Resources.discrete(x).resource());
Sho SHIMIZU2a704512016-01-26 14:41:34 -0800107 }
108
109 @Override
Sho SHIMIZUf33b8932016-01-25 18:43:32 -0800110 public int hashCode() {
111 // the value returing from volume() is excluded due to optimization
Sho SHIMIZU34b55b62016-01-27 12:49:43 -0800112 return id.hashCode();
Sho SHIMIZUf33b8932016-01-25 18:43:32 -0800113 }
114
115 @Override
116 public boolean equals(Object obj) {
117 if (this == obj) {
118 return true;
119 }
120 if (obj == null || getClass() != obj.getClass()) {
121 return false;
122 }
123 final DiscreteResource other = (DiscreteResource) obj;
124 // the value returing from volume() is excluded due to optimization
Sho SHIMIZU34b55b62016-01-27 12:49:43 -0800125 return Objects.equals(this.id, other.id);
Sho SHIMIZUf33b8932016-01-25 18:43:32 -0800126 }
Sho SHIMIZU2a704512016-01-26 14:41:34 -0800127
128 @Override
129 public String toString() {
130 return MoreObjects.toStringHelper(this)
131 .add("id", id)
Sho SHIMIZU2a704512016-01-26 14:41:34 -0800132 .toString();
133 }
Sho SHIMIZUf33b8932016-01-25 18:43:32 -0800134}