blob: 93c4472fd18cbd603e02b07f0fef3b14648a4a5a [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 -080024import java.util.Objects;
25
Sho SHIMIZU2d310222016-01-22 11:45:11 -080026import static com.google.common.base.Preconditions.checkArgument;
Sho SHIMIZU76b30f72016-01-11 14:08:35 -080027import static com.google.common.base.Preconditions.checkNotNull;
Sho SHIMIZU2d310222016-01-22 11:45:11 -080028import static com.google.common.base.Preconditions.checkState;
Sho SHIMIZU76b30f72016-01-11 14:08:35 -080029
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 SHIMIZU2d310222016-01-22 11:45:11 -080035public abstract class ResourceId {
Sho SHIMIZUf33b8932016-01-25 18:43:32 -080036 static final ResourceId ROOT = new DiscreteResourceId();
Sho SHIMIZU76b30f72016-01-11 14:08:35 -080037
38 final ImmutableList<Object> components;
39
Sho SHIMIZU2d310222016-01-22 11:45:11 -080040 static ResourceId discrete(DeviceId device, Object... components) {
Sho SHIMIZUf33b8932016-01-25 18:43:32 -080041 return new DiscreteResourceId(ImmutableList.builder()
Sho SHIMIZU76b30f72016-01-11 14:08:35 -080042 .add(device)
43 .add(components)
44 .build());
45 }
46
Sho SHIMIZU2d310222016-01-22 11:45:11 -080047 static ResourceId discrete(DeviceId device, PortNumber port, Object... components) {
Sho SHIMIZUf33b8932016-01-25 18:43:32 -080048 return new DiscreteResourceId(ImmutableList.builder()
Sho SHIMIZU76b30f72016-01-11 14:08:35 -080049 .add(device)
50 .add(port)
51 .add(components)
52 .build());
53 }
54
Sho SHIMIZU2d310222016-01-22 11:45:11 -080055 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) {
Sho SHIMIZUf33b8932016-01-25 18:43:32 -080075 return new ContinuousResourceId(parentComponents
Sho SHIMIZU2d310222016-01-22 11:45:11 -080076 .add(last.getCanonicalName())
77 .build(), last.getSimpleName());
78 }
79
Sho SHIMIZUf33b8932016-01-25 18:43:32 -080080 protected ResourceId(ImmutableList<Object> components) {
Sho SHIMIZU76b30f72016-01-11 14:08:35 -080081 this.components = checkNotNull(components);
82 }
83
84 // for serializer
Sho SHIMIZUf33b8932016-01-25 18:43:32 -080085 protected ResourceId() {
Sho SHIMIZU76b30f72016-01-11 14:08:35 -080086 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 SHIMIZUf33b8932016-01-25 18:43:32 -080094 return new DiscreteResourceId(components.subList(0, components.size() - 1));
Sho SHIMIZU76b30f72016-01-11 14:08:35 -080095 }
96 }
97
Sho SHIMIZU2d310222016-01-22 11:45:11 -080098 /**
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
Sho SHIMIZUf33b8932016-01-25 18:43:32 -0800101 * {@link ContinuousResourceId}. Otherwise, it returns an instance of {@link DiscreteResourceId}
102 * This method only work when the receiver is {@link DiscreteResourceId}. Otherwise,
Sho SHIMIZU2d310222016-01-22 11:45:11 -0800103 * 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) {
Sho SHIMIZUf33b8932016-01-25 18:43:32 -0800109 checkState(this instanceof DiscreteResourceId);
Sho SHIMIZU2d310222016-01-22 11:45:11 -0800110
111 if (child instanceof Class<?>) {
112 return continuous(ImmutableList.builder().addAll(components), (Class<?>) child);
113 } else {
Sho SHIMIZUf33b8932016-01-25 18:43:32 -0800114 return new DiscreteResourceId(ImmutableList.builder()
Sho SHIMIZU2d310222016-01-22 11:45:11 -0800115 .addAll(components)
116 .add(child)
117 .build());
118 }
Sho SHIMIZU76b30f72016-01-11 14:08:35 -0800119 }
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 SHIMIZU2d310222016-01-22 11:45:11 -0800131 if (obj == null || getClass() != obj.getClass()) {
Sho SHIMIZU76b30f72016-01-11 14:08:35 -0800132 return false;
133 }
Sho SHIMIZU2d310222016-01-22 11:45:11 -0800134 final ResourceId other = (ResourceId) obj;
Sho SHIMIZU76b30f72016-01-11 14:08:35 -0800135 return Objects.equals(this.components, other.components);
136 }
137
138 @Override
139 public String toString() {
HIGUCHI Yuta6f828c32016-01-20 18:11:05 -0800140 return components.toString();
Sho SHIMIZU76b30f72016-01-11 14:08:35 -0800141 }
Sho SHIMIZU2d310222016-01-22 11:45:11 -0800142
Sho SHIMIZU76b30f72016-01-11 14:08:35 -0800143}