blob: 7a541e70040898e2ba69b2a58c7a460aac435ca0 [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;
51
Thomas Vachuskac96058a2014-10-20 23:00:16 -070052 /**
53 * Constructor for serializer.
54 */
55 protected Intent() {
56 this.id = null;
57 this.appId = null;
Brian O'Connorea4d7d12015-01-28 16:37:46 -080058 this.key = null;
Thomas Vachuskac96058a2014-10-20 23:00:16 -070059 this.resources = null;
Ray Milkeyc24cde32015-03-10 18:20:18 -070060 this.priority = DEFAULT_INTENT_PRIORITY;
Luca Prete670ac5d2017-02-03 15:55:43 -080061 this.resourceGroup = null;
Thomas Vachuskac96058a2014-10-20 23:00:16 -070062 }
63
64 /**
65 * Creates a new intent.
Thomas Vachuska6519e6f2015-03-11 02:29:31 -070066 * @param appId application identifier
Thomas Vachuska6519e6f2015-03-11 02:29:31 -070067 * @param key optional key
68 * @param resources required network resources (optional)
69 * @param priority flow rule priority
Luca Prete670ac5d2017-02-03 15:55:43 -080070 * @param resourceGroup the resource group for intent
71 */
72 protected Intent(ApplicationId appId,
73 Key key,
74 Collection<NetworkResource> resources,
75 int priority,
76 ResourceGroup resourceGroup) {
77 checkState(idGenerator != null, "Id generator is not bound.");
78 checkArgument(priority <= MAX_PRIORITY && priority >= MIN_PRIORITY);
79 this.id = IntentId.valueOf(idGenerator.getNewId());
80 this.appId = checkNotNull(appId, "Application ID cannot be null");
81 this.key = (key != null) ? key : Key.of(id.fingerprint(), appId);
82 this.priority = priority;
83 this.resources = checkNotNull(resources);
84 this.resourceGroup = resourceGroup;
Thomas Vachuskac96058a2014-10-20 23:00:16 -070085 }
86
Brian O'Connorb876bf12014-10-02 14:59:37 -070087 /**
Ray Milkey3e3ec5f2015-03-17 17:00:38 -070088 * Abstract builder for intents.
89 */
90 public abstract static class Builder {
91 protected ApplicationId appId;
92 protected Key key;
93 protected int priority = Intent.DEFAULT_INTENT_PRIORITY;
Yuta HIGUCHI6cad1792016-11-07 15:03:36 -080094 protected Collection<NetworkResource> resources;
Luca Prete670ac5d2017-02-03 15:55:43 -080095 protected ResourceGroup resourceGroup;
Ray Milkey3e3ec5f2015-03-17 17:00:38 -070096
97 /**
Jonathan Hartb14221c2016-03-07 09:55:50 -080098 * Creates a new empty builder.
99 */
100 protected Builder() {
101 }
102
103 /**
104 * Creates a new builder pre-populated with the information in the given
105 * intent.
106 *
107 * @param intent initial intent
108 */
109 protected Builder(Intent intent) {
110 this.appId(intent.appId())
111 .key(intent.key())
Luca Prete670ac5d2017-02-03 15:55:43 -0800112 .priority(intent.priority())
113 .resourceGroup(intent.resourceGroup());
Jonathan Hartb14221c2016-03-07 09:55:50 -0800114 }
115
116 /**
Ray Milkey3e3ec5f2015-03-17 17:00:38 -0700117 * Sets the application id for the intent that will be built.
118 *
119 * @param appId application id to use for built intent
120 * @return this builder
121 */
122 public Builder appId(ApplicationId appId) {
123 this.appId = appId;
124 return this;
125 }
126
127 /**
128 * Sets the key for the intent that will be built.
129 *
130 * @param key key to use for built intent
131 * @return this builder
132 */
133 public Builder key(Key key) {
134 this.key = key;
135 return this;
136 }
137
138 /**
139 * Sets the priority for the intent that will be built.
140 *
141 * @param priority priority to use for built intent
142 * @return this builder
143 */
144 public Builder priority(int priority) {
145 this.priority = priority;
146 return this;
147 }
148
Yuta HIGUCHI6cad1792016-11-07 15:03:36 -0800149 /**
150 * Sets the collection of resources required for this intent.
151 *
152 * @param resources collection of resources
153 * @return this builder
154 */
155 public Builder resources(Collection<NetworkResource> resources) {
156 this.resources = resources;
157 return this;
158 }
Luca Prete670ac5d2017-02-03 15:55:43 -0800159
160 /**
161 * Sets the resource group for this intent.
162 *
163 * @param resourceGroup the resource group
164 * @return this builder
165 */
166 public Builder resourceGroup(ResourceGroup resourceGroup) {
167 this.resourceGroup = resourceGroup;
168 return this;
169 }
Ray Milkey3e3ec5f2015-03-17 17:00:38 -0700170 }
171
172 /**
Yuta HIGUCHI652f27f2016-10-31 16:54:30 -0700173 * Returns the intent object identifier.
Brian O'Connorb876bf12014-10-02 14:59:37 -0700174 *
Thomas Vachuskac96058a2014-10-20 23:00:16 -0700175 * @return intent fingerprint
176 */
177 public IntentId id() {
178 return id;
179 }
180
181 /**
182 * Returns the identifier of the application that requested the intent.
183 *
184 * @return application identifier
185 */
186 public ApplicationId appId() {
187 return appId;
188 }
189
190 /**
Ray Milkeyc24cde32015-03-10 18:20:18 -0700191 * Returns the priority of the intent.
192 *
193 * @return intent priority
194 */
195 public int priority() {
196 return priority;
197 }
198
199 /**
Thomas Vachuskac96058a2014-10-20 23:00:16 -0700200 * Returns the collection of resources required for this intent.
201 *
202 * @return collection of resources; may be null
203 */
204 public Collection<NetworkResource> resources() {
205 return resources;
206 }
207
208 /**
Luca Prete670ac5d2017-02-03 15:55:43 -0800209 * Returns the resource group for this intent.
210 *
211 * @return the resource group; may be null
212 */
213 public ResourceGroup resourceGroup() {
214 return resourceGroup;
215 }
216
217 /**
Thomas Vachuskac96058a2014-10-20 23:00:16 -0700218 * Indicates whether or not the intent is installable.
219 *
220 * @return true if installable
221 */
222 public boolean isInstallable() {
223 return false;
224 }
225
226 @Override
Thomas Vachuska46c07ad2014-10-21 16:01:01 -0700227 public final int hashCode() {
Brian O'Connor520c0522014-11-23 23:50:47 -0800228 return id.hashCode();
Thomas Vachuskac96058a2014-10-20 23:00:16 -0700229 }
230
231 @Override
Thomas Vachuska46c07ad2014-10-21 16:01:01 -0700232 public final boolean equals(Object obj) {
Thomas Vachuskac96058a2014-10-20 23:00:16 -0700233 if (this == obj) {
234 return true;
235 }
236 if (obj == null || getClass() != obj.getClass()) {
237 return false;
238 }
239 final Intent other = (Intent) obj;
Sho SHIMIZU23895232014-11-25 15:59:50 -0800240 return this.id().equals(other.id());
Thomas Vachuskac96058a2014-10-20 23:00:16 -0700241 }
242
Brian O'Connor520c0522014-11-23 23:50:47 -0800243 /**
244 * Binds an id generator for unique intent id generation.
245 *
246 * Note: A generator cannot be bound if there is already a generator bound.
Thomas Vachuska6519e6f2015-03-11 02:29:31 -0700247 *
Brian O'Connor520c0522014-11-23 23:50:47 -0800248 * @param newIdGenerator id generator
249 */
Sho SHIMIZUf57a7392014-11-25 16:11:32 -0800250 public static void bindIdGenerator(IdGenerator newIdGenerator) {
Brian O'Connor520c0522014-11-23 23:50:47 -0800251 checkState(idGenerator == null, "Id generator is already bound.");
252 idGenerator = checkNotNull(newIdGenerator);
253 }
254
255 /**
256 * Unbinds an id generator.
257 *
258 * Note: The caller must provide the old id generator to succeed.
Thomas Vachuska6519e6f2015-03-11 02:29:31 -0700259 *
Brian O'Connor520c0522014-11-23 23:50:47 -0800260 * @param oldIdGenerator the current id generator
261 */
Sho SHIMIZUf57a7392014-11-25 16:11:32 -0800262 public static void unbindIdGenerator(IdGenerator oldIdGenerator) {
Thomas Vachuska2048c1f2017-05-10 19:32:22 -0700263 if (idGenerator == oldIdGenerator) {
Brian O'Connor520c0522014-11-23 23:50:47 -0800264 idGenerator = null;
265 }
266 }
Brian O'Connorcff03322015-02-03 15:28:59 -0800267
Yuta HIGUCHI652f27f2016-10-31 16:54:30 -0700268 /**
269 * Returns the key to identify an "Intent".
270 * <p>
271 * When an Intent is updated,
272 * (e.g., flow is re-routed in reaction to network topology change)
273 * related Intent object's {@link IntentId} may change,
274 * but the key will remain unchanged.
275 *
276 * @return key
277 */
Ray Milkey5b3717e2015-02-05 11:44:08 -0800278 public Key key() {
Brian O'Connorcff03322015-02-03 15:28:59 -0800279 return key;
280 }
Brian O'Connorb876bf12014-10-02 14:59:37 -0700281}