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 | |
Sho SHIMIZU | 2a70451 | 2016-01-26 14:41:34 -0800 | [diff] [blame] | 21 | import java.util.List; |
Sho SHIMIZU | f33b893 | 2016-01-25 18:43:32 -0800 | [diff] [blame] | 22 | import java.util.Objects; |
Sho SHIMIZU | 2a70451 | 2016-01-26 14:41:34 -0800 | [diff] [blame] | 23 | import java.util.Optional; |
| 24 | |
| 25 | import static com.google.common.base.Preconditions.checkArgument; |
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. |
| 30 | * <p> |
| 31 | * Note: This class is exposed to the public, but intended to be used in the resource API |
| 32 | * implementation only. It is not for resource API user. |
| 33 | * </p> |
| 34 | */ |
| 35 | @Beta |
Sho SHIMIZU | 2a70451 | 2016-01-26 14:41:34 -0800 | [diff] [blame] | 36 | public final class DiscreteResource implements Resource { |
| 37 | private final DiscreteResourceId id; |
| 38 | |
| 39 | DiscreteResource(DiscreteResourceId id) { |
| 40 | this.id = id; |
Sho SHIMIZU | f33b893 | 2016-01-25 18:43:32 -0800 | [diff] [blame] | 41 | } |
| 42 | |
Sho SHIMIZU | 2a70451 | 2016-01-26 14:41:34 -0800 | [diff] [blame] | 43 | protected DiscreteResource() { |
| 44 | this.id = ResourceId.ROOT; |
| 45 | } |
| 46 | |
| 47 | @Override |
| 48 | public DiscreteResourceId id() { |
| 49 | return id; |
Sho SHIMIZU | f33b893 | 2016-01-25 18:43:32 -0800 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | /** |
| 53 | * The user of this methods must receive the return value as the correct type. |
| 54 | * Otherwise, this methods throws an exception. |
| 55 | * |
| 56 | * @param <T> type of the return value |
| 57 | * @return the volume of this resource |
| 58 | */ |
| 59 | @SuppressWarnings("unchecked") |
| 60 | @Override |
| 61 | // TODO: consider receiving Class<T> as an argument. Which approach is convenient? |
| 62 | public <T> T volume() { |
| 63 | return (T) last(); |
| 64 | } |
| 65 | |
| 66 | @Override |
Sho SHIMIZU | 2a70451 | 2016-01-26 14:41:34 -0800 | [diff] [blame] | 67 | public List<Object> components() { |
Sho SHIMIZU | 42ac51f | 2016-01-27 12:59:31 -0800 | [diff] [blame^] | 68 | return id.components(); |
Sho SHIMIZU | 2a70451 | 2016-01-26 14:41:34 -0800 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | @Override |
| 72 | public Object last() { |
Sho SHIMIZU | 42ac51f | 2016-01-27 12:59:31 -0800 | [diff] [blame^] | 73 | if (id.components().isEmpty()) { |
Sho SHIMIZU | 2a70451 | 2016-01-26 14:41:34 -0800 | [diff] [blame] | 74 | return null; |
| 75 | } |
Sho SHIMIZU | 42ac51f | 2016-01-27 12:59:31 -0800 | [diff] [blame^] | 76 | return id.components().get(id.components().size() - 1); |
Sho SHIMIZU | 2a70451 | 2016-01-26 14:41:34 -0800 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | @Override |
| 80 | public DiscreteResource child(Object child) { |
| 81 | checkArgument(!(child instanceof Class<?>)); |
| 82 | |
Sho SHIMIZU | 34b55b6 | 2016-01-27 12:49:43 -0800 | [diff] [blame] | 83 | return new DiscreteResource(id.child(child)); |
Sho SHIMIZU | 2a70451 | 2016-01-26 14:41:34 -0800 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | @Override |
| 87 | public ContinuousResource child(Class<?> child, double value) { |
| 88 | return new ContinuousResource(id.child(child), value); |
| 89 | } |
| 90 | |
| 91 | @Override |
| 92 | public Optional<DiscreteResource> parent() { |
| 93 | return Optional.ofNullable(id.parent()).map(DiscreteResource::new); |
| 94 | } |
| 95 | |
| 96 | @Override |
Sho SHIMIZU | f33b893 | 2016-01-25 18:43:32 -0800 | [diff] [blame] | 97 | public int hashCode() { |
| 98 | // the value returing from volume() is excluded due to optimization |
Sho SHIMIZU | 34b55b6 | 2016-01-27 12:49:43 -0800 | [diff] [blame] | 99 | return id.hashCode(); |
Sho SHIMIZU | f33b893 | 2016-01-25 18:43:32 -0800 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | @Override |
| 103 | public boolean equals(Object obj) { |
| 104 | if (this == obj) { |
| 105 | return true; |
| 106 | } |
| 107 | if (obj == null || getClass() != obj.getClass()) { |
| 108 | return false; |
| 109 | } |
| 110 | final DiscreteResource other = (DiscreteResource) obj; |
| 111 | // the value returing from volume() is excluded due to optimization |
Sho SHIMIZU | 34b55b6 | 2016-01-27 12:49:43 -0800 | [diff] [blame] | 112 | return Objects.equals(this.id, other.id); |
Sho SHIMIZU | f33b893 | 2016-01-25 18:43:32 -0800 | [diff] [blame] | 113 | } |
Sho SHIMIZU | 2a70451 | 2016-01-26 14:41:34 -0800 | [diff] [blame] | 114 | |
| 115 | @Override |
| 116 | public String toString() { |
| 117 | return MoreObjects.toStringHelper(this) |
| 118 | .add("id", id) |
| 119 | .add("volume", volume()) |
| 120 | .toString(); |
| 121 | } |
Sho SHIMIZU | f33b893 | 2016-01-25 18:43:32 -0800 | [diff] [blame] | 122 | } |