Thomas Vachuska | 83e090e | 2014-10-22 14:25:35 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Laboratory |
Thomas Vachuska | 83e090e | 2014-10-22 14:25:35 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 4 | * 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 Vachuska | 83e090e | 2014-10-22 14:25:35 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 8 | * 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 Vachuska | 83e090e | 2014-10-22 14:25:35 -0700 | [diff] [blame] | 15 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.net.intent; |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 17 | |
Brian O'Connor | 9476fa1 | 2015-06-25 15:17:17 -0400 | [diff] [blame] | 18 | import com.google.common.annotations.Beta; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 19 | import org.onosproject.core.ApplicationId; |
| 20 | import org.onosproject.core.IdGenerator; |
| 21 | import org.onosproject.net.NetworkResource; |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 22 | |
Jonathan Hart | b14221c | 2016-03-07 09:55:50 -0800 | [diff] [blame] | 23 | import java.util.Collection; |
| 24 | import java.util.Objects; |
| 25 | |
Ray Milkey | c24cde3 | 2015-03-10 18:20:18 -0700 | [diff] [blame] | 26 | import static com.google.common.base.Preconditions.checkArgument; |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 27 | import static com.google.common.base.Preconditions.checkNotNull; |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 28 | import static com.google.common.base.Preconditions.checkState; |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 29 | |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 30 | /** |
| 31 | * Abstraction of an application level intent. |
Thomas Vachuska | 4b42077 | 2014-10-30 16:46:17 -0700 | [diff] [blame] | 32 | * <p> |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 33 | * Make sure that an Intent should be immutable when a new type is defined. |
Thomas Vachuska | 4b42077 | 2014-10-30 16:46:17 -0700 | [diff] [blame] | 34 | * </p> |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 35 | */ |
Brian O'Connor | 9476fa1 | 2015-06-25 15:17:17 -0400 | [diff] [blame] | 36 | @Beta |
Sho SHIMIZU | 5e5d4aa | 2015-01-26 16:12:11 -0800 | [diff] [blame] | 37 | public abstract class Intent { |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 38 | |
| 39 | private final IntentId id; |
Brian O'Connor | ea4d7d1 | 2015-01-28 16:37:46 -0800 | [diff] [blame] | 40 | |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 41 | private final ApplicationId appId; |
Ray Milkey | 5b3717e | 2015-02-05 11:44:08 -0800 | [diff] [blame] | 42 | private final Key key; |
Brian O'Connor | ea4d7d1 | 2015-01-28 16:37:46 -0800 | [diff] [blame] | 43 | |
Ray Milkey | c24cde3 | 2015-03-10 18:20:18 -0700 | [diff] [blame] | 44 | 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 Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 49 | private final Collection<NetworkResource> resources; |
| 50 | |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 51 | private static IdGenerator idGenerator; |
| 52 | |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 53 | /** |
| 54 | * Constructor for serializer. |
| 55 | */ |
| 56 | protected Intent() { |
| 57 | this.id = null; |
| 58 | this.appId = null; |
Brian O'Connor | ea4d7d1 | 2015-01-28 16:37:46 -0800 | [diff] [blame] | 59 | this.key = null; |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 60 | this.resources = null; |
Ray Milkey | c24cde3 | 2015-03-10 18:20:18 -0700 | [diff] [blame] | 61 | this.priority = DEFAULT_INTENT_PRIORITY; |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Creates a new intent. |
| 66 | * |
Thomas Vachuska | 6519e6f | 2015-03-11 02:29:31 -0700 | [diff] [blame] | 67 | * @param appId application identifier |
Thomas Vachuska | 6519e6f | 2015-03-11 02:29:31 -0700 | [diff] [blame] | 68 | * @param key optional key |
| 69 | * @param resources required network resources (optional) |
| 70 | * @param priority flow rule priority |
| 71 | */ |
Brian O'Connor | ea4d7d1 | 2015-01-28 16:37:46 -0800 | [diff] [blame] | 72 | protected Intent(ApplicationId appId, |
Ray Milkey | 5b3717e | 2015-02-05 11:44:08 -0800 | [diff] [blame] | 73 | Key key, |
Ray Milkey | c24cde3 | 2015-03-10 18:20:18 -0700 | [diff] [blame] | 74 | Collection<NetworkResource> resources, |
| 75 | int priority) { |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 76 | checkState(idGenerator != null, "Id generator is not bound."); |
Ray Milkey | c24cde3 | 2015-03-10 18:20:18 -0700 | [diff] [blame] | 77 | checkArgument(priority <= MAX_PRIORITY && priority >= MIN_PRIORITY); |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 78 | this.id = IntentId.valueOf(idGenerator.getNewId()); |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 79 | this.appId = checkNotNull(appId, "Application ID cannot be null"); |
Ray Milkey | 5b3717e | 2015-02-05 11:44:08 -0800 | [diff] [blame] | 80 | this.key = (key != null) ? key : Key.of(id.fingerprint(), appId); |
Ray Milkey | c24cde3 | 2015-03-10 18:20:18 -0700 | [diff] [blame] | 81 | this.priority = priority; |
Sho SHIMIZU | d7d1800 | 2015-01-21 14:37:14 -0800 | [diff] [blame] | 82 | this.resources = checkNotNull(resources); |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 83 | } |
| 84 | |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 85 | /** |
Ray Milkey | 3e3ec5f | 2015-03-17 17:00:38 -0700 | [diff] [blame] | 86 | * 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 | /** |
Jonathan Hart | b14221c | 2016-03-07 09:55:50 -0800 | [diff] [blame] | 94 | * Creates a new empty builder. |
| 95 | */ |
| 96 | protected Builder() { |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Creates a new builder pre-populated with the information in the given |
| 101 | * intent. |
| 102 | * |
| 103 | * @param intent initial intent |
| 104 | */ |
| 105 | protected Builder(Intent intent) { |
| 106 | this.appId(intent.appId()) |
| 107 | .key(intent.key()) |
| 108 | .priority(intent.priority()); |
| 109 | } |
| 110 | |
| 111 | /** |
Ray Milkey | 3e3ec5f | 2015-03-17 17:00:38 -0700 | [diff] [blame] | 112 | * Sets the application id for the intent that will be built. |
| 113 | * |
| 114 | * @param appId application id to use for built intent |
| 115 | * @return this builder |
| 116 | */ |
| 117 | public Builder appId(ApplicationId appId) { |
| 118 | this.appId = appId; |
| 119 | return this; |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Sets the key for the intent that will be built. |
| 124 | * |
| 125 | * @param key key to use for built intent |
| 126 | * @return this builder |
| 127 | */ |
| 128 | public Builder key(Key key) { |
| 129 | this.key = key; |
| 130 | return this; |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Sets the priority for the intent that will be built. |
| 135 | * |
| 136 | * @param priority priority to use for built intent |
| 137 | * @return this builder |
| 138 | */ |
| 139 | public Builder priority(int priority) { |
| 140 | this.priority = priority; |
| 141 | return this; |
| 142 | } |
| 143 | |
| 144 | } |
| 145 | |
| 146 | /** |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 147 | * Returns the intent identifier. |
| 148 | * |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 149 | * @return intent fingerprint |
| 150 | */ |
| 151 | public IntentId id() { |
| 152 | return id; |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * Returns the identifier of the application that requested the intent. |
| 157 | * |
| 158 | * @return application identifier |
| 159 | */ |
| 160 | public ApplicationId appId() { |
| 161 | return appId; |
| 162 | } |
| 163 | |
| 164 | /** |
Ray Milkey | c24cde3 | 2015-03-10 18:20:18 -0700 | [diff] [blame] | 165 | * Returns the priority of the intent. |
| 166 | * |
| 167 | * @return intent priority |
| 168 | */ |
| 169 | public int priority() { |
| 170 | return priority; |
| 171 | } |
| 172 | |
| 173 | /** |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 174 | * Returns the collection of resources required for this intent. |
| 175 | * |
| 176 | * @return collection of resources; may be null |
| 177 | */ |
| 178 | public Collection<NetworkResource> resources() { |
| 179 | return resources; |
| 180 | } |
| 181 | |
| 182 | /** |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 183 | * Indicates whether or not the intent is installable. |
| 184 | * |
| 185 | * @return true if installable |
| 186 | */ |
| 187 | public boolean isInstallable() { |
| 188 | return false; |
| 189 | } |
| 190 | |
| 191 | @Override |
Thomas Vachuska | 46c07ad | 2014-10-21 16:01:01 -0700 | [diff] [blame] | 192 | public final int hashCode() { |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 193 | return id.hashCode(); |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | @Override |
Thomas Vachuska | 46c07ad | 2014-10-21 16:01:01 -0700 | [diff] [blame] | 197 | public final boolean equals(Object obj) { |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 198 | if (this == obj) { |
| 199 | return true; |
| 200 | } |
| 201 | if (obj == null || getClass() != obj.getClass()) { |
| 202 | return false; |
| 203 | } |
| 204 | final Intent other = (Intent) obj; |
Sho SHIMIZU | 2389523 | 2014-11-25 15:59:50 -0800 | [diff] [blame] | 205 | return this.id().equals(other.id()); |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 206 | } |
| 207 | |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 208 | /** |
| 209 | * Binds an id generator for unique intent id generation. |
| 210 | * |
| 211 | * Note: A generator cannot be bound if there is already a generator bound. |
Thomas Vachuska | 6519e6f | 2015-03-11 02:29:31 -0700 | [diff] [blame] | 212 | * |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 213 | * @param newIdGenerator id generator |
| 214 | */ |
Sho SHIMIZU | f57a739 | 2014-11-25 16:11:32 -0800 | [diff] [blame] | 215 | public static void bindIdGenerator(IdGenerator newIdGenerator) { |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 216 | checkState(idGenerator == null, "Id generator is already bound."); |
| 217 | idGenerator = checkNotNull(newIdGenerator); |
| 218 | } |
| 219 | |
| 220 | /** |
| 221 | * Unbinds an id generator. |
| 222 | * |
| 223 | * Note: The caller must provide the old id generator to succeed. |
Thomas Vachuska | 6519e6f | 2015-03-11 02:29:31 -0700 | [diff] [blame] | 224 | * |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 225 | * @param oldIdGenerator the current id generator |
| 226 | */ |
Sho SHIMIZU | f57a739 | 2014-11-25 16:11:32 -0800 | [diff] [blame] | 227 | public static void unbindIdGenerator(IdGenerator oldIdGenerator) { |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 228 | if (Objects.equals(idGenerator, oldIdGenerator)) { |
| 229 | idGenerator = null; |
| 230 | } |
| 231 | } |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 232 | |
Ray Milkey | 5b3717e | 2015-02-05 11:44:08 -0800 | [diff] [blame] | 233 | public Key key() { |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 234 | return key; |
| 235 | } |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 236 | } |