Thomas Vachuska | 83e090e | 2014-10-22 14:25:35 -0700 | [diff] [blame] | 1 | /* |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 2 | * Copyright 2014 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 | |
Ray Milkey | 3e3ec5f | 2015-03-17 17:00:38 -0700 | [diff] [blame] | 18 | import java.util.Collection; |
| 19 | import java.util.Objects; |
| 20 | |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 21 | import org.onosproject.core.ApplicationId; |
| 22 | import org.onosproject.core.IdGenerator; |
| 23 | import org.onosproject.net.NetworkResource; |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 24 | |
Ray Milkey | c24cde3 | 2015-03-10 18:20:18 -0700 | [diff] [blame] | 25 | import static com.google.common.base.Preconditions.checkArgument; |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 26 | import static com.google.common.base.Preconditions.checkNotNull; |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 27 | import static com.google.common.base.Preconditions.checkState; |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 28 | |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 29 | /** |
| 30 | * Abstraction of an application level intent. |
Thomas Vachuska | 4b42077 | 2014-10-30 16:46:17 -0700 | [diff] [blame] | 31 | * <p> |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 32 | * 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] | 33 | * </p> |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 34 | */ |
Sho SHIMIZU | 5e5d4aa | 2015-01-26 16:12:11 -0800 | [diff] [blame] | 35 | public abstract class Intent { |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 36 | |
| 37 | private final IntentId id; |
Brian O'Connor | ea4d7d1 | 2015-01-28 16:37:46 -0800 | [diff] [blame] | 38 | |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 39 | private final ApplicationId appId; |
Ray Milkey | 5b3717e | 2015-02-05 11:44:08 -0800 | [diff] [blame] | 40 | private final Key key; |
Brian O'Connor | ea4d7d1 | 2015-01-28 16:37:46 -0800 | [diff] [blame] | 41 | |
Ray Milkey | c24cde3 | 2015-03-10 18:20:18 -0700 | [diff] [blame] | 42 | 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 Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 47 | private final Collection<NetworkResource> resources; |
| 48 | |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 49 | private static IdGenerator idGenerator; |
| 50 | |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 51 | /** |
| 52 | * Constructor for serializer. |
| 53 | */ |
| 54 | protected Intent() { |
| 55 | this.id = null; |
| 56 | this.appId = null; |
Brian O'Connor | ea4d7d1 | 2015-01-28 16:37:46 -0800 | [diff] [blame] | 57 | this.key = null; |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 58 | this.resources = null; |
Ray Milkey | c24cde3 | 2015-03-10 18:20:18 -0700 | [diff] [blame] | 59 | this.priority = DEFAULT_INTENT_PRIORITY; |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Creates a new intent. |
| 64 | * |
Thomas Vachuska | 6519e6f | 2015-03-11 02:29:31 -0700 | [diff] [blame] | 65 | * @param appId application identifier |
Thomas Vachuska | 6519e6f | 2015-03-11 02:29:31 -0700 | [diff] [blame] | 66 | * @param key optional key |
| 67 | * @param resources required network resources (optional) |
| 68 | * @param priority flow rule priority |
| 69 | */ |
Brian O'Connor | ea4d7d1 | 2015-01-28 16:37:46 -0800 | [diff] [blame] | 70 | protected Intent(ApplicationId appId, |
Ray Milkey | 5b3717e | 2015-02-05 11:44:08 -0800 | [diff] [blame] | 71 | Key key, |
Ray Milkey | c24cde3 | 2015-03-10 18:20:18 -0700 | [diff] [blame] | 72 | Collection<NetworkResource> resources, |
| 73 | int priority) { |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 74 | checkState(idGenerator != null, "Id generator is not bound."); |
Ray Milkey | c24cde3 | 2015-03-10 18:20:18 -0700 | [diff] [blame] | 75 | checkArgument(priority <= MAX_PRIORITY && priority >= MIN_PRIORITY); |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 76 | this.id = IntentId.valueOf(idGenerator.getNewId()); |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 77 | this.appId = checkNotNull(appId, "Application ID cannot be null"); |
Ray Milkey | 5b3717e | 2015-02-05 11:44:08 -0800 | [diff] [blame] | 78 | this.key = (key != null) ? key : Key.of(id.fingerprint(), appId); |
Ray Milkey | c24cde3 | 2015-03-10 18:20:18 -0700 | [diff] [blame] | 79 | this.priority = priority; |
Sho SHIMIZU | d7d1800 | 2015-01-21 14:37:14 -0800 | [diff] [blame] | 80 | this.resources = checkNotNull(resources); |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 81 | } |
| 82 | |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 83 | /** |
Ray Milkey | 3e3ec5f | 2015-03-17 17:00:38 -0700 | [diff] [blame] | 84 | * 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'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 127 | * Returns the intent identifier. |
| 128 | * |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 129 | * @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 Milkey | c24cde3 | 2015-03-10 18:20:18 -0700 | [diff] [blame] | 145 | * Returns the priority of the intent. |
| 146 | * |
| 147 | * @return intent priority |
| 148 | */ |
| 149 | public int priority() { |
| 150 | return priority; |
| 151 | } |
| 152 | |
| 153 | /** |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 154 | * 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 Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 163 | * 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 Vachuska | 46c07ad | 2014-10-21 16:01:01 -0700 | [diff] [blame] | 172 | public final int hashCode() { |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 173 | return id.hashCode(); |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | @Override |
Thomas Vachuska | 46c07ad | 2014-10-21 16:01:01 -0700 | [diff] [blame] | 177 | public final boolean equals(Object obj) { |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 178 | 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 SHIMIZU | 2389523 | 2014-11-25 15:59:50 -0800 | [diff] [blame] | 185 | return this.id().equals(other.id()); |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 186 | } |
| 187 | |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 188 | /** |
| 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 Vachuska | 6519e6f | 2015-03-11 02:29:31 -0700 | [diff] [blame] | 192 | * |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 193 | * @param newIdGenerator id generator |
| 194 | */ |
Sho SHIMIZU | f57a739 | 2014-11-25 16:11:32 -0800 | [diff] [blame] | 195 | public static void bindIdGenerator(IdGenerator newIdGenerator) { |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 196 | 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 Vachuska | 6519e6f | 2015-03-11 02:29:31 -0700 | [diff] [blame] | 204 | * |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 205 | * @param oldIdGenerator the current id generator |
| 206 | */ |
Sho SHIMIZU | f57a739 | 2014-11-25 16:11:32 -0800 | [diff] [blame] | 207 | public static void unbindIdGenerator(IdGenerator oldIdGenerator) { |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 208 | if (Objects.equals(idGenerator, oldIdGenerator)) { |
| 209 | idGenerator = null; |
| 210 | } |
| 211 | } |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 212 | |
Ray Milkey | 5b3717e | 2015-02-05 11:44:08 -0800 | [diff] [blame] | 213 | public Key key() { |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 214 | return key; |
| 215 | } |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 216 | } |