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