blob: 7638f644de004aa01308296701acd039d5e8f5c9 [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 SHIMIZU76b30f72016-01-11 14:08:35 -080024
Sho SHIMIZU2d310222016-01-22 11:45:11 -080025import static com.google.common.base.Preconditions.checkArgument;
Sho SHIMIZU76b30f72016-01-11 14:08:35 -080026
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 SHIMIZU2d310222016-01-22 11:45:11 -080032public abstract class ResourceId {
Sho SHIMIZU2a704512016-01-26 14:41:34 -080033 static final DiscreteResourceId ROOT = new DiscreteResourceId();
Sho SHIMIZU76b30f72016-01-11 14:08:35 -080034
Sho SHIMIZU2a704512016-01-26 14:41:34 -080035 static DiscreteResourceId discrete(DeviceId device, Object... components) {
Sho SHIMIZUf33b8932016-01-25 18:43:32 -080036 return new DiscreteResourceId(ImmutableList.builder()
Sho SHIMIZU76b30f72016-01-11 14:08:35 -080037 .add(device)
38 .add(components)
39 .build());
40 }
41
Sho SHIMIZU2a704512016-01-26 14:41:34 -080042 static DiscreteResourceId discrete(DeviceId device, PortNumber port, Object... components) {
Sho SHIMIZUf33b8932016-01-25 18:43:32 -080043 return new DiscreteResourceId(ImmutableList.builder()
Sho SHIMIZU76b30f72016-01-11 14:08:35 -080044 .add(device)
45 .add(port)
46 .add(components)
47 .build());
48 }
49
Sho SHIMIZU2a704512016-01-26 14:41:34 -080050 static ContinuousResourceId continuous(DeviceId device, Object... components) {
Sho SHIMIZU2d310222016-01-22 11:45:11 -080051 Object last = components[components.length - 1];
52 checkArgument(last instanceof Class<?>);
53
Sho SHIMIZU2a704512016-01-26 14:41:34 -080054 return new ContinuousResourceId(ImmutableList.builder()
Sho SHIMIZU2d310222016-01-22 11:45:11 -080055 .add(device)
56 .add(Arrays.copyOfRange(components, 0, components.length - 1)), (Class<?>) last);
57 }
58
Sho SHIMIZU2a704512016-01-26 14:41:34 -080059 static ContinuousResourceId continuous(DeviceId device, PortNumber port, Object... components) {
Sho SHIMIZU2d310222016-01-22 11:45:11 -080060 Object last = components[components.length - 1];
61 checkArgument(last instanceof Class<?>);
62
Sho SHIMIZU2a704512016-01-26 14:41:34 -080063 return new ContinuousResourceId(ImmutableList.builder()
Sho SHIMIZU2d310222016-01-22 11:45:11 -080064 .add(device)
65 .add(port)
66 .add(Arrays.copyOfRange(components, 0, components.length - 1)), (Class<?>) last);
67 }
68
Sho SHIMIZU2a704512016-01-26 14:41:34 -080069 abstract DiscreteResourceId parent();
Sho SHIMIZU76b30f72016-01-11 14:08:35 -080070
Sho SHIMIZU2d310222016-01-22 11:45:11 -080071 /**
72 * Returns a resource ID of a child of this resource based on the specified object.
Sho SHIMIZU2a704512016-01-26 14:41:34 -080073 * If the given object is a {@link Class} instance, {@link IllegalArgumentException} is thrown.
Sho SHIMIZU2d310222016-01-22 11:45:11 -080074 *
75 * @param child the last component of the child
76 * @return a child resource ID
77 */
Sho SHIMIZU2a704512016-01-26 14:41:34 -080078 public abstract DiscreteResourceId child(Object child);
Sho SHIMIZU2d310222016-01-22 11:45:11 -080079
Sho SHIMIZU2a704512016-01-26 14:41:34 -080080 /**
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 SHIMIZU76b30f72016-01-11 14:08:35 -080087}