Thomas Vachuska | 83e090e | 2014-10-22 14:25:35 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Laboratory |
Thomas Vachuska | 83e090e | 2014-10-22 14:25:35 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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 |
Thomas Vachuska | 83e090e | 2014-10-22 14:25:35 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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. |
Thomas Vachuska | 83e090e | 2014-10-22 14:25:35 -0700 | [diff] [blame] | 15 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.net.intent; |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 17 | |
Brian O'Connor | 9476fa1 | 2015-06-25 15:17:17 -0400 | [diff] [blame] | 18 | import com.google.common.annotations.Beta; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 19 | import org.onosproject.core.ApplicationId; |
| 20 | import org.onosproject.core.IdGenerator; |
| 21 | import org.onosproject.net.NetworkResource; |
Luca Prete | 670ac5d | 2017-02-03 15:55:43 -0800 | [diff] [blame] | 22 | import org.onosproject.net.ResourceGroup; |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 23 | |
Jonathan Hart | b14221c | 2016-03-07 09:55:50 -0800 | [diff] [blame] | 24 | import java.util.Collection; |
| 25 | import java.util.Objects; |
| 26 | |
Ray Milkey | c24cde3 | 2015-03-10 18:20:18 -0700 | [diff] [blame] | 27 | import static com.google.common.base.Preconditions.checkArgument; |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 28 | import static com.google.common.base.Preconditions.checkNotNull; |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 29 | import static com.google.common.base.Preconditions.checkState; |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 30 | |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 31 | /** |
| 32 | * Abstraction of an application level intent. |
Thomas Vachuska | 4b42077 | 2014-10-30 16:46:17 -0700 | [diff] [blame] | 33 | * <p> |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 34 | * Make sure that an Intent should be immutable when a new type is defined. |
Thomas Vachuska | 4b42077 | 2014-10-30 16:46:17 -0700 | [diff] [blame] | 35 | * </p> |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 36 | */ |
Brian O'Connor | 9476fa1 | 2015-06-25 15:17:17 -0400 | [diff] [blame] | 37 | @Beta |
Sho SHIMIZU | 5e5d4aa | 2015-01-26 16:12:11 -0800 | [diff] [blame] | 38 | public abstract class Intent { |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 39 | |
| 40 | private final IntentId id; |
Brian O'Connor | ea4d7d1 | 2015-01-28 16:37:46 -0800 | [diff] [blame] | 41 | |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 42 | private final ApplicationId appId; |
Ray Milkey | 5b3717e | 2015-02-05 11:44:08 -0800 | [diff] [blame] | 43 | private final Key key; |
Brian O'Connor | ea4d7d1 | 2015-01-28 16:37:46 -0800 | [diff] [blame] | 44 | |
Ray Milkey | c24cde3 | 2015-03-10 18:20:18 -0700 | [diff] [blame] | 45 | private final int priority; |
| 46 | public static final int DEFAULT_INTENT_PRIORITY = 100; |
| 47 | public static final int MAX_PRIORITY = (1 << 16) - 1; |
| 48 | public static final int MIN_PRIORITY = 1; |
| 49 | |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 50 | private final Collection<NetworkResource> resources; |
Luca Prete | 670ac5d | 2017-02-03 15:55:43 -0800 | [diff] [blame] | 51 | private final ResourceGroup resourceGroup; |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 52 | |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 53 | private static IdGenerator idGenerator; |
| 54 | |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 55 | /** |
| 56 | * Constructor for serializer. |
| 57 | */ |
| 58 | protected Intent() { |
| 59 | this.id = null; |
| 60 | this.appId = null; |
Brian O'Connor | ea4d7d1 | 2015-01-28 16:37:46 -0800 | [diff] [blame] | 61 | this.key = null; |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 62 | this.resources = null; |
Ray Milkey | c24cde3 | 2015-03-10 18:20:18 -0700 | [diff] [blame] | 63 | this.priority = DEFAULT_INTENT_PRIORITY; |
Luca Prete | 670ac5d | 2017-02-03 15:55:43 -0800 | [diff] [blame] | 64 | this.resourceGroup = null; |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Creates a new intent. |
Thomas Vachuska | 6519e6f | 2015-03-11 02:29:31 -0700 | [diff] [blame] | 69 | * @param appId application identifier |
Thomas Vachuska | 6519e6f | 2015-03-11 02:29:31 -0700 | [diff] [blame] | 70 | * @param key optional key |
| 71 | * @param resources required network resources (optional) |
| 72 | * @param priority flow rule priority |
Luca Prete | 670ac5d | 2017-02-03 15:55:43 -0800 | [diff] [blame] | 73 | * @deprecated 1.9.1 |
Thomas Vachuska | 6519e6f | 2015-03-11 02:29:31 -0700 | [diff] [blame] | 74 | */ |
Luca Prete | 670ac5d | 2017-02-03 15:55:43 -0800 | [diff] [blame] | 75 | @Deprecated |
Brian O'Connor | ea4d7d1 | 2015-01-28 16:37:46 -0800 | [diff] [blame] | 76 | protected Intent(ApplicationId appId, |
Ray Milkey | 5b3717e | 2015-02-05 11:44:08 -0800 | [diff] [blame] | 77 | Key key, |
Ray Milkey | c24cde3 | 2015-03-10 18:20:18 -0700 | [diff] [blame] | 78 | Collection<NetworkResource> resources, |
| 79 | int priority) { |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 80 | checkState(idGenerator != null, "Id generator is not bound."); |
Ray Milkey | c24cde3 | 2015-03-10 18:20:18 -0700 | [diff] [blame] | 81 | checkArgument(priority <= MAX_PRIORITY && priority >= MIN_PRIORITY); |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 82 | this.id = IntentId.valueOf(idGenerator.getNewId()); |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 83 | this.appId = checkNotNull(appId, "Application ID cannot be null"); |
Ray Milkey | 5b3717e | 2015-02-05 11:44:08 -0800 | [diff] [blame] | 84 | this.key = (key != null) ? key : Key.of(id.fingerprint(), appId); |
Ray Milkey | c24cde3 | 2015-03-10 18:20:18 -0700 | [diff] [blame] | 85 | this.priority = priority; |
Sho SHIMIZU | d7d1800 | 2015-01-21 14:37:14 -0800 | [diff] [blame] | 86 | this.resources = checkNotNull(resources); |
Luca Prete | 670ac5d | 2017-02-03 15:55:43 -0800 | [diff] [blame] | 87 | this.resourceGroup = null; |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Creates a new intent. |
| 92 | * @param appId application identifier |
| 93 | * @param key optional key |
| 94 | * @param resources required network resources (optional) |
| 95 | * @param priority flow rule priority |
| 96 | * @param resourceGroup the resource group for intent |
| 97 | */ |
| 98 | protected Intent(ApplicationId appId, |
| 99 | Key key, |
| 100 | Collection<NetworkResource> resources, |
| 101 | int priority, |
| 102 | ResourceGroup resourceGroup) { |
| 103 | checkState(idGenerator != null, "Id generator is not bound."); |
| 104 | checkArgument(priority <= MAX_PRIORITY && priority >= MIN_PRIORITY); |
| 105 | this.id = IntentId.valueOf(idGenerator.getNewId()); |
| 106 | this.appId = checkNotNull(appId, "Application ID cannot be null"); |
| 107 | this.key = (key != null) ? key : Key.of(id.fingerprint(), appId); |
| 108 | this.priority = priority; |
| 109 | this.resources = checkNotNull(resources); |
| 110 | this.resourceGroup = resourceGroup; |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 111 | } |
| 112 | |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 113 | /** |
Ray Milkey | 3e3ec5f | 2015-03-17 17:00:38 -0700 | [diff] [blame] | 114 | * Abstract builder for intents. |
| 115 | */ |
| 116 | public abstract static class Builder { |
| 117 | protected ApplicationId appId; |
| 118 | protected Key key; |
| 119 | protected int priority = Intent.DEFAULT_INTENT_PRIORITY; |
Yuta HIGUCHI | 6cad179 | 2016-11-07 15:03:36 -0800 | [diff] [blame] | 120 | protected Collection<NetworkResource> resources; |
Luca Prete | 670ac5d | 2017-02-03 15:55:43 -0800 | [diff] [blame] | 121 | protected ResourceGroup resourceGroup; |
Ray Milkey | 3e3ec5f | 2015-03-17 17:00:38 -0700 | [diff] [blame] | 122 | |
| 123 | /** |
Jonathan Hart | b14221c | 2016-03-07 09:55:50 -0800 | [diff] [blame] | 124 | * Creates a new empty builder. |
| 125 | */ |
| 126 | protected Builder() { |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Creates a new builder pre-populated with the information in the given |
| 131 | * intent. |
| 132 | * |
| 133 | * @param intent initial intent |
| 134 | */ |
| 135 | protected Builder(Intent intent) { |
| 136 | this.appId(intent.appId()) |
| 137 | .key(intent.key()) |
Luca Prete | 670ac5d | 2017-02-03 15:55:43 -0800 | [diff] [blame] | 138 | .priority(intent.priority()) |
| 139 | .resourceGroup(intent.resourceGroup()); |
Jonathan Hart | b14221c | 2016-03-07 09:55:50 -0800 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | /** |
Ray Milkey | 3e3ec5f | 2015-03-17 17:00:38 -0700 | [diff] [blame] | 143 | * Sets the application id for the intent that will be built. |
| 144 | * |
| 145 | * @param appId application id to use for built intent |
| 146 | * @return this builder |
| 147 | */ |
| 148 | public Builder appId(ApplicationId appId) { |
| 149 | this.appId = appId; |
| 150 | return this; |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * Sets the key for the intent that will be built. |
| 155 | * |
| 156 | * @param key key to use for built intent |
| 157 | * @return this builder |
| 158 | */ |
| 159 | public Builder key(Key key) { |
| 160 | this.key = key; |
| 161 | return this; |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * Sets the priority for the intent that will be built. |
| 166 | * |
| 167 | * @param priority priority to use for built intent |
| 168 | * @return this builder |
| 169 | */ |
| 170 | public Builder priority(int priority) { |
| 171 | this.priority = priority; |
| 172 | return this; |
| 173 | } |
| 174 | |
Yuta HIGUCHI | 6cad179 | 2016-11-07 15:03:36 -0800 | [diff] [blame] | 175 | /** |
| 176 | * Sets the collection of resources required for this intent. |
| 177 | * |
| 178 | * @param resources collection of resources |
| 179 | * @return this builder |
| 180 | */ |
| 181 | public Builder resources(Collection<NetworkResource> resources) { |
| 182 | this.resources = resources; |
| 183 | return this; |
| 184 | } |
Luca Prete | 670ac5d | 2017-02-03 15:55:43 -0800 | [diff] [blame] | 185 | |
| 186 | /** |
| 187 | * Sets the resource group for this intent. |
| 188 | * |
| 189 | * @param resourceGroup the resource group |
| 190 | * @return this builder |
| 191 | */ |
| 192 | public Builder resourceGroup(ResourceGroup resourceGroup) { |
| 193 | this.resourceGroup = resourceGroup; |
| 194 | return this; |
| 195 | } |
Ray Milkey | 3e3ec5f | 2015-03-17 17:00:38 -0700 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | /** |
Yuta HIGUCHI | 652f27f | 2016-10-31 16:54:30 -0700 | [diff] [blame] | 199 | * Returns the intent object identifier. |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 200 | * |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 201 | * @return intent fingerprint |
| 202 | */ |
| 203 | public IntentId id() { |
| 204 | return id; |
| 205 | } |
| 206 | |
| 207 | /** |
| 208 | * Returns the identifier of the application that requested the intent. |
| 209 | * |
| 210 | * @return application identifier |
| 211 | */ |
| 212 | public ApplicationId appId() { |
| 213 | return appId; |
| 214 | } |
| 215 | |
| 216 | /** |
Ray Milkey | c24cde3 | 2015-03-10 18:20:18 -0700 | [diff] [blame] | 217 | * Returns the priority of the intent. |
| 218 | * |
| 219 | * @return intent priority |
| 220 | */ |
| 221 | public int priority() { |
| 222 | return priority; |
| 223 | } |
| 224 | |
| 225 | /** |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 226 | * Returns the collection of resources required for this intent. |
| 227 | * |
| 228 | * @return collection of resources; may be null |
| 229 | */ |
| 230 | public Collection<NetworkResource> resources() { |
| 231 | return resources; |
| 232 | } |
| 233 | |
| 234 | /** |
Luca Prete | 670ac5d | 2017-02-03 15:55:43 -0800 | [diff] [blame] | 235 | * Returns the resource group for this intent. |
| 236 | * |
| 237 | * @return the resource group; may be null |
| 238 | */ |
| 239 | public ResourceGroup resourceGroup() { |
| 240 | return resourceGroup; |
| 241 | } |
| 242 | |
| 243 | /** |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 244 | * Indicates whether or not the intent is installable. |
| 245 | * |
| 246 | * @return true if installable |
| 247 | */ |
| 248 | public boolean isInstallable() { |
| 249 | return false; |
| 250 | } |
| 251 | |
| 252 | @Override |
Thomas Vachuska | 46c07ad | 2014-10-21 16:01:01 -0700 | [diff] [blame] | 253 | public final int hashCode() { |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 254 | return id.hashCode(); |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | @Override |
Thomas Vachuska | 46c07ad | 2014-10-21 16:01:01 -0700 | [diff] [blame] | 258 | public final boolean equals(Object obj) { |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 259 | if (this == obj) { |
| 260 | return true; |
| 261 | } |
| 262 | if (obj == null || getClass() != obj.getClass()) { |
| 263 | return false; |
| 264 | } |
| 265 | final Intent other = (Intent) obj; |
Sho SHIMIZU | 2389523 | 2014-11-25 15:59:50 -0800 | [diff] [blame] | 266 | return this.id().equals(other.id()); |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 267 | } |
| 268 | |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 269 | /** |
| 270 | * Binds an id generator for unique intent id generation. |
| 271 | * |
| 272 | * Note: A generator cannot be bound if there is already a generator bound. |
Thomas Vachuska | 6519e6f | 2015-03-11 02:29:31 -0700 | [diff] [blame] | 273 | * |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 274 | * @param newIdGenerator id generator |
| 275 | */ |
Sho SHIMIZU | f57a739 | 2014-11-25 16:11:32 -0800 | [diff] [blame] | 276 | public static void bindIdGenerator(IdGenerator newIdGenerator) { |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 277 | checkState(idGenerator == null, "Id generator is already bound."); |
| 278 | idGenerator = checkNotNull(newIdGenerator); |
| 279 | } |
| 280 | |
| 281 | /** |
| 282 | * Unbinds an id generator. |
| 283 | * |
| 284 | * Note: The caller must provide the old id generator to succeed. |
Thomas Vachuska | 6519e6f | 2015-03-11 02:29:31 -0700 | [diff] [blame] | 285 | * |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 286 | * @param oldIdGenerator the current id generator |
| 287 | */ |
Sho SHIMIZU | f57a739 | 2014-11-25 16:11:32 -0800 | [diff] [blame] | 288 | public static void unbindIdGenerator(IdGenerator oldIdGenerator) { |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 289 | if (Objects.equals(idGenerator, oldIdGenerator)) { |
| 290 | idGenerator = null; |
| 291 | } |
| 292 | } |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 293 | |
Yuta HIGUCHI | 652f27f | 2016-10-31 16:54:30 -0700 | [diff] [blame] | 294 | /** |
| 295 | * Returns the key to identify an "Intent". |
| 296 | * <p> |
| 297 | * When an Intent is updated, |
| 298 | * (e.g., flow is re-routed in reaction to network topology change) |
| 299 | * related Intent object's {@link IntentId} may change, |
| 300 | * but the key will remain unchanged. |
| 301 | * |
| 302 | * @return key |
| 303 | */ |
Ray Milkey | 5b3717e | 2015-02-05 11:44:08 -0800 | [diff] [blame] | 304 | public Key key() { |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 305 | return key; |
| 306 | } |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 307 | } |