blob: 077fd89573b17337b43b9fba9fe33bcbd61ea4f5 [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'Connor9476fa12015-06-25 15:17:17 -040021import com.google.common.annotations.Beta;
Brian O'Connorabafb502014-12-02 22:26:20 -080022import org.onosproject.core.ApplicationId;
23import org.onosproject.core.IdGenerator;
24import org.onosproject.net.NetworkResource;
Thomas Vachuskac96058a2014-10-20 23:00:16 -070025
Ray Milkeyc24cde32015-03-10 18:20:18 -070026import static com.google.common.base.Preconditions.checkArgument;
Thomas Vachuskac96058a2014-10-20 23:00:16 -070027import static com.google.common.base.Preconditions.checkNotNull;
Brian O'Connor520c0522014-11-23 23:50:47 -080028import static com.google.common.base.Preconditions.checkState;
Thomas Vachuskac96058a2014-10-20 23:00:16 -070029
Brian O'Connorb876bf12014-10-02 14:59:37 -070030/**
31 * Abstraction of an application level intent.
Thomas Vachuska4b420772014-10-30 16:46:17 -070032 * <p>
Brian O'Connorb876bf12014-10-02 14:59:37 -070033 * Make sure that an Intent should be immutable when a new type is defined.
Thomas Vachuska4b420772014-10-30 16:46:17 -070034 * </p>
Brian O'Connorb876bf12014-10-02 14:59:37 -070035 */
Brian O'Connor9476fa12015-06-25 15:17:17 -040036@Beta
Sho SHIMIZU5e5d4aa2015-01-26 16:12:11 -080037public abstract class Intent {
Thomas Vachuskac96058a2014-10-20 23:00:16 -070038
39 private final IntentId id;
Brian O'Connorea4d7d12015-01-28 16:37:46 -080040
Thomas Vachuskac96058a2014-10-20 23:00:16 -070041 private final ApplicationId appId;
Ray Milkey5b3717e2015-02-05 11:44:08 -080042 private final Key key;
Brian O'Connorea4d7d12015-01-28 16:37:46 -080043
Ray Milkeyc24cde32015-03-10 18:20:18 -070044 private final int priority;
45 public static final int DEFAULT_INTENT_PRIORITY = 100;
46 public static final int MAX_PRIORITY = (1 << 16) - 1;
47 public static final int MIN_PRIORITY = 1;
48
Thomas Vachuskac96058a2014-10-20 23:00:16 -070049 private final Collection<NetworkResource> resources;
50
Brian O'Connor520c0522014-11-23 23:50:47 -080051 private static IdGenerator idGenerator;
52
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;
Thomas Vachuskac96058a2014-10-20 23:00:16 -070062 }
63
64 /**
65 * Creates a new intent.
66 *
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
71 */
Brian O'Connorea4d7d12015-01-28 16:37:46 -080072 protected Intent(ApplicationId appId,
Ray Milkey5b3717e2015-02-05 11:44:08 -080073 Key key,
Ray Milkeyc24cde32015-03-10 18:20:18 -070074 Collection<NetworkResource> resources,
75 int priority) {
Brian O'Connor520c0522014-11-23 23:50:47 -080076 checkState(idGenerator != null, "Id generator is not bound.");
Ray Milkeyc24cde32015-03-10 18:20:18 -070077 checkArgument(priority <= MAX_PRIORITY && priority >= MIN_PRIORITY);
Brian O'Connor520c0522014-11-23 23:50:47 -080078 this.id = IntentId.valueOf(idGenerator.getNewId());
Thomas Vachuskac96058a2014-10-20 23:00:16 -070079 this.appId = checkNotNull(appId, "Application ID cannot be null");
Ray Milkey5b3717e2015-02-05 11:44:08 -080080 this.key = (key != null) ? key : Key.of(id.fingerprint(), appId);
Ray Milkeyc24cde32015-03-10 18:20:18 -070081 this.priority = priority;
Sho SHIMIZUd7d18002015-01-21 14:37:14 -080082 this.resources = checkNotNull(resources);
Thomas Vachuskac96058a2014-10-20 23:00:16 -070083 }
84
Brian O'Connorb876bf12014-10-02 14:59:37 -070085 /**
Ray Milkey3e3ec5f2015-03-17 17:00:38 -070086 * Abstract builder for intents.
87 */
88 public abstract static class Builder {
89 protected ApplicationId appId;
90 protected Key key;
91 protected int priority = Intent.DEFAULT_INTENT_PRIORITY;
92
93 /**
94 * Sets the application id for the intent that will be built.
95 *
96 * @param appId application id to use for built intent
97 * @return this builder
98 */
99 public Builder appId(ApplicationId appId) {
100 this.appId = appId;
101 return this;
102 }
103
104 /**
105 * Sets the key for the intent that will be built.
106 *
107 * @param key key to use for built intent
108 * @return this builder
109 */
110 public Builder key(Key key) {
111 this.key = key;
112 return this;
113 }
114
115 /**
116 * Sets the priority for the intent that will be built.
117 *
118 * @param priority priority to use for built intent
119 * @return this builder
120 */
121 public Builder priority(int priority) {
122 this.priority = priority;
123 return this;
124 }
125
126 }
127
128 /**
Brian O'Connorb876bf12014-10-02 14:59:37 -0700129 * Returns the intent identifier.
130 *
Thomas Vachuskac96058a2014-10-20 23:00:16 -0700131 * @return intent fingerprint
132 */
133 public IntentId id() {
134 return id;
135 }
136
137 /**
138 * Returns the identifier of the application that requested the intent.
139 *
140 * @return application identifier
141 */
142 public ApplicationId appId() {
143 return appId;
144 }
145
146 /**
Ray Milkeyc24cde32015-03-10 18:20:18 -0700147 * Returns the priority of the intent.
148 *
149 * @return intent priority
150 */
151 public int priority() {
152 return priority;
153 }
154
155 /**
Thomas Vachuskac96058a2014-10-20 23:00:16 -0700156 * Returns the collection of resources required for this intent.
157 *
158 * @return collection of resources; may be null
159 */
160 public Collection<NetworkResource> resources() {
161 return resources;
162 }
163
164 /**
Thomas Vachuskac96058a2014-10-20 23:00:16 -0700165 * Indicates whether or not the intent is installable.
166 *
167 * @return true if installable
168 */
169 public boolean isInstallable() {
170 return false;
171 }
172
173 @Override
Thomas Vachuska46c07ad2014-10-21 16:01:01 -0700174 public final int hashCode() {
Brian O'Connor520c0522014-11-23 23:50:47 -0800175 return id.hashCode();
Thomas Vachuskac96058a2014-10-20 23:00:16 -0700176 }
177
178 @Override
Thomas Vachuska46c07ad2014-10-21 16:01:01 -0700179 public final boolean equals(Object obj) {
Thomas Vachuskac96058a2014-10-20 23:00:16 -0700180 if (this == obj) {
181 return true;
182 }
183 if (obj == null || getClass() != obj.getClass()) {
184 return false;
185 }
186 final Intent other = (Intent) obj;
Sho SHIMIZU23895232014-11-25 15:59:50 -0800187 return this.id().equals(other.id());
Thomas Vachuskac96058a2014-10-20 23:00:16 -0700188 }
189
Brian O'Connor520c0522014-11-23 23:50:47 -0800190 /**
191 * Binds an id generator for unique intent id generation.
192 *
193 * Note: A generator cannot be bound if there is already a generator bound.
Thomas Vachuska6519e6f2015-03-11 02:29:31 -0700194 *
Brian O'Connor520c0522014-11-23 23:50:47 -0800195 * @param newIdGenerator id generator
196 */
Sho SHIMIZUf57a7392014-11-25 16:11:32 -0800197 public static void bindIdGenerator(IdGenerator newIdGenerator) {
Brian O'Connor520c0522014-11-23 23:50:47 -0800198 checkState(idGenerator == null, "Id generator is already bound.");
199 idGenerator = checkNotNull(newIdGenerator);
200 }
201
202 /**
203 * Unbinds an id generator.
204 *
205 * Note: The caller must provide the old id generator to succeed.
Thomas Vachuska6519e6f2015-03-11 02:29:31 -0700206 *
Brian O'Connor520c0522014-11-23 23:50:47 -0800207 * @param oldIdGenerator the current id generator
208 */
Sho SHIMIZUf57a7392014-11-25 16:11:32 -0800209 public static void unbindIdGenerator(IdGenerator oldIdGenerator) {
Brian O'Connor520c0522014-11-23 23:50:47 -0800210 if (Objects.equals(idGenerator, oldIdGenerator)) {
211 idGenerator = null;
212 }
213 }
Brian O'Connorcff03322015-02-03 15:28:59 -0800214
Ray Milkey5b3717e2015-02-05 11:44:08 -0800215 public Key key() {
Brian O'Connorcff03322015-02-03 15:28:59 -0800216 return key;
217 }
Brian O'Connorb876bf12014-10-02 14:59:37 -0700218}