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