Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 3 | * |
| 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 |
| 7 | * |
| 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. |
| 15 | */ |
| 16 | package org.onosproject.net.intent; |
| 17 | |
Brian O'Connor | 9476fa1 | 2015-06-25 15:17:17 -0400 | [diff] [blame] | 18 | import com.google.common.annotations.Beta; |
Ray Milkey | 65cb59a | 2015-02-10 15:18:26 -0800 | [diff] [blame] | 19 | import com.google.common.base.MoreObjects; |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 20 | import com.google.common.collect.ImmutableList; |
Brian O'Connor | 5eb77c8 | 2015-03-02 18:09:39 -0800 | [diff] [blame] | 21 | import org.onosproject.cluster.NodeId; |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 22 | import org.onosproject.store.Timestamp; |
Jonathan Hart | 72175c2 | 2015-03-24 18:55:58 -0700 | [diff] [blame] | 23 | import org.slf4j.Logger; |
| 24 | import org.slf4j.LoggerFactory; |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 25 | |
Brian O'Connor | f0c5a05 | 2015-04-27 00:34:53 -0700 | [diff] [blame] | 26 | import java.util.Collections; |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 27 | import java.util.List; |
| 28 | import java.util.Objects; |
| 29 | |
Jonathan Hart | 0d18df3 | 2015-03-21 08:42:59 -0700 | [diff] [blame] | 30 | import static com.google.common.base.Preconditions.checkNotNull; |
Brian O'Connor | f0c5a05 | 2015-04-27 00:34:53 -0700 | [diff] [blame] | 31 | import static org.onosproject.net.intent.IntentState.*; |
Jonathan Hart | 0d18df3 | 2015-03-21 08:42:59 -0700 | [diff] [blame] | 32 | |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 33 | /** |
| 34 | * A wrapper class that contains an intents, its state, and other metadata for |
| 35 | * internal use. |
| 36 | */ |
Brian O'Connor | 9476fa1 | 2015-06-25 15:17:17 -0400 | [diff] [blame] | 37 | @Beta |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 38 | public class IntentData { //FIXME need to make this "immutable" |
| 39 | // manager should be able to mutate a local copy while processing |
Jonathan Hart | 72175c2 | 2015-03-24 18:55:58 -0700 | [diff] [blame] | 40 | |
| 41 | private static final Logger log = LoggerFactory.getLogger(IntentData.class); |
| 42 | |
Jonathan Hart | 0d18df3 | 2015-03-21 08:42:59 -0700 | [diff] [blame] | 43 | private final Intent intent; |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 44 | |
Brian O'Connor | f0c5a05 | 2015-04-27 00:34:53 -0700 | [diff] [blame] | 45 | private final IntentState request; //TODO perhaps we want a full fledged object for requests |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 46 | private IntentState state; |
Sho SHIMIZU | cbcc02b | 2016-03-31 13:22:42 -0700 | [diff] [blame] | 47 | private final Timestamp version; |
Brian O'Connor | 5eb77c8 | 2015-03-02 18:09:39 -0800 | [diff] [blame] | 48 | private NodeId origin; |
Brian O'Connor | 6d8e317 | 2015-04-30 15:43:57 -0700 | [diff] [blame] | 49 | private int errorCount; |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 50 | |
| 51 | private List<Intent> installables; |
| 52 | |
Jonathan Hart | 0d18df3 | 2015-03-21 08:42:59 -0700 | [diff] [blame] | 53 | /** |
| 54 | * Creates a new intent data object. |
| 55 | * |
| 56 | * @param intent intent this metadata references |
| 57 | * @param state intent state |
| 58 | * @param version version of the intent for this key |
| 59 | */ |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 60 | public IntentData(Intent intent, IntentState state, Timestamp version) { |
Sho SHIMIZU | 70b88d8 | 2016-01-19 14:27:22 -0800 | [diff] [blame] | 61 | checkNotNull(intent); |
| 62 | checkNotNull(state); |
| 63 | |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 64 | this.intent = intent; |
| 65 | this.state = state; |
Brian O'Connor | f0c5a05 | 2015-04-27 00:34:53 -0700 | [diff] [blame] | 66 | this.request = state; |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 67 | this.version = version; |
| 68 | } |
| 69 | |
Jonathan Hart | 0d18df3 | 2015-03-21 08:42:59 -0700 | [diff] [blame] | 70 | /** |
Sho SHIMIZU | 2f26fb2 | 2016-01-19 14:04:46 -0800 | [diff] [blame] | 71 | * Creates a new intent data object. |
| 72 | * |
| 73 | * @param intent intent this metadata references |
| 74 | * @param state intent state |
| 75 | * @param version version of the intent for this key |
| 76 | * @param origin ID of the node where the data was originally created |
| 77 | */ |
| 78 | public IntentData(Intent intent, IntentState state, Timestamp version, NodeId origin) { |
| 79 | checkNotNull(intent); |
| 80 | checkNotNull(state); |
| 81 | checkNotNull(version); |
| 82 | checkNotNull(origin); |
| 83 | |
| 84 | this.intent = intent; |
| 85 | this.state = state; |
| 86 | this.request = state; |
| 87 | this.version = version; |
| 88 | this.origin = origin; |
| 89 | } |
| 90 | |
| 91 | /** |
Brian O'Connor | c590ebb | 2016-12-08 18:16:41 -0800 | [diff] [blame] | 92 | * Creates a new intent data object. |
| 93 | * |
| 94 | * @param intent intent this metadata references |
| 95 | * @param state intent state |
| 96 | * @param request intent request |
| 97 | * @param version version of the intent for this key |
| 98 | * @param origin ID of the node where the data was originally created |
| 99 | */ |
| 100 | public IntentData(Intent intent, IntentState state, IntentState request, Timestamp version, NodeId origin) { |
| 101 | checkNotNull(intent); |
| 102 | checkNotNull(state); |
| 103 | checkNotNull(request); |
| 104 | checkNotNull(version); |
| 105 | checkNotNull(origin); |
| 106 | |
| 107 | this.intent = intent; |
| 108 | this.state = state; |
| 109 | this.request = request; |
| 110 | this.version = version; |
| 111 | this.origin = origin; |
| 112 | } |
| 113 | |
| 114 | /** |
Jonathan Hart | 0d18df3 | 2015-03-21 08:42:59 -0700 | [diff] [blame] | 115 | * Copy constructor. |
| 116 | * |
| 117 | * @param intentData intent data to copy |
| 118 | */ |
| 119 | public IntentData(IntentData intentData) { |
| 120 | checkNotNull(intentData); |
| 121 | |
| 122 | intent = intentData.intent; |
| 123 | state = intentData.state; |
Brian O'Connor | f0c5a05 | 2015-04-27 00:34:53 -0700 | [diff] [blame] | 124 | request = intentData.request; |
Jonathan Hart | 0d18df3 | 2015-03-21 08:42:59 -0700 | [diff] [blame] | 125 | version = intentData.version; |
| 126 | origin = intentData.origin; |
| 127 | installables = intentData.installables; |
Brian O'Connor | 6d8e317 | 2015-04-30 15:43:57 -0700 | [diff] [blame] | 128 | errorCount = intentData.errorCount; |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 129 | } |
| 130 | |
Sho SHIMIZU | 2f26fb2 | 2016-01-19 14:04:46 -0800 | [diff] [blame] | 131 | /** |
| 132 | * Create a new instance based on the original instance with new installables. |
| 133 | * |
| 134 | * @param original original data |
| 135 | * @param installables new installable intents to set |
| 136 | */ |
| 137 | public IntentData(IntentData original, List<Intent> installables) { |
| 138 | this(original); |
| 139 | |
Brian O'Connor | a78f060 | 2016-09-22 10:56:08 -0700 | [diff] [blame] | 140 | this.installables = checkNotNull(installables).isEmpty() ? |
| 141 | Collections.emptyList() : ImmutableList.copyOf(installables); |
Sho SHIMIZU | 2f26fb2 | 2016-01-19 14:04:46 -0800 | [diff] [blame] | 142 | } |
| 143 | |
Jonathan Hart | 0d18df3 | 2015-03-21 08:42:59 -0700 | [diff] [blame] | 144 | // kryo constructor |
| 145 | protected IntentData() { |
| 146 | intent = null; |
Brian O'Connor | f0c5a05 | 2015-04-27 00:34:53 -0700 | [diff] [blame] | 147 | request = null; |
Sho SHIMIZU | cbcc02b | 2016-03-31 13:22:42 -0700 | [diff] [blame] | 148 | version = null; |
Jonathan Hart | 0d18df3 | 2015-03-21 08:42:59 -0700 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | /** |
| 152 | * Returns the intent this metadata references. |
| 153 | * |
| 154 | * @return intent |
| 155 | */ |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 156 | public Intent intent() { |
| 157 | return intent; |
| 158 | } |
| 159 | |
Jonathan Hart | 0d18df3 | 2015-03-21 08:42:59 -0700 | [diff] [blame] | 160 | /** |
| 161 | * Returns the state of the intent. |
| 162 | * |
| 163 | * @return intent state |
| 164 | */ |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 165 | public IntentState state() { |
| 166 | return state; |
| 167 | } |
| 168 | |
Brian O'Connor | f0c5a05 | 2015-04-27 00:34:53 -0700 | [diff] [blame] | 169 | public IntentState request() { |
| 170 | return request; |
| 171 | } |
| 172 | |
Jonathan Hart | 0d18df3 | 2015-03-21 08:42:59 -0700 | [diff] [blame] | 173 | /** |
| 174 | * Returns the intent key. |
| 175 | * |
| 176 | * @return intent key |
| 177 | */ |
Ray Milkey | 5b3717e | 2015-02-05 11:44:08 -0800 | [diff] [blame] | 178 | public Key key() { |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 179 | return intent.key(); |
| 180 | } |
| 181 | |
Jonathan Hart | 0d18df3 | 2015-03-21 08:42:59 -0700 | [diff] [blame] | 182 | /** |
| 183 | * Returns the version of the intent for this key. |
| 184 | * |
| 185 | * @return intent version |
| 186 | */ |
Brian O'Connor | 2ba63fd | 2015-02-09 22:48:11 -0800 | [diff] [blame] | 187 | public Timestamp version() { |
| 188 | return version; |
| 189 | } |
| 190 | |
Brian O'Connor | 5eb77c8 | 2015-03-02 18:09:39 -0800 | [diff] [blame] | 191 | /** |
Jonathan Hart | 0d18df3 | 2015-03-21 08:42:59 -0700 | [diff] [blame] | 192 | * Returns the origin node that created this intent. |
| 193 | * |
| 194 | * @return origin node ID |
| 195 | */ |
Brian O'Connor | 5eb77c8 | 2015-03-02 18:09:39 -0800 | [diff] [blame] | 196 | public NodeId origin() { |
| 197 | return origin; |
| 198 | } |
| 199 | |
Jonathan Hart | 0d18df3 | 2015-03-21 08:42:59 -0700 | [diff] [blame] | 200 | /** |
| 201 | * Updates the state of the intent to the given new state. |
| 202 | * |
| 203 | * @param newState new state of the intent |
| 204 | */ |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 205 | public void setState(IntentState newState) { |
| 206 | this.state = newState; |
| 207 | } |
| 208 | |
Brian O'Connor | 2ba63fd | 2015-02-09 22:48:11 -0800 | [diff] [blame] | 209 | /** |
Brian O'Connor | 6d8e317 | 2015-04-30 15:43:57 -0700 | [diff] [blame] | 210 | * Increments the error count for this intent. |
| 211 | */ |
| 212 | public void incrementErrorCount() { |
| 213 | errorCount++; |
| 214 | } |
| 215 | |
| 216 | /** |
| 217 | * Sets the error count for this intent. |
| 218 | * |
| 219 | * @param newCount new count |
| 220 | */ |
| 221 | public void setErrorCount(int newCount) { |
| 222 | errorCount = newCount; |
| 223 | } |
| 224 | |
| 225 | /** |
| 226 | * Returns the number of times that this intent has encountered an error |
| 227 | * during installation or withdrawal. |
| 228 | * |
| 229 | * @return error count |
| 230 | */ |
| 231 | public int errorCount() { |
| 232 | return errorCount; |
| 233 | } |
| 234 | |
| 235 | /** |
Jonathan Hart | 0d18df3 | 2015-03-21 08:42:59 -0700 | [diff] [blame] | 236 | * Returns the installables associated with this intent. |
| 237 | * |
| 238 | * @return list of installable intents |
| 239 | */ |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 240 | public List<Intent> installables() { |
Brian O'Connor | f0c5a05 | 2015-04-27 00:34:53 -0700 | [diff] [blame] | 241 | return installables != null ? installables : Collections.emptyList(); |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 242 | } |
| 243 | |
Jonathan Hart | 72175c2 | 2015-03-24 18:55:58 -0700 | [diff] [blame] | 244 | /** |
| 245 | * Determines whether an intent data update is allowed. The update must |
| 246 | * either have a higher version than the current data, or the state |
| 247 | * transition between two updates of the same version must be sane. |
| 248 | * |
| 249 | * @param currentData existing intent data in the store |
| 250 | * @param newData new intent data update proposal |
| 251 | * @return true if we can apply the update, otherwise false |
| 252 | */ |
| 253 | public static boolean isUpdateAcceptable(IntentData currentData, IntentData newData) { |
| 254 | |
| 255 | if (currentData == null) { |
| 256 | return true; |
Sho SHIMIZU | 2c05f91 | 2015-04-10 14:23:16 -0700 | [diff] [blame] | 257 | } else if (currentData.version().isOlderThan(newData.version())) { |
Jonathan Hart | 72175c2 | 2015-03-24 18:55:58 -0700 | [diff] [blame] | 258 | return true; |
Sho SHIMIZU | 2c05f91 | 2015-04-10 14:23:16 -0700 | [diff] [blame] | 259 | } else if (currentData.version().isNewerThan(newData.version())) { |
Jonathan Hart | 72175c2 | 2015-03-24 18:55:58 -0700 | [diff] [blame] | 260 | return false; |
| 261 | } |
| 262 | |
| 263 | // current and new data versions are the same |
| 264 | IntentState currentState = currentData.state(); |
| 265 | IntentState newState = newData.state(); |
| 266 | |
| 267 | switch (newState) { |
| 268 | case INSTALLING: |
| 269 | if (currentState == INSTALLING) { |
| 270 | return false; |
| 271 | } |
| 272 | // FALLTHROUGH |
| 273 | case INSTALLED: |
| 274 | if (currentState == INSTALLED) { |
| 275 | return false; |
| 276 | } else if (currentState == WITHDRAWING || currentState == WITHDRAWN |
| 277 | || currentState == PURGE_REQ) { |
| 278 | log.warn("Invalid state transition from {} to {} for intent {}", |
| 279 | currentState, newState, newData.key()); |
| 280 | return false; |
| 281 | } |
| 282 | return true; |
| 283 | |
| 284 | case WITHDRAWING: |
| 285 | if (currentState == WITHDRAWING) { |
| 286 | return false; |
| 287 | } |
| 288 | // FALLTHROUGH |
| 289 | case WITHDRAWN: |
| 290 | if (currentState == WITHDRAWN) { |
| 291 | return false; |
| 292 | } else if (currentState == INSTALLING || currentState == INSTALLED |
| 293 | || currentState == PURGE_REQ) { |
| 294 | log.warn("Invalid state transition from {} to {} for intent {}", |
| 295 | currentState, newState, newData.key()); |
| 296 | return false; |
| 297 | } |
| 298 | return true; |
| 299 | |
| 300 | case FAILED: |
| 301 | if (currentState == FAILED) { |
| 302 | return false; |
| 303 | } |
| 304 | return true; |
| 305 | |
Brian O'Connor | f0c5a05 | 2015-04-27 00:34:53 -0700 | [diff] [blame] | 306 | case CORRUPT: |
| 307 | if (currentState == CORRUPT) { |
| 308 | return false; |
| 309 | } |
| 310 | return true; |
| 311 | |
Jonathan Hart | 72175c2 | 2015-03-24 18:55:58 -0700 | [diff] [blame] | 312 | case PURGE_REQ: |
Brian O'Connor | 597934f | 2015-07-16 11:44:03 -0700 | [diff] [blame] | 313 | // TODO we should enforce that only WITHDRAWN intents can be purged |
Jonathan Hart | 72175c2 | 2015-03-24 18:55:58 -0700 | [diff] [blame] | 314 | return true; |
| 315 | |
| 316 | case COMPILING: |
| 317 | case RECOMPILING: |
| 318 | case INSTALL_REQ: |
| 319 | case WITHDRAW_REQ: |
| 320 | default: |
| 321 | log.warn("Invalid state {} for intent {}", newState, newData.key()); |
| 322 | return false; |
| 323 | } |
| 324 | } |
| 325 | |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 326 | @Override |
| 327 | public int hashCode() { |
| 328 | return Objects.hash(intent, version); |
| 329 | } |
| 330 | |
| 331 | @Override |
| 332 | public boolean equals(Object obj) { |
| 333 | if (this == obj) { |
| 334 | return true; |
| 335 | } |
| 336 | if (obj == null || getClass() != obj.getClass()) { |
| 337 | return false; |
| 338 | } |
| 339 | final IntentData other = (IntentData) obj; |
| 340 | return Objects.equals(this.intent, other.intent) |
| 341 | && Objects.equals(this.version, other.version); |
| 342 | } |
Ray Milkey | 65cb59a | 2015-02-10 15:18:26 -0800 | [diff] [blame] | 343 | |
Jonathan Hart | 0d18df3 | 2015-03-21 08:42:59 -0700 | [diff] [blame] | 344 | @Override |
Ray Milkey | 65cb59a | 2015-02-10 15:18:26 -0800 | [diff] [blame] | 345 | public String toString() { |
| 346 | return MoreObjects.toStringHelper(getClass()) |
| 347 | .add("key", key()) |
| 348 | .add("state", state()) |
| 349 | .add("version", version()) |
Ray Milkey | 9f74c08 | 2015-02-11 15:40:16 -0800 | [diff] [blame] | 350 | .add("intent", intent()) |
Jonathan Hart | 0d18df3 | 2015-03-21 08:42:59 -0700 | [diff] [blame] | 351 | .add("origin", origin()) |
Jonathan Hart | d0ba217 | 2015-02-11 13:54:33 -0800 | [diff] [blame] | 352 | .add("installables", installables()) |
Ray Milkey | 65cb59a | 2015-02-10 15:18:26 -0800 | [diff] [blame] | 353 | .toString(); |
| 354 | } |
| 355 | |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 356 | } |