blob: 6f785df2e7746c8a98b03963a3e888841b3934fc [file] [log] [blame]
Sho SHIMIZU76b30f72016-01-11 14:08:35 -08001/*
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 */
16package org.onosproject.net.newresource;
17
18import com.google.common.annotations.Beta;
Sho SHIMIZU76b30f72016-01-11 14:08:35 -080019import com.google.common.collect.ImmutableList;
20import org.onosproject.net.DeviceId;
21import org.onosproject.net.PortNumber;
22
Sho SHIMIZU2d310222016-01-22 11:45:11 -080023import java.util.Arrays;
Sho SHIMIZU7dac8a42016-01-27 12:04:39 -080024import java.util.Optional;
Sho SHIMIZU76b30f72016-01-11 14:08:35 -080025
Sho SHIMIZU2d310222016-01-22 11:45:11 -080026import static com.google.common.base.Preconditions.checkArgument;
Sho SHIMIZU76b30f72016-01-11 14:08:35 -080027
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 SHIMIZU2d310222016-01-22 11:45:11 -080033public abstract class ResourceId {
Sho SHIMIZU2a704512016-01-26 14:41:34 -080034 static final DiscreteResourceId ROOT = new DiscreteResourceId();
Sho SHIMIZU76b30f72016-01-11 14:08:35 -080035
Sho SHIMIZU2a704512016-01-26 14:41:34 -080036 static DiscreteResourceId discrete(DeviceId device, Object... components) {
Sho SHIMIZUf33b8932016-01-25 18:43:32 -080037 return new DiscreteResourceId(ImmutableList.builder()
Sho SHIMIZU76b30f72016-01-11 14:08:35 -080038 .add(device)
39 .add(components)
40 .build());
41 }
42
Sho SHIMIZU2a704512016-01-26 14:41:34 -080043 static DiscreteResourceId discrete(DeviceId device, PortNumber port, Object... components) {
Sho SHIMIZUf33b8932016-01-25 18:43:32 -080044 return new DiscreteResourceId(ImmutableList.builder()
Sho SHIMIZU76b30f72016-01-11 14:08:35 -080045 .add(device)
46 .add(port)
47 .add(components)
48 .build());
49 }
50
Sho SHIMIZU2a704512016-01-26 14:41:34 -080051 static ContinuousResourceId continuous(DeviceId device, Object... components) {
Sho SHIMIZU2d310222016-01-22 11:45:11 -080052 Object last = components[components.length - 1];
53 checkArgument(last instanceof Class<?>);
54
Sho SHIMIZU2a704512016-01-26 14:41:34 -080055 return new ContinuousResourceId(ImmutableList.builder()
Sho SHIMIZU2d310222016-01-22 11:45:11 -080056 .add(device)
57 .add(Arrays.copyOfRange(components, 0, components.length - 1)), (Class<?>) last);
58 }
59
Sho SHIMIZU2a704512016-01-26 14:41:34 -080060 static ContinuousResourceId continuous(DeviceId device, PortNumber port, Object... components) {
Sho SHIMIZU2d310222016-01-22 11:45:11 -080061 Object last = components[components.length - 1];
62 checkArgument(last instanceof Class<?>);
63
Sho SHIMIZU2a704512016-01-26 14:41:34 -080064 return new ContinuousResourceId(ImmutableList.builder()
Sho SHIMIZU2d310222016-01-22 11:45:11 -080065 .add(device)
66 .add(port)
67 .add(Arrays.copyOfRange(components, 0, components.length - 1)), (Class<?>) last);
68 }
69
Sho SHIMIZU7dac8a42016-01-27 12:04:39 -080070 /**
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 SHIMIZU76b30f72016-01-11 14:08:35 -080077
Sho SHIMIZU2d310222016-01-22 11:45:11 -080078 /**
79 * Returns a resource ID of a child of this resource based on the specified object.
Sho SHIMIZU2a704512016-01-26 14:41:34 -080080 * If the given object is a {@link Class} instance, {@link IllegalArgumentException} is thrown.
Sho SHIMIZU2d310222016-01-22 11:45:11 -080081 *
82 * @param child the last component of the child
83 * @return a child resource ID
84 */
Sho SHIMIZU2a704512016-01-26 14:41:34 -080085 public abstract DiscreteResourceId child(Object child);
Sho SHIMIZU2d310222016-01-22 11:45:11 -080086
Sho SHIMIZU2a704512016-01-26 14:41:34 -080087 /**
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 SHIMIZU76b30f72016-01-11 14:08:35 -080094}