Sho SHIMIZU | 460b972 | 2016-01-28 10:48:26 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Sho SHIMIZU | 460b972 | 2016-01-28 10:48:26 -0800 | [diff] [blame] | 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 | */ |
Sho SHIMIZU | e18cb12 | 2016-02-22 21:04:56 -0800 | [diff] [blame] | 16 | package org.onosproject.net.resource; |
Sho SHIMIZU | 460b972 | 2016-01-28 10:48:26 -0800 | [diff] [blame] | 17 | |
| 18 | import com.google.common.annotations.Beta; |
| 19 | import com.google.common.collect.ImmutableList; |
| 20 | import org.onosproject.net.DeviceId; |
| 21 | import org.onosproject.net.PortNumber; |
| 22 | |
| 23 | import java.util.Arrays; |
| 24 | |
| 25 | import static com.google.common.base.Preconditions.checkArgument; |
| 26 | import static com.google.common.base.Preconditions.checkNotNull; |
| 27 | |
| 28 | /** |
| 29 | * Utility class for resource related classes. |
| 30 | */ |
| 31 | @Beta |
| 32 | public final class Resources { |
| 33 | // public construction is prohibited |
| 34 | private Resources() {} |
| 35 | |
| 36 | /** |
| 37 | * Create a factory for discrete-type with the specified resource ID. |
| 38 | * |
| 39 | * @param id resource ID |
| 40 | * @return {@link DiscreteFactory} |
| 41 | */ |
| 42 | public static DiscreteFactory discrete(DiscreteResourceId id) { |
| 43 | checkNotNull(id); |
| 44 | |
| 45 | return new DiscreteFactory(id); |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Creates a factory for discrete-type with the specified parent ID and child. |
| 50 | * |
| 51 | * @param parent ID of the parent |
| 52 | * @param child child |
| 53 | * @return {@link DiscreteFactory} |
| 54 | */ |
| 55 | public static DiscreteFactory discrete(DiscreteResourceId parent, Object child) { |
| 56 | checkNotNull(parent); |
| 57 | checkNotNull(child); |
| 58 | checkArgument(!(child instanceof Class<?>)); |
| 59 | |
| 60 | return new DiscreteFactory(new DiscreteResourceId(ImmutableList.builder() |
| 61 | .addAll(parent.components()) |
| 62 | .add(child) |
| 63 | .build())); |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Create a factory for discrete-type with the specified device ID. |
| 68 | * |
| 69 | * @param device device ID |
| 70 | * @return {@link DiscreteFactory} |
| 71 | */ |
| 72 | public static DiscreteFactory discrete(DeviceId device) { |
| 73 | checkNotNull(device); |
| 74 | |
| 75 | return new DiscreteFactory(new DiscreteResourceId(ImmutableList.of(device))); |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Create a factory for discrete-type with the specified device ID and components. |
| 80 | * |
| 81 | * @param device device ID |
| 82 | * @param components resource ID components other than the device ID |
| 83 | * @return {@link DiscreteFactory} |
| 84 | */ |
| 85 | public static DiscreteFactory discrete(DeviceId device, Object... components) { |
| 86 | checkNotNull(device); |
| 87 | checkNotNull(components); |
| 88 | |
| 89 | return new DiscreteFactory(new DiscreteResourceId(ImmutableList.builder() |
| 90 | .add(device) |
| 91 | .add(components) |
| 92 | .build())); |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Create a factory for discrete-type with the specified device ID, port number and components. |
| 97 | * |
| 98 | * @param device device ID |
| 99 | * @param port port number |
| 100 | * @param components resource ID components other than the device ID and port number |
| 101 | * @return {@link DiscreteFactory} |
| 102 | */ |
| 103 | public static DiscreteFactory discrete(DeviceId device, PortNumber port, Object... components) { |
| 104 | checkNotNull(device); |
| 105 | checkNotNull(port); |
| 106 | checkNotNull(components); |
| 107 | |
| 108 | return new DiscreteFactory(new DiscreteResourceId(ImmutableList.builder() |
| 109 | .add(device) |
| 110 | .add(port) |
| 111 | .add(components) |
| 112 | .build())); |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Create a factory for continuous-type with the specified resource ID. |
| 117 | * |
| 118 | * @param id resource ID |
| 119 | * @return {@link ContinuousFactory} |
| 120 | */ |
Sho SHIMIZU | 6a717dc | 2016-01-29 16:18:47 -0800 | [diff] [blame] | 121 | static ContinuousFactory continuous(ContinuousResourceId id) { |
Sho SHIMIZU | 460b972 | 2016-01-28 10:48:26 -0800 | [diff] [blame] | 122 | checkNotNull(id); |
| 123 | |
| 124 | return new ContinuousFactory(id); |
| 125 | } |
| 126 | |
| 127 | /** |
Sho SHIMIZU | 26a78df | 2016-01-29 11:59:26 -0800 | [diff] [blame] | 128 | * Creates a factory for continuous-type with the specified parent ID and child. |
Sho SHIMIZU | 460b972 | 2016-01-28 10:48:26 -0800 | [diff] [blame] | 129 | * |
| 130 | * @param parent ID of the parent |
| 131 | * @param child child |
| 132 | * @return {@link ContinuousFactory} |
| 133 | */ |
Sho SHIMIZU | 6a717dc | 2016-01-29 16:18:47 -0800 | [diff] [blame] | 134 | static ContinuousFactory continuous(DiscreteResourceId parent, Class<?> child) { |
Sho SHIMIZU | 460b972 | 2016-01-28 10:48:26 -0800 | [diff] [blame] | 135 | checkNotNull(parent); |
| 136 | checkNotNull(child); |
| 137 | |
| 138 | return new ContinuousFactory(new ContinuousResourceId(ImmutableList.builder() |
| 139 | .addAll(parent.components()), child)); |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Create a factory for continuous-type with the specified device ID and type. |
| 144 | * |
| 145 | * @param device device ID |
| 146 | * @param cls type of resource the returned factory will create |
| 147 | * @return {@link ContinuousFactory} |
| 148 | */ |
| 149 | public static ContinuousFactory continuous(DeviceId device, Class<?> cls) { |
| 150 | checkNotNull(device); |
| 151 | checkNotNull(cls); |
| 152 | |
| 153 | return new ContinuousFactory(new ContinuousResourceId(ImmutableList.builder().add(device), cls)); |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * Create a factory for continuous-type with the specified device ID and components. |
| 158 | * The last element of the components must be a {@link Class} instance. Otherwise, |
| 159 | * an {@link IllegalArgumentException} is thrown. |
| 160 | * |
| 161 | * @param device device ID |
| 162 | * @param components resource ID components other than the device ID. |
| 163 | * @return {@link ContinuousFactory} |
| 164 | */ |
| 165 | public static ContinuousFactory continuous(DeviceId device, Object... components) { |
| 166 | checkNotNull(device); |
| 167 | checkNotNull(components); |
| 168 | checkArgument(components.length > 1); |
| 169 | |
| 170 | Object last = components[components.length - 1]; |
| 171 | checkArgument(last instanceof Class<?>); |
| 172 | |
| 173 | return new ContinuousFactory(new ContinuousResourceId(ImmutableList.builder() |
| 174 | .add(device) |
| 175 | .add(Arrays.copyOfRange(components, 0, components.length - 1)), (Class<?>) last)); |
| 176 | } |
| 177 | |
| 178 | /** |
| 179 | * Create a factory for continuous-type with the specified device ID, port number and type. |
| 180 | * |
| 181 | * @param device device ID |
| 182 | * @param port port number |
| 183 | * @param cls type of resource the returned factory will create |
| 184 | * @return {@link ContinuousFactory} |
| 185 | */ |
| 186 | public static ContinuousFactory continuous(DeviceId device, PortNumber port, Class<?> cls) { |
| 187 | checkNotNull(device); |
| 188 | checkNotNull(port); |
| 189 | checkNotNull(cls); |
| 190 | |
| 191 | return new ContinuousFactory(new ContinuousResourceId(ImmutableList.builder() |
| 192 | .add(device) |
| 193 | .add(port), cls)); |
| 194 | } |
| 195 | |
| 196 | /** |
| 197 | * Create a factory for continuous-type with the specified device ID and components. |
| 198 | * The last element of the components must be a {@link Class} instance. Otherwise, |
| 199 | * an {@link IllegalArgumentException} is thrown. |
| 200 | * |
| 201 | * @param device device ID |
| 202 | * @param port port number |
| 203 | * @param components resource ID components other than the device ID and port number. |
| 204 | * @return {@link ContinuousFactory} |
| 205 | */ |
| 206 | public static ContinuousFactory continuous(DeviceId device, PortNumber port, Object... components) { |
| 207 | checkNotNull(device); |
| 208 | checkNotNull(port); |
| 209 | checkNotNull(components); |
| 210 | checkArgument(components.length > 1); |
| 211 | |
| 212 | Object last = components[components.length - 1]; |
| 213 | checkArgument(last instanceof Class<?>); |
| 214 | |
| 215 | return new ContinuousFactory(new ContinuousResourceId(ImmutableList.builder() |
| 216 | .add(device) |
| 217 | .add(port) |
| 218 | .add(Arrays.copyOfRange(components, 0, components.length - 1)), (Class<?>) last)); |
| 219 | } |
| 220 | } |