Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
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; |
Yuta HIGUCHI | 4f8a377 | 2017-05-16 20:23:49 -0700 | [diff] [blame] | 47 | /** |
| 48 | * Intent's user request version. |
| 49 | * <p> |
| 50 | * version is assigned when an Intent was picked up by batch worker |
| 51 | * and added to pending map. |
| 52 | */ |
Sho SHIMIZU | cbcc02b | 2016-03-31 13:22:42 -0700 | [diff] [blame] | 53 | private final Timestamp version; |
Yuta HIGUCHI | 4f8a377 | 2017-05-16 20:23:49 -0700 | [diff] [blame] | 54 | /** |
| 55 | * Intent's internal state version. |
| 56 | */ |
| 57 | // ~= mutation count |
| 58 | private int internalStateVersion; |
Brian O'Connor | 5eb77c8 | 2015-03-02 18:09:39 -0800 | [diff] [blame] | 59 | private NodeId origin; |
Brian O'Connor | 6d8e317 | 2015-04-30 15:43:57 -0700 | [diff] [blame] | 60 | private int errorCount; |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 61 | |
| 62 | private List<Intent> installables; |
| 63 | |
Jonathan Hart | 0d18df3 | 2015-03-21 08:42:59 -0700 | [diff] [blame] | 64 | /** |
Yuta HIGUCHI | 4f8a377 | 2017-05-16 20:23:49 -0700 | [diff] [blame] | 65 | * Creates IntentData for Intent submit request. |
| 66 | * |
| 67 | * @param intent to request |
| 68 | * @return IntentData |
| 69 | */ |
| 70 | public static IntentData submit(Intent intent) { |
| 71 | return new IntentData(checkNotNull(intent), INSTALL_REQ); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Creates IntentData for Intent withdraw request. |
| 76 | * |
| 77 | * @param intent to request |
| 78 | * @return IntentData |
| 79 | */ |
| 80 | public static IntentData withdraw(Intent intent) { |
| 81 | return new IntentData(checkNotNull(intent), WITHDRAW_REQ); |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Creates IntentData for Intent purge request. |
| 86 | * |
| 87 | * @param intent to request |
| 88 | * @return IntentData |
| 89 | */ |
| 90 | public static IntentData purge(Intent intent) { |
| 91 | return new IntentData(checkNotNull(intent), PURGE_REQ); |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Creates updated IntentData after assigning task to a node. |
| 96 | * |
| 97 | * @param data IntentData to update work assignment |
| 98 | * @param timestamp to assign to current request |
| 99 | * @param node node which was assigned to handle this request (local node id) |
| 100 | * @return updated IntentData object |
| 101 | */ |
| 102 | public static IntentData assign(IntentData data, Timestamp timestamp, NodeId node) { |
| 103 | IntentData assigned = new IntentData(data, checkNotNull(timestamp)); |
| 104 | assigned.origin = checkNotNull(node); |
| 105 | assigned.internalStateVersion++; |
| 106 | return assigned; |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Creates a copy of given IntentData. |
| 111 | * |
| 112 | * @param data intent data to copy |
| 113 | * @return copy |
| 114 | */ |
| 115 | public static IntentData copy(IntentData data) { |
| 116 | return new IntentData(data); |
| 117 | } |
| 118 | |
| 119 | /** |
jaegonkim | cbe1c5e | 2018-05-20 15:11:18 +0900 | [diff] [blame] | 120 | * Creates a copy of given IntentData, and update request version. |
| 121 | * |
| 122 | * @param data intent data to copy |
| 123 | * @param reqVersion request version to be updated |
| 124 | * @return copy |
| 125 | */ |
| 126 | public static IntentData copy(IntentData data, Timestamp reqVersion) { |
| 127 | return new IntentData(data, checkNotNull(reqVersion)); |
| 128 | } |
| 129 | |
| 130 | /** |
Yuta HIGUCHI | 4f8a377 | 2017-05-16 20:23:49 -0700 | [diff] [blame] | 131 | * Create a copy of IntentData in next state. |
| 132 | * |
| 133 | * @param data intent data to copy |
| 134 | * @param nextState to transition to |
| 135 | * @return next state |
| 136 | */ |
| 137 | public static IntentData nextState(IntentData data, IntentState nextState) { |
| 138 | IntentData next = new IntentData(data); |
| 139 | // TODO state machine sanity check |
| 140 | next.setState(checkNotNull(nextState)); |
| 141 | return next; |
| 142 | } |
| 143 | |
| 144 | // TODO Should this be method of it's own, or |
| 145 | // should nextState(*, CORRUPT) call increment error count? |
| 146 | /** |
| 147 | * Creates a copy of IntentData in corrupt state, |
| 148 | * incrementing error count. |
| 149 | * |
| 150 | * @param data intent data to copy |
| 151 | * @return next state |
| 152 | */ |
| 153 | public static IntentData corrupt(IntentData data) { |
| 154 | IntentData next = new IntentData(data); |
| 155 | next.setState(IntentState.CORRUPT); |
| 156 | next.incrementErrorCount(); |
| 157 | return next; |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Creates updated IntentData with compilation result. |
| 162 | * |
| 163 | * @param data IntentData to update |
| 164 | * @param installables compilation result |
| 165 | * @return updated IntentData object |
| 166 | */ |
| 167 | public static IntentData compiled(IntentData data, List<Intent> installables) { |
| 168 | return new IntentData(data, checkNotNull(installables)); |
| 169 | } |
| 170 | |
| 171 | |
| 172 | /** |
| 173 | * Constructor for creating IntentData representing user request. |
| 174 | * |
| 175 | * @param intent this metadata references |
| 176 | * @param reqState request state |
| 177 | */ |
| 178 | private IntentData(Intent intent, |
| 179 | IntentState reqState) { |
| 180 | this.intent = checkNotNull(intent); |
| 181 | this.request = checkNotNull(reqState); |
| 182 | this.version = null; |
| 183 | this.state = reqState; |
| 184 | this.installables = ImmutableList.of(); |
| 185 | } |
| 186 | |
| 187 | /** |
| 188 | * Constructor for creating updated IntentData. |
| 189 | * |
| 190 | * @param original IntentData to copy from |
| 191 | * @param newReqVersion new request version |
| 192 | */ |
| 193 | private IntentData(IntentData original, Timestamp newReqVersion) { |
| 194 | intent = original.intent; |
| 195 | state = original.state; |
| 196 | request = original.request; |
| 197 | version = newReqVersion; |
| 198 | internalStateVersion = original.internalStateVersion; |
| 199 | origin = original.origin; |
| 200 | installables = original.installables; |
| 201 | errorCount = original.errorCount; |
| 202 | } |
| 203 | |
| 204 | /** |
Jonathan Hart | 0d18df3 | 2015-03-21 08:42:59 -0700 | [diff] [blame] | 205 | * Creates a new intent data object. |
| 206 | * |
| 207 | * @param intent intent this metadata references |
| 208 | * @param state intent state |
| 209 | * @param version version of the intent for this key |
Yuta HIGUCHI | 4f8a377 | 2017-05-16 20:23:49 -0700 | [diff] [blame] | 210 | * |
| 211 | * @deprecated in 1.11.0 |
Jonathan Hart | 0d18df3 | 2015-03-21 08:42:59 -0700 | [diff] [blame] | 212 | */ |
Yuta HIGUCHI | 4f8a377 | 2017-05-16 20:23:49 -0700 | [diff] [blame] | 213 | // used to create initial IntentData (version = null) |
| 214 | @Deprecated |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 215 | public IntentData(Intent intent, IntentState state, Timestamp version) { |
Sho SHIMIZU | 70b88d8 | 2016-01-19 14:27:22 -0800 | [diff] [blame] | 216 | checkNotNull(intent); |
| 217 | checkNotNull(state); |
| 218 | |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 219 | this.intent = intent; |
| 220 | this.state = state; |
Brian O'Connor | f0c5a05 | 2015-04-27 00:34:53 -0700 | [diff] [blame] | 221 | this.request = state; |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 222 | this.version = version; |
| 223 | } |
| 224 | |
Jonathan Hart | 0d18df3 | 2015-03-21 08:42:59 -0700 | [diff] [blame] | 225 | /** |
Sho SHIMIZU | 2f26fb2 | 2016-01-19 14:04:46 -0800 | [diff] [blame] | 226 | * Creates a new intent data object. |
| 227 | * |
| 228 | * @param intent intent this metadata references |
| 229 | * @param state intent state |
| 230 | * @param version version of the intent for this key |
| 231 | * @param origin ID of the node where the data was originally created |
Yuta HIGUCHI | 4f8a377 | 2017-05-16 20:23:49 -0700 | [diff] [blame] | 232 | * |
| 233 | * @deprecated in 1.11.0 |
Sho SHIMIZU | 2f26fb2 | 2016-01-19 14:04:46 -0800 | [diff] [blame] | 234 | */ |
Yuta HIGUCHI | 4f8a377 | 2017-05-16 20:23:49 -0700 | [diff] [blame] | 235 | // No longer used in the code base anywhere |
| 236 | @Deprecated |
Sho SHIMIZU | 2f26fb2 | 2016-01-19 14:04:46 -0800 | [diff] [blame] | 237 | public IntentData(Intent intent, IntentState state, Timestamp version, NodeId origin) { |
| 238 | checkNotNull(intent); |
| 239 | checkNotNull(state); |
| 240 | checkNotNull(version); |
| 241 | checkNotNull(origin); |
| 242 | |
| 243 | this.intent = intent; |
| 244 | this.state = state; |
| 245 | this.request = state; |
| 246 | this.version = version; |
| 247 | this.origin = origin; |
| 248 | } |
| 249 | |
| 250 | /** |
Brian O'Connor | c590ebb | 2016-12-08 18:16:41 -0800 | [diff] [blame] | 251 | * Creates a new intent data object. |
| 252 | * |
| 253 | * @param intent intent this metadata references |
| 254 | * @param state intent state |
| 255 | * @param request intent request |
| 256 | * @param version version of the intent for this key |
| 257 | * @param origin ID of the node where the data was originally created |
Yuta HIGUCHI | 4f8a377 | 2017-05-16 20:23:49 -0700 | [diff] [blame] | 258 | * |
| 259 | * @deprecated in 1.11.0 |
Brian O'Connor | c590ebb | 2016-12-08 18:16:41 -0800 | [diff] [blame] | 260 | */ |
Yuta HIGUCHI | 4f8a377 | 2017-05-16 20:23:49 -0700 | [diff] [blame] | 261 | // No longer used in the code base anywhere |
| 262 | // was used when IntentData is picked up by some of the node and was assigned with a version |
| 263 | @Deprecated |
Brian O'Connor | c590ebb | 2016-12-08 18:16:41 -0800 | [diff] [blame] | 264 | public IntentData(Intent intent, IntentState state, IntentState request, Timestamp version, NodeId origin) { |
| 265 | checkNotNull(intent); |
| 266 | checkNotNull(state); |
| 267 | checkNotNull(request); |
| 268 | checkNotNull(version); |
| 269 | checkNotNull(origin); |
| 270 | |
| 271 | this.intent = intent; |
| 272 | this.state = state; |
| 273 | this.request = request; |
| 274 | this.version = version; |
| 275 | this.origin = origin; |
| 276 | } |
| 277 | |
| 278 | /** |
Jonathan Hart | 0d18df3 | 2015-03-21 08:42:59 -0700 | [diff] [blame] | 279 | * Copy constructor. |
| 280 | * |
| 281 | * @param intentData intent data to copy |
Yuta HIGUCHI | 4f8a377 | 2017-05-16 20:23:49 -0700 | [diff] [blame] | 282 | * |
| 283 | * @deprecated in 1.11.0 use {@link #copy(IntentData)} instead |
Jonathan Hart | 0d18df3 | 2015-03-21 08:42:59 -0700 | [diff] [blame] | 284 | */ |
Yuta HIGUCHI | 4f8a377 | 2017-05-16 20:23:49 -0700 | [diff] [blame] | 285 | // used to create a defensive copy |
| 286 | // to be made private |
| 287 | @Deprecated |
Jonathan Hart | 0d18df3 | 2015-03-21 08:42:59 -0700 | [diff] [blame] | 288 | public IntentData(IntentData intentData) { |
| 289 | checkNotNull(intentData); |
| 290 | |
| 291 | intent = intentData.intent; |
| 292 | state = intentData.state; |
Brian O'Connor | f0c5a05 | 2015-04-27 00:34:53 -0700 | [diff] [blame] | 293 | request = intentData.request; |
Jonathan Hart | 0d18df3 | 2015-03-21 08:42:59 -0700 | [diff] [blame] | 294 | version = intentData.version; |
Yuta HIGUCHI | 4f8a377 | 2017-05-16 20:23:49 -0700 | [diff] [blame] | 295 | internalStateVersion = intentData.internalStateVersion; |
Jonathan Hart | 0d18df3 | 2015-03-21 08:42:59 -0700 | [diff] [blame] | 296 | origin = intentData.origin; |
| 297 | installables = intentData.installables; |
Brian O'Connor | 6d8e317 | 2015-04-30 15:43:57 -0700 | [diff] [blame] | 298 | errorCount = intentData.errorCount; |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 299 | } |
| 300 | |
Sho SHIMIZU | 2f26fb2 | 2016-01-19 14:04:46 -0800 | [diff] [blame] | 301 | /** |
| 302 | * Create a new instance based on the original instance with new installables. |
| 303 | * |
| 304 | * @param original original data |
| 305 | * @param installables new installable intents to set |
Yuta HIGUCHI | 4f8a377 | 2017-05-16 20:23:49 -0700 | [diff] [blame] | 306 | * |
| 307 | * @deprecated in 1.11.0 use {@link #compiled(IntentData, List)} instead |
Sho SHIMIZU | 2f26fb2 | 2016-01-19 14:04:46 -0800 | [diff] [blame] | 308 | */ |
Yuta HIGUCHI | 4f8a377 | 2017-05-16 20:23:49 -0700 | [diff] [blame] | 309 | // used to create an instance who reached stable state |
| 310 | // note that state is mutable field, so it gets altered else where |
| 311 | // (probably that design is mother of all intent bugs) |
| 312 | @Deprecated |
Sho SHIMIZU | 2f26fb2 | 2016-01-19 14:04:46 -0800 | [diff] [blame] | 313 | public IntentData(IntentData original, List<Intent> installables) { |
| 314 | this(original); |
Yuta HIGUCHI | 4f8a377 | 2017-05-16 20:23:49 -0700 | [diff] [blame] | 315 | this.internalStateVersion++; |
Sho SHIMIZU | 2f26fb2 | 2016-01-19 14:04:46 -0800 | [diff] [blame] | 316 | |
Brian O'Connor | a78f060 | 2016-09-22 10:56:08 -0700 | [diff] [blame] | 317 | this.installables = checkNotNull(installables).isEmpty() ? |
Yuta HIGUCHI | 4f8a377 | 2017-05-16 20:23:49 -0700 | [diff] [blame] | 318 | ImmutableList.of() : ImmutableList.copyOf(installables); |
Sho SHIMIZU | 2f26fb2 | 2016-01-19 14:04:46 -0800 | [diff] [blame] | 319 | } |
| 320 | |
Jonathan Hart | 0d18df3 | 2015-03-21 08:42:59 -0700 | [diff] [blame] | 321 | // kryo constructor |
| 322 | protected IntentData() { |
| 323 | intent = null; |
Brian O'Connor | f0c5a05 | 2015-04-27 00:34:53 -0700 | [diff] [blame] | 324 | request = null; |
Sho SHIMIZU | cbcc02b | 2016-03-31 13:22:42 -0700 | [diff] [blame] | 325 | version = null; |
Jonathan Hart | 0d18df3 | 2015-03-21 08:42:59 -0700 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | /** |
| 329 | * Returns the intent this metadata references. |
| 330 | * |
| 331 | * @return intent |
| 332 | */ |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 333 | public Intent intent() { |
| 334 | return intent; |
| 335 | } |
| 336 | |
Jonathan Hart | 0d18df3 | 2015-03-21 08:42:59 -0700 | [diff] [blame] | 337 | /** |
| 338 | * Returns the state of the intent. |
| 339 | * |
| 340 | * @return intent state |
| 341 | */ |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 342 | public IntentState state() { |
| 343 | return state; |
| 344 | } |
| 345 | |
Brian O'Connor | f0c5a05 | 2015-04-27 00:34:53 -0700 | [diff] [blame] | 346 | public IntentState request() { |
| 347 | return request; |
| 348 | } |
| 349 | |
Jonathan Hart | 0d18df3 | 2015-03-21 08:42:59 -0700 | [diff] [blame] | 350 | /** |
| 351 | * Returns the intent key. |
| 352 | * |
| 353 | * @return intent key |
| 354 | */ |
Ray Milkey | 5b3717e | 2015-02-05 11:44:08 -0800 | [diff] [blame] | 355 | public Key key() { |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 356 | return intent.key(); |
| 357 | } |
| 358 | |
Jonathan Hart | 0d18df3 | 2015-03-21 08:42:59 -0700 | [diff] [blame] | 359 | /** |
Yuta HIGUCHI | 4f8a377 | 2017-05-16 20:23:49 -0700 | [diff] [blame] | 360 | * Returns the request version of the intent for this key. |
Jonathan Hart | 0d18df3 | 2015-03-21 08:42:59 -0700 | [diff] [blame] | 361 | * |
| 362 | * @return intent version |
| 363 | */ |
Brian O'Connor | 2ba63fd | 2015-02-09 22:48:11 -0800 | [diff] [blame] | 364 | public Timestamp version() { |
| 365 | return version; |
| 366 | } |
| 367 | |
Yuta HIGUCHI | 4f8a377 | 2017-05-16 20:23:49 -0700 | [diff] [blame] | 368 | // had to be made public for the store timestamp provider |
| 369 | public int internalStateVersion() { |
| 370 | return internalStateVersion; |
| 371 | } |
| 372 | |
Brian O'Connor | 5eb77c8 | 2015-03-02 18:09:39 -0800 | [diff] [blame] | 373 | /** |
Jonathan Hart | 0d18df3 | 2015-03-21 08:42:59 -0700 | [diff] [blame] | 374 | * Returns the origin node that created this intent. |
| 375 | * |
| 376 | * @return origin node ID |
| 377 | */ |
Brian O'Connor | 5eb77c8 | 2015-03-02 18:09:39 -0800 | [diff] [blame] | 378 | public NodeId origin() { |
| 379 | return origin; |
| 380 | } |
| 381 | |
Jonathan Hart | 0d18df3 | 2015-03-21 08:42:59 -0700 | [diff] [blame] | 382 | /** |
| 383 | * Updates the state of the intent to the given new state. |
| 384 | * |
| 385 | * @param newState new state of the intent |
| 386 | */ |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 387 | public void setState(IntentState newState) { |
Yuta HIGUCHI | 4f8a377 | 2017-05-16 20:23:49 -0700 | [diff] [blame] | 388 | this.internalStateVersion++; |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 389 | this.state = newState; |
| 390 | } |
| 391 | |
Brian O'Connor | 2ba63fd | 2015-02-09 22:48:11 -0800 | [diff] [blame] | 392 | /** |
Brian O'Connor | 6d8e317 | 2015-04-30 15:43:57 -0700 | [diff] [blame] | 393 | * Increments the error count for this intent. |
| 394 | */ |
| 395 | public void incrementErrorCount() { |
| 396 | errorCount++; |
| 397 | } |
| 398 | |
| 399 | /** |
| 400 | * Sets the error count for this intent. |
| 401 | * |
| 402 | * @param newCount new count |
| 403 | */ |
| 404 | public void setErrorCount(int newCount) { |
| 405 | errorCount = newCount; |
| 406 | } |
| 407 | |
| 408 | /** |
| 409 | * Returns the number of times that this intent has encountered an error |
| 410 | * during installation or withdrawal. |
| 411 | * |
| 412 | * @return error count |
| 413 | */ |
| 414 | public int errorCount() { |
| 415 | return errorCount; |
| 416 | } |
| 417 | |
| 418 | /** |
Jonathan Hart | 0d18df3 | 2015-03-21 08:42:59 -0700 | [diff] [blame] | 419 | * Returns the installables associated with this intent. |
| 420 | * |
| 421 | * @return list of installable intents |
| 422 | */ |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 423 | public List<Intent> installables() { |
Brian O'Connor | f0c5a05 | 2015-04-27 00:34:53 -0700 | [diff] [blame] | 424 | return installables != null ? installables : Collections.emptyList(); |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 425 | } |
| 426 | |
Jonathan Hart | 72175c2 | 2015-03-24 18:55:58 -0700 | [diff] [blame] | 427 | /** |
| 428 | * Determines whether an intent data update is allowed. The update must |
| 429 | * either have a higher version than the current data, or the state |
| 430 | * transition between two updates of the same version must be sane. |
| 431 | * |
| 432 | * @param currentData existing intent data in the store |
| 433 | * @param newData new intent data update proposal |
| 434 | * @return true if we can apply the update, otherwise false |
| 435 | */ |
| 436 | public static boolean isUpdateAcceptable(IntentData currentData, IntentData newData) { |
| 437 | |
| 438 | if (currentData == null) { |
| 439 | return true; |
Sho SHIMIZU | 2c05f91 | 2015-04-10 14:23:16 -0700 | [diff] [blame] | 440 | } else if (currentData.version().isOlderThan(newData.version())) { |
Jonathan Hart | 72175c2 | 2015-03-24 18:55:58 -0700 | [diff] [blame] | 441 | return true; |
Sho SHIMIZU | 2c05f91 | 2015-04-10 14:23:16 -0700 | [diff] [blame] | 442 | } else if (currentData.version().isNewerThan(newData.version())) { |
Yuta HIGUCHI | f76f6d5 | 2017-05-15 18:02:09 -0700 | [diff] [blame] | 443 | log.trace("{} update not acceptable: current is newer", newData.key()); |
Jonathan Hart | 72175c2 | 2015-03-24 18:55:58 -0700 | [diff] [blame] | 444 | return false; |
| 445 | } |
| 446 | |
Yuta HIGUCHI | 4f8a377 | 2017-05-16 20:23:49 -0700 | [diff] [blame] | 447 | assert (currentData.version().equals(newData.version())); |
| 448 | if (currentData.internalStateVersion >= newData.internalStateVersion) { |
| 449 | log.trace("{} update not acceptable: current is newer internally", newData.key()); |
| 450 | return false; |
| 451 | } |
| 452 | |
Jonathan Hart | 72175c2 | 2015-03-24 18:55:58 -0700 | [diff] [blame] | 453 | // current and new data versions are the same |
| 454 | IntentState currentState = currentData.state(); |
| 455 | IntentState newState = newData.state(); |
| 456 | |
| 457 | switch (newState) { |
| 458 | case INSTALLING: |
| 459 | if (currentState == INSTALLING) { |
Yuta HIGUCHI | f76f6d5 | 2017-05-15 18:02:09 -0700 | [diff] [blame] | 460 | log.trace("{} update not acceptable: no-op INSTALLING", newData.key()); |
Jonathan Hart | 72175c2 | 2015-03-24 18:55:58 -0700 | [diff] [blame] | 461 | return false; |
| 462 | } |
| 463 | // FALLTHROUGH |
Antonio Marsico | 4f68ec9 | 2017-03-09 11:16:32 +0100 | [diff] [blame] | 464 | case REALLOCATING: |
| 465 | if (currentState == REALLOCATING) { |
| 466 | log.trace("{} update not acceptable: no-op REALLOCATING", newData.key()); |
| 467 | return false; |
| 468 | } else if (currentState == INSTALLED) { |
| 469 | return true; |
| 470 | } |
Ray Milkey | 7dac7da | 2017-08-01 16:56:05 -0700 | [diff] [blame] | 471 | // FALLTHROUGH |
Jonathan Hart | 72175c2 | 2015-03-24 18:55:58 -0700 | [diff] [blame] | 472 | case INSTALLED: |
| 473 | if (currentState == INSTALLED) { |
| 474 | return false; |
| 475 | } else if (currentState == WITHDRAWING || currentState == WITHDRAWN |
| 476 | || currentState == PURGE_REQ) { |
| 477 | log.warn("Invalid state transition from {} to {} for intent {}", |
| 478 | currentState, newState, newData.key()); |
| 479 | return false; |
| 480 | } |
| 481 | return true; |
| 482 | |
| 483 | case WITHDRAWING: |
| 484 | if (currentState == WITHDRAWING) { |
Yuta HIGUCHI | f76f6d5 | 2017-05-15 18:02:09 -0700 | [diff] [blame] | 485 | log.trace("{} update not acceptable: no-op WITHDRAWING", newData.key()); |
Jonathan Hart | 72175c2 | 2015-03-24 18:55:58 -0700 | [diff] [blame] | 486 | return false; |
| 487 | } |
| 488 | // FALLTHROUGH |
| 489 | case WITHDRAWN: |
| 490 | if (currentState == WITHDRAWN) { |
Yuta HIGUCHI | f76f6d5 | 2017-05-15 18:02:09 -0700 | [diff] [blame] | 491 | log.trace("{} update not acceptable: no-op WITHDRAWN", newData.key()); |
Jonathan Hart | 72175c2 | 2015-03-24 18:55:58 -0700 | [diff] [blame] | 492 | return false; |
| 493 | } else if (currentState == INSTALLING || currentState == INSTALLED |
| 494 | || currentState == PURGE_REQ) { |
| 495 | log.warn("Invalid state transition from {} to {} for intent {}", |
| 496 | currentState, newState, newData.key()); |
| 497 | return false; |
| 498 | } |
| 499 | return true; |
| 500 | |
| 501 | case FAILED: |
| 502 | if (currentState == FAILED) { |
Yuta HIGUCHI | f76f6d5 | 2017-05-15 18:02:09 -0700 | [diff] [blame] | 503 | log.trace("{} update not acceptable: no-op FAILED", newData.key()); |
Jonathan Hart | 72175c2 | 2015-03-24 18:55:58 -0700 | [diff] [blame] | 504 | return false; |
| 505 | } |
| 506 | return true; |
| 507 | |
Brian O'Connor | f0c5a05 | 2015-04-27 00:34:53 -0700 | [diff] [blame] | 508 | case CORRUPT: |
| 509 | if (currentState == CORRUPT) { |
Yuta HIGUCHI | f76f6d5 | 2017-05-15 18:02:09 -0700 | [diff] [blame] | 510 | log.trace("{} update not acceptable: no-op CORRUPT", newData.key()); |
Brian O'Connor | f0c5a05 | 2015-04-27 00:34:53 -0700 | [diff] [blame] | 511 | return false; |
| 512 | } |
| 513 | return true; |
| 514 | |
Jonathan Hart | 72175c2 | 2015-03-24 18:55:58 -0700 | [diff] [blame] | 515 | case PURGE_REQ: |
Brian O'Connor | 597934f | 2015-07-16 11:44:03 -0700 | [diff] [blame] | 516 | // TODO we should enforce that only WITHDRAWN intents can be purged |
Jonathan Hart | 72175c2 | 2015-03-24 18:55:58 -0700 | [diff] [blame] | 517 | return true; |
| 518 | |
| 519 | case COMPILING: |
| 520 | case RECOMPILING: |
| 521 | case INSTALL_REQ: |
| 522 | case WITHDRAW_REQ: |
| 523 | default: |
| 524 | log.warn("Invalid state {} for intent {}", newState, newData.key()); |
| 525 | return false; |
| 526 | } |
| 527 | } |
| 528 | |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 529 | @Override |
| 530 | public int hashCode() { |
| 531 | return Objects.hash(intent, version); |
| 532 | } |
| 533 | |
| 534 | @Override |
| 535 | public boolean equals(Object obj) { |
| 536 | if (this == obj) { |
| 537 | return true; |
| 538 | } |
| 539 | if (obj == null || getClass() != obj.getClass()) { |
| 540 | return false; |
| 541 | } |
| 542 | final IntentData other = (IntentData) obj; |
| 543 | return Objects.equals(this.intent, other.intent) |
| 544 | && Objects.equals(this.version, other.version); |
| 545 | } |
Ray Milkey | 65cb59a | 2015-02-10 15:18:26 -0800 | [diff] [blame] | 546 | |
Jonathan Hart | 0d18df3 | 2015-03-21 08:42:59 -0700 | [diff] [blame] | 547 | @Override |
Ray Milkey | 65cb59a | 2015-02-10 15:18:26 -0800 | [diff] [blame] | 548 | public String toString() { |
| 549 | return MoreObjects.toStringHelper(getClass()) |
| 550 | .add("key", key()) |
| 551 | .add("state", state()) |
| 552 | .add("version", version()) |
Yuta HIGUCHI | 4f8a377 | 2017-05-16 20:23:49 -0700 | [diff] [blame] | 553 | .add("internalStateVersion", internalStateVersion) |
Ray Milkey | 9f74c08 | 2015-02-11 15:40:16 -0800 | [diff] [blame] | 554 | .add("intent", intent()) |
Jonathan Hart | 0d18df3 | 2015-03-21 08:42:59 -0700 | [diff] [blame] | 555 | .add("origin", origin()) |
Jonathan Hart | d0ba217 | 2015-02-11 13:54:33 -0800 | [diff] [blame] | 556 | .add("installables", installables()) |
Ray Milkey | 65cb59a | 2015-02-10 15:18:26 -0800 | [diff] [blame] | 557 | .toString(); |
| 558 | } |
| 559 | |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 560 | } |