blob: b7af54f305b297ec6cac411a6431013f05785cda [file] [log] [blame]
Thomas Vachuska83e090e2014-10-22 14:25:35 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2014-present Open Networking Foundation
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;
Jonathan Hartb14221c2016-03-07 09:55:50 -080025
Thomas Vachuska2048c1f2017-05-10 19:32:22 -070026import static com.google.common.base.Preconditions.*;
Thomas Vachuskac96058a2014-10-20 23:00:16 -070027
Brian O'Connorb876bf12014-10-02 14:59:37 -070028/**
29 * Abstraction of an application level intent.
Thomas Vachuska4b420772014-10-30 16:46:17 -070030 * <p>
Brian O'Connorb876bf12014-10-02 14:59:37 -070031 * Make sure that an Intent should be immutable when a new type is defined.
Thomas Vachuska4b420772014-10-30 16:46:17 -070032 * </p>
Brian O'Connorb876bf12014-10-02 14:59:37 -070033 */
Brian O'Connor9476fa12015-06-25 15:17:17 -040034@Beta
Sho SHIMIZU5e5d4aa2015-01-26 16:12:11 -080035public abstract class Intent {
Thomas Vachuskac96058a2014-10-20 23:00:16 -070036
37 private final IntentId id;
Brian O'Connorea4d7d12015-01-28 16:37:46 -080038
Thomas Vachuskac96058a2014-10-20 23:00:16 -070039 private final ApplicationId appId;
Ray Milkey5b3717e2015-02-05 11:44:08 -080040 private final Key key;
Brian O'Connorea4d7d12015-01-28 16:37:46 -080041
Ray Milkeyc24cde32015-03-10 18:20:18 -070042 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 Vachuskac96058a2014-10-20 23:00:16 -070047 private final Collection<NetworkResource> resources;
Luca Prete670ac5d2017-02-03 15:55:43 -080048 private final ResourceGroup resourceGroup;
Thomas Vachuskac96058a2014-10-20 23:00:16 -070049
Brian O'Connor520c0522014-11-23 23:50:47 -080050 private static IdGenerator idGenerator;
Ray Milkey06297ed2018-01-22 17:13:41 -080051 private static final Object ID_GENERATOR_LOCK = new Object();
Brian O'Connor520c0522014-11-23 23:50:47 -080052
Thomas Vachuskac96058a2014-10-20 23:00:16 -070053 /**
54 * Constructor for serializer.
55 */
56 protected Intent() {
57 this.id = null;
58 this.appId = null;
Brian O'Connorea4d7d12015-01-28 16:37:46 -080059 this.key = null;
Thomas Vachuskac96058a2014-10-20 23:00:16 -070060 this.resources = null;
Ray Milkeyc24cde32015-03-10 18:20:18 -070061 this.priority = DEFAULT_INTENT_PRIORITY;
Luca Prete670ac5d2017-02-03 15:55:43 -080062 this.resourceGroup = null;
Thomas Vachuskac96058a2014-10-20 23:00:16 -070063 }
64
65 /**
66 * Creates a new intent.
Thomas Vachuska6519e6f2015-03-11 02:29:31 -070067 * @param appId application identifier
Thomas Vachuska6519e6f2015-03-11 02:29:31 -070068 * @param key optional key
69 * @param resources required network resources (optional)
70 * @param priority flow rule priority
Luca Prete670ac5d2017-02-03 15:55:43 -080071 * @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 Vachuskac96058a2014-10-20 23:00:16 -070086 }
87
Brian O'Connorb876bf12014-10-02 14:59:37 -070088 /**
Ray Milkey3e3ec5f2015-03-17 17:00:38 -070089 * 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 HIGUCHI6cad1792016-11-07 15:03:36 -080095 protected Collection<NetworkResource> resources;
Luca Prete670ac5d2017-02-03 15:55:43 -080096 protected ResourceGroup resourceGroup;
Ray Milkey3e3ec5f2015-03-17 17:00:38 -070097
98 /**
Jonathan Hartb14221c2016-03-07 09:55:50 -080099 * 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 Prete670ac5d2017-02-03 15:55:43 -0800113 .priority(intent.priority())
114 .resourceGroup(intent.resourceGroup());
Jonathan Hartb14221c2016-03-07 09:55:50 -0800115 }
116
117 /**
Ray Milkey3e3ec5f2015-03-17 17:00:38 -0700118 * 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 HIGUCHI6cad1792016-11-07 15:03:36 -0800150 /**
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 Prete670ac5d2017-02-03 15:55:43 -0800160
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 Milkey3e3ec5f2015-03-17 17:00:38 -0700171 }
172
173 /**
Yuta HIGUCHI652f27f2016-10-31 16:54:30 -0700174 * Returns the intent object identifier.
Brian O'Connorb876bf12014-10-02 14:59:37 -0700175 *
Thomas Vachuskac96058a2014-10-20 23:00:16 -0700176 * @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 Milkeyc24cde32015-03-10 18:20:18 -0700192 * Returns the priority of the intent.
193 *
194 * @return intent priority
195 */
196 public int priority() {
197 return priority;
198 }
199
200 /**
Thomas Vachuskac96058a2014-10-20 23:00:16 -0700201 * 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 Prete670ac5d2017-02-03 15:55:43 -0800210 * 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 Vachuskac96058a2014-10-20 23:00:16 -0700219 * 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 Vachuska46c07ad2014-10-21 16:01:01 -0700228 public final int hashCode() {
Brian O'Connor520c0522014-11-23 23:50:47 -0800229 return id.hashCode();
Thomas Vachuskac96058a2014-10-20 23:00:16 -0700230 }
231
232 @Override
Thomas Vachuska46c07ad2014-10-21 16:01:01 -0700233 public final boolean equals(Object obj) {
Thomas Vachuskac96058a2014-10-20 23:00:16 -0700234 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 SHIMIZU23895232014-11-25 15:59:50 -0800241 return this.id().equals(other.id());
Thomas Vachuskac96058a2014-10-20 23:00:16 -0700242 }
243
Brian O'Connor520c0522014-11-23 23:50:47 -0800244 /**
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 Vachuska6519e6f2015-03-11 02:29:31 -0700248 *
Brian O'Connor520c0522014-11-23 23:50:47 -0800249 * @param newIdGenerator id generator
250 */
Sho SHIMIZUf57a7392014-11-25 16:11:32 -0800251 public static void bindIdGenerator(IdGenerator newIdGenerator) {
Ray Milkey06297ed2018-01-22 17:13:41 -0800252 synchronized (ID_GENERATOR_LOCK) {
253 checkState(idGenerator == null, "Id generator is already bound.");
254 idGenerator = checkNotNull(newIdGenerator);
255 }
Brian O'Connor520c0522014-11-23 23:50:47 -0800256 }
257
258 /**
259 * Unbinds an id generator.
260 *
261 * Note: The caller must provide the old id generator to succeed.
Thomas Vachuska6519e6f2015-03-11 02:29:31 -0700262 *
Brian O'Connor520c0522014-11-23 23:50:47 -0800263 * @param oldIdGenerator the current id generator
264 */
Sho SHIMIZUf57a7392014-11-25 16:11:32 -0800265 public static void unbindIdGenerator(IdGenerator oldIdGenerator) {
Ray Milkey06297ed2018-01-22 17:13:41 -0800266 synchronized (ID_GENERATOR_LOCK) {
267 if (idGenerator == oldIdGenerator) {
268 idGenerator = null;
269 }
Brian O'Connor520c0522014-11-23 23:50:47 -0800270 }
271 }
Brian O'Connorcff03322015-02-03 15:28:59 -0800272
Yuta HIGUCHI652f27f2016-10-31 16:54:30 -0700273 /**
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 Milkey5b3717e2015-02-05 11:44:08 -0800283 public Key key() {
Brian O'Connorcff03322015-02-03 15:28:59 -0800284 return key;
285 }
Brian O'Connorb876bf12014-10-02 14:59:37 -0700286}