Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019-present Open Networking Foundation |
| 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 | |
| 17 | package org.onosproject.p4runtime.api; |
| 18 | |
| 19 | import org.onosproject.net.pi.model.PiPipeconf; |
| 20 | import org.onosproject.net.pi.runtime.PiEntity; |
| 21 | import org.onosproject.net.pi.runtime.PiEntityType; |
| 22 | import org.onosproject.net.pi.runtime.PiHandle; |
| 23 | |
| 24 | import java.util.Collection; |
| 25 | import java.util.concurrent.CompletableFuture; |
| 26 | |
| 27 | /** |
| 28 | * P4Runtime client interface for the Write RPC that allows inserting, modifying |
| 29 | * and deleting PI entities. Allows batching of write requests and it returns a |
| 30 | * detailed response for each PI entity in the request. |
| 31 | */ |
| 32 | public interface P4RuntimeWriteClient { |
| 33 | |
| 34 | /** |
| 35 | * Returns a new {@link WriteRequest} instance that can be used to build a |
| 36 | * batched write request, for the given pipeconf. |
| 37 | * |
| 38 | * @param pipeconf pipeconf |
| 39 | * @return new write request |
| 40 | */ |
| 41 | WriteRequest write(PiPipeconf pipeconf); |
| 42 | |
| 43 | /** |
| 44 | * Signals the type of write operation for a given PI entity. |
| 45 | */ |
| 46 | enum UpdateType { |
| 47 | /** |
| 48 | * Inserts an entity. |
| 49 | */ |
| 50 | INSERT, |
| 51 | /** |
| 52 | * Modifies an existing entity. |
| 53 | */ |
| 54 | MODIFY, |
| 55 | /** |
| 56 | * Deletes an existing entity. |
| 57 | */ |
| 58 | DELETE |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Signals if the entity was written successfully or not. |
| 63 | */ |
Carmelo Cascone | 6146946 | 2019-03-05 23:59:11 -0800 | [diff] [blame^] | 64 | enum EntityUpdateStatus { |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 65 | /** |
| 66 | * The entity was written successfully, no errors occurred. |
| 67 | */ |
| 68 | OK, |
| 69 | /** |
| 70 | * The server didn't return any status for the entity. |
| 71 | */ |
| 72 | PENDING, |
| 73 | /** |
| 74 | * The entity was not added to the write request because it was not |
| 75 | * possible to encode/decode it. |
| 76 | */ |
| 77 | CODEC_ERROR, |
| 78 | /** |
| 79 | * Server responded that it was not possible to insert the entity as |
| 80 | * another one with the same handle already exists. |
| 81 | */ |
| 82 | ALREADY_EXIST, |
| 83 | /** |
| 84 | * Server responded that it was not possible to modify or delete the |
| 85 | * entity as the same cannot be found on the server. |
| 86 | */ |
| 87 | NOT_FOUND, |
| 88 | /** |
Carmelo Cascone | 6146946 | 2019-03-05 23:59:11 -0800 | [diff] [blame^] | 89 | * Other error. See {@link EntityUpdateResponse#explanation()} or {@link |
| 90 | * EntityUpdateResponse#throwable()} for more details. |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 91 | */ |
| 92 | OTHER_ERROR, |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Signals the atomicity mode that the server should follow when executing a |
| 97 | * write request. For more information on each atomicity mode please see the |
| 98 | * P4Runtime spec. |
| 99 | */ |
| 100 | enum Atomicity { |
| 101 | /** |
| 102 | * Continue on error. Default value for all write requests. |
| 103 | */ |
| 104 | CONTINUE_ON_ERROR, |
| 105 | /** |
| 106 | * Rollback on error. |
| 107 | */ |
| 108 | ROLLBACK_ON_ERROR, |
| 109 | /** |
| 110 | * Dataplane atomic. |
| 111 | */ |
| 112 | DATAPLANE_ATOMIC, |
| 113 | } |
| 114 | |
| 115 | /** |
Carmelo Cascone | 6146946 | 2019-03-05 23:59:11 -0800 | [diff] [blame^] | 116 | * Abstraction of a batched P4Runtime write request. Multiple entities can |
| 117 | * be added to the same request before submitting it. The implementation |
| 118 | * should guarantee that entities are added in the final P4Runtime protobuf |
| 119 | * message in the same order as added in this write request. |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 120 | */ |
| 121 | interface WriteRequest { |
| 122 | |
| 123 | /** |
| 124 | * Sets the atomicity mode of this write request. Default value is |
| 125 | * {@link Atomicity#CONTINUE_ON_ERROR}. |
| 126 | * |
| 127 | * @param atomicity atomicity mode |
| 128 | * @return this |
| 129 | */ |
| 130 | WriteRequest withAtomicity(Atomicity atomicity); |
| 131 | |
| 132 | /** |
| 133 | * Requests to insert one PI entity. |
| 134 | * |
| 135 | * @param entity PI entity |
| 136 | * @return this |
| 137 | */ |
| 138 | WriteRequest insert(PiEntity entity); |
| 139 | |
| 140 | /** |
| 141 | * Requests to insert multiple PI entities. |
| 142 | * |
| 143 | * @param entities iterable of PI entities |
| 144 | * @return this |
| 145 | */ |
| 146 | WriteRequest insert(Iterable<? extends PiEntity> entities); |
| 147 | |
| 148 | /** |
| 149 | * Requests to modify one PI entity. |
| 150 | * |
| 151 | * @param entity PI entity |
| 152 | * @return this |
| 153 | */ |
| 154 | WriteRequest modify(PiEntity entity); |
| 155 | |
| 156 | /** |
| 157 | * Requests to modify multiple PI entities. |
| 158 | * |
| 159 | * @param entities iterable of PI entities |
| 160 | * @return this |
| 161 | */ |
| 162 | WriteRequest modify(Iterable<? extends PiEntity> entities); |
| 163 | |
| 164 | /** |
| 165 | * Requests to delete one PI entity identified by the given handle. |
| 166 | * |
| 167 | * @param handle PI handle |
| 168 | * @return this |
| 169 | */ |
| 170 | WriteRequest delete(PiHandle handle); |
| 171 | |
| 172 | /** |
| 173 | * Requests to delete multiple PI entities identified by the given |
| 174 | * handles. |
| 175 | * |
| 176 | * @param handles iterable of handles |
| 177 | * @return this |
| 178 | */ |
| 179 | WriteRequest delete(Iterable<? extends PiHandle> handles); |
| 180 | |
| 181 | /** |
| 182 | * Requests to write the given PI entity with the given update type. If |
| 183 | * {@code updateType} is {@link UpdateType#DELETE}, then only the handle |
| 184 | * will be considered by the request. |
| 185 | * |
| 186 | * @param entity PI entity |
| 187 | * @param updateType update type |
| 188 | * @return this |
| 189 | */ |
| 190 | WriteRequest entity(PiEntity entity, UpdateType updateType); |
| 191 | |
| 192 | /** |
| 193 | * Requests to write the given PI entities with the given update type. |
| 194 | * If {@code updateType} is {@link UpdateType#DELETE}, then only the |
| 195 | * handles will be considered by the request. |
| 196 | * |
| 197 | * @param entities iterable of PI entity |
| 198 | * @param updateType update type |
| 199 | * @return this |
| 200 | */ |
| 201 | WriteRequest entities(Iterable<? extends PiEntity> entities, UpdateType updateType); |
| 202 | |
| 203 | /** |
| 204 | * Submits this write request to the server and returns a completable |
| 205 | * future holding the response. The future is completed only after the |
| 206 | * server signals that all entities are written. |
| 207 | * |
| 208 | * @return completable future of the write response |
| 209 | */ |
| 210 | CompletableFuture<WriteResponse> submit(); |
| 211 | |
| 212 | /** |
| 213 | * Similar to {@link #submit()}, but blocks until the operation is |
| 214 | * completed, after which, it returns a read response. |
| 215 | * |
| 216 | * @return read response |
| 217 | */ |
| 218 | P4RuntimeWriteClient.WriteResponse submitSync(); |
Carmelo Cascone | 6146946 | 2019-03-05 23:59:11 -0800 | [diff] [blame^] | 219 | |
| 220 | /** |
| 221 | * Returns all entity update requests for which we are expecting a |
| 222 | * responce from the device, in the same order they were added to this |
| 223 | * batch. |
| 224 | * |
| 225 | * |
| 226 | * |
| 227 | * @return entity update requests |
| 228 | */ |
| 229 | Collection<EntityUpdateRequest> pendingUpdates(); |
| 230 | } |
| 231 | |
| 232 | /** |
| 233 | * Represents the update request for a specific entity. |
| 234 | */ |
| 235 | interface EntityUpdateRequest { |
| 236 | /** |
| 237 | * Returns the handle of the PI entity subject of this update. |
| 238 | * |
| 239 | * @return handle |
| 240 | */ |
| 241 | PiHandle handle(); |
| 242 | |
| 243 | /** |
| 244 | * Returns the PI entity subject of this update. Returns {@code null} if |
| 245 | * the update type is {@link UpdateType#DELETE}, in which case only the |
| 246 | * handle is used in the request. |
| 247 | * |
| 248 | * @return PI entity or null |
| 249 | */ |
| 250 | PiEntity entity(); |
| 251 | |
| 252 | /** |
| 253 | * Returns the type of update requested for this entity. |
| 254 | * |
| 255 | * @return update type |
| 256 | */ |
| 257 | UpdateType updateType(); |
| 258 | |
| 259 | /** |
| 260 | * Returns the type of entity subject of this update. |
| 261 | * |
| 262 | * @return PI entity type |
| 263 | */ |
| 264 | PiEntityType entityType(); |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | /** |
| 268 | * Abstraction of a response obtained from a P4Runtime server after a write |
| 269 | * request is submitted. It allows returning a detailed response ({@link |
Carmelo Cascone | 6146946 | 2019-03-05 23:59:11 -0800 | [diff] [blame^] | 270 | * EntityUpdateResponse}) for each PI entity in the batched request. Entity |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 271 | * responses are guaranteed to be returned in the same order as the |
| 272 | * corresponding PI entity in the request. |
| 273 | */ |
| 274 | interface WriteResponse { |
| 275 | |
| 276 | /** |
| 277 | * Returns true if all entities in the request were successfully |
| 278 | * written. In other words, if no errors occurred. False otherwise. |
| 279 | * |
| 280 | * @return true if all entities were written successfully, false |
| 281 | * otherwise |
| 282 | */ |
| 283 | boolean isSuccess(); |
| 284 | |
| 285 | /** |
| 286 | * Returns a detailed response for each PI entity in the request. The |
| 287 | * implementation of this method should guarantee that the returned |
| 288 | * collection has size equal to the number of PI entities in the |
| 289 | * original write request. |
| 290 | * |
Carmelo Cascone | 6146946 | 2019-03-05 23:59:11 -0800 | [diff] [blame^] | 291 | * @return collection of {@link EntityUpdateResponse} |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 292 | */ |
Carmelo Cascone | 6146946 | 2019-03-05 23:59:11 -0800 | [diff] [blame^] | 293 | Collection<EntityUpdateResponse> all(); |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 294 | |
| 295 | /** |
| 296 | * Returns a detailed response for each PI entity that was successfully |
| 297 | * written. If {@link #isSuccess()} is {@code true}, then this method is |
| 298 | * expected to return the same values as {@link #all()}. |
| 299 | * |
Carmelo Cascone | 6146946 | 2019-03-05 23:59:11 -0800 | [diff] [blame^] | 300 | * @return collection of {@link EntityUpdateResponse} |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 301 | */ |
Carmelo Cascone | 6146946 | 2019-03-05 23:59:11 -0800 | [diff] [blame^] | 302 | Collection<EntityUpdateResponse> success(); |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 303 | |
| 304 | /** |
| 305 | * Returns a detailed response for each PI entity for which the server |
| 306 | * returned an error. If {@link #isSuccess()} is {@code true}, then this |
| 307 | * method is expected to return an empty collection. |
| 308 | * |
Carmelo Cascone | 6146946 | 2019-03-05 23:59:11 -0800 | [diff] [blame^] | 309 | * @return collection of {@link EntityUpdateResponse} |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 310 | */ |
Carmelo Cascone | 6146946 | 2019-03-05 23:59:11 -0800 | [diff] [blame^] | 311 | Collection<EntityUpdateResponse> failed(); |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 312 | |
| 313 | /** |
| 314 | * Returns a detailed response for each PI entity for which the server |
| 315 | * returned the given status. |
| 316 | * |
| 317 | * @param status status |
Carmelo Cascone | 6146946 | 2019-03-05 23:59:11 -0800 | [diff] [blame^] | 318 | * @return collection of {@link EntityUpdateResponse} |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 319 | */ |
Carmelo Cascone | 6146946 | 2019-03-05 23:59:11 -0800 | [diff] [blame^] | 320 | Collection<EntityUpdateResponse> status(EntityUpdateStatus status); |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | /** |
Carmelo Cascone | 6146946 | 2019-03-05 23:59:11 -0800 | [diff] [blame^] | 324 | * Represents the response to an update request request for a specific PI |
| 325 | * entity. |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 326 | */ |
Carmelo Cascone | 6146946 | 2019-03-05 23:59:11 -0800 | [diff] [blame^] | 327 | interface EntityUpdateResponse extends EntityUpdateRequest { |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 328 | |
| 329 | /** |
| 330 | * Returns true if this PI entity was written successfully, false |
| 331 | * otherwise. |
| 332 | * |
| 333 | * @return true if this PI entity was written successfully, false |
| 334 | * otherwise |
| 335 | */ |
| 336 | boolean isSuccess(); |
| 337 | |
| 338 | /** |
| 339 | * Returns the status for this PI entity. If {@link #isSuccess()} |
| 340 | * returns {@code true}, then this method is expected to return {@link |
Carmelo Cascone | 6146946 | 2019-03-05 23:59:11 -0800 | [diff] [blame^] | 341 | * EntityUpdateStatus#OK}. If {@link EntityUpdateStatus#OTHER_ERROR} |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 342 | * is returned, further details might be provided in {@link |
| 343 | * #explanation()} and {@link #throwable()}. |
| 344 | * |
| 345 | * @return status |
| 346 | */ |
Carmelo Cascone | 6146946 | 2019-03-05 23:59:11 -0800 | [diff] [blame^] | 347 | EntityUpdateStatus status(); |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 348 | |
| 349 | /** |
| 350 | * If the PI entity was NOT written successfully, this method returns a |
| 351 | * message explaining the error occurred. Returns an empty string if |
| 352 | * such message is not available, or {@code null} if no errors |
| 353 | * occurred. |
| 354 | * |
| 355 | * @return error explanation or empty string or null |
| 356 | */ |
| 357 | String explanation(); |
| 358 | |
| 359 | /** |
| 360 | * If the PI entity was NOT written successfully, this method returns |
| 361 | * the internal throwable instance associated with the error (e.g. a |
| 362 | * {@link io.grpc.StatusRuntimeException} instance). Returns null if |
| 363 | * such throwable instance is not available or if no errors occurred. |
| 364 | * |
| 365 | * @return throwable instance associated with this PI entity |
| 366 | */ |
| 367 | Throwable throwable(); |
| 368 | } |
| 369 | } |