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