blob: 4fac53b12e0ff315cc32e31fe14023b0027d67aa [file] [log] [blame]
Thomas Vachuska83e090e2014-10-22 14:25:35 -07001/*
Ray Milkey34c95902015-04-15 09:47:53 -07002 * Copyright 2014-2015 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
Ray Milkey3e3ec5f2015-03-17 17:00:38 -070018import java.util.Collection;
19import java.util.Objects;
20
Brian O'Connorabafb502014-12-02 22:26:20 -080021import org.onosproject.core.ApplicationId;
22import org.onosproject.core.IdGenerator;
23import org.onosproject.net.NetworkResource;
Thomas Vachuskac96058a2014-10-20 23:00:16 -070024
Ray Milkeyc24cde32015-03-10 18:20:18 -070025import static com.google.common.base.Preconditions.checkArgument;
Thomas Vachuskac96058a2014-10-20 23:00:16 -070026import static com.google.common.base.Preconditions.checkNotNull;
Brian O'Connor520c0522014-11-23 23:50:47 -080027import static com.google.common.base.Preconditions.checkState;
Thomas Vachuskac96058a2014-10-20 23:00:16 -070028
Brian O'Connorb876bf12014-10-02 14:59:37 -070029/**
30 * Abstraction of an application level intent.
Thomas Vachuska4b420772014-10-30 16:46:17 -070031 * <p>
Brian O'Connorb876bf12014-10-02 14:59:37 -070032 * Make sure that an Intent should be immutable when a new type is defined.
Thomas Vachuska4b420772014-10-30 16:46:17 -070033 * </p>
Brian O'Connorb876bf12014-10-02 14:59:37 -070034 */
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;
48
Brian O'Connor520c0522014-11-23 23:50:47 -080049 private static IdGenerator idGenerator;
50
Thomas Vachuskac96058a2014-10-20 23:00:16 -070051 /**
52 * Constructor for serializer.
53 */
54 protected Intent() {
55 this.id = null;
56 this.appId = null;
Brian O'Connorea4d7d12015-01-28 16:37:46 -080057 this.key = null;
Thomas Vachuskac96058a2014-10-20 23:00:16 -070058 this.resources = null;
Ray Milkeyc24cde32015-03-10 18:20:18 -070059 this.priority = DEFAULT_INTENT_PRIORITY;
Thomas Vachuskac96058a2014-10-20 23:00:16 -070060 }
61
62 /**
63 * Creates a new intent.
64 *
Thomas Vachuska6519e6f2015-03-11 02:29:31 -070065 * @param appId application identifier
Thomas Vachuska6519e6f2015-03-11 02:29:31 -070066 * @param key optional key
67 * @param resources required network resources (optional)
68 * @param priority flow rule priority
69 */
Brian O'Connorea4d7d12015-01-28 16:37:46 -080070 protected Intent(ApplicationId appId,
Ray Milkey5b3717e2015-02-05 11:44:08 -080071 Key key,
Ray Milkeyc24cde32015-03-10 18:20:18 -070072 Collection<NetworkResource> resources,
73 int priority) {
Brian O'Connor520c0522014-11-23 23:50:47 -080074 checkState(idGenerator != null, "Id generator is not bound.");
Ray Milkeyc24cde32015-03-10 18:20:18 -070075 checkArgument(priority <= MAX_PRIORITY && priority >= MIN_PRIORITY);
Brian O'Connor520c0522014-11-23 23:50:47 -080076 this.id = IntentId.valueOf(idGenerator.getNewId());
Thomas Vachuskac96058a2014-10-20 23:00:16 -070077 this.appId = checkNotNull(appId, "Application ID cannot be null");
Ray Milkey5b3717e2015-02-05 11:44:08 -080078 this.key = (key != null) ? key : Key.of(id.fingerprint(), appId);
Ray Milkeyc24cde32015-03-10 18:20:18 -070079 this.priority = priority;
Sho SHIMIZUd7d18002015-01-21 14:37:14 -080080 this.resources = checkNotNull(resources);
Thomas Vachuskac96058a2014-10-20 23:00:16 -070081 }
82
Brian O'Connorb876bf12014-10-02 14:59:37 -070083 /**
Ray Milkey3e3ec5f2015-03-17 17:00:38 -070084 * Abstract builder for intents.
85 */
86 public abstract static class Builder {
87 protected ApplicationId appId;
88 protected Key key;
89 protected int priority = Intent.DEFAULT_INTENT_PRIORITY;
90
91 /**
92 * Sets the application id for the intent that will be built.
93 *
94 * @param appId application id to use for built intent
95 * @return this builder
96 */
97 public Builder appId(ApplicationId appId) {
98 this.appId = appId;
99 return this;
100 }
101
102 /**
103 * Sets the key for the intent that will be built.
104 *
105 * @param key key to use for built intent
106 * @return this builder
107 */
108 public Builder key(Key key) {
109 this.key = key;
110 return this;
111 }
112
113 /**
114 * Sets the priority for the intent that will be built.
115 *
116 * @param priority priority to use for built intent
117 * @return this builder
118 */
119 public Builder priority(int priority) {
120 this.priority = priority;
121 return this;
122 }
123
124 }
125
126 /**
Brian O'Connorb876bf12014-10-02 14:59:37 -0700127 * Returns the intent identifier.
128 *
Thomas Vachuskac96058a2014-10-20 23:00:16 -0700129 * @return intent fingerprint
130 */
131 public IntentId id() {
132 return id;
133 }
134
135 /**
136 * Returns the identifier of the application that requested the intent.
137 *
138 * @return application identifier
139 */
140 public ApplicationId appId() {
141 return appId;
142 }
143
144 /**
Ray Milkeyc24cde32015-03-10 18:20:18 -0700145 * Returns the priority of the intent.
146 *
147 * @return intent priority
148 */
149 public int priority() {
150 return priority;
151 }
152
153 /**
Thomas Vachuskac96058a2014-10-20 23:00:16 -0700154 * Returns the collection of resources required for this intent.
155 *
156 * @return collection of resources; may be null
157 */
158 public Collection<NetworkResource> resources() {
159 return resources;
160 }
161
162 /**
Thomas Vachuskac96058a2014-10-20 23:00:16 -0700163 * Indicates whether or not the intent is installable.
164 *
165 * @return true if installable
166 */
167 public boolean isInstallable() {
168 return false;
169 }
170
171 @Override
Thomas Vachuska46c07ad2014-10-21 16:01:01 -0700172 public final int hashCode() {
Brian O'Connor520c0522014-11-23 23:50:47 -0800173 return id.hashCode();
Thomas Vachuskac96058a2014-10-20 23:00:16 -0700174 }
175
176 @Override
Thomas Vachuska46c07ad2014-10-21 16:01:01 -0700177 public final boolean equals(Object obj) {
Thomas Vachuskac96058a2014-10-20 23:00:16 -0700178 if (this == obj) {
179 return true;
180 }
181 if (obj == null || getClass() != obj.getClass()) {
182 return false;
183 }
184 final Intent other = (Intent) obj;
Sho SHIMIZU23895232014-11-25 15:59:50 -0800185 return this.id().equals(other.id());
Thomas Vachuskac96058a2014-10-20 23:00:16 -0700186 }
187
Brian O'Connor520c0522014-11-23 23:50:47 -0800188 /**
189 * Binds an id generator for unique intent id generation.
190 *
191 * Note: A generator cannot be bound if there is already a generator bound.
Thomas Vachuska6519e6f2015-03-11 02:29:31 -0700192 *
Brian O'Connor520c0522014-11-23 23:50:47 -0800193 * @param newIdGenerator id generator
194 */
Sho SHIMIZUf57a7392014-11-25 16:11:32 -0800195 public static void bindIdGenerator(IdGenerator newIdGenerator) {
Brian O'Connor520c0522014-11-23 23:50:47 -0800196 checkState(idGenerator == null, "Id generator is already bound.");
197 idGenerator = checkNotNull(newIdGenerator);
198 }
199
200 /**
201 * Unbinds an id generator.
202 *
203 * Note: The caller must provide the old id generator to succeed.
Thomas Vachuska6519e6f2015-03-11 02:29:31 -0700204 *
Brian O'Connor520c0522014-11-23 23:50:47 -0800205 * @param oldIdGenerator the current id generator
206 */
Sho SHIMIZUf57a7392014-11-25 16:11:32 -0800207 public static void unbindIdGenerator(IdGenerator oldIdGenerator) {
Brian O'Connor520c0522014-11-23 23:50:47 -0800208 if (Objects.equals(idGenerator, oldIdGenerator)) {
209 idGenerator = null;
210 }
211 }
Brian O'Connorcff03322015-02-03 15:28:59 -0800212
Ray Milkey5b3717e2015-02-05 11:44:08 -0800213 public Key key() {
Brian O'Connorcff03322015-02-03 15:28:59 -0800214 return key;
215 }
Brian O'Connorb876bf12014-10-02 14:59:37 -0700216}