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 | 7dac8a4 | 2016-01-27 12:04:39 -0800 | [diff] [blame^] | 24 | import java.util.Optional; |
Sho SHIMIZU | 76b30f7 | 2016-01-11 14:08:35 -0800 | [diff] [blame] | 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 | |
| 28 | /** |
| 29 | * Represents identifier of resource. |
| 30 | * This class is exposed to public, but intended to use only in ResourceStore implementations. |
| 31 | */ |
| 32 | @Beta |
Sho SHIMIZU | 2d31022 | 2016-01-22 11:45:11 -0800 | [diff] [blame] | 33 | public abstract class ResourceId { |
Sho SHIMIZU | 2a70451 | 2016-01-26 14:41:34 -0800 | [diff] [blame] | 34 | static final DiscreteResourceId ROOT = new DiscreteResourceId(); |
Sho SHIMIZU | 76b30f7 | 2016-01-11 14:08:35 -0800 | [diff] [blame] | 35 | |
Sho SHIMIZU | 2a70451 | 2016-01-26 14:41:34 -0800 | [diff] [blame] | 36 | static DiscreteResourceId discrete(DeviceId device, Object... components) { |
Sho SHIMIZU | f33b893 | 2016-01-25 18:43:32 -0800 | [diff] [blame] | 37 | return new DiscreteResourceId(ImmutableList.builder() |
Sho SHIMIZU | 76b30f7 | 2016-01-11 14:08:35 -0800 | [diff] [blame] | 38 | .add(device) |
| 39 | .add(components) |
| 40 | .build()); |
| 41 | } |
| 42 | |
Sho SHIMIZU | 2a70451 | 2016-01-26 14:41:34 -0800 | [diff] [blame] | 43 | static DiscreteResourceId discrete(DeviceId device, PortNumber port, Object... components) { |
Sho SHIMIZU | f33b893 | 2016-01-25 18:43:32 -0800 | [diff] [blame] | 44 | return new DiscreteResourceId(ImmutableList.builder() |
Sho SHIMIZU | 76b30f7 | 2016-01-11 14:08:35 -0800 | [diff] [blame] | 45 | .add(device) |
| 46 | .add(port) |
| 47 | .add(components) |
| 48 | .build()); |
| 49 | } |
| 50 | |
Sho SHIMIZU | 2a70451 | 2016-01-26 14:41:34 -0800 | [diff] [blame] | 51 | static ContinuousResourceId continuous(DeviceId device, Object... components) { |
Sho SHIMIZU | 2d31022 | 2016-01-22 11:45:11 -0800 | [diff] [blame] | 52 | Object last = components[components.length - 1]; |
| 53 | checkArgument(last instanceof Class<?>); |
| 54 | |
Sho SHIMIZU | 2a70451 | 2016-01-26 14:41:34 -0800 | [diff] [blame] | 55 | return new ContinuousResourceId(ImmutableList.builder() |
Sho SHIMIZU | 2d31022 | 2016-01-22 11:45:11 -0800 | [diff] [blame] | 56 | .add(device) |
| 57 | .add(Arrays.copyOfRange(components, 0, components.length - 1)), (Class<?>) last); |
| 58 | } |
| 59 | |
Sho SHIMIZU | 2a70451 | 2016-01-26 14:41:34 -0800 | [diff] [blame] | 60 | static ContinuousResourceId continuous(DeviceId device, PortNumber port, Object... components) { |
Sho SHIMIZU | 2d31022 | 2016-01-22 11:45:11 -0800 | [diff] [blame] | 61 | Object last = components[components.length - 1]; |
| 62 | checkArgument(last instanceof Class<?>); |
| 63 | |
Sho SHIMIZU | 2a70451 | 2016-01-26 14:41:34 -0800 | [diff] [blame] | 64 | return new ContinuousResourceId(ImmutableList.builder() |
Sho SHIMIZU | 2d31022 | 2016-01-22 11:45:11 -0800 | [diff] [blame] | 65 | .add(device) |
| 66 | .add(port) |
| 67 | .add(Arrays.copyOfRange(components, 0, components.length - 1)), (Class<?>) last); |
| 68 | } |
| 69 | |
Sho SHIMIZU | 7dac8a4 | 2016-01-27 12:04:39 -0800 | [diff] [blame^] | 70 | /** |
| 71 | * Returns the parent resource ID of this instance. |
| 72 | * |
| 73 | * @return the parent resource path of this instance. |
| 74 | * If there is no parent, empty instance will be returned. |
| 75 | */ |
| 76 | public abstract Optional<DiscreteResourceId> parent(); |
Sho SHIMIZU | 76b30f7 | 2016-01-11 14:08:35 -0800 | [diff] [blame] | 77 | |
Sho SHIMIZU | 2d31022 | 2016-01-22 11:45:11 -0800 | [diff] [blame] | 78 | /** |
| 79 | * Returns a resource ID of a child of this resource based on the specified object. |
Sho SHIMIZU | 2a70451 | 2016-01-26 14:41:34 -0800 | [diff] [blame] | 80 | * If the given object is a {@link Class} instance, {@link IllegalArgumentException} is thrown. |
Sho SHIMIZU | 2d31022 | 2016-01-22 11:45:11 -0800 | [diff] [blame] | 81 | * |
| 82 | * @param child the last component of the child |
| 83 | * @return a child resource ID |
| 84 | */ |
Sho SHIMIZU | 2a70451 | 2016-01-26 14:41:34 -0800 | [diff] [blame] | 85 | public abstract DiscreteResourceId child(Object child); |
Sho SHIMIZU | 2d31022 | 2016-01-22 11:45:11 -0800 | [diff] [blame] | 86 | |
Sho SHIMIZU | 2a70451 | 2016-01-26 14:41:34 -0800 | [diff] [blame] | 87 | /** |
| 88 | * Returns a resource ID of a child of this resource based on the specified object. |
| 89 | * |
| 90 | * @param child the last component of the child |
| 91 | * @return a child resource ID |
| 92 | */ |
| 93 | public abstract ContinuousResourceId child(Class<?> child); |
Sho SHIMIZU | 76b30f7 | 2016-01-11 14:08:35 -0800 | [diff] [blame] | 94 | } |