blob: 12dad21f39cabf3be4397dd63e513dfa0aec4201 [file] [log] [blame]
Thomas Vachuska83e090e2014-10-22 14:25:35 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2014-present Open Networking Laboratory
Thomas Vachuska83e090e2014-10-22 14:25:35 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * 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 Vachuska83e090e2014-10-22 14:25:35 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * 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 Vachuska83e090e2014-10-22 14:25:35 -070015 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.net.intent;
Brian O'Connorb876bf12014-10-02 14:59:37 -070017
Brian O'Connor9476fa12015-06-25 15:17:17 -040018import com.google.common.annotations.Beta;
Brian O'Connorabafb502014-12-02 22:26:20 -080019import org.onosproject.core.ApplicationId;
20import org.onosproject.core.IdGenerator;
21import org.onosproject.net.NetworkResource;
Luca Prete670ac5d2017-02-03 15:55:43 -080022import org.onosproject.net.ResourceGroup;
Thomas Vachuskac96058a2014-10-20 23:00:16 -070023
Jonathan Hartb14221c2016-03-07 09:55:50 -080024import java.util.Collection;
25import java.util.Objects;
26
Ray Milkeyc24cde32015-03-10 18:20:18 -070027import static com.google.common.base.Preconditions.checkArgument;
Thomas Vachuskac96058a2014-10-20 23:00:16 -070028import static com.google.common.base.Preconditions.checkNotNull;
Brian O'Connor520c0522014-11-23 23:50:47 -080029import static com.google.common.base.Preconditions.checkState;
Thomas Vachuskac96058a2014-10-20 23:00:16 -070030
Brian O'Connorb876bf12014-10-02 14:59:37 -070031/**
32 * Abstraction of an application level intent.
Thomas Vachuska4b420772014-10-30 16:46:17 -070033 * <p>
Brian O'Connorb876bf12014-10-02 14:59:37 -070034 * Make sure that an Intent should be immutable when a new type is defined.
Thomas Vachuska4b420772014-10-30 16:46:17 -070035 * </p>
Brian O'Connorb876bf12014-10-02 14:59:37 -070036 */
Brian O'Connor9476fa12015-06-25 15:17:17 -040037@Beta
Sho SHIMIZU5e5d4aa2015-01-26 16:12:11 -080038public abstract class Intent {
Thomas Vachuskac96058a2014-10-20 23:00:16 -070039
40 private final IntentId id;
Brian O'Connorea4d7d12015-01-28 16:37:46 -080041
Thomas Vachuskac96058a2014-10-20 23:00:16 -070042 private final ApplicationId appId;
Ray Milkey5b3717e2015-02-05 11:44:08 -080043 private final Key key;
Brian O'Connorea4d7d12015-01-28 16:37:46 -080044
Ray Milkeyc24cde32015-03-10 18:20:18 -070045 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 Vachuskac96058a2014-10-20 23:00:16 -070050 private final Collection<NetworkResource> resources;
Luca Prete670ac5d2017-02-03 15:55:43 -080051 private final ResourceGroup resourceGroup;
Thomas Vachuskac96058a2014-10-20 23:00:16 -070052
Brian O'Connor520c0522014-11-23 23:50:47 -080053 private static IdGenerator idGenerator;
54
Thomas Vachuskac96058a2014-10-20 23:00:16 -070055 /**
56 * Constructor for serializer.
57 */
58 protected Intent() {
59 this.id = null;
60 this.appId = null;
Brian O'Connorea4d7d12015-01-28 16:37:46 -080061 this.key = null;
Thomas Vachuskac96058a2014-10-20 23:00:16 -070062 this.resources = null;
Ray Milkeyc24cde32015-03-10 18:20:18 -070063 this.priority = DEFAULT_INTENT_PRIORITY;
Luca Prete670ac5d2017-02-03 15:55:43 -080064 this.resourceGroup = null;
Thomas Vachuskac96058a2014-10-20 23:00:16 -070065 }
66
67 /**
68 * Creates a new intent.
Thomas Vachuska6519e6f2015-03-11 02:29:31 -070069 * @param appId application identifier
Thomas Vachuska6519e6f2015-03-11 02:29:31 -070070 * @param key optional key
71 * @param resources required network resources (optional)
72 * @param priority flow rule priority
Luca Prete670ac5d2017-02-03 15:55:43 -080073 * @deprecated 1.9.1
Thomas Vachuska6519e6f2015-03-11 02:29:31 -070074 */
Luca Prete670ac5d2017-02-03 15:55:43 -080075 @Deprecated
Brian O'Connorea4d7d12015-01-28 16:37:46 -080076 protected Intent(ApplicationId appId,
Ray Milkey5b3717e2015-02-05 11:44:08 -080077 Key key,
Ray Milkeyc24cde32015-03-10 18:20:18 -070078 Collection<NetworkResource> resources,
79 int priority) {
Brian O'Connor520c0522014-11-23 23:50:47 -080080 checkState(idGenerator != null, "Id generator is not bound.");
Ray Milkeyc24cde32015-03-10 18:20:18 -070081 checkArgument(priority <= MAX_PRIORITY && priority >= MIN_PRIORITY);
Brian O'Connor520c0522014-11-23 23:50:47 -080082 this.id = IntentId.valueOf(idGenerator.getNewId());
Thomas Vachuskac96058a2014-10-20 23:00:16 -070083 this.appId = checkNotNull(appId, "Application ID cannot be null");
Ray Milkey5b3717e2015-02-05 11:44:08 -080084 this.key = (key != null) ? key : Key.of(id.fingerprint(), appId);
Ray Milkeyc24cde32015-03-10 18:20:18 -070085 this.priority = priority;
Sho SHIMIZUd7d18002015-01-21 14:37:14 -080086 this.resources = checkNotNull(resources);
Luca Prete670ac5d2017-02-03 15:55:43 -080087 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 Vachuskac96058a2014-10-20 23:00:16 -0700111 }
112
Brian O'Connorb876bf12014-10-02 14:59:37 -0700113 /**
Ray Milkey3e3ec5f2015-03-17 17:00:38 -0700114 * 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 HIGUCHI6cad1792016-11-07 15:03:36 -0800120 protected Collection<NetworkResource> resources;
Luca Prete670ac5d2017-02-03 15:55:43 -0800121 protected ResourceGroup resourceGroup;
Ray Milkey3e3ec5f2015-03-17 17:00:38 -0700122
123 /**
Jonathan Hartb14221c2016-03-07 09:55:50 -0800124 * 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 Prete670ac5d2017-02-03 15:55:43 -0800138 .priority(intent.priority())
139 .resourceGroup(intent.resourceGroup());
Jonathan Hartb14221c2016-03-07 09:55:50 -0800140 }
141
142 /**
Ray Milkey3e3ec5f2015-03-17 17:00:38 -0700143 * 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 HIGUCHI6cad1792016-11-07 15:03:36 -0800175 /**
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 Prete670ac5d2017-02-03 15:55:43 -0800185
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 Milkey3e3ec5f2015-03-17 17:00:38 -0700196 }
197
198 /**
Yuta HIGUCHI652f27f2016-10-31 16:54:30 -0700199 * Returns the intent object identifier.
Brian O'Connorb876bf12014-10-02 14:59:37 -0700200 *
Thomas Vachuskac96058a2014-10-20 23:00:16 -0700201 * @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 Milkeyc24cde32015-03-10 18:20:18 -0700217 * Returns the priority of the intent.
218 *
219 * @return intent priority
220 */
221 public int priority() {
222 return priority;
223 }
224
225 /**
Thomas Vachuskac96058a2014-10-20 23:00:16 -0700226 * 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 Prete670ac5d2017-02-03 15:55:43 -0800235 * 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 Vachuskac96058a2014-10-20 23:00:16 -0700244 * 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 Vachuska46c07ad2014-10-21 16:01:01 -0700253 public final int hashCode() {
Brian O'Connor520c0522014-11-23 23:50:47 -0800254 return id.hashCode();
Thomas Vachuskac96058a2014-10-20 23:00:16 -0700255 }
256
257 @Override
Thomas Vachuska46c07ad2014-10-21 16:01:01 -0700258 public final boolean equals(Object obj) {
Thomas Vachuskac96058a2014-10-20 23:00:16 -0700259 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 SHIMIZU23895232014-11-25 15:59:50 -0800266 return this.id().equals(other.id());
Thomas Vachuskac96058a2014-10-20 23:00:16 -0700267 }
268
Brian O'Connor520c0522014-11-23 23:50:47 -0800269 /**
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 Vachuska6519e6f2015-03-11 02:29:31 -0700273 *
Brian O'Connor520c0522014-11-23 23:50:47 -0800274 * @param newIdGenerator id generator
275 */
Sho SHIMIZUf57a7392014-11-25 16:11:32 -0800276 public static void bindIdGenerator(IdGenerator newIdGenerator) {
Brian O'Connor520c0522014-11-23 23:50:47 -0800277 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 Vachuska6519e6f2015-03-11 02:29:31 -0700285 *
Brian O'Connor520c0522014-11-23 23:50:47 -0800286 * @param oldIdGenerator the current id generator
287 */
Sho SHIMIZUf57a7392014-11-25 16:11:32 -0800288 public static void unbindIdGenerator(IdGenerator oldIdGenerator) {
Brian O'Connor520c0522014-11-23 23:50:47 -0800289 if (Objects.equals(idGenerator, oldIdGenerator)) {
290 idGenerator = null;
291 }
292 }
Brian O'Connorcff03322015-02-03 15:28:59 -0800293
Yuta HIGUCHI652f27f2016-10-31 16:54:30 -0700294 /**
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 Milkey5b3717e2015-02-05 11:44:08 -0800304 public Key key() {
Brian O'Connorcff03322015-02-03 15:28:59 -0800305 return key;
306 }
Brian O'Connorb876bf12014-10-02 14:59:37 -0700307}