blob: 1be737ea297d5ab3bddd119499a1da4ded9b1036 [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 {
36 static final ResourceId ROOT = new Discrete();
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) {
41 return new Discrete(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) {
48 return new Discrete(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) {
75 return new Continuous(parentComponents
76 .add(last.getCanonicalName())
77 .build(), last.getSimpleName());
78 }
79
Sho SHIMIZU76b30f72016-01-11 14:08:35 -080080 private ResourceId(ImmutableList<Object> components) {
81 this.components = checkNotNull(components);
82 }
83
84 // for serializer
85 private ResourceId() {
86 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 SHIMIZU2d310222016-01-22 11:45:11 -080094 return new Discrete(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
101 * {@link Continuous}. Otherwise, it returns an instance of {@link Discrete}
102 * This method only work when the receiver is {@link Discrete}. Otherwise,
103 * 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) {
109 checkState(this instanceof Discrete);
110
111 if (child instanceof Class<?>) {
112 return continuous(ImmutableList.builder().addAll(components), (Class<?>) child);
113 } else {
114 return new Discrete(ImmutableList.builder()
115 .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
143 /**
144 * ResourceId for {@link Resource.Discrete}.
145 *
146 * Note: This class is exposed to the public, but intended to be used in the resource API
147 * implementation only. It is not for resource API user.
148 */
149 public static final class Discrete extends ResourceId {
150 private Discrete(ImmutableList<Object> components) {
151 super(components);
152 }
153
154 private Discrete() {
155 super();
156 }
157 }
158
159 /**
160 * ResourceId for {@link Resource.Continuous}
161 *
162 * Note: This class is exposed to the public, but intended to be used in the resource API
163 * implementation only. It is not for resource API user.
164 */
165 public static final class Continuous extends ResourceId {
166 // for printing purpose only (used in toString() implementation)
167 private final String name;
168
169 private Continuous(ImmutableList<Object> components, String name) {
170 super(components);
171 this.name = checkNotNull(name);
172 }
173
174 @Override
175 public String toString() {
176 // due to performance consideration, the value might need to be stored in a field
177 return ImmutableList.builder()
178 .addAll(components.subList(0, components.size() - 1))
179 .add(name)
180 .build().toString();
181 }
182 }
Sho SHIMIZU76b30f72016-01-11 14:08:35 -0800183}