Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Open Networking Laboratory |
| 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 | 1c5ed0e | 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 | 1c5ed0e | 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; |
| 47 | private 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) { |
| 61 | this.intent = intent; |
| 62 | this.state = state; |
Brian O'Connor | f0c5a05 | 2015-04-27 00:34:53 -0700 | [diff] [blame] | 63 | this.request = state; |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 64 | this.version = version; |
| 65 | } |
| 66 | |
Jonathan Hart | 0d18df3 | 2015-03-21 08:42:59 -0700 | [diff] [blame] | 67 | /** |
| 68 | * Copy constructor. |
| 69 | * |
| 70 | * @param intentData intent data to copy |
| 71 | */ |
| 72 | public IntentData(IntentData intentData) { |
| 73 | checkNotNull(intentData); |
| 74 | |
| 75 | intent = intentData.intent; |
| 76 | state = intentData.state; |
Brian O'Connor | f0c5a05 | 2015-04-27 00:34:53 -0700 | [diff] [blame] | 77 | request = intentData.request; |
Jonathan Hart | 0d18df3 | 2015-03-21 08:42:59 -0700 | [diff] [blame] | 78 | version = intentData.version; |
| 79 | origin = intentData.origin; |
| 80 | installables = intentData.installables; |
Brian O'Connor | 6d8e317 | 2015-04-30 15:43:57 -0700 | [diff] [blame] | 81 | errorCount = intentData.errorCount; |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 82 | } |
| 83 | |
Jonathan Hart | 0d18df3 | 2015-03-21 08:42:59 -0700 | [diff] [blame] | 84 | // kryo constructor |
| 85 | protected IntentData() { |
| 86 | intent = null; |
Brian O'Connor | f0c5a05 | 2015-04-27 00:34:53 -0700 | [diff] [blame] | 87 | request = null; |
Jonathan Hart | 0d18df3 | 2015-03-21 08:42:59 -0700 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Returns the intent this metadata references. |
| 92 | * |
| 93 | * @return intent |
| 94 | */ |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 95 | public Intent intent() { |
| 96 | return intent; |
| 97 | } |
| 98 | |
Jonathan Hart | 0d18df3 | 2015-03-21 08:42:59 -0700 | [diff] [blame] | 99 | /** |
| 100 | * Returns the state of the intent. |
| 101 | * |
| 102 | * @return intent state |
| 103 | */ |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 104 | public IntentState state() { |
| 105 | return state; |
| 106 | } |
| 107 | |
Brian O'Connor | f0c5a05 | 2015-04-27 00:34:53 -0700 | [diff] [blame] | 108 | public IntentState request() { |
| 109 | return request; |
| 110 | } |
| 111 | |
Jonathan Hart | 0d18df3 | 2015-03-21 08:42:59 -0700 | [diff] [blame] | 112 | /** |
| 113 | * Returns the intent key. |
| 114 | * |
| 115 | * @return intent key |
| 116 | */ |
Ray Milkey | 5b3717e | 2015-02-05 11:44:08 -0800 | [diff] [blame] | 117 | public Key key() { |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 118 | return intent.key(); |
| 119 | } |
| 120 | |
Jonathan Hart | 0d18df3 | 2015-03-21 08:42:59 -0700 | [diff] [blame] | 121 | /** |
| 122 | * Returns the version of the intent for this key. |
| 123 | * |
| 124 | * @return intent version |
| 125 | */ |
Brian O'Connor | 2ba63fd | 2015-02-09 22:48:11 -0800 | [diff] [blame] | 126 | public Timestamp version() { |
| 127 | return version; |
| 128 | } |
| 129 | |
Brian O'Connor | 5eb77c8 | 2015-03-02 18:09:39 -0800 | [diff] [blame] | 130 | /** |
Jonathan Hart | 0d18df3 | 2015-03-21 08:42:59 -0700 | [diff] [blame] | 131 | * Sets the origin, which is the node that created the intent. |
Brian O'Connor | 5eb77c8 | 2015-03-02 18:09:39 -0800 | [diff] [blame] | 132 | * |
| 133 | * @param origin origin instance |
| 134 | */ |
| 135 | public void setOrigin(NodeId origin) { |
| 136 | this.origin = origin; |
| 137 | } |
| 138 | |
Jonathan Hart | 0d18df3 | 2015-03-21 08:42:59 -0700 | [diff] [blame] | 139 | /** |
| 140 | * Returns the origin node that created this intent. |
| 141 | * |
| 142 | * @return origin node ID |
| 143 | */ |
Brian O'Connor | 5eb77c8 | 2015-03-02 18:09:39 -0800 | [diff] [blame] | 144 | public NodeId origin() { |
| 145 | return origin; |
| 146 | } |
| 147 | |
Jonathan Hart | 0d18df3 | 2015-03-21 08:42:59 -0700 | [diff] [blame] | 148 | /** |
| 149 | * Updates the state of the intent to the given new state. |
| 150 | * |
| 151 | * @param newState new state of the intent |
| 152 | */ |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 153 | public void setState(IntentState newState) { |
| 154 | this.state = newState; |
| 155 | } |
| 156 | |
Brian O'Connor | 2ba63fd | 2015-02-09 22:48:11 -0800 | [diff] [blame] | 157 | /** |
| 158 | * Sets the version for this intent data. |
| 159 | * <p> |
| 160 | * The store should call this method only once when the IntentData is |
| 161 | * first passed into the pending map. Ideally, an IntentData is timestamped |
| 162 | * on the same thread that the called used to submit the intents. |
| 163 | * </p> |
| 164 | * |
| 165 | * @param version the version/timestamp for this intent data |
| 166 | */ |
| 167 | public void setVersion(Timestamp version) { |
| 168 | this.version = version; |
| 169 | } |
| 170 | |
Jonathan Hart | 0d18df3 | 2015-03-21 08:42:59 -0700 | [diff] [blame] | 171 | /** |
Brian O'Connor | 6d8e317 | 2015-04-30 15:43:57 -0700 | [diff] [blame] | 172 | * Increments the error count for this intent. |
| 173 | */ |
| 174 | public void incrementErrorCount() { |
| 175 | errorCount++; |
| 176 | } |
| 177 | |
| 178 | /** |
| 179 | * Sets the error count for this intent. |
| 180 | * |
| 181 | * @param newCount new count |
| 182 | */ |
| 183 | public void setErrorCount(int newCount) { |
| 184 | errorCount = newCount; |
| 185 | } |
| 186 | |
| 187 | /** |
| 188 | * Returns the number of times that this intent has encountered an error |
| 189 | * during installation or withdrawal. |
| 190 | * |
| 191 | * @return error count |
| 192 | */ |
| 193 | public int errorCount() { |
| 194 | return errorCount; |
| 195 | } |
| 196 | |
| 197 | /** |
Jonathan Hart | 0d18df3 | 2015-03-21 08:42:59 -0700 | [diff] [blame] | 198 | * Sets the intent installables to the given list of intents. |
| 199 | * |
| 200 | * @param installables list of installables for this intent |
| 201 | */ |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 202 | public void setInstallables(List<Intent> installables) { |
| 203 | this.installables = ImmutableList.copyOf(installables); |
| 204 | } |
| 205 | |
Jonathan Hart | 0d18df3 | 2015-03-21 08:42:59 -0700 | [diff] [blame] | 206 | /** |
| 207 | * Returns the installables associated with this intent. |
| 208 | * |
| 209 | * @return list of installable intents |
| 210 | */ |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 211 | public List<Intent> installables() { |
Brian O'Connor | f0c5a05 | 2015-04-27 00:34:53 -0700 | [diff] [blame] | 212 | return installables != null ? installables : Collections.emptyList(); |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 213 | } |
| 214 | |
Jonathan Hart | 72175c2 | 2015-03-24 18:55:58 -0700 | [diff] [blame] | 215 | /** |
| 216 | * Determines whether an intent data update is allowed. The update must |
| 217 | * either have a higher version than the current data, or the state |
| 218 | * transition between two updates of the same version must be sane. |
| 219 | * |
| 220 | * @param currentData existing intent data in the store |
| 221 | * @param newData new intent data update proposal |
| 222 | * @return true if we can apply the update, otherwise false |
| 223 | */ |
| 224 | public static boolean isUpdateAcceptable(IntentData currentData, IntentData newData) { |
| 225 | |
| 226 | if (currentData == null) { |
| 227 | return true; |
Sho SHIMIZU | 2c05f91 | 2015-04-10 14:23:16 -0700 | [diff] [blame] | 228 | } else if (currentData.version().isOlderThan(newData.version())) { |
Jonathan Hart | 72175c2 | 2015-03-24 18:55:58 -0700 | [diff] [blame] | 229 | return true; |
Sho SHIMIZU | 2c05f91 | 2015-04-10 14:23:16 -0700 | [diff] [blame] | 230 | } else if (currentData.version().isNewerThan(newData.version())) { |
Jonathan Hart | 72175c2 | 2015-03-24 18:55:58 -0700 | [diff] [blame] | 231 | return false; |
| 232 | } |
| 233 | |
| 234 | // current and new data versions are the same |
| 235 | IntentState currentState = currentData.state(); |
| 236 | IntentState newState = newData.state(); |
| 237 | |
| 238 | switch (newState) { |
| 239 | case INSTALLING: |
| 240 | if (currentState == INSTALLING) { |
| 241 | return false; |
| 242 | } |
| 243 | // FALLTHROUGH |
| 244 | case INSTALLED: |
| 245 | if (currentState == INSTALLED) { |
| 246 | return false; |
| 247 | } else if (currentState == WITHDRAWING || currentState == WITHDRAWN |
| 248 | || currentState == PURGE_REQ) { |
| 249 | log.warn("Invalid state transition from {} to {} for intent {}", |
| 250 | currentState, newState, newData.key()); |
| 251 | return false; |
| 252 | } |
| 253 | return true; |
| 254 | |
| 255 | case WITHDRAWING: |
| 256 | if (currentState == WITHDRAWING) { |
| 257 | return false; |
| 258 | } |
| 259 | // FALLTHROUGH |
| 260 | case WITHDRAWN: |
| 261 | if (currentState == WITHDRAWN) { |
| 262 | return false; |
| 263 | } else if (currentState == INSTALLING || currentState == INSTALLED |
| 264 | || currentState == PURGE_REQ) { |
| 265 | log.warn("Invalid state transition from {} to {} for intent {}", |
| 266 | currentState, newState, newData.key()); |
| 267 | return false; |
| 268 | } |
| 269 | return true; |
| 270 | |
| 271 | case FAILED: |
| 272 | if (currentState == FAILED) { |
| 273 | return false; |
| 274 | } |
| 275 | return true; |
| 276 | |
Brian O'Connor | f0c5a05 | 2015-04-27 00:34:53 -0700 | [diff] [blame] | 277 | case CORRUPT: |
| 278 | if (currentState == CORRUPT) { |
| 279 | return false; |
| 280 | } |
| 281 | return true; |
| 282 | |
Jonathan Hart | 72175c2 | 2015-03-24 18:55:58 -0700 | [diff] [blame] | 283 | case PURGE_REQ: |
| 284 | return true; |
| 285 | |
| 286 | case COMPILING: |
| 287 | case RECOMPILING: |
| 288 | case INSTALL_REQ: |
| 289 | case WITHDRAW_REQ: |
| 290 | default: |
| 291 | log.warn("Invalid state {} for intent {}", newState, newData.key()); |
| 292 | return false; |
| 293 | } |
| 294 | } |
| 295 | |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 296 | @Override |
| 297 | public int hashCode() { |
| 298 | return Objects.hash(intent, version); |
| 299 | } |
| 300 | |
| 301 | @Override |
| 302 | public boolean equals(Object obj) { |
| 303 | if (this == obj) { |
| 304 | return true; |
| 305 | } |
| 306 | if (obj == null || getClass() != obj.getClass()) { |
| 307 | return false; |
| 308 | } |
| 309 | final IntentData other = (IntentData) obj; |
| 310 | return Objects.equals(this.intent, other.intent) |
| 311 | && Objects.equals(this.version, other.version); |
| 312 | } |
Ray Milkey | 65cb59a | 2015-02-10 15:18:26 -0800 | [diff] [blame] | 313 | |
Jonathan Hart | 0d18df3 | 2015-03-21 08:42:59 -0700 | [diff] [blame] | 314 | @Override |
Ray Milkey | 65cb59a | 2015-02-10 15:18:26 -0800 | [diff] [blame] | 315 | public String toString() { |
| 316 | return MoreObjects.toStringHelper(getClass()) |
| 317 | .add("key", key()) |
| 318 | .add("state", state()) |
| 319 | .add("version", version()) |
Ray Milkey | 9f74c08 | 2015-02-11 15:40:16 -0800 | [diff] [blame] | 320 | .add("intent", intent()) |
Jonathan Hart | 0d18df3 | 2015-03-21 08:42:59 -0700 | [diff] [blame] | 321 | .add("origin", origin()) |
Jonathan Hart | d0ba217 | 2015-02-11 13:54:33 -0800 | [diff] [blame] | 322 | .add("installables", installables()) |
Ray Milkey | 65cb59a | 2015-02-10 15:18:26 -0800 | [diff] [blame] | 323 | .toString(); |
| 324 | } |
| 325 | |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 326 | } |