Sho SHIMIZU | 76b30f7 | 2016-01-11 14:08:35 -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 | 76b30f7 | 2016-01-11 14:08:35 -0800 | [diff] [blame] | 19 | import com.google.common.collect.ImmutableList; |
| 20 | import org.onosproject.net.DeviceId; |
| 21 | import org.onosproject.net.PortNumber; |
| 22 | |
Sho SHIMIZU | 2d31022 | 2016-01-22 11:45:11 -0800 | [diff] [blame^] | 23 | import java.util.Arrays; |
Sho SHIMIZU | 76b30f7 | 2016-01-11 14:08:35 -0800 | [diff] [blame] | 24 | import java.util.Objects; |
| 25 | |
Sho SHIMIZU | 2d31022 | 2016-01-22 11:45:11 -0800 | [diff] [blame^] | 26 | import static com.google.common.base.Preconditions.checkArgument; |
Sho SHIMIZU | 76b30f7 | 2016-01-11 14:08:35 -0800 | [diff] [blame] | 27 | import static com.google.common.base.Preconditions.checkNotNull; |
Sho SHIMIZU | 2d31022 | 2016-01-22 11:45:11 -0800 | [diff] [blame^] | 28 | import static com.google.common.base.Preconditions.checkState; |
Sho SHIMIZU | 76b30f7 | 2016-01-11 14:08:35 -0800 | [diff] [blame] | 29 | |
| 30 | /** |
| 31 | * Represents identifier of resource. |
| 32 | * This class is exposed to public, but intended to use only in ResourceStore implementations. |
| 33 | */ |
| 34 | @Beta |
Sho SHIMIZU | 2d31022 | 2016-01-22 11:45:11 -0800 | [diff] [blame^] | 35 | public abstract class ResourceId { |
| 36 | static final ResourceId ROOT = new Discrete(); |
Sho SHIMIZU | 76b30f7 | 2016-01-11 14:08:35 -0800 | [diff] [blame] | 37 | |
| 38 | final ImmutableList<Object> components; |
| 39 | |
Sho SHIMIZU | 2d31022 | 2016-01-22 11:45:11 -0800 | [diff] [blame^] | 40 | static ResourceId discrete(DeviceId device, Object... components) { |
| 41 | return new Discrete(ImmutableList.builder() |
Sho SHIMIZU | 76b30f7 | 2016-01-11 14:08:35 -0800 | [diff] [blame] | 42 | .add(device) |
| 43 | .add(components) |
| 44 | .build()); |
| 45 | } |
| 46 | |
Sho SHIMIZU | 2d31022 | 2016-01-22 11:45:11 -0800 | [diff] [blame^] | 47 | static ResourceId discrete(DeviceId device, PortNumber port, Object... components) { |
| 48 | return new Discrete(ImmutableList.builder() |
Sho SHIMIZU | 76b30f7 | 2016-01-11 14:08:35 -0800 | [diff] [blame] | 49 | .add(device) |
| 50 | .add(port) |
| 51 | .add(components) |
| 52 | .build()); |
| 53 | } |
| 54 | |
Sho SHIMIZU | 2d31022 | 2016-01-22 11:45:11 -0800 | [diff] [blame^] | 55 | static ResourceId continuous(DeviceId device, Object... components) { |
| 56 | Object last = components[components.length - 1]; |
| 57 | checkArgument(last instanceof Class<?>); |
| 58 | |
| 59 | return continuous(ImmutableList.builder() |
| 60 | .add(device) |
| 61 | .add(Arrays.copyOfRange(components, 0, components.length - 1)), (Class<?>) last); |
| 62 | } |
| 63 | |
| 64 | static ResourceId continuous(DeviceId device, PortNumber port, Object... components) { |
| 65 | Object last = components[components.length - 1]; |
| 66 | checkArgument(last instanceof Class<?>); |
| 67 | |
| 68 | return continuous(ImmutableList.builder() |
| 69 | .add(device) |
| 70 | .add(port) |
| 71 | .add(Arrays.copyOfRange(components, 0, components.length - 1)), (Class<?>) last); |
| 72 | } |
| 73 | |
| 74 | private static ResourceId continuous(ImmutableList.Builder<Object> parentComponents, Class<?> last) { |
| 75 | return new Continuous(parentComponents |
| 76 | .add(last.getCanonicalName()) |
| 77 | .build(), last.getSimpleName()); |
| 78 | } |
| 79 | |
Sho SHIMIZU | 76b30f7 | 2016-01-11 14:08:35 -0800 | [diff] [blame] | 80 | private ResourceId(ImmutableList<Object> components) { |
| 81 | this.components = checkNotNull(components); |
| 82 | } |
| 83 | |
| 84 | // for serializer |
| 85 | private ResourceId() { |
| 86 | this.components = ImmutableList.of(); |
| 87 | } |
| 88 | |
| 89 | // IndexOutOfBoundsException is raised when the instance is equal to ROOT |
| 90 | ResourceId parent() { |
| 91 | if (components.size() == 1) { |
| 92 | return ROOT; |
| 93 | } else { |
Sho SHIMIZU | 2d31022 | 2016-01-22 11:45:11 -0800 | [diff] [blame^] | 94 | return new Discrete(components.subList(0, components.size() - 1)); |
Sho SHIMIZU | 76b30f7 | 2016-01-11 14:08:35 -0800 | [diff] [blame] | 95 | } |
| 96 | } |
| 97 | |
Sho SHIMIZU | 2d31022 | 2016-01-22 11:45:11 -0800 | [diff] [blame^] | 98 | /** |
| 99 | * Returns a resource ID of a child of this resource based on the specified object. |
| 100 | * If the argument is an instance of {@link Class}, this method returns an instance of |
| 101 | * {@link Continuous}. Otherwise, it returns an instance of {@link Discrete} |
| 102 | * This method only work when the receiver is {@link Discrete}. Otherwise, |
| 103 | * this method throws an exception. |
| 104 | * |
| 105 | * @param child the last component of the child |
| 106 | * @return a child resource ID |
| 107 | */ |
| 108 | public ResourceId child(Object child) { |
| 109 | checkState(this instanceof Discrete); |
| 110 | |
| 111 | if (child instanceof Class<?>) { |
| 112 | return continuous(ImmutableList.builder().addAll(components), (Class<?>) child); |
| 113 | } else { |
| 114 | return new Discrete(ImmutableList.builder() |
| 115 | .addAll(components) |
| 116 | .add(child) |
| 117 | .build()); |
| 118 | } |
Sho SHIMIZU | 76b30f7 | 2016-01-11 14:08:35 -0800 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | @Override |
| 122 | public int hashCode() { |
| 123 | return components.hashCode(); |
| 124 | } |
| 125 | |
| 126 | @Override |
| 127 | public boolean equals(Object obj) { |
| 128 | if (this == obj) { |
| 129 | return true; |
| 130 | } |
Sho SHIMIZU | 2d31022 | 2016-01-22 11:45:11 -0800 | [diff] [blame^] | 131 | if (obj == null || getClass() != obj.getClass()) { |
Sho SHIMIZU | 76b30f7 | 2016-01-11 14:08:35 -0800 | [diff] [blame] | 132 | return false; |
| 133 | } |
Sho SHIMIZU | 2d31022 | 2016-01-22 11:45:11 -0800 | [diff] [blame^] | 134 | final ResourceId other = (ResourceId) obj; |
Sho SHIMIZU | 76b30f7 | 2016-01-11 14:08:35 -0800 | [diff] [blame] | 135 | return Objects.equals(this.components, other.components); |
| 136 | } |
| 137 | |
| 138 | @Override |
| 139 | public String toString() { |
HIGUCHI Yuta | 6f828c3 | 2016-01-20 18:11:05 -0800 | [diff] [blame] | 140 | return components.toString(); |
Sho SHIMIZU | 76b30f7 | 2016-01-11 14:08:35 -0800 | [diff] [blame] | 141 | } |
Sho SHIMIZU | 2d31022 | 2016-01-22 11:45:11 -0800 | [diff] [blame^] | 142 | |
| 143 | /** |
| 144 | * ResourceId for {@link Resource.Discrete}. |
| 145 | * |
| 146 | * Note: This class is exposed to the public, but intended to be used in the resource API |
| 147 | * implementation only. It is not for resource API user. |
| 148 | */ |
| 149 | public static final class Discrete extends ResourceId { |
| 150 | private Discrete(ImmutableList<Object> components) { |
| 151 | super(components); |
| 152 | } |
| 153 | |
| 154 | private Discrete() { |
| 155 | super(); |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * ResourceId for {@link Resource.Continuous} |
| 161 | * |
| 162 | * Note: This class is exposed to the public, but intended to be used in the resource API |
| 163 | * implementation only. It is not for resource API user. |
| 164 | */ |
| 165 | public static final class Continuous extends ResourceId { |
| 166 | // for printing purpose only (used in toString() implementation) |
| 167 | private final String name; |
| 168 | |
| 169 | private Continuous(ImmutableList<Object> components, String name) { |
| 170 | super(components); |
| 171 | this.name = checkNotNull(name); |
| 172 | } |
| 173 | |
| 174 | @Override |
| 175 | public String toString() { |
| 176 | // due to performance consideration, the value might need to be stored in a field |
| 177 | return ImmutableList.builder() |
| 178 | .addAll(components.subList(0, components.size() - 1)) |
| 179 | .add(name) |
| 180 | .build().toString(); |
| 181 | } |
| 182 | } |
Sho SHIMIZU | 76b30f7 | 2016-01-11 14:08:35 -0800 | [diff] [blame] | 183 | } |