Sho SHIMIZU | f33b893 | 2016-01-25 18:43:32 -0800 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.net.newresource; |
| 17 | |
| 18 | import com.google.common.annotations.Beta; |
Sho SHIMIZU | 2a70451 | 2016-01-26 14:41:34 -0800 | [diff] [blame] | 19 | import com.google.common.base.MoreObjects; |
Sho SHIMIZU | f33b893 | 2016-01-25 18:43:32 -0800 | [diff] [blame] | 20 | |
| 21 | import java.util.Objects; |
Sho SHIMIZU | 2a70451 | 2016-01-26 14:41:34 -0800 | [diff] [blame] | 22 | import java.util.Optional; |
| 23 | |
| 24 | import static com.google.common.base.Preconditions.checkArgument; |
Sho SHIMIZU | 0e1a476 | 2016-02-11 13:04:41 -0800 | [diff] [blame] | 25 | import static com.google.common.base.Preconditions.checkNotNull; |
Sho SHIMIZU | f33b893 | 2016-01-25 18:43:32 -0800 | [diff] [blame] | 26 | |
| 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 SHIMIZU | f33b893 | 2016-01-25 18:43:32 -0800 | [diff] [blame] | 30 | */ |
| 31 | @Beta |
Sho SHIMIZU | 2a70451 | 2016-01-26 14:41:34 -0800 | [diff] [blame] | 32 | public final class DiscreteResource implements Resource { |
| 33 | private final DiscreteResourceId id; |
| 34 | |
| 35 | DiscreteResource(DiscreteResourceId id) { |
| 36 | this.id = id; |
Sho SHIMIZU | f33b893 | 2016-01-25 18:43:32 -0800 | [diff] [blame] | 37 | } |
| 38 | |
Sho SHIMIZU | f95b96e | 2016-01-25 19:35:15 -0800 | [diff] [blame] | 39 | DiscreteResource() { |
Sho SHIMIZU | 2a70451 | 2016-01-26 14:41:34 -0800 | [diff] [blame] | 40 | this.id = ResourceId.ROOT; |
| 41 | } |
| 42 | |
| 43 | @Override |
| 44 | public DiscreteResourceId id() { |
| 45 | return id; |
Sho SHIMIZU | f33b893 | 2016-01-25 18:43:32 -0800 | [diff] [blame] | 46 | } |
| 47 | |
Sho SHIMIZU | 003ed32 | 2016-02-11 12:58:42 -0800 | [diff] [blame] | 48 | @Override |
| 49 | public boolean isTypeOf(Class<?> type) { |
| 50 | checkNotNull(type); |
| 51 | |
| 52 | if (isRoot()) { |
| 53 | return false; |
| 54 | } |
| 55 | |
| 56 | return type.isAssignableFrom(id.components().get(id.components().size() - 1).getClass()); |
| 57 | } |
| 58 | |
Sho SHIMIZU | f33b893 | 2016-01-25 18:43:32 -0800 | [diff] [blame] | 59 | @Override |
Sho SHIMIZU | b08d586 | 2016-02-11 12:37:28 -0800 | [diff] [blame] | 60 | public boolean isSubTypeOf(Class<?> ancestor) { |
Sho SHIMIZU | 0e1a476 | 2016-02-11 13:04:41 -0800 | [diff] [blame] | 61 | checkNotNull(ancestor); |
| 62 | |
Sho SHIMIZU | 9c02f3a | 2016-01-29 15:11:59 -0800 | [diff] [blame] | 63 | return id.components().stream() |
| 64 | .map(Object::getClass) |
Sho SHIMIZU | b08d586 | 2016-02-11 12:37:28 -0800 | [diff] [blame] | 65 | .filter(x -> x.equals(ancestor)) |
Sho SHIMIZU | 9c02f3a | 2016-01-29 15:11:59 -0800 | [diff] [blame] | 66 | .findAny() |
| 67 | .isPresent(); |
Sho SHIMIZU | 2a70451 | 2016-01-26 14:41:34 -0800 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | @Override |
Sho SHIMIZU | f08cb4c | 2016-02-11 18:35:59 -0800 | [diff] [blame^] | 71 | public <T> Optional<T> valueAs(Class<T> type) { |
| 72 | checkNotNull(type); |
| 73 | |
| 74 | if (!isTypeOf(type)) { |
| 75 | return Optional.empty(); |
| 76 | } |
| 77 | |
| 78 | @SuppressWarnings("unchecked") |
| 79 | T value = (T) id.components().get(id.components().size() - 1); |
| 80 | return Optional.of(value); |
| 81 | } |
| 82 | |
| 83 | @Override |
Sho SHIMIZU | 2a70451 | 2016-01-26 14:41:34 -0800 | [diff] [blame] | 84 | public Object last() { |
Sho SHIMIZU | 42ac51f | 2016-01-27 12:59:31 -0800 | [diff] [blame] | 85 | if (id.components().isEmpty()) { |
Sho SHIMIZU | 2a70451 | 2016-01-26 14:41:34 -0800 | [diff] [blame] | 86 | return null; |
| 87 | } |
Sho SHIMIZU | 42ac51f | 2016-01-27 12:59:31 -0800 | [diff] [blame] | 88 | return id.components().get(id.components().size() - 1); |
Sho SHIMIZU | 2a70451 | 2016-01-26 14:41:34 -0800 | [diff] [blame] | 89 | } |
| 90 | |
Sho SHIMIZU | 003ed32 | 2016-02-11 12:58:42 -0800 | [diff] [blame] | 91 | private boolean isRoot() { |
| 92 | return id.equals(ResourceId.ROOT); |
| 93 | } |
| 94 | |
Sho SHIMIZU | 2a70451 | 2016-01-26 14:41:34 -0800 | [diff] [blame] | 95 | @Override |
| 96 | public DiscreteResource child(Object child) { |
| 97 | checkArgument(!(child instanceof Class<?>)); |
| 98 | |
Sho SHIMIZU | 460b972 | 2016-01-28 10:48:26 -0800 | [diff] [blame] | 99 | return Resources.discrete(id.child(child)).resource(); |
Sho SHIMIZU | 2a70451 | 2016-01-26 14:41:34 -0800 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | @Override |
| 103 | public ContinuousResource child(Class<?> child, double value) { |
Sho SHIMIZU | 460b972 | 2016-01-28 10:48:26 -0800 | [diff] [blame] | 104 | return Resources.continuous(id.child(child)).resource(value); |
Sho SHIMIZU | 2a70451 | 2016-01-26 14:41:34 -0800 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | @Override |
| 108 | public Optional<DiscreteResource> parent() { |
Sho SHIMIZU | 460b972 | 2016-01-28 10:48:26 -0800 | [diff] [blame] | 109 | return id.parent().map(x -> Resources.discrete(x).resource()); |
Sho SHIMIZU | 2a70451 | 2016-01-26 14:41:34 -0800 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | @Override |
Sho SHIMIZU | f33b893 | 2016-01-25 18:43:32 -0800 | [diff] [blame] | 113 | public int hashCode() { |
| 114 | // the value returing from volume() is excluded due to optimization |
Sho SHIMIZU | 34b55b6 | 2016-01-27 12:49:43 -0800 | [diff] [blame] | 115 | return id.hashCode(); |
Sho SHIMIZU | f33b893 | 2016-01-25 18:43:32 -0800 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | @Override |
| 119 | public boolean equals(Object obj) { |
| 120 | if (this == obj) { |
| 121 | return true; |
| 122 | } |
| 123 | if (obj == null || getClass() != obj.getClass()) { |
| 124 | return false; |
| 125 | } |
| 126 | final DiscreteResource other = (DiscreteResource) obj; |
| 127 | // the value returing from volume() is excluded due to optimization |
Sho SHIMIZU | 34b55b6 | 2016-01-27 12:49:43 -0800 | [diff] [blame] | 128 | return Objects.equals(this.id, other.id); |
Sho SHIMIZU | f33b893 | 2016-01-25 18:43:32 -0800 | [diff] [blame] | 129 | } |
Sho SHIMIZU | 2a70451 | 2016-01-26 14:41:34 -0800 | [diff] [blame] | 130 | |
| 131 | @Override |
| 132 | public String toString() { |
| 133 | return MoreObjects.toStringHelper(this) |
| 134 | .add("id", id) |
Sho SHIMIZU | 2a70451 | 2016-01-26 14:41:34 -0800 | [diff] [blame] | 135 | .toString(); |
| 136 | } |
Sho SHIMIZU | f33b893 | 2016-01-25 18:43:32 -0800 | [diff] [blame] | 137 | } |